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_Observer
29: {
30: protected $_validator;
31:
32: 33: 34: 35: 36: 37: 38:
39: public function getValidator($event)
40: {
41: if (!$this->_validator) {
42: $this->_validator = Mage::getModel('salesrule/validator')
43: ->init($event->getWebsiteId(), $event->getCustomerGroupId(), $event->getCouponCode());
44: }
45: return $this->_validator;
46: }
47:
48: 49: 50: 51: 52: 53:
54: public function sales_quote_address_discount_item($observer)
55: {
56: $this->getValidator($observer->getEvent())
57: ->process($observer->getEvent()->getItem());
58: }
59:
60: public function sales_order_afterPlace($observer)
61: {
62: $order = $observer->getEvent()->getOrder();
63:
64: if (!$order) {
65: return $this;
66: }
67:
68:
69: $ruleIds = explode(',', $order->getAppliedRuleIds());
70: $ruleIds = array_unique($ruleIds);
71:
72: $ruleCustomer = null;
73: $customerId = $order->getCustomerId();
74:
75:
76: foreach ($ruleIds as $ruleId) {
77: if (!$ruleId) {
78: continue;
79: }
80: $rule = Mage::getModel('salesrule/rule');
81: $rule->load($ruleId);
82: if ($rule->getId()) {
83: $rule->setTimesUsed($rule->getTimesUsed() + 1);
84: $rule->save();
85:
86: if ($customerId) {
87: $ruleCustomer = Mage::getModel('salesrule/rule_customer');
88: $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
89:
90: if ($ruleCustomer->getId()) {
91: $ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed()+1);
92: }
93: else {
94: $ruleCustomer
95: ->setCustomerId($customerId)
96: ->setRuleId($ruleId)
97: ->setTimesUsed(1);
98: }
99: $ruleCustomer->save();
100: }
101: }
102: }
103:
104: $coupon = Mage::getModel('salesrule/coupon');
105:
106: $coupon->load($order->getCouponCode(), 'code');
107: if ($coupon->getId()) {
108: $coupon->setTimesUsed($coupon->getTimesUsed() + 1);
109: $coupon->save();
110: if ($customerId) {
111: $couponUsage = Mage::getResourceModel('salesrule/coupon_usage');
112: $couponUsage->updateCustomerCouponTimesUsed($customerId, $coupon->getId());
113: }
114: }
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function aggregateSalesReportCouponsData($schedule)
124: {
125: Mage::app()->getLocale()->emulate(0);
126: $currentDate = Mage::app()->getLocale()->date();
127: $date = $currentDate->subHour(25);
128: Mage::getResourceModel('salesrule/report_rule')->aggregate($date);
129: Mage::app()->getLocale()->revert();
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138: 139:
140: protected function _checkSalesRulesAvailability($attributeCode)
141: {
142:
143: $collection = Mage::getResourceModel('salesrule/rule_collection')
144: ->addAttributeInConditionFilter($attributeCode);
145:
146: $disabledRulesCount = 0;
147: foreach ($collection as $rule) {
148:
149: $rule->setIsActive(0);
150:
151: $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
152: $this->_removeAttributeFromConditions($rule->getActions(), $attributeCode);
153: $rule->save();
154:
155: $disabledRulesCount++;
156: }
157:
158: if ($disabledRulesCount) {
159: Mage::getSingleton('adminhtml/session')->addWarning(
160: Mage::helper('salesrule')->__('%d Shopping Cart Price Rules based on "%s" attribute have been disabled.', $disabledRulesCount, $attributeCode));
161: }
162:
163: return $this;
164: }
165:
166: 167: 168: 169: 170: 171:
172: protected function _removeAttributeFromConditions($combine, $attributeCode)
173: {
174: $conditions = $combine->getConditions();
175: foreach ($conditions as $conditionId => $condition) {
176: if ($condition instanceof Mage_Rule_Model_Condition_Combine) {
177: $this->_removeAttributeFromConditions($condition, $attributeCode);
178: }
179: if ($condition instanceof Mage_SalesRule_Model_Rule_Condition_Product) {
180: if ($condition->getAttribute() == $attributeCode) {
181: unset($conditions[$conditionId]);
182: }
183: }
184: }
185: $combine->setConditions($conditions);
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function catalogAttributeSaveAfter(Varien_Event_Observer $observer)
195: {
196: $attribute = $observer->getEvent()->getAttribute();
197: if ($attribute->dataHasChangedFor('is_used_for_promo_rules') && !$attribute->getIsUsedForPromoRules()) {
198: $this->_checkSalesRulesAvailability($attribute->getAttributeCode());
199: }
200:
201: return $this;
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: public function catalogAttributeDeleteAfter(Varien_Event_Observer $observer)
212: {
213: $attribute = $observer->getEvent()->getAttribute();
214: if ($attribute->getIsUsedForPromoRules()) {
215: $this->_checkSalesRulesAvailability($attribute->getAttributeCode());
216: }
217:
218: return $this;
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function addProductAttributes(Varien_Event_Observer $observer)
228: {
229:
230: $attributesTransfer = $observer->getEvent()->getAttributes();
231:
232: $attributes = Mage::getResourceModel('salesrule/rule')
233: ->getActiveAttributes(
234: Mage::app()->getWebsite()->getId(),
235: Mage::getSingleton('customer/session')->getCustomer()->getGroupId()
236: );
237: $result = array();
238: foreach ($attributes as $attribute) {
239: $result[$attribute['attribute_code']] = true;
240: }
241: $attributesTransfer->addData($result);
242: return $this;
243: }
244:
245: 246: 247: 248: 249: 250:
251: public function addSalesRuleNameToOrder($observer)
252: {
253: $order = $observer->getOrder();
254: $couponCode = $order->getCouponCode();
255:
256: if (empty($couponCode)) {
257: return $this;
258: }
259:
260: 261: 262:
263: $couponModel = Mage::getModel('salesrule/coupon');
264: $couponModel->loadByCode($couponCode);
265:
266: $ruleId = $couponModel->getRuleId();
267:
268: if (empty($ruleId)) {
269: return $this;
270: }
271:
272: 273: 274:
275: $ruleModel = Mage::getModel('salesrule/rule');
276: $ruleModel->load($ruleId);
277:
278: $order->setCouponRuleName($ruleModel->getName());
279:
280: return $this;
281: }
282: }
283:
284: