vendor/coreshop/core-shop/src/CoreShop/Component/Core/Product/TaxedProductPriceCalculator.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * CoreShop
  5.  *
  6.  * This source file is available under two different licenses:
  7.  *  - GNU General Public License version 3 (GPLv3)
  8.  *  - CoreShop Commercial License (CCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  13.  * @license    https://www.coreshop.org/license     GPLv3 and CCL
  14.  *
  15.  */
  16. namespace CoreShop\Component\Core\Product;
  17. use CoreShop\Component\Address\Model\AddressInterface;
  18. use CoreShop\Component\Core\Provider\DefaultTaxAddressProviderInterface;
  19. use CoreShop\Component\Core\Taxation\TaxApplicatorInterface;
  20. use CoreShop\Component\Order\Calculator\PurchasableCalculatorInterface;
  21. use CoreShop\Component\Order\Model\PurchasableInterface;
  22. use CoreShop\Component\Taxation\Calculator\TaxCalculatorInterface;
  23. class TaxedProductPriceCalculator implements TaxedProductPriceCalculatorInterface
  24. {
  25.     public function __construct(
  26.         private PurchasableCalculatorInterface $purchasableCalculator,
  27.         private DefaultTaxAddressProviderInterface $defaultTaxAddressProvider,
  28.         private ProductTaxCalculatorFactoryInterface $taxCalculatorFactory,
  29.         private TaxApplicatorInterface $taxApplicator,
  30.     ) {
  31.     }
  32.     public function getPrice(PurchasableInterface $product, array $contextbool $withTax true): int
  33.     {
  34.         $price $this->purchasableCalculator->getPrice($product$contexttrue);
  35.         $taxCalculator $this->getTaxCalculator($product$context);
  36.         if ($taxCalculator instanceof TaxCalculatorInterface) {
  37.             return $this->taxApplicator->applyTax($price$context$taxCalculator$withTax);
  38.         }
  39.         return $price;
  40.     }
  41.     public function getDiscountPrice(PurchasableInterface $product, array $contextbool $withTax true): int
  42.     {
  43.         $price $this->purchasableCalculator->getDiscountPrice($product$context);
  44.         $taxCalculator $this->getTaxCalculator($product$context);
  45.         if ($taxCalculator instanceof TaxCalculatorInterface) {
  46.             return $this->taxApplicator->applyTax($price$context$taxCalculator$withTax);
  47.         }
  48.         return $price;
  49.     }
  50.     public function getDiscount(PurchasableInterface $product, array $contextbool $withTax true): int
  51.     {
  52.         $price $this->purchasableCalculator->getPrice($product$context);
  53.         $discount $this->purchasableCalculator->getDiscount($product$context$price);
  54.         $taxCalculator $this->getTaxCalculator($product$context);
  55.         if ($taxCalculator instanceof TaxCalculatorInterface) {
  56.             return $this->taxApplicator->applyTax($discount$context$taxCalculator$withTax);
  57.         }
  58.         return $discount;
  59.     }
  60.     public function getRetailPrice(PurchasableInterface $product, array $contextbool $withTax true): int
  61.     {
  62.         $price $this->purchasableCalculator->getRetailPrice($product$context);
  63.         $taxCalculator $this->getTaxCalculator($product$context);
  64.         if ($taxCalculator instanceof TaxCalculatorInterface) {
  65.             return $this->taxApplicator->applyTax($price$context$taxCalculator$withTax);
  66.         }
  67.         return $price;
  68.     }
  69.     protected function getTaxCalculator(PurchasableInterface $product, array $context): ?TaxCalculatorInterface
  70.     {
  71.         return $this->taxCalculatorFactory->getTaxCalculator($product$this->getDefaultAddress($context), $context);
  72.     }
  73.     protected function getDefaultAddress($context): ?AddressInterface
  74.     {
  75.         return $this->defaultTaxAddressProvider->getAddress($context);
  76.     }
  77. }