From 8b15ef46a6eb48e952251908e5f6d98b8a8195dc Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 30 Jan 2018 17:15:33 +0100 Subject: [PATCH] Rework for dropshipping --- .../AdminAntDropshippingtracking.php | 337 ++++++++++++------ 1 file changed, 220 insertions(+), 117 deletions(-) diff --git a/modules/ant_dropshippingtracking/AdminAntDropshippingtracking.php b/modules/ant_dropshippingtracking/AdminAntDropshippingtracking.php index 87d577c2..c2d633fc 100644 --- a/modules/ant_dropshippingtracking/AdminAntDropshippingtracking.php +++ b/modules/ant_dropshippingtracking/AdminAntDropshippingtracking.php @@ -20,10 +20,13 @@ class AdminAntDropshippingtracking extends AdminTab } } - public function _postProcess() { - if(Tools::isSubmit('submitOrderTracking') && Tools::getValue('id_sale')){ + public function _postProcess() + { + $this->_html = ''; + + if ($id_sale = Tools::getValue('id_sale')) { + // Selection des produits de la vente $errors = 0; - $id_sale = (int)Tools::getValue('id_sale'); $sale_names = array(); foreach(Db::getInstance()->ExecuteS(' SELECT c.`name`, c.`id_lang` @@ -41,42 +44,114 @@ class AdminAntDropshippingtracking extends AdminTab ') as $key => $row) { $sale_products[(int)$row['id_product']] = (int)$row['id_product']; } - - if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') { - + + $infosTrackingEnable = $infosTrackingDisable = 0; + // Fichier avec infos tracking + if (Tools::isSubmit('submitOrderTracking')) { + $infosTrackingEnable = 1; + } + // Fichier sans infos de tracking + if (Tools::isSubmit('submitOrderTrackingNull')) { + $infosTrackingDisable = 1; + } + + // Gestion du fichier CSV + $orders = array(); + if (isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') { + $cols = array( + 'id_order', + 'tracking_number', + 'carrier', + 'link', + ); + $i = 0; $f = fopen($_FILES['csvfile']['tmp_name'], 'r'); - fgetcsv($f, 0, ';'); - $i = 1; - - $orders = array(); - while($line = fgetcsv($f, 0, ';')) { + while ($data = fgetcsv($f, 0, ';')) { $i++; - $orders[(int) $line[0]] = array( - 'id_order' => (int) $line[0], - 'tracking_number' => trim($line[1]), - 'carrier' => $line[2], - 'link' => trim($line[3]), + if ($i == 1) { + continue; + } + + // Get Data + $num = count($data); + + // Check line integrity + if ($num != count($cols)) { + $orders = array(); + break; + } + + // Assign var + $line = array(); + for ($c=0; $c<$num; $c++) { + $name = $cols[$c]; + switch ($name) { + case 'tracking_number': + case 'link'; + $line[$name] = trim($data[$c]); + break; + default: + $line[$name] = $data[$c]; + break; + } + + } + + // Vérification des données + if ($infosTrackingDisable == 1) { + if ($line['tracking_number'] != ''){ + $orders = array(); + break; + } + if ($line['link'] != ''){ + $orders = array(); + break; + } + } + if ($infosTrackingEnable == 1) { + if ($line['tracking_number'] == ''){ + $orders = array(); + break; + } + if ($line['link'] == ''){ + $orders = array(); + break; + } + } + + // Assign orders + $orders[(int) $line['id_order']] = array( + 'id_order' => (int)$line['id_order'], + 'tracking_number' => $line['tracking_number'], + 'carrier' => $line['carrier'], + 'link' => $line['link'], ); } - if(!empty($orders)) { - foreach ($orders as $id_order => $o) { - if($id_order != 0){ - $order = new Order((int)$id_order); - if(Validate::isLoadedObject($order)) { - $order_details = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT `id_order_detail`, `product_name`,`product_id`, `product_attribute_id`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `id_order` = '.(int) $id_order.' - ') as $key => $row) { - $order_details[(int)$row['id_order_detail']] = $row; - } - - $fully_sent = false; - - // Calcul des produits déjà envoyés par LaPoste - $products_sent = array(); - foreach(Db::getInstance()->ExecuteS(' + } + else { + $this->_html .= HelperFormBootstrap::displayErrors($this->l('Fichier manquant !')); + } + + // Traitement des commandes + if (count($orders) > 0) { + foreach ($orders as $id_order => $o) { + if ($id_order != 0) { + $order = new Order((int)$id_order); + if (Validate::isLoadedObject($order)) { + $order_details = array(); + foreach (Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_order_detail`, `product_name`,`product_id`, `product_attribute_id`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $id_order.' + ') as $key => $row) { + $order_details[(int)$row['id_order_detail']] = $row; + } + + $fully_sent = false; + + // Calcul des produits déjà envoyés par LaPoste + $products_sent = array(); + foreach (Db::getInstance()->ExecuteS(' SELECT d.`id_order_detail`, IF( (d.`product_quantity` - IF( d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded` @@ -88,13 +163,13 @@ class AdminAntDropshippingtracking extends AdminTab WHERE d.`id_order` = '.(int) $order->id.' GROUP BY d.`id_order_detail` ') as $quantity_remain) { - if ((int) $quantity_remain['remain'] == 0) { - $products_sent[] = (int) $quantity_remain['id_order_detail']; - } + if ((int) $quantity_remain['remain'] == 0) { + $products_sent[] = (int) $quantity_remain['id_order_detail']; } - - // Calcul des produits retourné / remboursé - foreach(Db::getInstance()->ExecuteS(' + } + + // Calcul des produits déjà envoyé Exapaq + foreach (Db::getInstance()->ExecuteS(' SELECT d.`id_order_detail`, IF( (d.`product_quantity` - IF( d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded` @@ -106,12 +181,13 @@ class AdminAntDropshippingtracking extends AdminTab WHERE d.`id_order` = '.(int) $order->id.' GROUP BY d.`id_order_detail` ') as $quantity_remain) { - if ((int) $quantity_remain['remain'] == 0) { - $products_sent[] = (int) $quantity_remain['id_order_detail']; - } + if ((int) $quantity_remain['remain'] == 0) { + $products_sent[] = (int) $quantity_remain['id_order_detail']; } - - foreach(Db::getInstance()->ExecuteS(' + } + + // Calcul des produits déjà envoyé MondialRelay + foreach (Db::getInstance()->ExecuteS(' SELECT d.`id_order_detail`, IF( (d.`product_quantity` - IF( d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded` @@ -123,12 +199,13 @@ class AdminAntDropshippingtracking extends AdminTab WHERE d.`id_order` = '.(int) $order->id.' GROUP BY d.`id_order_detail` ') as $quantity_remain) { - if ((int) $quantity_remain['remain'] == 0) { - $products_sent[] = (int) $quantity_remain['id_order_detail']; - } + if ((int) $quantity_remain['remain'] == 0) { + $products_sent[] = (int) $quantity_remain['id_order_detail']; } - - foreach(Db::getInstance()->ExecuteS(' + } + + // Calcul des produits déjà envoyé Philea + foreach (Db::getInstance()->ExecuteS(' SELECT d.`id_order_detail`, IF( (d.`product_quantity` - IF( d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded` @@ -140,12 +217,13 @@ class AdminAntDropshippingtracking extends AdminTab WHERE d.`id_order` = '.(int) $order->id.' GROUP BY d.`id_order_detail` ') as $quantity_remain) { - if ((int) $quantity_remain['remain'] == 0) { - $products_sent[] = (int) $quantity_remain['id_order_detail']; - } + if ((int) $quantity_remain['remain'] == 0) { + $products_sent[] = (int) $quantity_remain['id_order_detail']; } - - foreach(Db::getInstance()->ExecuteS(' + } + + // Calcul des produits déjà envoyé Dropshipping + foreach (Db::getInstance()->ExecuteS(' SELECT d.`id_order_detail`, IF( (d.`product_quantity` - IF( d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded` @@ -157,17 +235,21 @@ class AdminAntDropshippingtracking extends AdminTab WHERE d.`id_order` = '.(int) $order->id.' GROUP BY d.`id_order_detail` ') as $quantity_remain) { - if ((int) $quantity_remain['remain'] == 0) { - $products_sent[] = (int) $quantity_remain['id_order_detail']; - } + if ((int) $quantity_remain['remain'] == 0) { + $products_sent[] = (int) $quantity_remain['id_order_detail']; } - $remaining = array(); - $html_products_sent = ''; - foreach($order_details as $id_order_detail => $d) { - if(!in_array($id_order_detail, $products_sent) && !in_array($d['product_id'],$sale_products)) { - $remaining[] = (int)$id_order_detail; - } elseif(!in_array($id_order_detail, $products_sent) && in_array((int)$d['product_id'],$sale_products)) { - $html_products_sent .= '
'."\r\n".$d['quantity'] . 'x' . ' ' . $p['product_name']; + } + + $remaining = array(); + foreach($order_details as $id_order_detail => $d) { + // Calcul des produits restant + if (!in_array($id_order_detail, $products_sent) && !in_array($d['product_id'], $sale_products)) { + $remaining[] = (int)$id_order_detail; + } + // Marquage envoi + elseif (!in_array($id_order_detail, $products_sent) && in_array((int)$d['product_id'],$sale_products)) { + + if ($infosTrackingEnable == 1) { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'ant_dropshipping_parcel` VALUES ( @@ -180,14 +262,50 @@ class AdminAntDropshippingtracking extends AdminTab ) '); } + + if ($infosTrackingDisable == 1) { + // Supprimer tracking_number et link + Db::getInstance()->ExecuteS(' + INSERT INTO `'._DB_PREFIX_.'ant_dropshipping_parcel` + VALUES ( + '.(int) $id_order_detail.', + '.(int) $d['quantity'].', + "", + "NOTRACKING", + "", + NOW() + ) + '); + } } - - if(count($remaining) == 0) { - $fully_sent = true; - } else { - $fully_sent = false; - } - + } + + if (count($remaining) == 0) { + $fully_sent = true; + } else { + $fully_sent = false; + } + + $history = new OrderHistory(); + $history->id_order = (int) $order->id; + $history->changeIdOrderState(($fully_sent ? Configuration::get('PS_OS_SHIPPING') : 17), (int) $order->id); + $history->id_employee = 0; + $history->add(); + + Db::getInstance()->ExecuteS(' + INSERT INTO `'._DB_PREFIX_.'shipping_history` + VALUES ( + '.(int) $order->id.', + "'.pSQL($o['shipping_number']).'", + NOW(), + 0, + '.(int)$id_sale.' + ) + '); + + if ($infosTrackingEnable) { + global $_LANGMAIL; + $subject = 'Package in transit'; $customer = new Customer((int) $order->id_customer); $templateVars = array( '{followup}' => $o['link'], @@ -197,59 +315,42 @@ class AdminAntDropshippingtracking extends AdminTab '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{id_order}' => (int) $order->id, - // '{product_list}' => !empty($html_products_sent)? $content_html.$html_products_sent: '', - // '{product_list_txt}' => !empty($html_products_sent)? $content_txt.strip_tags($html_products_sent): '', ); - - $history = new OrderHistory(); - $history->id_order = (int) $order->id; - $history->changeIdOrderState(($fully_sent ? Configuration::get('PS_OS_SHIPPING') : 17), (int) $order->id); - $history->id_employee = 0; - $history->add(); - - Db::getInstance()->ExecuteS(' - INSERT INTO `'._DB_PREFIX_.'shipping_history` - VALUES ( - '.(int) $order->id.', - "'.pSQL($o['shipping_number']).'", - NOW(), - 0, - '.(int)$id_sale.' - ) - '); - - global $_LANGMAIL; - $subject = 'Package in transit'; - if(!Mail::Send(intval($order->id_lang), 'in_transit_dropshipping', ((is_array($_LANGMAIL) && key_exists($subject, $_LANGMAIL)) ? $_LANGMAIL[$subject] : $subject), $templateVars, $customer->email, $customer->firstname.' '.$customer->lastname)){ - $errors++; + if (!Mail::Send(intval($order->id_lang), 'in_transit_dropshipping', + ((is_array($_LANGMAIL) && key_exists($subject, $_LANGMAIL)) ? $_LANGMAIL[$subject] : $subject), + $templateVars, $customer->email, $customer->firstname.' '.$customer->lastname)) { + $errors++; } } } - } - if(!isset($_COOKIE['logistics_sales'])){ - setcookie('logistics_sales', $id_sale, 0, __PS_BASE_URI__); - } else { - $sales = explode('-', $_COOKIE['logistics_sales']); - if(!in_array($id_sale,$sales)){ - $sales[] = $id_sale; + + if ($errors == 0) { + $this->_html .= HelperFormBootstrap::displaySuccess($this->l('Fichier importé avec succès !')); + } else { + $this->_html .= HelperFormBootstrap::displayErrors($this->l($errors.' mails n\'ont pas pu être envoyés')); } - setcookie('logistics_sales', implode('-', $sales), 0, __PS_BASE_URI__); } - if($errors==0){ - $this->_html .= HelperFormBootstrap::displaySuccess($this->l('Fichier importé avec succès !')); - } else { - $this->_html .= HelperFormBootstrap::displayErrors($this->l($errors.' mails n\'ont pas pu être envoyés')); - } - } else { - $this->_html .= HelperFormBootstrap::displayErrors($this->l('Aucune commande trouvée, vérifier votre fichier')); } - } else { - $this->_html .= HelperFormBootstrap::displayErrors($this->l('Fichier manquant !')); + + // Cookie + if (!isset($_COOKIE['logistics_sales'])){ + setcookie('logistics_sales', $id_sale, 0, __PS_BASE_URI__); + } else { + $sales = explode('-', $_COOKIE['logistics_sales']); + if(!in_array($id_sale,$sales)){ + $sales[] = $id_sale; + } + setcookie('logistics_sales', implode('-', $sales), 0, __PS_BASE_URI__); + } + } + else { + $this->_html .= HelperFormBootstrap::displayErrors($this->l('Aucune commande trouvée, vérifier votre fichier')); } } } - public function display() { + public function display() + { global $cookie, $currentIndex; $base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminAntDropshippingtracking'); @@ -315,13 +416,15 @@ class AdminAntDropshippingtracking extends AdminTab
- + +
- '; + + '; $this->_html .= $helperForm->renderScript();