src/Ecommerce/PriceSystem/Event/PriceInfo.php line 173

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Ecommerce\PriceSystem\Event;
  7. use App\Model\Shop\Event\EventProduct;
  8. use App\Service\Shop\PriceFormatter;
  9. use Carbon\Carbon;
  10. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\AbstractPriceInfo;
  11. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\Price;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface;
  14. use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
  15. class PriceInfo extends AbstractPriceInfo implements PriceInfoInterface
  16. {
  17.     /** @var Price[] */
  18.     protected array $prices;
  19.     protected Price $price;
  20.     protected int $discount 0;
  21.     protected int $baseAmount 0;
  22.     protected ?Carbon $date null;
  23.     public function __construct(EventProduct $product)
  24.     {
  25.         $this->product $product;
  26.     }
  27.     /**
  28.      * @return Carbon|null
  29.      */
  30.     public function getDate(): ?Carbon
  31.     {
  32.         return $this->date;
  33.     }
  34.     /**
  35.      * @param Carbon|null $date
  36.      */
  37.     public function setDate(?Carbon $date): void
  38.     {
  39.         $this->date $date;
  40.     }
  41.     /**
  42.      * @return int
  43.      */
  44.     public function getDiscount(): int
  45.     {
  46.         return $this->discount;
  47.     }
  48.     /**
  49.      * @param int $discount
  50.      *
  51.      * @return void
  52.      */
  53.     public function setDiscount(int $discount): void
  54.     {
  55.         $this->discount $discount;
  56.     }
  57.     public function hasDiscount(): bool
  58.     {
  59.         return $this->getDiscount() > 0;
  60.     }
  61.     /**
  62.      * Calculate yielding price
  63.      *
  64.      * @param float $price
  65.      *
  66.      * @return float
  67.      */
  68.     protected function getYieldPrice(float $price): float
  69.     {
  70.         if ($this->getDiscount() > 0) {
  71.             $price *= (100 $this->getDiscount()) / 100;
  72.             $price PriceFormatter::round5Rp($price); // round to 0.05
  73.         }
  74.         return $price;
  75.     }
  76.     /**
  77.      * Get prices without yielding
  78.      *
  79.      * @return Price[]
  80.      */
  81.     public function getBasePrices(): array
  82.     {
  83.         return $this->getPrices(false);
  84.     }
  85.     /**
  86.      * Get prices from shopPrice descriptions
  87.      *
  88.      * @param bool $includeYielding
  89.      *
  90.      * @return Price[]
  91.      */
  92.     public function getPrices($includeYielding true): array
  93.     {
  94.         if ($includeYielding && $this->hasDiscount()) {
  95.             $yieldPrices = [];
  96.             foreach ($this->prices as $id => $price) {
  97.                 $yieldPrices[$id] = new Price(Decimal::fromNumeric($this->getYieldPrice($price->getAmount()->asNumeric())), $price->getCurrency());
  98.             }
  99.             return $yieldPrices;
  100.         }
  101.         return $this->prices;
  102.     }
  103.     /**
  104.      * @param Price[] $prices
  105.      */
  106.     public function setPrices(array $prices): void
  107.     {
  108.         $this->prices $prices;
  109.     }
  110.     /**
  111.      * @return Price
  112.      */
  113.     public function getPrice(): PriceInterface
  114.     {
  115.         if ($this->hasDiscount()) {
  116.             return new Price(Decimal::fromNumeric($this->getYieldPrice($this->price->getAmount()->asNumeric())), $this->price->getCurrency());
  117.         }
  118.         return $this->price;
  119.     }
  120.     /**
  121.      * @param Price $price
  122.      */
  123.     public function setPrice(Price $price): void
  124.     {
  125.         $this->price $price;
  126.     }
  127.     /**
  128.      * @return PriceInterface
  129.      */
  130.     public function getTotalBasePrice(): PriceInterface
  131.     {
  132.         return new Price(Decimal::fromNumeric($this->getBaseAmount() * $this->getQuantity()), $this->price->getCurrency(), false);
  133.     }
  134.     /**
  135.      * @return PriceInterface
  136.      */
  137.     public function getTotalPrice(): PriceInterface
  138.     {
  139.         return new Price(Decimal::fromNumeric($this->getPrice()->getAmount()->asNumeric() * $this->getQuantity()), $this->getPrice()->getCurrency(), false);
  140.     }
  141.     public function getBaseAmount(): int
  142.     {
  143.         return $this->baseAmount;
  144.     }
  145.     public function setBaseAmount(int $baseAmount): void
  146.     {
  147.         $this->baseAmount $baseAmount;
  148.     }
  149. }