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_Weee_Model_Total_Invoice_Weee 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: $store = $invoice->getStore();
39:
40: $totalTax = 0;
41: $baseTotalTax = 0;
42:
43: foreach ($invoice->getAllItems() as $item) {
44: $orderItem = $item->getOrderItem();
45: $orderItemQty = $orderItem->getQtyOrdered();
46:
47: if (!$orderItemQty || $orderItem->isDummy()) {
48: continue;
49: }
50:
51: $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty();
52: $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
53:
54: $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount);
55: $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount);
56: $newApplied = array();
57: $applied = Mage::helper('weee')->getApplied($item);
58: foreach ($applied as $one) {
59: $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
60: $one['row_amount'] = $one['amount'] * $item->getQty();
61: $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
62: $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
63:
64: $newApplied[] = $one;
65: }
66: Mage::helper('weee')->setApplied($item, $newApplied);
67:
68: $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
69: $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
70:
71: $totalTax += $weeeTaxAmount;
72: $baseTotalTax += $baseWeeeTaxAmount;
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: $order = $invoice->getOrder();
84: if (Mage::helper('weee')->includeInSubtotal($store)) {
85: $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
86: $allowedBaseSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced()
87: - $invoice->getBaseSubtotal();
88: $totalTax = min($allowedSubtotal, $totalTax);
89: $baseTotalTax = min($allowedBaseSubtotal, $baseTotalTax);
90:
91: $invoice->setSubtotal($invoice->getSubtotal() + $totalTax);
92: $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax);
93: } else {
94: $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
95: $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
96: $totalTax = min($allowedTax, $totalTax);
97: $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
98:
99: $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax);
100: $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax);
101: }
102:
103: $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
104: $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
105:
106: return $this;
107: }
108: }
109: