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: 29: 30: 31: 32: 33:
34: class Mage_Sales_Model_Order_Creditmemo_Total_Shipping extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract
35: {
36: public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
37: {
38: $order = $creditmemo->getOrder();
39: $allowedAmount = $order->getShippingAmount()-$order->getShippingRefunded();
40: $baseAllowedAmount = $order->getBaseShippingAmount()-$order->getBaseShippingRefunded();
41:
42: $shipping = $order->getShippingAmount();
43: $baseShipping = $order->getBaseShippingAmount();
44: $shippingInclTax = $order->getShippingInclTax();
45: $baseShippingInclTax = $order->getBaseShippingInclTax();
46:
47: $isShippingInclTax = Mage::getSingleton('tax/config')->displaySalesShippingInclTax($order->getStoreId());
48:
49: 50: 51: 52:
53: if ($creditmemo->hasBaseShippingAmount()) {
54: $baseShippingAmount = Mage::app()->getStore()->roundPrice($creditmemo->getBaseShippingAmount());
55: if ($isShippingInclTax && $baseShippingInclTax != 0) {
56: $part = $baseShippingAmount/$baseShippingInclTax;
57: $shippingInclTax = Mage::app()->getStore()->roundPrice($shippingInclTax*$part);
58: $baseShippingInclTax= $baseShippingAmount;
59: $baseShippingAmount = Mage::app()->getStore()->roundPrice($baseShipping*$part);
60: }
61: 62: 63: 64: 65:
66: if ($baseShippingAmount < Mage::app()->getStore()->roundPrice($baseAllowedAmount) + 0.0001) {
67: 68: 69: 70: 71:
72: if ($baseShippingAmount > $baseAllowedAmount - 0.0001) {
73: $shipping = $allowedAmount;
74: $baseShipping = $baseAllowedAmount;
75: } else {
76: if ($baseShipping != 0) {
77: $shipping = $shipping * $baseShippingAmount / $baseShipping;
78: }
79: $shipping = Mage::app()->getStore()->roundPrice($shipping);
80: $baseShipping = $baseShippingAmount;
81: }
82: } else {
83: $baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount,null,false);
84: Mage::throwException(
85: Mage::helper('sales')->__('Maximum shipping amount allowed to refund is: %s', $baseAllowedAmount)
86: );
87: }
88: } else {
89: if ($baseShipping != 0) {
90: $allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
91: $baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
92:
93: $shippingInclTax = Mage::app()->getStore()->roundPrice($allowedAmount + $allowedTaxAmount);
94: $baseShippingInclTax = Mage::app()->getStore()->roundPrice($baseAllowedAmount + $baseAllowedTaxAmount);
95: }
96: $shipping = $allowedAmount;
97: $baseShipping = $baseAllowedAmount;
98: }
99:
100: $creditmemo->setShippingAmount($shipping);
101: $creditmemo->setBaseShippingAmount($baseShipping);
102: $creditmemo->setShippingInclTax($shippingInclTax);
103: $creditmemo->setBaseShippingInclTax($baseShippingInclTax);
104:
105: $creditmemo->setGrandTotal($creditmemo->getGrandTotal()+$shipping);
106: $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal()+$baseShipping);
107: return $this;
108: }
109: }
110: