* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 8783 $
* @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;
class philea_magistor extends Module
{
const ID_SHIPPING = 4;
private $tab_carrier_magistor = array(
'CHRONO13H_BtoC'=>'Chronopost 13h',
'CHRONOCLA'=>'Chronopost europe',
'COLIAF'=>'COLISSIMO ACCESS F SEI',
'COLICO'=>'COLIECOMMERCE',
'COLIEDOM'=>'COLISSIMO EXPERT OM',
'COLIEF'=>'COLISSIMO EXPERT F SEI',
'COLINT'=>'COLISSIMO INTERNATIONAL',
'COLIEI'=>'COLISSIMO EXPERT INTERNATIONAL SEI',
'COLIEY'=>'COLISSIMO EXPERT INTERNATIONAL KPG',
'COLISF'=>'COLISSIMO SERVICE F SEI',
'GLS'=>'GLS',
'La Poste'=>'La Poste',
'LAPOSTECR'=>'La Poste Contre Remboursement',
'LETMAX'=>'Lettre Max',
'LETSIMPLE'=>'Lettre Simple',
'MDRY'=>'Mondial Relay',
'MESSAGERIE'=>'MESSAGERIE',
'RETSUPP'=>'Retrait Supplyweb',
'SOCOLCITY'=>'SO COLISSIMO CITYSSIMO',
'SOCOLMBP'=>'SO COLISSIMO MON BUREAU DE POSTE',
'SOCOLMC'=>'SO COLISSIMO MON COMMMERCANT',
'SOCOLMDS'=>'SO COLISSIMO MON DOMICILE AVEC SIGNATURE',
'SOCOLMDSS'=>'SO COLISSIMO MON DOMICILE SANS SIGNATURE',
'SOCOLMRRDV'=>'SO COLISSIMO MON RENDEZ VOUS (PARIS)',
'TNT'=>'TNT',
'EXAPAQ' => 'EXAPAQ',
'DUCROS' => 'MESSAGERIE (DUCROS)',
'DPD' => 'DPD',
);
public function __construct()
{
$this->name = 'philea_magistor';
$this->tab = 'shipping_logistics';
$this->version = '1.0';
$this->author = 'Antadis';
parent::__construct();
$this->displayName = $this->l('Philea');
$this->description = $this->l('Import and Export to Philea Solution');
}
public function install()
{
Db::getInstance()->ExecuteS('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_sent` (
`id_order` INTEGER NOT NULL,
`date_add` DATETIME NOT NULL,
INDEX (`id_order`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
');
Db::getInstance()->ExecuteS('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_parcel` (
`id_order_detail` INTEGER NOT NULL,
`quantity` INTEGER NOT NULL,
`shipping_number` VARCHAR(64) NOT NULL,
`date_add` DATETIME NOT NULL,
INDEX (`id_order_detail`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
');
Db::getInstance()->ExecuteS('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_sync` (
`id_sync` INTEGER NOT NULL AUTO_INCREMENT,
`id_sale` INTEGER NOT NULL,
`status` TINYINT NOT NULL,
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_sync`),
INDEX (`id_sale`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
');
Db::getInstance()->ExecuteS('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_auto_sync` (
`id_sale` INTEGER NOT NULL,
`active` TINYINT NOT NULL,
`last_sent` DATETIME NOT NULL,
PRIMARY KEY (`id_sale`),
INDEX (`id_sale`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
');
Db::getInstance()->execute('
CREATE TABLE `'._DB_PREFIX_.'philea_archive` (
`id_sale` int(11) NOT NULL,
`date_add` datetime NOT NULL,
PRIMARY KEY (`id_sale`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
');
if (parent::install() == false) {
return false;
}
return true;
}
public function uninstall() {
if (parent::uninstall() == false) {
return false;
}
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_sent`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_parcel`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_sync`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_auto_sync`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_archive`');
// old version
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_sent`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_parcel`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_sync`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_auto_sync`');
// Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_archive`');
return true;
}
public function displayForm()
{
global $cookie;
$order_states = OrderState::getOrderStates($cookie->id_lang);
$form = '
';
return $form;
}
public function getContent()
{
$output = ''.$this->displayName.'
';
if (!empty($_POST))
{
$output .= $this->_postProcess();
}
return $output.$this->displayForm();
}
private function _postProcess()
{
if(Tools::isSubmit('submitSettings')) {
if(isset($_POST['CONFIG']))
{
foreach($_POST['CONFIG'] as $key => $value)
Configuration::updateValue($key, $value);
}
if(isset($_POST['PHILEA_MAGISTOR_CARRIER']))
Configuration::updateValue('PHILEA_MAGISTOR_CARRIER', self::setTabState($_POST['PHILEA_MAGISTOR_CARRIER']));
return ' '.$this->l('Settings updated').'
';
} elseif(Tools::isSubmit('submitPhilea')) {
$id_sale = (int) Tools::getValue('id_sale');
if((int) Db::getInstance()->getValue('
SELECT `id_sale`
FROM `'._DB_PREFIX_.'privatesale_shipping_sale`
WHERE `id_sale` = '.(int) $id_sale.'
AND `id_shipping` = '. (int) self::ID_SHIPPING.'
')) {
if(!Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'philea_sync`
WHERE `id_sale` = '.(int) $id_sale.'
AND `status` = 0
')) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'philea_sync`
VALUES (
DEFAULT,
'.(int) $id_sale.',
0,
NOW()
)
');
echo ''.$this->l('Sale added to queue successfully').'
';
} else {
echo ''.$this->l('Sync already in progress for this sale').'
';
}
} else {
echo ''.$this->l('Invalid sale id').'
';
}
}
}
static public function setTabState($tab)
{
$tabState = '';
foreach ($tab as $key => $value)
{
$tabState .= $key.';'.$value.'|';
}
return substr($tabState,0,-1);
}
static public function getTabState()
{
$tab=explode('|',Configuration::get('PHILEA_MAGISTOR_CARRIER'));
$carrier_magistor=array();
if(is_array($tab) AND sizeof($tab) > 0)
{
foreach ($tab as $ligne_carrier)
{
$temp=explode(';',$ligne_carrier);
if(is_array($temp) AND sizeof($temp) > 1) {
$carrier_magistor[$temp[0]]=$temp[1];
foreach(Db::getInstance()->ExecuteS('
SELECT `id_carrier`
FROM `'._DB_PREFIX_.'carrier`
WHERE `name` = (
SELECT `name`
FROM `'._DB_PREFIX_.'carrier`
WHERE `id_carrier` = '.(int) str_replace(':', '', $temp[0]).'
)
AND `id_carrier` != '.(int) str_replace(':', '', $temp[0]).'
AND `deleted` = 1
') as $row) {
$carrier_magistor[(int) $row['id_carrier'].':'] = $temp[1];
}
}
}
return $carrier_magistor;
}
}
static public function getCompletedCodeCarrierMagistor($code_carrier)
{
switch($code_carrier){
case 'SOCOLCITY':
return 'CIT';
break;
case 'SOCOLMBP':
return 'BPR';
break;
case 'SOCOLMC':
return 'A2P';
break;
case 'SOCOLMDS':
return 'DOM';
break;
case 'SOCOLMDSS':
return 'DOS';
break;
case 'SOCOLMRRDV':
return 'RDV';
break;
default :
return '';
break;
}
}
public function getProductChild($by, $field) {
if(!strlen(trim($field))) return false;
$clause = 1;
if($by == 'reference')
$clause = 'pa.`reference` = "'.pSql(addslashes($field)).'"';
if($by == 'ean')
$clause = 'pa.`ean13` = "'.pSql(addslashes($field)).'"';
$sql = 'SELECT
p.`id_product`
,p.`price` as parent_price
,pa.id_product_attribute
,pa.`price`
,pa.`quantity`
FROM
`'._DB_PREFIX_.'product_attribute` as pa
INNER JOIN
`'._DB_PREFIX_.'product` as p
ON
p.`id_product` = pa.`id_product`
WHERE
'.$clause.'
';
$result = Db::getInstance()->getRow($sql);
//echo "
".$sql;
return $result;
}
public function getProductId($by, $field) {
if(!strlen(trim($field))) return false;
$clause = 1;
if($by == 'reference')
$clause = 'p.`reference` = "'.pSql(addslashes($field)).'"';
if($by == 'ean')
$clause = 'p.`ean13` = "'.pSql(addslashes($field)).'"';
$sql = 'SELECT
p.`id_product`
,p.`quantity`
FROM
`'._DB_PREFIX_.'product` as p
WHERE
'.$clause.'
';
$result = Db::getInstance()->getRow($sql);
return $result;
}
public static function getName($id_product,$id_lang, $id_product_attribute = False)
{
$name = DB::getInstance()->getValue('SELECT name FROM '._DB_PREFIX_.'product_lang WHERE id_product = '.$id_product.' AND id_lang = '.$id_lang);
if($id_product_attribute)
{
$names_attribute = DB::getInstance()->ExecuteS('SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, pa.`unit_price_impact`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)($id_lang).')
WHERE pa.`id_product_attribute` = '.(int)($id_product_attribute).'
ORDER BY pa.`id_product_attribute`');
foreach ($names_attribute as $name_attribute)
{
$name .= ' - '.$name_attribute['group_name'].' : '.$name_attribute['attribute_name'];
}
}
return $name;
}
static public function getCarriersIdByRef($carrier_ref)
{
$tab = explode('|',Configuration::get('PHILEA_MAGISTOR_CARRIER'));
$carriers = FALSE;
$carrier_ids = array();
if (is_array($tab) AND sizeof($tab) > 0)
{
foreach ($tab as $carrier)
{
$temp = explode(';',$carrier);
if (is_array($temp) AND sizeof($temp) > 1) {
if ($temp[1] != $carrier_ref)
continue;
$ids = explode(':', $temp[0]);
$carrier_ids[] = (int) $ids[0];
}
}
}
if ($carrier_ids && count($carrier_ids)){
$carriers = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'carrier`
WHERE `id_carrier` IN ('.implode(', ', $carrier_ids).')
');
return $carriers;
}
return FALSE;
}
}