bebeboutik/modules/philea_magistor/philea_magistor.php

557 lines
18 KiB
PHP
Raw Normal View History

<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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();
2016-09-07 12:03:31 +02:00
$this->displayName = $this->l('Philea');
$this->description = $this->l('Import and Export to Philea Solution');
}
public function install()
{
Db::getInstance()->ExecuteS('
2016-09-07 11:42:38 +02:00
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('
2016-09-07 11:42:38 +02:00
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_parcel` (
2016-09-07 12:03:31 +02:00
`id_order_detail` INTEGER NOT NULL,
`quantity` INTEGER NOT NULL,
`shipping_number` VARCHAR(64) NOT NULL,
`date_add` DATETIME NOT NULL,
2016-09-07 12:03:31 +02:00
INDEX (`id_order_detail`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
');
Db::getInstance()->ExecuteS('
2016-09-07 11:42:38 +02:00
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('
2016-09-07 11:42:38 +02:00
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('
2016-09-07 11:42:38 +02:00
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
');
2016-09-07 11:42:38 +02:00
if (parent::install() == false) {
return false;
2016-09-07 11:42:38 +02:00
}
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`');
2016-09-07 12:03:31 +02:00
// 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`');
2016-09-07 11:42:38 +02:00
return true;
}
public function displayForm()
{
global $cookie;
$order_states = OrderState::getOrderStates($cookie->id_lang);
$form = '<form action="'.$_SERVER['REQUEST_URI'].'" method="post" class="form"><div class="clear">&nbsp;</div>';
$form .= '<form action="'.$_SERVER['REQUEST_URI'].'" method="post" class="form"><div class="clear">&nbsp;</div>';
$form .= '<fieldset><legend>'.$this->l('Settings magistor FTP').'</legend>';
$form .= '
<label for="philea_magistor_ftp_svr">'.$this->l('Serveur').'</label>
<div class="margin-form">
<input type="text" id="philea_magistor_ftp_svr" name="CONFIG[PHILEA_MAGISTOR_FTP_SVR]" value="'.Configuration::get('PHILEA_MAGISTOR_FTP_SVR').'"/>
</div>
<label for="philea_magistor_ftp_user">'.$this->l('Identifiant').'</label>
<div class="margin-form">
<input type="text" id="philea_magistor_ftp_user" name="CONFIG[PHILEA_MAGISTOR_FTP_USER]" value="'.Configuration::get('PHILEA_MAGISTOR_FTP_USER').'"/>
</div>
<label for="philea_magistor_ftp_pwd">'.$this->l('Mot de passe').'</label>
<div class="margin-form">
<input type="text" id="philea_magistor_ftp_pwd" name="CONFIG[PHILEA_MAGISTOR_FTP_PWD]" value="'.Configuration::get('PHILEA_MAGISTOR_FTP_PWD').'"/>
</div>
<label for="PHILEA_MAGISTOR_FTP_MODE">'.$this->l('Mode').'</label>
<div class="margin-form">
<select id="philea_magistor_ftp_mode" name="CONFIG[PHILEA_MAGISTOR_FTP_MODE]">
<option value="active" '.(Configuration::get('PHILEA_MAGISTOR_FTP_MODE')=='active'? 'selected="selected"' : '').'>Active</option>
<option value="passive" '.(Configuration::get('PHILEA_MAGISTOR_FTP_MODE')=='passive'? 'selected="selected"' : '').'>Passive</option>
</select>
</div>
';
$form .= '</fieldset><div class="clear">&nbsp;</div>';
$form .= '<fieldset><legend>'.$this->l('Settings').'</legend>';
$form.='
<label for="PHILEA_MAGISTOR_STATUS_CRP">'.$this->l('Statut de préparation').'</label>
<div class="margin-form">
<select id="PHILEA_MAGISTOR_STATUS_CRP" name="CONFIG[PHILEA_MAGISTOR_STATUS_CRP]">';
foreach($order_states as $order_state)
$form .= '<option value="'.$order_state['id_order_state'].'" '.(Configuration::get('PHILEA_MAGISTOR_STATUS_CRP')==$order_state['id_order_state']?'selected="selected"':'').'>'.$order_state['name'].'</option>';
$form .= '</select>
</div>
<label for="PHILEA_MAGISTOR_STATUS_CRE">'.$this->l('Statut d\'expédition').'</label>
<div class="margin-form">
<select id="PHILEA_MAGISTOR_STATUS_CRE" name="CONFIG[PHILEA_MAGISTOR_STATUS_CRE]">';
foreach($order_states as $order_state)
$form .= '<option value="'.$order_state['id_order_state'].'" '.(Configuration::get('PHILEA_MAGISTOR_STATUS_CRE')==$order_state['id_order_state']?'selected="selected"':'').'>'.$order_state['name'].'</option>';
$form .= '</select>
</div>
<label for="philea_magistor_assurance">'.$this->l('Activer l\'assurance à partir de').'</label>
<div class="margin-form">
<input type="text" id="philea_magistor_assurance" name="CONFIG[PHILEA_MAGISTOR_ASSURANCE]" value="'.Configuration::get('PHILEA_MAGISTOR_ASSURANCE').'"/> &euro;
<p>'.$this->l('Laisser vide si vous ne souhaitez pas activer l\'assurance').'</p>
</div>
<label for="philea_magistor_code_ste">'.$this->l('Code Société').'</label>
<div class="margin-form">
<input type="text" id="philea_magistor_code_ste" name="CONFIG[PHILEA_MAGISTOR_CODE_STE]" value="'.Configuration::get('PHILEA_MAGISTOR_CODE_STE').'"/>
</div>
<div class="margin-form">
<table id="table_conv" style="border:1px solid #ccc;" cellpadding="3"><tbody>
<tr>
<th>'.$this->l('Id transporteur Prestashop').'</th>
<th>'.$this->l('Transporteur PrestaShop').'</th>
<th>'.$this->l('Transporteur Magistor associé').'</th>
<th>'.$this->l('Statut transporteur Prestashop').'</th>
</tr>';
$cpt = 0;
$tab = self::getTabState();
$keys_tab = array_keys($tab);
$id_associated = array();
if(isset($keys_tab[0]) && !empty($keys_tab[0]))
{
foreach ($tab as $carrier_presta => $carrier_magistor)
{
$id_associated[] = (int) $temp[0];
$temp = explode(':',$carrier_presta);
$qCarrier = Db::getInstance()->getRow('SELECT id_carrier,name,active,deleted FROM '._DB_PREFIX_.'carrier WHERE id_carrier ='.$temp[0]);
$form.='<tr id="'.$cpt.'">
<td style="text-align:center;">'.$qCarrier['id_carrier'].'</td>
<td>'.$qCarrier['name'].'<input type="hidden" value="'.$carrier_magistor.'" name="PHILEA_MAGISTOR_CARRIER['.stripslashes($carrier_presta).']"></td>
<td>'.stripslashes($this->tab_carrier_magistor[$carrier_magistor]).'</td>
<td>'.(($qCarrier['active'] AND !$qCarrier['deleted'])?'<font color="green">active</font>':'<font color="red">inactif</font>').'</td>
<!--td><a href="javascript:delete_row('.$cpt.')">'.$this->l('supprimer').'</a></td//-->
</tr>';
$cpt++;
}
}
$form.='</tbody></table>
</div>
<label for="carrier_magistor">'.$this->l('New carrier').'</label>
<div class="margin-form">
<select id="carrier_magistor">';
foreach ($this->tab_carrier_magistor as $key => $value)
{
$form.='<option value="'.$key.'|'.$value.'">'.$value.'</option>';
}
$form.='</select>
<select id="carrier_presta">';
//$result = Db::getInstance()->ExecuteS('SELECT id_order_state, name FROM '._DB_PREFIX_.'order_state_lang WHERE id_lang ='.$cookie->id_lang);
$result = Carrier::getCarriers($cookie->id_lang, false,false,false,NULL,5);
foreach ($result as $carrier)
{
if(!in_array((int) $carrier['id_carrier'], $id_associated)) {
$form.='<option value="'.$carrier['id_carrier'].'|'.$carrier['name'].'">'.$carrier['name'].'</option>';
}
}
$form.='</select>
<input type="button" class="button" onclick="add_carrier()" value="'.$this->l('Ajouter').'"/>
</div>
<script type="text/javascript">
var cpt='.$cpt.';
function add_carrier()
{
var temp = $("#carrier_presta").val();
var temp2 = $("#carrier_magistor").val();
temp = temp.split("|");
temp2 = temp2.split("|");
var add_html="<tr id=\""+cpt+"\"><td>"+temp2[1]+"</td>";
add_html+=\'<td>\'+temp[1]+\'<input type="hidden" value="\'+temp2[0]+\'" name="PHILEA_MAGISTOR_CARRIER[\'+temp[0]+\':\'+getCompletedCodeCarrierMagistor(temp2[0])+\']"/></td><td><a href="javascript:delete_row(\'+cpt+\')">'.$this->l('supprimer').'</a></td></tr>\';
cpt++;
$("table#table_conv tbody").append(add_html);
}
function delete_row(id)
{
$("#"+id).remove();
}
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 "DOS";
break;
case "SOCOLMDSS":
return "DOM";
break;
case "SOCOLMRRDV":
return "RDV";
break;*/
default :
return "";
break;
}
}
</script>
<div class="margin-form">
<input type="submit" name="submitSettings" value="'.$this->l('Enregistrer').'" class="button" />
</div>
</fieldset>
</form>';
return $form;
}
public function getContent()
{
$output = '<h2>'.$this->displayName.'</h2>';
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 '<div class="conf confirm"><img src="' . _PS_IMG_ . 'admin/enabled.gif" alt="ok" />&nbsp;'.$this->l('Settings updated').'</div>';
} 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 *
2016-09-07 11:42:38 +02:00
FROM `'._DB_PREFIX_.'philea_sync`
WHERE `id_sale` = '.(int) $id_sale.'
AND `status` = 0
')) {
Db::getInstance()->ExecuteS('
2016-09-07 11:42:38 +02:00
INSERT INTO `'._DB_PREFIX_.'philea_sync`
VALUES (
DEFAULT,
'.(int) $id_sale.',
0,
NOW()
)
');
echo '<p class="conf">'.$this->l('Sale added to queue successfully').'</p><br />';
} else {
echo '<p class="error">'.$this->l('Sync already in progress for this sale').'</p><br />';
}
} else {
echo '<p class="error">'.$this->l('Invalid sale id').'</p><br />';
}
}
}
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 "<br>".$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;
}
}