From abd8e8834387590545e571230bf6396bd9eee4b4 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Thu, 17 Aug 2017 11:22:54 +0200 Subject: [PATCH] addition of new delivery delay and cleaning of sale combination --- .../blockordersummary/blockordersummary.php | 10 +- .../blockordersummary/blockordersummary.tpl | 6 +- .../privatesales/AdminPrivateSalesSales.php | 5 + modules/privatesales/Sale.php | 3757 +++++++++-------- modules/privatesales_delay/delivery_date.tpl | 4 + modules/privatesales_delay/saledelay.php | 6 + override/controllers/CartController.php | 58 +- themes/site/product.tpl | 4 + themes/site/shopping-cart.tpl | 4 + themes/site_mobile/product.tpl | 4 + 10 files changed, 1964 insertions(+), 1894 deletions(-) diff --git a/modules/blockordersummary/blockordersummary.php b/modules/blockordersummary/blockordersummary.php index 00751772..0f1ebcfc 100644 --- a/modules/blockordersummary/blockordersummary.php +++ b/modules/blockordersummary/blockordersummary.php @@ -252,10 +252,10 @@ class BlockOrderSummary extends Module {

'.$data['carrier_name'].' '.Tools::displayPrice($cart->getOrderTotal(TRUE, 5),$currency).'

-
'.$this->l('Delivery delays').'
-

'.$data['delay_name'].'

'; +
'.$this->l('Delivery delays').'
'; if(isset($data['delivery_date'])){ foreach($data['delivery_date'] as $k => $date) { + echo '

'.$data['delay_name'][$k].'

'; if($k == 1) { echo '

'.$this->l('Date de réception prévue entre le').' '.$date['date_start']->format('d/m/Y').' '.$this->l('et le').' '.$date['date_end']->format('d/m/Y').' @@ -264,6 +264,8 @@ class BlockOrderSummary extends Module { echo '

'.$this->l('Livraison prévue avant le').' '.$this->l('24/12').'

'; } elseif ($k == 8){ echo '

'.$this->l('Envoi en 48h').'

'; + } elseif ($k == 9){ + echo '

'.$this->l('Delai indiqué sur la fiche produit').'

'; } } } @@ -428,6 +430,7 @@ class BlockOrderSummary extends Module { } $delivery_date = SaleDelay::getDeliveryDate($delays, null, $date, true); + $delay_name = array(); foreach($delays as $delay) { $row = Db::getInstance()->getRow(' SELECT l.`name` @@ -435,8 +438,7 @@ class BlockOrderSummary extends Module { WHERE l.`id_lang` = '.(int) $id_lang.' AND l.`id_delay`='.(int)$delay.' '); - $delay_name = $row['name']; - break; + $delay_name[(int)$delay] = $row['name']; } } diff --git a/modules/blockordersummary/blockordersummary.tpl b/modules/blockordersummary/blockordersummary.tpl index 4ce040b4..0c737e57 100644 --- a/modules/blockordersummary/blockordersummary.tpl +++ b/modules/blockordersummary/blockordersummary.tpl @@ -18,9 +18,9 @@
{l s='Delivery delays' mod='blockordersummary'}
-

{$delay_name}

{if isset($delivery_date)} {foreach from=$delivery_date key=k item=date} +

{$delay_name[$k]}

{if $k == 1}

{l s='Date de réception prévue entre le' mod='blockordersummary'} {$date.date_start|date_format:'%d/%m/%Y'} {l s='et le' mod='blockordersummary'} {$date.date_end|date_format:'%d/%m/%Y'} @@ -33,6 +33,10 @@

{l s='Envoi en 48h' mod='blockordersummary'}

+ {elseif $k == 9} +

+ {l s='Delai indiqué sur la fiche produit' mod='blockordersummary'} +

{/if} {/foreach} {/if} diff --git a/modules/privatesales/AdminPrivateSalesSales.php b/modules/privatesales/AdminPrivateSalesSales.php index 5f295e74..a35bedae 100755 --- a/modules/privatesales/AdminPrivateSalesSales.php +++ b/modules/privatesales/AdminPrivateSalesSales.php @@ -493,6 +493,11 @@ class AdminPrivateSalesSales extends AdminTab { 'value' => 8, 'selected' => ($this->cursale!==NULL && $this->cursale->delivery_delay== 8?true:false) ), + array( + 'label' => $this->l('Livraison spécifique'), + 'value' => 9, + 'selected' => ($this->cursale!==NULL && $this->cursale->delivery_delay== 8?true:false) + ), ); // Delivery delay $input = array( diff --git a/modules/privatesales/Sale.php b/modules/privatesales/Sale.php index 4fbcffdf..f8a7e5b3 100755 --- a/modules/privatesales/Sale.php +++ b/modules/privatesales/Sale.php @@ -3,1972 +3,2047 @@ if (!class_exists('Sale')) { class Sale { - var $id; - var $date_start; - var $date_end; - var $timelaps; - var $id_country = 0; - var $enabled = 0; - var $featured = 0; - var $logout = 0; - var $pub = 0; - var $new = 0; - var $braderie = 0; - var $forward_news = 0; - var $lock_position = 0; - var $id_category; - var $id_employee = 0; - var $shipping_fr = 0; - var $title = array(); - var $alias = array(); - var $groups = array(); - var $carriers = array(); - var $description = array(); - var $versions = array(); + var $id; + var $date_start; + var $date_end; + var $timelaps; + var $id_country = 0; + var $enabled = 0; + var $featured = 0; + var $logout = 0; + var $pub = 0; + var $new = 0; + var $braderie = 0; + var $forward_news = 0; + var $lock_position = 0; + var $id_category; + var $id_employee = 0; + var $shipping_fr = 0; + var $title = array(); + var $alias = array(); + var $groups = array(); + var $carriers = array(); + var $description = array(); + var $versions = array(); - public function __construct($id=NULL) { - if($id !== NULL) { - $sale = Sale::getSale($id); - if($sale) { - $this->id = $id; - $this->date_start = $sale['date_start']; - $this->date_end = $sale['date_end']; - $this->date_upd = $sale['date_upd']; - $this->id_country = $sale['id_country']; - $this->enabled = $sale['enabled']; - $this->featured = $sale['featured']; - $this->logout = $sale['logout']; - $this->pub = $sale['pub']; - $this->new = $sale['new']; - $this->braderie = $sale['braderie']; - $this->forward_news = $sale['forward_news']; - $this->id_category = $sale['id_category']; - $this->id_employee = $sale['id_employee']; - $this->shipping_fr = $sale['shipping_fr']; - $this->title = $sale['title']; - $this->alias = $sale['alias']; - $this->groups = $sale['groups']; - $this->carriers = $sale['carriers']; - $this->description = $sale['description']; - $this->video = $sale['video']; - $this->timelaps = $sale['timelaps']; - $this->sale_type = $sale['sale_type']; - $this->position = $sale['position']; - $this->lock_position = $sale['lock_position']; - $this->delivery_delay = $sale['delivery_delay']; - $this->versions = $sale['versions']; - } - } - } + public function __construct($id=NULL) { + if($id !== NULL) { + $sale = Sale::getSale($id); + if($sale) { + $this->id = $id; + $this->date_start = $sale['date_start']; + $this->date_end = $sale['date_end']; + $this->date_upd = $sale['date_upd']; + $this->id_country = $sale['id_country']; + $this->enabled = $sale['enabled']; + $this->featured = $sale['featured']; + $this->logout = $sale['logout']; + $this->pub = $sale['pub']; + $this->new = $sale['new']; + $this->braderie = $sale['braderie']; + $this->forward_news = $sale['forward_news']; + $this->id_category = $sale['id_category']; + $this->id_employee = $sale['id_employee']; + $this->shipping_fr = $sale['shipping_fr']; + $this->title = $sale['title']; + $this->alias = $sale['alias']; + $this->groups = $sale['groups']; + $this->carriers = $sale['carriers']; + $this->description = $sale['description']; + $this->video = $sale['video']; + $this->timelaps = $sale['timelaps']; + $this->sale_type = $sale['sale_type']; + $this->position = $sale['position']; + $this->lock_position = $sale['lock_position']; + $this->delivery_delay = $sale['delivery_delay']; + $this->versions = $sale['versions']; + } + } + } - public function getMobileMenu() { - include_once dirname(__FILE__).'/../blockprivatesalescategories/blockprivatesalescategories.php'; + public function getMobileMenu() { + include_once dirname(__FILE__).'/../blockprivatesalescategories/blockprivatesalescategories.php'; - $blockprivatesalescategories = new BlockPrivateSalesCategories(); - return $blockprivatesalescategories->buildSelector(array('sale' => $this)); - } + $blockprivatesalescategories = new BlockPrivateSalesCategories(); + return $blockprivatesalescategories->buildSelector(array('sale' => $this)); + } - public function isFinished($check_enabled=FALSE) { - if(($check_enabled === TRUE && $this->enabled || $check_enabled === FALSE) && strtotime($this->date_end) < mktime()) { - return TRUE; - } - return FALSE; - } + public function isFinished($check_enabled=FALSE) { + if(($check_enabled === TRUE && $this->enabled || $check_enabled === FALSE) && strtotime($this->date_end) < mktime()) { + return TRUE; + } + return FALSE; + } - public function isStarted($check_enabled=FALSE) { - if(($check_enabled === TRUE && $this->enabled || $check_enabled === FALSE) && strtotime($this->date_start) < mktime()) { - return TRUE; - } - return FALSE; - } + public function isStarted($check_enabled=FALSE) { + if(($check_enabled === TRUE && $this->enabled || $check_enabled === FALSE) && strtotime($this->date_start) < mktime()) { + return TRUE; + } + return FALSE; + } - public function save() { - if($this->id !== NULL) { - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'privatesale` SET - `date_start` = "'.pSQL($this->date_start).'", - `date_end` = "'.pSQL($this->date_end).'", - `id_country` = '.(int) $this->id_country.', - `enabled` = '.(int) $this->enabled.', - `featured` = '.(int) $this->featured.', - `logout` = '.(int) $this->logout.', - `new` = '.(int) $this->new.', - `braderie` = '.(int) $this->braderie.', - `forward_news` = '.(int) $this->forward_news.', - `public` = '.(int) $this->pub.', - `id_category` = '.(int) $this->id_category.', - `id_employee` = '.(int) $this->id_employee.', - `shipping_fr` = '.(int) $this->shipping_fr.', - `delivery_delay` = '.(int) $this->delivery_delay.', - `lock_position` = '.(int) $this->lock_position.', - `date_upd` = NOW() - WHERE `id_sale` = '.(int) $this->id.' - '); + public function save() { + if($this->id !== NULL) { + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'privatesale` SET + `date_start` = "'.pSQL($this->date_start).'", + `date_end` = "'.pSQL($this->date_end).'", + `id_country` = '.(int) $this->id_country.', + `enabled` = '.(int) $this->enabled.', + `featured` = '.(int) $this->featured.', + `logout` = '.(int) $this->logout.', + `new` = '.(int) $this->new.', + `braderie` = '.(int) $this->braderie.', + `forward_news` = '.(int) $this->forward_news.', + `public` = '.(int) $this->pub.', + `id_category` = '.(int) $this->id_category.', + `id_employee` = '.(int) $this->id_employee.', + `shipping_fr` = '.(int) $this->shipping_fr.', + `delivery_delay` = '.(int) $this->delivery_delay.', + `lock_position` = '.(int) $this->lock_position.', + `date_upd` = NOW() + WHERE `id_sale` = '.(int) $this->id.' + '); - // Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_type` WHERE `id_sale` = '.(int) $this->id); - // foreach($this->sale_type as $k => $sale_type) { - // Db::getInstance()->Execute(' - // INSERT INTO `'._DB_PREFIX_.'privatesale_type` VALUES ( - // '. (int)$this->id.', - // '. (int)$sale_type.' - // ) - // '); - // } + // Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_type` WHERE `id_sale` = '.(int) $this->id); + // foreach($this->sale_type as $k => $sale_type) { + // Db::getInstance()->Execute(' + // INSERT INTO `'._DB_PREFIX_.'privatesale_type` VALUES ( + // '. (int)$this->id.', + // '. (int)$sale_type.' + // ) + // '); + // } - Db::getInstance()->ExecuteS('DELETE FROM `'._DB_PREFIX_.'privatesale_site_version` WHERE `id_sale` = '.(int) $this->id.''); - foreach($this->versions as $version) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_site_version` VALUES ( - '.$this->id.', - "'.pSQL($version).'" - ) - '); - } + Db::getInstance()->ExecuteS('DELETE FROM `'._DB_PREFIX_.'privatesale_site_version` WHERE `id_sale` = '.(int) $this->id.''); + foreach($this->versions as $version) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_site_version` VALUES ( + '.$this->id.', + "'.pSQL($version).'" + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $this->id); - $sale_i18n = array(); - foreach($this->description as $k => $v) { - if(!isset($sale_i18n[$k])) { - $sale_i18n[$k] = array(); - } - $sale_i18n[$k]['description'] = $v; - } - foreach($this->video as $k => $v) { - if(!isset($sale_i18n[$k])) { - $sale_i18n[$k] = array(); - } - $sale_i18n[$k]['video'] = $v; - } - foreach($sale_i18n as $lang => $values) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_lang` VALUES ( - '.$this->id.', - '.$lang.', - "'.(isset($values['description'])? pSQL($values['description'], TRUE): '').'", - "'.(isset($values['video'])? pSQL($values['video']): '').'" - ) - '); - } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $this->id); + $sale_i18n = array(); + foreach($this->description as $k => $v) { + if(!isset($sale_i18n[$k])) { + $sale_i18n[$k] = array(); + } + $sale_i18n[$k]['description'] = $v; + } + foreach($this->video as $k => $v) { + if(!isset($sale_i18n[$k])) { + $sale_i18n[$k] = array(); + } + $sale_i18n[$k]['video'] = $v; + } + foreach($sale_i18n as $lang => $values) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_lang` VALUES ( + '.$this->id.', + '.$lang.', + "'.(isset($values['description'])? pSQL($values['description'], TRUE): '').'", + "'.(isset($values['video'])? pSQL($values['video']): '').'" + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $this->id); - foreach($this->groups as $group) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_group` VALUES ( - '.$this->id.', - '.$group.' - ) - '); - } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $this->id); + foreach($this->groups as $group) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_group` VALUES ( + '.$this->id.', + '.$group.' + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $this->id); - foreach($this->carriers as $carrier) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_carrier` VALUES ( - '.$this->id.', - '.$carrier.' - ) - '); - } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $this->id); + foreach($this->carriers as $carrier) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_carrier` VALUES ( + '.$this->id.', + '.$carrier.' + ) + '); + } - Module::hookExec('privatesales_update', array('sale' => $this)); - } else { - if(Sale::getSaleFromCategory($this->id_category) !== NULL) { - $this->enabled = FALSE; - } + Module::hookExec('privatesales_update', array('sale' => $this)); + } else { + if(Sale::getSaleFromCategory($this->id_category) !== NULL) { + $this->enabled = FALSE; + } - $sql_position = 'SELECT MAX(position) FROM '. _DB_PREFIX_.'privatesale'; - $position = Db::getInstance()->getValue($sql_position); + $sql_position = 'SELECT MAX(position) FROM '. _DB_PREFIX_.'privatesale'; + $position = Db::getInstance()->getValue($sql_position); - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale` VALUES ( - DEFAULT, - "'.pSQL($this->date_start).'", - "'.pSQL($this->date_end).'", - '.(int) $this->id_country.', - '.(int) $this->enabled.', - '.(int) $this->featured.', - '.(int) $this->logout.', - '.(int) $this->pub.', - '.(int) $this->new.', - '.(int) $this->braderie.', - '.(int) $this->forward_news.', - '.(int) $this->id_category.', - '.(int) $this->id_employee.', - '.(int) $this->shipping_fr.', - NOW(), - '.(int) $this->sale_type.', - '.(int) $this->delivery_delay.', - '.($position+1).', - 0 - ) - '); - $this->id = Db::getInstance()->Insert_ID(); + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale` VALUES ( + DEFAULT, + "'.pSQL($this->date_start).'", + "'.pSQL($this->date_end).'", + '.(int) $this->id_country.', + '.(int) $this->enabled.', + '.(int) $this->featured.', + '.(int) $this->logout.', + '.(int) $this->pub.', + '.(int) $this->new.', + '.(int) $this->braderie.', + '.(int) $this->forward_news.', + '.(int) $this->id_category.', + '.(int) $this->id_employee.', + '.(int) $this->shipping_fr.', + NOW(), + '.(int) $this->sale_type.', + '.(int) $this->delivery_delay.', + '.($position+1).', + 0 + ) + '); + $this->id = Db::getInstance()->Insert_ID(); - // Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_type` WHERE `id_sale` = '.(int) $this->id); - // foreach($this->sale_type as $k => $sale_type) { - // Db::getInstance()->Execute(' - // INSERT INTO `'._DB_PREFIX_.'privatesale_type` VALUES ( - // '. (int)$this->id.', - // '. (int)$sale_type.' - // ) - // '); - // } + // Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_type` WHERE `id_sale` = '.(int) $this->id); + // foreach($this->sale_type as $k => $sale_type) { + // Db::getInstance()->Execute(' + // INSERT INTO `'._DB_PREFIX_.'privatesale_type` VALUES ( + // '. (int)$this->id.', + // '. (int)$sale_type.' + // ) + // '); + // } - foreach($this->versions as $version) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_site_version` VALUES ( - '.$this->id.', - "'.pSQL($version).'" - ) - '); - } + foreach($this->versions as $version) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_site_version` VALUES ( + '.$this->id.', + "'.pSQL($version).'" + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $this->id); - $sale_i18n = array(); - foreach($this->description as $k => $v) { - if(!isset($sale_i18n[$k])) { - $sale_i18n[$k] = array(); - } - $sale_i18n[$k]['description'] = $v; - } - foreach($this->video as $k => $v) { - if(!isset($sale_i18n[$k])) { - $sale_i18n[$k] = array(); - } - $sale_i18n[$k]['video'] = $v; - } - foreach($sale_i18n as $lang => $values) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_lang` VALUES ( - '.$this->id.', - '.$lang.', - "'.(isset($values['description'])? pSQL($values['description'], TRUE): '').'", - "'.(isset($values['video'])? pSQL($values['video']): '').'" - ) - '); - } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $this->id); + $sale_i18n = array(); + foreach($this->description as $k => $v) { + if(!isset($sale_i18n[$k])) { + $sale_i18n[$k] = array(); + } + $sale_i18n[$k]['description'] = $v; + } + foreach($this->video as $k => $v) { + if(!isset($sale_i18n[$k])) { + $sale_i18n[$k] = array(); + } + $sale_i18n[$k]['video'] = $v; + } + foreach($sale_i18n as $lang => $values) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_lang` VALUES ( + '.$this->id.', + '.$lang.', + "'.(isset($values['description'])? pSQL($values['description'], TRUE): '').'", + "'.(isset($values['video'])? pSQL($values['video']): '').'" + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $this->id); - foreach($this->groups as $group) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_group` VALUES ( - '.$this->id.', - '.$group.' - ) - '); - } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $this->id); + foreach($this->groups as $group) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_group` VALUES ( + '.$this->id.', + '.$group.' + ) + '); + } - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $this->id); - foreach($this->carriers as $carrier) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_carrier` VALUES ( - '.$this->id.', - '.$carrier.' - ) - '); - } - // $today = strtotime(date("Y-m-d H:i:s")); - // $start = strtotime($sale->date_start); - // $end = strtotime($sale->date_end); - // if($start <= $today && $end > $today){ - // self::sortActiveSaleAfterAdd(1); - // } + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $this->id); + foreach($this->carriers as $carrier) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_carrier` VALUES ( + '.$this->id.', + '.$carrier.' + ) + '); + } + // $today = strtotime(date("Y-m-d H:i:s")); + // $start = strtotime($sale->date_start); + // $end = strtotime($sale->date_end); + // if($start <= $today && $end > $today){ + // self::sortActiveSaleAfterAdd(1); + // } - Module::hookExec('privatesales_create', array('sale' => $this)); - } - } + Module::hookExec('privatesales_create', array('sale' => $this)); + } + } - public static function deleteSale($id) { - if($sale = Sale::getSale($id)) { - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $id); - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $id); - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale` WHERE `id_sale` = '.(int) $id); - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_category` WHERE `id_sale` = '.(int) $id); - Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $id); - Sale::rrmdir(_PS_ROOT_DIR_.'/modules/privatesales/img/'.$id); - Module::hookExec('privatesales_delete', array('sale' => $this)); - } - } + public static function deleteSale($id) { + if($sale = Sale::getSale($id)) { + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_lang` WHERE `id_sale` = '.(int) $id); + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_group` WHERE `id_sale` = '.(int) $id); + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale` WHERE `id_sale` = '.(int) $id); + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_category` WHERE `id_sale` = '.(int) $id); + Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $id); + Sale::rrmdir(_PS_ROOT_DIR_.'/modules/privatesales/img/'.$id); + Module::hookExec('privatesales_delete', array('sale' => $this)); + } + } - public static function lockPosition($id) { - if($sale = Sale::getSale($id)) { - Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 1 WHERE id_sale = '. $id); - } - } + public static function lockPosition($id) { + if($sale = Sale::getSale($id)) { + Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 1 WHERE id_sale = '. $id); + } + } - public static function unlockPosition($id) { - if($sale = Sale::getSale($id)) { - Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 0 WHERE id_sale = '. $id); - } - } + public static function unlockPosition($id) { + if($sale = Sale::getSale($id)) { + Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 0 WHERE id_sale = '. $id); + } + } - public static function sortActiveSaleAfterAdd($nb = 1){ - for ($i=0; $i < $nb; $i++) { - foreach(Db::getInstance()->executeS(' - SELECT id_sale, position - FROM '._DB_PREFIX_.'privatesale - WHERE lock_position = 1 - AND `date_end` > NOW() - AND `date_start` < NOW() - ORDER BY position DESC - ') as $sale){ - $pos = $sale['position']+1; + public static function sortActiveSaleAfterAdd($nb = 1){ + for ($i=0; $i < $nb; $i++) { + foreach(Db::getInstance()->executeS(' + SELECT id_sale, position + FROM '._DB_PREFIX_.'privatesale + WHERE lock_position = 1 + AND `date_end` > NOW() + AND `date_start` < NOW() + ORDER BY position DESC + ') as $sale){ + $pos = $sale['position']+1; - Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = position-1 WHERE position = '. $pos); - Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = position+1 WHERE id_sale = '. $sale['id_sale']); - } - } - } + Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = position-1 WHERE position = '. $pos); + Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = position+1 WHERE id_sale = '. $sale['id_sale']); + } + } + } - public static function rrmdir($dir) { - if(is_dir($dir)) { - $objects = scandir($dir); - foreach($objects as $object) { - if($object != '.' && $object != '..') { - if(filetype($dir.'/'.$object) == 'dir') { - rrmdir($dir.'/'.$object); - } else { - unlink($dir."/".$object); - } - } - } - reset($objects); - rmdir($dir); - } - } + public static function rrmdir($dir) { + if(is_dir($dir)) { + $objects = scandir($dir); + foreach($objects as $object) { + if($object != '.' && $object != '..') { + if(filetype($dir.'/'.$object) == 'dir') { + rrmdir($dir.'/'.$object); + } else { + unlink($dir."/".$object); + } + } + } + reset($objects); + rmdir($dir); + } + } - public static function getSale($id) { - if(!($ps = Db::getInstance()->ExecuteS(' - SELECT * - FROM `'._DB_PREFIX_.'privatesale` - WHERE `id_sale` = '.(int) $id - )) || count($ps) == 0) { - return false; - } + public static function getSale($id) { + if(!($ps = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'privatesale` + WHERE `id_sale` = '.(int) $id + )) || count($ps) == 0) { + return false; + } - $d1 = new DateTime(date('Y-m-d H:i:s')); - $d2 = new DateTime($ps[0]['date_end']); - $diff = $d1->diff($d2); + $d1 = new DateTime(date('Y-m-d H:i:s')); + $d2 = new DateTime($ps[0]['date_end']); + $diff = $d1->diff($d2); - $result = array( - 'id_sale' => $ps[0]['id_sale'], - 'date_start' => $ps[0]['date_start'], - 'date_end' => $ps[0]['date_end'], - 'date_upd' => $ps[0]['date_upd'], - 'timelaps' => $diff, - 'id_country' => $ps[0]['id_country'], - 'enabled' => $ps[0]['enabled'], - 'featured' => $ps[0]['featured'], - 'logout' => $ps[0]['logout'], - 'pub' => $ps[0]['public'], - 'new' => $ps[0]['new'], - 'braderie' => $ps[0]['braderie'], - 'forward_news' => $ps[0]['forward_news'], - 'position' => $ps[0]['position'], - 'lock_position' => $ps[0]['lock_position'], - 'id_category' => $ps[0]['id_category'], - 'id_employee' => $ps[0]['id_employee'], - 'shipping_fr' => $ps[0]['shipping_fr'], - 'delivery_delay' => $ps[0]['delivery_delay'], - 'sale_type' => array(), - 'title' => array(), - 'alias' => array(), - 'groups' => array(), - 'carriers' => array(), - 'description' => array(), - 'versions' => array(), - 'video' => array(), - ); + $result = array( + 'id_sale' => $ps[0]['id_sale'], + 'date_start' => $ps[0]['date_start'], + 'date_end' => $ps[0]['date_end'], + 'date_upd' => $ps[0]['date_upd'], + 'timelaps' => $diff, + 'id_country' => $ps[0]['id_country'], + 'enabled' => $ps[0]['enabled'], + 'featured' => $ps[0]['featured'], + 'logout' => $ps[0]['logout'], + 'pub' => $ps[0]['public'], + 'new' => $ps[0]['new'], + 'braderie' => $ps[0]['braderie'], + 'forward_news' => $ps[0]['forward_news'], + 'position' => $ps[0]['position'], + 'lock_position' => $ps[0]['lock_position'], + 'id_category' => $ps[0]['id_category'], + 'id_employee' => $ps[0]['id_employee'], + 'shipping_fr' => $ps[0]['shipping_fr'], + 'delivery_delay' => $ps[0]['delivery_delay'], + 'sale_type' => array(), + 'title' => array(), + 'alias' => array(), + 'groups' => array(), + 'carriers' => array(), + 'description' => array(), + 'versions' => array(), + 'video' => array(), + ); - // $types = Db::getInstance()->ExecuteS(' - // SELECT * - // FROM `'._DB_PREFIX_.'privatesale_type` - // WHERE `id_sale` = '.(int) $id - // ); - // foreach ($types as $key => $type) { - // $result['sale_type'][$key] = $type['id_sale_type']; - // } + // $types = Db::getInstance()->ExecuteS(' + // SELECT * + // FROM `'._DB_PREFIX_.'privatesale_type` + // WHERE `id_sale` = '.(int) $id + // ); + // foreach ($types as $key => $type) { + // $result['sale_type'][$key] = $type['id_sale_type']; + // } - $psl = Db::getInstance()->ExecuteS(' - SELECT * - FROM `'._DB_PREFIX_.'privatesale_lang` - WHERE `id_sale` = '.(int) $id - ); - foreach($psl as $l) { - $result['description'][$l['id_lang']] = $l['description']; - $result['video'][$l['id_lang']] = $l['video']; - } + $psl = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'privatesale_lang` + WHERE `id_sale` = '.(int) $id + ); + foreach($psl as $l) { + $result['description'][$l['id_lang']] = $l['description']; + $result['video'][$l['id_lang']] = $l['video']; + } - $category_i18n = Db::getInstance()->ExecuteS(' - SELECT `id_lang`, `name`, `link_rewrite` - FROM `'._DB_PREFIX_.'category_lang` - WHERE `id_category` = '.$ps[0]['id_category'] - ); - foreach($category_i18n as $c) { - $result['title'][$c['id_lang']] = $c['name']; - $result['alias'][$c['id_lang']] = $c['link_rewrite']; - } + $category_i18n = Db::getInstance()->ExecuteS(' + SELECT `id_lang`, `name`, `link_rewrite` + FROM `'._DB_PREFIX_.'category_lang` + WHERE `id_category` = '.$ps[0]['id_category'] + ); + foreach($category_i18n as $c) { + $result['title'][$c['id_lang']] = $c['name']; + $result['alias'][$c['id_lang']] = $c['link_rewrite']; + } - $sale_groups = Db::getInstance()->ExecuteS(' - SELECT `id_group` - FROM `'._DB_PREFIX_.'privatesale_group` - WHERE `id_sale` = '.(int) $id - ); - foreach($sale_groups as $group) { - $result['groups'][] = $group['id_group']; - } + $sale_groups = Db::getInstance()->ExecuteS(' + SELECT `id_group` + FROM `'._DB_PREFIX_.'privatesale_group` + WHERE `id_sale` = '.(int) $id + ); + foreach($sale_groups as $group) { + $result['groups'][] = $group['id_group']; + } - $sale_carriers = Db::getInstance()->ExecuteS(' - SELECT `id_carrier` - FROM `'._DB_PREFIX_.'privatesale_carrier` - WHERE `id_sale` = '.(int) $id - ); - foreach($sale_carriers as $carrier) { - $result['carriers'][] = $carrier['id_carrier']; - } + $sale_carriers = Db::getInstance()->ExecuteS(' + SELECT `id_carrier` + FROM `'._DB_PREFIX_.'privatesale_carrier` + WHERE `id_sale` = '.(int) $id + ); + foreach($sale_carriers as $carrier) { + $result['carriers'][] = $carrier['id_carrier']; + } - foreach(Db::getInstance()->ExecuteS(' - SELECT `version` - FROM `'._DB_PREFIX_.'privatesale_site_version` - WHERE `id_sale` = '.(int) $id - ) as $version) { - $result['versions'][] = $version['version']; - } + foreach(Db::getInstance()->ExecuteS(' + SELECT `version` + FROM `'._DB_PREFIX_.'privatesale_site_version` + WHERE `id_sale` = '.(int) $id + ) as $version) { + $result['versions'][] = $version['version']; + } - return $result; - } + return $result; + } - public function getOrders() { - $orders = array(); - $products = $this->getProducts(); + public function getOrders() { + $orders = array(); + $products = $this->getProducts(); - if(count($products) > 0) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $this->getProducts()).') - ') as $order) { - $orders[] = $order['id_order']; - } - } + if(count($products) > 0) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $this->getProducts()).') + ') as $order) { + $orders[] = $order['id_order']; + } + } - return $orders; - } + return $orders; + } - public function getMultiForStats($orders = array(), $nb_sale, $strict = false) - { - $products = $this->getProducts(); + public function getMultiForStats($orders = array(), $nb_sale, $strict = false) + { + $products = $this->getProducts(); - if(count($products) > 0) { - if (count($orders) > 0) { - $_orders = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $orders).') - ORDER BY d.`product_quantity` ASC - ') as $row) { - if(!isset($_orders[(int)$row['id_order']])) { - $_orders[(int)$row['id_order']] = array(); - } - if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ - $_orders[(int)$row['id_order']][$row['id_sale']] = 1; - } - } + if(count($products) > 0) { + if (count($orders) > 0) { + $_orders = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $orders).') + ORDER BY d.`product_quantity` ASC + ') as $row) { + if(!isset($_orders[(int)$row['id_order']])) { + $_orders[(int)$row['id_order']] = array(); + } + if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ + $_orders[(int)$row['id_order']][$row['id_sale']] = 1; + } + } - $multi = array(); - foreach ($_orders as $key => $order) { - if($strict) { - if(count($order) == (int)$nb_sale) { - $multi[] = $key; - } - } else { - if(count($order) >= (int)$nb_sale) { - $multi[] = $key; - } - } - } - return $multi; - } - } else { - return array(); - } - } + $multi = array(); + foreach ($_orders as $key => $order) { + if($strict) { + if(count($order) == (int)$nb_sale) { + $multi[] = $key; + } + } else { + if(count($order) >= (int)$nb_sale) { + $multi[] = $key; + } + } + } + return $multi; + } + } else { + return array(); + } + } - public function getOrdersFromSaleMulti($id_status=NULL, $nb_sale = 2) { - $orders = array(); - $products = $this->getProducts(); + public function getOrdersFromSaleMulti($id_status=NULL, $nb_sale = 2) { + $orders = array(); + $products = $this->getProducts(); - if(count($products) > 0) { - if($id_status === NULL) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $products).') - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } else { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ('.implode(',', $id_status).') - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $products).') - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } + if(count($products) > 0) { + if($id_status === NULL) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $products).') + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } else { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ('.implode(',', $id_status).') + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $products).') + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } - $orders_not_sended = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT(o.`id_order`) AS `id_order` - FROM `'._DB_PREFIX_.'order_detail` od - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` - WHERE o.`id_order` IN ('.implode(', ', $orders).') - AND od.`product_id` IN ('.implode(', ', $products).') - AND od.`id_order_detail` NOT IN ( - SELECT `id_order_detail` - FROM `'._DB_PREFIX_.'lapostews` la - WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) - )' - ) as $order) { - $orders_not_sended[] = $order['id_order']; - }; + $orders_not_sended = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT(o.`id_order`) AS `id_order` + FROM `'._DB_PREFIX_.'order_detail` od + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` + WHERE o.`id_order` IN ('.implode(', ', $orders).') + AND od.`product_id` IN ('.implode(', ', $products).') + AND od.`id_order_detail` NOT IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'lapostews` la + WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) + )' + ) as $order) { + $orders_not_sended[] = $order['id_order']; + }; - $orders2 = array(); - if(count($orders) > 0) { - $_orders = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $orders_not_sended).') - ORDER BY d.`product_quantity` ASC - ') as $row) { - if(!isset($_orders[(int)$row['id_order']])) { - $_orders[(int)$row['id_order']] = array(); - } - if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ - $_orders[(int)$row['id_order']][$row['id_sale']] = 1; - } - } + $orders2 = array(); + if(count($orders) > 0) { + $_orders = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $orders_not_sended).') + ORDER BY d.`product_quantity` ASC + ') as $row) { + if(!isset($_orders[(int)$row['id_order']])) { + $_orders[(int)$row['id_order']] = array(); + } + if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ + $_orders[(int)$row['id_order']][$row['id_sale']] = 1; + } + } - $order_print = array(); - foreach ($_orders as $key => $order) { - // M2 - if($nb_sale == 2) { - if( count($order) == $nb_sale) { - $order_print[] = $key; - } - } - } + $order_print = array(); + foreach ($_orders as $key => $order) { + // M2 + if($nb_sale == 2) { + if( count($order) == $nb_sale) { + $order_print[] = $key; + } + } + } - $order_print_range = array(); - foreach ($order_print as $key => $order) { - $total = Db::getInstance()->getValue(' - SELECT COUNT(d.`id_order_detail`) as total_ref - FROM `'._DB_PREFIX_.'order_detail` d - WHERE `id_order` = '. (int)$order .' - AND `id_order_detail` NOT IN ( - SELECT DISTINCT `id_order_detail` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $products).') - ) - ORDER BY total_ref ASC' - ); - $order_print_range[$order] = $total; - } + $order_print_range = array(); + foreach ($order_print as $key => $order) { + $total = Db::getInstance()->getValue(' + SELECT COUNT(d.`id_order_detail`) as total_ref + FROM `'._DB_PREFIX_.'order_detail` d + WHERE `id_order` = '. (int)$order .' + AND `id_order_detail` NOT IN ( + SELECT DISTINCT `id_order_detail` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $products).') + ) + ORDER BY total_ref ASC' + ); + $order_print_range[$order] = $total; + } - unset($order_print); - asort($order_print_range); - $order_print = array_keys($order_print_range); - } - } + unset($order_print); + asort($order_print_range); + $order_print = array_keys($order_print_range); + } + } - if(count($order_print) > 0) { - $printed = array(); - foreach($order_print as $order) { - $printed[] = '('.(int) $order.')'; - } + if(count($order_print) > 0) { + $printed = array(); + foreach($order_print as $order) { + $printed[] = '('.(int) $order.')'; + } - Db::getInstance()->ExecuteS(' - INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - VALUES '.implode(', ', $printed).' - '); - } + Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + VALUES '.implode(', ', $printed).' + '); + } - return $order_print; - } + return $order_print; + } - // public function getOrdersFromSaleMulti($id_status=NULL, $nb_sale = 2) { - // $orders = array(); - // $products = $this->getProducts(); + // public function getOrdersFromSaleMulti($id_status=NULL, $nb_sale = 2) { + // $orders = array(); + // $products = $this->getProducts(); - // if(count($products) > 0) { - // if($id_status === NULL) { - // foreach(Db::getInstance()->ExecuteS(' - // SELECT DISTINCT `id_order` - // FROM `'._DB_PREFIX_.'order_detail` - // WHERE `product_id` IN ('.implode(', ', $products).') - // ') as $order) { - // $orders[] = (int) $order['id_order']; - // } - // } else { - // foreach(Db::getInstance()->ExecuteS(' - // SELECT DISTINCT h1.`id_order` AS `id_order` - // FROM `'._DB_PREFIX_.'order_history` h1 - // LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - // ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - // WHERE h2.`id_order_history` IS NULL - // AND h1.`id_order_state` IN ('.implode(',', $id_status).') - // AND h1.`id_order` IN ( - // SELECT DISTINCT `id_order` - // FROM `'._DB_PREFIX_.'order_detail` - // WHERE `product_id` IN ('.implode(', ', $products).') - // ) - // ') as $order) { - // $orders[] = (int) $order['id_order']; - // } - // } + // if(count($products) > 0) { + // if($id_status === NULL) { + // foreach(Db::getInstance()->ExecuteS(' + // SELECT DISTINCT `id_order` + // FROM `'._DB_PREFIX_.'order_detail` + // WHERE `product_id` IN ('.implode(', ', $products).') + // ') as $order) { + // $orders[] = (int) $order['id_order']; + // } + // } else { + // foreach(Db::getInstance()->ExecuteS(' + // SELECT DISTINCT h1.`id_order` AS `id_order` + // FROM `'._DB_PREFIX_.'order_history` h1 + // LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + // ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + // WHERE h2.`id_order_history` IS NULL + // AND h1.`id_order_state` IN ('.implode(',', $id_status).') + // AND h1.`id_order` IN ( + // SELECT DISTINCT `id_order` + // FROM `'._DB_PREFIX_.'order_detail` + // WHERE `product_id` IN ('.implode(', ', $products).') + // ) + // ') as $order) { + // $orders[] = (int) $order['id_order']; + // } + // } - // $orders_not_sended = array(); - // foreach(Db::getInstance()->ExecuteS(' - // SELECT DISTINCT(o.`id_order`) AS `id_order` - // FROM `'._DB_PREFIX_.'order_detail` od - // LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` - // WHERE o.`id_order` IN ('.implode(', ', $orders).') - // AND od.`product_id` IN ('.implode(', ', $products).') - // AND od.`id_order_detail` NOT IN ( - // SELECT `id_order_detail` - // FROM `'._DB_PREFIX_.'lapostews` la - // WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) - // )' - // ) as $order) { - // $orders_not_sended[] = $order['id_order']; - // }; + // $orders_not_sended = array(); + // foreach(Db::getInstance()->ExecuteS(' + // SELECT DISTINCT(o.`id_order`) AS `id_order` + // FROM `'._DB_PREFIX_.'order_detail` od + // LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` + // WHERE o.`id_order` IN ('.implode(', ', $orders).') + // AND od.`product_id` IN ('.implode(', ', $products).') + // AND od.`id_order_detail` NOT IN ( + // SELECT `id_order_detail` + // FROM `'._DB_PREFIX_.'lapostews` la + // WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) + // )' + // ) as $order) { + // $orders_not_sended[] = $order['id_order']; + // }; - // $orders2 = array(); - // if(count($orders) > 0) { - // $_orders = array(); - // foreach(Db::getInstance()->ExecuteS(' - // SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - // FROM `'._DB_PREFIX_.'privatesale_category` c - // LEFT JOIN `'._DB_PREFIX_.'product` p - // ON c.`id_category` = p.`id_category_default` - // LEFT JOIN `'._DB_PREFIX_.'order_detail` d - // ON d.`product_id` = p.`id_product` - // WHERE d.`id_order` IN ('.implode(', ', $orders_not_sended).') - // ORDER BY d.`product_quantity` ASC - // ') as $row) { - // if(!isset($_orders[(int)$row['id_order']])) { - // $_orders[(int)$row['id_order']] = array(); - // } - // if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ - // $_orders[(int)$row['id_order']][$row['id_sale']] = 1; - // } - // } + // $orders2 = array(); + // if(count($orders) > 0) { + // $_orders = array(); + // foreach(Db::getInstance()->ExecuteS(' + // SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + // FROM `'._DB_PREFIX_.'privatesale_category` c + // LEFT JOIN `'._DB_PREFIX_.'product` p + // ON c.`id_category` = p.`id_category_default` + // LEFT JOIN `'._DB_PREFIX_.'order_detail` d + // ON d.`product_id` = p.`id_product` + // WHERE d.`id_order` IN ('.implode(', ', $orders_not_sended).') + // ORDER BY d.`product_quantity` ASC + // ') as $row) { + // if(!isset($_orders[(int)$row['id_order']])) { + // $_orders[(int)$row['id_order']] = array(); + // } + // if( !isset($_orders[(int)$row['id_order']][$row['id_sale']]) ){ + // $_orders[(int)$row['id_order']][$row['id_sale']] = 1; + // } + // } - // $order_print = array(); - // foreach ($_orders as $key => $order) { - // // M2 - // if($nb_sale == 2) { - // if( count($order) == $nb_sale) { - // $order_print[] = $key; - // } - // } - // } - // } - // } + // $order_print = array(); + // foreach ($_orders as $key => $order) { + // // M2 + // if($nb_sale == 2) { + // if( count($order) == $nb_sale) { + // $order_print[] = $key; + // } + // } + // } + // } + // } - // if(count($order_print) > 0) { - // $printed = array(); - // foreach($order_print as $order) { - // $printed[] = '('.(int) $order.')'; - // } + // if(count($order_print) > 0) { + // $printed = array(); + // foreach($order_print as $order) { + // $printed[] = '('.(int) $order.')'; + // } - // Db::getInstance()->ExecuteS(' - // INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - // VALUES '.implode(', ', $printed).' - // '); - // } + // Db::getInstance()->ExecuteS(' + // INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + // VALUES '.implode(', ', $printed).' + // '); + // } - // return $order_print; - // } + // return $order_print; + // } - /** + /** * @param $id_status * @param array $id_sales * @param date $date_from, $date_to * @param boolean $multi_only (if true, check only sales past in params) * @return array order_print */ - public static function getOrdersM3($id_status=NULL, $id_sales = array(), $date_from, $date_to, $multi_only = false) { - if($id_sales < 3) { - return false; - } + public static function getOrdersM3($id_status=NULL, $id_sales = array(), $date_from, $date_to, $multi_only = false) { + if($id_sales < 3) { + return false; + } - $orders = array(); + $orders = array(); - foreach ($id_sales as $key => $id_sale) { - $sale = new Sale($id_sale); - $products = $sale->getProducts(); + foreach ($id_sales as $key => $id_sale) { + $sale = new Sale($id_sale); + $products = $sale->getProducts(); - if(count($products) > 0) { - if($id_status === NULL) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT d.`id_order` - FROM `'._DB_PREFIX_.'order_detail` d - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` - WHERE `product_id` IN ('.implode(', ', $products).') - AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" - AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } else { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = h1.`id_order` - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ('.implode(',', $id_status).') - AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" - AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $products).') - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } - } - } + if(count($products) > 0) { + if($id_status === NULL) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT d.`id_order` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` + WHERE `product_id` IN ('.implode(', ', $products).') + AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" + AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } else { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = h1.`id_order` + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ('.implode(',', $id_status).') + AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" + AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $products).') + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } + } + } - $orders = array_unique($orders); + $orders = array_unique($orders); - if(count($orders) > 0) { - $_orders = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $orders).') - ORDER BY d.`product_quantity` ASC - ') as $row) { - if(!isset($_orders[(int)$row['id_order']])) { - $_orders[(int)$row['id_order']] = array(); - } - if(!isset($_orders[(int)$row['id_order']][$row['id_sale']]) ) { - $_orders[(int)$row['id_order']][$row['id_sale']] = 1; - } - } + if(count($orders) > 0) { + $_orders = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $orders).') + ORDER BY d.`product_quantity` ASC + ') as $row) { + if(!isset($_orders[(int)$row['id_order']])) { + $_orders[(int)$row['id_order']] = array(); + } + if(!isset($_orders[(int)$row['id_order']][$row['id_sale']]) ) { + $_orders[(int)$row['id_order']][$row['id_sale']] = 1; + } + } - $order_print = array(); + $order_print = array(); - foreach ($_orders as $key => $order) { + foreach ($_orders as $key => $order) { - // supprime les orders avec plus de 2 ventes pour les multi only - if($multi_only) { - if (count($order) > 2) { - continue; - } - } + // supprime les orders avec plus de 2 ventes pour les multi only + if($multi_only) { + if (count($order) > 2) { + continue; + } + } - // exclu les ventes avec des id_sales non selectionnes - $sales_order = array_keys($order); - foreach ($sales_order as $sale) { - if (!in_array($sale, $id_sales)) { - continue 2; - } - } + // exclu les ventes avec des id_sales non selectionnes + $sales_order = array_keys($order); + foreach ($sales_order as $sale) { + if (!in_array($sale, $id_sales)) { + continue 2; + } + } - $diff = array_intersect($sales_order, $id_sales); - if ($multi_only) { - // si multionly, test différence exacte - if (count($diff) == 2) { - $order_print[] = $key; - } - } else { - if (count($diff) >= 3) { - $order_print[] = $key; - } - } - } - } + $diff = array_intersect($sales_order, $id_sales); + if ($multi_only) { + // si multionly, test différence exacte + if (count($diff) == 2) { + $order_print[] = $key; + } + } else { + if (count($diff) >= 3) { + $order_print[] = $key; + } + } + } + } - if(count($order_print) > 0) { - $printed = array(); - foreach($order_print as $order) { - $printed[] = '('.(int) $order.')'; - } + if(count($order_print) > 0) { + $printed = array(); + foreach($order_print as $order) { + $printed[] = '('.(int) $order.')'; + } - Db::getInstance()->ExecuteS(' - INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - VALUES '.implode(', ', $printed).' - '); - } + Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + VALUES '.implode(', ', $printed).' + '); + } - if ($multi_only) { - return $order_print; - } else { - // tri par date pour les M3+ - sort($order_print); - return $order_print; - } + if ($multi_only) { + return $order_print; + } else { + // tri par date pour les M3+ + sort($order_print); + return $order_print; + } - return $order_print; - } + return $order_print; + } - /** + /** * @param $id_status * @param array $id_sales * @param date $date_from, $date_to * @param boolean $multi_only (if true, check only sales past in params) * @return array order_print */ - public static function getOrdersM2($id_status=NULL, $id_sales = array(), $date_from, $date_to, $multi_only = false, $carrier = 'lp') { - if($id_sales < 2) { - return false; - } + public static function getOrdersM2($id_status=NULL, $id_sales = array(), $date_from, $date_to, $multi_only = false, $carrier = 'lp') { + if($id_sales < 2) { + return false; + } - if($carrier === "lp") { + if($carrier === "lp") { $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_SOCOL'))); $carriers[] = 22; // dropshipping - } else { - $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_MR'))); - } + } else { + $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_MR'))); + } - $orders = array(); + $orders = array(); - foreach ($id_sales as $key => $id_sale) { - $sale = new Sale($id_sale); - $products = $sale->getProducts(); + foreach ($id_sales as $key => $id_sale) { + $sale = new Sale($id_sale); + $products = $sale->getProducts(); - if(count($products) > 0) { - if($id_status === NULL) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT d.`id_order` - FROM `'._DB_PREFIX_.'order_detail` d - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` - WHERE `product_id` IN ('.implode(', ', $products).') - AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" - AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" - AND o.`id_carrier` IN ('.implode(',', $carriers).') - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } else { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = h1.`id_order` - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ('.implode(',', $id_status).') - AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" - AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" - AND o.`id_carrier` IN ('.implode(',', $carriers).') - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `product_id` IN ('.implode(', ', $products).') - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } - } - } + if(count($products) > 0) { + if($id_status === NULL) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT d.`id_order` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` + WHERE `product_id` IN ('.implode(', ', $products).') + AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" + AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" + AND o.`id_carrier` IN ('.implode(',', $carriers).') + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } else { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = h1.`id_order` + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ('.implode(',', $id_status).') + AND o.`invoice_date` >= "'.pSQL($date_from).' 00:00:00" + AND o.`invoice_date` <= "'.pSQL($date_to).' 23:59:59" + AND o.`id_carrier` IN ('.implode(',', $carriers).') + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `product_id` IN ('.implode(', ', $products).') + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } + } + } - $orders = array_unique($orders); + $orders = array_unique($orders); - if(count($orders) > 0) { - $_orders = array(); - $_nb_product = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $orders).') - ORDER BY d.`product_quantity` ASC - ') as $row) { - if(!isset($_orders[(int)$row['id_order']])) { - $_orders[(int)$row['id_order']] = array(); - $_nb_product[(int)$row['id_order']] = 1; - } else { - $_nb_product[(int)$row['id_order']]++; - } - if(!isset($_orders[(int)$row['id_order']][$row['id_sale']]) ) { - $_orders[(int)$row['id_order']][$row['id_sale']] = 1; - } - } + if(count($orders) > 0) { + $_orders = array(); + $_nb_product = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $orders).') + ORDER BY d.`product_quantity` ASC + ') as $row) { + if(!isset($_orders[(int)$row['id_order']])) { + $_orders[(int)$row['id_order']] = array(); + $_nb_product[(int)$row['id_order']] = 1; + } else { + $_nb_product[(int)$row['id_order']]++; + } + if(!isset($_orders[(int)$row['id_order']][$row['id_sale']]) ) { + $_orders[(int)$row['id_order']][$row['id_sale']] = 1; + } + } - $order_print = array(); - $order_sales = array(); + $order_print = array(); + $order_sales = array(); - foreach ($_orders as $key => $order) { + foreach ($_orders as $key => $order) { - // supprime les orders avec plus de 2 ventes pour les multi only - if($multi_only) { - if (count($order) > 2) { - continue; - } - } + // supprime les orders avec plus de 2 ventes pour les multi only + if($multi_only) { + if (count($order) > 2) { + continue; + } + } - // exclu les ventes avec des id_sales non selectionnes - $sales_order = array_keys($order); - foreach ($sales_order as $sale) { - if (!in_array($sale, $id_sales)) { - continue 2; - } - } + // exclu les ventes avec des id_sales non selectionnes + $sales_order = array_keys($order); + foreach ($sales_order as $sale) { + if (!in_array($sale, $id_sales)) { + continue 2; + } + } - $diff = array_intersect($sales_order, $id_sales); - if (count($diff) > 1) { - $order_sales[(int)$key] = count($diff); - } + $diff = array_intersect($sales_order, $id_sales); + if (count($diff) > 1) { + $order_sales[(int)$key] = count($diff); + } - if ($multi_only) { - // si multionly, test différence exacte - if (count($diff) == 2) { - $order_print[] = $key; - } - } else { - if (count($diff) >= 2) { - $order_print[] = $key; - } - } - } - } + if ($multi_only) { + // si multionly, test différence exacte + if (count($diff) == 2) { + $order_print[] = $key; + } + } else { + if (count($diff) >= 2) { + $order_print[] = $key; + } + } + } + } - if(count($order_print) > 0) { - $printed = array(); - foreach($order_print as $order) { - $printed[] = '('.(int) $order.')'; - } + if(count($order_print) > 0) { + $printed = array(); + foreach($order_print as $order) { + $printed[] = '('.(int) $order.')'; + } - Db::getInstance()->ExecuteS(' - INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - VALUES '.implode(', ', $printed).' - '); - } + Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + VALUES '.implode(', ', $printed).' + '); + } - if ($multi_only) { - return $order_print; - } else { - asort($_nb_product); - $result_to_print = array(); - foreach ($_nb_product as $key => $value) { - if (in_array($key,$order_print)) { - $result_to_print[]=$key; - } else { - continue; - } - } - return $result_to_print; - } + if ($multi_only) { + return $order_print; + } else { + asort($_nb_product); + $result_to_print = array(); + foreach ($_nb_product as $key => $value) { + if (in_array($key,$order_print)) { + $result_to_print[]=$key; + } else { + continue; + } + } + return $result_to_print; + } - return $order_print; - } + return $order_print; + } - public function getOrdersFromSale($id_status=NULL, $single_only=TRUE, $send_test = FALSE, $carrier = 'lp') { - $orders = array(); - $products = $this->getProducts(); + public function getOrdersFromSale($id_status=NULL, $single_only=TRUE, $send_test = FALSE, $carrier = 'lp') { + $orders = array(); + $products = $this->getProducts(); - if($carrier === "lp") { + if($carrier === "lp") { $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_SOCOL'))); $carriers[] = 22; // dropshipping - } else { - $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_MR'))); - } - - if(count($products) > 0) { - if($id_status === NULL) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT od.`id_order` - FROM `'._DB_PREFIX_.'order_detail` od - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` - WHERE od.`product_id` IN ('.implode(', ', $products).') - AND o.`id_carrier` IN ('.implode(',', $carriers).') - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } else { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ( '.implode(',', $id_status) .') - AND h1.`id_order` IN ( - SELECT DISTINCT od.`id_order` - FROM `'._DB_PREFIX_.'order_detail` od - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` - WHERE od.`product_id` IN ('.implode(', ', $products).') - AND o.`id_carrier` IN ('.implode(',', $carriers).') - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } - - // TEST pour les commandes en 48h - if ($send_test) { - $orders_not_sended = array(); - - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT(o.`id_order`) AS `id_order` - FROM `'._DB_PREFIX_.'order_detail` od - LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` - WHERE o.`id_order` IN ('.implode(', ', $orders).') - AND od.`product_id` IN ('.implode(', ', $products).') - AND od.`id_order_detail` NOT IN ( - SELECT `id_order_detail` - FROM `'._DB_PREFIX_.'lapostews` la - WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) - ) - AND od.`id_order_detail` NOT IN ( - SELECT `id_order_detail` - FROM `'._DB_PREFIX_.'mondialrelay_parcel` mr - WHERE mr.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) - )' - ) as $order) { - $orders_not_sended[] = $order['id_order']; - }; - - // tri orders par nb product - $order_ordered = array(); - foreach ($orders_not_sended as $key => $order) { - $total_ref = Db::getInstance()->getValue(' - SELECT COUNT(`id_order_detail`) - FROM `'._DB_PREFIX_.'order_detail` - WHERE `id_order` = '.(int) $order - ); - $order_ordered[(int) $order] = $total_ref; - } - asort($order_ordered); - return array_keys($order_ordered); - // return $orders_not_sended; - } - - if(!$single_only) { - return $orders; - } - - $orders2 = array(); - - if(count($orders) > 0) { - $_orders = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_order` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `id_order` IN ('.implode(', ', $orders).') - AND `product_quantity` - `product_quantity_return` != 0 - ORDER BY `product_name` ASC - ') as $order) { - if(!isset($_orders[$order['id_order']])) { - $_orders[$order['id_order']] = 1; - } else { - $_orders[$order['id_order']]++; - } - } - - $__orders = array(); - foreach($_orders as $id_order => $count) { - if(!isset($__orders[$count])) { - $__orders[$count] = array(); - } - $__orders[$count][] = $id_order; - } - - $keys = array_keys($__orders); - asort($keys); - foreach($keys as $key) { - // tri les commandes avec 1 produit - if ($key == 1) { - $tmp_order = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_order` - FROM `ps_order_detail` - WHERE `id_order` IN ('.implode(',', $__orders[$key]).') - ORDER BY `product_reference` - ') as $value) { - $tmp_order[] = $value['id_order']; - } - $__orders[$key] = $tmp_order; - } - - foreach($__orders[$key] as $order) { - $orders2[$order] = TRUE; - } - } - - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_order`, `product_id` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `id_order` IN ('.implode(', ', $orders).') - ') as $order) { - $orders2[(int) $order['id_order']] = $orders2[(int) $order['id_order']] && in_array($order['product_id'], $products); - } - } - - $orders = array(); - - foreach($orders2 as $k => $v) { - if($v) { - $orders[] = $k; - } - } - } - - if(count($orders) > 0) { - $printed = array(); - foreach($orders as $order) { - $printed[] = '('.(int) $order.')'; - } - - Db::getInstance()->ExecuteS(' - INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - VALUES '.implode(', ', $printed).' - '); - } - - return $orders; - } - - public static function _recurse_sort($product_sales, $product_orders, $sales, $index, &$result) { - set_time_limit(180); - $sale_orders = array(); - - foreach($product_sales[$sales[$index]] as $product) { - foreach($product_orders[$product] as $order) { - if(!in_array($order, $sale_orders)) { - $sale_orders[] = $order; - } - } - } - - foreach($sale_orders as $order) { - if(!isset($result[$order])) { - $result[$order] = array(); - } - if(!in_array(sprintf('%05d', $index + 1), $result[$order])) { - $result[$order][] = sprintf('%05d', $index + 1); - } - } - - for($i=$index+1, $l=count($sales); $i < $l; $i++) { - self::_recurse_sort($product_sales, $product_orders, $sales, $i, $result); - } - - return $result; - } - - public static function getSalesByDate($id_status = array(), $date_start=NULL, $date_end=NULL) { - $sales_ids = array(); - $result = array(); - - if($date_start != NULL) { - $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; - } - if($date_end != NULL) { - $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; - } - - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ('. implode(',', $id_status).') - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'orders` - '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - - $total_orders = count($orders); - $_orders = array_chunk($orders, 150); - - foreach ($_orders as $key => $order_ids) { - foreach (Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity`, ps.`date_start` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps - ON ps.`id_sale` = c.`id_sale` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $order_ids).') - ORDER BY ps.`date_start` ASC - ') as $row) { - if (!in_array($row['id_sale'], $sales_ids)) { - $sales_ids[] = $row['id_sale']; - } - } - } - - // tri par date de début - $sales_ids_bis = array(); - foreach (Db::getInstance()->ExecuteS(' - SELECT `id_sale` - FROM `'._DB_PREFIX_.'privatesale` - WHERE `id_sale` IN ('.implode(', ', $sales_ids).') - ORDER BY `date_start` ASC') as $row ) { - if (!in_array($row['id_sale'], $sales_ids_bis)) { - $sales_ids_bis[] = $row['id_sale']; - } - } - - foreach ($sales_ids_bis as $key => $sale_id) { - $data = self::_getInfoSale((int)$sale_id); - $sale = new Sale($sale_id); - $total_products = $sale->getProducts(); - $total_products_wh_ean = self::_getNbNoEANProduct($total_products); - $data[0]['percent_ean'] = number_format(count($total_products_wh_ean) * 100 / count($total_products), 2); - $result[] = $data[0]; - } - - - return $result; - } - - public static function getSalesByDateBis($id_status = array(), $date_start=NULL, $date_end=NULL) { - $sales_ids = array(); - $result = array(); - - if($date_start != NULL) { - $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; - } - if($date_end != NULL) { - $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; - } - - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` IN ('. implode(',', $id_status).') - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'orders` - '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - - $total_orders = count($orders); - $_orders = array_chunk($orders, 150); - - foreach ($_orders as $key => $order_ids) { - foreach (Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity`, ps.`date_start` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps - ON ps.`id_sale` = c.`id_sale` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $order_ids).') - ORDER BY ps.`date_start` ASC - ') as $row) { - if (!in_array($row['id_sale'], $sales_ids)) { - $sales_ids[] = $row['id_sale']; - } - } - } - - // tri par date de début - $sales_ids_bis = array(); - foreach (Db::getInstance()->ExecuteS(' - SELECT `id_sale` - FROM `'._DB_PREFIX_.'privatesale` - WHERE `id_sale` IN ('.implode(', ', $sales_ids).') - ORDER BY `date_start` ASC') as $row ) { - if (!in_array($row['id_sale'], $sales_ids_bis)) { - $sales_ids_bis[] = $row['id_sale']; - } - } - - foreach ($sales_ids_bis as $key => $sale_id) { - $data = self::_getInfoSaleMore((int)$sale_id); - $result[] = $data[0]; - } - - return $result; - } - - private static function _getNbNoEANProduct($products) { - return Db::getInstance()->executeS(' - SELECT `id_product` - FROM `'._DB_PREFIX_.'product` - WHERE `id_product` IN ('.implode(',', $products).') - AND `ean13` != "" - '); - } - - - private static function _getInfoSale($id_sale) { - return Db::getInstance()->executeS(' - SELECT - p.`id_sale`, - p.`date_start`, - l.`name` as title, - p.`delivery_delay` as delivery_delay - FROM `ps_privatesale` p - LEFT JOIN `ps_privatesale_category` c ON c.`id_sale` = p.`id_sale` - LEFT JOIN `ps_category_lang` l ON l.`id_category` = p.`id_category` - WHERE p.`id_sale` = '.(int) $id_sale.' - AND l.`id_lang` = '. (int) Context::getContext()->language->id.' - LIMIT 1'); - } - - private static function _getInfoSaleMore($id_sale) { - return Db::getInstance()->executeS(' - SELECT - p.`id_sale`, - p.`date_start`, - l.`name` as title, - p.`delivery_delay` as delivery_delay, - dl.`name` as delivery_delay_name - FROM `'._DB_PREFIX_.'privatesale` p - LEFT JOIN `'._DB_PREFIX_.'privatesale_category` c ON c.`id_sale` = p.`id_sale` - LEFT JOIN `'._DB_PREFIX_.'category_lang` l ON l.`id_category` = p.`id_category` - LEFT JOIN `'._DB_PREFIX_.'privatesale_delay_lang` dl ON dl.`id_delay` = p.`delivery_delay` - WHERE p.`id_sale` = '.(int) $id_sale.' - AND l.`id_lang` = '. (int) Context::getContext()->language->id.' - AND dl.`id_lang` = '. (int) Context::getContext()->language->id.' - LIMIT 1'); - } - - public static function getOrdersByDate($crossed_only=TRUE, $date_start=NULL, $date_end=NULL, $id_status=NULL) { - set_time_limit(180); - global $cookie; - $product_sales = array(); - - $orders = array(); - $where = array(); - - if($date_start != NULL) { - $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; - } - if($date_end != NULL) { - $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; - } - - if($id_status === NULL) { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'orders` - '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } else { - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT h1.`id_order` AS `id_order` - FROM `'._DB_PREFIX_.'order_history` h1 - LEFT JOIN `'._DB_PREFIX_.'order_history` h2 - ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) - WHERE h2.`id_order_history` IS NULL - AND h1.`id_order_state` = '.(int) $id_status.' - AND h1.`id_order` IN ( - SELECT DISTINCT `id_order` - FROM `'._DB_PREFIX_.'orders` - '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' - ) - ') as $order) { - $orders[] = (int) $order['id_order']; - } - } - - if($crossed_only) { - $_orders = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT DISTINCT `id_order`, COUNT(*) AS `total` - FROM `'._DB_PREFIX_.'order_detail` - WHERE `id_order` IN ('.implode(', ', $orders).') - GROUP BY `id_order` - ') as $row) { - if($row['total'] > 1) { - $_orders[] = (int) $row['id_order']; - } - } - - $orders = $_orders; - } - - if(count($orders) > 0) { - $product_orders = array(); - - foreach(Db::getInstance()->ExecuteS(' - SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` - FROM `'._DB_PREFIX_.'privatesale_category` c - LEFT JOIN `'._DB_PREFIX_.'product` p - ON c.`id_category` = p.`id_category_default` - LEFT JOIN `'._DB_PREFIX_.'order_detail` d - ON d.`product_id` = p.`id_product` - WHERE d.`id_order` IN ('.implode(', ', $orders).') - ORDER BY d.`product_quantity` ASC - ') as $row) { - if(!isset($product_sales[(int) $row['id_sale']])) { - $product_sales[(int) $row['id_sale']] = array(); - } - if(!in_array((int) $row['id_product'], $product_sales[(int) $row['id_sale']])) { - $product_sales[(int) $row['id_sale']][] = (int) $row['id_product']; - } - - if(!isset($product_orders[(int) $row['id_product']])) { - $product_orders[(int) $row['id_product']] = array(); - } - $product_orders[(int) $row['id_product']][] = (int) $row['id_order']; - } - - // order sales by end date - $sales = array(); - - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_sale` - FROM `'._DB_PREFIX_.'privatesale` p - LEFT JOIN `'._DB_PREFIX_.'category_lang` c - ON p.`id_category` = c.`id_category` - WHERE p.`id_sale` IN ('.implode(', ', array_keys($product_sales)).') - AND c.`id_lang` = '.(int) $cookie->id_lang.' - ORDER BY p.`date_end` ASC, c.`name` ASC - ') as $sale) { - $sales[] = (int) $sale['id_sale']; - } - - $sorted_orders = array(); - $orders = array(); - - // NEW // - $sale_orders = array(); - $order_sales = array(); - foreach($sales as $id_sale) { - foreach($product_sales[$id_sale] as $id_product) { - foreach($product_orders[$id_product] as $id_order) { - if(!isset($sale_orders[$id_sale])) { - $sale_orders[$id_sale] = array(); - } - if(!isset($order_sales[$id_order])) { - $order_sales[$id_order] = array(); - } - - if(!in_array($id_order, $sale_orders[$id_sale])) { - $sale_orders[$id_sale][] = $id_order; - } - if(!in_array($id_sale, $order_sales[$id_order])) { - $order_sales[$id_order][] = $id_sale; - } - } - } - } - - $orders = array(); - - for($i=0, $l=count($sale_orders); $i < $l; $i++) { - //echo $i."\n"; - //echo $sales[$i]."\n"; - foreach($sale_orders[$sales[$i]] as $id_order) { - //echo "\t".$id_order."\n"; - if(!$crossed_only || $crossed_only && count($order_sales[$id_order]) > 1) { - for($j=$i+1; $j < $l; $j++) { - if(count($order_sales[$id_order]) == 2) { - if(in_array($sales[$j], $order_sales[$id_order]) && !in_array($id_order, $orders)) { - $orders[] = $id_order; - } - } else { - for($k=$j+1; $k < $l; $k++) { - if(in_array($sales[$k], $order_sales[$id_order]) && !in_array($id_order, $orders)) { - $orders[] = $id_order; - } - } - } - } - } - } - } - - //var_dump($orders);exit; - - // - - //foreach(self::_recurse_sort($product_sales, $product_orders, $sales, 0, $sorted_orders) as $order => $ids) { - // if(!$crossed_only || ($crossed_only && count($ids) > 1)) { - // $orders[$order] = implode('-', $ids); - // } - //} - - asort($orders); - } - - /*$orders = array_keys($orders);*/ - - if(count($orders) > 0) { - $printed = array(); - foreach($orders as $order) { - $printed[] = '('.(int) $order.')'; - } - - Db::getInstance()->ExecuteS(' - INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` - VALUES '.implode(', ', $printed).' - '); - } - - return $orders; - } - - public static function getSales($enabled=NULL, $logout=NULL, $featured=NULL, $future=NULL, $lite=FALSE, $pub=FALSE, $order_by='`date_end` ASC', $limit=NULL, $sale_type=NULL, $site_version=FALSE, $without_consumable=FALSE) { - global $cookie; - $result = array(); - $where = array(); - - $query = ' - SELECT p.`id_sale` - FROM `'._DB_PREFIX_.'privatesale` p - '; - - // if($sale_type !== NULL){ - // $query.= ' LEFT JOIN `'._DB_PREFIX_.'privatesale_type` pt ON(pt.id_sale = p.id_sale AND pt.id_sale_type = '. $sale_type.')'; - // } - - if($site_version !== FALSE) { - $query .= ' - LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` sv - ON p.`id_sale` = sv.`id_sale` - '; - $where[] = 'sv.`version` = "'.pSQL($site_version).'"'; - } - - if($enabled !== NULL OR $logout !== NULL OR $featured !== NULL OR $future !== NULL) { - $enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE; - $logout !== null? $where[] = '`logout` = '.(int) $logout: TRUE; - $pub != FALSE? '': $where[] = '`public` = 0'; - $featured !== null? $where[] = '`featured` = '.(int) $featured: TRUE; - // $sale_type !== NULL? $where[] = '`sale_type` = '.(int) $sale_type: TRUE; - - if($future === 'current') { - $where[] = '`date_start` < NOW() AND `date_end` > NOW()'; - } elseif($future === 'not_ended') { - $where[] = '`date_end` > NOW()';} - elseif($future === 'all') { - $where[] = ' 1 = 1'; - } elseif ($future === 'today') { - $where[] = 'CAST(`date_start` AS DATE) = CAST(NOW() AS DATE)'; - } elseif ($future === 'tomorrow') { - $where[] = 'CAST(`date_start` AS DATE) = CAST(DATE_ADD(NOW(), INTERVAL 1 DAY) AS DATE)'; - } elseif ($future === 'last') { - $where[] = 'CAST(`date_end` AS DATE) = CAST(NOW() AS DATE)'; - } elseif ($future === 'ended_tomorrow') { - $where[] = 'CAST(`date_end` AS DATE) = CAST(DATE_ADD(NOW(), INTERVAL 1 DAY) AS DATE)'; - } elseif(is_int($future)) { - $where[] = '(`date_start` > NOW() AND `date_start` < DATE_ADD(NOW(), INTERVAL '.$future.' DAY))'; - } elseif($future === TRUE) { - $where[] = '`date_start` > NOW()'; - } elseif($future === FALSE) { - $where[] = '`date_start` < NOW() AND `date_end` < NOW()'; - } - $query .= 'WHERE '.implode(' AND ', $where); - } - - // if($sale_type !== NULL){ - // $query.= ' AND pt.id_sale_type = '. $sale_type; - // } - - if ($without_consumable && _SHOP_CATEGORYENABLED!== FALSE) { - $query .= ' AND p.`id_sale` != '.(int) _SHOP_PRIVATESALES_CONSUMABLE; - } - - $query .= ' ORDER BY '.$order_by; - - if($limit !== NULL) { - $query .= ' LIMIT '.$limit; - } - - if($sales = Db::getInstance()->ExecuteS($query)) { - if($lite) { - foreach($sales AS $sale) { - $result[] = $sale['id_sale']; - } - } else { - foreach($sales AS $sale) { - $result[] = new Sale($sale['id_sale']); - } - } - } - return $result; - } - - - public static function getOthersSales($enabled=NULL, $logout=NULL, $featured=NULL, $future=NULL, $lite=FALSE, $pub=FALSE, $order_by='`date_end` ASC', $site_version) { - global $cookie; - - $id_sales = array(); - $result = array(); - $query = ' - SELECT p.`id_sale` - FROM `'._DB_PREFIX_.'privatesale` p - LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` cv ON (cv.`id_sale` = p.`id_sale`) - '; - $where = array(); - $enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE; - $pub != FALSE? '': $where[] = '`public` = 0'; - - if ($site_version) { - $where[] = ' cv.`version` = "'.pSQL($site_version).'"'; - } - - if($future === 'current') { - $where[] = '`date_start` < NOW() AND `date_end` > NOW()'; - } - $query .= 'WHERE '.implode(' AND ', $where); - - // if($sale_type !== NULL){ - // $query.= ' AND pt.id_sale_type = '. $sale_type; - // } - $query .= ' ORDER BY '.$order_by; - - if($sales = Db::getInstance()->ExecuteS($query)) { - foreach($sales AS $sale) { - $id_sales[] = $sale['id_sale']; - } - $result = Db::getInstance()->ExecuteS(' - SELECT s.id_sale, s.id_category, s.date_end, c.link_rewrite, c.name - FROM `'._DB_PREFIX_.'privatesale` s - LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON ( c.id_category = s.id_category ) - WHERE c.id_lang = '. (int)$cookie->id_lang .' - AND s.id_sale IN ('. implode(',', $id_sales).')'); - } - return $result; - } - - - public function getCarriers() { - $carriers = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_carrier` - FROM `'._DB_PREFIX_.'privatesale_carrier` - WHERE `id_sale` = '.$this->id - ) as $carrier) { - $carriers[] = $carrier['id_carrier']; - } - return $carriers; - } - - public static function flatRecurseCategory($id_category, $children=array()) { - $children[] = (int) $id_category; - $_children = Db::getInstance()->ExecuteS(' - SELECT `id_category` - FROM `'._DB_PREFIX_.'category` - WHERE `id_parent` = '.(int) $id_category - ); - foreach($_children as $child) { - $children = Sale::flatRecurseCategory($child['id_category'], $children); - } - return $children; - } - - public static function getCategoriesFromCache($id_sale) { - $categories = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_category` - FROM `'._DB_PREFIX_.'privatesale_category` - WHERE `id_sale` = '.$id_sale - ) as $category) { - $categories[] = $category['id_category']; - } - return $categories; - } - - public function buildCategoryCache() { - $categories = Sale::flatRecurseCategory($this->id_category); - //mail('marion@antadis.com', '[BBB] Creation category - buildCategoryCache', http_build_query($categories,'',', ')); // check for bad behaviour - Db::getInstance()->Execute(' - DELETE FROM `'._DB_PREFIX_.'privatesale_category` - WHERE `id_sale` = '.$this->id - ); - foreach($categories as $category) { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_category` VALUES ( - '.$this->id.', - '.$category.' - ) - '); - } - } - - public static function getSaleFromCategory($id=0, $use_cache=TRUE) { - if($use_cache) { - $id_sale = Db::getInstance()->ExecuteS(' - SELECT `id_sale` - FROM `'._DB_PREFIX_.'privatesale_category` - WHERE `id_category` = '.$id - ); - } else { - $current = $id; - $parent = $id; - $root = Configuration::get('PRIVATESALES_ROOT'); - while($parent > $root) { - $current = $parent; - $result = Db::getInstance()->ExecuteS(' - SELECT `id_parent` - FROM `'._DB_PREFIX_.'category` - WHERE `id_category` = '.$current - ); - $parent = $result[0]['id_parent']; - } - $id_sale = Db::getInstance()->ExecuteS(' - SELECT `id_sale` - FROM `'._DB_PREFIX_.'privatesale` - WHERE `id_category` = '.$current - ); - } - if(count($id_sale) > 0) { - return new Sale((int) $id_sale[0]['id_sale']); - } - } - - public function getProducts($order = FALSE) { - $categories = Sale::flatRecurseCategory($this->id_category); - - $products = array(); - foreach(Db::getInstance()->ExecuteS(' - SELECT `id_product` - FROM `'._DB_PREFIX_.'product` - WHERE `id_category_default` IN ('.implode(', ', $categories).') - '.(($order) ? ' ORDER BY `reference` ASC' : '') - ) as $product) { - $products[] = $product['id_product']; - } - - return $products; - } - - public function end($run_hook=FALSE) { - if ($this->id_category == 1) { - return FALSE; - } - - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'privatesale` - SET `enabled` = 0 - WHERE `id_sale` = '.(int) $this->id.' - LIMIT 1 - '); - - // $this->enabled = FALSE; - // $this->save(); - - $categories = Sale::flatRecurseCategory($this->id_category); - - $products = $this->getProducts(); - - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'product` - SET `active` = 0 - WHERE `id_product` IN ('.implode(', ', $products).') - '); - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'category` - SET `active` = 0 - WHERE `id_category` IN ('.implode(', ', $categories).') - '); - - if($run_hook) { - Module::hookExec('privatesales_end', array('sale' => $this)); - } - } - - public function subscribe($email=NULL) { - if($email !== NULL) { - $customer = new Customer(); - $customer->getByEmail($email); - - if(count(Db::getInstance()->ExecuteS(' - SELECT `email` - FROM `'._DB_PREFIX_.'privatesale_notify` - WHERE `email` = "'.pSQL($email).'" - AND `id_sale` = '.$this->id.' - ')) > 0) { - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'privatesale_notify` - SET `deleted` = 0 - WHERE `email` = "'.pSQL($email).'" - AND `id_sale` = '.$this->id.' - '); - } else { - Db::getInstance()->Execute(' - INSERT INTO `'._DB_PREFIX_.'privatesale_notify` - VALUES ( - '.$this->id.', - "'.pSQL($email).'", - '.($customer->id? $customer->id: 0).', - 0 - ) - '); - } - } - } - - public function isSubscribed($email=NULL) { - if($email !== NULL) { - if(count(Db::getInstance()->ExecuteS(' - SELECT `email` - FROM `'._DB_PREFIX_.'privatesale_notify` - WHERE `email` = "'.pSQL($email).'" - AND `id_sale` = '.$this->id.' - AND `deleted` = 0 - ')) > 0) { - return TRUE; - } - } - return FALSE; - } - - public function unsubscribe($email=NULL, $real_delete=FALSE) { - if($email !== NULL) { - if($real_delete === TRUE) { - Db::getInstance()->Execute(' - DELETE FROM `'._DB_PREFIX_.'privatesale_notify` - WHERE `email` = "'.pSQL($email).'" - AND `id_sale` = '.$this->id.' - '); - } else { - Db::getInstance()->Execute(' - UPDATE `'._DB_PREFIX_.'privatesale_notify` - SET `deleted` = 1 - WHERE `email` = "'.pSQL($email).'" - AND `id_sale` = '.$this->id.' - '); - } - } - } - - static public function getNbSales(){ - $sql = 'SELECT count(id_sale) - FROM `'._DB_PREFIX_.'privatesale` p'; - return Db::getInstance()->getValue($sql); - } - - public static function getByPosition($position = 0){ - $sql = 'SELECT id_sale - FROM `'._DB_PREFIX_.'privatesale` p - WHERE position = '. $position; - $id_sale = Db::getInstance()->getValue($sql); - - if(!empty($id_sale)){ - return $sale = new Sale($id_sale); - } - return false; - } - - public static function getShippingSale($id_sale){ - $shipping = Db::getInstance()->getValue(' - SELECT `id_shipping` + } else { + $carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_MR'))); + } + + if(count($products) > 0) { + if($id_status === NULL) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT od.`id_order` + FROM `'._DB_PREFIX_.'order_detail` od + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` + WHERE od.`product_id` IN ('.implode(', ', $products).') + AND o.`id_carrier` IN ('.implode(',', $carriers).') + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } else { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ( '.implode(',', $id_status) .') + AND h1.`id_order` IN ( + SELECT DISTINCT od.`id_order` + FROM `'._DB_PREFIX_.'order_detail` od + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` + WHERE od.`product_id` IN ('.implode(', ', $products).') + AND o.`id_carrier` IN ('.implode(',', $carriers).') + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } + + // TEST pour les commandes en 48h + if ($send_test) { + $orders_not_sended = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT(o.`id_order`) AS `id_order` + FROM `'._DB_PREFIX_.'order_detail` od + LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = od.`id_order` + WHERE o.`id_order` IN ('.implode(', ', $orders).') + AND od.`product_id` IN ('.implode(', ', $products).') + AND od.`id_order_detail` NOT IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'lapostews` la + WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) + ) + AND od.`id_order_detail` NOT IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'mondialrelay_parcel` mr + WHERE mr.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`) + )' + ) as $order) { + $orders_not_sended[] = $order['id_order']; + }; + + // tri orders par nb product + $order_ordered = array(); + foreach ($orders_not_sended as $key => $order) { + $total_ref = Db::getInstance()->getValue(' + SELECT COUNT(`id_order_detail`) + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order + ); + $order_ordered[(int) $order] = $total_ref; + } + asort($order_ordered); + return array_keys($order_ordered); + // return $orders_not_sended; + } + + if(!$single_only) { + return $orders; + } + + $orders2 = array(); + + if(count($orders) > 0) { + $_orders = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` IN ('.implode(', ', $orders).') + AND `product_quantity` - `product_quantity_return` != 0 + ORDER BY `product_name` ASC + ') as $order) { + if(!isset($_orders[$order['id_order']])) { + $_orders[$order['id_order']] = 1; + } else { + $_orders[$order['id_order']]++; + } + } + + $__orders = array(); + foreach($_orders as $id_order => $count) { + if(!isset($__orders[$count])) { + $__orders[$count] = array(); + } + $__orders[$count][] = $id_order; + } + + $keys = array_keys($__orders); + asort($keys); + foreach($keys as $key) { + // tri les commandes avec 1 produit + if ($key == 1) { + $tmp_order = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order` + FROM `ps_order_detail` + WHERE `id_order` IN ('.implode(',', $__orders[$key]).') + ORDER BY `product_reference` + ') as $value) { + $tmp_order[] = $value['id_order']; + } + $__orders[$key] = $tmp_order; + } + + foreach($__orders[$key] as $order) { + $orders2[$order] = TRUE; + } + } + + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order`, `product_id` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` IN ('.implode(', ', $orders).') + ') as $order) { + $orders2[(int) $order['id_order']] = $orders2[(int) $order['id_order']] && in_array($order['product_id'], $products); + } + } + + $orders = array(); + + foreach($orders2 as $k => $v) { + if($v) { + $orders[] = $k; + } + } + } + + if(count($orders) > 0) { + $printed = array(); + foreach($orders as $order) { + $printed[] = '('.(int) $order.')'; + } + + Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + VALUES '.implode(', ', $printed).' + '); + } + + return $orders; + } + + public static function _recurse_sort($product_sales, $product_orders, $sales, $index, &$result) { + set_time_limit(180); + $sale_orders = array(); + + foreach($product_sales[$sales[$index]] as $product) { + foreach($product_orders[$product] as $order) { + if(!in_array($order, $sale_orders)) { + $sale_orders[] = $order; + } + } + } + + foreach($sale_orders as $order) { + if(!isset($result[$order])) { + $result[$order] = array(); + } + if(!in_array(sprintf('%05d', $index + 1), $result[$order])) { + $result[$order][] = sprintf('%05d', $index + 1); + } + } + + for($i=$index+1, $l=count($sales); $i < $l; $i++) { + self::_recurse_sort($product_sales, $product_orders, $sales, $i, $result); + } + + return $result; + } + + public static function getSalesByDate($id_status = array(), $date_start=NULL, $date_end=NULL) { + $sales_ids = array(); + $result = array(); + + if($date_start != NULL) { + $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; + } + if($date_end != NULL) { + $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; + } + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ('. implode(',', $id_status).') + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'orders` + '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + + $total_orders = count($orders); + $_orders = array_chunk($orders, 150); + + foreach ($_orders as $key => $order_ids) { + foreach (Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity`, ps.`date_start` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'privatesale` ps + ON ps.`id_sale` = c.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $order_ids).') + ORDER BY ps.`date_start` ASC + ') as $row) { + if (!in_array($row['id_sale'], $sales_ids)) { + $sales_ids[] = $row['id_sale']; + } + } + } + + // tri par date de début + $sales_ids_bis = array(); + foreach (Db::getInstance()->ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `id_sale` IN ('.implode(', ', $sales_ids).') + ORDER BY `date_start` ASC') as $row ) { + if (!in_array($row['id_sale'], $sales_ids_bis)) { + $sales_ids_bis[] = $row['id_sale']; + } + } + + foreach ($sales_ids_bis as $key => $sale_id) { + $data = self::_getInfoSale((int)$sale_id); + $sale = new Sale($sale_id); + $total_products = $sale->getProducts(); + $total_products_wh_ean = self::_getNbNoEANProduct($total_products); + $data[0]['percent_ean'] = number_format(count($total_products_wh_ean) * 100 / count($total_products), 2); + $result[] = $data[0]; + } + + + return $result; + } + + public static function getSalesByDateBis($id_status = array(), $date_start=NULL, $date_end=NULL) { + $sales_ids = array(); + $result = array(); + + if($date_start != NULL) { + $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; + } + if($date_end != NULL) { + $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; + } + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` IN ('. implode(',', $id_status).') + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'orders` + '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + + $total_orders = count($orders); + $_orders = array_chunk($orders, 150); + + foreach ($_orders as $key => $order_ids) { + foreach (Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity`, ps.`date_start` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'privatesale` ps + ON ps.`id_sale` = c.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $order_ids).') + ORDER BY ps.`date_start` ASC + ') as $row) { + if (!in_array($row['id_sale'], $sales_ids)) { + $sales_ids[] = $row['id_sale']; + } + } + } + + // tri par date de début + $sales_ids_bis = array(); + foreach (Db::getInstance()->ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `id_sale` IN ('.implode(', ', $sales_ids).') + ORDER BY `date_start` ASC') as $row ) { + if (!in_array($row['id_sale'], $sales_ids_bis)) { + $sales_ids_bis[] = $row['id_sale']; + } + } + + foreach ($sales_ids_bis as $key => $sale_id) { + $data = self::_getInfoSaleMore((int)$sale_id); + $result[] = $data[0]; + } + + return $result; + } + + private static function _getNbNoEANProduct($products) { + return Db::getInstance()->executeS(' + SELECT `id_product` + FROM `'._DB_PREFIX_.'product` + WHERE `id_product` IN ('.implode(',', $products).') + AND `ean13` != "" + '); + } + + + private static function _getInfoSale($id_sale) { + return Db::getInstance()->executeS(' + SELECT + p.`id_sale`, + p.`date_start`, + l.`name` as title, + p.`delivery_delay` as delivery_delay + FROM `ps_privatesale` p + LEFT JOIN `ps_privatesale_category` c ON c.`id_sale` = p.`id_sale` + LEFT JOIN `ps_category_lang` l ON l.`id_category` = p.`id_category` + WHERE p.`id_sale` = '.(int) $id_sale.' + AND l.`id_lang` = '. (int) Context::getContext()->language->id.' + LIMIT 1'); + } + + private static function _getInfoSaleMore($id_sale) { + return Db::getInstance()->executeS(' + SELECT + p.`id_sale`, + p.`date_start`, + l.`name` as title, + p.`delivery_delay` as delivery_delay, + dl.`name` as delivery_delay_name + FROM `'._DB_PREFIX_.'privatesale` p + LEFT JOIN `'._DB_PREFIX_.'privatesale_category` c ON c.`id_sale` = p.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'category_lang` l ON l.`id_category` = p.`id_category` + LEFT JOIN `'._DB_PREFIX_.'privatesale_delay_lang` dl ON dl.`id_delay` = p.`delivery_delay` + WHERE p.`id_sale` = '.(int) $id_sale.' + AND l.`id_lang` = '. (int) Context::getContext()->language->id.' + AND dl.`id_lang` = '. (int) Context::getContext()->language->id.' + LIMIT 1'); + } + + public static function getOrdersByDate($crossed_only=TRUE, $date_start=NULL, $date_end=NULL, $id_status=NULL) { + set_time_limit(180); + global $cookie; + $product_sales = array(); + + $orders = array(); + $where = array(); + + if($date_start != NULL) { + $where[] = ' `invoice_date` >= "'.pSQL($date_start).' 00:00:00"'; + } + if($date_end != NULL) { + $where[] = ' `invoice_date` <= "'.pSQL($date_end).' 23:59:59"'; + } + + if($id_status === NULL) { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'orders` + '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } else { + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT h1.`id_order` AS `id_order` + FROM `'._DB_PREFIX_.'order_history` h1 + LEFT JOIN `'._DB_PREFIX_.'order_history` h2 + ON (h1.`id_order` = h2.`id_order` AND h1.`id_order_history` < h2.`id_order_history`) + WHERE h2.`id_order_history` IS NULL + AND h1.`id_order_state` = '.(int) $id_status.' + AND h1.`id_order` IN ( + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'orders` + '.(count($where) > 0? 'WHERE '.implode(' AND ', $where): '').' + ) + ') as $order) { + $orders[] = (int) $order['id_order']; + } + } + + if($crossed_only) { + $_orders = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_order`, COUNT(*) AS `total` + FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` IN ('.implode(', ', $orders).') + GROUP BY `id_order` + ') as $row) { + if($row['total'] > 1) { + $_orders[] = (int) $row['id_order']; + } + } + + $orders = $_orders; + } + + if(count($orders) > 0) { + $product_orders = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT d.`id_order`, p.`id_product`, c.`id_sale`, GREATEST(d.`product_quantity` - d.`product_quantity_return`, 0) AS `product_quantity` + FROM `'._DB_PREFIX_.'privatesale_category` c + LEFT JOIN `'._DB_PREFIX_.'product` p + ON c.`id_category` = p.`id_category_default` + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON d.`product_id` = p.`id_product` + WHERE d.`id_order` IN ('.implode(', ', $orders).') + ORDER BY d.`product_quantity` ASC + ') as $row) { + if(!isset($product_sales[(int) $row['id_sale']])) { + $product_sales[(int) $row['id_sale']] = array(); + } + if(!in_array((int) $row['id_product'], $product_sales[(int) $row['id_sale']])) { + $product_sales[(int) $row['id_sale']][] = (int) $row['id_product']; + } + + if(!isset($product_orders[(int) $row['id_product']])) { + $product_orders[(int) $row['id_product']] = array(); + } + $product_orders[(int) $row['id_product']][] = (int) $row['id_order']; + } + + // order sales by end date + $sales = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` p + LEFT JOIN `'._DB_PREFIX_.'category_lang` c + ON p.`id_category` = c.`id_category` + WHERE p.`id_sale` IN ('.implode(', ', array_keys($product_sales)).') + AND c.`id_lang` = '.(int) $cookie->id_lang.' + ORDER BY p.`date_end` ASC, c.`name` ASC + ') as $sale) { + $sales[] = (int) $sale['id_sale']; + } + + $sorted_orders = array(); + $orders = array(); + + // NEW // + $sale_orders = array(); + $order_sales = array(); + foreach($sales as $id_sale) { + foreach($product_sales[$id_sale] as $id_product) { + foreach($product_orders[$id_product] as $id_order) { + if(!isset($sale_orders[$id_sale])) { + $sale_orders[$id_sale] = array(); + } + if(!isset($order_sales[$id_order])) { + $order_sales[$id_order] = array(); + } + + if(!in_array($id_order, $sale_orders[$id_sale])) { + $sale_orders[$id_sale][] = $id_order; + } + if(!in_array($id_sale, $order_sales[$id_order])) { + $order_sales[$id_order][] = $id_sale; + } + } + } + } + + $orders = array(); + + for($i=0, $l=count($sale_orders); $i < $l; $i++) { + //echo $i."\n"; + //echo $sales[$i]."\n"; + foreach($sale_orders[$sales[$i]] as $id_order) { + //echo "\t".$id_order."\n"; + if(!$crossed_only || $crossed_only && count($order_sales[$id_order]) > 1) { + for($j=$i+1; $j < $l; $j++) { + if(count($order_sales[$id_order]) == 2) { + if(in_array($sales[$j], $order_sales[$id_order]) && !in_array($id_order, $orders)) { + $orders[] = $id_order; + } + } else { + for($k=$j+1; $k < $l; $k++) { + if(in_array($sales[$k], $order_sales[$id_order]) && !in_array($id_order, $orders)) { + $orders[] = $id_order; + } + } + } + } + } + } + } + + //var_dump($orders);exit; + + // + + //foreach(self::_recurse_sort($product_sales, $product_orders, $sales, 0, $sorted_orders) as $order => $ids) { + // if(!$crossed_only || ($crossed_only && count($ids) > 1)) { + // $orders[$order] = implode('-', $ids); + // } + //} + + asort($orders); + } + + /*$orders = array_keys($orders);*/ + + if(count($orders) > 0) { + $printed = array(); + foreach($orders as $order) { + $printed[] = '('.(int) $order.')'; + } + + Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` + VALUES '.implode(', ', $printed).' + '); + } + + return $orders; + } + + public static function getSales($enabled=NULL, $logout=NULL, $featured=NULL, $future=NULL, $lite=FALSE, $pub=FALSE, $order_by='`date_end` ASC', $limit=NULL, $sale_type=NULL, $site_version=FALSE, $without_consumable=FALSE) { + global $cookie; + $result = array(); + $where = array(); + + $query = ' + SELECT p.`id_sale` + FROM `'._DB_PREFIX_.'privatesale` p + '; + + // if($sale_type !== NULL){ + // $query.= ' LEFT JOIN `'._DB_PREFIX_.'privatesale_type` pt ON(pt.id_sale = p.id_sale AND pt.id_sale_type = '. $sale_type.')'; + // } + + if($site_version !== FALSE) { + $query .= ' + LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` sv + ON p.`id_sale` = sv.`id_sale` + '; + $where[] = 'sv.`version` = "'.pSQL($site_version).'"'; + } + + if($enabled !== NULL OR $logout !== NULL OR $featured !== NULL OR $future !== NULL) { + $enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE; + $logout !== null? $where[] = '`logout` = '.(int) $logout: TRUE; + $pub != FALSE? '': $where[] = '`public` = 0'; + $featured !== null? $where[] = '`featured` = '.(int) $featured: TRUE; + // $sale_type !== NULL? $where[] = '`sale_type` = '.(int) $sale_type: TRUE; + + if($future === 'current') { + $where[] = '`date_start` < NOW() AND `date_end` > NOW()'; + } elseif($future === 'not_ended') { + $where[] = '`date_end` > NOW()';} + elseif($future === 'all') { + $where[] = ' 1 = 1'; + } elseif ($future === 'today') { + $where[] = 'CAST(`date_start` AS DATE) = CAST(NOW() AS DATE)'; + } elseif ($future === 'tomorrow') { + $where[] = 'CAST(`date_start` AS DATE) = CAST(DATE_ADD(NOW(), INTERVAL 1 DAY) AS DATE)'; + } elseif ($future === 'last') { + $where[] = 'CAST(`date_end` AS DATE) = CAST(NOW() AS DATE)'; + } elseif ($future === 'ended_tomorrow') { + $where[] = 'CAST(`date_end` AS DATE) = CAST(DATE_ADD(NOW(), INTERVAL 1 DAY) AS DATE)'; + } elseif(is_int($future)) { + $where[] = '(`date_start` > NOW() AND `date_start` < DATE_ADD(NOW(), INTERVAL '.$future.' DAY))'; + } elseif($future === TRUE) { + $where[] = '`date_start` > NOW()'; + } elseif($future === FALSE) { + $where[] = '`date_start` < NOW() AND `date_end` < NOW()'; + } + $query .= 'WHERE '.implode(' AND ', $where); + } + + // if($sale_type !== NULL){ + // $query.= ' AND pt.id_sale_type = '. $sale_type; + // } + + if ($without_consumable && _SHOP_CATEGORYENABLED!== FALSE) { + $query .= ' AND p.`id_sale` != '.(int) _SHOP_PRIVATESALES_CONSUMABLE; + } + + $query .= ' ORDER BY '.$order_by; + + if($limit !== NULL) { + $query .= ' LIMIT '.$limit; + } + + if($sales = Db::getInstance()->ExecuteS($query)) { + if($lite) { + foreach($sales AS $sale) { + $result[] = $sale['id_sale']; + } + } else { + foreach($sales AS $sale) { + $result[] = new Sale($sale['id_sale']); + } + } + } + return $result; + } + + + public static function getOthersSales($enabled=NULL, $logout=NULL, $featured=NULL, $future=NULL, $lite=FALSE, $pub=FALSE, $order_by='`date_end` ASC', $site_version) { + global $cookie; + + $id_sales = array(); + $result = array(); + $query = ' + SELECT p.`id_sale` + FROM `'._DB_PREFIX_.'privatesale` p + LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` cv ON (cv.`id_sale` = p.`id_sale`) + '; + $where = array(); + $enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE; + $pub != FALSE? '': $where[] = '`public` = 0'; + + if ($site_version) { + $where[] = ' cv.`version` = "'.pSQL($site_version).'"'; + } + + if($future === 'current') { + $where[] = '`date_start` < NOW() AND `date_end` > NOW()'; + } + $query .= 'WHERE '.implode(' AND ', $where); + + // if($sale_type !== NULL){ + // $query.= ' AND pt.id_sale_type = '. $sale_type; + // } + $query .= ' ORDER BY '.$order_by; + + if($sales = Db::getInstance()->ExecuteS($query)) { + foreach($sales AS $sale) { + $id_sales[] = $sale['id_sale']; + } + $result = Db::getInstance()->ExecuteS(' + SELECT s.id_sale, s.id_category, s.date_end, c.link_rewrite, c.name + FROM `'._DB_PREFIX_.'privatesale` s + LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON ( c.id_category = s.id_category ) + WHERE c.id_lang = '. (int)$cookie->id_lang .' + AND s.id_sale IN ('. implode(',', $id_sales).')'); + } + return $result; + } + + + public function getCarriers() { + $carriers = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_carrier` + FROM `'._DB_PREFIX_.'privatesale_carrier` + WHERE `id_sale` = '.$this->id + ) as $carrier) { + $carriers[] = $carrier['id_carrier']; + } + return $carriers; + } + + public static function flatRecurseCategory($id_category, $children=array()) { + $children[] = (int) $id_category; + $_children = Db::getInstance()->ExecuteS(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = '.(int) $id_category + ); + foreach($_children as $child) { + $children = Sale::flatRecurseCategory($child['id_category'], $children); + } + return $children; + } + + public static function getCategoriesFromCache($id_sale) { + $categories = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'privatesale_category` + WHERE `id_sale` = '.$id_sale + ) as $category) { + $categories[] = $category['id_category']; + } + return $categories; + } + + public function buildCategoryCache() { + $categories = Sale::flatRecurseCategory($this->id_category); + //mail('marion@antadis.com', '[BBB] Creation category - buildCategoryCache', http_build_query($categories,'',', ')); // check for bad behaviour + Db::getInstance()->Execute(' + DELETE FROM `'._DB_PREFIX_.'privatesale_category` + WHERE `id_sale` = '.$this->id + ); + foreach($categories as $category) { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_category` VALUES ( + '.$this->id.', + '.$category.' + ) + '); + } + } + + public static function getSaleFromCategory($id=0, $use_cache=TRUE) { + if($use_cache) { + $id_sale = Db::getInstance()->ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale_category` + WHERE `id_category` = '.$id + ); + } else { + $current = $id; + $parent = $id; + $root = Configuration::get('PRIVATESALES_ROOT'); + while($parent > $root) { + $current = $parent; + $result = Db::getInstance()->ExecuteS(' + SELECT `id_parent` + FROM `'._DB_PREFIX_.'category` + WHERE `id_category` = '.$current + ); + $parent = $result[0]['id_parent']; + } + $id_sale = Db::getInstance()->ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `id_category` = '.$current + ); + } + if(count($id_sale) > 0) { + return new Sale((int) $id_sale[0]['id_sale']); + } + } + + public function getProducts($order = FALSE) { + $categories = Sale::flatRecurseCategory($this->id_category); + + $products = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_product` + FROM `'._DB_PREFIX_.'product` + WHERE `id_category_default` IN ('.implode(', ', $categories).') + '.(($order) ? ' ORDER BY `reference` ASC' : '') + ) as $product) { + $products[] = $product['id_product']; + } + + return $products; + } + + public function end($run_hook=FALSE) { + if ($this->id_category == 1) { + return FALSE; + } + + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'privatesale` + SET `enabled` = 0 + WHERE `id_sale` = '.(int) $this->id.' + LIMIT 1 + '); + + // $this->enabled = FALSE; + // $this->save(); + + $categories = Sale::flatRecurseCategory($this->id_category); + + $products = $this->getProducts(); + + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'product` + SET `active` = 0 + WHERE `id_product` IN ('.implode(', ', $products).') + '); + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'category` + SET `active` = 0 + WHERE `id_category` IN ('.implode(', ', $categories).') + '); + + if($run_hook) { + Module::hookExec('privatesales_end', array('sale' => $this)); + } + } + + public function subscribe($email=NULL) { + if($email !== NULL) { + $customer = new Customer(); + $customer->getByEmail($email); + + if(count(Db::getInstance()->ExecuteS(' + SELECT `email` + FROM `'._DB_PREFIX_.'privatesale_notify` + WHERE `email` = "'.pSQL($email).'" + AND `id_sale` = '.$this->id.' + ')) > 0) { + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'privatesale_notify` + SET `deleted` = 0 + WHERE `email` = "'.pSQL($email).'" + AND `id_sale` = '.$this->id.' + '); + } else { + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'privatesale_notify` + VALUES ( + '.$this->id.', + "'.pSQL($email).'", + '.($customer->id? $customer->id: 0).', + 0 + ) + '); + } + } + } + + public function isSubscribed($email=NULL) { + if($email !== NULL) { + if(count(Db::getInstance()->ExecuteS(' + SELECT `email` + FROM `'._DB_PREFIX_.'privatesale_notify` + WHERE `email` = "'.pSQL($email).'" + AND `id_sale` = '.$this->id.' + AND `deleted` = 0 + ')) > 0) { + return TRUE; + } + } + return FALSE; + } + + public function unsubscribe($email=NULL, $real_delete=FALSE) { + if($email !== NULL) { + if($real_delete === TRUE) { + Db::getInstance()->Execute(' + DELETE FROM `'._DB_PREFIX_.'privatesale_notify` + WHERE `email` = "'.pSQL($email).'" + AND `id_sale` = '.$this->id.' + '); + } else { + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'privatesale_notify` + SET `deleted` = 1 + WHERE `email` = "'.pSQL($email).'" + AND `id_sale` = '.$this->id.' + '); + } + } + } + + static public function getNbSales(){ + $sql = 'SELECT count(id_sale) + FROM `'._DB_PREFIX_.'privatesale` p'; + return Db::getInstance()->getValue($sql); + } + + public static function getByPosition($position = 0){ + $sql = 'SELECT id_sale + FROM `'._DB_PREFIX_.'privatesale` p + WHERE position = '. $position; + $id_sale = Db::getInstance()->getValue($sql); + + if(!empty($id_sale)){ + return $sale = new Sale($id_sale); + } + return false; + } + + public static function getShippingSale($id_sale){ + $shipping = Db::getInstance()->getValue(' + SELECT `id_shipping` FROM `'._DB_PREFIX_.'privatesale_shipping_sale` WHERE `id_sale` = '.(int) $id_sale ); - return (int)$shipping; - } + return (int)$shipping; + } - /** - * Update the percentage or amount in a promotion string - * for each sale with the maximum reduction percentage or amount - * of its products. - * - * @param string $date_day the sales starting day (to express in "Y-m-d") - */ - public static function updateSalesReductionAtDate() - { - $id_field = 2; // id=2 for the extra field sale "pourcentage" + /** + * Update the percentage or amount in a promotion string + * for each sale with the maximum reduction percentage or amount + * of its products. + * + * @param string $date_day the sales starting day (to express in "Y-m-d") + */ + public static function updateSalesReductionAtDate() + { + $id_field = 2; // id=2 for the extra field sale "pourcentage" - // for all sales at "date_day" and hour > "from_hour" - // return the maximal reduction applied from all products of each sale - $sql = "SELECT MAX(sp.reduction) as reduc, - s.id_sale, sf.value, sf.id_field, sf.id_lang - FROM `"._DB_PREFIX_."specific_price` sp - JOIN `"._DB_PREFIX_."category_product` cp - ON cp.id_product = sp.id_product - JOIN `"._DB_PREFIX_."privatesale_category` psc - ON psc.id_category = cp.id_category - JOIN `"._DB_PREFIX_."privatesale` s - ON s.id_sale = psc.id_sale - JOIN `"._DB_PREFIX_."privatesale_extrafield_sale` sf - ON sf.id_sale = s.id_sale - WHERE - sp.reduction_type='percentage' - AND CAST(s.date_start AS DATE) = CAST(NOW() AS DATE) - AND sf.id_field = ".$id_field." - AND sf.`value` IS NOT NULL - AND sf.`value` <> '' - GROUP BY s.id_sale, sf.value, sf.id_field, sf.id_lang - "; + // for all sales at "date_day" and hour > "from_hour" + // return the maximal reduction applied from all products of each sale + $sql = "SELECT MAX(sp.reduction) as reduc, + s.id_sale, sf.value, sf.id_field, sf.id_lang + FROM `"._DB_PREFIX_."specific_price` sp + JOIN `"._DB_PREFIX_."category_product` cp + ON cp.id_product = sp.id_product + JOIN `"._DB_PREFIX_."privatesale_category` psc + ON psc.id_category = cp.id_category + JOIN `"._DB_PREFIX_."privatesale` s + ON s.id_sale = psc.id_sale + JOIN `"._DB_PREFIX_."privatesale_extrafield_sale` sf + ON sf.id_sale = s.id_sale + WHERE + sp.reduction_type='percentage' + AND CAST(s.date_start AS DATE) = CAST(NOW() AS DATE) + AND sf.id_field = ".$id_field." + AND sf.`value` IS NOT NULL + AND sf.`value` <> '' + GROUP BY s.id_sale, sf.value, sf.id_field, sf.id_lang + "; - $results = Db::getInstance()->ExecuteS($sql); + $results = Db::getInstance()->ExecuteS($sql); - // update the sentence with the max reduction, if any, for - // each sale - foreach ($results as $result) { + // update the sentence with the max reduction, if any, for + // each sale + foreach ($results as $result) { - $reduction_in_percent = round($result['reduc']*100); - $new_value = preg_replace( - '/\d+\s*%/', - $reduction_in_percent."%", - $result['value'] - ); + $reduction_in_percent = round($result['reduc']*100); + $new_value = preg_replace( + '/\d+\s*%/', + $reduction_in_percent."%", + $result['value'] + ); - $sql = "UPDATE `"._DB_PREFIX_."privatesale_extrafield_sale` - SET value=\"".$new_value."\" - WHERE id_field = ".$id_field." - AND id_lang = '".$result['id_lang']."' - AND id_sale = '".$result['id_sale']."' - "; - Db::getInstance()->Execute($sql); - } - } + $sql = "UPDATE `"._DB_PREFIX_."privatesale_extrafield_sale` + SET value=\"".$new_value."\" + WHERE id_field = ".$id_field." + AND id_lang = '".$result['id_lang']."' + AND id_sale = '".$result['id_sale']."' + "; + Db::getInstance()->Execute($sql); + } + } + + /** + * Test the combination of two sale in the cart + * Only classic and particular delivery sales can be added together + * Or product of the same sale + * Or philea sales with same delivery delay + * Or Noel sales with the same shipping + * + * @param array(id_sale, delivery_delay, shipping) $current_sale [the first sale found in the cart] + * @param array(id_sale, delivery_delay, shipping) $adding_sale [the sale of the new added product] + * @return boolean + */ + public static function isCombinable($current_sale, $adding_sale) + { + // Same sale + if((int)$adding_sale['id_sale'] == (int)$current_sale['id_sale']){ + return true; + } + + // Current sale is not Noel, not classic, not particular + if((int)$current_sale['delivery_delay'] != 1 && (int)$current_sale['delivery_delay'] != 5 && (int)$current_sale['delivery_delay'] != 9){ + + // Different sale but same shipping (philea for now) and same delivery delay + if( + (int)$adding_sale['id_sale'] != (int)$current_sale['id_sale'] + && (int)$current_sale['shipping'] == (int)$adding_sale['shipping'] + && (int)$adding_sale['delivery_delay'] == (int)$current_sale['delivery_delay'] + ){ + return true; + } + + // Not the same delivery delay or not the same shipping or not the same sale (condition is here for the understanding) + elseif ( + (int)$current_sale['shipping'] != (int)$adding_sale['shipping'] + || (int)$adding_sale['delivery_delay'] != (int)$current_sale['delivery_delay'] + || (int)$adding_sale['id_sale'] != (int)$current_sale['id_sale'] + ){ + return false; + } + + } + + // only 9 and 1 can be combined (classic and particular ones) + if( + ((int)$current_sale['delivery_delay'] == 1 || (int)$current_sale['delivery_delay'] == 9) + && ((int)$adding_sale['delivery_delay'] != 1 && (int)$adding_sale['delivery_delay'] != 9) + ){ + return false; + } + + // Particular and classic but different shipping + if( + ((int)$current_sale['delivery_delay'] == 1 || (int)$current_sale['delivery_delay'] == 9) + && ((int)$adding_sale['delivery_delay'] == 1 || (int)$adding_sale['delivery_delay'] == 9) + && (int)$current_sale['shipping'] != (int)$adding_sale['shipping'] + ){ + return false; + } + + // Noel sale can be combined only with Noel sale of same shipping + if( + (int)$adding_sale['delivery_delay'] == 5 + && ( + (int)$current_sale['delivery_delay'] != (int)$adding_sale['delivery_delay'] + || ( + (int)$current_sale['delivery_delay'] == (int)$adding_sale['delivery_delay'] + && (int)$current_sale['shipping'] != (int)$adding_sale['shipping'] + ) + ) + ){ + return false; + } + + return true; + } } diff --git a/modules/privatesales_delay/delivery_date.tpl b/modules/privatesales_delay/delivery_date.tpl index bd152825..310e08c9 100644 --- a/modules/privatesales_delay/delivery_date.tpl +++ b/modules/privatesales_delay/delivery_date.tpl @@ -8,6 +8,10 @@

{l s='Livraison avant Noel' mod='privatesales_delay'} : {l s='Envoi en 48h' mod='privatesales_delay'}

+ {elseif $k == 0} +

+ {l s='Livraison Spécifique' mod='privatesales_delay'} +

{else}

{$date.name} : {l s='Date de réception prévue entre le' mod='privatesales_delay'} {$date.date_start|date_format:'%d/%m/%Y'} {l s='et le' mod='privatesales_delay'} {$date.date_end|date_format:'%d/%m/%Y'} diff --git a/modules/privatesales_delay/saledelay.php b/modules/privatesales_delay/saledelay.php index dcca956b..24cc2a72 100755 --- a/modules/privatesales_delay/saledelay.php +++ b/modules/privatesales_delay/saledelay.php @@ -148,6 +148,9 @@ if (!class_exists('SaleDelay')) { case '8': return '48h Noël'; break; + case '9': + return 'Spé'; + break; default: return 'Inconnu'; break; @@ -175,6 +178,9 @@ if (!class_exists('SaleDelay')) { case '7': return '72h'; break; + case '9': + return 'Spé'; + break; case '8': return '48h Noël'; break; diff --git a/override/controllers/CartController.php b/override/controllers/CartController.php index abe42da0..761f2abb 100644 --- a/override/controllers/CartController.php +++ b/override/controllers/CartController.php @@ -60,13 +60,6 @@ class CartController extends CartControllerCore if ($add){ $current_sale = array(); foreach(self::$cart->getProducts() as $product) { - /*$current_sale = Db::getInstance()->getRow(' - SELECT ps.`delivery_delay`, ps.`id_sale` - FROM `'._DB_PREFIX_.'product_ps_cache` psc - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`) - WHERE psc.`id_product` = '.(int) $product['id_product'] - );*/ - $sale = Sale::getSaleFromCategory((int)$product['id_category_default'], false); $current_sale = array( 'id_sale'=> (int) $sale->id, @@ -76,12 +69,6 @@ class CartController extends CartControllerCore // Only one delivery id per cart, so we can break now break; } - /*$adding_sale = Db::getInstance()->getRow(' - SELECT ps.`delivery_delay`, ps.`id_sale` - FROM `'._DB_PREFIX_.'product_ps_cache` psc - LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`) - WHERE psc.`id_product` = '.(int) $idProduct - );*/ $current_product = new Product((int) $idProduct); $sale2 = Sale::getSaleFromCategory($current_product->id_category_default, false); $adding_sale = array( @@ -90,49 +77,24 @@ class CartController extends CartControllerCore 'shipping' => Sale::getShippingSale((int)$sale2->id) ); - //die('{"hasError" : true,"popup_error_cart": true, "errors" : [" '.implode(' , ',$current_sale).' - '.implode(' , ',$adding_sale).'"]}'); - if (!empty($current_sale)) { global $smarty; - // Only classic/Noel delivery sales can be added together or product of the same sale or philea sales with same delivery delay - if (( - ((int)$current_sale['delivery_delay'] != 1 && (int)$current_sale['delivery_delay'] != 5) - && (int)$adding_sale['id_sale'] != (int)$current_sale['id_sale'] - && ( - ( - ((int)$current_sale['shipping'] == 1 && (int)$current_sale['shipping'] == (int)$adding_sale['shipping']) - && (int)$adding_sale['delivery_delay'] != (int)$current_sale['delivery_delay'] - ) - || ((int)$current_sale['shipping'] != 1 && (int)$adding_sale['shipping'] == 1) - || ((int)$current_sale['shipping'] == 1 && (int)$adding_sale['shipping'] != 1) - || ((int)$current_sale['shipping'] != 1 && (int)$adding_sale['shipping'] != 1) - ) - ) || ( - ((int)$current_sale['delivery_delay'] != 1 && (int)$adding_sale['delivery_delay'] == 1) - || ( - (int)$current_sale['delivery_delay'] == 1 - && (int)$adding_sale['delivery_delay'] == 1 - && (int)$current_sale['shipping'] != (int)$adding_sale['shipping'] - ) - ) || ( - ((int)$current_sale['delivery_delay'] != 5 && (int)$adding_sale['delivery_delay'] == 5) - || ((int)$current_sale['delivery_delay'] == 5 && (int)$adding_sale['delivery_delay'] != 5) - ) - ) { - // Product in cart comes from special delivery sale - only products from the same sale can be added - if (Tools::getValue('ajax') == 'true') { - die('{"hasError" : true,"popup_error_cart": true, "errors" : ["'.Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false).'"]}'); - } else { - $this->errors[] = Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false); - } - } elseif ((int)$current_sale['delivery_delay'] == 1 && (int)$adding_sale['delivery_delay'] != 1) { - // Product in cart comes from classic delivery sale - only products from classic delivery sale can be added + if ((int)$current_sale['delivery_delay'] == 1 && !Sale::isCombinable($current_sale,$adding_sale)){ + // Product in cart comes from classic delivery sale if (Tools::getValue('ajax') == 'true') { die('{"hasError" : true,"popup_error_cart": true, "errors" : ["'.Tools::displayError('Sorry your cart already contains products from a sale shipped within 3 weeks. To continue, please confirm your order or empty your cart.', false).'"]}'); } else { $this->errors[] = Tools::displayError('Sorry your cart already contains products from a sale shipped within 3 weeks. To continue, please confirm your order or empty your cart.', false); } } + elseif (!Sale::isCombinable($current_sale,$adding_sale)){ + // Product in cart comes from special delivery sale + if (Tools::getValue('ajax') == 'true') { + die('{"hasError" : true,"popup_error_cart": true, "errors" : ["'.Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false).'"]}'); + } else { + $this->errors[] = Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false); + } + } } } // @End adding Antadis diff --git a/themes/site/product.tpl b/themes/site/product.tpl index ddc39637..bd0174c5 100755 --- a/themes/site/product.tpl +++ b/themes/site/product.tpl @@ -515,8 +515,12 @@ var oneQuantityAvailableSentence = '{l s='Warning: 1 item in stock!' js=1}'; {include file="./themes/site/delay.tpl" delivery_delay=$sale->delivery_delay} {else} + {if $sale->delivery_delay == 1} {l s='Livraison estimée sous'}
{l s=' 3 semaines'} + {elseif $sale->delivery_delay == 9} +  
  + {/if} {/if}

diff --git a/themes/site/shopping-cart.tpl b/themes/site/shopping-cart.tpl index c42861b4..4179c9a1 100755 --- a/themes/site/shopping-cart.tpl +++ b/themes/site/shopping-cart.tpl @@ -230,6 +230,10 @@ - {l s='Livraison prévue avant le'} {l s='24/12'} + {elseif $k==9 && $k==$key} + + - {l s='Livraison spécifique'} + {/if} {/foreach} {/if} diff --git a/themes/site_mobile/product.tpl b/themes/site_mobile/product.tpl index 04250efb..a89b6d95 100755 --- a/themes/site_mobile/product.tpl +++ b/themes/site_mobile/product.tpl @@ -464,8 +464,12 @@ var oneQuantityAvailableSentence = '{l s='Warning: 1 item in stock!' js=1}'; {include file="./themes/site_mobile/delay.tpl" delivery_delay=$sale->delivery_delay} {else} + {if $sale->delivery_delay == 1} {l s='Livraison estimée sous'}
{l s=' 3 semaines'} + {elseif $sale->delivery_delay == 9} +  
  + {/if} {/if}