1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: class Mage_Sales_Model_Order_Creditmemo_Total_Tax extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract
29: {
30: public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
31: {
32: $shippingTaxAmount = 0;
33: $baseShippingTaxAmount = 0;
34: $totalTax = 0;
35: $baseTotalTax = 0;
36: $totalHiddenTax = 0;
37: $baseTotalHiddenTax = 0;
38:
39: $order = $creditmemo->getOrder();
40:
41:
42: foreach ($creditmemo->getAllItems() as $item) {
43: $orderItem = $item->getOrderItem();
44: if ($orderItem->isDummy()) {
45: continue;
46: }
47: $orderItemTax = $orderItem->getTaxInvoiced();
48: $baseOrderItemTax = $orderItem->getBaseTaxInvoiced();
49: $orderItemQty = $orderItem->getQtyInvoiced();
50:
51: if ($orderItemTax && $orderItemQty) {
52: 53: 54:
55:
56: $tax = $orderItemTax - $orderItem->getTaxRefunded();
57: $baseTax = $baseOrderItemTax - $orderItem->getTaxRefunded();
58: $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxRefunded();
59: $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxRefunded();
60: if (!$item->isLast()) {
61: $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
62: $tax = $creditmemo->roundPrice($tax / $availableQty * $item->getQty());
63: $baseTax = $creditmemo->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
64: $hiddenTax = $creditmemo->roundPrice($hiddenTax / $availableQty * $item->getQty());
65: $baseHiddenTax = $creditmemo->roundPrice($baseHiddenTax / $availableQty * $item->getQty(), 'base');
66: }
67:
68: $item->setTaxAmount($tax);
69: $item->setBaseTaxAmount($baseTax);
70: $item->setHiddenTaxAmount($hiddenTax);
71: $item->setBaseHiddenTaxAmount($baseHiddenTax);
72:
73: $totalTax += $tax;
74: $baseTotalTax += $baseTax;
75: $totalHiddenTax += $hiddenTax;
76: $baseTotalHiddenTax += $baseHiddenTax;
77: }
78: }
79:
80: if ($invoice = $creditmemo->getInvoice()) {
81:
82: if ($order->getBaseShippingAmount() && $creditmemo->getBaseShippingAmount()) {
83: $taxFactor = $creditmemo->getBaseShippingAmount()/$order->getBaseShippingAmount();
84: $shippingTaxAmount = $invoice->getShippingTaxAmount()*$taxFactor;
85: $baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount()*$taxFactor;
86: $totalHiddenTax += $invoice->getShippingHiddenTaxAmount()*$taxFactor;
87: $baseTotalHiddenTax += $invoice->getBaseShippingHiddenTaxAmount()*$taxFactor;
88: $shippingHiddenTaxAmount = $invoice->getShippingHiddenTaxAmount()*$taxFactor;
89: $baseShippingHiddenTaxAmount = $invoice->getBaseShippingHiddenTaxAmount()*$taxFactor;
90: $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
91: $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
92: $totalHiddenTax = $creditmemo->roundPrice($totalHiddenTax);
93: $baseTotalHiddenTax = $creditmemo->roundPrice($baseTotalHiddenTax, 'base');
94: $shippingHiddenTaxAmount = $creditmemo->roundPrice($shippingHiddenTaxAmount);
95: $baseShippingHiddenTaxAmount = $creditmemo->roundPrice($baseShippingHiddenTaxAmount, 'base');
96: $totalTax += $shippingTaxAmount;
97: $baseTotalTax += $baseShippingTaxAmount;
98: }
99: } else {
100: $orderShippingAmount = $order->getShippingAmount();
101: $baseOrderShippingAmount = $order->getBaseShippingAmount();
102: $orderShippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount();
103: $baseOrderShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount();
104:
105: $baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
106: $baseOrderShippingHiddenTaxRefunded = $order->getBaseShippingHiddenTaxRefunded();
107:
108: $shippingTaxAmount = 0;
109: $baseShippingTaxAmount = 0;
110: $shippingHiddenTaxAmount = 0;
111: $baseShippingHiddenTaxAmount = 0;
112:
113: $shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount;
114:
115: if ($shippingDelta > $creditmemo->getBaseShippingAmount()) {
116: $part = $creditmemo->getShippingAmount()/$orderShippingAmount;
117: $basePart = $creditmemo->getBaseShippingAmount()/$baseOrderShippingAmount;
118: $shippingTaxAmount = $order->getShippingTaxAmount()*$part;
119: $baseShippingTaxAmount = $order->getBaseShippingTaxAmount()*$basePart;
120: $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount()*$part;
121: $baseShippingHiddenTaxAmount= $order->getBaseShippingHiddenTaxAmount()*$basePart;
122: $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
123: $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
124: $shippingHiddenTaxAmount = $creditmemo->roundPrice($shippingHiddenTaxAmount);
125: $baseShippingHiddenTaxAmount= $creditmemo->roundPrice($baseShippingHiddenTaxAmount, 'base');
126: } elseif ($shippingDelta == $creditmemo->getBaseShippingAmount()) {
127: $shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
128: $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
129: $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount()
130: - $order->getShippingHiddenTaxRefunded();
131: $baseShippingHiddenTaxAmount= $order->getBaseShippingHiddenTaxAmount()
132: - $order->getBaseShippingHiddenTaxRefunded();
133: }
134: $totalTax += $shippingTaxAmount;
135: $baseTotalTax += $baseShippingTaxAmount;
136: $totalHiddenTax += $shippingHiddenTaxAmount;
137: $baseTotalHiddenTax += $baseShippingHiddenTaxAmount;
138: }
139:
140: $allowedTax = $order->getTaxAmount() - $order->getTaxRefunded();
141: $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxRefunded();
142: $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount()
143: - $order->getHiddenTaxRefunded() - $order->getShippingHiddenTaxRefunded();
144: $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount()
145: - $order->getBaseHiddenTaxRefunded() - $order->getBaseShippingHiddenTaxRefunded();
146:
147:
148: $totalTax = min($allowedTax, $totalTax);
149: $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
150: $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
151: $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
152:
153: $creditmemo->setTaxAmount($totalTax);
154: $creditmemo->setBaseTaxAmount($baseTotalTax);
155: $creditmemo->setHiddenTaxAmount($totalHiddenTax);
156: $creditmemo->setBaseHiddenTaxAmount($baseTotalHiddenTax);
157:
158:
159: $creditmemo->setShippingTaxAmount($shippingTaxAmount);
160: $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
161:
162: $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalHiddenTax);
163: $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
164: return $this;
165: }
166: }
167: