executeS($sql); $orders = array(); 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 = ' '; $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('
',' ',$LIVCOMM); $LIVCOMM = str_replace('
',' ',$LIVCOMM); $LIVCOMM = str_replace('
',' ',$LIVCOMM); $LIVCOMM = str_replace('\n',' ',$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); $txt .= ' '; /* Entete commande */ $txt .= ' GAR Garancia-GAR '.$good_order->id.$dateAnnneMois.(isset($lettre) ? $lettre : '').' '.$invoice_date.' '; /* Donnees de livraison */ $txt .= ' '.str_replace('&','&',$nom).' '.str_replace('&','&',$prenom).' '.str_replace('&','&',$company).' '.str_replace('&','&',$address1).' '.str_replace('&','&',$address2).' '.str_replace('&','&',(isset($instructions) ? $instructions : '')).' '.$codpost.' '.str_replace('&','&',utf8_encode($ville)).' '.$isoPays.' '.$telephone.' '.$LIVCOMM.''; /* Nom du transporteur */ $txt .= ' '.str_replace('&','&',$carrier_name).''; /* Point relais */ $txt .= ' '.(isset($MondialRelay) ? $MondialRelay : '').''; /* Caddeau */ $txt .= ' '.$INSTPRP.''; $order_details = OrderDetail::getList((int) $good_order->id); l4_anta_log('Detail commande recupere'); $ligne=0; foreach ($order_details as $order_detail) { $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.' '.$product_reference.' '.$qty.' '.iconv("UTF-8", "ISO-8859-1",wd_remove_accents($product_name)).' '; } $txt.=" "; 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.="
"; $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_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('#&#', '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. 'œ' $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; } ?>