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_parcelstore', false)) {
38: Module::getInstanceByName('eabi_dpd_parcelstore');
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51:
52: class eabi_dpd_courier extends eabi_dpd_parcelstore {
53: const CONST_PREFIX = 'E_DPDEEC_';
54:
55: const NAME = 'eabi_dpd_courier';
56:
57:
58: 59: 60: 61: 62:
63: private $form_fields = array();
64:
65:
66: 67: 68: 69:
70: protected $_parent_code;
71:
72:
73: 74: 75: 76:
77: public $dataSendExecutor;
78:
79: 80: 81: 82:
83: protected $_carrierDisplayed = false;
84:
85:
86: 87: 88:
89: protected function _construct() {
90: $this->_const_prefix = eabi_dpd_courier::CONST_PREFIX;
91: $this->name = 'eabi_dpd_courier';
92: $this->tab = 'shipping_logistics';
93: $this->_parent_code = eabi_dpd_parcelstore::NAME;
94: $this->version = '0.4';
95: $this->dependencies[] = 'eabi_dpd_parcelstore';
96: $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.7');
97: $this->displayName = $this->l('DPD kulleriga koju või tööle');
98: $this->description = $this->l('DPD offers high-quality shipping service from Estonia to whole Europe.');
99: $this->confirmUninstall = $this->l('Are you sure you want to delete your details?');
100: if (file_exists(_PS_MODULE_DIR_ . eabi_dpd_parcelstore::NAME. '/datasend-executor.php')) {
101: if (!class_exists(eabi_dpd_parcelstore::NAME . '_data_send_executor', false)) {
102: require_once(_PS_MODULE_DIR_ . eabi_dpd_parcelstore::NAME . '/datasend-executor.php');
103: }
104: $executorClass = eabi_dpd_parcelstore::NAME . '_data_send_executor';
105: $this->dataSendExecutor = new $executorClass($this);
106: $this->dataSendExecutor->setConfigPrefix(eabi_dpd_parcelstore::CONST_PREFIX);
107: }
108: if (!$this->_getHelperModule() || !$this->getConfigData('TITLE') OR !$this->getConfigData('HANDLING_FEE')) {
109: $this->warning = $this->l('Details must be configured in order to use this module correctly');
110: }
111:
112:
113: $this->form_fields = array();
114:
115:
116: }
117:
118:
119: 120: 121:
122: private function init() {
123: }
124:
125: 126: 127: 128: 129: 130: 131: 132:
133: public function getRequestForAutoSendData($order, $address, $selectedOfficeId, $forceCod = false) {
134: $telephone = $address->phone_mobile;
135: if (!$telephone) {
136: $telephone = $address->phone;
137: }
138:
139: $requestData = array(
140: 'Sh_name' => $address->firstname.' '.$address->lastname,
141: 'Sh_company' => $address->company,
142: 'Sh_street' => implode(' ', array($address->address1, $address->address2)),
143: 'Sh_postal' => $address->postcode,
144: 'Sh_country' => strtolower(Country::getIsoById($address->id_country)),
145: 'Sh_city' => $address->city,
146: 'Sh_contact' => $address->firstname.' '.$address->lastname,
147: 'Sh_phone' => $telephone,
148: 'Po_remark' => $this->_getRemark($order),
149: 'Sh_remark' => '',
150: 'Sh_pudo' => 'false',
151: 'Sh_parcel_qty' => $this->_getNumberOfPackagesForOrder($order),
152: 'Sh_cust_reference' => $order->id,
153: );
154: if ($address->id_state) {
155: $requestData['Sh_city'] = $address->city . ', ' . State::getNameById($address->id_state);
156: }
157:
158: if (!$order->hasBeenPaid() && $forceCod) {
159: $requestData['Sh_cod_amount'] = number_format(round($order->total_paid_tax_incl, 2), 2, '.', '');
160: $requestData['Sh_cod_currency'] = $this->_getCurrencyIso($order->id_currency);
161: }
162:
163:
164:
165:
166: return $requestData;
167:
168: }
169:
170:
171:
172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: protected function _install() {
186: if (!$this->registerHook('extraCarrier')
187: || !$this->_getHelperModule()->addCarrierModule($this->name, get_class($this), self::TRACKING_URL) or !$this->registerHook('paymentConfirm')
188: || !$this->registerHook('actionAdminControllerSetMedia')
189: || !$this->registerHook('displayBackOfficeFooter')
190: || !$this->registerHook('displayHeader')
191: || !$this->registerHook('displayFooter')
192: || !$this->upgrade_module_0_3()) {
193: return false;
194: }
195: return true;
196: }
197:
198:
199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
213: protected function _uninstall() {
214: if (!$this->unregisterHook('extraCarrier') || !$this->unregisterHook('paymentConfirm')
215: || !$this->unregisterHook('displayBackOfficeFooter')
216: || !$this->unregisterHook('displayHeader')
217: || !$this->unregisterHook('displayFooter')) {
218: return false;
219: }
220:
221:
222: $this->unregisterHook('displayBackOfficeHeader');
223: $this->unregisterHook('actionAdminControllerSetMedia');
224: if (!$this->_getHelperModule()->removeCarrierModule($this->name)) {
225: return false;
226: }
227:
228: return true;
229:
230: }
231:
232:
233:
234:
235: 236: 237: 238: 239:
240: public function hookPaymentConfirm(&$params) {
241: if ($this->dataSendExecutor != null) {
242: $this->dataSendExecutor->hookpaymentConfirm($params);
243: }
244: return '';
245: }
246:
247: 248: 249:
250: public function hookActionAdminControllerSetMedia() {
251: $path = __PS_BASE_URI__.'modules/'.eabi_dpd_parcelstore::NAME.'/';
252: $this->context->controller->addCSS($path . 'css/'. eabi_dpd_parcelstore::NAME.'.css', 'all');
253: $this->context->controller->addJqueryPlugin('loadTemplate', $path .'js/plugins/');
254: $this->context->controller->addJS($path . 'js/'.eabi_dpd_parcelstore::NAME.'.js');
255:
256: }
257:
258: 259: 260:
261: public function () {
262: $path = __PS_BASE_URI__.'modules/'.eabi_dpd_parcelstore::NAME.'/';
263: $this->context->controller->addCSS($path . 'css/'.eabi_dpd_parcelstore::NAME.'-public.css', 'all');
264: $this->context->controller->addJS($path . 'js/'.eabi_dpd_parcelstore::NAME.'-public.js');
265: }
266:
267: 268: 269: 270: 271:
272: public function () {
273: $className = 'OrderOpcController';
274: $php_self = 'order-opc';
275: if ($this->context->controller instanceof $className && $this->context->controller->php_self == $php_self) {
276: if (!$this->_carrierDisplayed) {
277: return $this->hookExtraCarrier(array('cart' => $this->context->cart));
278: }
279:
280: }
281: }
282:
283:
284:
285:
286: 287: 288: 289:
290: private function _postProcess() {
291: $html = '';
292: $data = $_POST;
293: if (isset($data['btnSubmit'])) {
294: foreach ($this->_initFormFields() as $formFieldName => $formFieldDataSet) {
295:
296: if ($formFieldDataSet['type'] == 'multiselect') {
297: if (!isset($data[strtoupper($formFieldName)])) {
298: $data[strtoupper($formFieldName)] = '';
299: }
300: if (is_array($data[strtoupper($formFieldName)])) {
301: $data[strtoupper($formFieldName)] = implode(',', $data[strtoupper($formFieldName)]);
302: }
303: }
304:
305:
306: if (is_array($data[strtoupper($formFieldName)])) {
307: $data[strtoupper($formFieldName)] = serialize($data[strtoupper($formFieldName)]);
308: }
309: Configuration::updateValue(self::CONST_PREFIX . strtoupper($formFieldName), $data[strtoupper($formFieldName)]);
310: }
311: $this->_getHelperModule()->setTaxGroup($this->name, $this->getConfigData('TAX'));
312:
313:
314: $title = $this->getConfigData('TITLE');
315: $finalTitle = $title;
316: if ($this->_isSerialized($title)) {
317: $title = @unserialize($title);
318: if (is_array($title)) {
319: $finalTitle = isset($title[$this->context->language->id])?$title[$this->context->language->id]:$title[0];
320: }
321: }
322: $this->_getHelperModule()->setDisplayName($this->name, $finalTitle);
323:
324: $this->_getHelperModule()->refresh($this->name, true);
325:
326:
327: $html .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="' . $this->l('ok') . '" /> ' . $this->l('Settings updated') . '</div>';
328: }
329: return $html;
330: }
331:
332: 333: 334: 335:
336: private function () {
337: $html = '<img src="../modules/'.$this->name.'/'.$this->name.'.png" style="float:left; margin-right:15px;"><b>' . $this->l('DPD offers high-quality shipping service from Estonia to whole Europe.') . '</b><br /><br />
338: ' . $this->l('DPD courier service can be used in Estonia, Baltics and Europe.') . '<br />';
339: return $html;
340: }
341:
342:
343:
344:
345:
346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366:
367: public function ($params) {
368:
369: $shouldHide = false;
370: $this->_carrierDisplayed = true;
371:
372:
373: $cart = $params['cart'];
374: $summaryDetails = $cart->getSummaryDetails();
375:
376:
377: if ($this->getConfigData('SALLOWSPECIFIC') == '1') {
378: $allowedCountries = explode(',', $this->getConfigData('SPECIFICCOUNTRY'));
379: if (!in_array(Country::getIsoById($summaryDetails['delivery']->id_country), $allowedCountries)) {
380: $shouldHide = true;
381: }
382: }
383:
384:
385: if (!$summaryDetails['delivery']->country) {
386: $shouldHide = true;
387: if (!isset($params['address']) || !$params['address']) {
388: $params['address'] = (object) array(
389: 'id' => '0',
390: );
391: }
392: }
393:
394:
395: if ($this->getConfigData('CHECKITEMS') == 'yes' && !$shouldHide) {
396: $prods = $cart->getProducts();
397: foreach ($prods as $prod) {
398: if (stripos($prod['description_short'], '<!-- no dpd_ee_module -->') !== false) {
399: $shouldHide = true;
400: break;
401: }
402: }
403: }
404:
405:
406: $loadedProductWeights = array();
407: if (($this->getConfigData('MAX_PACKAGE_WEIGHT') > 0 || $this->getConfigData('MIN_PACKAGE_WEIGHT') > 0) && !$shouldHide) {
408: $products = $cart->getProducts();
409: foreach ($products as $product) {
410: if ($product['is_virtual']) {
411: continue;
412: }
413: for ($i = 0; $i < $product['cart_quantity']; $i++) {
414: $loadedProductWeights[] = $product['weight'];
415: }
416: if ($this->getConfigData('MAX_PACKAGE_WEIGHT') > 0 && !$shouldHide) {
417: if (max($loadedProductWeights) > (float)$this->getConfigData('MAX_PACKAGE_WEIGHT')) {
418: $shouldHide = true;
419: }
420: }
421: if ($this->getConfigData('MIN_PACKAGE_WEIGHT') > 0 && !$shouldHide) {
422: if (min($loadedProductWeights) > (float)$this->getConfigData('MIN_PACKAGE_WEIGHT')) {
423: $shouldHide = true;
424: }
425: }
426: }
427: }
428:
429: $title = $this->getConfigData('TITLE');
430: $finalTitle = $title;
431: if ($this->_isSerialized($title)) {
432: $title = @unserialize($title);
433: if (is_array($title)) {
434: $finalTitle = isset($title[$this->context->language->id]) ? $title[$this->context->language->id] : $title[0];
435: }
436: }
437:
438:
439:
440: $extraParams = array(
441: 'id_address_delivery' => $cart->id_address_delivery,
442: 'price' => $this->getOrderShippingCost($cart),
443: 'title' => $finalTitle,
444: 'logo' => __PS_BASE_URI__ . 'modules/' . $this->name . '/logo.gif',
445: 'id_address_invoice' => $cart->id_address_invoice,
446: 'error_message' => '',
447: 'is_default' => false,
448: );
449:
450: return $this->_getHelperModule()->displayExtraCarrier($this->name, $extraParams, $shouldHide);
451:
452:
453:
454:
455: }
456:
457:
458: 459: 460: 461:
462: public function getContent() {
463: $html = '<h2>'.$this->displayName.'</h2>';
464:
465: if (!empty($_POST)) {
466: $postErrors = $this->_postValidation();
467: if (!sizeof($postErrors)) {
468: $html .= $this->_postProcess();
469: } else {
470: foreach ($postErrors as $err) {
471: $html .= '<div class="alert error">' . $err . '</div>';
472: }
473:
474: }
475: }
476: else {
477: $html .= '<br />';
478:
479: }
480: $html .= $this->_displayFormHeader();
481: $html .= $this->_displayForm();
482:
483:
484: return $html;
485: }
486:
487: 488: 489: 490: 491:
492: public function getConfigData($param) {
493: $value = Configuration::get(eabi_dpd_courier::CONST_PREFIX . $param);
494: if ($value === null || $value === false) {
495: $formFields = $this->_initFormFields();
496: if (isset($formFields[strtolower($param)]) && $formFields[strtolower($param)]['default']) {
497: return $formFields[strtolower($param)]['default'];
498: }
499: return Configuration::get(eabi_dpd_parcelstore::CONST_PREFIX . $param);
500: }
501: return $value;
502: }
503:
504:
505:
506:
507: 508: 509: 510: 511: 512: 513:
514: protected function _initFormFields() {
515: if (count($this->form_fields)) {
516: return $this->form_fields;
517: }
518: $yesno = array(
519: 'yes' => $this->l('Yes'),
520: 'no' => $this->l('No'),
521: );
522: $boxUnits = array(
523: 'order' => $this->l('Per Order'),
524: 'item' => $this->l('Per Package'),
525: );
526: $countryUnits = array(
527: '0' => $this->l('All Allowed Countries'),
528: '1' => $this->l('Specific Countries'),
529: );
530:
531: $this->form_fields = array(
532: 'title' => array(
533: 'title' => $this->l('Title'),
534: 'type' => 'multilang',
535: 'description' => $this->l('This controls the title which the user sees during checkout.'),
536: 'default' => $this->l('DPD kulleriga koju või tööle'),
537: 'css' => 'width: 300px;',
538: ),
539: 'handling_fee' => array(
540: 'title' => $this->l('Price'),
541: 'type' => 'text',
542: 'description' => '',
543: 'default' => '5.80',
544: 'css' => 'width: 300px;',
545: 'validate' => array('required_entry', 'validate_number'),
546: ),
547: 'tax' => array(
548: 'title' => $this->l('Tax Id'),
549: 'type' => 'select',
550: 'description' => $this->l('Prices here are tax inclusive'),
551: 'default' => '',
552: 'css' => 'width: 300px;',
553: 'validate' => array(),
554: 'options' => $this->_getHelperModule()->getTaxes(),
555: ),
556: 'handling_fee_country' => array(
557: 'title' => $this->l('Price per country'),
558: 'type' => 'countryprice',
559: 'description' => $this->l('If country is not listed here, but this method is available, then general handling fee is applied'),
560: 'default' => 'a:3:{s:18:"_1388700940288_288";a:5:{s:10:"country_id";s:2:"EE";s:10:"base_price";s:3:"5.8";s:8:"kg_price";s:4:"1.28";s:18:"free_shipping_from";s:0:"";s:7:"cod_fee";s:4:"2.50";}s:18:"_1388700961178_178";a:5:{s:10:"country_id";s:2:"LV";s:10:"base_price";s:5:"10.38";s:8:"kg_price";s:4:"3.15";s:18:"free_shipping_from";s:0:"";s:7:"cod_fee";s:4:"2.50";}s:18:"_1388700962221_221";a:5:{s:10:"country_id";s:2:"LT";s:10:"base_price";s:5:"11.75";s:8:"kg_price";s:4:"3.85";s:18:"free_shipping_from";s:0:"";s:7:"cod_fee";s:4:"2.50";}}',
561: 'css' => 'width: 300px;',
562: 'validate' => array('validate_handling_fee_country'),
563: ),
564: 'checkitems' => array(
565: 'title' => sprintf($this->l('Disable this carrier if product\'s short description contains HTML comment %s'), '<!-- no dpd_ee_module -->'),
566: 'type' => 'select',
567: 'description' => '',
568: 'default' => 'no',
569: 'css' => 'width: 300px;',
570: 'options' => $yesno,
571: ),
572: 'max_package_weight' => array(
573: 'title' => $this->l('Maximum allowed package weight for this carrier'),
574: 'type' => 'text',
575: 'description' => '',
576: 'default' => '31.5',
577: 'css' => 'width: 300px;',
578: ),
579: 'handling_action' => array(
580: 'title' => $this->l('Handling action'),
581: 'type' => 'select',
582: 'description' => $this->l('Per Order: Shipping cost equals Shipping price')
583: . '<br/>' . $this->l('Per Package: Shipping cost equals Number of Items in cart multiplied by shipping price')
584: ,
585: 'default' => 'yes',
586: 'css' => 'width: 300px;',
587: 'options' => $boxUnits,
588: ),
589: 'free_groups' => array(
590: 'title' => $this->l('Client groups who can get free shipping'),
591: 'type' => 'multiselect',
592: 'description' => $this->l('hold down CTRL / CMD button to select/deselect multiple'),
593: 'default' => '',
594: 'css' => 'width: 300px;',
595: 'options' => $this->_getHelperModule()->getClientGroups(),
596: ),
597: 'enable_free_shipping' => array(
598: 'title' => $this->l('Enable free shipping'),
599: 'type' => 'select',
600: 'description' => '',
601: 'default' => 'no',
602: 'css' => 'width: 300px;',
603: 'options' => $yesno,
604: ),
605: 'free_shipping_from' => array(
606: 'title' => $this->l('Free shipping subtotal'),
607: 'type' => 'text',
608: 'description' => '',
609: 'default' => '',
610: 'css' => 'width: 300px;',
611: 'validate' => array('validate_number'),
612: ),
613: 'sallowspecific' => array(
614: 'title' => $this->l('Ship to applicable countries'),
615: 'type' => 'select',
616: 'description' => '',
617: 'default' => '1',
618: 'css' => 'width: 300px;',
619: 'options' => $countryUnits,
620: ),
621: 'specificcountry' => array(
622: 'title' => $this->l('Ship to Specific countries'),
623: 'type' => 'multiselect',
624: 'description' => '',
625: 'default' => 'EE,LV,LT',
626: 'css' => 'width: 300px;',
627: 'options' => $this->_getHelperModule()->getCountriesAsOptions(),
628: ),
629:
630: );
631:
632:
633:
634: return $this->form_fields;
635: }
636:
637:
638: 639: 640: 641: 642: 643: 644:
645: public function __getPostOffices($code, &$groupId = null, &$officeId = null, &$addressId = null) {
646: $addressId = null;
647: }
648:
649: 650: 651: 652: 653: 654:
655: public function displayInfoByCart($cart_id) {
656: if ($this->dataSendExecutor != null) {
657: $extraInfo = $this->dataSendExecutor->displayInfoByCart($cart_id);
658: if ($extraInfo) {
659: return '<div class="eabi_dpd_parcelstore_chosen">'. $extraInfo.'</div>';
660: }
661: }
662: return false;
663: }
664:
665:
666: 667: 668: 669:
670: protected function _getHtmlHelper() {
671:
672: $helper = $this->_getHelperModule()->helper('html_helper', eabi_dpd_parcelstore::NAME);
673: $helper->setContext($this->context)->setModuleInstance(Module::getInstanceByName(eabi_dpd_parcelstore::NAME));
674: return $helper;
675: }
676:
677:
678:
679:
680: 681: 682: 683: 684:
685: public function setLastUpdated($lastUpdated) {
686: return;
687: }
688:
689: 690: 691: 692:
693: public function getLastUpdated() {
694: return time();
695: }
696:
697:
698: 699: 700: 701:
702: public function getGroupTitle($group) {
703: return '';
704: }
705:
706:
707: 708: 709: 710: 711: 712:
713: public function getTerminalTitle($terminal) {
714: $title = $this->getConfigData('TITLE');
715: $finalTitle = $title;
716: if ($this->_isSerialized($title)) {
717: $title = @unserialize($title);
718: if (is_array($title)) {
719: $finalTitle = isset($title[$this->context->language->id]) ? $title[$this->context->language->id] : $title[0];
720: }
721: }
722:
723: return htmlspecialchars($finalTitle);
724: }
725:
726:
727:
728: 729: 730: 731: 732: 733:
734: public function getAdminTerminalTitle($terminal) {
735: $title = $this->getConfigData('TITLE');
736: $finalTitle = $title;
737: if ($this->_isSerialized($title)) {
738: $title = @unserialize($title);
739: if (is_array($title)) {
740: $finalTitle = isset($title[$this->context->language->id]) ? $title[$this->context->language->id] : $title[0];
741: }
742: }
743:
744: return htmlspecialchars($finalTitle);
745: }
746:
747:
748:
749:
750: 751: 752: 753:
754: public function getOfficeList() {
755:
756: $result = array();
757: $result[] = array(
758: 'place_id' => 1,
759: 'name' => $this->getConfigData('TITLE'),
760: 'city' => '',
761: 'county' => '',
762: 'description' => '',
763: 'country' => '',
764: 'zip' => '',
765: 'group_sort' => 0,
766: );
767: return $result;
768:
769: }
770:
771:
772:
773: 774: 775: 776:
777: public function upgrade_module_0_3() {
778: 779: 780: 781: 782: 783: 784: 785:
786: $configPaths = array(
787: 'E_DPDEEC_HANDLING_FEE_COUNTRY',
788: );
789: $countriesToApply = array(
790: 'EE', 'LV', 'LT',
791: );
792: $db = Db::getInstance();
793:
794:
795:
796: $inConfigPaths = implode("','", $configPaths);
797: $sql = "SELECT * FROM `"._DB_PREFIX_."configuration` where `name` IN ('{$inConfigPaths}')";
798: $configDatas = $db->executeS($sql, true);
799:
800: foreach ($configDatas as $configData) {
801: $oldShippingPriceSet = @unserialize($configData['value']);
802:
803: if (is_array($oldShippingPriceSet)) {
804: foreach ($oldShippingPriceSet as $randomKey => $countryPrices) {
805: if (in_array($countryPrices['country_id'], $countriesToApply)) {
806: if (!isset($countryPrices['cod_fee']) || !$countryPrices['cod_fee']) {
807: $oldShippingPriceSet[$randomKey]['cod_fee'] = '2.50';
808: }
809: }
810: }
811:
812: $configData['value'] = serialize($oldShippingPriceSet);
813: $idConfiguration = $configData['id_configuration'];
814:
815: unset($configData['id_configuration']);
816:
817:
818:
819: unset($configData['id_shop']);
820: unset($configData['id_shop_group']);
821: unset($configData['name']);
822:
823: $res = $db->update('configuration', $configData, "id_configuration = '{$db->escape($idConfiguration)}'");
824: if ($res === false) {
825: return false;
826: }
827: }
828: }
829: return true;
830: }
831:
832:
833:
834: }
835: