src/Ecommerce/PriceSystem/StoeltingPriceSystem.php line 150

Open in your IDE?
  1. <?php
  2. namespace App\Ecommerce\PriceSystem;
  3. use App\Ecommerce\PriceSystem\PriceSystemRule\HasPriceSystemRuleContract;
  4. use App\Ecommerce\PriceSystem\PriceSystemRule\Processor as PriceSystemRuleProcessor;
  5. use App\Model\Customer;
  6. use Factory\CheckoutBundle\Repository\TaxRepository;
  7. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\UnsupportedException;
  8. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  9. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\AbstractPriceSystem;
  10. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\Currency;
  11. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\AttributePriceInfo;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceInterface;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\TaxManagement\TaxEntry;
  14. use Pimcore\Bundle\EcommerceFrameworkBundle\Type\Decimal;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\PricingManagerLocatorInterface;
  18. use Pimcore\Model\DataObject\AbstractObject;
  19. use Pimcore\Model\DataObject\Data\QuantityValue;
  20. use Pimcore\Model\DataObject\OnlineShopTaxClass;
  21. use Pimcore\Model\DataObject\PriceSystemRule;
  22. class StoeltingPriceSystem extends AbstractPriceSystem
  23. {
  24.     /**
  25.      * @var Customer
  26.      */
  27.     protected $customer;
  28.     /**
  29.      * @var EnvironmentInterface
  30.      */
  31.     protected $environment;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $attributeName;
  36.     /**
  37.      * @var string
  38.      */
  39.     protected $priceType;
  40.     /**
  41.      * @var string
  42.      */
  43.     protected $priceClass;
  44.     /**
  45.      * @var TaxRepository
  46.      */
  47.     protected $taxRepository;
  48.     public function __construct(PricingManagerLocatorInterface $pricingManagersEnvironmentInterface $environmentTaxRepository $taxRepository, array $options = [])
  49.     {
  50.         parent::__construct($pricingManagers);
  51.         $this->environment $environment;
  52.         $this->taxRepository $taxRepository;
  53.         $this->attributeName $options['attribute_name'];
  54.         $this->priceType $options['price_type'];
  55.         $this->priceClass $options['price_class'];
  56.     }
  57.     /**
  58.      * Undocumented function
  59.      *
  60.      * @param int $quantityScale
  61.      * @param CheckoutableInterface|HasPriceSystemRuleContract $product
  62.      * @param mix $products
  63.      */
  64.     public function createPriceInfoInstance($quantityScale$product$products)
  65.     {
  66.         $taxClass $this->getTaxClassForProduct($product);
  67.         $amount $this->calculateAmount($product$products);
  68.         $priceSystemRule $this->getPriceSystemRule($product$products);
  69.         $priceSystemRuleProcessor = new PriceSystemRuleProcessor($this->environment);
  70.         $action $priceSystemRuleProcessor->getMatchingAction(
  71.             $priceSystemRule
  72.         );
  73.         $action->setDefaultCurrency($this->getDefaultCurrency());
  74.         $price $action->getPriceObject($amount);
  75.         $totalPrice $action->getPriceObject($amount->mul($quantityScale));
  76.         // $price = $this->getPriceClassInstance($amount);
  77.         // $totalPrice = $this->getPriceClassInstance($amount->mul($quantityScale));
  78.         if ($taxClass) {
  79.             // Move this into OnlineShopTaxClass
  80.             $inheritanceBackup AbstractObject::getGetInheritedValues();
  81.             AbstractObject::setGetInheritedValues(true);
  82.             $mode $taxClass->getTaxEntryCombinationType();
  83.             AbstractObject::setGetInheritedValues($inheritanceBackup);
  84.             $price->setTaxEntryCombinationMode($mode);
  85.             $price->setTaxEntries(TaxEntry::convertTaxEntries($taxClass));
  86.             $totalPrice->setTaxEntryCombinationMode($mode);
  87.             $totalPrice->setTaxEntries(TaxEntry::convertTaxEntries($taxClass));
  88.         }
  89.         $taxCalculationService $this->getTaxCalculationService();
  90.         $taxCalculationService->updateTaxes($price$this->priceType);
  91.         $taxCalculationService->updateTaxes($totalPrice$this->priceType);
  92.         $priceVisibility $this->isPriceVisible($priceSystemRule);
  93.         $price->setIsVisible($priceVisibility);
  94.         return new StoeltingPriceInfo($price$quantityScale$totalPrice$priceVisibility);
  95.     }
  96.     protected function isPriceVisible(PriceSystemRuleContract $priceSystemRule)
  97.     {
  98.         if ($this->environment->getCurrentUserId() > 0) {
  99.             return true;
  100.         }
  101.         if ($priceSystemRule->getGuestCustomerSee() == 'not-price') {
  102.             return false;
  103.         }
  104.         return true;
  105.     }
  106.     public function filterProductIds($productIds$fromPrice$toPrice$order$offset$limit)
  107.     {
  108.         throw new UnsupportedException(__METHOD__  ' is not supported for ' get_class($this));
  109.     }
  110.     /**
  111.      * Calculates prices from product
  112.      *
  113.      * @param CheckoutableInterface $product
  114.      * @param CheckoutableInterface[] $products
  115.      *
  116.      * @return Decimal
  117.      */
  118.     protected function calculateAmount(CheckoutableInterface $product$products): Decimal
  119.     {
  120.         $getter 'get'.$this->attributeName;
  121.         $quantityValue $product->{$getter}();
  122.         if (!$quantityValue) {
  123.             return Decimal::zero();
  124.         }
  125.         if (!$quantityValue instanceof QuantityValue) {
  126.             return Decimal::fromNumeric($quantityValue);
  127.         }
  128.         return Decimal::create($quantityValue->getValue());
  129.     }
  130.     /**
  131.      * Returns default currency based on environment settings
  132.      *
  133.      * @return Currency
  134.      */
  135.     protected function getDefaultCurrency(): Currency
  136.     {
  137.         return $this->environment->getDefaultCurrency();
  138.     }
  139.     protected function getPriceSystemRule(HasPriceSystemRuleContract $product$products)
  140.     {
  141.         $priceSysRule $product->getPriceSystemRule();
  142.         if ($priceSysRule) {
  143.             return $priceSysRule;
  144.         }
  145.         return new PriceSystemRule();
  146.     }
  147.     protected function getCustomer()
  148.     {
  149.         $env Factory::getInstance()->getEnvironment();
  150.         if (!$this->customer && $env->getCurrentUserId() > 0) {
  151.             $this->customer Customer::getById($env->getCurrentUserId());
  152.         }
  153.         return $this->customer;
  154.     }
  155.     /** @inheritDoc */
  156.     public function getDefaultTaxClass()
  157.     {
  158.         if ($this->getCustomer() && $this->getCustomer()->getTaxExempt()) {
  159.             return $this->zeroTax();
  160.         }
  161.         $billingAddress Factory::getInstance()->getEnvironment()->getBillingAddress();
  162.         $shippingAddress Factory::getInstance()->getEnvironment()->getShippingAddress();
  163.         $billingTax $billingAddress $this->taxRepository->getByAddress($billingAddress) : null;
  164.         $shippingTax $shippingAddress $this->taxRepository->getByAddress($shippingAddress) : null;
  165.         $taxes = [];
  166.         if ($shippingTax && $shippingTax->applyToShipping()) {
  167.             $taxes[] = $shippingTax;
  168.         }
  169.         if ($billingTax && $billingTax->applyToBilling()) {
  170.             $taxes[] = $billingTax;
  171.         }
  172.         if (count($taxes) > 1) {
  173.             $taxes = [$billingTax];
  174.         }
  175.         return $taxes[0] ?? parent::getDefaultTaxClass();
  176.     }
  177.     protected function zeroTax()
  178.     {
  179.         $taxClass = new OnlineShopTaxClass();
  180.         $taxClass->setTaxEntryCombinationType(TaxEntry::CALCULATION_MODE_COMBINE);
  181.         return $taxClass;
  182.     }
  183.     public function getAttributeName()
  184.     {
  185.         return $this->attributeName;
  186.     }
  187.     public function setAttributeName($attributeName)
  188.     {
  189.         $this->attributeName $attributeName;
  190.     }
  191. }