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: 36: 37: 38:
39: class Eabi_Postoffice_Model_Observer {
40:
41:
42: 43: 44:
45: public function listenLicense($observer) {
46:
47: $shipmentMethod = $observer->getEvent()->getShipmentMethod();
48:
49: $request = $observer->getEvent()->getRequest();
50:
51:
52:
53: }
54:
55:
56: 57: 58:
59: public function registerUsage($observer) {
60: $order = $observer->getEvent()->getOrder();
61:
62: }
63:
64:
65: 66: 67: 68: 69:
70: public function validateSelectedTerminal($observer) {
71:
72:
73: $order = $observer->getEvent()->getOrder();
74:
75: $quote = $observer->getEvent()->getQuote();
76:
77: if ($order->getIsNotVirtual()) {
78: $shippingMethod = $order->getShippingMethod();
79:
80: $shippingCarrierCode = substr($shippingMethod, 0, strpos($shippingMethod, '_'));
81: $shippingMethodModel = Mage::getModel('shipping/shipping')->getCarrierByCode($shippingCarrierCode, $order->getStoreId());
82: if ($shippingMethodModel && ($shippingMethodModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)) {
83: $resultCarrierId = substr($shippingMethod, strrpos($shippingMethod, '_') + 1);
84: if (!is_numeric($resultCarrierId)) {
85: Mage::throwException($this->_getOfficeHelper()->__('Please select pickup point for %s', $shippingMethodModel->getConfigData('title')));
86:
87: }
88: }
89: }
90: }
91:
92: 93: 94: 95:
96: public function dispatchEmailsToCarriers($observer) {
97:
98:
99: $order = $observer->getEvent()->getOrder();
100: $shipmentMethod = $observer->getEvent()->getShipmentMethod();
101: $request = $observer->getEvent()->getRequest();
102: $result = $observer->getEvent()->getResult();
103: $barcode = $shipmentMethod->getBarcode($order);
104:
105: if (!$barcode) {
106:
107: return;
108: }
109: if (is_array($barcode)) {
110: $barcode = implode(', ', $barcode);
111: }
112:
113:
114:
115: $appEmulation = Mage::getSingleton('core/app_emulation');
116: $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($order->getStoreId());
117:
118: try {
119:
120: $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
121: ->setIsSecureMode(true);
122: $paymentBlock->getMethod()->setStore($order->getStoreId());
123: $paymentBlockHtml = $paymentBlock->toHtml();
124: } catch (Exception $exception) {
125:
126: $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
127: throw $exception;
128: }
129:
130:
131:
132: if ($order->getCustomerIsGuest()) {
133: $templateId = Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_GUEST_TEMPLATE, $order->getStoreId());
134: $customerName = $order->getBillingAddress()->getName();
135: } else {
136: $templateId = Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_TEMPLATE, $order->getStoreId());
137: $customerName = $order->getCustomerName();
138: }
139:
140:
141:
142: $mailer = $this->_getEmailerModel();
143:
144:
145:
146: Mage::dispatchEvent('eabi_' . $shipmentMethod->getCode() . '_autosend_data_success', array(
147: 'request' => $request,
148: 'shipment_method' => $shipmentMethod,
149: 'order' => $order,
150: 'result' => $result,
151:
152: 'mailer' => array($mailer),
153: ));
154:
155:
156: $order->setCustomerNoteNotify(true);
157: $order->setCustomerNote($this->_getOfficeHelper()->__('Barcode: %s', $barcode));
158:
159:
160: $mailer->setSender(Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()));
161: $mailer->setStoreId($order->getStoreId());
162: $mailer->setTemplateId($templateId);
163: $mailer->setTemplateParams(array(
164: 'order' => $order,
165: 'billing' => $order->getBillingAddress(),
166: 'payment_html' => $paymentBlockHtml
167: )
168: );
169:
170:
171:
172: $mailer->send();
173:
174:
175:
176:
177: }
178:
179:
180:
181: 182: 183: 184:
185: protected function _getEmailerModel() {
186: return Mage::getModel('core/email_template_mailer');
187: }
188:
189: 190: 191: 192:
193: protected function _getOfficeHelper() {
194: return Mage::helper('eabi_postoffice');
195: }
196:
197:
198:
199: }
200: