9

When issuing a Credit Memo (aka refund), Magento automatically refunds tax on shipping and products BUT NOT on values entered in the "Adjustment refund" field (aka partial refund).

Is it possible to configure Magento so that it automatically refunds tax on values entered in the adjustment refund field?

Reference: enter image description here

3 Answers 3

3
+50

You will find the code that deals with this in the class Mage_Sales_Model_Order_Creditmemo_Total_Tax.

The line of code $part = $creditmemo->getShippingAmount()/$orderShippingAmount; (located at line 116) clearly shows this is specifically coded to only calculate the taxes as per the shippingAmount field from the creditMemo form.

The obvious change is to adjust that code to also use the "Adjustment refund" field.

You can't actually rewrite this class, due to how magento instantiates it as part of the totals collector subsystem, in the creditmemo calculations.

You can however adjust the collector to use your own version of the class, so all is not lost.

So, in your own module you will place the following code in the config.xml. It goes inside the <global> elements

<global>
    <sales>
      <order_creditmemo>
         <totals>
            <tax>
              <class>NAMESPACE_MODULE/order_creditmemo_total_tax</class>
              <after>subtotal</after>
            </tax>
         </totals>
      </order_creditmemo>
   </sales>
<global>

Now you will create the class file NAMESPACE/MODULE/Model/Order/Creditmemo/Total/Tax, which extends the core file.

class NAMESPACE_MODULE_Model_Order_Creditmemo_Total_Tax extends Mage_Sales_Model_Order_Creditmemo_Total_Tax

You will need to copy the entire `collect' method form the core class to your new file.

Add the following code at line 114 (just after the code $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 **/

and adjust line 116 from $part = $creditmemo->getShippingAmount()/$orderShippingAmount; to $part = $adjustment/$orderShippingAmount;

This will then effectively use either the Shipping Amount or the Adjustment Amount in the calculation.

1
  • Thanks! But if I understand correctly, this only works if there are shipping costs. It will not work without shipping costs.
    – Simon
    May 11, 2017 at 10:46
8

The substantial problem is, that magento doesn't know which tax factor to use. When there are no products which are refunded, there is no tax percentage.

I fixed the problem by just using the highest tax percentage I can find in the products, feel free to adjust to your use case.

The tax is calculated in calculateTaxForRefundAdjustment at the end of the class.

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];
    }
}
8
  • I can confirm this works better than the accepted answer for recent Magento versions (1.9.x.x). Jan 19, 2018 at 10:01
  • @Fabian, Your solution is good but why it updates subtotal of credit memo?, As if I don't return any product and shipping but refund some amount via Adjustment Refund then it show the tax amount from adjustment refund amount to subtotal.
    – Yogita
    Nov 16, 2018 at 7:10
  • It doesn't. the code doesn't containt "subtotal" Nov 19, 2018 at 7:31
  • @FabianBlechschmidt, You are right, the code doesn't contain subtotal, But how I use it - I didn't refund shipping or items, so my subtotal is initially $0.00, I only Refund "Adjustment Refund". After credit memo generation when I open that to view, It will show me tax amount in subtotal but the subtotal should be zero As I didn't refund any item. My system is incl. of tax and when I refund the adjusment Refund $10.00 ($0.65-Tax). Then it show me Subtotal= $0.65 Adjustment Refund = $10.00 Grand total Incl. Tax = $10.00 Tax = $0.65 Grand total Excl. Tax = $9.35 Why Subtotal is zero?
    – Yogita
    Nov 20, 2018 at 9:41
  • Please open a new question, describe what you have done, what happened and what you would have been expected. Nov 20, 2018 at 11:53
0

With a lack of replies and bounty expiring tomorrow, my work around is the following:

Enter the Adjustment Refund with taxes included.

Note the breakdown in the comments for your reference and the customers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.