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: 40:
41: class Eabi_DpdEE_Model_Button_Courier extends Eabi_Livehandler_Model_Adminhtml_Gridmanager {
42: protected $_id = 'eabi_dpdee__button_courier';
43: protected $_shippingMethodCode;
44:
45: public function _construct() {
46: parent::_construct();
47: $this->_init('eabi_dpdee/button_courier');
48:
49:
50: $this->_shippingMethodCode = Eabi_DpdEE_Model_Config::SHIPPING_METHOD_CODE_PARCEL_TERMINAL;
51: $shippingMethodModel = Mage::getModel('shipping/shipping')->getCarrierByCode($this->_shippingMethodCode);
52: if (!$shippingMethodModel || !$shippingMethodModel->getConfigData('active')) {
53: $this->_shippingMethodCode = Eabi_DpdEE_Model_Config::SHIPPING_METHOD_CODE_FLAT;
54: $shippingMethodModel = Mage::getModel('shipping/shipping')->getCarrierByCode($this->_shippingMethodCode);
55: }
56: if ($shippingMethodModel && ($shippingMethodModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)
57: && $shippingMethodModel->getConfigData('active')
58: && $shippingMethodModel->getConfigData('senddata_enable')
59: && $shippingMethodModel->getConfigData('courier_enable')
60: ) {
61: $this->addActionButton($this->_id, $this->_getDpdHelper()->__('Order courier to pick up goods'), 'return false;');
62: }
63:
64: }
65:
66:
67: 68: 69: 70: 71:
72: protected function _getAdditionalJs($currentJs) {
73: if (!count($this->_actionButtons)) {
74: return '';
75: }
76: $js = <<<JS
77: var eabi_dpdJsObject = new EabiDpdEE('{$this->_id}', null, function(infoBox) {
78: new Ajax.Request(action_url, {
79: method: 'post',
80: parameters: {},
81: asynchronous: false,
82: evalJSON: 'force',
83: onSuccess: function(transport){
84: var ul = new Element('ul', {'class': 'messages'});
85: if (transport.responseJSON.errors) {
86: transport.responseJSON.errors.each(function(message) {
87: ul.insert({bottom: '<li class="success-msg">' + message + '</li>'});
88: });
89: }
90: if (transport.responseJSON.messages) {
91: transport.responseJSON.messages.each(function(message) {
92: ul.insert({bottom: '<li class="success-msg">' + message + '</li>'});
93: });
94: }
95: if (transport.responseJSON.errors || transport.responseJSON.messages) {
96: infoBox.update(ul);
97: }
98: if (transport.responseJSON.html) {
99: infoBox.update(transport.responseJSON.html);
100: }
101: },
102: onFailure: function(transport){
103: alert('Request failed, check your error logs');
104: }
105: });
106:
107: }, null, function(endResult) { if (!endResult['Po_Date'] || endResult['Po_Date'] == '-') { return false; } return endResult; });
108: $('{$this->_id}').observe('click', function(event) {
109: var submitResult;
110: eabi_dpdJsObject.update('');
111:
112: submitResult = eabi_dpdJsObject.submit(action_url, function(json, infoBox) {
113: var ul = new Element('ul', {'class': 'messages'});
114: if (json.errors || json.messages) {
115: if (json.errors) {
116: json.errors.each(function(message) {
117: ul.insert({bottom: '<li class="success-msg">' + message + '</li>'});
118: });
119: }
120: if (json.messages) {
121: json.messages.each(function(message) {
122: ul.insert({bottom: '<li class="success-msg">' + message + '</li>'});
123: });
124: }
125:
126: infoBox.update(ul);
127:
128: } else {
129: if (json.html) {
130: eabi_dpdJsObject.update(json.html, 'dummy', null);
131: }
132:
133: }
134:
135:
136: });
137:
138:
139:
140: });
141:
142: JS;
143: return $js;
144: }
145:
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
163: public function service($params) {
164: if (!count($this->_actionButtons)) {
165:
166: return array();
167: }
168: try {
169: $api = $this->_getDpdHelper()->getApi(Mage_Core_Model_App::ADMIN_STORE_ID);
170:
171:
172: $isCourierComing = $api->isCourierComing();
173: if (isset($params['Po_Date']) && $params['Po_Date'] != '-' && !$isCourierComing) {
174:
175:
176: $orderSendData = array(
177: 'Po_remark' => isset($params['Po_remark']) ? $params['Po_remark'] : '',
178: 'Po_type' => 'PO',
179: 'Po_Date' => $params['Po_Date'],
180: 'Po_Time_from' => $this->_getTimeFrom($params['Po_Time']),
181: 'Po_Time_til' => $this->_getTimeTil($params['Po_Time']),
182: 'Po_envelope_qty' => $params['Po_envelope_qty'],
183: 'Po_parcel_qty' => $params['Po_parcel_qty'],
184: 'Po_pallet_qty' => $params['Po_pallet_qty'],
185: 'Sh_envelope_qty' => $params['Po_envelope_qty'],
186: 'Sh_parcel_qty' => $params['Po_parcel_qty'],
187: 'Sh_pallet_qty' => $params['Po_pallet_qty'],
188: 'Sh_pudo' => 'false',
189: );
190:
191: $orderSendResult = $api->autoSendData($orderSendData);
192:
193: $courierArrivalDate = $orderSendData['Po_Date'];
194: $courierArrivalTime = $orderSendData['Po_Time_from'] . ' ' . $this->_getDpdHelper()->__('and') . ' ' . $orderSendData['Po_Time_til'];
195:
196:
197:
198: $pickupTimeFrom = new Zend_Date(0, Zend_Date::TIMESTAMP);
199: $pickupTimeFrom->setTimezone('Europe/Tallinn');
200: $pickupTimeFrom->set($orderSendData['Po_Date'], 'yyyy-MM-dd');
201:
202:
203: if (strlen($orderSendData['Po_Time_from']) > 2) {
204: $pickupTimeFrom->add(substr($orderSendData['Po_Time_from'], strlen($orderSendData['Po_Time_from']) - 2), Zend_Date::MINUTE);
205: } else {
206: $pickupTimeFrom->add($orderSendData['Po_Time_from'], 'H');
207: }
208:
209:
210: $pickupTimeTill = new Zend_Date(0, Zend_Date::TIMESTAMP);
211: $pickupTimeTill->setTimezone('Europe/Tallinn');
212: $pickupTimeTill->set($orderSendData['Po_Date'] , 'yyyy-MM-dd');
213:
214: if (strlen($orderSendData['Po_Time_til']) > 2) {
215: $pickupTimeTill->add(substr($orderSendData['Po_Time_til'], strlen($orderSendData['Po_Time_til']) - 2), Zend_Date::MINUTE);
216: } else {
217: $pickupTimeTill->add($orderSendData['Po_Time_til'], 'H');
218: }
219:
220:
221:
222: $configTimeStamp = $pickupTimeFrom->get(Zend_Date::TIMESTAMP) . ',' . $pickupTimeTill->get(Zend_Date::TIMESTAMP);
223: $this->_getEabi()->setConfigData('carriers/eabidpdee/courier_pickup_time', $configTimeStamp, 'default', 0, true);
224: } else {
225: if (!$isCourierComing) {
226: $dpdBlock = Mage::getSingleton('core/layout')
227: ->createBlock('eabi_dpdee/order_courier')
228: ->setCode(Eabi_DpdEE_Model_Config::SHIPPING_METHOD_CODE_PARCEL_TERMINAL)
229: ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
230: ;
231:
232: $result = array(
233: 'needs_reload' => true,
234: 'is_action_error' => false,
235: 'html' => $dpdBlock->toHtml(),
236: );
237: return $result;
238: } else {
239: $dateFormatIso = Mage::app()->getLocale()
240: ->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
241:
242: $pickupTimeFrom = new Zend_Date($isCourierComing[0], Zend_Date::TIMESTAMP);
243: $pickupTimeTill = new Zend_Date($isCourierComing[1], Zend_Date::TIMESTAMP);
244: $pickupTimeFrom->setTimezone('Europe/Tallinn');
245: $pickupTimeTill->setTimezone('Europe/Tallinn');
246: $courierArrivalDate = $pickupTimeFrom->get($dateFormatIso);
247: $courierArrivalTime = $pickupTimeFrom->get(Zend_Date::HOUR_SHORT) . ' ' . $this->_getDpdHelper()->__('and') . ' ' . $pickupTimeTill->get(Zend_Date::HOUR_SHORT);
248: }
249: }
250: } catch (Mage_Core_Exception $e) {
251: $result = array(
252: 'errors' => array($e->getMessage()),
253: 'needs_reload' => false,
254: 'is_action_error' => false,
255: );
256: return $result;
257: } catch (Exception $e) {
258: $result = array(
259: 'errors' => array($this->_getDpdHelper()->__('Cannot call courier.').$e->__toString()),
260: 'needs_reload' => true,
261: 'is_action_error' => false,
262: );
263: return $result;
264: }
265: $result = array(
266: 'messages' => array($this->_getDpdHelper()->__('Courier comes to pick up your shipment on %1$s between %2$s', $courierArrivalDate, $courierArrivalTime)),
267: 'needs_reload' => true,
268: 'is_action_error' => false,
269: );
270: return $result;
271: }
272:
273:
274: 275: 276: 277: 278: 279:
280: protected function _getTimeFrom($input, $removeMinutesIfZero = true) {
281: $parts = explode('-', $input);
282: if ($removeMinutesIfZero) {
283: return str_replace('00', '', $parts[0]);
284: } else {
285: return $parts[0];
286: }
287: }
288:
289: 290: 291: 292: 293: 294:
295: protected function _getTimeTil($input, $removeMinutesIfZero = true) {
296: $parts = explode('-', $input);
297: if ($removeMinutesIfZero) {
298: return str_replace('00', '', $parts[1]);
299: } else {
300: return $parts[1];
301: }
302: }
303:
304:
305: 306: 307: 308:
309: protected function _getDpdHelper() {
310: return Mage::helper('eabi_dpdee');
311: }
312: 313: 314: 315:
316: protected function _getOfficeHelper() {
317: return Mage::helper('eabi_postoffice');
318: }
319:
320: 321: 322: 323: 324:
325: protected function _toJson($input) {
326: return json_encode($input);
327: }
328:
329: 330: 331: 332:
333: protected function _getEabi() {
334: return Mage::helper('eabi');
335: }
336:
337:
338:
339: }
340: