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->uncombinable = $sale['uncombinable']; $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'; $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 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.', `uncombinable` = '.(int) $this->uncombinable.', `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()->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_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.' ) '); } 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); 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->uncombinable.', '.(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.' // ) // '); // } 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_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); // } 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 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 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']); } } } 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; } $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'], 'uncombinable' => $ps[0]['uncombinable'], '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']; // } $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']; } $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']; } 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; } 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']; } } return $orders; } 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; } } $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(); 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']; }; $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_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); } } 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).' '); } return $order_print; } // 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']; // } // } // $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; // } // } // $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.')'; // } // Db::getInstance()->ExecuteS(' // INSERT IGNORE INTO `'._DB_PREFIX_.'privatesale_printedinvoices` // VALUES '.implode(', ', $printed).' // '); // } // 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; } $orders = array(); 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']; } } } } $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; } } $order_print = array(); 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; } } // 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; } } } } 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).' '); } if ($multi_only) { return $order_print; } else { // tri par date pour les M3+ sort($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; } 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'))); } $orders = array(); 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']; } } } } $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; } } $order_print = array(); $order_sales = array(); foreach ($_orders as $key => $order) { $added_sale = false; // 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)) { foreach(Db::getInstance()->executeS(' SELECT GREATEST(d.`product_quantity` - d.`product_quantity_refunded`, 0) as `p_quantity` FROM `'._DB_PREFIX_.'product_ps_cache` c LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`product_id` = c.`id_product`) WHERE d.`id_order` = '.(int)$key.' AND c.id_sale = '.(int)$sale ) as $row){ if((int)$row['p_quantity'] != 0) { continue 3; } $added_sale = true; } } } $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 || $added_sale) { $order_print[] = $key; } } } } 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).' '); } 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; } public function getOrdersFromSale($id_status=NULL, $single_only=TRUE, $send_test = FALSE, $carrier = 'lp') { $orders = array(); $products = $this->getProducts(); 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 ((is_int($future) || $future === true) || false === ($sales = CacheRedis::getInstance()->getQuery($query, self::CONTROLLER_NAME))) { $sales = Db::getInstance()->ExecuteS($query); CacheRedis::getInstance()->setQuery($query, self::CONTROLLER_NAME, $sales); } if($sales) { 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; } /** * 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 "; $results = Db::getInstance()->ExecuteS($sql); // 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'] ); $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 if the sale is tagged as uncombinable or not * @param [int] $id_sale * @return boolean */ public static function isUncombinable($id_sale) { $result = Db::getInstance()->getValue(' SELECT `uncombinable` FROM `'._DB_PREFIX_.'privatesale` WHERE `id_sale` = '.(int)$id_sale.' '); if($result && (int)$result == 1){ return true; } return false; } /** * 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($sale, $added_sale) { if(is_array($sale) && is_array($added_sale)){ $current_sale = $sale; $adding_sale = $added_sale; } else { // Compatibility with object - We can test with the attribute // Same sale if((int)$sale->id == (int)$added_sale->id){ return true; } // Uncombinable if((int)$sale->uncombinable == 1 || (int)$added_sale->uncombinable == 1) { return false; } // $sale and $added_sale are Sale object $current_sale = array( 'id_sale'=> (int) $sale->id, 'delivery_delay'=> (int) $sale->delivery_delay, 'shipping' => self::getShippingSale((int)$sale->id) ); $adding_sale = array( 'id_sale'=> (int) $added_sale->id, 'delivery_delay'=> (int) $added_sale->delivery_delay, 'shipping' => self::getShippingSale((int)$added_sale->id) ); } // Same sale if((int)$adding_sale['id_sale'] == (int)$current_sale['id_sale']){ return true; } // Sale is uncombinable if(self::isUncombinable((int)$current_sale['id_sale']) || self::isUncombinable((int)$adding_sale['id_sale'])) { return false; } // 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 : id_shipping 1) 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'] && (int)$current_sale['shipping'] == 1 ){ 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 (only dropshipping and default shipping can be combinable) 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']) && (!in_array((int)$current_sale['shipping'],array(0,2)) || !in_array((int)$adding_sale['shipping'],array(0,2))) ){ 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; } } }