Files
bio-concept-pharma/web/modules/csuivitracking/csuivitracking.php
2019-11-17 19:14:07 +01:00

484 lines
19 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license.
* Use, copy, modification or distribution of this source file without written
* license agreement is strictly forbidden.
* In order to obtain a license, please contact us: simon@daig.re
* ...........................................................................
* INFORMATION SUR LA LICENCE D'UTILISATION
*
* L'utilisation de ce fichier source est soumise a une licence commerciale.
* Toute utilisation, reproduction, modification ou distribution du present
* fichier source sans contrat de licence ecrit est expressement interdite.
* Pour obtenir une licence, veuillez nous contacter a l'adresse: simon@daig.re
*
* @package Courrier suivi Tracking
* @author Agencya
* @copyright Copyright(c) 2015-2018
* @license See Readme.md
* Contact by Email : simon@daig.re
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_.'csuivitracking/models/CsuiviTrackingModel.php';
require_once _PS_MODULE_DIR_.'csuivitracking/classes/CsuiviTracking_Tools.php';
/**
* Class CsuiviTracking
*/
class CsuiviTracking extends Module
{
/** @var string */
public $url;
/**
* CsuiviTracking constructor.
*/
public function __construct()
{
$this->name = 'csuivitracking';
$this->tab = 'shipping_logistics';
$this->version = '2.2.0';
$this->author = 'Agencya';
$this->module_key = '77b6c6ba7a32d1548217598f131e428d';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('La Poste • Track my shipments');
$this->description = $this->l('Easily track your orders shipped by La Poste for you and your customers.');
$this->confirmUninstall = $this->l('Are you sure to uninstall this module ?');
$this->ps_versions_compliancy = array('min' => '1.5.0.0', 'max' => _PS_VERSION_);
$this->url = $this->context->shop->getBaseURL(true).'modules/'.$this->name.'/';
// translations
// $this->l('API key missing. Please check module configuration.');
}
/**
* @return bool
*/
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
$tab = new Tab();
$tab->class_name = 'AdminCsuiviTracking';
foreach (Language::getLanguages(true) as $lang) {
$tab->name[(int)$lang['id_lang']] = $this->l('La Poste • Tracking');
}
$tab->id_parent = (int)Tab::getIdFromClassName('AdminParentShipping');
$tab->module = $this->name;
$tab->add();
Configuration::updateValue('CSUIVITRACKING_API_KEY', '');
Configuration::updateValue('CSUIVITRACKING_UPDSTATES', true);
Configuration::updateValue('CSUIVITRACKING_MAPPING', '');
Configuration::updateValue('CSUIVITRACKING_CARRIERS', '');
Configuration::updateValue('CSUIVITRACKING_FORCE', false);
Configuration::updateValue('CSUIVITRACKING_FIND_TRACKING', false);
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_.'csuivi_tracking` (
`id_csuivi_tracking` INT(10) NOT NULL AUTO_INCREMENT,
`id_order` INT(10) NOT NULL,
`tracking` VARCHAR(13) NOT NULL,
`status` TEXT NOT NULL DEFAULT \'\',
`delivered` BOOLEAN DEFAULT FALSE NULL,
`date` DATE NOT NULL,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY ( `id_csuivi_tracking` )
) 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('AdminCsuiviTracking'));
$tab->delete();
Configuration::deleteByName('CSUIVITRACKING_API_KEY');
Configuration::deleteByName('CSUIVITRACKING_UPDSTATES');
Configuration::deleteByName('CSUIVITRACKING_MAPPING');
Configuration::deleteByName('CSUIVITRACKING_CARRIERS');
Configuration::deleteByName('CSUIVITRACKING_FORCE');
Configuration::deleteByName('CSUIVITRACKING_FIND_TRACKING');
Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'csuivi_tracking');
return parent::uninstall();
}
/**
* @param array $params
*/
public function hookDisplayBackOfficeHeader($params)
{
if (Tools::strtolower((string)Tools::getValue('controller')) === 'admincsuivitracking') {
$this->context->controller->addJquery();
$this->context->controller->addJS($this->_path.'views/js/csuivi_session.js');
}
}
/**
* @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 = trim($order_carrier->tracking_number);
if (!empty($tracking) && Tools::strlen($tracking) == 13 && CsuiviTracking_Tools::isCsuiviCarrier($order->id_carrier)) {
$id_csuivi_tracking = CsuiviTrackingModel::getIdByOrderId($order->id);
if ($id_csuivi_tracking) {
$detail_suivi = new CsuiviTrackingModel($id_csuivi_tracking);
$detail_suivi->tracking = $tracking;
$detail_suivi->date = date('Y-m-d');
$detail_suivi->update();
} else {
$detail_suivi = new CsuiviTrackingModel();
$detail_suivi->id_order = $order->id;
$detail_suivi->tracking = $tracking;
$detail_suivi->date = date('Y-m-d');
$detail_suivi->add();
}
}
}
/**
* @param array $params
*/
public function hookActionCarrierUpdate($params)
{
CsuiviTracking_Tools::updateCsuiviCarriers();
}
/**
* @param array $params
* @return string
*/
public function hookDisplayOrderDetail($params)
{
if (Tools::version_compare(_PS_VERSION_, '1.6.0.9', '<')) {
$order = new Order($params['id_order']);
} else {
$order = new Order($params['order']->id);
}
if (CsuiviTracking_Tools::isCsuiviCarrier($order->id_carrier) && CsuiviTrackingModel::getIdByOrderId($order->id)) {
$details = new CsuiviTrackingModel(CsuiviTrackingModel::getIdByOrderId($order->id));
if (Validate::isLoadedObject($details)) {
$this->context->smarty->assign(
array(
'etat' => true,
'csuivitracking_img' => $this->url . 'views/img/',
'details' => $details,
)
);
} else {
$this->context->smarty->assign(
array(
'etat' => true,
'csuivitracking_img' => $this->url . 'views/img/',
)
);
}
return $this->display(__FILE__, 'order-detail.tpl');
}
}
/**
* @param array $params
* @return string
*/
public function hookDisplayAdminOrderTabShip($params)
{
if (CsuiviTracking_Tools::isCsuiviCarrier($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 = new Order($params['order']->id);
}
if (CsuiviTracking_Tools::isCsuiviCarrier($order->id_carrier)) {
$details = new CsuiviTrackingModel(CsuiviTrackingModel::getIdByOrderId($order->id));
if (Validate::isLoadedObject($details)) {
$this->context->smarty->assign(
array(
'etat' => true,
'details' => $details,
)
);
} else {
$this->context->smarty->assign(
array(
'etat' => false,
'csuivitracking_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 = '';
$id_shop = '';
if (Shop::isFeatureActive() && Shop::getContext() !== Shop::CONTEXT_ALL) {
$id_shop = '&id_shop='.$this->context->shop->id;
}
$this->smarty->assign(
array(
'url_cron' => $this->url.$this->name.'.cron.php?token='.Tools::substr(Tools::encrypt($this->name.'/index'), 0, 10).$id_shop,
)
);
if (Tools::isSubmit('submit'.$this->name)) {
Configuration::updateValue('CSUIVITRACKING_API_KEY', Tools::getValue('CSUIVITRACKING_API_KEY'));
Configuration::updateValue('CSUIVITRACKING_UPDSTATES', Tools::getValue('CSUIVITRACKING_UPDSTATES'));
Configuration::updateValue('CSUIVITRACKING_MAPPING', serialize(Tools::getValue(version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'CSUIVITRACKING_MAPPING' : 'CSUIVITRACKING_MAPPING_selected')));
Configuration::updateValue('CSUIVITRACKING_FIND_TRACKING', Tools::getValue('CSUIVITRACKING_FIND_TRACKING'));
if (is_array(Tools::unSerialize(Configuration::get('CSUIVITRACKING_MAPPING')))) {
CsuiviTracking_Tools::updateCsuiviCarriers();
}
$message = $this->displayConfirmation($this->l('The configuration has been updated.'));
}
if ((bool)Tools::getValue('CSUIVITRACKING_FORCE') && is_array(Tools::unSerialize(Configuration::get('CSUIVITRACKING_MAPPING')))) {
CsuiviTracking_Tools::fillOrdersData();
CsuiviTracking_Tools::updateOrdersStatus($this->context->shop->id);
}
return $message.$this->display(__FILE__, 'infos.tpl').$this->displayForm();
}
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' => 'text',
'label' => $this->l('API key'),
'required' => true,
'name' => 'CSUIVITRACKING_API_KEY',
'desc' => $this->l('Please read documentation to learn how obtain your free API key.'),
),
array(
'type' => version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'select' : 'swap',
'label' => $this->l('Carriers mapping'),
'name' => version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'CSUIVITRACKING_MAPPING[]' : 'CSUIVITRACKING_MAPPING',
'multiple' => true,
'options' => array(
'query' => Carrier::getCarriers($this->context->language->id, false, false, false, null, ALL_CARRIERS),
'id' => 'id_reference',
'name' => 'name'
),
'hint' => 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 -> La Poste • Tracking.'),
),
array(
'type' => version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch',
'label' => $this->l('Automatically update order status'),
'name' => 'CSUIVITRACKING_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' => version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch',
'label' => $this->l('Force re-add old orders'),
'name' => 'CSUIVITRACKING_FORCE',
'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 La Poste to Carriers -> La Poste • Tracking. This function can take some time to process.'),
),
array(
'type' => version_compare(_PS_VERSION_, '1.6', '<') ? 'radio' : 'switch',
'label' => $this->l('Rechercher les suivis manquants à la synchronisation'),
'name' => 'CSUIVITRACKING_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['CSUIVITRACKING_UPDSTATES'] = Configuration::get('CSUIVITRACKING_UPDSTATES');
$helper->fields_value['CSUIVITRACKING_API_KEY'] = Configuration::get('CSUIVITRACKING_API_KEY');
$helper->fields_value[version_compare(_PS_VERSION_, '1.6.0.10', '<') ? 'CSUIVITRACKING_MAPPING[]' : 'CSUIVITRACKING_MAPPING'] = is_array(Tools::unSerialize(Configuration::get('CSUIVITRACKING_MAPPING'))) ? Tools::unSerialize(Configuration::get('CSUIVITRACKING_MAPPING')) : array();
$helper->fields_value['CSUIVITRACKING_FORCE'] = Configuration::get('CSUIVITRACKING_FORCE');
$helper->fields_value['CSUIVITRACKING_FIND_TRACKING'] = Configuration::get('CSUIVITRACKING_FIND_TRACKING');
return $helper->generateForm($fields_form);
}
}