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_Discount extends Mage_Sales_Model_Order_Invoice_Total_Abstract
29: {
30: public function collect(Mage_Sales_Model_Order_Invoice $invoice)
31: {
32: $invoice->setDiscountAmount(0);
33: $invoice->setBaseDiscountAmount(0);
34:
35: $totalDiscountAmount = 0;
36: $baseTotalDiscountAmount = 0;
37:
38: 39: 40: 41: 42:
43: $addShippingDicount = true;
44: foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
45: if ($previusInvoice->getDiscountAmount()) {
46: $addShippingDicount = false;
47: }
48: }
49:
50: if ($addShippingDicount) {
51: $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount();
52: $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount();
53: }
54:
55:
56: foreach ($invoice->getAllItems() as $item) {
57: $orderItem = $item->getOrderItem();
58: if ($orderItem->isDummy()) {
59: continue;
60: }
61:
62: $orderItemDiscount = (float) $orderItem->getDiscountAmount();
63: $baseOrderItemDiscount = (float) $orderItem->getBaseDiscountAmount();
64: $orderItemQty = $orderItem->getQtyOrdered();
65:
66: if ($orderItemDiscount && $orderItemQty) {
67: 68: 69:
70: $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
71: $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
72: if (!$item->isLast()) {
73: $activeQty = $orderItemQty - $orderItem->getQtyInvoiced();
74: $discount = $invoice->roundPrice($discount / $activeQty * $item->getQty(), 'regular', true);
75: $baseDiscount = $invoice->roundPrice($baseDiscount / $activeQty * $item->getQty(), 'base', true);
76: }
77:
78: $item->setDiscountAmount($discount);
79: $item->setBaseDiscountAmount($baseDiscount);
80:
81: $totalDiscountAmount += $discount;
82: $baseTotalDiscountAmount += $baseDiscount;
83: }
84: }
85:
86:
87: $invoice->setDiscountAmount($totalDiscountAmount);
88: $invoice->setBaseDiscountAmount($baseTotalDiscountAmount);
89:
90: $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
91: $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
92: return $this;
93: }
94: }
95: