garancia/L4/commandes-spe.php

443 lines
12 KiB
PHP

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once(dirname(dirname(__FILE__)).'/config/config.inc.php');
l4_anta_log('Commandes a traiter');
$ret = l4_generateXML(l4_getOrders());
l4_sendXML($ret['xml']);
exit;
//l4_changeOrderState($ret['good_orders']);
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 id_order = 3515 OR id_order = 3535
and valid = 1 ";
/*
$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();
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;
}
}
/*
foreach ($result as $order)
$orders[] = new Order((int) $order['id_order']);
*/
return $orders;
}
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));
/* ADDRESS FORMAT */
$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);
$codpost = strtoupper(utf8_encode($address->postcode));
$ville = wd_remove_accents($address->city);
$pays = strtoupper(utf8_encode($address->country));
$isoPays = strtoupper($country->iso_code);
$Transporteur = strtoupper(" ");
$express = " ";
if($address->phone != "")
$telephone = strtoupper($address->phone);
else
$telephone = strtoupper(utf8_encode($address->phone_mobile));
$INSTPRP = "";
$LIVCOMM = "";
if((int) $order->gift == 1)
{
$INSTPRP ="CADEAU";
$LIVCOMM = str_pad(wd_remove_accents($good_order->gift_message), 100, " ", STR_PAD_RIGHT);
$LIVCOMM = str_replace('<br/>','&#xa;',$LIVCOMM);
$LIVCOMM = str_replace('<br />','&#xa;',$LIVCOMM);
$LIVCOMM = str_replace('<br>','&#xa;',$LIVCOMM);
$LIVCOMM = str_replace('\n','&#xa;',$LIVCOMM);
$LIVCOMM = str_replace('
','&#xa;',$LIVCOMM);
$LIVCOMM = str_replace('\r','&#xd;',$LIVCOMM);
$LIVCOMM = str_replace("\'","'",$LIVCOMM);
$LIVCOMM = str_replace('
','&#xd;',$LIVCOMM);
}
$invoice_date = str_replace(' ','T',$good_order->invoice_date);
$txt .= '
<ORDER>';
/* Entete commande */
$txt .= '
<CODACTI>GAR</CODACTI>
<LIBACTI>Garancia-GAR</LIBACTI>
<IDORDER>'.$good_order->id.$dateAnnneMois.(isset($lettre) ? $lettre : '').'</IDORDER>
<DATECDE>'.$invoice_date.'</DATECDE> ';
/* Donnees de livraison */
$txt .= '
<LIVRNOM>'.str_replace('&amp;','&',$nom).'</LIVRNOM>
<LIVPNOM>'.str_replace('&amp;','&',$prenom).'</LIVPNOM>
<LIVSCTE>'.str_replace('&amp;','&',$company).'</LIVSCTE>
<LIVADR1>'.str_replace('&amp;','&',$address1).'</LIVADR1>
<LIVADR2>'.str_replace('&amp;','&',$address2).'</LIVADR2>
<LIVADR3>'.str_replace('&amp;','&',(isset($instructions) ? $instructions : '')).'</LIVADR3>
<LIVCPOS>'.$codpost.'</LIVCPOS>
<LIVVILL>'.str_replace('&amp;','&',utf8_encode($ville)).'</LIVVILL>
<LIVPAYS>'.$isoPays.'</LIVPAYS>
<LIVRTEL>'.$telephone.'</LIVRTEL>
<LIVMAIL></LIVMAIL>
<LIVCOMM>'.$LIVCOMM.'</LIVCOMM>';
/* Nom du transporteur */
$txt .= '
<TRPGEST>'.str_replace('&amp;','&',$carrier_name).'</TRPGEST>';
/* Point relais */
$txt .= '
<IDPOINT>'.(isset($MondialRelay) ? $MondialRelay : '').'</IDPOINT>';
/* Caddeau */
$txt .= '
<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)
{
$temp_product = new Product((int) $order_detail_list['product_id']);
if(!$temp_product->is_virtual == 1)
{
$ligne++;
$product_reference=$order_detail['product_reference'];
$product_name=$order_detail['product_name'];
$product_name=str_replace('&amp;','&',$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>
";
$ret['xml'] = $txt;
$ret['good_orders'] = $good_orders;
/*
print_r($ret);
exit;
*/
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_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'). '] # '.$msg."\n");
}
}
function wd_remove_accents($str, $charset='utf-8')
{
$str = preg_replace('#&amp;#', '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|oelig|Oelig|aelig|Aelig);#', '\1', $str);
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;'
$str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
return $str;
}
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 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;
}
?>