vendor/shopware/core/Checkout/Cart/Price/GrossPriceCalculator.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Price;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  4. use Shopware\Core\Checkout\Cart\Price\Struct\ListPrice;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  6. use Shopware\Core\Checkout\Cart\Tax\TaxCalculator;
  7. class GrossPriceCalculator
  8. {
  9.     /**
  10.      * @var TaxCalculator
  11.      */
  12.     private $taxCalculator;
  13.     /**
  14.      * @var PriceRoundingInterface
  15.      */
  16.     private $priceRounding;
  17.     /**
  18.      * @var ReferencePriceCalculator
  19.      */
  20.     private $referencePriceCalculator;
  21.     public function __construct(
  22.         TaxCalculator $taxCalculator,
  23.         PriceRoundingInterface $priceRounding,
  24.         ReferencePriceCalculator $referencePriceCalculator
  25.     ) {
  26.         $this->taxCalculator $taxCalculator;
  27.         $this->priceRounding $priceRounding;
  28.         $this->referencePriceCalculator $referencePriceCalculator;
  29.     }
  30.     public function calculate(QuantityPriceDefinition $definition): CalculatedPrice
  31.     {
  32.         $unitPrice $this->getUnitPrice($definition);
  33.         $unitTaxes $this->taxCalculator->calculateGrossTaxes($unitPrice$definition->getTaxRules());
  34.         foreach ($unitTaxes as $tax) {
  35.             $total $this->priceRounding->round(
  36.                 $tax->getTax() * $definition->getQuantity(),
  37.                 $definition->getPrecision()
  38.             );
  39.             $tax->setTax($total);
  40.             $tax->setPrice($tax->getPrice() * $definition->getQuantity());
  41.         }
  42.         $price $this->priceRounding->round(
  43.             $unitPrice $definition->getQuantity(),
  44.             $definition->getPrecision()
  45.         );
  46.         return new CalculatedPrice(
  47.             $unitPrice,
  48.             $price,
  49.             $unitTaxes,
  50.             $definition->getTaxRules(),
  51.             $definition->getQuantity(),
  52.             $this->referencePriceCalculator->calculate($price$definition),
  53.             $this->calculateListPrice($unitPrice$definition)
  54.         );
  55.     }
  56.     private function getUnitPrice(QuantityPriceDefinition $definition): float
  57.     {
  58.         //item price already calculated?
  59.         if ($definition->isCalculated()) {
  60.             return $definition->getPrice();
  61.         }
  62.         $price $this->taxCalculator->calculateGross(
  63.             $definition->getPrice(),
  64.             $definition->getTaxRules()
  65.         );
  66.         return $this->priceRounding->round($price$definition->getPrecision());
  67.     }
  68.     private function calculateListPrice(float $unitPriceQuantityPriceDefinition $definition): ?ListPrice
  69.     {
  70.         if (!$definition->getListPrice()) {
  71.             return null;
  72.         }
  73.         $price $definition->getListPrice();
  74.         if (!$definition->isCalculated()) {
  75.             $price $this->taxCalculator->calculateGross(
  76.                 $definition->getListPrice(),
  77.                 $definition->getTaxRules()
  78.             );
  79.         }
  80.         $listPrice $this->priceRounding->round($price$definition->getPrecision());
  81.         return ListPrice::createFromUnitPrice($unitPrice$listPrice);
  82.     }
  83. }