* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 10545 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; require_once(_PS_MODULE_DIR_.'mondialrelay/classes/MondialRelayClass.php'); require_once(_PS_MODULE_DIR_.'mondialrelay/classes/MRTools.php'); class MondialRelay extends Module { const INSTALL_SQL_FILE = 'mrInstall.sql'; private $_postErrors; public static $modulePath = ''; public static $moduleURL = ''; static public $MRFrontToken = ''; static public $MRBackToken = ''; // Added for 1.3 compatibility const ONLY_PRODUCTS = 1; const ONLY_DISCOUNTS = 2; const BOTH = 3; const BOTH_WITHOUT_SHIPPING = 4; const ONLY_SHIPPING = 5; const ONLY_WRAPPING = 6; const ONLY_PRODUCTS_WITHOUT_SHIPPING = 7; // SQL FILTER ORDER const NO_FILTER = 0; const WITHOUT_HOME_DELIVERY = 1; public function __construct() { $this->name = 'mondialrelay'; $this->tab = 'shipping_logistics'; $this->version = '1.7.9'; parent::__construct(); $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('Mondial Relay'); $this->description = $this->l('Deliver in Relay points'); self::initModuleAccess(); // Call everytime to prevent the changement of the module by a recent one $this->_updateProcess(); } public function install() { global $cookie; $name = "shipping"; $title = "Mondial Relay API"; if (!parent::install()) return false; Db::getInstance()->ExecuteS( 'SELECT `name` FROM `' . _DB_PREFIX_ . 'hook` WHERE `name` = \''.$name.'\' AND `title` = \''.$title.'\''); if (!Db::getInstance()->NumRows()) Db::getInstance()->Execute('INSERT INTO ' . _DB_PREFIX_ . 'hook (name, title, description, position) VALUES(\''.$name.'\', \''.$title.'\', NULL, 0)'); if (!$this->registerHookByVersion()) return false; if ((!file_exists(self::$modulePath.self::INSTALL_SQL_FILE)) || (!$sql = file_get_contents(self::$modulePath.self::INSTALL_SQL_FILE))) return false; $sql = str_replace('PREFIX_', _DB_PREFIX_, $sql); $sql = preg_split("/;\s*[\r\n]+/", $sql); foreach($sql AS $k => $query) if (!empty($query)) Db::getInstance()->Execute(trim($query)); $result = Db::getInstance()->getRow(' SELECT id_tab FROM `' . _DB_PREFIX_ . 'tab` WHERE class_name="AdminMondialRelay"'); if (!$result) { // AdminOrders id_tab $id_parent = 3; /*tab install */ $result = Db::getInstance()->getRow(' SELECT position FROM `' . _DB_PREFIX_ . 'tab` WHERE `id_parent` = '.(int)$id_parent.' ORDER BY `'. _DB_PREFIX_ .'tab`.`position` DESC'); $pos = (isset($result['position'])) ? $result['position'] + 1 : 0; Db::getInstance()->Execute(' INSERT INTO ' . _DB_PREFIX_ . 'tab (id_parent, class_name, position, module) VALUES('.(int)$id_parent.', "AdminMondialRelay", "'.(int)($pos).'", "mondialrelay")'); $id_tab = Db::getInstance()->Insert_ID(); $languages = Language::getLanguages(); foreach ($languages as $language) Db::getInstance()->Execute(' INSERT INTO ' . _DB_PREFIX_ . 'tab_lang (id_lang, id_tab, name) VALUES("'.(int)($language['id_lang']).'", "'.(int)($id_tab).'", "Mondial Relay")'); $profiles = Profile::getProfiles(Configuration::get('PS_LANG_DEFAULT')); foreach ($profiles as $profile) Db::getInstance()->Execute(' INSERT INTO ' . _DB_PREFIX_ . 'access (`id_profile`,`id_tab`,`view`,`add`,`edit`,`delete`) VALUES('.$profile['id_profile'].', '.(int)($id_tab).', 1, 1, 1, 1)'); @copy(_PS_MODULE_DIR_.'mondialrelay/AdminMondialRelay.gif', _PS_IMG_DIR_.'/AdminMondialRelay.gif'); } // If module isn't installed, set default value if (!Configuration::get('MONDIAL_RELAY')) { Configuration::updateValue('MONDIAL_RELAY', $this->version); Configuration::updateValue('MONDIAL_RELAY_ORDER_STATE', 3); Configuration::updateValue('MONDIAL_RELAY_SECURE_KEY', md5(time().rand(0,10))); Configuration::updateValue('MR_GOOGLE_MAP', '1'); Configuration::updateValue('MR_ENSEIGNE_WEBSERVICE', ''); Configuration::updateValue('MR_CODE_MARQUE', ''); Configuration::updateValue('MR_KEY_WEBSERVICE', ''); Configuration::updateValue('MR_LANGUAGE', ''); Configuration::updateValue('MR_WEIGHT_COEF', ''); } else { // Reactive transport if database wasn't remove at the last uninstall Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'carrier` c, `'._DB_PREFIX_.'mr_method` m SET `deleted` = 0 WHERE c.id_carrier = m.id_carrier'); if (Configuration::get('MONDIAL_RELAY') < $this->version) ;// TODO : ADD upgrade process depending of the last and new version } return true; } /* ** Return the token depend of the type */ static public function getToken($type = 'front') { return ($type == 'front') ? self::$MRFrontToken : (($type == 'back') ? self::$MRBackToken : NULL); } /* ** Register hook depending of the Prestashop version used */ private function registerHookByVersion() { if (_PS_VERSION_ >= '1.3' && (!$this->registerHook('shipping') || !$this->registerHook('extraCarrier') || !$this->registerHook('updateCarrier') || !$this->registerHook('newOrder') || !$this->registerHook('BackOfficeHeader'))) return false; if (_PS_VERSION_ >= '1.4' && (!$this->registerHook('processCarrier') || !$this->registerHook('orderDetail') || !$this->registerHook('orderDetailDisplayed') || !$this->registerHook('paymentTop'))) return false; return true; } public function uninstallCommonData() { // Tab uninstall $result = Db::getInstance()->getRow(' SELECT id_tab FROM `' . _DB_PREFIX_ . 'tab` WHERE class_name="AdminMondialRelay"'); if ($result) { $id_tab = $result['id_tab']; if (isset($id_tab) && !empty($id_tab)) { Db::getInstance()->Execute('DELETE FROM ' . _DB_PREFIX_ . 'tab WHERE id_tab = '.(int)($id_tab)); Db::getInstance()->Execute('DELETE FROM ' . _DB_PREFIX_ . 'tab_lang WHERE id_tab = '.(int)($id_tab)); Db::getInstance()->Execute('DELETE FROM ' . _DB_PREFIX_ . 'access WHERE id_tab = '.(int)($id_tab)); } } if (_PS_VERSION_ >= '1.4' && !Db::getInstance()->Execute(' UPDATE '._DB_PREFIX_ .'carrier SET `active` = 0, `deleted` = 1 WHERE `external_module_name` = "mondialrelay"')) return false; else if (!Db::getInstance()->Execute(' UPDATE '._DB_PREFIX_ .'carrier SET `active` = 0, `deleted` = 1 WHERE `name` = "mondialrelay"')) return false; return true; } public function uninstall() { if (!parent::uninstall()) return false; // Uninstall data that doesn't need to be keep if (!$this->uninstallCommonData()) return false; if (Tools::getValue('keepDatabase')) { // Retro Compatibility for older Version than 1.7 if (Configuration::get('MONDIAL_RELAY_1_4')) { Configuration::updateValue('MONDIAL_RELAY', '1.6'); Configuration::deleteByName('MONDIAL_RELAY_1_4'); Configuration::deleteByName('MONDIAL_RELAY_INSTALL_UPDATE_1'); } return true; } // MondialRelay Configuration if (!Configuration::deleteByName('MONDIAL_RELAY') || !Configuration::deleteByName('MONDIAL_RELAY_INSTALL_UPDATE') || !Configuration::deleteByName('MONDIAL_RELAY_SECURE_KEY') || !Configuration::deleteByName('MONDIAL_RELAY_ORDER_STATE') || !Configuration::deleteByName('MR_GOOGLE_MAP') || !Configuration::deleteByName('MR_ENSEIGNE_WEBSERVICE') || !Configuration::deleteByName('MR_CODE_MARQUE') || !Configuration::deleteByName('MR_KEY_WEBSERVICE') || !Configuration::deleteByName('MR_WEIGHT_COEF')) return false; // Drop databases if (!Db::getInstance()->Execute(' DROP TABLE '._DB_PREFIX_ .'mr_historique, '._DB_PREFIX_ .'mr_method, '._DB_PREFIX_ .'mr_selected')) return false; elseif (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'carrier SET `active` = 0, `deleted` = 1 WHERE `name` = "mondialrelay"')) return false; return true; } /* ** UpdateProcess if merchant update the module without a ** normal installation */ private function _updateProcess() { if (Module::isInstalled('mondialrelay') && (($installedVersion = Configuration::get('MONDIAL_RELAY')) || $installedVersion = Configuration::get('MONDIAL_RELAY_1_4')) && $installedVersion < $this->version) { if ($installedVersion < '1.4') $this->_update_v1_4(); if ($installedVersion < '1.4.2') $this->_update_v1_4_2(); } // Process update done just try to update the new configuration value if (Configuration::get('MONDIAL_RELAY_1_4')) { Configuration::updateValue('MONDIAL_RELAY', $this->version); Configuration::deleteByName('MONDIAL_RELAY_1_4'); } } /* ** Use if the mechant was using Prestashop 1.3 and ** now use 1.4 or more recent */ private function _update_v1_4() { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'carrier` SET `shipping_external` = 0, `need_range` = 1, `external_module_name` = "mondialrelay", `shipping_method` = 1 WHERE `id_carrier` IN (SELECT `id_carrier` FROM `'._DB_PREFIX_.'mr_method`)'); } /* ** Add new Hook for the last recent version >= 1.4.2 */ private function _update_v1_4_2() { if (!$this->isRegisteredInHook('newOrder')) $this->registerHook('newOrder'); if (!$this->isRegisteredInHook('BackOfficeHeader')) $this->registerHook('BackOfficeHeader'); } /* ** Get the content to ask for a backup of the database */ private function askForBackup($href) { return 'targetButton = \''.$href.'\'; PS_MRGetUninstallDetail();'; } /* ** OnClick for input fields under the module list fields action */ public function onclickOption($type, $href = false) { $content = ''; switch($type) { case 'desactive': break; case 'reset': break; case 'delete': break; case 'uninstall': $content = $this->askForBackup($href); break; default: } return $content; } /* ** Init the access directory module for URL and file system ** Allow a compatibility for Presta < 1.4 */ public static function initModuleAccess() { self::$modulePath = _PS_MODULE_DIR_. 'mondialrelay/'; self::$MRFrontToken = sha1('mr'._COOKIE_KEY_.'Front'); self::$MRBackToken = sha1('mr'._COOKIE_KEY_.'Back'); $protocol = (Configuration::get('PS_SSL_ENABLED') || (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://'; $endURL = __PS_BASE_URI__.'modules/mondialrelay/'; if (method_exists('Tools', 'getShopDomainSsl')) self::$moduleURL = $protocol.Tools::getShopDomainSsl().$endURL; else self::$moduleURL = $protocol.$_SERVER['HTTP_HOST'].$endURL; } /* ** Override a jQuery version included by another one us. ** Allow a compatibility for Presta < 1.4 */ public static function getJqueryCompatibility($overloadCurrent = false) { // Store the last inclusion into a variable and include the new one if ($overloadCurrent) return ' '; return ' '; } public function hookNewOrder($params) { DB::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'mr_selected` SET `id_order` = '.(int)$params['order']->id.' WHERE `id_cart` = '.(int)$params['cart']->id); } public function hookBackOfficeHeader() { $cssFilePath = $this->_path.'style.css'; $jsFilePath = $this->_path.'mondialrelay.js'; $ret = ''; if (Tools::getValue('tab') == 'AdminMondialRelay') $ret .= self::getJqueryCompatibility(true); $ret .= ' '; return $ret; } private function _postValidation() { if (Tools::isSubmit('submitMR')) { if (Tools::getValue('mr_Enseigne_WebService') != '' AND !preg_match("#^[0-9A-Z]{2}[0-9A-Z ]{6}$#", Tools::getValue('mr_Enseigne_WebService'))) $this->_postErrors[] = $this->l('Invalid Shop'); if (Tools::getValue('mr_code_marque') != '' AND !preg_match("#^[0-9]{2}$#", Tools::getValue('mr_code_marque'))) $this->_postErrors[] = $this->l('Invalid Mark code'); if (Tools::getValue('mr_Key_WebService') != '' AND !preg_match("#^[0-9A-Za-z_\'., /\-]{2,32}$#", Tools::getValue('mr_Key_WebService'))) $this->_postErrors[] = $this->l('Invalid Webservice Key'); if (Tools::getValue('mr_Langage') != '' AND !preg_match("#^[A-Z]{2}$#", Tools::getValue('mr_Langage'))) $this->_postErrors[] = $this->l('Invalid Language'); if (!Tools::getValue('mr_weight_coef') OR !Validate::isInt(Tools::getValue('mr_weight_coef'))) $this->_postErrors[] = $this->l('Invalid Weight Coefficient'); } elseif (Tools::isSubmit('submitMethod')) { if (Configuration::get('MR_ENSEIGNE_WEBSERVICE') == '' OR Configuration::get('MR_CODE_MARQUE') == '' OR Configuration::get('MR_KEY_WEBSERVICE') == '' OR Configuration::get('MR_LANGUAGE') == '') $this->_postErrors[] = $this->l('Please configure your Mondial Relay account settings before creating a carrier.'); if (!preg_match("#^[0-9A-Za-z_\'., /\-]{2,32}$#", Tools::getValue('mr_Name'))) $this->_postErrors[] = $this->l('Invalid carrier name'); if (Tools::getValue('mr_ModeCol') != 'CCC') $this->_postErrors[] = $this->l('Invalid Col mode'); if (!preg_match("#^REL|24R|ESP|DRI|LDS|LDR|LD1$#", Tools::getValue('mr_ModeLiv'))) $this->_postErrors[] = $this->l('Invalid delivery mode'); if (!Validate::isInt(Tools::getValue('mr_ModeAss')) OR Tools::getValue('mr_ModeAss') > 5 OR Tools::getValue('mr_ModeAss') < 0) $this->_postErrors[] = $this->l('Invalid Assurance mode'); if (!Tools::getValue('mr_Pays_list')) $this->_postErrors[] = $this->l('You must choose at least one delivery country.'); } elseif (Tools::isSubmit('submit_order_state')) { if (!Validate::isBool(Tools::getValue('mr_google_key'))) $this->_postErrors[] = $this->l('Invalid google key'); if (!Validate::isUnsignedInt(Tools::getValue('id_order_state'))) $this->_postErrors[] = $this->l('Invalid order state'); } /* elseif (Tools::isSubmit('PS_MRSubmitFieldPersonalization')) { $addr1 = Tools::getValue('Expe_ad1'); if (!preg_match('#^[0-9A-Z_\-\'., /]{2,32}$#', strtoupper($addr1), $match)) $this->_postErrors[] = $this->l('The Main address submited hasn\'t a good format'); }*/ } private function _postProcess() { foreach($_POST AS $key => $value) { $setArray[] = $value; $keyArray[] = pSQL($key); } array_pop($setArray); array_pop($keyArray); if (isset($_POST['submitMR']) AND $_POST['submitMR']) self::mrUpdate('settings', $setArray, $keyArray); elseif (isset($_POST['submitShipping']) AND $_POST['submitShipping']) self::mrUpdate('shipping', $_POST, array()); /*elseif (Tools::getValue('PS_MRSubmitFieldPersonalization')) $this->updateFieldsPersonalization();*/ elseif (isset($_POST['submitMethod']) AND $_POST['submitMethod']) self::mrUpdate('addShipping', $setArray, $keyArray); elseif (isset($_POST['submit_order_state']) AND $_POST['submit_order_state']) { Configuration::updateValue('MONDIAL_RELAY_ORDER_STATE', Tools::getValue('id_order_state')); Configuration::updateValue('MR_GOOGLE_MAP', Tools::getValue('mr_google_key')); $this->_html .= '
'.$this->l('Settings updated').'
'; } } public function hookOrderDetail($params) { global $smarty; $carrier = $params['carrier']; $order = $params['order']; if ($carrier->is_module AND $order->shipping_number) { $module = $carrier->external_module_name; include_once(_PS_MODULE_DIR_.$module.'/'.$module.'.php'); $module_carrier = new $module(); $smarty->assign('followup', $module_carrier->get_followup($order->shipping_number)); } elseif ($carrier->url AND $order->shipping_number) $smarty->assign('followup', str_replace('@', $order->shipping_number, $carrier->url)); } public function hookOrderDetailDisplayed($params) { global $smarty; $res = Db::getInstance()->getRow(' SELECT s.`MR_Selected_LgAdr1`, s.`MR_Selected_LgAdr2`, s.`MR_Selected_LgAdr3`, s.`MR_Selected_LgAdr4`, s.`MR_Selected_CP`, s.`MR_Selected_Ville`, s.`MR_Selected_Pays`, s.`MR_Selected_Num`, s.`url_suivi` FROM `'._DB_PREFIX_.'mr_selected` s WHERE s.`id_cart` = '.$params['order']->id_cart); if ((!$res) OR ($res['MR_Selected_Num'] == 'LD1') OR ($res['MR_Selected_Num'] == 'LDS')) return ''; $smarty->assign('mr_addr', $res['MR_Selected_LgAdr1'].($res['MR_Selected_LgAdr1'] ? ' - ' : '').$res['MR_Selected_LgAdr2'].($res['MR_Selected_LgAdr2'] ? ' - ' : '').$res['MR_Selected_LgAdr3'].($res['MR_Selected_LgAdr3'] ? ' - ' : '').$res['MR_Selected_LgAdr4'].($res['MR_Selected_LgAdr4'] ? ' - ' : '').$res['MR_Selected_CP'].' '.$res['MR_Selected_Ville'].' - '.$res['MR_Selected_Pays']); $smarty->assign('mr_url', $res['url_suivi']); return $this->display(__FILE__, 'orderDetail.tpl'); } /* ** No need anymore */ public function hookProcessCarrier($params) { } /* ** Update the carrier id to use the new one if changed */ public function hookupdateCarrier($params) { if ((int)($params['id_carrier']) != (int)($params['carrier']->id)) { Db::getInstance()->Execute(' INSERT INTO `'._DB_PREFIX_.'mr_method` (mr_Name, mr_Pays_list, mr_ModeCol, mr_ModeLiv, mr_ModeAss, id_carrier) ( SELECT mr_Name, mr_Pays_list, mr_ModeCol, mr_ModeLiv, mr_ModeAss, "'.(int)$params['carrier']->id.'" FROM `'._DB_PREFIX_.'mr_method` WHERE id_carrier ='.(int)$params['id_carrier'].')'); } } /* ** Get a carrier list liable to the module */ public function _getCarriers() { global $cookie; $id_lang = (int)$cookie->id_lang; // Query don't use the external_module_name to keep the // 1.3 compatibility $carriers = Db::getInstance()->ExecuteS(' SELECT c.id_carrier, c.range_behavior, m.id_mr_method, m.mr_ModeLiv, cl.delay FROM `'._DB_PREFIX_.'mr_method` m LEFT JOIN `'._DB_PREFIX_.'carrier` c ON c.`id_carrier` = m.`id_carrier` LEFT JOIN `'._DB_PREFIX_.'carrier_lang` cl ON c.`id_carrier` = cl.`id_carrier` WHERE c.`deleted` = 0 AND c.active = 1'); if (!is_array($carriers)) $carriers = array(); return $carriers; } public function hookextraCarrier($params) { global $smarty, $cart, $cookie, $defaultCountry, $nbcarriers; if (Configuration::get('MR_ENSEIGNE_WEBSERVICE') == '' || Configuration::get('MR_CODE_MARQUE') == '' || Configuration::get('MR_KEY_WEBSERVICE') == '' || Configuration::get('MR_LANGUAGE') == '') return ''; $address = new Address((int)($cart->id_address_delivery)); $id_zone = Address::getZoneById((int)($address->id)); $carriersList = self::_getCarriers(); // Check if the defined carrier are ok foreach ($carriersList as $k => $row) { $carrier = new Carrier((int)($row['id_carrier'])); if ((Configuration::get('PS_SHIPPING_METHOD') AND $carrier->getMaxDeliveryPriceByWeight($id_zone) === false) || (!Configuration::get('PS_SHIPPING_METHOD') AND $carrier->getMaxDeliveryPriceByPrice($id_zone) === false)) unset($carriersList[$k]); else if ($row['range_behavior']) { // Get id zone $id_zone = (isset($cart->id_address_delivery) AND $cart->id_address_delivery) ? Address::getZoneById((int)($cart->id_address_delivery)) : (int)$defaultCountry->id_zone; if ((Configuration::get('PS_SHIPPING_METHOD') AND (!Carrier::checkDeliveryPriceByWeight($row['id_carrier'], $cart->getTotalWeight(), $id_zone))) OR (!Configuration::get('PS_SHIPPING_METHOD') AND (!Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $cart->getOrderTotal(true, self::BOTH_WITHOUT_SHIPPING), $id_zone, $cart->id_currency)))) unset($carriersList[$k]); } } $preSelectedRelay = $this->getRelayPointSelected($params['cart']->id); $smarty->assign(array( 'one_page_checkout' => (Configuration::get('PS_ORDER_PROCESS_TYPE') ? Configuration::get('PS_ORDER_PROCESS_TYPE') : 0), 'new_base_dir' => self::$moduleURL, 'MRToken' => self::$MRFrontToken, 'carriersextra' => $carriersList, 'preSelectedRelay' => isset($preSelectedRelay['MR_selected_num']) ? $preSelectedRelay['MR_selected_num'] : '', 'jQueryOverload' => self::getJqueryCompatibility(false))); return $this->display(__FILE__, 'mondialrelay.tpl'); } public function getContent() { global $cookie; $error = null; $html = ''; if (!empty($_POST)) { $this->_postValidation(); if (!sizeof($this->_postErrors)) $this->_postProcess(); else { $nbErrors = sizeof($this->_postErrors); $this->_html .= '

'.$nbErrors.' '.($nbErrors > 1 ? $this->l('errors') : $this->l('error')).'

    '; foreach ($this->_postErrors AS $error) $this->_html .= '
  1. '.$error.'
  2. '; $this->_html .= '
'; } } if (isset($_GET['delete_mr']) && !empty($_GET['delete_mr'])) self::mrDelete((int)($_GET['delete_mr'])); $this->_html .= '

'.$this->l('Configure Mondial Relay Rate Module').'

'.$this->l('Try to turn off the cache and put the force compilation to on').' '.$this->l('if you have any problems with the module after an update').'.
'.$this->l('Have a look to the following HOW-TO to help you to configure the Mondial Relay module').'

'.$this->l('To create a Mondial Relay carrier'). ' - '.$this->l('Enter and save your Mondial Relay account settings').'
- '.$this->l('Create a Carrier using the form "add a carrier" below').'
- '.$this->l('Define a price for your carrier on').' '.$this->l('The Carrier page').'
- '.$this->l('To generate labels, you must have a valid and registered address of your store on your'). ' '.$this->l('contact page').'

'. $this->settingsForm(). '
'. $this->settingsstateorderForm(). '
'. $this->advancedSettings(). '
'. $this->addMethodForm(). '
'. $this->shippingForm(). '

'; return $this->_html; } /* ** Update the new defined fields of the merchant */ public function updateFieldsPersonalization() { Configuration::updateValue('PS_MR_SHOP_NAME', Tools::getValue('Expe_ad1')); $this->_html .= '
'.$this->l('Settings updated').'
'; } public function mrDelete($id) { $id = Db::getInstance()->getValue('SELECT `id_carrier` FROM `'._DB_PREFIX_ .'mr_method` WHERE `id_mr_method` = "'.(int)($id).'"'); Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_ .'carrier` SET `active` = 0, `deleted` = 1 WHERE `id_carrier` = "'.(int)($id).'"'); $this->_html .= '
'.$this->l('Delete successful').'
'; } public function mrUpdate($type, $array, $keyArray) { global $cookie; if ($type == 'settings') { Configuration::updateValue('MR_ENSEIGNE_WEBSERVICE', $array[0]); Configuration::updateValue('MR_CODE_MARQUE', $array[1]); Configuration::updateValue('MR_KEY_WEBSERVICE', $array[2]); Configuration::updateValue('MR_LANGUAGE', $array[3]); Configuration::updateValue('MR_WEIGHT_COEF', $array[4]); } elseif ($type == 'shipping') { array_pop($array); foreach ($array AS $Key => $value) { $key = explode(',', $Key); $id = Db::getInstance()->getValue('SELECT `id_carrier` FROM `'._DB_PREFIX_ .'mr_method` WHERE `id_mr_method` = "'.(int)($key[0]).'"'); Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'carrier SET active = "'.(int)($value).'" WHERE `id_carrier` = "'.(int)($id).'"'); } } elseif ($type == 'addShipping') { $query = 'INSERT INTO ' . _DB_PREFIX_ . 'mr_method ('; for ($q = 0; $q <= count($keyArray) - 1; $q++) { $end = ($q == count($keyArray) - 1) ? '' : ', '; $query .= $keyArray[$q] . $end; } $query .= ') VALUES('; for ($j = 0; $j <= count($array) - 1; $j++) { $var = $array[$j]; if (is_array($var)) $var = implode(",", $var); $end = ($j == count($array) - 1) ? '' : ', '; $query .= "'" . pSQL($var). "'" . $end; } $query .= ')'; Db::getInstance()->Execute($query); $mainInsert = mysql_insert_id(); $default = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "configuration WHERE name = 'PS_CARRIER_DEFAULT'"); $check = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "carrier"); $checkD = array(); foreach($check AS $Key) { foreach($Key AS $key => $value) if ($key == "id_carrier") $checkD[] = $value; } // Added for 1.3 compatibility to match with the right key if (_PS_VERSION_ >= '1.4') Db::getInstance()->Execute(' INSERT INTO `' . _DB_PREFIX_ . 'carrier` (`id_tax_rules_group`, `url`, `name`, `active`, `is_module`, `range_behavior`, `shipping_external`, `need_range`, `external_module_name`, `shipping_method`) VALUES("0", NULL, "'.pSQL($array[1]).'", "1", "1", "1", "0", "1", "mondialrelay", "1")'); else Db::getInstance()->Execute(' INSERT INTO `' . _DB_PREFIX_ . 'carrier` (`url`, `name`, `active`, `is_module`, `range_behavior`) VALUES(NULL, "'.pSQL('mondialrelay').'", "1", "0", "1")'); $get = Db::getInstance()->getRow('SELECT * FROM `' . _DB_PREFIX_ . 'carrier` WHERE `id_carrier` = "' . mysql_insert_id() . '"'); Db::getInstance()->Execute('UPDATE `' . _DB_PREFIX_ . 'mr_method` SET `id_carrier` = "' . (int)($get['id_carrier']) . '" WHERE `id_mr_method` = "' . pSQL($mainInsert) . '"'); $weight_coef = Configuration::get('MR_WEIGHT_COEF'); $range_weight = array('24R' => array(0, 20000 / $weight_coef), 'DRI' => array(20000 / $weight_coef, 130000 / $weight_coef), 'LD1' => array(0, 60000 / $weight_coef), 'LDS' => array(30000 / $weight_coef, 130000 / $weight_coef)); Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'range_weight` (`id_carrier`, `delimiter1`, `delimiter2`) VALUES ('.(int)($get['id_carrier']).', '.$range_weight[$array[2]][0].', '.$range_weight[$array[2]][1].')'); Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'range_price` (`id_carrier`, `delimiter1`, `delimiter2`) VALUES ('.(int)($get['id_carrier']).', 0.000000, 10000.000000)'); $groups = Group::getGroups(Configuration::get('PS_LANG_DEFAULT')); foreach ($groups as $group) Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'carrier_group` (id_carrier, id_group) VALUES('.(int)($get['id_carrier']).', '.(int)($group['id_group']).')'); $zones = Zone::getZones(); foreach ($zones as $zone) { Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'carrier_zone` (id_carrier, id_zone) VALUES('.(int)($get['id_carrier']).', '.(int)($zone['id_zone']).')'); $range_price_id = Db::getInstance()->getValue('SELECT id_range_price FROM ' . _DB_PREFIX_ . 'range_price WHERE id_carrier = "'.(int)($get['id_carrier']).'"'); $range_weight_id = Db::getInstance()->getValue('SELECT id_range_weight FROM ' . _DB_PREFIX_ . 'range_weight WHERE id_carrier = "'.(int)($get['id_carrier']).'"'); Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'delivery` (id_carrier, id_range_price, id_range_weight, id_zone, price) VALUES('.(int)($get['id_carrier']).', '.(int)($range_price_id).', NULL,'.(int)($zone['id_zone']).', 0.00)'); Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'delivery` (id_carrier, id_range_price, id_range_weight, id_zone, price) VALUES('.(int)($get['id_carrier']).', NULL, '.(int)($range_weight_id).','.(int)($zone['id_zone']).', 0.00)'); } if (!in_array($default[0]['value'], $checkD)) $default = Db::getInstance()->ExecuteS("UPDATE " . _DB_PREFIX_ . "configuration SET value = '" . (int)($get['id_carrier']) . "' WHERE name = 'PS_CARRIER_DEFAULT'"); } else return false; $this->_html .= '
'.$this->l('Settings updated').'
'; return true; } public function addMethodForm() { $zones = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "zone WHERE active = 1"); $output = '
'.$this->l('Add a Shipping Method').'
'; return $output; } public function shippingForm() { global $cookie; $query = Db::getInstance()->ExecuteS(' SELECT m.* FROM `'._DB_PREFIX_.'mr_method` m JOIN `'._DB_PREFIX_.'carrier` c ON (c.`id_carrier` = m.`id_carrier`) WHERE c.`deleted` = 0'); $output = '
'.$this->l('Shipping Method\'s list').'

'; return $output; } /* ** Display advanced settings form */ public function advancedSettings() { $form = ''; $form .= '
'.$this->l('Advanced Settings'). ' - '.$this->l('Click to display / hide the options').' '. '

'. $this->l('URL Cron Task:').' '.Tools::getHttpHost(true, true). _MODULE_DIR_.$this->name.'/cron.php?secure_key='. Configuration::get('MONDIAL_RELAY_SECURE_KEY'). '

'; return $form; } /* ** Form to allow personalization fields sent for MondialRelay ** Not used anymore but still present if needed */ public function personalizeFormFields() { $form = ''; $warn = ''; // Load the Default value from the configuration $addr1 = (Configuration::get('PS_MR_SHOP_NAME')) ? Configuration::get('PS_MR_SHOP_NAME') : Configuration::get('PS_SHOP_NAME'); // Check if a request exist and if errors occured, use the post variable if (Tools::isSubmit('PS_MRSubmitFieldPersonalization') && count($this->_postErrors)) $addr1 = Tools::safeOutput(Tools::getValue('Expe_ad1')); if (!Configuration::get('PS_MR_SHOP_NAME')) $warn .= '
'. $this->l('Its seems you updated Mondialrelay without use the uninstall / install method, you have to set up this part to make working the generating ticket process'). '
'; // Form $form = '
'; $form .= '
'.$this->l('Advanced Settings'). ' - '.$this->l('Click to display / hide the options').' '. ''. $warn.'
- '.$this->l('This part allow to override the data sent at MondialRelay when you want to generate Ticket. Some fields are restricted by the length, or forbidden char').'.

'.$this->l('The key used by Mondialrelay is').' Expe_ad1 '.$this->l('and has this default value').' : '.Configuration::get('PS_SHOP_NAME').'


'; return $form; } public function settingsstateorderForm() { global $cookie; $this->orderState = Configuration::get('MONDIAL_RELAY_ORDER_STATE'); $output = ''; $output .= '
'; $output .= '
'.$this->l('Settings').''; $output .= ''; $output .= '
'; $output .= ''; $output .= '

' . $this->l('Choose the order state for labels. You can manage the labels on').' '; $output .= ''. $this->l('the Mondial Relay administration page').'

'; $output .= '
'; $output .= '
'; $output .= '

'; return $output; } public function settingsForm() { $output = '
'.$this->l('Mondial Relay Account Settings').'
- '.$this->l('These parameters are provided by Mondial Relay once you subscribed to their service').'
'; return $output; } public function displayInfoByCart($id_cart) { $html = '

'; $simpleresul = Db::getInstance()->ExecuteS(' SELECT * FROM ' . _DB_PREFIX_ . 'mr_selected WHERE id_cart='.(int)($id_cart)); if (trim($simpleresul[0]['exp_number']) != '0') $html .= $this->l('Nb expedition:').$simpleresul[0]['exp_number']."
"; if (trim($simpleresul[0]['url_etiquette']) != '0') $html .= "".$this->l('Label URL')."
"; if (trim($simpleresul[0]['url_suivi']) != '0') $html .= "".$this->l('Follow-up URL')."
"; if (trim($simpleresul[0]['MR_Selected_Num']) != '') $html .= $this->l('Nb Point Relay :').$simpleresul[0]['MR_Selected_Num']."
"; if (trim($simpleresul[0]['MR_Selected_LgAdr1']) != '') $html .= $simpleresul[0]['MR_Selected_LgAdr1']."
"; if (trim($simpleresul[0]['MR_Selected_LgAdr2']) != '') $html .= $simpleresul[0]['MR_Selected_LgAdr2']."
"; if (trim($simpleresul[0]['MR_Selected_LgAdr3']) != '') $html .= $simpleresul[0]['MR_Selected_LgAdr3']."
"; if (trim($simpleresul[0]['MR_Selected_LgAdr4']) != '') $html .= $simpleresul[0]['MR_Selected_LgAdr4']."
"; if (trim($simpleresul[0]['MR_Selected_CP']) != '') $html .= $simpleresul[0]['MR_Selected_CP']." "; if (trim($simpleresul[0]['MR_Selected_Ville']) != '') $html .= $simpleresul[0]['MR_Selected_Ville']."
"; if (trim($simpleresul[0]['MR_Selected_Pays']) != '') $html .= $simpleresul[0]['MR_Selected_Pays']."
"; $html .= '

'; return $html; } public function get_followup($shipping_number) { $query = 'SELECT url_suivi FROM '._DB_PREFIX_ .'mr_selected WHERE id_mr_selected=\''.(int)($shipping_number).'\';'; $settings = Db::getInstance()->ExecuteS($query); if(!isset($settings[0]['url_suivi'])) return null; return $settings[0]['url_suivi']; } public function set_carrier($key, $value, $id_carrier) { if ($key == 'name') $key = 'mr_Name'; return Db::getInstance()->Execute(' UPDATE ' . _DB_PREFIX_ . 'mr_method SET '.pSQL($key).'="'.pSQL($value).'" WHERE id_carrier=\''.(int)($id_carrier).'\' ; '); } // Add for 1.3 compatibility and avoid duplicate code public static function jsonEncode($result) { return (method_exists('Tools', 'jsonEncode')) ? Tools::jsonEncode($result) : json_encode($result); } public static function ordersSQLQuery1_4($id_order_state) { return 'SELECT o.`id_address_delivery` as id_address_delivery, o.`id_order` as id_order, o.`id_customer` as id_customer, o.`id_cart` as id_cart, o.`id_lang` as id_lang, mrs.`id_mr_selected` as id_mr_selected, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`, o.`total_paid_real` as total, o.`total_shipping` as shipping, o.`date_add` as date, o.`id_currency` as id_currency, o.`id_lang` as id_lang, mrs.`MR_poids` as weight, mr.`mr_Name` as mr_Name, mrs.`MR_Selected_Num` as MR_Selected_Num, mrs.`MR_Selected_Pays` as MR_Selected_Pays, mrs.`exp_number` as exp_number, mr.`mr_ModeCol` as mr_ModeCol, mr.`mr_ModeLiv` as mr_ModeLiv, mr.`mr_ModeAss` as mr_ModeAss FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'carrier` ca ON (ca.`id_carrier` = o.`id_carrier` AND ca.`external_module_name` = "mondialrelay") LEFT JOIN `'._DB_PREFIX_.'mr_selected` mrs ON (mrs.`id_cart` = o.`id_cart`) LEFT JOIN `'._DB_PREFIX_.'mr_method` mr ON (mr.`id_mr_method` = mrs.`id_method`) LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = o.`id_customer`) WHERE ( SELECT moh.`id_order_state` FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` ORDER BY moh.`date_add` DESC LIMIT 1) = '.(int)($id_order_state).' AND ca.`external_module_name` = "mondialrelay"'; } public static function ordersSQLQuery1_3($id_order_state) { return ' SELECT o.`id_address_delivery` as id_address_delivery, o.`id_order` as id_order, o.`id_customer` as id_customer, o.`id_cart` as id_cart, mrs.`id_mr_selected` as id_mr_selected, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`, o.`total_paid_real` as total, o.`total_shipping` as shipping, o.`date_add` as date, o.`id_currency` as id_currency, o.`id_lang` as id_lang, mrs.`MR_poids` as weight, mr.`mr_Name` as mr_Name, mrs.`MR_Selected_Num` as MR_Selected_Num, mrs.`MR_Selected_Pays` as MR_Selected_Pays, mrs.`exp_number` as exp_number, mr.`mr_ModeCol` as mr_ModeCol, mr.`mr_ModeLiv` as mr_ModeLiv, mr.`mr_ModeAss` as mr_ModeAss FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'carrier` ca ON (ca.`id_carrier` = o.`id_carrier` AND ca.`name` = "mondialrelay") LEFT JOIN `'._DB_PREFIX_.'mr_selected` mrs ON (mrs.`id_cart` = o.`id_cart`) LEFT JOIN `'._DB_PREFIX_.'mr_method` mr ON (mr.`id_mr_method` = mrs.`id_method`) LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = o.`id_customer`) WHERE ( SELECT moh.`id_order_state` FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` ORDER BY moh.`date_add` DESC LIMIT 1) = '.(int)($id_order_state).' AND ca.`name` = "mondialrelay"'; } public static function getBaseOrdersSQLQuery($id_order_state) { if (_PS_VERSION_ >= '1.4') return self::ordersSQLQuery1_4($id_order_state); else return self::ordersSQLQuery1_3($id_order_state); } public static function getOrders($orderIdList = array(), $filterEntries = self::NO_FILTER) { $id_order_state = Configuration::get('MONDIAL_RELAY_ORDER_STATE'); $sql = self::getBaseOrdersSQLQuery($id_order_state); if (count($orderIdList)) { $sql .= ' AND o.id_order IN ('; foreach ($orderIdList as $id_order) $sql .= (int)$id_order.', '; $sql = rtrim($sql, ', ').')'; } switch($filterEntries) { case self::WITHOUT_HOME_DELIVERY: $sql .= 'AND mr.mr_ModeLiv != "LD1" AND mr.mr_ModeLiv != "LDS"'; break; default: break; } $sql .= ' GROUP BY o.`id_order` ORDER BY o.`date_add` ASC'; return Db::getInstance()->ExecuteS($sql); } public function getErrorCodeDetail($code) { global $statCode; if (isset($statCode[$code])) return $statCode[$code]; return $this->l('This error isn\'t referred : ') . $code; } public function getRelayPointSelected($id_cart) { return Db::getInstance()->getRow(' SELECT s.`MR_selected_num` FROM `'._DB_PREFIX_.'mr_selected` s WHERE s.`id_cart` = '.(int)$id_cart); } public function isMondialRelayCarrier($id_carrier) { return Db::getInstance()->getRow(' SELECT `id_carrier` FROM `'._DB_PREFIX_.'mr_method` WHERE `id_carrier` = '.(int)$id_carrier); } public function hookpaymentTop($params) { if ($this->isMondialRelayCarrier($params['cart']->id_carrier) && !$this->getRelayPointSelected($params['cart']->id)) $params['cart']->id_carrier = 0; } }