diff --git a/modules/philea_magistor/AdminPhileaMagistor.php b/modules/philea_magistor/AdminPhileaMagistor.php index a3d49787..c9ee1b0a 100644 --- a/modules/philea_magistor/AdminPhileaMagistor.php +++ b/modules/philea_magistor/AdminPhileaMagistor.php @@ -15,12 +15,12 @@ class AdminPhileaMagistor extends AdminTab { if(!Db::getInstance()->getRow(' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_sync` + FROM `'._DB_PREFIX_.'philea_sync` WHERE `id_sale` = '.(int) $id_sale.' AND `status` = 0 ')) { Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'philea_magistor_sync` + INSERT INTO `'._DB_PREFIX_.'philea_sync` VALUES ( DEFAULT, '.(int) $id_sale.', @@ -59,7 +59,7 @@ class AdminPhileaMagistor extends AdminTab { if($id_sale) { $active = (int) Tools::getValue('active'); $sql = ' - INSERT INTO `'._DB_PREFIX_.'philea_magistor_auto_sync` + INSERT INTO `'._DB_PREFIX_.'philea_auto_sync` VALUES ( '.(int) $id_sale.', '.(int) $active.', @@ -82,13 +82,13 @@ class AdminPhileaMagistor extends AdminTab { if($id_sale) { if(Db::getInstance()->getRow(' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_sync` + FROM `'._DB_PREFIX_.'philea_sync` WHERE `id_sale` = '.(int) $id_sale.' AND (`status` = 2 OR `status` = 4) ')) { Db::getInstance()->ExecuteS(' - UPDATE `'._DB_PREFIX_.'philea_magistor_sync` + UPDATE `'._DB_PREFIX_.'philea_sync` SET `status` = 3 WHERE id_sale = '.(int) $id_sale.' LIMIT 1 @@ -111,7 +111,7 @@ class AdminPhileaMagistor extends AdminTab { $id_sales = Tools::getValue('id_sales'); if (count($id_sales)){ $sql_values = array(); - $sql_insert = 'INSERT INTO `'._DB_PREFIX_.'philea_magistor_archive` (`id_sale`, `date_add`) VALUES'; + $sql_insert = 'INSERT INTO `'._DB_PREFIX_.'philea_archive` (`id_sale`, `date_add`) VALUES'; foreach ($id_sales as $id_sale) $sql_values[] = '(' . (int) $id_sale . ', NOW())'; $sql_insert .= implode(', ', $sql_values) . ' ON DUPLICATE KEY UPDATE `date_add` = NOW()'; @@ -128,7 +128,7 @@ class AdminPhileaMagistor extends AdminTab { } elseif(Tools::isSubmit('unarchiveSale') && Tools::getValue('id_sale_unarchive')){ if (Db::getInstance()->executeS(' - DELETE FROM `' . _DB_PREFIX_ . 'philea_magistor_archive` + DELETE FROM `' . _DB_PREFIX_ . 'philea_archive` WHERE `id_sale` = ' . (int) Tools::getValue('id_sale_unarchive') . ' LIMIT 1 ')){ @@ -144,7 +144,7 @@ class AdminPhileaMagistor extends AdminTab { $sql = ' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_reception` + FROM `'._DB_PREFIX_.'philea_reception` WHERE `id_sale` = ' . (int) $id_sale . ' AND `filename` LIKE "' . pSQL($filename) . '"'; $recept = Db::getInstance()->getRow($sql); @@ -385,8 +385,8 @@ class AdminPhileaMagistor extends AdminTab { SELECT pm_sync.`id_sale`, MAX(pm_sync.`date_add`) as `sync_date` - FROM `'._DB_PREFIX_.'philea_magistor_sync` pm_sync - LEFT JOIN `'._DB_PREFIX_.'philea_magistor_archive` pm_arch + FROM `'._DB_PREFIX_.'philea_sync` pm_sync + LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch ON pm_arch.`id_sale` = pm_sync.`id_sale` WHERE 1 AND `pm_arch`.`id_sale` IS NULL @@ -404,7 +404,7 @@ class AdminPhileaMagistor extends AdminTab { pm_r.`id_sale`, pm_r.`filename`, MAX(pm_r.`date_add`) as `recep_date` - FROM `'._DB_PREFIX_.'philea_magistor_reception` pm_r + FROM `'._DB_PREFIX_.'philea_reception` pm_r WHERE `id_sale` IN (' . implode(', ', $id_sales) . ') GROUP BY `id_sale`'; foreach (Db::getInstance()->executeS($sql) as $row) @@ -430,7 +430,7 @@ class AdminPhileaMagistor extends AdminTab { SELECT pm_as.`id_sale`, pm_as.`active` as `auto_sync_active` - FROM `'._DB_PREFIX_.'philea_magistor_auto_sync` pm_as + FROM `'._DB_PREFIX_.'philea_auto_sync` pm_as WHERE `id_sale` IN (' . implode(', ', $id_sales) . ') AND pm_as.`active` = 1'; foreach (Db::getInstance()->executeS($sql) as $row) @@ -441,7 +441,7 @@ class AdminPhileaMagistor extends AdminTab { SELECT pm_s.`id_sale`, MAX(pm_s.`date_add`) as `sent_date` - FROM `'._DB_PREFIX_.'philea_magistor_sent` pm_s + FROM `'._DB_PREFIX_.'philea_sent` pm_s WHERE pm_s.`id_sale` IN (' . implode(', ', $id_sales) . ') GROUP BY pm_s.`id_sale`'; foreach (Db::getInstance()->executeS($sql) as $row) @@ -481,18 +481,18 @@ class AdminPhileaMagistor extends AdminTab { // pmas.`active` as `auto_sync_active`, // p.date_start, // p.date_end - // FROM `'._DB_PREFIX_.'philea_magistor_sync` pmsync - // LEFT JOIN `'._DB_PREFIX_.'philea_magistor_sent` pms + // FROM `'._DB_PREFIX_.'philea_sync` pmsync + // LEFT JOIN `'._DB_PREFIX_.'philea_sent` pms // ON pms.`id_sale` = pmsync.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'privatesale` p // ON p.`id_sale` = pmsync.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'category_lang` c // ON c.`id_category` = p.`id_category` - // LEFT JOIN `'._DB_PREFIX_.'philea_magistor_reception` pmr + // LEFT JOIN `'._DB_PREFIX_.'philea_reception` pmr // ON pmsync.`id_sale` = pmr.`id_sale` - // LEFT JOIN `'._DB_PREFIX_.'philea_magistor_auto_sync` pmas + // LEFT JOIN `'._DB_PREFIX_.'philea_auto_sync` pmas // ON pmas.`id_sale` = pmsync.`id_sale` - // LEFT JOIN `'._DB_PREFIX_.'philea_magistor_archive` pm_arch + // LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch // ON pm_arch.`id_sale` = pmsync.`id_sale` // WHERE 1 // AND c.id_lang = 2 @@ -518,7 +518,7 @@ class AdminPhileaMagistor extends AdminTab { $archives_options = array(); foreach(Db::getInstance()->executeS(' SELECT a.`id_sale`, cl.`name` - FROM `' . _DB_PREFIX_ . 'philea_magistor_archive` a + FROM `' . _DB_PREFIX_ . 'philea_archive` a LEFT JOIN `' . _DB_PREFIX_ . 'privatesale` p ON p.`id_sale` = a.`id_sale` LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl diff --git a/modules/philea_magistor/cron.php b/modules/philea_magistor/cron.php index 20e96eb3..35fca359 100644 --- a/modules/philea_magistor/cron.php +++ b/modules/philea_magistor/cron.php @@ -8,13 +8,13 @@ if(isset($_SERVER['REMOTE_ADDR'])) { set_time_limit(600); if($row = Db::getInstance()->getRow(' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_sync` + FROM `'._DB_PREFIX_.'philea_sync` WHERE `status` = 0 ORDER BY `date_add` ASC ')) { // SEND ART01 Db::getInstance()->ExecuteS(' - UPDATE `'._DB_PREFIX_.'philea_magistor_sync` + UPDATE `'._DB_PREFIX_.'philea_sync` SET `status` = 1 WHERE `id_sync` = '.(int) $row['id_sync'].' LIMIT 1 @@ -22,9 +22,9 @@ if($row = Db::getInstance()->getRow(' system('cd '.dirname(__FILE__).'/script && php send_article.php '.(int) $row['id_sale']); sleep(20); - // SEND REC01 - pas de gestion recption fournisseur sur bbb + // SEND REC01 - pas de gestion recption fournisseur sur bbb /*Db::getInstance()->ExecuteS(' - UPDATE `'._DB_PREFIX_.'philea_magistor_sync` + UPDATE `'._DB_PREFIX_.'philea_sync` SET `status` = 2 WHERE `id_sync` = '.(int) $row['id_sync'].' LIMIT 1 @@ -34,13 +34,13 @@ if($row = Db::getInstance()->getRow(' if($row = Db::getInstance()->getRow(' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_sync` + FROM `'._DB_PREFIX_.'philea_sync` WHERE `status` = 3 ORDER BY `date_add` ASC ')) { // SEND CDC02 /*Db::getInstance()->ExecuteS(' - UPDATE `'._DB_PREFIX_.'philea_magistor_sync` + UPDATE `'._DB_PREFIX_.'philea_sync` SET `status` = 4 WHERE `id_sync` = '.(int) $row['id_sync'].' LIMIT 1 diff --git a/modules/philea_magistor/philea_magistor.php b/modules/philea_magistor/philea_magistor.php index faf6e57f..5c31599c 100644 --- a/modules/philea_magistor/philea_magistor.php +++ b/modules/philea_magistor/philea_magistor.php @@ -79,7 +79,7 @@ class philea_magistor extends Module public function install() { Db::getInstance()->ExecuteS(' - CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_magistor_sent` ( + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_sent` ( `id_order` INTEGER NOT NULL, `date_add` DATETIME NOT NULL, INDEX (`id_order`) @@ -87,7 +87,7 @@ class philea_magistor extends Module '); Db::getInstance()->ExecuteS(' - CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_magistor_parcel` ( + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_parcel` ( `id_order` INTEGER NOT NULL, `shipping_number` VARCHAR(64) NOT NULL, `date_add` DATETIME NOT NULL, @@ -96,7 +96,7 @@ class philea_magistor extends Module '); Db::getInstance()->ExecuteS(' - CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_magistor_sync` ( + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_sync` ( `id_sync` INTEGER NOT NULL AUTO_INCREMENT, `id_sale` INTEGER NOT NULL, `status` TINYINT NOT NULL, @@ -107,7 +107,7 @@ class philea_magistor extends Module '); Db::getInstance()->ExecuteS(' - CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_magistor_auto_sync` ( + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'philea_auto_sync` ( `id_sale` INTEGER NOT NULL, `active` TINYINT NOT NULL, `last_sent` DATETIME NOT NULL, @@ -117,15 +117,36 @@ class philea_magistor extends Module '); Db::getInstance()->execute(' - CREATE TABLE `'._DB_PREFIX_.'philea_magistor_archive` ( + CREATE TABLE `'._DB_PREFIX_.'philea_archive` ( `id_sale` int(11) NOT NULL, `date_add` datetime NOT NULL, PRIMARY KEY (`id_sale`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 '); - if (parent::install() == false) + if (parent::install() == false) { return false; + } + return true; + } + + public function uninstall() { + if (parent::uninstall() == false) { + return false; + } + + // Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_sent`'); + // Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_parcel`'); + // Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_sync`'); + // Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_auto_sync`'); + // Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_archive`'); + + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_sent`'); + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_parcel`'); + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_sync`'); + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_auto_sync`'); + Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'philea_magistor_archive`'); + return true; } @@ -334,12 +355,12 @@ class philea_magistor extends Module ')) { if(!Db::getInstance()->getRow(' SELECT * - FROM `'._DB_PREFIX_.'philea_magistor_sync` + FROM `'._DB_PREFIX_.'philea_sync` WHERE `id_sale` = '.(int) $id_sale.' AND `status` = 0 ')) { Db::getInstance()->ExecuteS(' - INSERT INTO `'._DB_PREFIX_.'philea_magistor_sync` + INSERT INTO `'._DB_PREFIX_.'philea_sync` VALUES ( DEFAULT, '.(int) $id_sale.', diff --git a/modules/philea_magistor/script/recept_expedition.php b/modules/philea_magistor/script/recept_expedition.php index 98e69797..3d540649 100644 --- a/modules/philea_magistor/script/recept_expedition.php +++ b/modules/philea_magistor/script/recept_expedition.php @@ -12,7 +12,7 @@ $magistorModule = new philea_magistor(); - $id_shipping = 4; // PHILEA + //$id_shipping = 4; // PHILEA $CRE = array( 'OP_CODE' => array(1,10), @@ -53,34 +53,30 @@ @set_time_limit(0); // Checking for ".bal" files with a "CRE" prefix that has an equivalent ".dat" file and process them... - $inFolder = dirname(__FILE__) . '/IN/'; $iterator = new DirectoryIterator($inFolder); $id_order_state = Configuration::get('PHILEA_MAGISTOR_STATUS_CRE'); foreach ($iterator as $fileinfo) { - if ($fileinfo->isFile()) { - if( preg_match( '@^CRE(.*)\.BAL@', $fileinfo->getFilename() ) ) { - + if( preg_match('@^CRE(.*)\.BAL@', $fileinfo->getFilename())) { $datFile = $fileinfo->getPath() . '/' . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() ); - if( file_exists( $datFile ) ) { - - $content = file_get_contents( $datFile ); - - $lines = preg_split( '@\n@', $content ); - - if( !empty($lines) ) { + if(file_exists($datFile)) { + $content = file_get_contents($datFile); + $lines = preg_split('@\n@',$content); + if(!empty($lines)) { foreach( $lines as $line ) { $data = array(); - foreach($format as $field=>$value) + foreach($format as $field=>$value) { $data[] = substr($line, ($value[0]-1), $value[1]); + } - if( !isset($data[2]) || (isset($data[2]) && !$data[2]) ) + if(!isset($data[2]) || (isset($data[2]) && !$data[2])){ continue; + } $id_sale = 0; $id_order = (string) $data[3]; @@ -95,227 +91,285 @@ $id_order = (int) $id_order; } - $order = new Order((int) ($id_order)); + if($detail = Db::getInstance()->getRow(' + SELECT `id_order_detail`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $id_order.' + AND ( + `product_ean13` = "'.pSQL($data[9]).'" + OR `product_supplier_reference` = "'.pSQL($data[9]).'" + ) + ')) { + $sent = (int) Db::getInstance()->getValue(' + SELECT SUM(`quantity`) + FROM `'._DB_PREFIX_.'philea_parcel` + WHERE `id_order_detail` = '.(int) $detail['id_order_detail'].' + '); - if(Validate::isLoadedObject($order)) - { - $order->shipping_number = trim($data[5]); - $order->id_sale_shipping = $id_sale; + $to_send = (int) $detail['quantity'] - $sent; - $new_id_order_state = $id_order_state; - - ## GET SHIPPING CARRIER ID - $id_active_carrier = FALSE; - $id_inactive_carrier = FALSE; - $carrier_found = FALSE; - if (trim($data[5]) && isset($data[8]) && $data[8]){ - $carrier_name = trim($data[8]); - - if ($carrier_name == '315') - $carrier_name = 'DUCROS'; - - $carriers = philea_magistor::getCarriersIdByRef($carrier_name); - if ($carriers){ - foreach ($carriers as $carrier) { - if ($carrier['name'] == 'GLS' && $carrier_name == 'DPD') - continue; - if ($carrier['active'] == 1 && $carrier['deleted'] == 0) - $id_active_carrier = (int) $carrier['id_carrier']; - if ($carrier['active'] == 0 && $carrier['deleted'] == 0) - $id_inactive_carrier = (int) $carrier['id_carrier']; - if ((int) $order->id_carrier == (int) $carrier['id_carrier']){ - $carrier_found = (int) $order->id_carrier; - } - } - } - } - if ($carrier_found) - $id_carrier = (int) $carrier_found; - elseif ($id_active_carrier) - $id_carrier = (int) $id_active_carrier; - elseif ($id_inactive_carrier) - $id_carrier = (int) $id_inactive_carrier; - else - $id_carrier = (int) $order->id_carrier; - $order->id_carrier_shipping = (int) $id_carrier; - - $order->update(); - - if (version_compare(_PS_VERSION_,'1.5','<')) - $last_order_state = $order->getCurrentState(); - else - $last_order_state = $order->getCurrentOrderState(); - - // ANTADIS - $is_single = !((bool) Db::getInstance()->getRow(' - SELECT * - FROM `'._DB_PREFIX_.'product_ps_cache` c - LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`product_id` = c.`id_product`) - WHERE d.`id_order` = '.(int) $order->id.' - ')); - - // PRODUCTS FROM OTHER SALES - if(!$is_single) { - $sent = TRUE; - - // GET QUANTITIES - SENT BY LAPOSTE - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order_detail`, d.`product_id`, d.`product_attribute_id`, SUM(d.`product_quantity` - GREATEST(d.`product_quantity_return`, d.`product_quantity_refunded`) - IFNULL(l.`quantity`, 0)) AS `quantity` - FROM `'._DB_PREFIX_.'order_detail` d - LEFT JOIN `'._DB_PREFIX_.'lapostews` l - ON d.`id_order_detail` = l.`id_order_detail` - WHERE d.`id_order` = '.(int) $order->id.' - GROUP BY d.`id_order_detail` - ') as $row) { - // GET QUANTITIES - SENT BY LAPOSTE - SENT BY EXAPAQ - $row['quantity'] -= (int) Db::getInstance()->getValue(' - SELECT SUM(`quantity`) - FROM `'._DB_PREFIX_.'exapaqws` - WHERE `id_order_detail` = '.(int) $row['id_order_detail'].' + if((int) $data[10] <= $to_send) { + if(trim($data[5]) != '') { + Db::getInstance()->ExecuteS(' + INSERT INTO `'._DB_PREFIX_.'philea_parcel` + VALUES ( + '.(int) $detail['id_order_detail'].', + '.(int) $data[10].', + "'.pSQL(trim($data[5])).'", + "'.pSQL(substr($data[7], 0, 4).'-'.substr($data[7], 4, 2).'-'.substr($data[7], 6, 2).' 18:00:00').'" + ) '); - // IF QTIES > 0 (NOT ALL SENT) GET IF PRODUCT HAS BEEN SENT - // order has been registered in shipping_history for another shipping methode - if((int) $row['quantity'] > 0) { - if(!Db::getInstance()->getRow(' - SELECT c.`id_sale` - FROM `'._DB_PREFIX_.'product_ps_cache` c - LEFT JOIN `'._DB_PREFIX_.'shipping_history` h - ON h.`id_sale` = c.`id_sale` - WHERE c.`id_product` = '.(int) $row['product_id'].' - AND h.`id_order` = '.(int) $order->id.' - ')) { - $sent = FALSE; - break; + $id_order_details[] = (int) $detail['id_order_detail']; + $shipping_numbers[] = '"'.pSQL(trim($data[5])).'"'; + + if (isset($data[8]) && $data[8]){ + $carrier_name = trim($data[8]); + $carriers = philea_magistor::getCarriersIdByRef($carrier_name); + if ($carriers){ + foreach ($carriers as $carrier) { + if ($carrier['name'] == 'GLS' && $carrier_name == 'DPD') { + continue; + } + if ($carrier['active'] == 1 && $carrier['deleted'] == 0) { + $id_active_carrier = (int) $carrier['id_carrier']; + } + if ($carrier['active'] == 0 && $carrier['deleted'] == 0) { + $id_inactive_carrier = (int) $carrier['id_carrier']; + } + // if ((int) $order->id_carrier == (int) $carrier['id_carrier']){ + // $carrier_found = (int) $order->id_carrier; + // } + } } } + + if(isset($id_active_carrier) && $id_active_carrier) { + $parcel_carrier[(int) $id_order.'|'.trim($data[5])] = $carriers[$data[8]]; + } else { // MISSING CARRIER + $errors[] = array( + $data[3], + $data[8], // carrier name + $data[9], + $data[10], // quantity + $data[5], // shipping number + $data[7], + 'carrier_missing', + ); + } + } else { // SHIPPING NUMBER IS EMPTY + $errors[] = array( + $data[3], + $data[8], + $data[9], + $data[10], + $data[5], + $data[7], + 'empty_shipping_number', + ); } - - if(!$sent) { - $new_id_order_state = 17; - } - } - - $products_sent = ''; - - mail('marion@antadis.com', 'philea id op', serialize($data[3])); - - - if(substr((string) $data[3], 0, 2) == 'OP') { - $id_sale = explode('-', substr((string) $data[3], 2)); - if(count($id_sale) < 2) { - mail('marion@antadis.com', '[BBB] Philea erreur', serialize($data)); - exit; - } - $id_sale = (int) $id_sale[0]; } else { - $id_sale = 0; + // TOO MUCH + $errors[] = array( + $data[3], + $data[8], + $data[9], + $data[10], + $data[5], + $data[7], + 'wrong_quantities', + ); } - - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`product_name`, d.`product_quantity` - GREATEST(d.`product_quantity_return`, d.`product_quantity_refunded`) AS `quantity` - FROM `'._DB_PREFIX_.'order_detail` d - LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c - ON c.`id_product` = d.`product_id` - WHERE c.`id_sale` = '.(int) $id_sale.' - AND d.`id_order` = '.(int) $id_order.' - ') as $p) { - if((int) $p['quantity'] > 0) { - $products_sent .= '
'."\r\n".$p['quantity'].' x '.$p['product_name']; - } - } - - $templateVars = array( - '{product_list}' => !empty($products_sent)? 'Contenu du colis :'.$products_sent: '', - '{product_list_txt}' => !empty($products_sent)? 'Contenu du colis :'.strip_tags($products_sent): '', + } else { + // NOT FOUND + $errors[] = array( + $data[3], + $data[8], + $data[9], + $data[10], + $data[5], + $data[7], + 'product_not_found', ); + } + } - if(/*$last_order_state->id*/ $last_order_state != $new_id_order_state && $last_order_state != 5 && ($new_id_order_state != 17 || $last_order_state != 4)) - { - $id_order = (int)$order->id; - $new_history = new OrderHistory(); - $new_history->id_order = (int)$order->id; - $new_history->changeIdOrderState((int)$new_id_order_state, $id_order); - $new_history->addWithemail(TRUE, $templateVars); - } + $id_order_details = array_unique($id_order_details); + $shipping_numbers = array_unique($shipping_numbers); - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'philea_magistor_parcel` + if(count($id_order_details) > 0) { + $id_orders = array(); + $orders_infos = array(); + + foreach(Db::getInstance()->ExecuteQ(' + SELECT d.`id_order`, p.`shipping_number`, p.`date_add` + FROM `'._DB_PREFIX_.'philea_parcel` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON p.`id_order_detail` = d.`id_order_detail` + WHERE d.`id_order_detail` IN ('.implode(', ', $id_order_details).') + AND p.`shipping_number` IN ('.implode(', ', $shipping_numbers).') + GROUP BY d.`id_order`, p.`shipping_number` + ') as $row) { + Db::getInstance()->ExecuteS(' + INSERT INTO `'._DB_PREFIX_.'shipping_history` VALUES ( - '.(int) $order->id.', - "'.pSQl(trim($order->shipping_number)).'", - '.(int) $id_sale.', - NOW() + '.(int) $row['id_order'].', + "'.pSQL($row['shipping_number']).'", + "'.pSQL($row['date_add']).'", + '.(int) $parcel_carrier[(int) $row['id_order'].'|'.$row['shipping_number']].', + 0 ) '); - if ($data[5]) - { - // $code_socolissimo = Db::getInstance()->getValue('SELECT delivery_mode FROM `'._DB_PREFIX_.'socolissimo_delivery_info` WHERE id_cart = '.$order->id_cart); - // $key_carrier = $order->id_carrier.':'.($code_socolissimo? $code_socolissimo: ''); - // if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) { - // $carrier_value = $socol_to_magistor[$code_socolissimo]; - // } else { - // if(!isset($tab_conversion_carrier[$key_carrier])) { - // $carrier_value = $socol_to_magistor['DOM']; - // } else { - // $carrier_value = $tab_conversion_carrier[$key_carrier]; - // } - // } + $id_orders[] = (int) $row['id_order']; + if(!isset($orders_infos[(int) $row['id_order']])) { + $orders_infos[(int) $row['id_order']] = array(); + } + $orders_infos[(int) $row['id_order']][] = array($row['shipping_number'], $row['date_add'], (int) $parcel_carrier[(int) $row['id_order'].'|'.$row['shipping_number']]); + } - // $code_socolissimo = FALSE; - // $code_mr = FALSE; - // $relay_code = FALSE; + $id_orders = array_unique($id_orders); - // // GET DELIVERY METHOD - // $code_socolissimo = Db::getInstance()->getValue('SELECT delivery_mode FROM `'._DB_PREFIX_.'socolissimo_delivery_info` WHERE id_cart = '.$order->id_cart); - // // Socolissimo delivery method - // if ($code_socolissimo){ - // $relay_code = $code_socolissimo; - // } - // elseif (Module::isInstalled('mondialrelay')){ - // $code_mr = Db::getInstance()->getValue(' - // SELECT mr_m.`dlv_mode` - // FROM `'._DB_PREFIX_.'mr_selected` mr_s - // LEFT JOIN `'._DB_PREFIX_.'mr_method` mr_m - // ON (mr_m.id_mr_method = mr_s.id_method) - // WHERE mr_s.`id_cart` = ' . (int) $order->id_cart - // ); - // // Mondial Relay delivery method - // if ($code_mr) - // $relay_code = $code_mr; - // } - // $key_carrier = $order->id_carrier.':'.($relay_code? $relay_code: ''); - // if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) { - // $carrier_value = $socol_to_magistor[$code_socolissimo]; - // } elseif($code_mr && isset($mr_to_magistor[$code_mr])) { - // $carrier_value = $mr_to_magistor[$code_mr]; - // } else { - // if(!isset($tab_conversion_carrier[$key_carrier])) { - // $carrier_value = $socol_to_magistor['DOM']; - // } else { - // $carrier_value = $tab_conversion_carrier[$key_carrier]; - // } - // } - // if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) { - // $carrier_value = $socol_to_magistor[$code_socolissimo]; - // } else { - // if(!isset($tab_conversion_carrier[$key_carrier])) { - // $carrier_value = $socol_to_magistor['DOM']; - // } else { - // $carrier_value = $tab_conversion_carrier[$key_carrier]; - // } - // } + $products = Db::getInstance()->ExecuteQ(' + SELECT d.`id_order_detail`, d.`id_order`, c.`id_sale`, d.`product_quantity` - GREATEST(d.`product_quantity_refunded`, d.`product_quantity_return`) AS `quantity` + FROM ps_order_detail d + LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c + ON d.`product_id` = c.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $id_orders).') + '); - ## GET DELIVERY METHOD - // $carrier = new Carrier((int)($order->id_carrier)); - $customer = new Customer((int) $order->id_customer); - $carrier = new Carrier((int) $order->id_carrier_shipping); - if (!Validate::isLoadedObject($customer) OR !Validate::isLoadedObject($carrier)) - die(Tools::displayError()); + $sent_sales = array(); + foreach(Db::getInstance()->ExecuteQ(' + SELECT `id_order`, `id_sale` + FROM `'._DB_PREFIX_.'shipping_history` + WHERE `id_order` IN ('.implode(', ', $id_orders).') + AND `id_sale` != 0 + GROUP BY `id_order`, `id_sale` + ') as $row) { + if(!isset($sent_sales[(int) $row['id_order']])) { + $sent_sales[(int) $row['id_order']] = array(); + } + $sent_sales[(int) $row['id_order']][] = (int) $row['id_sale']; + } + + $to_send = array(); + foreach($products as $product) { + if((int) $product['quantity'] > 0 + && (!isset($sent_sales[(int) $product['id_order']]) + || !in_array((int) $product['id_sale'], $sent_sales[(int) $product['id_order']]))) { + $to_send[] = $product; + } + } + + unset($products); + + $sent_products = array(); + foreach(Db::getInstance()->ExecuteQ(' + SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity` + FROM `'._DB_PREFIX_.'lapostews` l + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON l.`id_order_detail` = d.`id_order_detail` + WHERE d.`id_order` IN ('.implode(', ', $id_orders).') + GROUP BY l.`id_order_detail` + ') as $row) { + $sent_products[(int) $row['id_order_detail']] = (int) $row['quantity']; + } + foreach(Db::getInstance()->ExecuteQ(' + SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity` + FROM `'._DB_PREFIX_.'philea_parcel` l + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON l.`id_order_detail` = d.`id_order_detail` + WHERE d.`id_order` IN ('.implode(', ', $id_orders).') + GROUP BY l.`id_order_detail` + ') as $row) { + $sent_products[(int) $row['id_order_detail']] = (int) $row['quantity']; + } + + $orders_to_send = array(); + $orders_list = array(); + + $total = count($to_send); + + foreach($to_send as $product) { + if(!in_array((int) $product['id_order'], $orders_list)) { + $orders_list[] = (int) $product['id_order']; + } + if(!in_array((int) $product['id_order'], $orders_to_send)) { + if(!isset($sent_products[(int) $product['id_order_detail']]) + || $product['quantity'] > $sent_products[(int) $product['id_order_detail']]) { + $orders_to_send[] = (int) $product['id_order']; + } + } + } + + unset($sent_products); + unset($to_send); + + $orders_sent = array(); + foreach($orders_list as $order) { + if(!in_array($order, $orders_to_send)) { + $orders_sent[] = $order; + } + } + + unset($orders_list); + + $status_sent = array(); + $status_partially_sent = array(); + + foreach($orders_sent as $order) { + if((int) Db::getInstance()->getValue(' + SELECT `id_order_state` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order` = '.(int) $order.' + ORDER BY `date_add` DESC + ') != 4) { + $status_sent[] = (int) $order; + } + } + + foreach($id_orders as $order) { + if(!in_array($order, $status_sent)) { + if(!in_array((int) Db::getInstance()->getValue(' + SELECT `id_order_state` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order` = '.(int) $order.' + ORDER BY `date_add` DESC + '), array(4, 17, 9))) { + $status_partially_sent[] = (int) $order; + } + } + } + + unset($orders_sent); + unset($orders_to_send); + + foreach($orders_infos as $id_order => $parcels) { + $order = new Order($id_order); + $customer = new Customer((int) $order->id_customer); + + foreach($parcels as $parcel) { + $products_sent = ''; + + $products_names = array(); + + foreach(Db::getInstance()->ExecuteQ(' + SELECT d.`id_order_detail`, d.`product_name`, SUM(p.`quantity`) AS `quantity` + FROM `'._DB_PREFIX_.'philea_parcel` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON p.`id_order_detail` = d.`id_order_detail` + WHERE d.`id_order` = '.(int) $id_order.' + AND p.`shipping_number` = "'.pSQL($parcel[0]).'" + AND p.`date_add` = "'.pSQL($parcel[1]).'" + GROUP BY d.`id_order_detail` + ') as $p) { + $products_sent .= '
'."\r\n".$p['quantity'].' x '.$p['product_name']; + } + + $carrier = new Carrier($parcel[2], (int) $order->id_lang); if((int) $order->id_lang == 3) { - if(preg_match('/colissimo/i', $carrier->name)) { + if(in_array((int) $carrier->id, array(145, 152, 103))) { $carrier->url .= '&language=es_ES'; } $content_html = 'Contenido del paquete:'; @@ -326,26 +380,47 @@ } $templateVars = array( - '{product_list}' => !empty($products_sent)? $content_html.$products_sent: '', - '{product_list_txt}' => !empty($products_sent)? $content_txt.strip_tags($products_sent): '', - '{followup}' => str_replace('@', trim($order->shipping_number), $carrier->url), + '{followup}' => str_replace('@', $parcel[0], $carrier->url), '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, - '{id_order}' => (int)($order->id) + '{id_order}' => (int) $order->id, + '{product_list}' => !empty($products_sent)? $content_html.$products_sent: '', + '{product_list_txt}' => !empty($products_sent)? $content_txt.strip_tags($products_sent): '', ); - $title = array( + if(in_array($order->id, $status_sent)) { + $history = new OrderHistory(); + $history->id_order = (int) $order->id; + $history->changeIdOrderState(4, (int) $order->id); + $history->addWithemail(TRUE, $templateVars); + } elseif(in_array($order->id, $status_partially_sent)) { + $history = new OrderHistory(); + $history->id_order = (int) $order->id; + $history->changeIdOrderState(17, (int) $order->id); + $history->addWithemail(TRUE, $templateVars); + } + + $subjects = array( + 1 => 'Package in transit', 2 => 'Livraison en cours', - 3 => 'Paquete en transito', - 5 => 'Consegna in corso' + 3 => 'Pedido en trĂ¡nsito', ); - Mail::Send((int)($order->id_lang), 'in_transit', $title[(int)$order->id_lang], $templateVars, - $customer->email, $customer->firstname.' '.$customer->lastname, NULL, NULL, NULL, NULL, - _PS_MAIL_DIR_, true); + Mail::Send( + intval($order->id_lang), + 'in_transit', + $subjects[(int) $order->id_lang], + $templateVars, + $customer->email, + $customer->firstname.' '.$customer->lastname + ); } } } + + if(count($errors) > 0) { + mail('marion@antadis.com', '[BBB] Erreurs integration envoi Philea', serialize($errors)); + } } $repo_archive = './archives/IN/LIVRAISON/'; diff --git a/modules/philea_magistor/script/send_commande.php b/modules/philea_magistor/script/send_commande.php index 9a1fabaf..78d6ae31 100644 --- a/modules/philea_magistor/script/send_commande.php +++ b/modules/philea_magistor/script/send_commande.php @@ -54,7 +54,7 @@ if($magistorModule->active) { FROM `'._DB_PREFIX_.'order_state_current` o LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON o.`id_order` = d.`id_order` LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c ON d.`product_id` = c.`id_product` - LEFT JOIN `'._DB_PREFIX_.'philea_magistor_sent` pms ON (pms.`id_sale` = c.`id_sale` AND pms.`id_order` = o.`id_order`) + LEFT JOIN `'._DB_PREFIX_.'philea_sent` pms ON (pms.`id_sale` = c.`id_sale` AND pms.`id_order` = o.`id_order`) WHERE o.`id_order_state` IN (2, 3, 4, 13, 17, 9, 18, 19) AND c.`id_sale` = '.(int) $id_sale.' AND pms.`id_order` IS NULL @@ -70,7 +70,7 @@ if($magistorModule->active) { $exclude_orders = array(); // foreach($db->ExecuteS(' // SELECT DISTINCT `id_order` - // FROM `'._DB_PREFIX_.'philea_magistor_sent` + // FROM `'._DB_PREFIX_.'philea_sent` // WHERE `id_sale` = '.(int) $id_sale.' // ') as $row) { // $exclude_orders[] = (int) $row['id_order']; @@ -259,8 +259,8 @@ if($magistorModule->active) { $data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_7 $data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_8 - if(Configuration::get('PHILEA_MAGISTOR_ASSURANCE') - && Configuration::get('PHILEA_MAGISTOR_ASSURANCE') <= ($order->total_paid_real - $order->total_shipping) + if(Configuration::get('PHILEA_MAGISTOR_ASSURANCE') + && Configuration::get('PHILEA_MAGISTOR_ASSURANCE') <= ($order->total_paid_real - $order->total_shipping) && Configuration::get('PHILEA_MAGISTOR_ASSURANCE') > 0 ) { $data .= str_pad( str_replace('.',',',($order->total_paid_real - $order->total_shipping)), 50, ' ', STR_PAD_RIGHT ); // VALEUR DE COMMANDE @@ -299,7 +299,7 @@ if($magistorModule->active) { } Db::getInstance()->ExecuteS(' - INSERT INTO `'._DB_PREFIX_.'philea_magistor_sent` + INSERT INTO `'._DB_PREFIX_.'philea_sent` VALUES ( '.(int) $order->id.', '.(int) $id_sale.',