public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo) { $result = parent::collect($creditmemo); // We added taxes on delivery surcharge $creditmemo->setData("zitec_dpd_cashondelivery_surcharge_tax", 0); $creditmemo->setData("base_zitec_dpd_cashondelivery_surcharge_tax", 0); if ($creditmemo->getData("zitec_dpd_cashondelivery_surcharge")) { $baseTax = $creditmemo->getOrder()->getData("base_zitec_dpd_cashondelivery_surcharge_tax"); $creditmemo->setData("base_zitec_dpd_cashondelivery_surcharge_tax", $baseTax); $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTax); $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTax); $tax = $creditmemo->getOrder()->getData("zitec_dpd_cashondelivery_surcharge_tax"); $creditmemo->setData("zitec_dpd_cashondelivery_surcharge_tax", $tax); $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $tax); $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $tax); } return $result; }
public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo) { $order = $creditmemo->getOrder(); $orderTaxPerc = 0; $taxDetails = $order->getFullTaxInfo(); if (count($taxDetails)) { $det = array_pop($taxDetails); $orderTaxPerc = $det['percent']; } // Adding adjustment tax amounts to total tax $totalAdjusment = $creditmemo->getAdjustmentPositive() - $creditmemo->getAdjustmentNegative(); $baseTotalAdjusment = $creditmemo->getBaseAdjustmentPositive() - $creditmemo->getBaseAdjustmentNegative(); // Adjustment values already include tax in my case. Modify calculation if you're entering values without tax $totalAdjusmentTax = $totalAdjusment - $totalAdjusment * 100 / (100 + $orderTaxPerc); $baseTotalAdjusmentTax = $baseTotalAdjusment - $baseTotalAdjusment * 100 / (100 + $orderTaxPerc); parent::collect($creditmemo); $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalAdjusmentTax); $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalAdjusmentTax); return $this; }
/** * Collects the total tax for the credit memo * * @param Mage_Sales_Model_Order_Creditmemo $creditmemo * * @return $this * * @throws Aoe_AvaTax_Exception */ public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo) { $store = $creditmemo->getStore(); if (!$this->getHelper()->isActive($store)) { return parent::collect($creditmemo); } $creditmemo->setTaxAmount(0.0); $creditmemo->setBaseTaxAmount(0.0); $creditmemo->setHiddenTaxAmount(0.0); $creditmemo->setBaseHiddenTaxAmount(0.0); $items = $this->getHelper()->getActionableCreditmemoItems($creditmemo); // Get taxes via API call $api = $this->getHelper()->getApi($store); $result = $api->callGetTaxForCreditmemo($creditmemo, false); if ($result['ResultCode'] !== 'Success') { throw new Aoe_AvaTax_Exception($result['ResultCode'], $result['Messages']); } $totalTax = 0; $baseTotalTax = 0; /** @var Mage_Tax_Model_Config $taxConfig */ $taxConfig = Mage::getSingleton('tax/config'); $hasDisplayCurrency = $creditmemo->getBaseCurrencyCode() !== $creditmemo->getOrderCurrencyCode(); $exchangeRate = $hasDisplayCurrency ? $creditmemo->getBaseToOrderRate() : 1.0; $shippingPriceIncludesTax = $taxConfig->shippingPriceIncludesTax($store); $itemPriceIncludesTax = $taxConfig->priceIncludesTax($store); foreach ($result['TaxLines'] as $line) { $itemId = $line['LineNo']; $chargeTax = $store->roundPrice(floatval($line['Tax'])); switch ($itemId) { case 'SHIPPING': // Store the tax amount $creditmemo->setBaseShippingTaxAmount(-$chargeTax); $creditmemo->setShippingTaxAmount($store->roundPrice(-$chargeTax * $exchangeRate)); // Update shipping totals if ($shippingPriceIncludesTax) { $creditmemo->setBaseShippingAmount($creditmemo->getBaseShippingInclTax() - $creditmemo->getBaseShippingTaxAmount()); $creditmemo->setShippingAmount($creditmemo->getShippingInclTax() - $creditmemo->getShippingTaxAmount()); } else { $creditmemo->setBaseShippingInclTax($creditmemo->getBaseShippingAmount() + $creditmemo->getBaseShippingTaxAmount()); $creditmemo->setShippingInclTax($creditmemo->getShippingAmount() + $creditmemo->getShippingTaxAmount()); } // Add shipping tax to total $baseTotalTax += $creditmemo->getBaseShippingTaxAmount(); $totalTax += $creditmemo->getShippingTaxAmount(); break; default: /** @var Mage_Sales_Model_Order_Creditmemo_Item $item */ $item = isset($items[$itemId]) ? $items[$itemId] : false; if (!$item) { continue; } // Store the tax amount $item->setBaseTaxAmount(-$chargeTax); $item->setTaxAmount($store->roundPrice(-$chargeTax * $exchangeRate)); if ($itemPriceIncludesTax) { $item->setBaseRowTotal($item->getBaseRowTotalInclTax() - $item->getBaseTaxAmount()); $item->setRowTotal($item->getRowTotalInclTax() - $item->getTaxAmount()); } else { $item->setBaseRowTotalInclTax($item->getBaseRowTotal() + $item->getBaseTaxAmount()); $item->setRowTotalInclTax($item->getRowTotal() + $item->getTaxAmount()); } // Add item tax to tax total $baseTotalTax += $item->getBaseTaxAmount(); $totalTax += $item->getTaxAmount(); } } $creditmemo->setTaxAmount($totalTax); $creditmemo->setBaseTaxAmount($baseTotalTax); $creditmemo->setHiddenTaxAmount(0.0); $creditmemo->setBaseHiddenTaxAmount(0.0); $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax); $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax); if ($creditmemo->getCommitTaxDocuments()) { $creditmemo->setAvataxDocument($result['DocCode']); } }