クレジットメモ(別名返金)を発行すると、Magentoは自動的に配送料と製品の税金を払い戻しますが、「調整払い戻し」フィールドに入力された値(別名、一部払い戻し)は払い戻しません。
調整払い戻しフィールドに入力された値に税金を自動的に払い戻すようにMagentoを構成することはできますか?
クレジットメモ(別名返金)を発行すると、Magentoは自動的に配送料と製品の税金を払い戻しますが、「調整払い戻し」フィールドに入力された値(別名、一部払い戻し)は払い戻しません。
調整払い戻しフィールドに入力された値に税金を自動的に払い戻すようにMagentoを構成することはできますか?
回答:
これを処理するコードがクラスにありMage_Sales_Model_Order_Creditmemo_Total_Tax
ます。
コード行$part = $creditmemo->getShippingAmount()/$orderShippingAmount;
(行116にあります)は、creditMemoフォームのshippingAmountフィールドごとに税金のみを計算するように特別にコード化されていることを明確に示しています。
明らかな変更は、「調整払い戻し」フィールドも使用するようにコードを調整することです。
クレジットメモの計算で、magentoが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
税込みで入力してください。
あなたの参照と顧客のためにコメントの内訳に注意してください。