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