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_Invoice_Total_Tax extends Mage_Sales_Model_Order_Invoice_Total_Abstract
29: {
30: 31: 32: 33: 34: 35:
36: public function collect(Mage_Sales_Model_Order_Invoice $invoice)
37: {
38: $totalTax = 0;
39: $baseTotalTax = 0;
40: $totalHiddenTax = 0;
41: $baseTotalHiddenTax = 0;
42:
43: $order = $invoice->getOrder();
44:
45:
46: foreach ($invoice->getAllItems() as $item) {
47: $orderItem = $item->getOrderItem();
48: $orderItemQty = $orderItem->getQtyOrdered();
49:
50: if ($orderItem->getTaxAmount() && $orderItemQty) {
51: if ($item->getOrderItem()->isDummy()) {
52: continue;
53: }
54:
55: 56: 57:
58: $tax = $orderItem->getTaxAmount() - $orderItem->getTaxInvoiced();
59: $baseTax = $orderItem->getBaseTaxAmount() - $orderItem->getBaseTaxInvoiced();
60: $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxInvoiced();
61: $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxInvoiced();
62: if (!$item->isLast()) {
63: $availableQty = $orderItemQty - $orderItem->getQtyInvoiced();
64: $tax = $invoice->roundPrice($tax / $availableQty * $item->getQty());
65: $baseTax = $invoice->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
66: $hiddenTax = $invoice->roundPrice($hiddenTax / $availableQty * $item->getQty());
67: $baseHiddenTax = $invoice->roundPrice($baseHiddenTax / $availableQty * $item->getQty(), 'base');
68: }
69:
70: $item->setTaxAmount($tax);
71: $item->setBaseTaxAmount($baseTax);
72: $item->setHiddenTaxAmount($hiddenTax);
73: $item->setBaseHiddenTaxAmount($baseHiddenTax);
74:
75: $totalTax += $tax;
76: $baseTotalTax += $baseTax;
77: $totalHiddenTax += $hiddenTax;
78: $baseTotalHiddenTax += $baseHiddenTax;
79: }
80: }
81:
82: if ($this->_canIncludeShipping($invoice)) {
83: $totalTax += $order->getShippingTaxAmount();
84: $baseTotalTax += $order->getBaseShippingTaxAmount();
85: $totalHiddenTax += $order->getShippingHiddenTaxAmount();
86: $baseTotalHiddenTax += $order->getBaseShippingHiddenTaxAmount();
87: $invoice->setShippingTaxAmount($order->getShippingTaxAmount());
88: $invoice->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount());
89: $invoice->setShippingHiddenTaxAmount($order->getShippingHiddenTaxAmount());
90: $invoice->setBaseShippingHiddenTaxAmount($order->getBaseShippingHiddenTaxAmount());
91: }
92: $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced();
93: $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();;
94: $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount()
95: - $order->getHiddenTaxInvoiced() - $order->getShippingHiddenTaxInvoiced();
96: $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount()
97: - $order->getBaseHiddenTaxInvoiced() - $order->getBaseShippingHiddenTaxInvoiced();
98:
99: if ($invoice->isLast()) {
100: $totalTax = $allowedTax;
101: $baseTotalTax = $allowedBaseTax;
102: $totalHiddenTax = $allowedHiddenTax;
103: $baseTotalHiddenTax = $allowedBaseHiddenTax;
104: } else {
105: $totalTax = min($allowedTax, $totalTax);
106: $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
107: $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
108: $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
109: }
110:
111: $invoice->setTaxAmount($totalTax);
112: $invoice->setBaseTaxAmount($baseTotalTax);
113: $invoice->setHiddenTaxAmount($totalHiddenTax);
114: $invoice->setBaseHiddenTaxAmount($baseTotalHiddenTax);
115:
116: $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax + $totalHiddenTax);
117: $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
118:
119: return $this;
120: }
121:
122: 123: 124: 125: 126:
127: protected function _canIncludeShipping($invoice)
128: {
129: $includeShippingTax = true;
130: 131: 132:
133: foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
134: if ($previusInvoice->getShippingAmount() && !$previusInvoice->isCanceled()) {
135: $includeShippingTax = false;
136: }
137: }
138: return $includeShippingTax;
139: }
140: }
141: