/** * Return discount item qty * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Mage_SalesRule_Model_Rule $rule * @return int */ protected function _getItemQty($item, $rule) { //@nelkaake: Magento 1.3 and lower doesnt have the item->getTotalQty attribute. if (Mage::helper('rewards/version')->isBaseMageVersionAtLeast('1.4')) { $qty = $item->getTotalQty(); } else { $qty = $item->getQty(); if ($item->getParentItem()) { $qty *= $item->getParentItem()->getQty(); } } return $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty; }
/** * Return discount item qty * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Mage_SalesRule_Model_Rule $rule * @return int */ protected function _getItemQty($item, $rule) { $qty = $item->getTotalQty(); return $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty; }
/** * Calculate unit tax anount based on unit price * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _calcUnitTaxAmount(Mage_Sales_Model_Quote_Item_Abstract $item, $rate) { $qty = $item->getTotalQty(); $inclTax = $item->getIsPriceInclTax(); $price = $item->getTaxableAmount() + $item->getExtraTaxableAmount(); $basePrice = $item->getBaseTaxableAmount() + $item->getBaseExtraTaxableAmount(); $rateKey = (string) $rate; $item->setTaxPercent($rate); $hiddenTax = null; $baseHiddenTax = null; switch ($this->_config->getCalculationSequence($this->_store)) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $unitTax = $this->_calculator->calcTaxAmount($price, $rate, $inclTax); $baseUnitTax = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $discountAmount = $item->getDiscountAmount() / $qty; $baseDiscountAmount = $item->getBaseDiscountAmount() / $qty; $unitTax = $this->_calculator->calcTaxAmount($price, $rate, $inclTax); $discountRate = $unitTax / $price * 100; $unitTaxDiscount = $this->_calculator->calcTaxAmount($discountAmount, $discountRate, $inclTax, false); $unitTax = max($unitTax - $unitTaxDiscount, 0); $baseUnitTax = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax); $baseDiscountRate = $baseUnitTax / $basePrice * 100; $baseUnitTaxDiscount = $this->_calculator->calcTaxAmount($baseDiscountAmount, $baseDiscountRate, $inclTax, false); $baseUnitTax = max($baseUnitTax - $baseUnitTaxDiscount, 0); if ($inclTax && $discountAmount > 0) { $hiddenTax = $this->_calculator->calcTaxAmount($discountAmount, $rate, $inclTax, false); $baseHiddenTax = $this->_calculator->calcTaxAmount($baseDiscountAmount, $rate, $inclTax, false); $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => $qty, 'item' => $item, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } elseif ($discountAmount > $price) { // case with 100% discount on price incl. tax $hiddenTax = $discountAmount - $price; $baseHiddenTax = $baseDiscountAmount - $basePrice; $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => $qty, 'item' => $item, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } break; } $item->setTaxAmount($this->_store->roundPrice(max(0, $qty * $unitTax))); $item->setBaseTaxAmount($this->_store->roundPrice(max(0, $qty * $baseUnitTax))); return $this; }
/** * Recollect item price and row total using after taxes subtract. * Declare item price including tax attributes * * @deprecated after 1.4.1 * * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote_Item_Abstract $item * * @return Mage_Tax_Model_Sales_Total_Quote_Subtotal */ protected function _recollectItem($address, Mage_Sales_Model_Quote_Item_Abstract $item) { $store = $address->getQuote()->getStore(); $request = $this->_getStoreTaxRequest($address); $request->setProductClassId($item->getProduct()->getTaxClassId()); $rate = $this->_calculator->getRate($request); $qty = $item->getTotalQty(); $price = $taxPrice = $item->getCalculationPriceOriginal(); $basePrice = $baseTaxPrice = $item->getBaseCalculationPriceOriginal(); $subtotal = $taxSubtotal = $item->getRowTotal(); $baseSubtotal = $baseTaxSubtotal = $item->getBaseRowTotal(); if ($this->_config->discountTax($store)) { $item->setDiscountCalculationPrice($price); $item->setBaseDiscountCalculationPrice($basePrice); } /** * Use original price for tax calculation */ if ($item->hasCustomPrice() && !$this->_helper->applyTaxOnCustomPrice($store)) { $taxPrice = $item->getOriginalPrice(); $baseTaxPrice = $item->getBaseOriginalPrice(); $taxSubtotal = $taxPrice * $qty; $baseTaxSubtotal = $baseTaxPrice * $qty; } if ($this->_areTaxRequestsSimilar) { $item->setRowTotalInclTax($subtotal); $item->setBaseRowTotalInclTax($baseSubtotal); $item->setPriceInclTax($price); $item->setBasePriceInclTax($basePrice); $item->setTaxCalcPrice($taxPrice); $item->setBaseTaxCalcPrice($baseTaxPrice); $item->setTaxCalcRowTotal($taxSubtotal); $item->setBaseTaxCalcRowTotal($baseTaxSubtotal); } $this->_subtotalInclTax += $subtotal; $this->_baseSubtotalInclTax += $baseSubtotal; if ($this->_config->getAlgorithm($store) == Mage_Tax_Model_Calculation::CALC_UNIT_BASE) { $taxAmount = $this->_calculator->calcTaxAmount($taxPrice, $rate, true); $baseTaxAmount = $this->_calculator->calcTaxAmount($baseTaxPrice, $rate, true); $unitPrice = $this->_calculator->round($price - $taxAmount); $baseUnitPrice = $this->_calculator->round($basePrice - $baseTaxAmount); $subtotal = $this->_calculator->round($unitPrice * $qty); $baseSubtotal = $this->_calculator->round($baseUnitPrice * $qty); } else { $taxAmount = $this->_calculator->calcTaxAmount($taxSubtotal, $rate, true, false); $baseTaxAmount = $this->_calculator->calcTaxAmount($baseTaxSubtotal, $rate, true, false); $unitPrice = ($subtotal - $taxAmount) / $qty; $baseUnitPrice = ($baseSubtotal - $baseTaxAmount) / $qty; $subtotal = $this->_calculator->round($subtotal - $taxAmount); $baseSubtotal = $this->_calculator->round($baseSubtotal - $baseTaxAmount); } if ($item->hasCustomPrice()) { $item->setCustomPrice($unitPrice); $item->setBaseCustomPrice($baseUnitPrice); } $item->setPrice($baseUnitPrice); $item->setOriginalPrice($unitPrice); $item->setBasePrice($baseUnitPrice); $item->setRowTotal($subtotal); $item->setBaseRowTotal($baseSubtotal); return $this; }
/** * build out an ILineItem payload and add it to the ILineItemIterable. * * @param Mage_Sales_Model_Quote_Item_Abstract * @param ILineItemIterable * @param string * @return self */ public function createLineItem(Mage_Sales_Model_Quote_Item_Abstract $item, ILineItemIterable $lineItems, $currencyCode) { $lineItem = $lineItems->getEmptyLineItem(); $lineItem->setName($this->helper->__($item->getProduct()->getName()))->setSequenceNumber($item->getId())->setQuantity($item->getTotalQty())->setCurrencyCode($currencyCode); if ($this->canIncludeAmounts($item)) { $lineItem->setUnitAmount($item->getPrice()); } $lineItems->offsetSet($lineItem, null); }
/** * Calculate unit tax anount based on unit price * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _calcUnitTaxAmount(Mage_Sales_Model_Quote_Item_Abstract $item, $rate) { $extra = $item->getExtraTaxableAmount(); $baseExtra = $item->getBaseExtraTaxableAmount(); $qty = $item->getTotalQty(); $inclTax = $item->getIsPriceInclTax(); $price = $item->getTaxableAmount(); $basePrice = $item->getBaseTaxableAmount(); $item->setTaxPercent($rate); $hiddenTax = null; $baseHiddenTax = null; switch ($this->_config->getCalculationSequence($this->_store)) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $unitTax = $this->_calculator->calcTaxAmount($price, $rate, $inclTax); $baseUnitTax = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax); break; break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $discountAmount = $item->getDiscountAmount() / $qty; $baseDiscountAmount = $item->getBaseDiscountAmount() / $qty; $unitTax = $this->_calculator->calcTaxAmount(max($price - $discountAmount, 0), $rate, $inclTax); $baseUnitTax = $this->_calculator->calcTaxAmount(max($basePrice - $baseDiscountAmount, 0), $rate, $inclTax); if ($inclTax && $discountAmount > 0) { $hiddenTax = $price - $unitTax - $item->getConvertedPrice(); $baseHiddenTax = $basePrice - $unitTax - $item->getBasePrice(); } elseif ($discountAmount > $price) { // case with 100% discount on price incl. tax $hiddenTax = $discountAmount - $price; $baseHiddenTax = $baseDiscountAmount - $basePrice; } break; } $item->setTaxAmount($this->_store->roundPrice(max(0, $qty * $unitTax))); $item->setBaseTaxAmount($this->_store->roundPrice(max(0, $qty * $baseUnitTax))); $item->setHiddenTaxAmount(max(0, $qty * $hiddenTax)); $item->setBaseHiddenTaxAmount(max(0, $qty * $baseHiddenTax)); return $this; }
/** * Calculates and updates the wee tax based on the customer tax rate and discount for Row * * @param float $discountAmount * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @param bool $base * @return int */ protected function _calculateRowWeeeTax($discountAmount, $item, $rate, $base = true) { //We want to update the weee tax for the unit too. discount amount set on the item is by row $discountAmountByUnit = $discountAmount / ($item->getTotalQty() ? $item->getTotalQty() : 1); $this->_calculateWeeeTax($discountAmountByUnit, $item, $rate, $base); $totalWeeeAmountInclTax = 0; $totalWeeeAmountExclTax = 0; foreach ($this->_weeeHelper->getApplied($item) as $tax) { $weeeAmountInclTax = 0; $weeeAmountExclTax = 0; if ($base) { $weeeAmountInclTax = isset($tax['base_row_amount_incl_tax']) ? $tax['base_row_amount_incl_tax'] : 0; $weeeAmountExclTax = isset($tax['base_row_amount']) ? $tax['base_row_amount'] : 0; } else { $weeeAmountInclTax = isset($tax['row_amount_incl_tax']) ? $tax['row_amount_incl_tax'] : 0; $weeeAmountExclTax = isset($tax['row_amount']) ? $tax['row_amount'] : 0; } $weeeTaxWithOutDiscount = $this->_getWeeeTax($rate, $item, 0, $weeeAmountInclTax, $weeeAmountExclTax); //We want to update the tax calculated on Weee to the Item without discount. //We do not show the discount to the user. $weeeAmountIncludingTax = $weeeTaxWithOutDiscount + $weeeAmountExclTax; if ($base) { $this->_weeeHelper->setWeeeTaxesAppliedProperty($item, $tax['title'], 'base_row_amount_incl_tax', $weeeAmountIncludingTax); } else { $this->_weeeHelper->setWeeeTaxesAppliedProperty($item, $tax['title'], 'row_amount_incl_tax', $weeeAmountIncludingTax); } $totalWeeeAmountInclTax += $weeeAmountInclTax; $totalWeeeAmountExclTax += $weeeAmountExclTax; } return $this->_getWeeeTax($rate, $item, $discountAmount, $totalWeeeAmountInclTax, $totalWeeeAmountExclTax); }
/** * Aggregate row totals per tax rate in array * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @param array $taxGroups * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _aggregateTaxPerRate($item, $rate, &$taxGroups) { $store = $item->getStore(); $inclTax = $this->_usePriceIncludeTax($store); if ($inclTax) { $subtotal = $item->getTaxCalcRowTotal(); $baseSubtotal = $item->getBaseTaxCalcRowTotal(); } else { if ($item->hasCustomPrice() && $this->_helper->applyTaxOnCustomPrice($store)) { $subtotal = $item->getRowTotal(); $baseSubtotal = $item->getBaseRowTotal(); } else { $subtotal = $item->getTotalQty() * $item->getOriginalPrice(); $baseSubtotal = $item->getTotalQty() * $item->getBaseOriginalPrice(); } } $discountAmount = $item->getDiscountAmount(); $baseDiscountAmount = $item->getBaseDiscountAmount(); $qty = $item->getTotalQty(); $rateKey = (string) $rate; /** * Add extra amounts which can be taxable too */ $calcTotal = $subtotal + $item->getExtraRowTaxableAmount(); $baseCalcTotal = $baseSubtotal + $item->getBaseExtraRowTaxableAmount(); $item->setTaxPercent($rate); if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['totals'] = array(); } if (!isset($taxGroups[$rateKey]['totals'])) { $taxGroups[$rateKey]['base_totals'] = array(); } $calculationSequence = $this->_helper->getCalculationSequence($store); switch ($calculationSequence) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); break; case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); $discountPrice = $inclTax ? $subtotal / $qty : ($subtotal + $rowTax) / $qty; $baseDiscountPrice = $inclTax ? $baseSubtotal / $qty : ($baseSubtotal + $baseRowTax) / $qty; $item->setDiscountCalculationPrice($discountPrice); $item->setBaseDiscountCalculationPrice($baseDiscountPrice); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $calcTotal = $calcTotal - $discountAmount; $baseCalcTotal = $baseCalcTotal - $baseDiscountAmount; $rowTax = $this->_calculator->calcTaxAmount($calcTotal, $rate, $inclTax, false); $baseRowTax = $this->_calculator->calcTaxAmount($baseCalcTotal, $rate, $inclTax, false); break; } /** * "Delta" rounding */ $delta = isset($this->_roundingDeltas[$rateKey]) ? $this->_roundingDeltas[$rateKey] : 0; $baseDelta = isset($this->_baseRoundingDeltas[$rateKey]) ? $this->_baseRoundingDeltas[$rateKey] : 0; $rowTax += $delta; $baseRowTax += $baseDelta; $this->_roundingDeltas[$rateKey] = $rowTax - $this->_calculator->round($rowTax); $this->_baseRoundingDeltas[$rateKey] = $baseRowTax - $this->_calculator->round($baseRowTax); $rowTax = $this->_calculator->round($rowTax); $baseRowTax = $this->_calculator->round($baseRowTax); /** * Renew item amounts in case if we are working with price include tax */ if ($inclTax) { $unitTax = $this->_calculator->round($rowTax / $qty); $baseUnitTax = $this->_calculator->round($baseRowTax / $qty); if ($item->hasCustomPrice()) { $item->setCustomPrice($item->getPriceInclTax() - $unitTax); $item->setBaseCustomPrice($item->getBasePriceInclTax() - $baseUnitTax); } else { $item->setOriginalPrice($item->getPriceInclTax() - $unitTax); $item->setPrice($item->getBasePriceInclTax() - $baseUnitTax); $item->setBasePrice($item->getBasePriceInclTax() - $baseUnitTax); } $item->setRowTotal($item->getRowTotalInclTax() - $rowTax); $item->setBaseRowTotal($item->getBaseRowTotalInclTax() - $baseRowTax); } $item->setTaxAmount($rowTax); $item->setBaseTaxAmount($baseRowTax); $taxGroups[$rateKey]['totals'][] = $calcTotal; $taxGroups[$rateKey]['base_totals'][] = $baseCalcTotal; return $this; }
/** * Calculate item fixed tax and prepare information for discount and recular taxation * * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote_Item_Abstract $item * @return Mage_Weee_Model_Total_Quote_Weee */ protected function _process(Mage_Sales_Model_Quote_Address $address, $item) { if (!$this->_helper->isEnabled($this->_store)) { return $this; } $attributes = $this->_helper->getProductWeeeAttributes($item->getProduct(), $address, $address->getQuote()->getBillingAddress(), $this->_store->getWebsiteId()); $applied = array(); $productTaxes = array(); $totalValue = 0; $baseTotalValue = 0; $totalRowValue = 0; $baseTotalRowValue = 0; foreach ($attributes as $k => $attribute) { $baseValue = $attribute->getAmount(); $value = $this->_store->convertPrice($baseValue); $rowValue = $value * $item->getTotalQty(); $baseRowValue = $baseValue * $item->getTotalQty(); $title = $attribute->getName(); $totalValue += $value; $baseTotalValue += $baseValue; $totalRowValue += $rowValue; $baseTotalRowValue += $baseRowValue; $productTaxes[] = array('title' => $title, 'base_amount' => $baseValue, 'amount' => $value, 'row_amount' => $rowValue, 'base_row_amount' => $baseRowValue, 'base_amount_incl_tax' => $baseValue, 'amount_incl_tax' => $value, 'row_amount_incl_tax' => $rowValue, 'base_row_amount_incl_tax' => $baseRowValue); $applied[] = array('id' => $attribute->getCode(), 'percent' => null, 'hidden' => $this->_helper->includeInSubtotal($this->_store), 'rates' => array(array('base_real_amount' => $baseRowValue, 'base_amount' => $baseRowValue, 'amount' => $rowValue, 'code' => $attribute->getCode(), 'title' => $title, 'percent' => null, 'position' => 1, 'priority' => -1000 + $k))); } $item->setWeeeTaxAppliedAmount($totalValue)->setBaseWeeeTaxAppliedAmount($baseTotalValue)->setWeeeTaxAppliedRowAmount($totalRowValue)->setBaseWeeeTaxAppliedRowAmount($baseTotalRowValue); $this->_processTaxSettings($item, $totalValue, $baseTotalValue, $totalRowValue, $baseTotalRowValue)->_processTotalAmount($address, $totalRowValue, $baseTotalRowValue)->_processDiscountSettings($item, $totalValue, $baseTotalValue); $this->_helper->setApplied($item, array_merge($this->_helper->getApplied($item), $productTaxes)); if ($applied) { $this->_saveAppliedTaxes($address, $applied, $item->getWeeeTaxAppliedAmount(), $item->getBaseWeeeTaxAppliedAmount(), null); } }
public function getItemRuleQty(Mage_Sales_Model_Quote_Item_Abstract $item, Mage_SalesRule_Model_Rule $rule) { return $rule->getDiscountQty() ? min($item->getTotalQty(), $rule->getDiscountQty()) : $item->getTotalQty(); }
/** * Fire a legacy event to process this rule for this item * * @param Mage_SalesRule_Model_Rule $rule * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Mage_Sales_Model_Quote_Address $address * * @return bool */ protected function fireLegacyEvent(Mage_SalesRule_Model_Rule $rule, Mage_Sales_Model_Quote_Item_Abstract $item, Mage_Sales_Model_Quote_Address $address) { // Prepare values for the event $quote = $address->getQuote(); $qty = $rule->getDiscountQty() ? min($item->getTotalQty(), $rule->getDiscountQty()) : $item->getTotalQty(); // Prepare result object $result = new Varien_Object(['discount_amount' => 0.0, 'base_discount_amount' => 0.0, 'applied' => false]); // Fire legacy event Mage::dispatchEvent('salesrule_validator_process', ['quote' => $quote, 'address' => $address, 'rule' => $rule, 'item' => $item, 'qty' => $qty, 'result' => $result]); // Save discount amounts $item->setDiscountAmount($item->getDiscountAmount() + $result->getData('discount_amount')); $item->setBaseDiscountAmount($item->getBaseDiscountAmount() + $result->getData('base_discount_amount')); // brain-dead check to see if a rule was applied return $result->getData('applied') || $result->getData('discount_amount') != 0.0; }
/** * Calculate item price and row total including/excluding tax based on total price rounding level * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param Varien_Object $request * @return Mage_Tax_Model_Sales_Total_Quote_Subtotal */ protected function _totalBaseCalculation($item, $request) { $calc = $this->_calculator; $request->setProductClassId($item->getProduct()->getTaxClassId()); $rate = $calc->getRate($request); $qty = $item->getTotalQty(); $price = $taxPrice = $item->getCalculationPriceOriginal(); $basePrice = $baseTaxPrice = $item->getBaseCalculationPriceOriginal(); $subtotal = $taxSubtotal = $item->getRowTotal(); $baseSubtotal = $baseTaxSubtotal = $item->getBaseRowTotal(); $taxOnOrigPrice = !$this->_helper->applyTaxOnCustomPrice($this->_store) && $item->hasCustomPrice(); if ($taxOnOrigPrice) { $origSubtotal = $item->getOriginalPrice() * $qty; $baseOrigSubtotal = $item->getBaseOriginalPrice() * $qty; } $item->setTaxPercent($rate); if ($this->_config->priceIncludesTax($this->_store)) { if ($this->_sameRateAsStore($request)) { if ($taxOnOrigPrice) { $rowTax = $this->_deltaRound($calc->calcTaxAmount($origSubtotal, $rate, true, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseOrigSubtotal, $rate, true, false), $rate, true, 'base'); $taxable = $origSubtotal; $baseTaxable = $baseOrigSubtotal; } else { $rowTax = $this->_deltaRound($calc->calcTaxAmount($subtotal, $rate, true, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseSubtotal, $rate, true, false), $rate, true, 'base'); $taxable = $subtotal; $baseTaxable = $baseSubtotal; } $taxPrice = $price; $baseTaxPrice = $basePrice; $taxSubtotal = $subtotal; $baseTaxSubtotal = $baseSubtotal; $subtotal = $subtotal - $rowTax; $baseSubtotal = $baseSubtotal - $baseRowTax; $price = $calc->round($subtotal / $qty); $basePrice = $calc->round($baseSubtotal / $qty); $isPriceInclTax = true; } else { $storeRate = $calc->getStoreRate($request, $this->_store); if ($taxOnOrigPrice) { $storeTax = $calc->calcTaxAmount($origSubtotal, $storeRate, true, false); $baseStoreTax = $calc->calcTaxAmount($baseOrigSubtotal, $storeRate, true, false); } else { $storeTax = $calc->calcTaxAmount($subtotal, $storeRate, true, false); $baseStoreTax = $calc->calcTaxAmount($baseSubtotal, $storeRate, true, false); } $subtotal = $calc->round($subtotal - $storeTax); $baseSubtotal = $calc->round($baseSubtotal - $baseStoreTax); $price = $calc->round($subtotal / $qty); $basePrice = $calc->round($baseSubtotal / $qty); $rowTax = $this->_deltaRound($calc->calcTaxAmount($subtotal, $rate, false, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseSubtotal, $rate, false, false), $rate, true, 'base'); $taxSubtotal = $subtotal + $rowTax; $baseTaxSubtotal = $baseSubtotal + $baseRowTax; $taxPrice = $calc->round($taxSubtotal / $qty); $baseTaxPrice = $calc->round($baseTaxSubtotal / $qty); $taxable = $subtotal; $baseTaxable = $baseSubtotal; $isPriceInclTax = false; } } else { if ($taxOnOrigPrice) { $rowTax = $this->_deltaRound($calc->calcTaxAmount($origSubtotal, $rate, false, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseOrigSubtotal, $rate, false, false), $rate, true, 'base'); $taxable = $origSubtotal; $baseTaxable = $baseOrigSubtotal; } else { $rowTax = $this->_deltaRound($calc->calcTaxAmount($subtotal, $rate, false, false), $rate, true); $baseRowTax = $this->_deltaRound($calc->calcTaxAmount($baseSubtotal, $rate, false, false), $rate, true, 'base'); $taxable = $subtotal; $baseTaxable = $baseSubtotal; } $taxSubtotal = $subtotal + $rowTax; $baseTaxSubtotal = $baseSubtotal + $baseRowTax; $taxPrice = $calc->round($taxSubtotal / $qty); $baseTaxPrice = $calc->round($baseTaxSubtotal / $qty); $isPriceInclTax = false; } if ($item->hasCustomPrice()) { /** * Initialize item original price before declaring custom price */ $item->getOriginalPrice(); $item->setCustomPrice($price); $item->setBaseCustomPrice($basePrice); } else { $item->setConvertedPrice($price); } $item->setPrice($basePrice); $item->setBasePrice($basePrice); $item->setRowTotal($subtotal); $item->setBaseRowTotal($baseSubtotal); $item->setPriceInclTax($taxPrice); $item->setBasePriceInclTax($baseTaxPrice); $item->setRowTotalInclTax($taxSubtotal); $item->setBaseRowTotalInclTax($baseTaxSubtotal); $item->setTaxableAmount($taxable); $item->setBaseTaxableAmount($baseTaxable); $item->setIsPriceInclTax($isPriceInclTax); if ($this->_config->discountTax($this->_store)) { $item->setDiscountCalculationPrice($taxSubtotal / $qty); $item->setBaseDiscountCalculationPrice($baseTaxSubtotal / $qty); } elseif ($isPriceInclTax) { $item->setDiscountCalculationPrice($subtotal / $qty); $item->setBaseDiscountCalculationPrice($baseSubtotal / $qty); } return $this; }
/** * Inject general item data into the order item payload. * * @return self */ protected function _injectItemData() { $this->_orderItem->setLineNumber($this->_item->getId())->setItemId($this->_item->getSku())->setQuantity((int) $this->_item->getTotalQty())->setDescription($this->_item->getName())->setHtsCode($this->_taxHelper->getProductHtsCodeByCountry($this->_itemProduct, $this->_address->getCountryId()))->setManufacturingCountryCode($this->_itemProduct->getCountryOfManufacture()); return $this; }
/** * Calculate unit tax anount based on unit price * * @param Mage_Sales_Model_Quote_Item_Abstract $item * @param float $rate * @return Mage_Tax_Model_Sales_Total_Quote */ protected function _calcUnitTaxAmount(Mage_Sales_Model_Quote_Item_Abstract $item, $rate) { $qty = $item->getTotalQty(); $inclTax = $item->getIsPriceInclTax(); $price = $item->getTaxableAmount(); $basePrice = $item->getBaseTaxableAmount(); $rateKey = (string) $rate; $item->setTaxPercent($rate); $isWeeeEnabled = $this->_weeeHelper->isEnabled(); $isWeeeTaxable = $this->_weeeHelper->isTaxable(); $hiddenTax = null; $baseHiddenTax = null; switch ($this->_config->getCalculationSequence($this->_store)) { case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL: $unitTax = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false); $baseUnitTax = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax, false); if ($isWeeeEnabled && $isWeeeTaxable) { $unitTax += $item->getWeeeTaxAppliedAmount() * $rate / 100; $baseUnitTax += $item->getBaseWeeeTaxAppliedAmount() * $rate / 100; } $unitTax = $this->_calculator->round($unitTax); $baseUnitTax = $this->_calculator->round($baseUnitTax); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $discountAmount = $item->getDiscountAmount() / $qty; $baseDiscountAmount = $item->getBaseDiscountAmount() / $qty; if ($isWeeeEnabled) { $discountAmount = $discountAmount - $item->getWeeeDiscount() / $qty; $baseDiscountAmount = $baseDiscountAmount - $item->getBaseWeeeDiscount() / $qty; } $unitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false); $unitTaxDiscount = $this->_calculator->calcTaxAmount($discountAmount, $rate, $inclTax, false); $unitTax = $this->_calculator->round(max($unitTaxBeforeDiscount - $unitTaxDiscount, 0)); $baseUnitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax, false); $baseUnitTaxDiscount = $this->_calculator->calcTaxAmount($baseDiscountAmount, $rate, $inclTax, false); $baseUnitTax = $this->_calculator->round(max($baseUnitTaxBeforeDiscount - $baseUnitTaxDiscount, 0)); if ($isWeeeEnabled && $this->_weeeHelper->isTaxable()) { $weeeTax = ($item->getWeeeTaxAppliedRowAmount() - $item->getWeeeDiscount()) * $rate / 100; $weeeTax = $weeeTax / $qty; $unitTax += $weeeTax; $baseWeeeTax = ($item->getBaseWeeeTaxAppliedRowAmount() - $item->getBaseWeeeDiscount()) * $rate / 100; $baseWeeeTax = $baseWeeeTax / $qty; $baseUnitTax += $baseWeeeTax; } $unitTax = $this->_calculator->round($unitTax); $baseUnitTax = $this->_calculator->round($baseUnitTax); if ($inclTax && $discountAmount > 0) { $hiddenTax = $this->_calculator->round($unitTaxBeforeDiscount) - $unitTax; $baseHiddenTax = $this->_calculator->round($baseUnitTaxBeforeDiscount) - $baseUnitTax; $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => $qty, 'item' => $item, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } elseif ($discountAmount > $price) { // case with 100% discount on price incl. tax $hiddenTax = $discountAmount - $price; $baseHiddenTax = $baseDiscountAmount - $basePrice; $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'qty' => $qty, 'item' => $item, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } // calculate discount compensation if (!$item->getNoDiscount() && $item->getWeeeTaxApplied()) { $unitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false); $baseUnitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false); if ($isWeeeTaxable) { $unitTaxBeforeDiscount += $item->getWeeeTaxAppliedAmount() * $rate / 100; $baseUnitTaxBeforeDiscount += $item->getBaseWeeeTaxAppliedAmount() * $rate / 100; } $unitTaxBeforeDiscount = max(0, $this->_calculator->round($unitTaxBeforeDiscount)); $baseUnitTaxBeforeDiscount = max(0, $this->_calculator->round($baseUnitTaxBeforeDiscount)); $item->setDiscountTaxCompensation($unitTaxBeforeDiscount * $qty - max(0, $unitTax) * $qty); $item->setBaseDiscountTaxCompensation($baseUnitTaxBeforeDiscount * $qty - max(0, $baseUnitTax) * $qty); } break; } $item->setTaxAmount($this->_store->roundPrice(max(0, $qty * $unitTax))); $item->setBaseTaxAmount($this->_store->roundPrice(max(0, $qty * $baseUnitTax))); $rowTotalInclTax = $item->getRowTotalInclTax(); if (!isset($rowTotalInclTax)) { if ($this->_config->priceIncludesTax($this->_store)) { $weeeTaxBeforeDiscount = 0; $baseWeeeTaxBeforeDiscount = 0; if ($isWeeeTaxable) { $weeeTaxBeforeDiscount = $item->getWeeeTaxAppliedRowAmount() * $rate / 100; $baseWeeeTaxBeforeDiscount = $item->getBaseWeeeTaxAppliedRowAmount() * $rate / 100; } $item->setRowTotalInclTax($price * $qty + $weeeTaxBeforeDiscount); $item->setBaseRowTotalInclTax($basePrice * $qty + $baseWeeeTaxBeforeDiscount); } else { $taxCompensation = $item->getDiscountTaxCompensation() ? $item->getDiscountTaxCompensation() : 0; $item->setRowTotalInclTax($price * $qty + $unitTax * $qty + $taxCompensation); $item->setBaseRowTotalInclTax($basePrice * $qty + $baseUnitTax * $qty + $item->getBaseDiscountTaxCompensation()); } } return $this; }