/** * Check what taxes should be applied after discount * * @param null|int|string|Store $store * @return bool */ public function applyTaxAfterDiscount($store = null) { return $this->_config->applyTaxAfterDiscount($store); }
/** * Calculate item price and row total including/excluding tax based on total price rounding level * * @param QuoteDetailsItem $item * @param \Magento\Framework\Object $taxRateRequest * @param int $storeId * @return TaxDetailsItem */ protected function totalBaseCalculation(QuoteDetailsItem $item, \Magento\Framework\Object $taxRateRequest, $storeId) { /** @var \Magento\Tax\Service\V1\Data\TaxDetails\AppliedTax[] $appliedTaxes */ $appliedTaxes = []; $appliedTaxBuilder = $this->taxDetailsItemBuilder->getAppliedTaxBuilder(); $appliedTaxRateBuilder = $appliedTaxBuilder->getAppliedTaxRateBuilder(); $taxRateRequest->setProductClassId($item->getTaxClassId()); $appliedRates = $this->calculator->getAppliedRates($taxRateRequest); $rate = $this->calculator->getRate($taxRateRequest); $quantity = $this->getTotalQuantity($item); $price = $priceInclTax = $this->calculator->round($item->getUnitPrice()); $rowTotal = $rowTotalInclTax = $taxableAmount = $this->calcRowTotal($item); $discountAmount = $item->getDiscountAmount(); $applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($storeId); $discountTaxCompensationAmount = 0; $isTotalBasedCalculation = $this->config->getAlgorithm($storeId) == Calculation::CALC_TOTAL_BASE; if ($item->getTaxIncluded()) { if ($taxRateRequest->getSameRateAsStore()) { $rowTaxExact = $this->calculator->calcTaxAmount($rowTotalInclTax, $rate, true, false); if ($isTotalBasedCalculation) { $rowTax = $this->deltaRound($rowTaxExact, $rate, true); } else { $rowTax = $this->calculator->round($rowTaxExact); } $rowTotal = $rowTotalInclTax - $rowTax; $price = $this->calculator->round($rowTotal / $quantity); } else { $storeRate = $this->calculator->getStoreRate($taxRateRequest, $storeId); $priceInclTax = $this->calculatePriceInclTax($price, $storeRate, $rate); $rowTotalInclTax = $priceInclTax * $quantity; $taxableAmount = $rowTotalInclTax; if ($isTotalBasedCalculation) { $rowTax = $this->deltaRound($this->calculator->calcTaxAmount($rowTotalInclTax, $rate, true, false), $rate, true); } else { $rowTax = $this->calculator->calcTaxAmount($rowTotalInclTax, $rate, true, true); } $rowTotal = $rowTotalInclTax - $rowTax; $price = $this->calculator->round($rowTotal / $quantity); } //Handle discount if ($discountAmount && $applyTaxAfterDiscount) { //TODO: handle originalDiscountAmount $taxableAmount = max($taxableAmount - $discountAmount, 0); $rowTaxAfterDiscount = $this->calculator->calcTaxAmount($taxableAmount, $rate, true, false); if ($isTotalBasedCalculation) { //Round the row tax using a different type so that we don't pollute the rounding deltas $rowTaxAfterDiscount = $this->deltaRound($rowTaxAfterDiscount, $rate, true, self::KEY_TAX_AFTER_DISCOUNT_DELTA_ROUNDING); } else { $rowTaxAfterDiscount = $this->calculator->round($rowTaxAfterDiscount); } // Set discount tax compensation $discountTaxCompensationAmount = $rowTax - $rowTaxAfterDiscount; $rowTax = $rowTaxAfterDiscount; } //save applied taxes $appliedTaxes = $this->getAppliedTaxes($appliedTaxBuilder, $rowTax, $rate, $appliedRates); } else { //catalog price does not include tax $appliedRates = $this->calculator->getAppliedRates($taxRateRequest); $rowTaxes = []; $rowTaxesBeforeDiscount = []; //Apply each tax rate separately foreach ($appliedRates as $appliedRate) { $taxId = $appliedRate['id']; $taxRate = $appliedRate['percent']; if ($isTotalBasedCalculation) { $rowTaxPerRate = $this->deltaRound($this->calculator->calcTaxAmount($rowTotal, $taxRate, false, false), $taxId, false); } else { $rowTaxPerRate = $this->calculator->calcTaxAmount($rowTotal, $taxRate, false, true); } $rowTaxAfterDiscount = $rowTaxPerRate; //Handle discount if ($discountAmount && $applyTaxAfterDiscount) { //TODO: handle originalDiscountAmount $rowTaxAfterDiscount = $this->calculator->calcTaxAmount(max($rowTotal - $discountAmount, 0), $taxRate, false, false); if ($isTotalBasedCalculation) { //Round the row tax using a different type so that we don't pollute the rounding deltas $rowTaxAfterDiscount = $this->deltaRound($rowTaxAfterDiscount, $taxRate, false, self::KEY_TAX_AFTER_DISCOUNT_DELTA_ROUNDING); } else { $rowTaxAfterDiscount = $this->calculator->round($rowTaxAfterDiscount); } } $appliedTaxes[$appliedRate['id']] = $this->getAppliedTax($appliedTaxBuilder, $rowTaxAfterDiscount, $appliedRate); $rowTaxes[] = $rowTaxAfterDiscount; $rowTaxesBeforeDiscount[] = $rowTaxPerRate; } $rowTax = array_sum($rowTaxes); $rowTaxBeforeDiscount = array_sum($rowTaxesBeforeDiscount); $rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount; $priceInclTax = $this->calculator->round($rowTotalInclTax / $quantity); } $this->taxDetailsItemBuilder->setCode($item->getCode()); $this->taxDetailsItemBuilder->setRowTax($rowTax); $this->taxDetailsItemBuilder->setPrice($price); $this->taxDetailsItemBuilder->setPriceInclTax($priceInclTax); $this->taxDetailsItemBuilder->setRowTotal($rowTotal); $this->taxDetailsItemBuilder->setRowTotalInclTax($rowTotalInclTax); $this->taxDetailsItemBuilder->setCode($item->getCode()); $this->taxDetailsItemBuilder->setType($item->getType()); $this->taxDetailsItemBuilder->setTaxPercent($rate); $this->taxDetailsItemBuilder->setDiscountTaxCompensationAmount($discountTaxCompensationAmount); $this->taxDetailsItemBuilder->setAppliedTaxes($appliedTaxes); return $this->taxDetailsItemBuilder->create(); }
/** * Check if tax discount settings are compatible * * Matrix for invalid discount settings is as follows: * Before Discount / Excluding Tax * Before Discount / Including Tax * * @param null|int|bool|string|\Magento\Store\Model\Store $store $store * @return bool */ public function checkDiscountSettings($store = null) { return $this->taxConfig->applyTaxAfterDiscount($store); }