vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PriceSystem/AttributePriceSystem.php line 91

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\PriceSystem;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\UnsupportedException;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractSetProductEntry;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\Currency;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\TaxManagement\TaxCalculationService;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\TaxManagement\TaxEntry;
  22. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\PricingManagerLocatorInterface;
  23. use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
  24. use Symfony\Component\OptionsResolver\OptionsResolver;
  25. class AttributePriceSystem extends CachingPriceSystem implements PriceSystemInterface
  26. {
  27.     /**
  28.      * @var EnvironmentInterface
  29.      */
  30.     protected $environment;
  31.     /**
  32.      * @var string
  33.      */
  34.     protected $attributeName;
  35.     /**
  36.      * @var string
  37.      */
  38.     protected $priceType;
  39.     /**
  40.      * @var string
  41.      */
  42.     protected $priceClass;
  43.     public function __construct(PricingManagerLocatorInterface $pricingManagersEnvironmentInterface $environment, array $options = [])
  44.     {
  45.         parent::__construct($pricingManagers);
  46.         $this->environment $environment;
  47.         $resolver = new OptionsResolver();
  48.         $this->configureOptions($resolver);
  49.         $this->processOptions($resolver->resolve($options));
  50.     }
  51.     protected function processOptions(array $options)
  52.     {
  53.         $this->attributeName $options['attribute_name'];
  54.         $this->priceClass $options['price_class'];
  55.         $this->priceType $options['price_type'];
  56.     }
  57.     protected function configureOptions(OptionsResolver $resolver)
  58.     {
  59.         $resolver->setRequired([
  60.             'attribute_name',
  61.             'price_class',
  62.         ]);
  63.         $resolver->setDefaults([
  64.             'attribute_name' => 'price',
  65.             'price_class' => Price::class,
  66.             'price_type' => TaxCalculationService::CALCULATION_FROM_GROSS,
  67.         ]);
  68.         $resolver->setAllowedTypes('attribute_name''string');
  69.         $resolver->setAllowedTypes('price_class''string');
  70.         $resolver->setAllowedTypes('price_type''string');
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function createPriceInfoInstance($quantityScaleCheckoutableInterface $product$products): PriceInfoInterface
  76.     {
  77.         $taxClass $this->getTaxClassForProduct($product);
  78.         $amount $this->calculateAmount($product$products);
  79.         $price $this->getPriceClassInstance($amount);
  80.         $totalPrice $this->getPriceClassInstance($amount->mul($quantityScale));
  81.         $price->setTaxEntryCombinationMode($taxClass->getTaxEntryCombinationType());
  82.         $price->setTaxEntries(TaxEntry::convertTaxEntries($taxClass));
  83.         $totalPrice->setTaxEntryCombinationMode($taxClass->getTaxEntryCombinationType());
  84.         $totalPrice->setTaxEntries(TaxEntry::convertTaxEntries($taxClass));
  85.         $taxCalculationService $this->getTaxCalculationService();
  86.         $taxCalculationService->updateTaxes($price$this->priceType);
  87.         $taxCalculationService->updateTaxes($totalPrice$this->priceType);
  88.         return new AttributePriceInfo($price$quantityScale$totalPrice);
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function filterProductIds($productIds$fromPrice$toPrice$order$offset$limit)
  94.     {
  95.         throw new UnsupportedException(__METHOD__  ' is not supported for ' get_class($this));
  96.     }
  97.     /**
  98.      * Calculates prices from product
  99.      *
  100.      * @param CheckoutableInterface $product
  101.      * @param CheckoutableInterface[] $products
  102.      *
  103.      * @return Decimal
  104.      */
  105.     protected function calculateAmount(CheckoutableInterface $product$products): Decimal
  106.     {
  107.         $getter 'get' ucfirst($this->attributeName);
  108.         if (is_callable([$product$getter])) {
  109.             if (!empty($products)) {
  110.                 // TODO where to start using price value object?
  111.                 $sum 0;
  112.                 foreach ($products as $p) {
  113.                     if ($p instanceof AbstractSetProductEntry) {
  114.                         $sum += $p->getProduct()->$getter() * $p->getQuantity();
  115.                     } else {
  116.                         $sum += $p->$getter();
  117.                     }
  118.                 }
  119.                 return Decimal::create($sum);
  120.             } else {
  121.                 return Decimal::create((float) $product->$getter());
  122.             }
  123.         }
  124.         return Decimal::zero();
  125.     }
  126.     /**
  127.      * Returns default currency based on environment settings
  128.      *
  129.      * @return Currency
  130.      */
  131.     protected function getDefaultCurrency(): Currency
  132.     {
  133.         return $this->environment->getDefaultCurrency();
  134.     }
  135.     /**
  136.      * Creates instance of PriceInterface
  137.      *
  138.      * @param Decimal $amount
  139.      *
  140.      * @return PriceInterface
  141.      */
  142.     protected function getPriceClassInstance(Decimal $amount): PriceInterface
  143.     {
  144.         $priceClass $this->priceClass;
  145.         $price = new $priceClass($amount$this->getDefaultCurrency(), false);
  146.         return $price;
  147.     }
  148. }