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_SalesRule_Model_Quote_Discount extends Mage_Sales_Model_Quote_Address_Total_Abstract
29: {
30: 31: 32: 33: 34:
35: protected $_calculator;
36:
37: public function __construct()
38: {
39: $this->setCode('discount');
40: $this->_calculator = Mage::getSingleton('salesrule/validator');
41: }
42:
43: 44: 45: 46: 47: 48:
49: public function collect(Mage_Sales_Model_Quote_Address $address)
50: {
51: parent::collect($address);
52: $quote = $address->getQuote();
53: $store = Mage::app()->getStore($quote->getStoreId());
54: $this->_calculator->reset($address);
55:
56: $items = $this->_getAddressItems($address);
57: if (!count($items)) {
58: return $this;
59: }
60:
61: $eventArgs = array(
62: 'website_id' => $store->getWebsiteId(),
63: 'customer_group_id' => $quote->getCustomerGroupId(),
64: 'coupon_code' => $quote->getCouponCode(),
65: );
66:
67: $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode());
68: $this->_calculator->initTotals($items, $address);
69:
70: $address->setDiscountDescription(array());
71:
72: foreach ($items as $item) {
73: if ($item->getNoDiscount()) {
74: $item->setDiscountAmount(0);
75: $item->setBaseDiscountAmount(0);
76: }
77: else {
78: 79: 80:
81: if ($item->getParentItemId()) {
82: continue;
83: }
84:
85: $eventArgs['item'] = $item;
86: Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
87:
88: if ($item->getHasChildren() && $item->isChildrenCalculated()) {
89: foreach ($item->getChildren() as $child) {
90: $this->_calculator->process($child);
91: $eventArgs['item'] = $child;
92: Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
93: $this->_aggregateItemDiscount($child);
94: }
95: } else {
96: $this->_calculator->process($item);
97: $this->_aggregateItemDiscount($item);
98: }
99: }
100: }
101:
102: 103: 104:
105: $address->setShippingDiscountAmount(0);
106: $address->setBaseShippingDiscountAmount(0);
107: if ($address->getShippingAmount()) {
108: $this->_calculator->processShippingAmount($address);
109: $this->_addAmount(-$address->getShippingDiscountAmount());
110: $this->_addBaseAmount(-$address->getBaseShippingDiscountAmount());
111: }
112:
113: $this->_calculator->prepareDescription($address);
114: return $this;
115: }
116:
117: 118: 119: 120: 121: 122:
123: protected function _aggregateItemDiscount($item)
124: {
125: $this->_addAmount(-$item->getDiscountAmount());
126: $this->_addBaseAmount(-$item->getBaseDiscountAmount());
127: return $this;
128: }
129:
130: 131: 132: 133: 134: 135:
136: public function fetch(Mage_Sales_Model_Quote_Address $address)
137: {
138: $amount = $address->getDiscountAmount();
139:
140: if ($amount!=0) {
141: $description = $address->getDiscountDescription();
142: if (strlen($description)) {
143: $title = Mage::helper('sales')->__('Discount (%s)', $description);
144: } else {
145: $title = Mage::helper('sales')->__('Discount');
146: }
147: $address->addTotal(array(
148: 'code' => $this->getCode(),
149: 'title' => $title,
150: 'value' => $amount
151: ));
152: }
153: return $this;
154: }
155: }
156: