543 lines
20 KiB
PHP
543 lines
20 KiB
PHP
<?php
|
|
require_once(dirname(dirname(__FILE__)).'/config/config.inc.php');
|
|
|
|
if (isset($_GET['secure_key']) &&
|
|
( $_SERVER['REMOTE_ADDR'] == 'localhost'
|
|
|| $_SERVER['REMOTE_ADDR'] == '127.0.0.1'
|
|
|| $_SERVER['REMOTE_ADDR'] == '37.59.17.186'
|
|
))
|
|
{
|
|
$secureKey = md5(_COOKIE_KEY_.Configuration::get('PS_SHOP_NAME'));
|
|
if (!empty($secureKey) AND $secureKey === $_GET['secure_key'])
|
|
{
|
|
$l4_orders = l4_getOrders();
|
|
if(count($l4_orders) > 0)
|
|
{
|
|
l4_anta_log('Commandes a traiter');
|
|
|
|
$ret = l4_generateXML($l4_orders);
|
|
|
|
l4_sendXML($ret['xml']);
|
|
l4_changeOrderState($ret['good_orders']);
|
|
|
|
if(count($ret['no_mr_orders']) > 0)
|
|
l4_changeOrderStateNoMR($ret['no_mr_orders']);
|
|
|
|
unset($l4_ret);
|
|
exit;
|
|
}
|
|
else
|
|
{
|
|
l4_anta_log('Aucune commandes à traiter');
|
|
}
|
|
}
|
|
}
|
|
|
|
function l4_getOrders()
|
|
{
|
|
$date_to = date('Y-m-d')." 5:00:00";
|
|
$date_from = date('Y-m-d', strtotime("-1 day"))." 12:00:00";
|
|
|
|
$sql = "SELECT o.*,c.name as carrier_name
|
|
from ps_orders o
|
|
left join ps_carrier c on (o.id_carrier=c.id_carrier)
|
|
where invoice_date >= '".$date_from."'
|
|
and invoice_date <= '".$date_to."'
|
|
and valid = 1 ";
|
|
|
|
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
|
$orders = array();
|
|
|
|
/* Checking if Order has Non virtual product to be exported */
|
|
foreach ($result as $order)
|
|
{
|
|
$temp_order = new Order((int) $order['id_order']);
|
|
|
|
$order_detail_lists = $temp_order->getOrderDetailList();
|
|
$is_virtual = true;
|
|
foreach ($order_detail_lists as $order_detail_list)
|
|
{
|
|
$temp_product = new Product((int) $order_detail_list['product_id']);
|
|
if(!$temp_product->is_virtual == 1)
|
|
$is_virtual = false;
|
|
|
|
}
|
|
|
|
if(!$is_virtual)
|
|
$orders[] = $temp_order;
|
|
|
|
}
|
|
return $orders;
|
|
}
|
|
|
|
$id_lang = 1;
|
|
$id_products_to_replace = array();
|
|
if (Module::isInstalled('antadis_productpack')) {
|
|
include_once(_PS_MODULE_DIR_.'antadis_productpack/models/ProductPack.php');
|
|
$id_products_to_replace = ProductPack::getIdProductsToReplace();
|
|
}
|
|
|
|
function l4_generateXML($orders)
|
|
{
|
|
$txt = '';
|
|
$good_orders='';
|
|
$good_orders_id='';
|
|
$dateAnnneMois=date('Ymd');
|
|
|
|
|
|
foreach($orders as $order)
|
|
{
|
|
if((int) $order->current_state == 13 ||
|
|
(int) $order->current_state == 12 ||
|
|
(int) $order->current_state == 2)
|
|
{
|
|
$good_orders[] = $order;
|
|
$good_orders_id[] = $order->id;
|
|
}
|
|
}
|
|
|
|
l4_anta_log(implode('# ', $good_orders_id));
|
|
|
|
$txt = '<?xml version="1.0" encoding="UTF-8"?>
|
|
<ORDERS>';
|
|
|
|
$i=0;
|
|
foreach($good_orders as $good_order)
|
|
{
|
|
l4_anta_log('Recuperation informations CMDE # '.$good_order->id);
|
|
|
|
$carrier = new Carrier((int) $good_order->id_carrier);
|
|
l4_anta_log('Transporteur recupere # '.$carrier->id.' - '.$carrier->name);
|
|
|
|
$carrier_name = utf8_encode($carrier->name);
|
|
|
|
if(preg_match('#Mondial Relay#', $carrier_name))
|
|
{
|
|
$carrier_name='MONDIAL RELAY 24R GAR';
|
|
}
|
|
elseif(preg_match('#Colissimo#', $carrier_name))
|
|
{
|
|
$carrier_name='COLISSIMO EXPERT FR GAR';
|
|
}
|
|
elseif(preg_match('#UPS#', $carrier_name))
|
|
{
|
|
$carrier_name='UPS SAVER GAR';
|
|
}
|
|
elseif(preg_match('#IMX#', $carrier_name))
|
|
{
|
|
$carrier_name='IMX GAR';
|
|
}
|
|
else
|
|
{
|
|
$carrier_name='IMX GAR';
|
|
}
|
|
|
|
$address = new Address((int) $good_order->id_address_delivery);
|
|
l4_anta_log('Addresse recuperee # '.$address->id);
|
|
|
|
$country_id = Country::getIdByName(1, $address->country);
|
|
$country = new Country((int) $country_id);
|
|
l4_anta_log('Pays recupere # '.$country->id.' - '.$country->iso_code.' - '.$country->name[1]);
|
|
|
|
$customer = new customer((int) $good_order->id_customer);
|
|
l4_anta_log('Client recupere # '.$customer->id.' - '.$customer->firstname.' '.$customer->lastname);
|
|
|
|
if(preg_match('#MONDIAL RELAY#',$carrier_name))
|
|
{
|
|
$sql_MR = "SELECT MR_Selected_Pays,MR_Selected_Num FROM `ps_mr_selected` WHERE id_order = ".(int)$good_order->id.' LIMIT 1';
|
|
$result_MR = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql_MR);
|
|
|
|
foreach ($result_MR as $row_MR)
|
|
{
|
|
$MondialRelay = '';
|
|
$MondialRelay = $row_MR['MR_Selected_Pays'].$row_MR['MR_Selected_Num'];
|
|
|
|
l4_anta_log('Mondial Relay recupere # '.$row_MR['MR_Selected_Pays'].' - '.$row_MR['MR_Selected_Num']);
|
|
}
|
|
|
|
}
|
|
|
|
$reference = strtoupper($good_order->id);
|
|
$nom = strtoupper(wd_remove_accents($address->firstname));
|
|
$prenom = strtoupper(wd_remove_accents($address->lastname));
|
|
$company = strtoupper(wd_remove_accents($address->company));
|
|
$addresses = formatAdress($address);
|
|
$address1 = isset($addresses[0]) ? str_pad(wd_remove_accents($addresses[0]), 35, " ", STR_PAD_RIGHT) : str_pad(' ',35," ", STR_PAD_RIGHT);
|
|
$address2 = isset($addresses[1]) ? str_pad(wd_remove_accents($addresses[1]), 35, " ", STR_PAD_RIGHT) : str_pad(' ',35," ", STR_PAD_RIGHT);
|
|
$ville = wd_remove_accents($address->city);
|
|
$pays = strtoupper(utf8_encode($address->country));
|
|
$isoPays = strtoupper($country->iso_code);
|
|
$codpost = strtoupper(utf8_encode($address->postcode));
|
|
$mail = strtoupper($customer->email);
|
|
|
|
if($isoPays == 'IE' && ($codpost == '' || $codpost == ' '))
|
|
$codpost = '00000';
|
|
|
|
|
|
$Transporteur = strtoupper(" ");
|
|
$express = " ";
|
|
|
|
if($address->phone != "")
|
|
$telephone = strtoupper($address->phone);
|
|
else
|
|
$telephone = strtoupper(utf8_encode($address->phone_mobile));
|
|
|
|
$INSTPRP = "";
|
|
$LIVCOMM = "";
|
|
|
|
if((int) $good_order->gift == 1)
|
|
{
|
|
$INSTPRP ="CADEAU";
|
|
$LIVCOMM = str_pad(wd_remove_accents($good_order->gift_message), 100, " ", STR_PAD_RIGHT);
|
|
$LIVCOMM = str_replace('<br/>','
',$LIVCOMM);
|
|
$LIVCOMM = str_replace('<br />','
',$LIVCOMM);
|
|
$LIVCOMM = str_replace('<br>','
',$LIVCOMM);
|
|
$LIVCOMM = str_replace('\n','
',$LIVCOMM);
|
|
|
|
$LIVCOMM=str_replace(';',' ',$LIVCOMM);
|
|
$LIVCOMM=str_replace('|',' ',$LIVCOMM);
|
|
$LIVCOMM=str_replace('*',' ',$LIVCOMM);
|
|
|
|
$LIVCOMM = str_replace('
|
|
','
',$LIVCOMM);
|
|
$LIVCOMM = str_replace('\r','
',$LIVCOMM);
|
|
$LIVCOMM = str_replace("\'","'",$LIVCOMM);
|
|
$LIVCOMM = str_replace('
|
|
','
',$LIVCOMM);
|
|
}
|
|
|
|
$invoice_date = str_replace(' ','T',$good_order->invoice_date);
|
|
|
|
|
|
if(preg_match('#MONDIAL RELAY#',$carrier_name)
|
|
&& (!isset($MondialRelay) || $MondialRelay == ''))
|
|
{
|
|
$ret['no_mr_orders'][] = $good_order;
|
|
}
|
|
else
|
|
{
|
|
|
|
$txt.='
|
|
<ORDER>
|
|
<!-- Entete de commande -->
|
|
<CODACTI>GAR</CODACTI>
|
|
<LIBACTI>Garancia-GAR</LIBACTI>
|
|
<IDORDER>'.$good_order->id.$dateAnnneMois.(isset($lettre) ? $lettre : '').'</IDORDER>
|
|
<DATECDE>'.$invoice_date.'</DATECDE>
|
|
<!-- Donnees de livraison -->
|
|
<LIVRNOM>'.str_replace('&','&',$nom).'</LIVRNOM>
|
|
<LIVPNOM>'.str_replace('&','&',$prenom).'</LIVPNOM>
|
|
<LIVSCTE>'.str_replace('&','&',$company).'</LIVSCTE>
|
|
<LIVADR1>'.substr(str_replace('&','&',$address1), 0, 35).'</LIVADR1>
|
|
<LIVADR2>'.str_replace('&','&',$address2).'</LIVADR2>
|
|
<LIVADR3>'.str_replace('&','&',(isset($instructions) ? $instructions : '')).'</LIVADR3>
|
|
<LIVCPOS>'.$codpost.'</LIVCPOS>
|
|
<LIVVILL>'.str_replace('&','&',utf8_encode($ville)).'</LIVVILL>
|
|
<LIVPAYS>'.$isoPays.'</LIVPAYS>
|
|
<LIVRTEL>'.$telephone.'</LIVRTEL>
|
|
<LIVMAIL>'.$mail.'</LIVMAIL>
|
|
<LIVCOMM>'.$LIVCOMM.'</LIVCOMM>
|
|
<!--Nom du transporteur-->
|
|
<TRPGEST>'.str_replace('&','&',$carrier_name).'</TRPGEST>
|
|
<!-- Point relais -->
|
|
<IDPOINT>'.(isset($MondialRelay) ? $MondialRelay : '').'</IDPOINT>
|
|
<!-- cadeau -->
|
|
<INSTPRP>'.$INSTPRP.'</INSTPRP>
|
|
';
|
|
|
|
|
|
$order_details = OrderDetail::getList((int) $good_order->id);
|
|
|
|
l4_anta_log('Detail commande recupere');
|
|
|
|
$ligne=0;
|
|
foreach ($order_details as $order_detail)
|
|
{
|
|
if((int)$order_detail['product_id'] == 80 || (int)$order_detail['product_id'] == 81 || (int)$order_detail['product_id'] == 82) {
|
|
continue;
|
|
}
|
|
|
|
if (isset($id_products_to_replace[$order_detail['product_id']]) &&
|
|
is_array($id_products_to_replace[$order_detail['product_id']])){
|
|
|
|
foreach($id_products_to_replace[$order_detail['product_id']] as $id_product) {
|
|
$temp_product = new Product((int) $id_product);
|
|
|
|
if(!$temp_product->is_virtual == 1)
|
|
{
|
|
if((int)$temp_product->id == 80
|
|
|| (int)$temp_product->id == 81
|
|
|| (int)$temp_product->id == 82
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$ligne++;
|
|
$product_reference=$temp_product->reference;
|
|
$product_name=$temp_product->name[$id_lang];
|
|
$product_name=str_replace('&','&',$product_name);
|
|
$qty=$order_detail['product_quantity'];
|
|
l4_anta_log(' Ref: '.$product_reference.' - Nom : '.$product_name.' Qty : '.$qty);
|
|
$txt.='
|
|
<LIGNE>
|
|
<IDLIGNE>'.$ligne.'</IDLIGNE>
|
|
<CODARTI>'.$product_reference.'</CODARTI>
|
|
<QTTECDE>'.$qty.'</QTTECDE>
|
|
<DESLANG>'.iconv("UTF-8", "ISO-8859-1",wd_remove_accents($product_name)).'</DESLANG>
|
|
</LIGNE>';
|
|
}
|
|
}
|
|
|
|
}
|
|
else {
|
|
// $order_detail_list non defini ici !!!
|
|
// $temp_product = new Product((int) $order_detail_list['product_id']);
|
|
$temp_product = new Product((int) $order_detail['product_id']);
|
|
if(!$temp_product->is_virtual == 1)
|
|
{
|
|
if((int)$temp_product->id == 80 || (int)$temp_product->id == 81 || (int)$temp_product->id == 82) {
|
|
continue;
|
|
}
|
|
|
|
$ligne++;
|
|
$product_reference=$order_detail['product_reference'];
|
|
$product_name=$order_detail['product_name'];
|
|
$product_name=str_replace('&','&',$product_name);
|
|
$qty=$order_detail['product_quantity'];
|
|
l4_anta_log(' Ref: '.$product_reference.' - Nom : '.$product_name.' Qty : '.$qty);
|
|
$txt.='
|
|
<LIGNE>
|
|
<IDLIGNE>'.$ligne.'</IDLIGNE>
|
|
<CODARTI>'.$product_reference.'</CODARTI>
|
|
<QTTECDE>'.$qty.'</QTTECDE>
|
|
<DESLANG>'.iconv("UTF-8", "ISO-8859-1", wd_remove_accents($product_name)).'</DESLANG>
|
|
</LIGNE>';
|
|
}
|
|
}
|
|
}
|
|
|
|
$txt.="
|
|
</ORDER>";
|
|
}
|
|
unset($product_reference);
|
|
unset($product_name);
|
|
unset($qty);
|
|
unset($invoice_date);
|
|
unset($instructions);
|
|
unset($reference);
|
|
unset($nom);
|
|
unset($prenom);
|
|
unset($company);
|
|
unset($address1);
|
|
unset($address2);
|
|
unset($codpost);
|
|
unset($ville);
|
|
unset($pays);
|
|
unset($isoPays);
|
|
unset($Transporteur);
|
|
unset($express);
|
|
unset($telephone);
|
|
unset($LIVCOMM);
|
|
unset($MondialRelay);
|
|
unset($INSTPRP);
|
|
unset($carrier_name);
|
|
}
|
|
|
|
$txt.="
|
|
</ORDERS>
|
|
";
|
|
|
|
/*if(count($ret['no_mr_orders']) > 0)
|
|
sendNotification($ret['no_mr_orders']);*/
|
|
|
|
$ret['xml'] = $txt;
|
|
$ret['good_orders'] = $good_orders;
|
|
return $ret;
|
|
}
|
|
|
|
function l4_sendXML($txt)
|
|
{
|
|
if($txt!='')
|
|
{
|
|
$file = "CMDCLI".date('YmdHi')."00.xml";
|
|
$ar=fopen('archives/'.$file,'w') or die("Probleme de création");
|
|
fputs($ar,$txt);
|
|
fclose($ar);
|
|
/* FTP */
|
|
|
|
$fileFTP = "CMDCLI".date('YmdHi')."00.xml";
|
|
$serveur_FTP = "ftp.l4-logistics.fr";
|
|
$id_con = ftp_connect($serveur_FTP);
|
|
|
|
$ftp_USER = "Gar_log";
|
|
$ftp_PASSWORD = "ZAnP49*!";
|
|
|
|
$ftp_dossier_local = 'archives/';
|
|
$ftp_dossier_serveur = "/IN/";
|
|
$result_login = ftp_login($id_con, $ftp_USER, $ftp_PASSWORD);
|
|
|
|
if(ftp_put($id_con, $ftp_dossier_serveur.$fileFTP, $ftp_dossier_local.$file, FTP_BINARY))
|
|
l4_anta_log('OK = Le fichier '.$file.' a bien ete ajoute sur le FTP');
|
|
else
|
|
l4_anta_log('FAIL = Le fichier '.$file.' n\'a pas été ajoute sur le FTP');
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function l4_changeOrderState($good_orders)
|
|
{
|
|
foreach ($good_orders as $good_order)
|
|
{
|
|
$history = new OrderHistory();
|
|
$history->id_order = $good_order->id;
|
|
$history->id_employee = 7; //Specific employee for L4 Logistics
|
|
|
|
if((int) $good_order->current_state != 3)
|
|
{
|
|
$history->changeIdOrderState2(3, (int) $good_order->id, false);
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Changing state to state # 3');
|
|
|
|
if($history->addWithemail(true))
|
|
l4_anta_log('History email sent.');
|
|
else
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Cannot add history with mail');
|
|
}
|
|
else
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Wrong Order State != 3');
|
|
}
|
|
}
|
|
|
|
function l4_changeOrderStateNoMR($no_mr_orders)
|
|
{
|
|
foreach ($no_mr_orders as $order)
|
|
{
|
|
$history = new OrderHistory();
|
|
$history->id_order = $good_order->id;
|
|
$history->id_employee = 7; //Specific employee for L4 Logistics
|
|
|
|
if((int) $good_order->current_state != 3
|
|
&& (int) $good_order->current_state != 19 )
|
|
{
|
|
$history->changeIdOrderState2(19, (int) $good_order->id, false);
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Changing state to state # 19 - No Mondial Relay Point available');
|
|
|
|
if($history->add())
|
|
l4_anta_log('History added.');
|
|
else
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Cannot add history');
|
|
}
|
|
else
|
|
l4_anta_log('ORDER # '.$good_order->id.' - Wrong Order State != 3 && Wrong Order State != 19');
|
|
}
|
|
}
|
|
|
|
function l4_anta_log($msg='')
|
|
{
|
|
$file = dirname(__FILE__).'/logs/log_commandes-entre-12h-5h_'.date("d-m-Y_H-i").'.txt';
|
|
|
|
if($fp = fopen($file, 'a+') )
|
|
{
|
|
fseek($fp, SEEK_END);
|
|
|
|
if($msg == '')
|
|
fputs($fp,''."\n");
|
|
else
|
|
fputs($fp, '['.date('d/m/y - h:i:s'). '] # '.$_SERVER['REMOTE_ADDR'].' # '.$msg."\n");
|
|
}
|
|
}
|
|
|
|
|
|
function wd_remove_accents($str, $charset='utf-8')
|
|
{
|
|
$str = preg_replace('#&#', 'et', $str);
|
|
$str = preg_replace('#&#', 'et', $str);
|
|
$str = preg_replace('#œ#', 'oe', $str);
|
|
$str = preg_replace('#\r\n#', ' ', $str);
|
|
$str = htmlentities($str, ENT_NOQUOTES, $charset);
|
|
|
|
$str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml|reg);#', '\1', $str);
|
|
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. 'œ'
|
|
$str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
|
|
|
|
return $str;
|
|
}
|
|
|
|
function formatAdress($address)
|
|
{
|
|
if($address->address1 && $address->address2)
|
|
{
|
|
$line_address = trim(wd_remove_accents($address->address1.' '.$address->address2));
|
|
$ant_adress = truncate($line_address, 35);
|
|
}
|
|
elseif(strlen($address->address1) > 35)
|
|
{
|
|
$line_address = trim(wd_remove_accents($address->address1));
|
|
$ant_adress = truncate($line_address, 35);
|
|
}
|
|
else
|
|
{
|
|
$adr[0] = wd_remove_accents($address->address1);
|
|
$adr[1] = $adr[2] = $adr[3] = '';
|
|
}
|
|
|
|
$count_adr_array = 0;
|
|
if(isset($ant_adress))
|
|
{
|
|
if(is_array($ant_adress))
|
|
{
|
|
foreach ($ant_adress as $adr_line)
|
|
{
|
|
$adr[$count_adr_array] = $adr_line;
|
|
$count_adr_array++;
|
|
}
|
|
}
|
|
else
|
|
$adr[0] = $ant_adress;
|
|
|
|
unset($ant_adress);
|
|
}
|
|
|
|
$adr_tab_a = array_values($adr);
|
|
$adr_tab = array_values($adr_tab_a);
|
|
|
|
return $adr_tab;
|
|
}
|
|
|
|
function truncate($string, $length)
|
|
{
|
|
if(strlen($string) > $length)
|
|
{
|
|
$string_work = $string;
|
|
|
|
$i=0;
|
|
while(strlen($string_work) > $length)
|
|
{
|
|
$space_place = strripos(substr($string_work, 0,35) , ' ');
|
|
$adr[$i] = trim(substr($string_work, 0, $space_place));
|
|
$string_work = trim(str_replace($adr[$i], '', $string_work));
|
|
$i++;
|
|
}
|
|
$adr[$i+1] = trim($string_work);
|
|
}
|
|
else
|
|
$adr = trim($string);
|
|
return $adr;
|
|
}
|
|
|
|
// function sendNotification($msg)
|
|
// {
|
|
// $message = 'Bonjour'.PHP_EOL;
|
|
// $message .= 'Voici la liste des commandes qui n\'ont pas été exportée pour cause de point relay manquant :'.PHP_EOL;
|
|
// $message .= PHP_EOL;
|
|
// $message .= (is_array($msg)) ? implode(', ', $msg) : $msg;
|
|
// $message .= PHP_EOL;
|
|
// $message .= 'Ces commandes seront exportée lors de l\'export suivant la modification des informations de livraison.'.PHP_EOL;
|
|
// $message .= PHP_EOL;
|
|
|
|
// error_log($message, 1, 'beaumont@antadis.com');
|
|
// return true;
|
|
// //error_log($message, 1, 'a-khalil@garancia-beauty.com');
|
|
// //error_log($message, 1, 'c-allibert@garancia-beauty.com');
|
|
// }
|
|
?>
|