name = 'neolys'; $this->version = '1.0'; $this->author = 'Neolys IT'; $this->tab = 'shipping_logistics'; $this->module_key = '860c602ea93e26697a23dfe497d32433'; $this->_postErrors = array(); parent::__construct(); $this->displayName = $this->l('Neolys - Partenaire Logistique'); $this->description = $this->l('Interface PS v1.6 avec Neolys'); } protected function setConfig($key, $value) { return Configuration::updateValue($this->name.$key,$value,true); } protected function getConfig($value) { return Configuration::get($this->name.$value); } protected function deleteConfig($value) { return Configuration::deleteByName($this->name.$value); } public function install() { if (!parent::install() // OR !$this->registerHook('') OR !$this->setConfig("WS_KEY","") OR !$this->setConfig("FTP_IP","") OR !$this->setConfig("FTP_login","") OR !$this->setConfig("FTP_password","") OR !$this->setConfig("order_flag","") OR !$this->setConfig("order_directory","") OR !$this->setConfig("article_flag","") OR !$this->setConfig("article_directory","") OR !$this->setConfig("statuts_flag","") OR !$this->setConfig("stock_flag","") OR !$this->setConfig("stock_directory","") ) return false; return true; } public function uninstall() { $this->deleteConfig("WS_KEY"); $this->deleteConfig("FTP_IP"); $this->deleteConfig("FTP_login"); $this->deleteConfig("FTP_password"); $this->deleteConfig("order_flag"); $this->deleteConfig("order_directory"); $this->deleteConfig("article_flag"); $this->deleteConfig("article_directory"); $this->deleteConfig("statuts_flag"); $this->deleteConfig("stock_flag"); $this->deleteConfig("stock_directory"); parent::uninstall(); return true; } //public function hook($params) // { // } private function _postProcess() { $this->setConfig("WS_KEY", Tools::getValue("WS_KEY")); $this->setConfig("FTP_IP", Tools::getValue("FTP_IP")); $this->setConfig("FTP_login", Tools::getValue("FTP_login")); $this->setConfig("FTP_password", Tools::getValue("FTP_password")); $this->setConfig("order_flag", Tools::getValue("order_flag")); $this->setConfig("order_directory", './ecom_to_neolys/ordres/received/'); $this->setConfig("article_flag", Tools::getValue("article_flag")); $this->setConfig("article_directory", './ecom_to_neolys/articles/received/'); $this->setConfig("statuts_flag", Tools::getValue("statuts_flag")); $this->setConfig("stock_flag", Tools::getValue("stock_flag")); $this->setConfig("stock_directory", './neolys_to_ecom/stock/'); if(Tools::getValue("force_articles")== 'YES') {$cart = new neolys; $cart->getContentArticles(); } if(Tools::getValue("force_orders")== 'YES') {$cart = new neolys; $cart->getContentCommandes(); } if(Tools::getValue("force_stock")== 'YES') {$cart = new neolys; $cart->updateStock(); } if(Tools::getValue("force_statuts")== 'YES') {$cart = new neolys; $cart->updateStatuts(); } Tools::redirectAdmin('?tab=AdminModules&configure=neolys&token='.Tools::getValue('token')); } public function getContent() { $this->_html .= "

".$this->displayName."

"; if (Tools::isSubmit("submit")) { $this->_postProcess(); } $this->_displayForm(); return $this->_html; } private function _displayForm() { $this->_html .= '
'.$this->l('Connexion Neolys').'
'; $this->_html .= '
'; $this->_html .= '
'.$this->l('Envoi des Commandes').'
'; if( $this->getConfig("order_flag") == 1 ) {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } else {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } $this->_html .= '

'.$this->getConfig("order_directory").'

'; $this->_html .= '
'; $this->_html .= '
'.$this->l('Envoi de la base article').'
'; if( $this->getConfig("article_flag") == 1 ) {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } else {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } $this->_html .= '

'.$this->getConfig("article_directory").'

'; $this->_html .= '
'; $this->_html .= '
'.$this->l('Mise à jour du stock').'
'; if( $this->getConfig("stock_flag") == 1 ) {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } else {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } $this->_html .= '

'.$this->getConfig("stock_directory").'

'; $this->_html .= '
'; $this->_html .= '
'.$this->l('Mise à jour des statuts des commandes').'
'; if( $this->getConfig("statuts_flag") == 1 ) {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } else {$this->_html .= ' '; $this->_html .= ' Activé
'; $this->_html .= ' '; $this->_html .= ' Désactivé '; } $this->_html .= '

'; $this->_html .= '
'; $this->_html .= '

'; } public function getcategoryname($id_category) { $id_lang = $this->getidLang4Neolys() ; $sql_category = "select cl.name AS name, cl.description AS descr from "._DB_PREFIX_."category_lang cl where cl.id_category = '$id_category' and cl.id_lang = '$id_lang'"; //and cl.link_rewrite <> 'home' "; $row_category = Db::getInstance()->getRow($sql_category); $value = $row_category['name']; return($value); } public function getrootcategory($id_category) { $root_level = 1 ; $sql_category = "select level_depth , id_parent from "._DB_PREFIX_."category where id_category = '$id_category' "; $row_category = Db::getInstance()->getRow($sql_category); $level_depth = $row_category['level_depth']; $id_father = $row_category['id_parent']; if($level_depth == $root_level){$root_category = $id_category ;} if($level_depth > $root_level) { for($i=$level_depth;$i>1;$i--) { $sql_category_2 = "select level_depth , id_parent from "._DB_PREFIX_."category where id_category = '$id_father' "; $row_category_2 = Db::getInstance()->getRow($sql_category_2); $level_depth_2 = $row_category_2['level_depth']; if($level_depth_2 == $root_level){$root_category = $id_father ;} $id_father = $row_category_2['id_parent']; } } return($root_category); } public function getcategoriesproduct($id_product) { $stack = array(); $id_lang = $this->getidLang4Neolys() ; $sql_categories = "select cl.name AS name, cl.description AS descr from "._DB_PREFIX_."category_product cp , "._DB_PREFIX_."category_lang cl where cp.id_product = '$id_product' and cp.id_category = cl.id_category and cl.id_lang = '$id_lang' and cl.link_rewrite <> 'home' "; $LIST_CATEGORIES = Db::getInstance()->Query($sql_categories); while($category = Db::getInstance()->nextRow($LIST_CATEGORIES)) { $c_name = $category['name']; array_push($stack,$c_name); } return($stack); } public function getgroupattributename($id_attribute, $id_lang) { $sql = "SELECT pagl.public_name AS PUBLIC_NAME FROM "._DB_PREFIX_."attribute pa , "._DB_PREFIX_."attribute_group_lang pagl where pagl.id_lang = '$id_lang' and pa.id_attribute_group = pagl.id_attribute_group and pa.id_attribute = '$id_attribute' "; $row_attr_group = Db::getInstance()->getRow($sql); $value = $row_attr_group['PUBLIC_NAME']; return($value); } public function getidLang4Neolys() { $sql = "SELECT "._DB_PREFIX_."lang.id_lang FROM "._DB_PREFIX_."lang where iso_code = 'fr' "; $row_lang = Db::getInstance()->getRow($sql); $id_lang = $row_lang['id_lang']; return($id_lang); } public function getordermessages($id_order, $id_customer) { $stack = array(); $sql_mes = "select * from "._DB_PREFIX_."message where id_order = '$id_order' and private ='0' and id_customer = '$id_customer' "; $list_mes = Db::getInstance()->Query($sql_mes); while($messages = Db::getInstance()->nextRow($list_mes)) { $message = $messages['message'] ; array_push($stack,$message); } return($stack); } public function getshippingdetails($id_cart) { $stack = array(); $sql_so = "select * from "._DB_PREFIX_."socolissimo_delivery_info where id_cart = '$id_cart' "; $details_so = Db::getInstance()->Execute($sql_so); $lines_so = Db::getInstance()->numRows($details_so); if ($lines_so > 0) { array_push($stack,'SOCOL'); $row_so = Db::getInstance()->getRow($sql_so); $data1 = $row_so['delivery_mode']; array_push($stack,$data1); $data2 = $row_so['prid']; array_push($stack,$data2); $data3 = $row_so['cephonenumber']; array_push($stack,$data3); $data4 = $row_so['ceemail']; array_push($stack,$data4); $data5 = $row_so['cecompanyname']; array_push($stack,$data5); $data6 = $row_so['cedeliveryinformation']; array_push($stack,$data6); $data7 = $row_so['cedoorcode1']; array_push($stack,$data7); $data8 = $row_so['cedoorcode2']; array_push($stack,$data8); $data9 = $row_so['prname']; array_push($stack,$data9); $data10 = $row_so['prfirstname']; array_push($stack,$data10); $data11 = $row_so['przipcode']; array_push($stack,$data11); $data12 = $row_so['prtown']; array_push($stack,$data12); } $sql_mr = "select * from "._DB_PREFIX_."mr_selected where id_cart = '$id_cart' "; $details_mr = Db::getInstance()->Execute($sql_mr); $lines_mr = Db::getInstance()->numRows($details_mr); if ($lines_mr > 0) { array_push($stack,'MR'); $row_mr = Db::getInstance()->getRow($sql_mr); $data1 = ''; array_push($stack,$data1); $data2 = $row_mr['MR_Selected_Num']; array_push($stack,$data2); $data3 = ''; array_push($stack,$data3); $data4 = ''; array_push($stack,$data4); $data5 = ''; array_push($stack,$data5); $data6 = ''; array_push($stack,$data6); $data7 = ''; array_push($stack,$data7); $data8 = ''; array_push($stack,$data8); } return($stack); } public function getContentArticles() { if($this->getConfig("article_flag")=='1') { $id_lang = $this->getidLang4Neolys() ; //$URL_SITE = "http://" . Configuration::get('CANONICAL_URL') ; $DATE_LOG = date("Ymd H:i:s"); $log_articles = "Neolys Articles | ".$DATE_LOG." | "; $DATE = date("YmdHis"); $FICHIER = "base_articles_". $DATE .".xml"; $PATH = dirname(__FILE__) . "/data/" ; $FICHIER_A_ENVOYER = $PATH . $FICHIER ; $f = fopen($FICHIER_A_ENVOYER,"w"); //DEBUT DU FICHIER $entetxml = '' ; $entetxml .= "\n" ; $entetxml .= '' ; $entetxml .= "\n" ; fputs($f,$entetxml); $SQL_PRODUITS = 'SELECT '._DB_PREFIX_.'product.id_product , '._DB_PREFIX_.'product.active as PRODUCT_ACTIVE, '._DB_PREFIX_.'product.reference , '._DB_PREFIX_.'product.supplier_reference , '._DB_PREFIX_.'product.ean13 , '._DB_PREFIX_.'product.weight , '._DB_PREFIX_.'product_lang.link_rewrite , '._DB_PREFIX_.'product.quantity , '._DB_PREFIX_.'supplier.name as namefournisseur, '._DB_PREFIX_.'product.is_virtual as virtuel, '._DB_PREFIX_.'product_lang.name as nameproduct , '._DB_PREFIX_.'product.id_category_default as id_cat_default, '._DB_PREFIX_.'product_lang.link_rewrite FROM '._DB_PREFIX_.'product LEFT JOIN '._DB_PREFIX_.'product_lang ON '._DB_PREFIX_.'product_lang.id_product = '._DB_PREFIX_.'product.id_product LEFT JOIN '._DB_PREFIX_.'supplier ON '._DB_PREFIX_.'supplier.id_supplier = '._DB_PREFIX_.'product.id_supplier WHERE '._DB_PREFIX_.'product_lang.id_lang = '.$id_lang.' group by '._DB_PREFIX_.'product.id_product '; $LIST_PRODUITS = Db::getInstance()->Query($SQL_PRODUITS); $NOMBRELIGNE_PRODUITS = Db::getInstance()->numRows($LIST_PRODUITS); $articlesxml = "\n" ; fputs($f,$articlesxml); while($produit = Db::getInstance()->nextRow($LIST_PRODUITS)) { $id_product = $produit['id_product'] ; $SQL_IMAGE = 'SELECT '._DB_PREFIX_.'image.id_image FROM '._DB_PREFIX_.'image where '._DB_PREFIX_.'image.id_product = '.$produit['id_product'].' order by '._DB_PREFIX_.'image.cover desc'; //$LIST_IMAGES = Db::getInstance()->Query($SQL_IMAGE); //echo $SQL_IMAGE . "\n" ; $row_id_image = Db::getInstance()->getRow($SQL_IMAGE); $id_image = $row_id_image['id_image']; if($id_image == '') { $url = ''; } else { $url = Link::getImageLink($produit['link_rewrite'], $produit['id_product'].'-'.$id_image, 'home'); } $default_category = $produit['id_cat_default'] ; $debutproduitxml = "\n\n\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "getcategoryname($default_category) . "]]>\n" ; $debutproduitxml .= "getrootcategory($default_category) . " - " . $this->getcategoryname($this->getrootcategory($default_category)) . "]]>\n" ; $debutproduitxml .= "\n" ; $categories = $this->getcategoriesproduct($id_product); foreach($categories as $category) { $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; } $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $sql_pack = "select * from "._DB_PREFIX_."pack where id_product_pack = '$id_product' order by id_product_item asc "; $LIST_PACK = Db::getInstance()->Query($sql_pack); while($pack = Db::getInstance()->nextRow($LIST_PACK)) { $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; } $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; fputs($f,$debutproduitxml); $SQL_ATTRIBUT_PRODUITS = 'SELECT * FROM '._DB_PREFIX_.'product_attribute pa WHERE pa.id_product ='.$produit['id_product'] ; $LIST_ATTRIBUT_PRODUITS = Db::getInstance()->Query($SQL_ATTRIBUT_PRODUITS); // $NOMBRE_ATTRIBUT_PRODUITS = Db::getInstance()->numRows($LIST_ATTRIBUT_PRODUITS); while($attribut_produit = Db::getInstance()->nextRow($LIST_ATTRIBUT_PRODUITS)) { //DEBUT D UN PRODUIT $SQL_ATTRIBUT_NAME = 'SELECT al.name AS NAME_ATTR , pac.id_attribute AS ID_ATTR FROM '._DB_PREFIX_.'product_attribute_combination pac , '._DB_PREFIX_.'attribute_lang al where al.id_attribute = pac.id_attribute and al.id_lang = '.$id_lang.' and pac.id_product_attribute = '.$attribut_produit['id_product_attribute'] ; $LIST_ATTRIBUT_NAME = Db::getInstance()->Query($SQL_ATTRIBUT_NAME); $name_attribut = ''; while($attribut_name = Db::getInstance()->nextRow($LIST_ATTRIBUT_NAME)) { $id_attribute_4_group = $attribut_name['ID_ATTR']; if($name_attribut == ''){$name_attribut .= $this->getgroupattributename($id_attribute_4_group,$id_lang)." : ".$attribut_name['NAME_ATTR'];} else{$name_attribut .= " - ".$this->getgroupattributename($id_attribute_4_group,$id_lang)." : ".$attribut_name['NAME_ATTR'];} } $SQL_IMAGE_ATTR = 'SELECT '._DB_PREFIX_.'product_attribute_image.id_image FROM '._DB_PREFIX_.'product_attribute_image where '._DB_PREFIX_.'product_attribute_image.id_product_attribute = '.$attribut_produit['id_product_attribute'] ; $LIST_IMAGES_ATTR = Db::getInstance()->Query($SQL_IMAGE_ATTR); $NB_LIST_IMAGES_ATTR = Db::getInstance()->numRows($LIST_IMAGES_ATTR); if($NB_LIST_IMAGES_ATTR == '0') { $url_attr = ''; } else { $row_id_image_attr = Db::getInstance()->getRow($SQL_IMAGE_ATTR); $id_image_attr = $row_id_image_attr['id_image']; $url_attr = $id_image_attr; $url_attr = Link::getImageLink($produit['link_rewrite'], $produit['id_product'].'-'.$id_image_attr, 'home'); } $debutproduitxml = "\n\n\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "getcategoryname($default_category) . "]]>\n" ; $debutproduitxml .= "getrootcategory($default_category) . " - " . $this->getcategoryname($this->getrootcategory($default_category)) . "]]>\n" ; $debutproduitxml .= "\n" ; $debutproduitxml .= "\n" ; fputs($f,$debutproduitxml); } } $entetpackxml = "\n" ; fputs($f,$entetpackxml); $finxml = "\n" ; fputs($f,$finxml); fclose($f); $log_articles .= "Nb articles : ".$NOMBRELIGNE_PRODUITS." | " ; // Création de la connexion $conn_id = ftp_connect($this->getConfig("FTP_IP")); // Authentification avec nom de compte et mot de passe $login_result = ftp_login($conn_id, $this->getConfig("FTP_login"), $this->getConfig("FTP_password")); //mode passif ftp_pasv($conn_id,1); // Vérification de la connexion if ((!$conn_id) || (!$login_result)) { $log_articles .= "Connexion FTP KO "; } else { $log_articles .= "Connexion FTP OK "; // Passage ds le répertoire DES ARTICLES ftp_chdir($conn_id, $this->getConfig("article_directory")); // Liste les fichiers du répertoire if( ftp_put($conn_id, $FICHIER, $FICHIER_A_ENVOYER, FTP_BINARY) ) { $log_articles .= " | Transfert fichier ".$FICHIER; } else { $log_articles .= " Transfert fichier KO"; } } unlink($FICHIER_A_ENVOYER); echo "Log : ".$log_articles; Logger::addLog($log_articles, 1, 101, 'Logging object'); // Fermeture de la connexion FTP. ftp_quit($conn_id); } } ///////////////////////////// //////// ENVOI DES COMMANDES ///////////////////////////// public function getContentCommandes() { if($this->getConfig("order_flag")=='1') { $DATE_LOG = date("Ymd H:i:s"); // Création de la connexion $conn_id = ftp_connect($this->getConfig("FTP_IP")); // Authentification avec nom de compte et mot de passe $login_result = ftp_login($conn_id, $this->getConfig("FTP_login"), $this->getConfig("FTP_password")); //mode passif ftp_pasv($conn_id,1); // Vérification de la connexion if ((!$conn_id) || (!$login_result)) { $log_orders = "Neolys Commandes | ".$DATE_LOG." | Connexion FTP KO "; } else { // DONNE LE NUMERO DE COMMANDE $SQL_COMMANDES = 'SELECT '._DB_PREFIX_.'order_history.id_order, MAX( '._DB_PREFIX_.'order_history.id_order_state ) AS DernierEtat, '._DB_PREFIX_.'orders.invoice_number, '._DB_PREFIX_.'orders.date_add, '._DB_PREFIX_.'orders.total_shipping, '._DB_PREFIX_.'orders.total_paid_real, '._DB_PREFIX_.'orders.total_paid, '._DB_PREFIX_.'orders.total_paid_tax_incl, '._DB_PREFIX_.'orders.total_paid_tax_excl, '._DB_PREFIX_.'orders.total_discounts, '._DB_PREFIX_.'orders.total_discounts_tax_incl, '._DB_PREFIX_.'orders.total_discounts_tax_excl, '._DB_PREFIX_.'orders.total_products, '._DB_PREFIX_.'orders.total_shipping_tax_incl, '._DB_PREFIX_.'orders.total_shipping_tax_excl, '._DB_PREFIX_.'orders.payment, '._DB_PREFIX_.'orders.reference, '._DB_PREFIX_.'orders.id_address_delivery, '._DB_PREFIX_.'orders.id_address_invoice, '._DB_PREFIX_.'orders.id_cart, '._DB_PREFIX_.'orders.id_customer, '._DB_PREFIX_.'customer.lastname , '._DB_PREFIX_.'customer.firstname , '._DB_PREFIX_.'customer.email , '._DB_PREFIX_.'carrier.id_carrier, '._DB_PREFIX_.'carrier.name, '._DB_PREFIX_.'carrier.id_reference, '._DB_PREFIX_.'cart.gift, '._DB_PREFIX_.'cart.recyclable, '._DB_PREFIX_.'cart.gift_message FROM '._DB_PREFIX_.'order_history LEFT JOIN '._DB_PREFIX_.'orders ON '._DB_PREFIX_.'order_history.id_order = '._DB_PREFIX_.'orders.id_order LEFT JOIN '._DB_PREFIX_.'customer ON '._DB_PREFIX_.'customer.id_customer = '._DB_PREFIX_.'orders.id_customer LEFT JOIN '._DB_PREFIX_.'carrier ON '._DB_PREFIX_.'carrier.id_carrier = '._DB_PREFIX_.'orders.id_carrier LEFT JOIN '._DB_PREFIX_.'cart ON '._DB_PREFIX_.'cart.id_cart = '._DB_PREFIX_.'orders.id_cart WHERE '._DB_PREFIX_.'order_history.id_order_state = 2 GROUP BY '._DB_PREFIX_.'order_history.id_order HAVING MAX( '._DB_PREFIX_.'order_history.id_order_state ) in (2,8,9,10,11) '; echo $SQL_COMMANDES . "
" ; $LIST_COMMANDES = Db::getInstance()->Query($SQL_COMMANDES); $nb_commandes = Db::getInstance()->numRows($LIST_COMMANDES); echo $nb_commandes . "
" ; if($nb_commandes > 0) { //$URL_SITE = "http://" . Configuration::get('CANONICAL_URL') ; $DATE = date('YmdHis'); $FICHIER = "orders_". $DATE .".xml"; $PATH = dirname(__FILE__) . "/data/" ; $FICHIER_A_ENVOYER = $PATH . $FICHIER ; $f = fopen($FICHIER_A_ENVOYER,"w"); //DEBUT DU FICHIER $debutxml = '' ; $debutxml .= "\n" ; $debutxml .= '' ; $debutxml .= "\n" ; fputs($f,$debutxml); } $orderlist = array(); $nb_commandes_0 = 0 ; while($commande = Db::getInstance()->nextRow($LIST_COMMANDES)) { // TEST SUR LE DERNIER STATUT $SQL_LAST_STATUS = 'SELECT id_order_state FROM '._DB_PREFIX_.'order_history where id_order = '.$commande['id_order'].' ORDER BY id_order_history desc '; //$LIST_LAST_STATUS = Db::getInstance()->Query($SQL_LAST_STATUS); $ROW_LAST_STATUS = Db::getInstance()->getRow($SQL_LAST_STATUS); $LAST_STATUS = $ROW_LAST_STATUS['id_order_state']; //$LAST_STATUS = mysql_result($LIST_LAST_STATUS,0,"id_order_state"); echo $LAST_STATUS . "
" ; if($LAST_STATUS == '2' ) { $nb_commandes_0 ++ ; $cart_id = $commande['id_cart']; $order_id = $commande['id_order']; array_push($orderlist, $order_id); //DEBUT D UNE COMMANDE $debutcommandexml = "\n\n\n" ; $debutcommandexml .= "
\n" ; $debutcommandexml .= "" . $commande['id_order'] . "\n" ; $debutcommandexml .= "" . $commande['reference'] . "\n" ; $debutcommandexml .= "Commande Ecommerce\n" ; $debutcommandexml .= "" . $commande['date_add'] . "\n" ; $debutcommandexml .= "" . $commande['invoice_number'] . "\n" ; //BLOCK MESSAGE //$debutcommandexml .= "\n" ; //$debutcommandexml .= "\n" ; //$debutcommandexml .= "\n" ; fputs($f,$debutcommandexml); //BLOCK CUSTOMER $orderxml = "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; // ADRESSE DE FACTURATION (id_address_invoice) $SQL_ADRESSE_FACTURATION = 'SELECT '._DB_PREFIX_.'address.company , '._DB_PREFIX_.'address.lastname , '._DB_PREFIX_.'address.firstname , '._DB_PREFIX_.'address.address1 , '._DB_PREFIX_.'address.address2 , '._DB_PREFIX_.'address.postcode , '._DB_PREFIX_.'address.city , '._DB_PREFIX_.'address.phone , '._DB_PREFIX_.'address.phone_mobile , '._DB_PREFIX_.'country.iso_code AS COUNTRY_CODE , '._DB_PREFIX_.'state.iso_code AS STATE_CODE FROM '._DB_PREFIX_.'address LEFT JOIN '._DB_PREFIX_.'country ON '._DB_PREFIX_.'country.id_country = '._DB_PREFIX_.'address.id_country LEFT JOIN '._DB_PREFIX_.'state ON '._DB_PREFIX_.'state.id_state = '._DB_PREFIX_.'address.id_state WHERE '._DB_PREFIX_.'address.id_address = '. $commande['id_address_invoice'] .' limit 1'; //echo $SQL_ADRESSE_FACTURATION . "\n" ; $LIST_ADRESSE_FACTURATION = Db::getInstance()->Query($SQL_ADRESSE_FACTURATION); while($adresse_facturation = Db::getInstance()->nextRow($LIST_ADRESSE_FACTURATION)) { //BLOCK SHIPPING ADRESS $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "" .$commande['email']. "\n" ; $orderxml .= "" .$adresse_facturation['phone']. "\n" ; $orderxml .= "" .$adresse_facturation['phone_mobile']. "\n" ; $orderxml .= "\n" ; } // ADRESSE LIVRAISON (id_address_delivery) $SQL_ADRESSE_LIVRAISON = 'SELECT '._DB_PREFIX_.'address.company , '._DB_PREFIX_.'address.lastname , '._DB_PREFIX_.'address.firstname , '._DB_PREFIX_.'address.address1 , '._DB_PREFIX_.'address.address2 , '._DB_PREFIX_.'address.postcode , '._DB_PREFIX_.'address.other , '._DB_PREFIX_.'address.city , '._DB_PREFIX_.'address.phone , '._DB_PREFIX_.'address.phone_mobile , '._DB_PREFIX_.'country.iso_code AS COUNTRY_CODE , '._DB_PREFIX_.'state.iso_code AS STATE_CODE FROM '._DB_PREFIX_.'address LEFT JOIN '._DB_PREFIX_.'country ON '._DB_PREFIX_.'country.id_country = '._DB_PREFIX_.'address.id_country LEFT JOIN '._DB_PREFIX_.'state ON '._DB_PREFIX_.'state.id_state = '._DB_PREFIX_.'address.id_state WHERE '._DB_PREFIX_.'address.id_address = '. $commande['id_address_delivery'] .' limit 1'; //echo $SQL_ADRESSE_LIVRAISON . "\n" ; $LIST_ADRESSE_LIVRAISON = Db::getInstance()->Query($SQL_ADRESSE_LIVRAISON); while($adresse_livraison = Db::getInstance()->nextRow($LIST_ADRESSE_LIVRAISON)) { //BLOCK SHIPPING ADRESS $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "" .$commande['email']. "\n" ; $orderxml .= "" .$adresse_livraison['phone']. "\n" ; $orderxml .= "" .$adresse_livraison['phone_mobile']. "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; } //BLOCK FIN CUSTOMER $orderxml .= "\n" ; $orderxml .= "
\n" ; //BLOCK ARTICLES $SQL_ARTICLES = 'SELECT * FROM '._DB_PREFIX_.'order_detail JOIN '._DB_PREFIX_.'product ON '._DB_PREFIX_.'product.id_product = '._DB_PREFIX_.'order_detail.product_id WHERE '._DB_PREFIX_.'order_detail.id_order = '.$commande['id_order'].' '; //echo $SQL_ARTICLES . "\n" ; $LIST_ARTICLES = Db::getInstance()->Query($SQL_ARTICLES); $NOMBRE_ARTICLES = Db::getInstance()->numRows($LIST_ARTICLES); $orderxml .= "\n" ; while($article = Db::getInstance()->nextRow($LIST_ARTICLES)) { //$tax_amount = $article['product_price'] * $article['tax_rate'] / 100 * $article['product_quantity'] ; $tax_amount = $article['total_price_tax_incl'] - $article['total_price_tax_excl']; $tax_rate = (100 * $tax_amount) / $article['total_price_tax_excl'] ; $tax_rate = round($tax_rate, 1); //$total_price_ttc = ( $article['product_price'] * $article['product_quantity'] ) + $tax_amount ; $total_price_ttc = $article['total_price_tax_incl']; //$total_price_ht = $article['product_price'] * $article['product_quantity'] ; $total_price_ht = $article['total_price_tax_excl']; $unit_price_ht = $article['product_price']; $unit_price_ht = round($unit_price_ht, 2); $tax_amount_arrondit = round($tax_amount, 2); $total_price_ttc_arrondit = round($total_price_ttc, 2); $total_price_ht_arrondit = round($total_price_ht, 2); $reduction_amount_tax_incl = $article['reduction_amount_tax_incl']; $reduction_amount_tax_excl = $article['reduction_amount_tax_excl']; $reduction_amount = $article['reduction_amount']; $orderxml .= "\n" ; if ($article['product_attribute_id'] == 0 ) { $orderxml .= "\n" ; } else { $orderxml .= "\n" ; } $orderxml .= "\n" ; //SKU $orderxml .= "" .$article['product_quantity']. "\n" ; //Quantite commande de ce produit $orderxml .= "\n" ; $orderxml .= "" .$total_price_ht_arrondit. "\n" ; $orderxml .= "" .$total_price_ttc_arrondit. "\n" ; $orderxml .= "" .$unit_price_ht. "\n" ; $orderxml .= "" .$tax_rate. "\n" ; $orderxml .= "" .$tax_amount_arrondit. "\n" ; $orderxml .= "" .round($reduction_amount, 2). "\n" ; $orderxml .= "" .round($reduction_amount_tax_incl, 2). "\n" ; $orderxml .= "" .round($reduction_amount_tax_excl, 2). "\n" ; if ($article['product_attribute_id'] == 0 ) { $id_product = $article['product_id']; $orderxml .= "\n" ; $sql_pack = "select * from "._DB_PREFIX_."pack where id_product_pack = '$id_product' order by id_product_item asc "; $LIST_PACK = Db::getInstance()->Query($sql_pack); while($pack = Db::getInstance()->nextRow($LIST_PACK)) { $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; } $orderxml .= "\n" ; } $orderxml .= "\n" ; } $orderxml .= "\n" ; //BLOCK FOOTER $shipping_details = $this->getshippingdetails($cart_id); $orderxml .= "
\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= " \n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "1\n" ; $orderxml .= "0\n" ; $orderxml .= "
\n" ; $orderxml .= "\n" ; $id_customer = $commande['id_customer']; $id_order = $commande['id_order']; $messages = $this->getordermessages($id_order,$id_customer); foreach($messages as $message) { $orderxml .= "\n" ; $orderxml .= "\n" ; $orderxml .= "\n" ; } $orderxml .= "\n" ; $orderxml .= "
\n" ; fputs($f,$orderxml); } } if($nb_commandes > 0) { $finxml = "\n
" ; fputs($f,$finxml); fclose($f); } $log_orders = "Neolys Commandes | ".$DATE_LOG." | Connexion FTP OK | "; $log_orders .= "Nb commandes : ".$nb_commandes_0 ; if($nb_commandes > 0) { // Passage ds le répertoire DES ARTICLES ftp_chdir($conn_id, $this->getConfig("order_directory")); // Liste les fichiers du répertoire if( ftp_put($conn_id, $FICHIER, $FICHIER_A_ENVOYER, FTP_BINARY) ) { $log_orders .= " | Transfert fichier ".$FICHIER; unlink($FICHIER_A_ENVOYER); foreach($orderlist as $order_id) { $orderHistory = new OrderHistory(); $orderHistory->id_order = $order_id; $orderHistory->changeIdOrderState(intval(_PS_OS_PREPARATION_), intval($orderHistory->id_order), true); $orderHistory->addWithemail(); } } else { $log_orders .= " Transfert fichier KO"; } } } // Fermeture de la connexion FTP. ftp_quit($conn_id); } echo "Log : ".$log_orders; Logger::addLog($log_orders, 1, 101, 'Logging object'); } ////////////////////////////////////////// ////////// IMPORT STOCK ////////////////////////////////////////// public function updateStock() { if($this->getConfig("stock_flag")=='1') { $DATE_LOG = date("Ymd H:i:s"); $log_stock = $DATE_LOG." | "; $transfert_status = ''; // Création de la connexion $conn_id = ftp_connect($this->getConfig("FTP_IP")); // Authentification avec nom de compte et mot de passe $login_result = ftp_login($conn_id, $this->getConfig("FTP_login"), $this->getConfig("FTP_password")); //mode passif ftp_pasv($conn_id,1); // Vérification de la connexion if ((!$conn_id) || (!$login_result)) { $log_stock .= 'Connexion FTP KO'; die; } else { $log_stock .= 'Connexion FTP OK'; ftp_chdir($conn_id, $this->getConfig("stock_directory")); $liste_fichiers_stock = ftp_nlist($conn_id, "."); sort($liste_fichiers_stock); foreach($liste_fichiers_stock as $fichier_stock_a_telecharger) { } $date = date('Ymd'); if(Tools::substr($fichier_stock_a_telecharger,6,8) == $date and Tools::substr($fichier_stock_a_telecharger,0,5) == 'stock') { $directory_ps = dirname(__FILE__).'/data/'; if(ftp_get($conn_id, $directory_ps.$fichier_stock_a_telecharger, $fichier_stock_a_telecharger, FTP_BINARY)) { $log_stock .= ' | Transfert fichier '.$fichier_stock_a_telecharger.' OK'; $transfert_status = '1'; } else{$log_stock .= ' | Transfert fichier '.$fichier_stock_a_telecharger.' KO';} } else { $log_stock .= ' | Aucun fichier a transferer'; } } // Fermeture de la connexion FTP. ftp_quit($conn_id); if($transfert_status == '1') { $fichier_a_traiter = dirname(__FILE__).'/data/'.$fichier_stock_a_telecharger; $file_content=file($fichier_a_traiter); $nb_lines = count($file_content); for( $i = 0 ; $i < count($file_content); $i++ ) { $file_contents = explode(";",$file_content[$i]); $ref_art = addslashes($file_contents[1]); $ref_art_l = explode("_",$ref_art); $id_product = $ref_art_l[0]; $id_product_attribute = $ref_art_l[1]; $stock = addslashes($file_contents[2]); $poids = addslashes($file_contents[3]); $poids = $poids / 1000 ; if($poids > 0) { if($id_product_attribute == '') { $sql='UPDATE '._DB_PREFIX_.'product AS P set P.weight = '.$poids.' where id_product = '.$id_product; $sql2='UPDATE '._DB_PREFIX_.'stock_available AS P set P.quantity = '.$stock.' where id_product = '.$id_product.' and id_product_attribute = 0 '; } else { $sql='UPDATE '._DB_PREFIX_.'product_attribute AS PA set PA.weight = '.$poids.' where PA.id_product_attribute = '.$id_product_attribute.' and PA.id_product = '.$id_product; $sql2='UPDATE '._DB_PREFIX_.'stock_available AS P set P.quantity = '.$stock.' where id_product = '.$id_product.' and id_product_attribute = '.$id_product_attribute ; } Db::getInstance()->Execute($sql); Db::getInstance()->Execute($sql2); } elseif($poids == '0') { if($id_product_attribute == '') { $sql='UPDATE '._DB_PREFIX_.'stock_available AS P set P.quantity = '.$stock.' where id_product = '.$id_product.' and id_product_attribute = 0 '; } else { $sql='UPDATE '._DB_PREFIX_.'stock_available AS P set P.quantity = '.$stock.' where id_product = '.$id_product.' and id_product_attribute = '.$id_product_attribute ; } Db::getInstance()->Execute($sql); } } $log_stock .= ' | MAJ : '.$nb_lines.' records'; } echo "Log : ".$log_stock; Logger::addLog($log_stock, 1, 101, 'Logging object'); } } ///////////////////////////////////////////////////////////////////// ///////////////// STATUTS //////////////////////////////////////////////////////////////////// public function updateStatuts() { if($this->getConfig("statuts_flag")=='1') { $DATE = date("Ymd H:i:s"); // ACCES WEBSERVICE $postdata = http_build_query( array( 'ws_key' => $this->getConfig("WS_KEY"), 'content' => 'ORDER_HISTORY', 'is_test' => '0' ) ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $link = "http://".$this->getConfig("FTP_IP")."/webservice/webservices.php"; $result = Tools::file_get_contents($link, false, $context); echo $result; $xml = simplexml_load_string($result); $nb_done = 0; $p_cnt = count($xml->EVENT); for($i = 0; $i < $p_cnt; $i++) { $event = $xml->EVENT[$i]; $event_type = '' ; $event_type = $event->EVENT_TYPE ; $order_id = '' ; $order_id = $event->ORDER_ID ; $id_colis = '' ; $id_colis = $event->ID_COLIS ; //$event_date = '' ; //$event_date = $event->EVENT_DATE ; $SQL = 'SELECT * FROM '._DB_PREFIX_.'orders where id_order = '.$order_id.' ORDER BY '._DB_PREFIX_.'orders.id_order ASC'; $RESULTAT_SQL = Db::getInstance()->Query($SQL); $NOMBRELIGNE = Db::getInstance()->numRows($RESULTAT_SQL); if ($NOMBRELIGNE > 0) { echo "
"; echo "No de commande : ".$order_id; echo "
"; $last_order_state = OrderHistory::getLastOrderState(intval($order_id)); echo "Dernier etat :".$last_order_state->id ; echo "
"; if($event_type == '10') { echo "COMMANDE ENVOYEE"; echo "
"; if($last_order_state->id != _PS_OS_SHIPPING_ ) { $SQL_SHIPPING = 'UPDATE '._DB_PREFIX_."orders SET shipping_number = '$id_colis' WHERE id_order = '$order_id' "; Db::getInstance()->Query($SQL_SHIPPING); $SQL_SHIPPING = 'UPDATE '._DB_PREFIX_."order_carrier SET tracking_number = '$id_colis' WHERE id_order = '$order_id' "; Db::getInstance()->Query($SQL_SHIPPING); $orderHistory = new OrderHistory(); $orderHistory->id_order = $order_id; $orderHistory->changeIdOrderState(intval(_PS_OS_SHIPPING_), intval($orderHistory->id_order), true); /* Antadis */ //$orderHistory->add(); $order = new Order((int) $order_id); $carrier = new Carrier((int) $order->getIdOrderCarrier()); if($id_colis && $carrier->url) { $templateVars = array('{followup}' => str_replace('@', $id_colis, $carrier->url)); $orderHistory->addWithemail(true, $templateVars); } else { $orderHistory->addWithemail(); } /* Antadis */ echo "
"; $last_order_state_shipping = OrderHistory::getLastOrderState(intval($order_id)); echo "Nouvel etat :".$last_order_state_shipping->id; echo "
"; $nb_done ++; } else { echo "COMMANDE DONT LE STATUT EST DEJA OS SHIPPING"."
"; } } elseif($event_type == '20') { echo "COMMANDE LIVREE"; echo "
"; if($last_order_state->id != _PS_OS_DELIVERED_ ) { echo "
"; $orderHistory = new OrderHistory(); $orderHistory->id_order = $order_id; $orderHistory->changeIdOrderState(intval(_PS_OS_DELIVERED_), intval($orderHistory->id_order), true); $orderHistory->add(); echo "
"; $last_order_state_delivered = OrderHistory::getLastOrderState(intval($order_id)); echo "Nouvel etat :".$last_order_state_delivered->id; echo "
"; $nb_done ++; } else { echo "COMMANDE DONT LE STATUT EST DEJA OS DELIVERED"."
"; } } elseif($event_type == '30') { echo "COMMANDE ANNULEE"; echo "
"; if($last_order_state->id != _PS_OS_CANCELED_ ) { $orderHistory = new OrderHistory(); $orderHistory->id_order = $order_id; $orderHistory->changeIdOrderState(intval(_PS_OS_CANCELED_), intval($orderHistory->id_order)); $orderHistory->add(); echo "
"; $last_order_state_canceled = OrderHistory::getLastOrderState(intval($order_id)); echo "Nouvel etat :".$last_order_state_canceled->id; echo "
"; $nb_done ++; } else { echo "COMMANDE DONT LE STATUT EST DEJA OS CANCELEDED"."
"; } } } } $log_statuts = $DATE.' | MAJ : '.$nb_done.' / '.$p_cnt.' records'; echo "Log : ".$log_statuts; Logger::addLog($log_statuts, 1, 101, 'Logging object'); } } }