/** * * @param Mage_Sales_Model_Quote_Address $address * @param float $rate * @param array $appliedRates * @param string $taxId */ protected function _calculateShippingTaxByRate(Mage_Sales_Model_Quote_Address $address, $rate, $appliedRates, $taxId = null) { $inclTax = $address->getIsShippingInclTax(); $shipping = $address->getShippingTaxable(); $baseShipping = $address->getBaseShippingTaxable(); $rateKey = $taxId == null ? (string) $rate : $taxId; $hiddenTax = null; $baseHiddenTax = null; switch ($this->_helper->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: $tax = $this->_calculator->calcTaxAmount($shipping, $rate, $inclTax, false); $baseTax = $this->_calculator->calcTaxAmount($baseShipping, $rate, $inclTax, false); break; case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL: case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL: $discountAmount = $address->getShippingDiscountAmount(); $baseDiscountAmount = $address->getBaseShippingDiscountAmount(); $tax = $this->_calculator->calcTaxAmount($shipping - $discountAmount, $rate, $inclTax, false); $baseTax = $this->_calculator->calcTaxAmount($baseShipping - $baseDiscountAmount, $rate, $inclTax, false); break; } if ($this->_config->getAlgorithm($this->_store) == Mage_Tax_Model_Calculation::CALC_TOTAL_BASE) { $tax = $this->_deltaRound($tax, $rateKey, $inclTax); $baseTax = $this->_deltaRound($baseTax, $rateKey, $inclTax, 'base'); $this->_addAmount(max(0, $tax)); $this->_addBaseAmount(max(0, $baseTax)); } else { $tax = $this->_calculator->round($tax); $baseTax = $this->_calculator->round($baseTax); $this->_addAmount(max(0, $tax)); $this->_addBaseAmount(max(0, $baseTax)); } if ($inclTax && !empty($discountAmount)) { $taxBeforeDiscount = $this->_calculator->calcTaxAmount($shipping, $rate, $inclTax, false); $baseTaxBeforeDiscount = $this->_calculator->calcTaxAmount($baseShipping, $rate, $inclTax, false); if ($this->_config->getAlgorithm($this->_store) == Mage_Tax_Model_Calculation::CALC_TOTAL_BASE) { $taxBeforeDiscount = $this->_deltaRound($taxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount'); $baseTaxBeforeDiscount = $this->_deltaRound($baseTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount_base'); } else { $taxBeforeDiscount = $this->_calculator->round($taxBeforeDiscount); $baseTaxBeforeDiscount = $this->_calculator->round($baseTaxBeforeDiscount); } $hiddenTax = max(0, $taxBeforeDiscount - max(0, $tax)); $baseHiddenTax = max(0, $baseTaxBeforeDiscount - max(0, $baseTax)); $this->_hiddenTaxes[] = array('rate_key' => $rateKey, 'value' => $hiddenTax, 'base_value' => $baseHiddenTax, 'incl_tax' => $inclTax); } $address->setShippingTaxAmount($address->getShippingTaxAmount() + max(0, $tax)); $address->setBaseShippingTaxAmount($address->getBaseShippingTaxAmount() + max(0, $baseTax)); $this->_saveAppliedTaxes($address, $appliedRates, $tax, $baseTax, $rate); }
/** * update earning points for address items * * @param Mage_Sales_Model_Quote_Address $address * @return Magestore_RewardPoints_Model_Total_Quote_Earning */ protected function _updateEarningPoints($address) { $items = $address->getAllItems(); $earningPoints = $address->getRewardpointsEarn(); if (!count($items) || $earningPoints <= 0) { return $this; } // Calculate total item prices $baseItemsPrice = 0; $totalItemsQty = 0; $isBaseOnQty = false; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $baseItemsPrice += $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount()); $totalItemsQty += $item->getQty() * $child->getQty(); } } elseif ($item->getProduct()) { $baseItemsPrice += $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount(); $totalItemsQty += $item->getQty(); } } $baseItemsPrice += $address->getBaseShippingAmount() + $address->getBaseShippingTaxAmount(); if ($baseItemsPrice < 0.0001) { $isBaseOnQty = true; } // Update for items foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $baseItemPrice = $item->getQty() * ($child->getQty() * $child->getBasePriceInclTax() - $child->getBaseDiscountAmount()); $itemQty = $item->getQty() * $child->getQty(); if ($isBaseOnQty) { $itemEarning = round($itemQty * $earningPoints / $totalItemsQty, 0, PHP_ROUND_HALF_DOWN); } else { $itemEarning = round($baseItemPrice * $earningPoints / $baseItemsPrice, 0, PHP_ROUND_HALF_DOWN); } $child->setRewardpointsEarn($itemEarning); } } elseif ($item->getProduct()) { $baseItemPrice = $item->getQty() * $item->getBasePriceInclTax() - $item->getBaseDiscountAmount(); $itemQty = $item->getQty(); if ($isBaseOnQty) { $itemEarning = round($itemQty * $earningPoints / $totalItemsQty, 0, PHP_ROUND_HALF_DOWN); } else { $itemEarning = round($baseItemPrice * $earningPoints / $baseItemsPrice, 0, PHP_ROUND_HALF_DOWN); } $item->setRewardpointsEarn($itemEarning); } } return $this; }
/** * Prepare Discount Amount used for Tax * * @param Mage_Sales_Model_Quote_Address $address * @param type $baseDiscount * @return Magestore_RewardPoints_Model_Total_Quote_Point */ public function _prepareDiscountForTaxAmount(Mage_Sales_Model_Quote_Address $address, $baseDiscount, $points) { $items = $address->getAllItems(); if (!count($items)) { return $this; } // Calculate total item prices $baseItemsPrice = 0; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $baseItemsPrice += $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount() + $child->getBaseTaxAmount()); } } elseif ($item->getProduct()) { $baseItemsPrice += $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount() - $item->getRewardpointsBaseDiscount() + $item->getBaseTaxAmount(); } } $discountForShipping = Mage::getStoreConfig(Magestore_RewardPoints_Helper_Calculation_Spending::XML_PATH_SPEND_FOR_SHIPPING, $address->getQuote()->getStoreId()); $discountForShippingTax = Mage::getStoreConfig(Magestore_RewardPoints_Helper_Calculation_Spending::XML_PATH_SPEND_FOR_SHIPPING_TAX, $address->getQuote()->getStoreId()); if ($discountForShipping) { $baseItemsPrice += $address->getBaseShippingAmount(); if ($discountForShippingTax) { $baseItemsPrice += $address->getBaseShippingTaxAmount(); } } if ($baseItemsPrice < 0.0001) { return $this; } // Update discount for each item foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $baseItemPrice = $item->getQty() * ($child->getQty() * $child->getBasePrice() - $child->getBaseDiscountAmount() + $child->getBaseTaxAmount()); $itemBaseDiscount = $baseDiscount * $baseItemPrice / $baseItemsPrice; $itemDiscount = Mage::app()->getStore()->convertPrice($itemBaseDiscount); $pointSpent = round($points * $baseItemPrice / $baseItemsPrice, 0, PHP_ROUND_HALF_DOWN); $child->setRewardpointsBaseDiscount($child->getRewardpointsBaseDiscount() + $itemBaseDiscount)->setRewardpointsDiscount($child->getRewardpointsDiscount() + $itemDiscount)->setMagestoreBaseDiscount($child->getMagestoreBaseDiscount() + $itemBaseDiscount)->setRewardpointsSpent($child->getRewardpointsSpent() + $pointSpent); } } elseif ($item->getProduct()) { $baseItemPrice = $item->getQty() * $item->getBasePrice() - $item->getBaseDiscountAmount() - $item->getRewardpointsBaseDiscount() + $item->getBaseTaxAmount(); $itemBaseDiscount = $baseDiscount * $baseItemPrice / $baseItemsPrice; $itemDiscount = Mage::app()->getStore()->convertPrice($itemBaseDiscount); $pointSpent = round($points * $baseItemPrice / $baseItemsPrice, 0, PHP_ROUND_HALF_DOWN); $item->setRewardpointsBaseDiscount($item->getRewardpointsBaseDiscount() + $itemBaseDiscount)->setRewardpointsDiscount($item->getRewardpointsDiscount() + $itemDiscount)->setMagestoreBaseDiscount($item->getMagestoreBaseDiscount() + $itemBaseDiscount)->setRewardpointsSpent($item->getRewardpointsSpent() + $pointSpent); } } if ($discountForShipping) { $shipping = $address->getBaseShippingAmount(); if ($discountForShippingTax) { $shipping += $address->getBaseShippingTaxAmount(); } $itemBaseDiscount = $baseDiscount * $shipping / $baseItemsPrice; $itemDiscount = Mage::app()->getStore()->convertPrice($itemBaseDiscount); $address->setRewardpointsBaseAmount($itemBaseDiscount)->setMagestoreBaseDiscountForShipping($itemBaseDiscount)->setRewardpointsAmount($itemDiscount); } return $this; }
public function _prepareDiscountCreditForAmount(Mage_Sales_Model_Quote_Address $address, $rate, $baseCustomercreditForShipping) { // Update discount for each item $helper = Mage::helper('customercredit'); foreach ($address->getAllItems() as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { if (!$child->isDeleted() && $child->getProduct()->getTypeId() != 'customercredit') { $baseItemPrice = $child->getBaseRowTotal() + $child->getBaseTaxAmount() - $child->getBaseDiscountAmount() - $child->getMagestoreBaseDiscount(); $itemBaseDiscount = $baseItemPrice * $rate; $itemDiscount = Mage::app()->getStore()->convertPrice($itemBaseDiscount); $child->setMagestoreBaseDiscount($child->getMagestoreBaseDiscount() + $itemBaseDiscount); $child->setBaseCustomercreditDiscount($itemBaseDiscount)->setCustomercreditDiscount($itemDiscount); } } } else { if ($item->getProduct()) { if (!$item->isDeleted() && $item->getProduct()->getTypeId() != 'customercredit') { $baseItemPrice = $item->getBaseRowTotal() + $item->getBaseTaxAmount() - $item->getBaseDiscountAmount() - $item->getMagestoreBaseDiscount(); $itemBaseDiscount = $baseItemPrice * $rate; $itemDiscount = Mage::app()->getStore()->convertPrice($itemBaseDiscount); $item->setMagestoreBaseDiscount($item->getMagestoreBaseDiscount() + $itemBaseDiscount); $item->setBaseCustomercreditDiscount($itemBaseDiscount)->setCustomercreditDiscount($itemDiscount); } } } } if ($helper->getSpendConfig('shipping') && $baseCustomercreditForShipping) { $baseShippingPrice = $address->getBaseShippingAmount() + $address->getBaseShippingTaxAmount() - $address->getBaseShippingDiscountAmount() - $address->getMagestoreBaseDiscountForShipping(); $baseShippingDiscount = min($baseShippingPrice, $baseCustomercreditForShipping); $shippingDiscount = Mage::app()->getStore()->convertPrice($baseShippingDiscount); $address->setMagestoreBaseDiscountForShipping($address->getMagestoreBaseDiscountForShipping() + $baseShippingDiscount); $address->setBaseCustomercreditDiscountForShipping($baseShippingDiscount); $address->setCustomercreditDiscountForShipping($shippingDiscount); } return $this; }