/**
  * {@inheritdoc}
  */
 protected function calculateWithTaxNotInPrice(QuoteDetailsItemInterface $item, $quantity, $round = true)
 {
     $taxRateRequest = $this->getAddressRateRequest()->setProductClassId($this->taxClassManagement->getTaxClassId($item->getTaxClassKey()));
     $rate = $this->calculationTool->getRate($taxRateRequest);
     $appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
     $applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
     $discountAmount = $item->getDiscountAmount();
     $discountTaxCompensationAmount = 0;
     // Calculate $rowTotal
     $price = $this->calculationTool->round($item->getUnitPrice());
     $rowTotal = $price * $quantity;
     $rowTaxes = [];
     $rowTaxesBeforeDiscount = [];
     $appliedTaxes = [];
     //Apply each tax rate separately
     foreach ($appliedRates as $appliedRate) {
         $taxId = $appliedRate['id'];
         $taxRate = $appliedRate['percent'];
         $rowTaxPerRate = $this->calculationTool->calcTaxAmount($rowTotal, $taxRate, false, false);
         $deltaRoundingType = self::KEY_REGULAR_DELTA_ROUNDING;
         if ($applyTaxAfterDiscount) {
             $deltaRoundingType = self::KEY_TAX_BEFORE_DISCOUNT_DELTA_ROUNDING;
         }
         $rowTaxPerRate = $this->roundAmount($rowTaxPerRate, $taxId, false, $deltaRoundingType, $round);
         $rowTaxAfterDiscount = $rowTaxPerRate;
         //Handle discount
         if ($applyTaxAfterDiscount) {
             //TODO: handle originalDiscountAmount
             $taxableAmount = max($rowTotal - $discountAmount, 0);
             $rowTaxAfterDiscount = $this->calculationTool->calcTaxAmount($taxableAmount, $taxRate, false, false);
             $rowTaxAfterDiscount = $this->roundAmount($rowTaxAfterDiscount, $taxId, false, self::KEY_REGULAR_DELTA_ROUNDING, $round);
         }
         $appliedTaxes[$taxId] = $this->getAppliedTax($rowTaxAfterDiscount, $appliedRate);
         $rowTaxes[] = $rowTaxAfterDiscount;
         $rowTaxesBeforeDiscount[] = $rowTaxPerRate;
     }
     $rowTax = array_sum($rowTaxes);
     $rowTaxBeforeDiscount = array_sum($rowTaxesBeforeDiscount);
     $rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount;
     $priceInclTax = $rowTotalInclTax / $quantity;
     if ($round) {
         $priceInclTax = $this->calculationTool->round($priceInclTax);
     }
     return $this->taxDetailsItemDataObjectFactory->create()->setCode($item->getCode())->setType($item->getType())->setRowTax($rowTax)->setPrice($price)->setPriceInclTax($priceInclTax)->setRowTotal($rowTotal)->setRowTotalInclTax($rowTotalInclTax)->setDiscountTaxCompensationAmount($discountTaxCompensationAmount)->setAssociatedItemCode($item->getAssociatedItemCode())->setTaxPercent($rate)->setAppliedTaxes($appliedTaxes);
 }
 /**
  * {@inheritdoc}
  */
 protected function calculateWithTaxNotInPrice(QuoteDetailsItemInterface $item, $quantity, $round = true)
 {
     $taxRateRequest = $this->getAddressRateRequest()->setProductClassId($this->taxClassManagement->getTaxClassId($item->getTaxClassKey()));
     $rate = $this->calculationTool->getRate($taxRateRequest);
     $appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
     $applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
     $discountAmount = $item->getDiscountAmount();
     $discountTaxCompensationAmount = 0;
     // Calculate $price
     $price = $this->calculationTool->round($item->getUnitPrice());
     $unitTaxes = [];
     $unitTaxesBeforeDiscount = [];
     $appliedTaxes = [];
     //Apply each tax rate separately
     foreach ($appliedRates as $appliedRate) {
         $taxId = $appliedRate['id'];
         $taxRate = $appliedRate['percent'];
         $unitTaxPerRate = $this->calculationTool->calcTaxAmount($price, $taxRate, false);
         $unitTaxAfterDiscount = $unitTaxPerRate;
         //Handle discount
         if ($discountAmount && $applyTaxAfterDiscount) {
             //TODO: handle originalDiscountAmount
             $unitDiscountAmount = $discountAmount / $quantity;
             $taxableAmount = max($price - $unitDiscountAmount, 0);
             $unitTaxAfterDiscount = $this->calculationTool->calcTaxAmount($taxableAmount, $taxRate, false, true);
         }
         $appliedTaxes[$taxId] = $this->getAppliedTax($unitTaxAfterDiscount * $quantity, $appliedRate);
         $unitTaxes[] = $unitTaxAfterDiscount;
         $unitTaxesBeforeDiscount[] = $unitTaxPerRate;
     }
     $unitTax = array_sum($unitTaxes);
     $unitTaxBeforeDiscount = array_sum($unitTaxesBeforeDiscount);
     $rowTax = $unitTax * $quantity;
     $priceInclTax = $price + $unitTaxBeforeDiscount;
     return $this->taxDetailsItemDataObjectFactory->create()->setCode($item->getCode())->setType($item->getType())->setRowTax($rowTax)->setPrice($price)->setPriceInclTax($priceInclTax)->setRowTotal($price * $quantity)->setRowTotalInclTax($priceInclTax * $quantity)->setDiscountTaxCompensationAmount($discountTaxCompensationAmount)->setAssociatedItemCode($item->getAssociatedItemCode())->setTaxPercent($rate)->setAppliedTaxes($appliedTaxes);
 }