'.$this->l('Configure Mondial Relay Rate Module').'
'.
$this->settingsForm().
'
'.
$this->settingsstateorderForm().
'
'.
$this->personalizeFormFields().
'
'.
$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[0]).'", "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", "1", "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 = '
';
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 = '
';
return $output;
}
/*
** Form to allow personalization fields sent for MondialRelay
*/
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::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 = ' ';
return $form;
}
public function settingsstateorderForm()
{
global $cookie;
$this->orderState = Configuration::get('MONDIAL_RELAY_ORDER_STATE');
$output = '';
$output .= ' ';
return $output;
}
public function settingsForm()
{
$output = '
';
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);
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,
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_carrier` = ca.`id_carrier`)
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_carrier` = ca.`id_carrier`)
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())
{
$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, ', ').')';
}
$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;
}
}