garancia/L4/commandes-entre-5h-12h.php.bak

334 lines
9.9 KiB
PHP

<?php
$conn = mysql_connect('localhost', 'garancia_prod', 'yoeLB4Znn1E1');
mysql_select_db('garancia_prod');
$dateAnnneMois=date('Ymd');
$dayNOW=date('Y-m-d');
$dayNOW_INF=date('Y-m-d')." 5:00:00";
$dayNOW_SUP=date('Y-m-d')." 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 >= '".$dayNOW_INF."'
and invoice_date <= '".$dayNOW_SUP."'
and valid = 1 ";
$res = mysql_query($sql, $conn);
$results=array();
$i=0;
while($row = mysql_fetch_array($res))
{
$sql='SELECT * from ps_order_history oh where id_order='.$row['id_order'].' order by id_order_history desc limit 0,1';
$result = mysql_query($sql, $conn);
$id_order_state = mysql_fetch_array($result);
if((int) $id_order_state['id_order_state'] == 13 || (int) $id_order_state['id_order_state'] == 12 || (int) $id_order_state['id_order_state'] == 2 )
{
$results[]=$row;
$i++;
}
}
echo '
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />';
echo "<fieldset class='debug'>";
echo "SQL : ".$sql. "<br/>";
echo $i.' commandes trouvés pour la période : invoice_date >= "'.$dayNOW_INF.'" and invoice_date <= "'.$dayNOW_SUP.'"';
echo "</fieldset>";
$txt = '<?xml version="1.0" encoding="UTF-8"?>
<ORDERS>';
$i=0;
foreach($results as $row)
{
$carrier_name=$row['carrier_name'];
$carrier_name=utf8_encode($carrier_name);
if($carrier_name=="Mondial Relay")
$carrier_name='MONDIAL RELAY 24R GAR';
elseif($carrier_name=="Livraison Colissimo")
$carrier_name='COLISSIMO EXPERT FR GAR';
elseif($carrier_name=="UPS")
$carrier_name='UPS SAVER GAR';
elseif($carrier_name=="IMX")
$carrier_name='IMX GAR';
else
$carrier_name='IMX GAR';
$sql = "SELECT * from ps_address where id_address = '".$row['id_address_delivery']."'";
$resultAdress = mysql_query($sql, $conn);
$rowAdress = mysql_fetch_array($resultAdress);
$sql = "SELECT * from ps_customer where id_customer = '".$row['id_customer']."'";
$resultClient = mysql_query($sql, $conn);
$rowClient = mysql_fetch_array($resultClient);
$MondialRelay="";
$sql = "SELECT MR_Selected_Pays,MR_Selected_Num FROM `ps_mr_selected` where id_order=".$row['id_order'];
$MR = mysql_query($sql, $conn);
if($MondialR = mysql_fetch_array($MR))
$MondialRelay=$MondialR['MR_Selected_Pays'].$MondialR['MR_Selected_Num'];
$sql = "SELECT * from ps_country where id_country = '".$rowAdress['id_country']."'";
$resultPays = mysql_query($sql, $conn);
$rowPays = mysql_fetch_array($resultPays);
$reference=strtoupper($row['id_order']);
$nom=strtoupper(utf8_encode(($rowAdress['firstname'])));
$prenom=strtoupper(utf8_encode(($rowAdress['lastname'])));
$company=strtoupper(utf8_encode(($rowAdress['company'])));
$address1=str_pad(utf8_encode($rowAdress['address1']), 35, " ", STR_PAD_RIGHT);
$address2=str_pad(utf8_encode($rowAdress['address2']), 35, " ", STR_PAD_RIGHT);
$codpost=strtoupper(utf8_encode($rowAdress['postcode']));
$ville=$rowAdress['city'];
$pays=strtoupper(utf8_encode($rowPays['country']));
$isoPays=strtoupper($rowPays['iso_code']);
$transporteur=strtoupper(" ");
$express=" ";
if($rowAdress['phone']!="")
$telephone=strtoupper($rowAdress['phone']);
else
$telephone=strtoupper(utf8_encode($rowAdress['phone_mobile']));
$INSTPRP="";
$LIVCOMM="";
if($row['gift']==1)
{
$INSTPRP="CADEAU";
$LIVCOMM=str_pad(htmlspecialchars(utf8_encode($row['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);
}
// $LIVCOMM=stripslashes($LIVCOMM);
$invoice_date=str_replace(' ','T',$row['invoice_date']);
$txt.='
<ORDER>
<!-- Entête de commande -->
<CODACTI>GAR</CODACTI>
<LIBACTI>Garancia-GAR</LIBACTI>
<IDORDER>'.$row['id_order'].$dateAnnneMois.$lettre.'</IDORDER>
<DATECDE>'.$invoice_date.'</DATECDE>
<!-- Données de livraison -->
<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;',$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-->
<TRPGEST>'.str_replace('&','&amp;',$carrier_name).'</TRPGEST>
<!-- Point relais -->
<IDPOINT>'.$MondialRelay.'</IDPOINT>
<!-- cadeau -->
<INSTPRP>'.$INSTPRP.'</INSTPRP>
';
$sql = "SELECT product_reference, product_quantity, product_ean13,product_name from ps_order_detail where id_order = '".$row['id_order']."'";
$lettre="";
$i=0;
if($resultProd = mysql_query($sql, $conn))
{
// $txt.= "<!--\n nb:".count($resultProd)."-->\n";
// print_r($rowProd);
$ligne=0;
while($rowProd = mysql_fetch_array($resultProd))
{
$ligne++;
$product_reference=$rowProd['product_reference'];
$product_name=utf8_encode($rowProd['product_name']);
$product_name=str_replace('&','&amp;',$product_name);
$qty=$rowProd['product_quantity'];
$txt.='
<LIGNE>
<IDLIGNE>'.$ligne.'</IDLIGNE>
<CODARTI>'.$product_reference.'</CODARTI>
<QTTECDE>'.$qty.'</QTTECDE>
<DESLANG>'.$product_name.'</DESLANG>
</LIGNE>';
}
}
$txt.="
</ORDER>";
$log=date('Y\-m-d')." Commande #".$row['id_order']." ajouté dans le XML \n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
}
//Log de vérification des dernières commandes
$sql_log = "
SELECT o . * , c.name AS carrier_name, oh.id_order_state
FROM ps_orders o
LEFT JOIN ps_carrier c ON ( o.id_carrier = c.id_carrier )
LEFT JOIN ps_order_history oh ON ( oh.id_order = o.id_order ) AND oh.date_add = (
SELECT max(date_add) FROM ps_order_history WHERE id_order = o.id_order
)
ORDER BY `o`.`id_order`
DESC LIMIT 0 , 30
";
$res = mysql_query($sql_log, $conn);
$log_verif = '';
while($row = mysql_fetch_array($res)) {
foreach($row as $field => $data) {
if(!is_numeric($field))
{
$log_verif .= '<strong>' . $field . '</strong> : ' . $data . '<br />';
}
}
$log_verif .= '<br />**********************************************************<br /><br />';
}
file_put_contents('logs/verif_'.date('d-m-Y-h').'.html', $log_verif);
mysql_close($conn);
$txt.="
</ORDERS>
";
if($txt!='')
{
$file = "CMDCLI".date('YmdHis').".xml";
$ar=fopen('archives/'.$file,'w') or die("Probleme de création");
fputs($ar,$txt);
fclose($ar);
$fileFTP = "CMDCLI".date('YmdHis').".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))
$log=date('Y\-m\-d')." Le fichier ".$file." a bien été ajouté dans le dossier /IN sur le FTP de L4LOGISTICS\n";
else
$log=date('Y\-m\-d')." Le fichier ".$file." n a pas été ajouté dans le ftp distant IN\n";
// $log=date('Y\-m-d')." Commande en cours de préparation :#".$row['id_order']." \n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
}
$log="TEST 1 - ".count($results)."\n\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
include(dirname(__FILE__) .'/../config/config.inc.php');
foreach($results as $row)
{
$IDORDER= $row['id_order'];
$log="TEST2 #".$IDORDER." \n\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
$history = new OrderHistory();
$log="TEST 3\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
$history->id_order = $IDORDER;
$history->id_employee = 7;
$use_existings_payment = false;
$log="TEST 4\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
$order = new Order($IDORDER);
$current_order_state=$order->current_state;
if($current_order_state!=3)
{
$history->changeIdOrderState2(3, $IDORDER, $use_existings_payment);
if ($history->addWithemail(true, $templateVars))
{
$log="TEST 5\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
// echo "<br/><br/><a href='logs/".$file3."' target='_blank'>Voir les logs</a>";
}
}
$log=date('Ymd')." Commande en cours de préparation :#".$IDORDER." \n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
}
$log="\n";
$file3 = "LOG_COMMANDES_entre_5H_12H.txt";
$ar3=fopen('logs/'.$file3,'a+');
fputs($ar3,$log);
fclose($ar3);
// ftp_close($id_con);
function anta_log($msg='')
{
$file = dirname(dirname(__FILE__)).'/logs/log_'.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");
}
}
?>