Answers:
您将在该类中找到处理此问题的代码Mage_Sales_Model_Order_Creditmemo_Total_Tax
。
该行代码$part = $creditmemo->getShippingAmount()/$orderShippingAmount;
(位于第116行)清楚地表明,这是经过专门编码的,仅用于根据creditMemo表单中的shippingAmount字段计算税款。
明显的变化是将代码调整为也使用“调整退款”字段。
由于magento如何在creditmemo计算中将其实例化为Totals Collector子系统的一部分,因此您实际上无法重写此类。
但是,您可以调整收集器以使用您自己的类的版本,因此不会丢失所有内容。
因此,在您自己的模块中,将以下代码放置在config.xml中。它进入<global>
元素内部
<global>
<sales>
<order_creditmemo>
<totals>
<tax>
<class>NAMESPACE_MODULE/order_creditmemo_total_tax</class>
<after>subtotal</after>
</tax>
</totals>
</order_creditmemo>
</sales>
<global>
现在,您将创建类文件NAMESPACE/MODULE/Model/Order/Creditmemo/Total/Tax
,该文件扩展了核心文件。
class NAMESPACE_MODULE_Model_Order_Creditmemo_Total_Tax extends Mage_Sales_Model_Order_Creditmemo_Total_Tax
您将需要将核心类的整个`collect'方法复制到新文件中。
在第114行添加以下代码(紧随该代码之后$shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount
)
/** adjust to also calculate tax on the adjustment value **/
$adjustment = ($creditmemo->getAdjustment() > 0)?$creditmemo->getAdjustment():$creditmemo->getShippingAmount();
if($creditmemo->getAdjustment() > 0 && $creditmemo->getShippingAmount() > 0) {
$adjustment = $creditmemo->getAdjustment() + $creditmemo->getShippingAmount();
}
/** end adjustment **/
并将线116从调整$part = $creditmemo->getShippingAmount()/$orderShippingAmount;
为$part = $adjustment/$orderShippingAmount;
然后,这将有效地在计算中使用“装运金额”或“调整金额”。
实质性的问题是,magento不知道要使用哪个税收因素。如果没有可退款的产品,则没有税率。
我只是使用我可以在产品中找到的最高税率来解决此问题,请随时根据您的用例进行调整。
该税是在课程结束时在calculateTaxForRefundAdjustment中计算的。
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Project_RefundPartialCreditmemoWithTax>
<version>1.0.0</version>
</Project_RefundPartialCreditmemoWithTax>
</modules>
<global>
<models>
<project_refundpartialcreditmemowithtax>
<class>Project_RefundPartialCreditmemoWithTax_Model</class>
</project_refundpartialcreditmemowithtax>
</models>
<sales>
<order_creditmemo>
<totals>
<tax>
<class>project_refundpartialcreditmemowithtax/order_creditmemo_total_tax</class>
</tax>
</totals>
</order_creditmemo>
</sales>
</global>
</config>
app / code / local / Project / RefundPartialCreditmemoWithTax / Model / Order / Creditmemo / Total / Tax.php
<?php
class Project_RefundPartialCreditmemoWithTax_Model_Order_Creditmemo_Total_Tax
extends Mage_Sales_Model_Order_Creditmemo_Total_Tax
{
public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$shippingTaxAmount = 0;
$baseShippingTaxAmount = 0;
$totalHiddenTax = 0;
$baseTotalHiddenTax = 0;
$order = $creditmemo->getOrder();
list($totalTax, $baseTotalTax) = $this->calculateTaxForRefundAdjustment($creditmemo);
/** @var $item Mage_Sales_Model_Order_Creditmemo_Item */
foreach ($creditmemo->getAllItems() as $item) {
$orderItem = $item->getOrderItem();
if ($orderItem->isDummy()) {
continue;
}
$orderItemTax = $orderItem->getTaxInvoiced();
$baseOrderItemTax = $orderItem->getBaseTaxInvoiced();
$orderItemHiddenTax = $orderItem->getHiddenTaxInvoiced();
$baseOrderItemHiddenTax = $orderItem->getBaseHiddenTaxInvoiced();
$orderItemQty = $orderItem->getQtyInvoiced();
if (($orderItemTax || $orderItemHiddenTax) && $orderItemQty) {
/**
* Check item tax amount
*/
$tax = $orderItemTax - $orderItem->getTaxRefunded();
$baseTax = $baseOrderItemTax - $orderItem->getTaxRefunded();
$hiddenTax = $orderItemHiddenTax - $orderItem->getHiddenTaxRefunded();
$baseHiddenTax = $baseOrderItemHiddenTax - $orderItem->getBaseHiddenTaxRefunded();
if (!$item->isLast()) {
$availableQty = $orderItemQty - $orderItem->getQtyRefunded();
$tax = $creditmemo->roundPrice($tax / $availableQty * $item->getQty());
$baseTax = $creditmemo->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
$hiddenTax = $creditmemo->roundPrice($hiddenTax / $availableQty * $item->getQty());
$baseHiddenTax = $creditmemo->roundPrice($baseHiddenTax / $availableQty * $item->getQty(), 'base');
}
$item->setTaxAmount($tax);
$item->setBaseTaxAmount($baseTax);
$item->setHiddenTaxAmount($hiddenTax);
$item->setBaseHiddenTaxAmount($baseHiddenTax);
$totalTax += $tax;
$baseTotalTax += $baseTax;
$totalHiddenTax += $hiddenTax;
$baseTotalHiddenTax += $baseHiddenTax;
}
}
$invoice = $creditmemo->getInvoice();
if ($invoice) {
//recalculate tax amounts in case if refund shipping value was changed
if ($order->getBaseShippingAmount() && $creditmemo->getBaseShippingAmount()) {
$taxFactor = $creditmemo->getBaseShippingAmount() / $order->getBaseShippingAmount();
$shippingTaxAmount = $invoice->getShippingTaxAmount() * $taxFactor;
$baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount() * $taxFactor;
$totalHiddenTax += $invoice->getShippingHiddenTaxAmount() * $taxFactor;
$baseTotalHiddenTax += $invoice->getBaseShippingHiddenTaxAmount() * $taxFactor;
$shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
$baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
$totalHiddenTax = $creditmemo->roundPrice($totalHiddenTax);
$baseTotalHiddenTax = $creditmemo->roundPrice($baseTotalHiddenTax, 'base');
$totalTax += $shippingTaxAmount;
$baseTotalTax += $baseShippingTaxAmount;
}
} else {
$orderShippingAmount = $order->getShippingAmount();
$baseOrderShippingAmount = $order->getBaseShippingAmount();
$baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
$shippingTaxAmount = 0;
$baseShippingTaxAmount = 0;
$shippingHiddenTaxAmount = 0;
$baseShippingHiddenTaxAmount = 0;
$shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount;
if ($shippingDelta > $creditmemo->getBaseShippingAmount()) {
$part = $creditmemo->getShippingAmount() / $orderShippingAmount;
$basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
$shippingTaxAmount = $order->getShippingTaxAmount() * $part;
$baseShippingTaxAmount = $order->getBaseShippingTaxAmount() * $basePart;
$shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount() * $part;
$baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount() * $basePart;
$shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
$baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
$shippingHiddenTaxAmount = $creditmemo->roundPrice($shippingHiddenTaxAmount);
$baseShippingHiddenTaxAmount = $creditmemo->roundPrice($baseShippingHiddenTaxAmount, 'base');
} elseif ($shippingDelta == $creditmemo->getBaseShippingAmount()) {
$shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
$baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
$shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount()
- $order->getShippingHiddenTaxRefunded();
$baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount()
- $order->getBaseShippingHiddenTaxRefunded();
}
$totalTax += $shippingTaxAmount;
$baseTotalTax += $baseShippingTaxAmount;
$totalHiddenTax += $shippingHiddenTaxAmount;
$baseTotalHiddenTax += $baseShippingHiddenTaxAmount;
}
$allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
$allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded()
- $creditmemo->getBaseTaxAmount();
$allowedHiddenTax = $order->getHiddenTaxInvoiced() + $order->getShippingHiddenTaxAmount()
- $order->getHiddenTaxRefunded() - $order->getShippingHiddenTaxRefunded();
$allowedBaseHiddenTax = $order->getBaseHiddenTaxInvoiced() + $order->getBaseShippingHiddenTaxAmount()
- $order->getBaseHiddenTaxRefunded() - $order->getBaseShippingHiddenTaxRefunded();
$totalTax = min($allowedTax, $totalTax);
$baseTotalTax = min($allowedBaseTax, $baseTotalTax);
$totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
$baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
$creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
$creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
$creditmemo->setHiddenTaxAmount($totalHiddenTax);
$creditmemo->setBaseHiddenTaxAmount($baseTotalHiddenTax);
$creditmemo->setShippingTaxAmount($shippingTaxAmount);
$creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
$creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalHiddenTax);
$creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
return $this;
}
/**
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
* @return array
*/
private function calculateTaxForRefundAdjustment(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
/** @var Mage_Sales_Model_Resource_Order_Item_Collection $orderItems */
$orderItems = $creditmemo->getOrder()->getItemsCollection();
$taxPercentage = 0;
foreach ($orderItems as $item) {
$taxPercentage = max($taxPercentage, $item->getTaxPercent() / 100);
}
$totalAdjustment = $creditmemo->getAdjustmentPositive() - $creditmemo->getAdjustmentNegative();
$baseTotalAdjustment = $creditmemo->getBaseAdjustmentPositive() - $creditmemo->getBaseAdjustmentNegative();
// Adjustment values already include tax in my case. Modify calculation if you're entering values without tax
$totalAdjustmentTax = $totalAdjustment / ($taxPercentage + 1) * $taxPercentage;
$baseTotalAdjustmentTax = $baseTotalAdjustment / ($taxPercentage + 1) * $taxPercentage;
$creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalAdjustmentTax);
$creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalAdjustmentTax);
return [$totalAdjustmentTax, $baseTotalAdjustmentTax];
}
}
由于明天缺少回复且赏金到期,因此我的解决方法如下:
输入Adjustment Refund
含税的。
请注意注释中的细分,以供您参考和供客户参考。