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: if (!defined('_PS_VERSION_')) {
34: exit;
35: }
36:
37: if (!class_exists('eabi_dpd_courier', false)) {
38: Module::getInstanceByName('eabi_dpd_courier');
39: }
40:
41:
42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: class eabi_dpd_parcelstore_data_send_executor {
54: 55: 56:
57: const = '-----EABI_DPDEE-----';
58:
59: 60: 61: 62:
63: protected $_code;
64:
65: 66: 67: 68:
69: protected $_configPrefix;
70:
71: 72: 73: 74:
75: protected $_baseInstance;
76:
77: 78: 79: 80:
81: protected static $_infoByCartDisplayed;
82:
83: 84: 85: 86:
87: protected $form_fields = array();
88:
89: protected static $_paymentConfirmRun = false;
90:
91:
92:
93:
94:
95: 96: 97: 98:
99: public function __construct($baseInstance) {
100: $this->_baseInstance = $baseInstance;
101: $this->_code = $this->_baseInstance->name;
102: $this->_configPrefix = constant(get_class($this->_baseInstance).'::CONST_PREFIX');
103:
104: }
105:
106: 107: 108: 109: 110:
111: public function setConfigPrefix($configPrefix) {
112: $this->_configPrefix = $configPrefix;
113: return $this;
114: }
115:
116: 117: 118: 119:
120: public function install() {
121: return true;
122: }
123:
124: 125: 126: 127:
128: public function uninstall() {
129: return true;
130: }
131:
132: 133: 134: 135:
136: public function _postProcess() {
137: return true;
138: }
139:
140: 141: 142: 143:
144: public function _postValidation(&$postErrors) {
145:
146: }
147:
148: 149: 150: 151: 152:
153: public function displayInfoByCart($cart_id) {
154: $data = '';
155: if (is_null(self::$_infoByCartDisplayed)) {
156:
157:
158: $order = new Order(Order::getOrderByCartId($cart_id));
159: $orderData = $this->getDataFromOrder($order);
160:
161: if (isset($orderData['Parcel_numbers'])) {
162: $data .= '<div class="eabi_dpd_parcelstore_track">';
163: foreach ($orderData['Parcel_numbers'] as $trackingNumber) {
164: $data .= '<p><a target="_blank" href="'.htmlspecialchars(str_replace('@', $trackingNumber, eabi_dpd_parcelstore::TRACKING_URL)).'">'. htmlspecialchars(sprintf($this->l('Track shipment: %s'), $trackingNumber)).'</a></p>';
165: }
166: $data .= '</div>';
167: }
168:
169: self::$_infoByCartDisplayed = TRUE;
170:
171: }
172: return $data;
173:
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190:
191: public function hookpaymentConfirm(&$params) {
192: if (self::$_paymentConfirmRun) {
193:
194: return;
195: }
196: $order = new Order($params['id_order']);
197: if ($this->_baseInstance->getConfigData('SENDDATA_ENABLE') != 'yes') {
198: return;
199: }
200:
201:
202: $order = new Order($params['id_order']);
203: if (!Validate::isLoadedObject($order)) {
204: return;
205: }
206:
207:
208: $carrier = new Carrier($order->id_carrier);
209: if (!Validate::isLoadedObject($carrier)) {
210: return;
211: }
212:
213:
214: if ($this->_baseInstance->name != $carrier->name) {
215: return;
216: }
217:
218: if (!in_array($carrier->name, array(eabi_dpd_parcelstore::NAME, eabi_dpd_courier::NAME))) {
219: return;
220: }
221:
222:
223: if ($this->isDataSent($order)) {
224: return;
225: }
226:
227:
228: $selectedParcelTerminals = $this->_baseInstance->_getHelperModule()->getOfficesFromCart($order->id_cart);
229:
230: $forceCod = isset($params['is_eabi_dpd_cod'])? (bool) $params['is_eabi_dpd_cod'] : false;
231:
232: foreach ($selectedParcelTerminals as $addressId => $selectedParcelTerminal) {
233: try {
234: $autoSendResult = $this->autoSendData($order, new Address($addressId), $selectedParcelTerminal, $forceCod);
235:
236: $this->_addOrderComment($order->id, $order->id_customer, sprintf($this->_baseInstance->ls('Parcel data sent to server, barcode: %s'), $autoSendResult['barcode']));
237:
238: } catch (Exception $ex) {
239:
240: $this->_addOrderComment($order->id, $order->id_customer, $this->_baseInstance->ls('Parcel data send to server failed: ').$ex->getMessage());
241:
242: }
243: }
244: self::$_paymentConfirmRun = true;
245:
246:
247: }
248:
249: 250: 251: 252: 253:
254: public function getBarcodePdf($order) {
255: $orderData = $this->getDataFromOrder($order);
256: if (isset($orderData['PDF_URL']) && $orderData['PDF_URL']) {
257: return file_get_contents(urldecode($orderData['PDF_URL']));
258: }
259: }
260:
261:
262: 263: 264: 265: 266:
267: public function getBarcode($order) {
268: $orderData = $this->getDataFromOrder($order);
269: if (isset($orderData['PDF_URL']) && $orderData['PDF_URL']) {
270: return $orderData['PDF_URL'];
271: }
272: return false;
273: }
274:
275:
276: 277: 278: 279: 280:
281: protected function _getNumberOfPackagesForOrder($order) {
282: $productWeights = array();
283: $orderItems = $order->getProducts();
284: foreach ($orderItems as $orderItem) {
285:
286: for ($i = 0; $i < ($orderItem['product_quantity'] - $orderItem['product_quantity_refunded']); $i++) {
287: $productWeights[] = $orderItem['product_weight'];
288: }
289:
290: }
291: return $this->_getDpdHelper()->getNumberOfPackagesFromItemWeights($productWeights, $this->_baseInstance->getConfigData('MAX_PACKAGE_WEIGHT'));
292: }
293:
294:
295: 296: 297: 298: 299: 300: 301:
302: protected function ($id_order, $id_customer, $message) {
303: $db = Db::getInstance();
304:
305: return $db->insert('message', array(
306: 'id_order' => $id_order,
307: 'message' => $db->escape($message),
308: 'private' => 1,
309: 'id_customer' => $id_customer,
310: 'date_add' => date('Y-m-d H:i:s'),
311: ));
312:
313: }
314:
315:
316: 317: 318: 319: 320:
321: public function isDataSent($order) {
322: $orderData = $this->getDataFromOrder($order);
323: if (isset($orderData['DPD_OrderID'])) {
324: return true;
325: }
326: return false;
327: }
328:
329:
330:
331:
332: 333: 334: 335:
336: protected function _getDialCodeHelper() {
337: return $this->_baseInstance->_getHelperModule()->helper('dialcode_helper', 'eabi_postoffice');
338: }
339:
340:
341:
342: 343: 344: 345: 346: 347: 348: 349:
350: public function autoSendData($order, $address, $selectedOfficeId, $forceCod = false) {
351:
352:
353:
354: $requestResult = $this->_getDpdHelper()->getApi(Context::getContext()->shop->id, $this->_configPrefix)
355: ->autoSendData($this->_baseInstance->getRequestForAutoSendData($order, $address, $selectedOfficeId, $forceCod));
356:
357: $this->setDataToOrder($order, $requestResult);
358:
359: $db = Db::getInstance();
360: $db->update('orders', array('shipping_number' => $db->escape(implode('', $requestResult['Parcel_numbers']))), 'id_order = '.$db->escape($order->id));
361: $db->update('order_carrier', array('tracking_number' => $db->escape(implode('', $requestResult['Parcel_numbers']))), 'id_order = '.$db->escape($order->id));
362:
363:
364:
365:
366:
367:
368: return array('barcode' => '##'. implode(',', $requestResult['Parcel_numbers']).'##');
369: }
370:
371:
372: 373: 374: 375:
376: protected function _getDpdHelper() {
377: return $this->_baseInstance->_getHelperModule()->helper('dpd_helper', eabi_dpd_parcelstore::NAME);
378: }
379:
380:
381:
382: 383: 384: 385:
386: public function initFormFields() {
387: if (count($this->form_fields)) {
388: return $this->form_fields;
389: }
390: $yesno = array(
391: 'yes' => $this->l('Yes'),
392: 'no' => $this->l('No'),
393: );
394: $labelPositions = array(
395: '1234' => sprintf($this->l('Position %s'), '1234'),
396: '4123' => sprintf($this->l('Position %s'), '4123'),
397: '3412' => sprintf($this->l('Position %s'), '3412'),
398: '3421' => sprintf($this->l('Position %s'), '3421'),
399: );
400:
401: $this->form_fields = array(
402: 'senddata_enable' => array(
403: 'title' => $this->l('Auto send data to DPD server'),
404: 'type' => 'select',
405: 'description' => $this->l('Only if the order has been paid for or the order is COD'),
406: 'default' => 'no',
407: 'css' => 'width: 300px;',
408: 'options' => $yesno,
409: ),
410: 'courier_enable' => array(
411: 'title' => $this->l('Allow courier pickup'),
412: 'type' => 'select',
413: 'description' => $this->l('Only if the order has been paid for or the order is COD'),
414: 'default' => 'yes',
415: 'css' => 'width: 300px;',
416: 'options' => $yesno,
417: 'validate-if' => array('senddata_enable' => 'yes'),
418: ),
419: 'sendpackage_username' => array(
420: 'title' => $this->l('DPD Self service username'),
421: 'type' => 'text',
422: 'description' => '',
423: 'default' => '',
424: 'css' => 'width: 300px;',
425: 'validate' => array('required_entry'),
426: 'validate-if' => array('senddata_enable' => 'yes'),
427: ),
428: 'sendpackage_password' => array(
429: 'title' => $this->l('DPD Self-service password'),
430: 'type' => 'password',
431: 'description' => '',
432: 'default' => '',
433: 'css' => 'width: 300px;',
434: 'validate' => array('required_entry'),
435: 'validate-if' => array('senddata_enable' => 'yes'),
436: ),
437: 'http_request_timeout' => array(
438: 'title' => $this->l('Http request timeout'),
439: 'type' => 'text',
440: 'description' => $this->_baseInstance->ls('If timeout is greater than 10 seconds, then parcel data can only be sent manually. This field with the limitation will be removed latest 17th of February'),
441: 'default' => '60',
442: 'css' => 'width: 300px;',
443: 'validate' => array('required_entry', 'validate_digit'),
444: 'validate-if' => array('senddata_enable' => 'yes'),
445: ),
446: 'return_name' => array(
447: 'title' => $this->l('Pickup address name'),
448: 'type' => 'text',
449: 'description' => '',
450: 'default' => '',
451: 'css' => 'width: 300px;',
452: 'validate' => array('required_entry'),
453: 'validate-if' => array('senddata_enable' => 'yes'),
454: ),
455: 'return_company' => array(
456: 'title' => $this->l('Pickup address company'),
457: 'type' => 'text',
458: 'description' => '',
459: 'default' => '',
460: 'css' => 'width: 300px;',
461: 'validate' => array('required_entry'),
462: 'validate-if' => array('senddata_enable' => 'yes'),
463: ),
464: 'return_email' => array(
465: 'title' => $this->l('Pickup address e-mail'),
466: 'type' => 'text',
467: 'description' => '',
468: 'default' => '',
469: 'css' => 'width: 300px;',
470: 'validate' => array('required_entry', 'validate_email'),
471: 'validate-if' => array('senddata_enable' => 'yes'),
472: ),
473: 'return_phone' => array(
474: 'title' => $this->l('Pickup address phone'),
475: 'type' => 'text',
476: 'description' => '',
477: 'default' => '',
478: 'css' => 'width: 300px;',
479: 'validate' => array('required_entry'),
480: 'validate-if' => array('senddata_enable' => 'yes'),
481: ),
482: 'return_street' => array(
483: 'title' => $this->l('Pickup address street'),
484: 'type' => 'text',
485: 'description' => '',
486: 'default' => '',
487: 'css' => 'width: 300px;',
488: 'validate' => array('required_entry'),
489: 'validate-if' => array('senddata_enable' => 'yes'),
490: ),
491: 'return_citycounty' => array(
492: 'title' => $this->l('Pickup address city, county'),
493: 'type' => 'text',
494: 'description' => '',
495: 'default' => '',
496: 'css' => 'width: 300px;',
497: 'validate' => array('required_entry'),
498: 'validate-if' => array('senddata_enable' => 'yes'),
499: ),
500: 'return_postcode' => array(
501: 'title' => $this->l('Pickup address zip code'),
502: 'type' => 'text',
503: 'description' => '',
504: 'default' => '',
505: 'css' => 'width: 300px;',
506: 'validate' => array('required_entry'),
507: 'validate-if' => array('senddata_enable' => 'yes'),
508: ),
509: 'return_country' => array(
510: 'title' => $this->l('Pickup address country'),
511: 'type' => 'select',
512: 'description' => '',
513: 'default' => 'EE',
514: 'css' => 'width: 300px;',
515: 'options' => $this->_baseInstance->_getHelperModule()->getCountriesAsOptions(),
516: 'validate' => array('required_entry'),
517: 'validate-if' => array('senddata_enable' => 'yes'),
518: ),
519: 'po_show_on_label' => array(
520: 'title' => $this->l('Show pickup address on packing label'),
521: 'type' => 'select',
522: 'description' => '',
523: 'default' => 'no',
524: 'css' => 'width: 300px;',
525: 'options' => $yesno,
526: 'validate-if' => array('senddata_enable' => 'yes'),
527: ),
528: 'label_position' => array(
529: 'title' => $this->l('Labels position on packing slip'),
530: 'type' => 'select',
531: 'description' => '',
532: 'default' => 'no',
533: 'css' => 'width: 300px;',
534: 'options' => $labelPositions,
535: 'validate-if' => array('senddata_enable' => 'yes'),
536: ),
537: 'api_url' => array(
538: 'title' => $this->l('Api URL'),
539: 'type' => 'text',
540: 'description' => '<b>Live:</b> http://www.pakivedu.ee/rpc/gateway<br/><b>Test:</b> http://demo.surflink.ee:51680/rpc/gateway/',
541: 'default' => 'http://www.pakivedu.ee/rpc/gateway',
542: 'css' => 'width: 300px;',
543: 'validate' => array('required_entry'),
544: 'validate-if' => array('senddata_enable' => 'yes'),
545: ),
546: 'enable_log' => array(
547: 'title' => $this->l('Log API requests'),
548: 'type' => 'select',
549: 'description' => '',
550: 'default' => 'no',
551: 'css' => 'width: 300px;',
552: 'options' => $yesno,
553: 'validate-if' => array('senddata_enable' => 'yes'),
554: ),
555:
556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567:
568:
569: );
570: return $this->form_fields;
571:
572: }
573:
574: 575: 576: 577: 578:
579: public function getDataFromOrder($order) {
580: return $this->_baseInstance->_getHelperModule()->getDataFromOrder($order, self::ORDER_COMMENT_START_PREFIX);
581: }
582:
583: 584: 585: 586: 587: 588:
589: public function setDataToOrder($order, $data = array()) {
590: return $this->_baseInstance->_getHelperModule()->setDataToOrder($order, $data, self::ORDER_COMMENT_START_PREFIX);
591: }
592:
593:
594: 595: 596: 597: 598: 599:
600: public function l($string) {
601: return $this->_baseInstance->ls($string);
602: }
603:
604:
605:
606:
607:
608:
609:
610:
611: }
612:
613: