vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/PriceInfo.php line 130

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInfoInterface as PriceSystemPriceInfoInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
  20. class PriceInfo implements PriceInfoInterface
  21. {
  22.     /**
  23.      * @var PriceSystemPriceInfoInterface
  24.      */
  25.     protected $priceInfo;
  26.     /**
  27.      * @var Decimal
  28.      */
  29.     protected $amount;
  30.     /**
  31.      * @var RuleInterface[]
  32.      */
  33.     protected $rules = [];
  34.     /**
  35.      * @var RuleInterface[]|null
  36.      */
  37.     protected $validRules;
  38.     /**
  39.      * @var bool
  40.      */
  41.     protected $rulesApplied false;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $priceEnvironmentHash;
  46.     /**
  47.      * @var EnvironmentInterface
  48.      */
  49.     protected $environment;
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function __construct(PriceSystemPriceInfoInterface $priceInfoEnvironmentInterface $environment)
  54.     {
  55.         $this->amount Decimal::create(0);
  56.         $this->priceInfo $priceInfo;
  57.         $this->environment $environment;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function addRule(RuleInterface $rule)
  63.     {
  64.         $this->rules[] = $rule;
  65.         return $this;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getEnvironment(): EnvironmentInterface
  71.     {
  72.         return $this->environment;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function setEnvironment(EnvironmentInterface $environment)
  78.     {
  79.         $this->environment $environment;
  80.         return $this;
  81.     }
  82.     /**
  83.      * Checks if environment changed based on hash
  84.      * if so, resets valid rules
  85.      *
  86.      * @return bool
  87.      */
  88.     protected function environmentHashChanged()
  89.     {
  90.         $hash $this->getEnvironment()->getHash();
  91.         if ($this->priceEnvironmentHash != $hash) {
  92.             $this->validRules null;
  93.             $this->rulesApplied false;
  94.             $this->priceEnvironmentHash $hash;
  95.             return true;
  96.         }
  97.         return false;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getRules(bool $forceRecalc false): array
  103.     {
  104.         if ($forceRecalc || $this->validRules === null) {
  105.             $env $this->getEnvironment();
  106.             $this->validRules = [];
  107.             foreach ($this->rules as $rule) {
  108.                 $env->setRule($rule);
  109.                 if ($rule->check($env) === true) {
  110.                     $this->validRules[] = $rule;
  111.                     // is this a stop rule?
  112.                     if ($rule->getBehavior() == 'stopExecute') {
  113.                         break;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         return $this->validRules;
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function getPrice(): PriceInterface
  124.     {
  125.         $price = clone $this->priceInfo->getPrice();
  126.         if (!$this->rulesApplied || $this->environmentHashChanged()) {
  127.             $this->setAmount($price->getAmount());
  128.             $env $this->getEnvironment();
  129.             foreach ($this->getRules() as $rule) {
  130.                 $env->setRule($rule);
  131.                 // execute rule
  132.                 $rule->executeOnProduct($env);
  133.             }
  134.             $this->rulesApplied true;
  135.             if ($this->getAmount()->isNegative()) {
  136.                 $this->setAmount(Decimal::create(0));
  137.             }
  138.         }
  139.         $price->setAmount($this->getAmount(), PriceInterface::PRICE_MODE_GROSStrue);
  140.         return $price;
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function getTotalPrice(): PriceInterface
  146.     {
  147.         $price = clone $this->priceInfo->getPrice();
  148.         $price->setAmount(
  149.             $this->getPrice()->getAmount()->mul($this->getQuantity()),
  150.             PriceInterface::PRICE_MODE_GROSS,
  151.             true
  152.         );
  153.         return $price;
  154.     }
  155.     /**
  156.      * {@inheritdoc}
  157.      */
  158.     public function isMinPrice(): bool
  159.     {
  160.         return $this->priceInfo->isMinPrice();
  161.     }
  162.     /**
  163.      * {@inheritdoc}
  164.      */
  165.     public function getQuantity()
  166.     {
  167.         return $this->priceInfo->getQuantity();
  168.     }
  169.     /**
  170.      * {@inheritdoc}
  171.      */
  172.     public function setQuantity($quantity)
  173.     {
  174.         return $this->priceInfo->setQuantity($quantity);
  175.     }
  176.     /**
  177.      * {@inheritdoc}
  178.      */
  179.     public function setPriceSystem(PriceSystemInterface $priceSystem)
  180.     {
  181.         $this->priceInfo->setPriceSystem($priceSystem);
  182.         return $this;
  183.     }
  184.     /**
  185.      * {@inheritdoc}
  186.      */
  187.     public function setProduct(CheckoutableInterface $product)
  188.     {
  189.         $this->priceInfo->setProduct($product);
  190.         return $this;
  191.     }
  192.     /**
  193.      * {@inheritdoc}
  194.      */
  195.     public function getProduct()
  196.     {
  197.         return $this->priceInfo->getProduct();
  198.     }
  199.     /**
  200.      * {@inheritdoc}
  201.      */
  202.     public function setAmount(Decimal $amount)
  203.     {
  204.         $this->amount $amount;
  205.         return $this;
  206.     }
  207.     /**
  208.      * {@inheritdoc}
  209.      */
  210.     public function getAmount(): Decimal
  211.     {
  212.         return $this->amount;
  213.     }
  214.     /**
  215.      * loop through any other calls
  216.      *
  217.      * @param string $name
  218.      * @param array $arguments
  219.      *
  220.      * @return mixed
  221.      */
  222.     public function __call($name$arguments)
  223.     {
  224.         return call_user_func_array([$this->priceInfo$name], $arguments);
  225.     }
  226.     /**
  227.      * {@inheritdoc}
  228.      */
  229.     public function getOriginalPrice(): PriceInterface
  230.     {
  231.         return $this->priceInfo->getPrice();
  232.     }
  233.     /**
  234.      * {@inheritdoc}
  235.      */
  236.     public function getOriginalTotalPrice(): PriceInterface
  237.     {
  238.         return $this->priceInfo->getTotalPrice();
  239.     }
  240.     /**
  241.      * {@inheritdoc}
  242.      */
  243.     public function hasDiscount(): bool
  244.     {
  245.         return $this->getPrice()->getAmount()->lessThan(
  246.             $this->getOriginalPrice()->getAmount()
  247.         );
  248.     }
  249.     /**
  250.      * {@inheritdoc}
  251.      */
  252.     public function getDiscount(): PriceInterface
  253.     {
  254.         $discount $this->getPrice()->getAmount()->sub($this->getOriginalPrice()->getAmount());
  255.         $price = clone $this->priceInfo->getPrice();
  256.         $price->setAmount($discount);
  257.         return $price;
  258.     }
  259.     /**
  260.      * {@inheritdoc}
  261.      */
  262.     public function getTotalDiscount(): PriceInterface
  263.     {
  264.         $discount $this->getTotalPrice()->getAmount()->sub($this->getOriginalTotalPrice()->getAmount());
  265.         $price = clone $this->priceInfo->getPrice();
  266.         $price->setAmount($discount);
  267.         return $price;
  268.     }
  269.     /**
  270.      * {@inheritdoc}
  271.      */
  272.     public function getDiscountPercent()
  273.     {
  274.         $percent $this->getPrice()->getAmount()->discountPercentageOf(
  275.             $this->getOriginalPrice()->getAmount()
  276.         );
  277.         return round($percent2);
  278.     }
  279.     /**
  280.      * {@inheritdoc}
  281.      */
  282.     public function hasRulesApplied(): bool
  283.     {
  284.         return (bool)$this->rulesApplied;
  285.     }
  286. }