<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Ecommerce\PriceSystem\Event;
use App\Model\Shop\Event\EventProduct;
use App\Service\Shop\PriceFormatter;
use Carbon\Carbon;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\AbstractPriceInfo;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\Price;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
class PriceInfo extends AbstractPriceInfo implements PriceInfoInterface
{
/** @var Price[] */
protected array $prices;
protected Price $price;
protected int $discount = 0;
protected int $baseAmount = 0;
protected ?Carbon $date = null;
public function __construct(EventProduct $product)
{
$this->product = $product;
}
/**
* @return Carbon|null
*/
public function getDate(): ?Carbon
{
return $this->date;
}
/**
* @param Carbon|null $date
*/
public function setDate(?Carbon $date): void
{
$this->date = $date;
}
/**
* @return int
*/
public function getDiscount(): int
{
return $this->discount;
}
/**
* @param int $discount
*
* @return void
*/
public function setDiscount(int $discount): void
{
$this->discount = $discount;
}
public function hasDiscount(): bool
{
return $this->getDiscount() > 0;
}
/**
* Calculate yielding price
*
* @param float $price
*
* @return float
*/
protected function getYieldPrice(float $price): float
{
if ($this->getDiscount() > 0) {
$price *= (100 - $this->getDiscount()) / 100;
$price = PriceFormatter::round5Rp($price); // round to 0.05
}
return $price;
}
/**
* Get prices without yielding
*
* @return Price[]
*/
public function getBasePrices(): array
{
return $this->getPrices(false);
}
/**
* Get prices from shopPrice descriptions
*
* @param bool $includeYielding
*
* @return Price[]
*/
public function getPrices($includeYielding = true): array
{
if ($includeYielding && $this->hasDiscount()) {
$yieldPrices = [];
foreach ($this->prices as $id => $price) {
$yieldPrices[$id] = new Price(Decimal::fromNumeric($this->getYieldPrice($price->getAmount()->asNumeric())), $price->getCurrency());
}
return $yieldPrices;
}
return $this->prices;
}
/**
* @param Price[] $prices
*/
public function setPrices(array $prices): void
{
$this->prices = $prices;
}
/**
* @return Price
*/
public function getPrice(): PriceInterface
{
if ($this->hasDiscount()) {
return new Price(Decimal::fromNumeric($this->getYieldPrice($this->price->getAmount()->asNumeric())), $this->price->getCurrency());
}
return $this->price;
}
/**
* @param Price $price
*/
public function setPrice(Price $price): void
{
$this->price = $price;
}
/**
* @return PriceInterface
*/
public function getTotalBasePrice(): PriceInterface
{
return new Price(Decimal::fromNumeric($this->getBaseAmount() * $this->getQuantity()), $this->price->getCurrency(), false);
}
/**
* @return PriceInterface
*/
public function getTotalPrice(): PriceInterface
{
return new Price(Decimal::fromNumeric($this->getPrice()->getAmount()->asNumeric() * $this->getQuantity()), $this->getPrice()->getCurrency(), false);
}
public function getBaseAmount(): int
{
return $this->baseAmount;
}
public function setBaseAmount(int $baseAmount): void
{
$this->baseAmount = $baseAmount;
}
}