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:
35: class Mage_Sales_Model_Observer
36: {
37: 38: 39: 40: 41:
42: protected $_expireQuotesFilterFields = array();
43:
44: 45: 46: 47: 48: 49:
50: public function cleanExpiredQuotes($schedule)
51: {
52: Mage::dispatchEvent('clear_expired_quotes_before', array('sales_observer' => $this));
53:
54: $lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');
55: foreach ($lifetimes as $storeId=>$lifetime) {
56: $lifetime *= 86400;
57:
58:
59: $quotes = Mage::getModel('sales/quote')->getCollection();
60:
61: $quotes->addFieldToFilter('store_id', $storeId);
62: $quotes->addFieldToFilter('updated_at', array('to'=>date("Y-m-d", time()-$lifetime)));
63: $quotes->addFieldToFilter('is_active', 0);
64:
65: foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
66: $quotes->addFieldToFilter($field, $condition);
67: }
68:
69: $quotes->walk('delete');
70: }
71: return $this;
72: }
73:
74: 75: 76: 77: 78:
79: public function getExpireQuotesAdditionalFilterFields()
80: {
81: return $this->_expireQuotesFilterFields;
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function setExpireQuotesAdditionalFilterFields(array $fields)
91: {
92: $this->_expireQuotesFilterFields = $fields;
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function substractQtyFromQuotes($observer)
104: {
105: $product = $observer->getEvent()->getProduct();
106: Mage::getResourceSingleton('sales/quote')->substractProductFromQuotes($product);
107: return $this;
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function markQuotesRecollectOnCatalogRules($observer)
117: {
118: Mage::getResourceSingleton('sales/quote')->markQuotesRecollectOnCatalogRules();
119: return $this;
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function catalogProductSaveAfter(Varien_Event_Observer $observer)
129: {
130: $product = $observer->getEvent()->getProduct();
131: if ($product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
132: return $this;
133: }
134:
135: Mage::getResourceSingleton('sales/quote')->markQuotesRecollect($product->getId());
136:
137: return $this;
138: }
139:
140: 141: 142: 143: 144: 145:
146: public function catalogProductStatusUpdate(Varien_Event_Observer $observer)
147: {
148: $status = $observer->getEvent()->getStatus();
149: if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
150: return $this;
151: }
152: $productId = $observer->getEvent()->getProductId();
153: Mage::getResourceSingleton('sales/quote')->markQuotesRecollect($productId);
154:
155: return $this;
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function aggregateSalesReportOrderData($schedule)
165: {
166: Mage::app()->getLocale()->emulate(0);
167: $currentDate = Mage::app()->getLocale()->date();
168: $date = $currentDate->subHour(25);
169: Mage::getResourceModel('sales/report_order')->aggregate($date);
170: Mage::app()->getLocale()->revert();
171: return $this;
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function aggregateSalesReportShipmentData($schedule)
181: {
182: Mage::app()->getLocale()->emulate(0);
183: $currentDate = Mage::app()->getLocale()->date();
184: $date = $currentDate->subHour(25);
185: Mage::getResourceModel('sales/report_shipping')->aggregate($date);
186: Mage::app()->getLocale()->revert();
187: return $this;
188: }
189:
190: 191: 192: 193: 194: 195:
196: public function aggregateSalesReportInvoicedData($schedule)
197: {
198: Mage::app()->getLocale()->emulate(0);
199: $currentDate = Mage::app()->getLocale()->date();
200: $date = $currentDate->subHour(25);
201: Mage::getResourceModel('sales/report_invoiced')->aggregate($date);
202: Mage::app()->getLocale()->revert();
203: return $this;
204: }
205:
206: 207: 208: 209: 210: 211:
212: public function aggregateSalesReportRefundedData($schedule)
213: {
214: Mage::app()->getLocale()->emulate(0);
215: $currentDate = Mage::app()->getLocale()->date();
216: $date = $currentDate->subHour(25);
217: Mage::getResourceModel('sales/report_refunded')->aggregate($date);
218: Mage::app()->getLocale()->revert();
219: return $this;
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function aggregateSalesReportBestsellersData($schedule)
229: {
230: Mage::app()->getLocale()->emulate(0);
231: $currentDate = Mage::app()->getLocale()->date();
232: $date = $currentDate->subHour(25);
233: Mage::getResourceModel('sales/report_bestsellers')->aggregate($date);
234: Mage::app()->getLocale()->revert();
235: return $this;
236: }
237:
238: 239: 240: 241: 242:
243: public function prepareProductEditFormRecurringProfile($observer)
244: {
245:
246: $profileElement = $observer->getEvent()->getProductElement();
247: $block = Mage::app()->getLayout()->createBlock('sales/adminhtml_recurring_profile_edit_form',
248: 'adminhtml_recurring_profile_edit_form')->setParentElement($profileElement)
249: ->setProductEntity($observer->getEvent()->getProduct());
250: $observer->getEvent()->getResult()->output = $block->toHtml();
251:
252:
253: $dependencies = Mage::app()->getLayout()->createBlock('adminhtml/widget_form_element_dependence',
254: 'adminhtml_recurring_profile_edit_form_dependence')->addFieldMap('is_recurring', 'product[is_recurring]')
255: ->addFieldMap($profileElement->getHtmlId(), $profileElement->getName())
256: ->addFieldDependence($profileElement->getName(), 'product[is_recurring]', '1')
257: ->addConfigOptions(array('levels_up' => 2));
258: $observer->getEvent()->getResult()->output .= $dependencies->toHtml();
259: }
260:
261: 262: 263: 264: 265:
266: public function restrictAdminBillingAgreementUsage($observer)
267: {
268: $methodInstance = $observer->getEvent()->getMethodInstance();
269: if (!($methodInstance instanceof Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract)) {
270: return;
271: }
272: if (!Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/use')) {
273: $observer->getEvent()->getResult()->isAvailable = false;
274: }
275: }
276:
277: 278: 279: 280: 281: 282:
283: public function customerSaveAfter(Varien_Event_Observer $observer)
284: {
285:
286: $customer = $observer->getEvent()->getCustomer();
287:
288: if ($customer->getGroupId() !== $customer->getOrigData('group_id')) {
289: 290: 291: 292:
293: $websites = (Mage::getSingleton('customer/config_share')->isWebsiteScope())
294: ? array(Mage::app()->getWebsite($customer->getWebsiteId()))
295: : Mage::app()->getWebsites();
296:
297:
298: $quote = Mage::getSingleton('sales/quote');
299:
300: foreach ($websites as $website) {
301: $quote->setWebsite($website);
302: $quote->loadByCustomer($customer);
303:
304: if ($quote->getId()) {
305: $quote->setCustomerGroupId($customer->getGroupId());
306: $quote->collectTotals();
307: $quote->save();
308: }
309: }
310: }
311:
312: return $this;
313: }
314:
315: 316: 317: 318: 319:
320: public function setQuoteCanApplyMsrp(Varien_Event_Observer $observer)
321: {
322:
323: $quote = $observer->getEvent()->getQuote();
324:
325: $canApplyMsrp = false;
326: if (Mage::helper('catalog')->isMsrpEnabled()) {
327: foreach ($quote->getAllAddresses() as $adddress) {
328: if ($adddress->getCanApplyMsrp()) {
329: $canApplyMsrp = true;
330: break;
331: }
332: }
333: }
334:
335: $quote->setCanApplyMsrp($canApplyMsrp);
336: }
337:
338: 339: 340: 341: 342: 343:
344: public function (Varien_Event_Observer $observer)
345: {
346:
347: $orderInstance = $observer->getOrder();
348:
349: $orderAddress = $this->_getVatRequiredSalesAddress($orderInstance);
350: if (!($orderAddress instanceof Mage_Sales_Model_Order_Address)) {
351: return;
352: }
353:
354: $vatRequestId = $orderAddress->getVatRequestId();
355: $vatRequestDate = $orderAddress->getVatRequestDate();
356: if (is_string($vatRequestId) && !empty($vatRequestId) && is_string($vatRequestDate)
357: && !empty($vatRequestDate)
358: ) {
359: $orderHistoryComment = Mage::helper('customer')->__('VAT Request Identifier')
360: . ': ' . $vatRequestId . '<br />' . Mage::helper('customer')->__('VAT Request Date')
361: . ': ' . $vatRequestDate;
362: $orderInstance->addStatusHistoryComment($orderHistoryComment, false);
363: }
364: }
365:
366: 367: 368: 369: 370: 371: 372:
373: protected function _getVatRequiredSalesAddress($salesModel, $store = null)
374: {
375: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType($store);
376: $requiredAddress = null;
377: switch ($configAddressType) {
378: case Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING:
379: $requiredAddress = $salesModel->getShippingAddress();
380: break;
381: default:
382: $requiredAddress = $salesModel->getBillingAddress();
383: }
384: return $requiredAddress;
385: }
386:
387: 388: 389: 390: 391: 392: 393:
394: protected function _getVatRequiredCustomerAddress(Mage_Customer_Model_Customer $customer, $store = null)
395: {
396: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType($store);
397: $requiredAddress = null;
398: switch ($configAddressType) {
399: case Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING:
400: $requiredAddress = $customer->getDefaultShipping();
401: break;
402: default:
403: $requiredAddress = $customer->getDefaultBilling();
404: }
405: return $requiredAddress;
406: }
407:
408: 409: 410: 411: 412:
413: public function changeQuoteCustomerGroupId(Varien_Event_Observer $observer)
414: {
415:
416: $addressHelper = Mage::helper('customer/address');
417:
418: $quoteAddress = $observer->getQuoteAddress();
419: $quoteInstance = $quoteAddress->getQuote();
420: $customerInstance = $quoteInstance->getCustomer();
421:
422: $storeId = $customerInstance->getStore();
423:
424: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType($storeId);
425:
426:
427: $additionalBillingAddressCondition = ($configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_BILLING)
428: ? $configAddressType != $quoteAddress->getAddressType() : false;
429:
430: if (!$addressHelper->isVatValidationEnabled($storeId) || $additionalBillingAddressCondition) {
431: return;
432: }
433:
434:
435: $customerHelper = Mage::helper('customer');
436:
437: $customerCountryCode = $quoteAddress->getCountryId();
438: $customerVatNumber = $quoteAddress->getVatId();
439:
440: if (empty($customerVatNumber) || !Mage::helper('core')->isCountryInEU($customerCountryCode)) {
441: $groupId = ($customerInstance->getId()) ? $customerHelper->getDefaultCustomerGroupId($storeId)
442: : Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
443:
444: $quoteAddress->setPrevQuoteCustomerGroupId($quoteInstance->getCustomerGroupId());
445: $customerInstance->setGroupId($groupId);
446: $quoteInstance->setCustomerGroupId($groupId);
447:
448: return;
449: }
450:
451:
452: $coreHelper = Mage::helper('core');
453: $merchantCountryCode = $coreHelper->getMerchantCountryCode();
454: $merchantVatNumber = $coreHelper->getMerchantVatNumber();
455:
456: $gatewayResponse = null;
457: if ($addressHelper->getValidateOnEachTransaction($storeId)
458: || $customerCountryCode != $quoteAddress->getValidatedCountryCode()
459: || $customerVatNumber != $quoteAddress->getValidatedVatNumber()
460: ) {
461:
462: $gatewayResponse = $customerHelper->checkVatNumber(
463: $customerCountryCode,
464: $customerVatNumber,
465: ($merchantVatNumber !== '') ? $merchantCountryCode : '',
466: $merchantVatNumber
467: );
468:
469:
470: $quoteAddress->setVatIsValid((int)$gatewayResponse->getIsValid())
471: ->setVatRequestId($gatewayResponse->getRequestIdentifier())
472: ->setVatRequestDate($gatewayResponse->getRequestDate())
473: ->setVatRequestSuccess($gatewayResponse->getRequestSuccess())
474: ->setValidatedVatNumber($customerVatNumber)
475: ->setValidatedCountryCode($customerCountryCode)
476: ->save();
477: } else {
478:
479: $gatewayResponse = new Varien_Object(array(
480: 'is_valid' => (int)$quoteAddress->getVatIsValid(),
481: 'request_identifier' => (string)$quoteAddress->getVatRequestId(),
482: 'request_date' => (string)$quoteAddress->getVatRequestDate(),
483: 'request_success' => (boolean)$quoteAddress->getVatRequestSuccess()
484: ));
485: }
486:
487:
488: $groupId = $customerHelper->getCustomerGroupIdBasedOnVatNumber(
489: $customerCountryCode, $gatewayResponse, $customerInstance->getStore()
490: );
491:
492: if ($groupId) {
493: $quoteAddress->setPrevQuoteCustomerGroupId($quoteInstance->getCustomerGroupId());
494: $customerInstance->setGroupId($groupId);
495: $quoteInstance->setCustomerGroupId($groupId);
496: }
497: }
498:
499: 500: 501: 502: 503:
504: public function restoreQuoteCustomerGroupId($observer)
505: {
506: $quoteAddress = $observer->getQuoteAddress();
507: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType();
508:
509: if ($quoteAddress->hasPrevQuoteCustomerGroupId()
510: && $configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING
511: ) {
512: $quoteAddress->getQuote()->setCustomerGroupId($quoteAddress->getPrevQuoteCustomerGroupId());
513: $quoteAddress->unsPrevQuoteCustomerGroupId();
514: }
515: }
516: }
517: