name = 'colissimosuivi'; $this->tab = 'shipping_logistics'; $this->version = '1.5.1'; $this->author = 'Agencya'; $this->module_key = '79bf522022441e1bc04121c23ab2adec'; $config = Configuration::getMultiple(array('COLISSIMOSUIVI_UPDSTATES', 'COLISSIMOSUIVI_NUMCONTRAT', 'COLISSIMOSUIVI_PASS', 'COLISSIMOSUIVI_CARRIERS')); if (isset($config['COLISSIMOSUIVI_NUMCONTRAT'])) { $this->colissimosuivi_numcontrat = $config['COLISSIMOSUIVI_NUMCONTRAT']; } if (isset($config['COLISSIMOSUIVI_PASS'])) { $this->colissimosuivi_pass = $config['COLISSIMOSUIVI_PASS']; } if (isset($config['COLISSIMOSUIVI_UPDSTATES'])) { $this->colissimosuivi_updstates = $config['COLISSIMOSUIVI_UPDSTATES']; } $this->colissimosuivi_carriers = Tools::unSerialize($config['COLISSIMOSUIVI_CARRIERS']); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Colissimo • Track my shipments'); $this->description = $this->l('Easily track your orders shipped by La Poste Colissimo for you and your customers.'); $this->confirmUninstall = $this->l('Are you sure to uninstall this module ?'); if ((!isset($this->colissimosuivi_numcontrat) || empty($this->colissimosuivi_numcontrat))) { $this->warning .= $this->l('Please enter your Coliposte contract number. '); } if ((!isset($this->colissimosuivi_pass) || empty($this->colissimosuivi_pass))) { $this->warning .= $this->l('Please enter your Coliposte password.'); } $this->ps_versions_compliancy = array('min' => '1.5.0.0', 'max' => _PS_VERSION_); $this->url = $this->context->shop->getBaseURL(true).'modules/'.$this->name.'/'; } /** * @return bool */ public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } $tab = new Tab(); $tab->class_name = 'AdminColissimoSuivi'; foreach (Language::getLanguages(true) as $lang) { $tab->name[(int)$lang['id_lang']] = $this->l('Colissimo • Tracking'); } $tab->id_parent = (int)Tab::getIdFromClassName('AdminParentShipping'); $tab->module = $this->name; $tab->add(); Configuration::updateValue('COLISSIMOSUIVI_NUMCONTRAT', ''); Configuration::updateValue('COLISSIMOSUIVI_PASS', ''); Configuration::updateValue('COLISSIMOSUIVI_UPDSTATES', true); Configuration::updateValue('COLISSIMOSUIVI_MAPPING', serialize(array())); Configuration::updateValue('COLISSIMOSUIVI_CARRIERS', serialize(array())); Configuration::updateValue('COLISSIMOSUIVI_INSTALLED', false); Configuration::updateValue('COLISSIMOSUIVI_FIND_TRACKING', true); if (Tools::version_compare(_PS_VERSION_, '1.6.0.9', '<')) { return ( parent::install() && $this->installDB() && $this->registerHook('displayOrderDetail') && $this->registerHook('actionAdminOrdersTrackingNumberUpdate') && $this->registerHook('actionCarrierUpdate') && $this->registerHook('adminOrder') ); } else { return ( parent::install() && $this->installDB() && $this->registerHook('displayOrderDetail') && $this->registerHook('actionAdminOrdersTrackingNumberUpdate') && $this->registerHook('actionCarrierUpdate') && $this->registerHook('displayAdminOrderTabShip') && $this->registerHook('displayAdminOrderContentShip') ); } } /** * @return bool */ public function installDB() { return Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'colissimo_suivi` ( `id_colissimo_suivi` INT(10) NOT NULL AUTO_INCREMENT, `id_order` INT(10) NOT NULL, `tracking` VARCHAR(13) NOT NULL, `error_code` INT(10) NOT NULL DEFAULT \'0\', `event_code` VARCHAR(6) NULL, `event_site` VARCHAR(30) NULL, `event_libelle` VARCHAR(256) NULL, `event_date` DATETIME NOT NULL, `date_add` DATETIME NOT NULL, `date_upd` DATETIME NOT NULL, PRIMARY KEY ( `id_colissimo_suivi` ) ) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET = utf8;' ); } /** * @return bool */ public function uninstall() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } $tab = new Tab((int)Tab::getIdFromClassName('AdminColissimoSuivi')); $tab->delete(); Configuration::deleteByName('COLISSIMOSUIVI_NUMCONTRAT'); Configuration::deleteByName('COLISSIMOSUIVI_PASS'); Configuration::deleteByName('COLISSIMOSUIVI_UPDSTATES'); Configuration::deleteByName('COLISSIMOSUIVI_MAPPING'); Configuration::deleteByName('COLISSIMOSUIVI_CARRIERS'); Configuration::deleteByName('COLISSIMOSUIVI_INSTALLED'); Configuration::deleteByName('COLISSIMOSUIVI_FIND_TRACKING'); Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'colissimo_suivi'); return parent::uninstall(); } /** * @param array|null $id_list */ public function updateOrdersStatus($id_list = null) { if ((bool)Configuration::get('COLISSIMOSUIVI_FIND_TRACKING')) { self::fillOrdersData(); } if ($id_list == null) { $id_list = ColissimoSuiviModel::getAll(); } foreach ($id_list as $id_detail) { $detail_suivi = new ColissimoSuiviModel((int)$id_detail['id_colissimo_suivi']); if (Validate::isTrackingNumber((string)$detail_suivi->tracking)) { $retour = $this->getColissimoTracking((string)$detail_suivi->tracking); if ($retour->errorCode == 0 && Validate::isLoadedObject($detail_suivi)) { $order = new Order($detail_suivi->id_order); if (Validate::isLoadedObject($order)) { if ((bool)$this->colissimosuivi_updstates == true && Tools::substr((string)$retour->eventCode, 0, 3) === 'LIV' && !in_array($order->current_state, array(_PS_OS_DELIVERED_, _PS_OS_REFUND_))) { Logger::addLog($this->l('Changing order status to delivered.'), 1, null, 'ColissimoSuivi', $detail_suivi->id_order); $order->setCurrentState(_PS_OS_DELIVERED_); $order->update(); } if (strtotime((string)$retour->eventDate) != strtotime((string)$detail_suivi->event_date)) { $detail_suivi->event_code = (string)$retour->eventCode; $detail_suivi->event_site = (string)$retour->eventSite; $detail_suivi->event_date = date('Y-m-d H:i:s', strtotime((string)$retour->eventDate)); $detail_suivi->error_code = (string)$retour->errorCode; $detail_suivi->event_libelle = (string)$retour->eventLibelle; $detail_suivi->update(); } } } elseif ($retour->errorCode != 0 && Validate::isLoadedObject($detail_suivi)) { $detail_suivi->event_date = date('Y-m-d H:i:s'); $detail_suivi->error_code = (string)$retour->errorCode; $detail_suivi->event_code = null; $detail_suivi->event_site = null; $detail_suivi->event_date = null; $detail_suivi->event_libelle = (string)$retour->errorMessage; $detail_suivi->update(); } unset($order); unset($detail_suivi); } } } /** * @param string $tracking * @return SimpleXMLElement */ public function getColissimoTracking($tracking) { if (!isset($this->colissimosuivi_numcontrat) || empty($this->colissimosuivi_numcontrat) || !isset($this->colissimosuivi_pass) || empty($this->colissimosuivi_pass)) { die($this->l('Please finish module configuration before using it.')); } // on récupère le retour $url = 'https://www.coliposte.fr/tracking-chargeur-cxf/TrackingServiceWS/track?accountNumber='.$this->colissimosuivi_numcontrat.'&password='.$this->colissimosuivi_pass.'&skybillNumber='.$tracking; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); curl_close($ch); $res = new SimpleXMLElement($res); $array = $res->xpath("//*[name()='return']"); return $array[0]; } /** * @param array $params */ public function hookActionAdminOrdersTrackingNumberUpdate($params) { $order = $params['order']; if (Tools::version_compare(_PS_VERSION_, '1.5.5.0', '>=')) { $order_carrier = new OrderCarrier($order->getIdOrderCarrier()); } else { $order_carrier = new OrderCarrier( Db::getInstance()->getValue(' SELECT `id_order_carrier` FROM `'._DB_PREFIX_.'order_carrier` WHERE `id_order` = '.(int)$order->id ) ); } if (!Validate::isLoadedObject($order_carrier)) { throw new PrestaShopException('Can\'t load Order Carrier object.'); } $tracking = $order_carrier->tracking_number; if (!empty($tracking) && Tools::strlen($tracking) == 13 && $this->isColissimoCarrier($order->id_carrier)) { $id = ColissimoSuiviModel::getIdByOrderId($order->id); if (!$id) { $detail_suivi = new ColissimoSuiviModel(); $detail_suivi->id_order = $order->id; $detail_suivi->tracking = $tracking; $detail_suivi->add(); } else { $detail_suivi = new ColissimoSuiviModel($id); $detail_suivi->tracking = $tracking; $detail_suivi->event_code = null; $detail_suivi->event_site = null; $detail_suivi->event_date = date('Y-m-d H:i:s'); $detail_suivi->event_libelle = null; $detail_suivi->update(); } $this->updateOrdersStatus( array( array( 'id_colissimo_suivi' => $detail_suivi->id, ), ) ); } } /** * @param int $order_carrier * @return bool */ public function isColissimoCarrier($order_carrier) { if (is_array($this->colissimosuivi_carriers)) { foreach ($this->colissimosuivi_carriers as $id_carrier) { if ((int)$id_carrier['id_carrier'] == $order_carrier) { return true; } } } return false; } public function updateColissimoCarriers() { $id_list = Db::getInstance()->executeS(' SELECT c.`id_carrier` FROM `'._DB_PREFIX_.'carrier` c WHERE c.`id_reference` IN ('.implode(', ', array_map('intval', Tools::unSerialize(Configuration::get('COLISSIMOSUIVI_MAPPING')))).')' ); Configuration::updateValue('COLISSIMOSUIVI_CARRIERS', serialize($id_list)); $this->colissimosuivi_mapping = $id_list; } /** * @param array $params */ public function hookActionCarrierUpdate($params) { $this->updateColissimoCarriers(); } public function fillOrdersData() { $date = new DateTime(); $date->sub(new DateInterval('P1M')); $id_list = Db::getInstance()->executeS(' SELECT o.`id_order` FROM `'._DB_PREFIX_.'orders` o WHERE o.`date_add` > \''.$date->format('Y-m-d H:i:s').'\'' ); foreach ($id_list as $id_order) { $order = new Order($id_order['id_order']); if (Tools::version_compare(_PS_VERSION_, '1.5.5.0', '>=')) { $order_carrier = new OrderCarrier($order->getIdOrderCarrier()); } else { $order_carrier = new OrderCarrier( Db::getInstance()->getValue(' SELECT `id_order_carrier` FROM `'._DB_PREFIX_.'order_carrier` WHERE `id_order` = '.(int)$order->id ) ); } if (!Validate::isLoadedObject($order_carrier)) { throw new PrestaShopException('Can\'t load Order Carrier object.'); } if (!ColissimoSuiviModel::getIdByOrderId($order->id) && (!empty($order_carrier->tracking_number)) && Tools::strlen($order_carrier->tracking_number) == 13 && $this->isColissimoCarrier($order->id_carrier)) { $detail_suivi = new ColissimoSuiviModel(); $detail_suivi->id_order = $order->id; $detail_suivi->tracking = $order_carrier->tracking_number; $detail_suivi->add(); } } } /** * @param array $params * @return string */ public function hookDisplayOrderDetail($params) { if ($this->isColissimoCarrier($params['order']->id_carrier)) { if (ColissimoSuiviModel::getIdByOrderId($params['order']->id)) { $details = new ColissimoSuiviModel(ColissimoSuiviModel::getIdByOrderId($params['order']->id)); $this->context->smarty->assign( array( 'colissimosuivi_img' => $this->url.'views/img/', 'details' => $details, ) ); return $this->display(__FILE__, 'order-detail.tpl'); } } } /** * @param array $params * @return string */ public function hookDisplayAdminOrderTabShip($params) { if ($this->isColissimoCarrier($params['order']->id_carrier)) { return $this->display(__FILE__, 'order-shipping-tab.tpl'); } } /** * @param array $params * @return string */ public function hookDisplayAdminOrderContentShip($params) { if (Tools::version_compare(_PS_VERSION_, '1.6.0.9', '<')) { $order = new Order($params['id_order']); } else { $order = $params['order']; } if ($this->isColissimoCarrier($order->id_carrier)) { $details = new ColissimoSuiviModel(ColissimoSuiviModel::getIdByOrderId($order->id)); if (Validate::isLoadedObject($details)) { $this->context->smarty->assign( array( 'etat' => true, 'colissimosuivi_img' => $this->url.'views/img/', 'details' => $details, ) ); } else { $this->context->smarty->assign( array( 'etat' => false, 'colissimosuivi_img' => $this->url.'views/img/', ) ); } if (Tools::version_compare(_PS_VERSION_, '1.6', '<')) { return $this->display(__FILE__, 'admin-order-15.tpl'); } elseif (Tools::version_compare(_PS_VERSION_, '1.6.0.9', '<')) { return $this->display(__FILE__, 'admin-order-16.tpl'); } else { return $this->display(__FILE__, 'order-shipping-content.tpl'); } } } /** * @param array $params * @return string */ public function hookAdminOrder($params) { return $this->hookDisplayAdminOrderContentShip($params); } /** * @return string */ public function getContent() { $message = ''; $this->smarty->assign(array( 'url_synchro' => $this->url.$this->name.'.cron.php?token='.Tools::substr(Tools::encrypt($this->name.'/index'), 0, 10), )); if (Tools::isSubmit('submit'.$this->name)) { Configuration::updateValue('COLISSIMOSUIVI_UPDSTATES', Tools::getValue('COLISSIMOSUIVI_UPDSTATES')); Configuration::updateValue('COLISSIMOSUIVI_NUMCONTRAT', Tools::getValue('COLISSIMOSUIVI_NUMCONTRAT')); Configuration::updateValue('COLISSIMOSUIVI_PASS', Tools::getValue('COLISSIMOSUIVI_PASS')); Configuration::updateValue('COLISSIMOSUIVI_MAPPING', serialize(Tools::getValue(version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'COLISSIMOSUIVI_MAPPING' : 'COLISSIMOSUIVI_MAPPING_selected'))); Configuration::updateValue('COLISSIMOSUIVI_FIND_TRACKING', Tools::getValue('COLISSIMOSUIVI_FIND_TRACKING')); if (is_array(Tools::unSerialize(Configuration::get('COLISSIMOSUIVI_MAPPING')))) { $this->updateColissimoCarriers(); } if (Tools::getValue('COLISSIMOSUIVI_INSTALLED') == 1 && is_array(Tools::unSerialize(Configuration::get('COLISSIMOSUIVI_MAPPING')))) { $this->fillOrdersData(); $this->updateOrdersStatus(); } $message = $this->displayConfirmation($this->l('The configuration has been updated.')); } return $message.$this->display(__FILE__, 'infos.tpl').$this->displayForm(); } /** * @return string */ public function displayForm() { $default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Module configuration'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => Tools::version_compare(_PS_VERSION_, '1.6.0.10', '<=') ? 'select' : 'swap', 'label' => $this->l('Carriers mapping'), 'name' => Tools::version_compare(_PS_VERSION_, '1.6.0.10', '<=') ? 'COLISSIMOSUIVI_MAPPING[]' : 'COLISSIMOSUIVI_MAPPING', 'required' => false, 'multiple' => true, 'options' => array( 'query' => Carrier::getCarriers($this->context->language->id, false, false, false, null, ALL_CARRIERS), 'id' => 'id_reference', 'name' => 'name' ), 'hint' => Tools::version_compare(_PS_VERSION_, '1.6.0.10', '<=') ? : array( $this->l('Associated carriers.'), $this->l('You can choose here carriers for which you can track shipments for an order.'), $this->l('If you don\'t select any carriers, you can\'t track any carriers.'), ), 'desc' => $this->l('If you don\'t select any carriers, you can\'t track any carriers in Carriers -> Colissimo • Tracking'), ), array( 'type' => Tools::version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch', 'label' => $this->l('Automatically update order status'), 'name' => 'COLISSIMOSUIVI_UPDSTATES', 'is_bool' => true, 'class' => 't', 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ) ), array( 'type' => Tools::version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch', 'label' => $this->l('Force re-add old orders'), 'name' => 'COLISSIMOSUIVI_INSTALLED', 'is_bool' => true, 'class' => 't', 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), 'desc' => $this->l('This option re-add old orders shipped by Colissimo to Carriers -> Colissimo • Tracking. This function can take some time to process.'), ), array( 'type' => 'text', 'label' => $this->l('Colissimo Flexibilité login'), 'name' => 'COLISSIMOSUIVI_NUMCONTRAT', 'class' => 'fixed-width-md', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Colissimo Flexibilité password'), 'name' => 'COLISSIMOSUIVI_PASS', 'class' => 'fixed-width-md', 'required' => true ), array( 'type' => version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch', 'label' => $this->l('Rechercher les suivis manquants à la synchronisation'), 'name' => 'COLISSIMOSUIVI_FIND_TRACKING', 'is_bool' => true, 'class' => 't', 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), 'desc' => $this->l('Activer cette option si le module qui insère les numéros de suivi dans vos commande ne respecte pas les standards de Prestashop.'), ), ), 'submit' => array( 'title' => $this->l('Save'), ) ); $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name; $helper->default_form_language = $default_lang; $helper->allow_employee_form_lang = $default_lang; $helper->title = $this->displayName; $helper->show_toolbar = true; $helper->toolbar_scroll = true; $helper->submit_action = 'submit' . $this->name; $helper->toolbar_btn = array( 'save' => array( 'desc' => $this->l('Save'), 'href' => AdminController::$currentIndex . '&configure=' . $this->name . '&save' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules'), ), 'back' => array( 'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'), 'desc' => $this->l('Back to list') ) ); $helper->fields_value['COLISSIMOSUIVI_UPDSTATES'] = Configuration::get('COLISSIMOSUIVI_UPDSTATES'); $helper->fields_value['COLISSIMOSUIVI_INSTALLED'] = Configuration::get('COLISSIMOSUIVI_INSTALLED'); $helper->fields_value['COLISSIMOSUIVI_NUMCONTRAT'] = Configuration::get('COLISSIMOSUIVI_NUMCONTRAT'); $helper->fields_value['COLISSIMOSUIVI_PASS'] = Configuration::get('COLISSIMOSUIVI_PASS'); $helper->fields_value[version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'COLISSIMOSUIVI_MAPPING[]' : 'COLISSIMOSUIVI_MAPPING'] = is_array(Tools::unSerialize(Configuration::get('COLISSIMOSUIVI_MAPPING'))) ? Tools::unSerialize(Configuration::get('COLISSIMOSUIVI_MAPPING')) : array(); $helper->fields_value['COLISSIMOSUIVI_FIND_TRACKING'] = Configuration::get('COLISSIMOSUIVI_FIND_TRACKING'); return $helper->generateForm($fields_form); } }