* @copyright 2008-today Mediacom87 * @license define in the module */ if (!defined('_TB_VERSION_') && !defined('_PS_VERSION_')) { exit; } include_once dirname(__FILE__).'/class/mediacom87.php'; class MedTntTrackingParcel extends Module { public $smarty; public $context; public $controller; public $_path; private $errors = array(); protected $config_form = false; public function __construct() { $this->name = 'medtnttrackingparcel'; $this->tab = 'shipping_logistics'; $this->version = '1.3.0'; $this->author = 'Mediacom87'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.7.99.99'); $this->module_key = 'fc130d6b23ab25ae03fe254d78dd9c70'; $this->addons_id = '22328'; // 22328 $this->author_address = '0x58fc3c5faebac44d590fa20ca7757cc802ad2708'; parent::__construct(); $this->displayName = $this->l('Track TNT parcels'); $this->description = $this->l('Automatically track your parcel TNT to update their delivery status.'); /* boostrap */ if (version_compare(_PS_VERSION_, '1.6.0.0', '>=')) { $this->bootstrap = true; } else { $this->bootstrap = false; } $this->mediacom87 = new MedTntTrackingParcelClass($this); $this->conf = unserialize(Configuration::get($this->name)); $this->tpl_path = _PS_ROOT_DIR_.'/modules/'.$this->name; $this->cron_url = Tools::getShopDomainSsl(true) .$this->context->shop->getBaseURI() .'modules/' .$this->name .'/cron.php?sk=' .$this->conf['SECURITY']; } public function install() { if (!parent::install() || !$this->defaultConf()) { return false; } return true; } public function defaultConf() { $conf = array(); $conf['SECURITY'] = Tools::encrypt(Tools::getShopDomainSsl().time()); $conf['DELIVERY_BEFORE'] = 4; $conf['DELIVERY_STATE'] = 5; $conf['DELIVERY_RELAY'] = 5; $conf['DELIVERY_RELAY_FLAG'] = true; $conf['DELIVERY_ABS'] = 5; $conf['DELIVERY_INFO'] = false; $conf['DELIVERY_PROOF'] = false; $conf['NB_ORDERS'] = 20; if (!Configuration::updateValue($this->name, serialize($conf))) { return false; } return true; } public function uninstall() { if (!parent::uninstall() || !Configuration::deleteByName($this->name)) { return false; } return true; } public function getContent($tab = 'AdminModules') { $this->errors = array(); $output = ''; $languages = Language::getLanguages(); $cron_task = _PS_MODULE_DIR_.$this->name.'/cron.php?sk='.$this->conf['SECURITY']; if (Tools::isSubmit('saveconf')) { $this->postProcess(); if (!count($this->errors)) { $output .= $this->displayConfirmation($this->l('Settings updated')); } } if (isset($this->addons_id) && $this->addons_id) { $json_rates = $this->mediacom87->medJsonModuleRate($this->addons_id, $this->module_key); } else { $json_rates = false; } if (isset($this->module_key) && $this->module_key) { $json_modules = $this->mediacom87->medJsonModuleFile(); } else { $json_modules = null; } $form_url = AdminController::$currentIndex .'&configure='.$this->name .'&token='.Tools::getAdminToken( $tab .(int)Tab::getIdFromClassName($tab) .(int)$this->context->cookie->id_employee ); $this->context->smarty->assign(array( 'form_url' => $form_url, 'addons_id' => $this->addons_id, 'tpl_path' => $this->tpl_path, 'img_path' => $this->_path.'views/img/', 'languages' => $languages, 'path' => $this->_path, 'languages' => Language::getLanguages(), 'description' => $this->description, 'author' => $this->author, 'name' => $this->name, 'version' => $this->version, 'ps_version' => _PS_VERSION_, 'iso_code' => $this->mediacom87->isoCode(), 'iso_domain' => $this->mediacom87->isoCode(true), 'id_active_lang' => (int)$this->context->language->id, 'json_modules' => $json_modules, 'json_rates' => $json_rates, 'order_states' => OrderState::getOrderStates((int)$this->context->language->id), 'url_manager' => Context::getContext()->link->getAdminLink('AdminStatuses'), 'cron_url' => $this->cron_url, 'cron_task' => $cron_task, 'max_execution_time' => ini_get('max_execution_time'), 'config' => $this->conf )); if (count($this->errors)) { $output .= $this->displayError(implode('
', $this->errors)); } $this->context->controller->addJS($this->_path.'libraries/js/riotcompiler.min.js'); $this->context->controller->addJS($this->_path.'libraries/js/pageloader.js'); $this->context->controller->addCSS($this->_path.'views/css/back.css'); $output .= $this->display(__FILE__, 'views/templates/admin/admin.tpl'); $output .= $this->display(__FILE__, 'libraries/prestui/ps-tags.tpl'); return $output; } private function postProcess() { $conf = array(); $conf['SECURITY'] = $this->conf['SECURITY']; $this->conf['DELIVERY_BEFORE'] = (int)Tools::getValue('DELIVERY_BEFORE'); $this->conf['DELIVERY_STATE'] = (int)Tools::getValue('DELIVERY_STATE'); $this->conf['DELIVERY_RELAY'] = (int)Tools::getValue('DELIVERY_RELAY'); $this->conf['DELIVERY_RELAY_FLAG'] = (bool)Tools::getValue('DELIVERY_RELAY_FLAG'); $this->conf['DELIVERY_ABS'] = (int)Tools::getValue('DELIVERY_ABS'); $this->conf['DELIVERY_INFO'] = (bool)Tools::getValue('DELIVERY_INFO'); $this->conf['DELIVERY_PROOF'] = (bool)Tools::getValue('DELIVERY_PROOF'); $this->conf['NB_ORDERS'] = (int)Tools::getValue('NB_ORDERS'); if (Configuration::updateValue($this->name, serialize($this->conf))) { if ($this->conf['DELIVERY_PROOF']) { if (!$this->mediacom87->medTestDir(_PS_MODULE_DIR_.$this->name.'/proof/')) { $this->errors[] = $this->l('Give permission on this directory to save proof of delivery:')._PS_MODULE_DIR_.$this->name.'/proof/'; } } return true; } else { $this->errors[] = $this->l('Error during saving settings'); } return false; } public function trackParcels() { if (Validate::isLoadedObject($this) && $this->active) { $orders = $this->mediacom87->getOrderIdsByStatus($this->conf['DELIVERY_BEFORE']); if ($this->conf['DELIVERY_RELAY'] != $this->conf['DELIVERY_STATE']) { $orders_relay = $this->mediacom87->getOrderIdsByStatus($this->conf['DELIVERY_RELAY']); $orders = array_merge($orders, $orders_relay); } $tpl = array(); $compteur = 0; $filename = _PS_MODULE_DIR_.$this->name.'/datas'; foreach ($orders as $o) { $history_modified = false; $order_carrier = new OrderCarrier($this->mediacom87->getIdOrderCarrier($o)); $shipping_number = $order_carrier->tracking_number; if (!isset($shipping_number) && !$shipping_number) { $order = new Order($o); $shipping_number = $order->shipping_number; } if (Tools::strlen(trim($shipping_number)) == 16) { $parcel = $this->mediacom87->trackingByConsignments($shipping_number); if (is_object($parcel) && !is_null($parcel)) { if (property_exists($parcel->events, 'deliveryDate') && $parcel->events->deliveryDate != '') { if (property_exists($parcel->dropOffPoint, 'deliveryFlag') && $parcel->dropOffPoint->deliveryFlag != '') { if ($this->conf['DELIVERY_RELAY'] != $this->conf['DELIVERY_STATE']) { if ($parcel->dropOffPoint->deliveryFlag == 1) { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_STATE']); $history_modified = true; } else { if (!isset($order) || !is_object($order)) { $order = new Order($o); } if ($order->current_state != $this->conf['DELIVERY_RELAY']) { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_RELAY']); $history_modified = true; } } } else { if ($this->conf['DELIVERY_RELAY_FLAG']) { if ($parcel->dropOffPoint->deliveryFlag == 1) { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_STATE']); $history_modified = true; } } else { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_RELAY']); $history_modified = true; } } } else { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_STATE']); $history_modified = true; } } } else { $this->mediacom87->changeIOrderStatus($o, $this->conf['DELIVERY_ABS']); $this->mediacom87->addDeliveryInformations($o, $this->l('This package does not exist or is not attached to this account TNT Express')); } } if ($history_modified) { $compteur++; $long_status = ''; $primaryPODUrl = ''; if (property_exists($parcel, 'longStatus')) { $nb = count($parcel->longStatus); if ($nb >= 1) { $long_status = implode(' / ', $parcel->longStatus); } elseif ($nb == 1) { $long_status = $parcel->longStatus; } if ($this->conf['DELIVERY_INFO'] && isset($long_status) && !Tools::isEmpty($long_status)) { $this->mediacom87->addDeliveryInformations($o, $long_status); } } if (property_exists($parcel, 'primaryPODUrl')) { $primaryPODUrl = $parcel->primaryPODUrl; if (isset($primaryPODUrl) && !Tools::isEmpty($primaryPODUrl)) { if ($this->conf['DELIVERY_PROOF']) { $name = $o.'-'.$shipping_number.'.jpg'; if (copy($primaryPODUrl, _PS_MODULE_DIR_.$this->name.'/proof/'.$name)) { $url_file = Tools::getShopDomainSsl(true).$this->context->shop->getBaseURI().'modules/'.$this->name.'/proof/'.$name; $this->mediacom87->addDeliveryInformations($o, $url_file); } else { $this->mediacom87->addDeliveryInformations($o, $primaryPODUrl); } } else { $this->mediacom87->addDeliveryInformations($o, $primaryPODUrl); } } } $tpl[] = array( 'id_order' => $o, 'long_status' => $long_status, 'primaryPODUrl' => $primaryPODUrl ); } if ($this->conf['NB_ORDERS'] && $compteur >= $this->conf['NB_ORDERS']) { if ($content = Tools::file_get_contents('datas')) { $datas = unserialize($content); $tpl = array_merge($datas, $tpl); } file_put_contents('datas', serialize($tpl)); Tools::redirectAdmin($this->cron_url.'&f=datas'); exit; } } if ($content = Tools::file_get_contents('datas')) { $datas = unserialize($content); if (count($tpl)) { $tpl = array_merge($datas, $tpl); } else { $tpl = $datas; } unlink($filename); } $this->context->smarty->assign('datas', $tpl); return $this->context->smarty->fetch($this->tpl_path.'/views/templates/front/cron.tpl'); } } }