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_Subtotal 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: $subtotal = 0;
39: $baseSubtotal = 0;
40: $subtotalInclTax= 0;
41: $baseSubtotalInclTax = 0;
42:
43: $order = $invoice->getOrder();
44:
45: foreach ($invoice->getAllItems() as $item) {
46: if ($item->getOrderItem()->isDummy()) {
47: continue;
48: }
49:
50: $item->calcRowTotal();
51:
52: $subtotal += $item->getRowTotal();
53: $baseSubtotal += $item->getBaseRowTotal();
54: $subtotalInclTax+= $item->getRowTotalInclTax();
55: $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
56: }
57:
58: $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
59: $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
60: $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount()
61: + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced();
62: $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount()
63: + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced();
64:
65: 66: 67:
68: $includeShippingTax = true;
69: foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
70: if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
71: $includeShippingTax = false;
72: break;
73: }
74: }
75:
76: if ($includeShippingTax) {
77: $allowedSubtotalInclTax -= $order->getShippingTaxAmount();
78: $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
79: } else {
80: $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount();
81: $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
82: }
83:
84: if ($invoice->isLast()) {
85: $subtotal = $allowedSubtotal;
86: $baseSubtotal = $baseAllowedSubtotal;
87: $subtotalInclTax = $allowedSubtotalInclTax;
88: $baseSubtotalInclTax = $baseAllowedSubtotalInclTax;
89: } else {
90: $subtotal = min($allowedSubtotal, $subtotal);
91: $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
92: $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
93: $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
94: }
95:
96: $invoice->setSubtotal($subtotal);
97: $invoice->setBaseSubtotal($baseSubtotal);
98: $invoice->setSubtotalInclTax($subtotalInclTax);
99: $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
100:
101: $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
102: $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
103: return $this;
104: }
105: }
106: