bebeboutik/modules/privatesales/Sale.php

1905 lines
56 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
if (!class_exists('Sale')) {
class Sale {
var $id;
var $date_start;
var $date_end;
var $timelaps;
2016-04-04 10:49:37 +02:00
var $id_country = 0;
2016-01-04 12:49:26 +01:00
var $enabled = 0;
var $featured = 0;
var $logout = 0;
var $pub = 0;
var $new = 0;
2016-03-14 10:59:58 +01:00
var $braderie = 0;
2016-01-04 12:49:26 +01:00
var $forward_news = 0;
var $id_category;
var $id_employee = 0;
var $shipping_fr = 0;
2016-01-04 12:49:26 +01:00
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'];
2016-04-04 10:49:37 +02:00
$this->id_country = $sale['id_country'];
2016-01-04 12:49:26 +01:00
$this->enabled = $sale['enabled'];
$this->featured = $sale['featured'];
$this->logout = $sale['logout'];
$this->pub = $sale['pub'];
$this->new = $sale['new'];
2016-03-14 10:59:58 +01:00
$this->braderie = $sale['braderie'];
2016-01-04 12:49:26 +01:00
$this->forward_news = $sale['forward_news'];
$this->id_category = $sale['id_category'];
$this->id_employee = $sale['id_employee'];
$this->shipping_fr = $sale['shipping_fr'];
2016-01-04 12:49:26 +01:00
$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->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).'",
2016-04-04 10:49:37 +02:00
`id_country` = '.(int) $this->id_country.',
2016-01-04 12:49:26 +01:00
`enabled` = '.(int) $this->enabled.',
`featured` = '.(int) $this->featured.',
`logout` = '.(int) $this->logout.',
`new` = '.(int) $this->new.',
2016-03-14 10:59:58 +01:00
`braderie` = '.(int) $this->braderie.',
2016-01-04 12:49:26 +01:00
`forward_news` = '.(int) $this->forward_news.',
`public` = '.(int) $this->pub.',
2016-03-16 15:24:12 +01:00
`id_category` = '.(int) $this->id_category.',
2016-01-04 12:49:26 +01:00
`id_employee` = '.(int) $this->id_employee.',
`shipping_fr` = '.(int) $this->shipping_fr.',
2016-01-04 12:49:26 +01:00
`delivery_delay` = '.(int) $this->delivery_delay.',
`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).'",
2016-04-04 10:49:37 +02:00
'.(int) $this->id_country.',
2016-01-04 12:49:26 +01:00
'.(int) $this->enabled.',
'.(int) $this->featured.',
'.(int) $this->logout.',
'.(int) $this->pub.',
'.(int) $this->new.',
2016-03-14 10:59:58 +01:00
'.(int) $this->braderie.',
2016-01-04 12:49:26 +01:00
'.(int) $this->forward_news.',
'.(int) $this->id_category.',
'.(int) $this->id_employee.',
'.(int) $this->shipping_fr.',
2016-01-04 12:49:26 +01:00
NOW(),
2016-03-16 15:24:12 +01:00
'.(int) $this->sale_type.',
2016-01-04 12:49:26 +01:00
'.(int) $this->delivery_delay.',
'.($position+1).'
)
');
$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).'"
)
');
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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_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 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,
2016-04-04 10:49:37 +02:00
'id_country' => $ps[0]['id_country'],
2016-01-04 12:49:26 +01:00
'enabled' => $ps[0]['enabled'],
'featured' => $ps[0]['featured'],
'logout' => $ps[0]['logout'],
'pub' => $ps[0]['public'],
'new' => $ps[0]['new'],
2016-03-14 10:59:58 +01:00
'braderie' => $ps[0]['braderie'],
2016-01-04 12:49:26 +01:00
'forward_news' => $ps[0]['forward_news'],
'position' => $ps[0]['position'],
'id_category' => $ps[0]['id_category'],
'id_employee' => $ps[0]['id_employee'],
'shipping_fr' => $ps[0]['shipping_fr'],
2016-01-04 12:49:26 +01:00
'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;
}
2016-04-12 15:17:45 +02:00
public function getMultiForStats($orders = array(), $nb_sale, $strict = false)
2016-01-04 12:49:26 +01:00
{
$products = $this->getProducts();
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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) {
2016-04-06 13:09:54 +02:00
if($strict) {
if(count($order) == (int)$nb_sale) {
$multi[] = $key;
}
} else {
if(count($order) >= (int)$nb_sale) {
$multi[] = $key;
}
2016-01-04 12:49:26 +01:00
}
}
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`
2016-03-16 15:24:12 +01:00
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`
2016-01-04 12:49:26 +01:00
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();
2016-03-16 15:24:12 +01:00
foreach ($order_print as $key => $order) {
2016-01-04 12:49:26 +01:00
$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'
2016-03-16 15:24:12 +01:00
);
2016-01-04 12:49:26 +01:00
$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`
2016-03-16 15:24:12 +01:00
// 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`
2016-01-04 12:49:26 +01:00
// FROM `'._DB_PREFIX_.'lapostews` la
// WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`)
// )'
// ) as $order) {
// $orders_not_sended[] = $order['id_order'];
// };
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
// $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();
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
foreach ($id_sales as $key => $id_sale) {
$sale = new Sale($id_sale);
$products = $sale->getProducts();
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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);
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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();
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
foreach ($_orders as $key => $order) {
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
// supprime les orders avec plus de 2 ventes pour les multi only
if($multi_only) {
if (count($order) > 2) {
continue;
}
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
// 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).'
');
}
2016-03-16 15:24:12 +01:00
if ($multi_only) {
2016-01-04 12:49:26 +01:00
return $order_print;
} else {
// tri par date pour les M3+
sort($order_print);
return $order_print;
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
return $order_print;
}
2016-06-20 12:49:32 +02:00
/**
* @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) {
if($id_sales < 2) {
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();
2016-07-11 16:15:04 +02:00
$_nb_product = array();
2016-06-20 12:49:32 +02:00
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();
2016-07-11 16:13:21 +02:00
$_nb_product[(int)$row['id_order']] = 1;
} else {
$_nb_product[(int)$row['id_order']]++;
2016-06-20 12:49:32 +02:00
}
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) {
// 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 (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(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 {
2016-07-11 17:06:11 +02:00
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;
2016-06-20 12:49:32 +02:00
}
return $order_print;
}
2016-01-04 12:49:26 +01:00
public function getOrdersFromSale($id_status=NULL, $single_only=TRUE, $send_test = FALSE) {
$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'];
}
}
2016-03-16 15:24:12 +01:00
// TEST pour les commandes en 48h
2016-01-04 12:49:26 +01:00
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`
2016-03-16 15:24:12 +01:00
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`
2016-01-04 12:49:26 +01:00
FROM `'._DB_PREFIX_.'lapostews` la
WHERE la.`quantity` = (od.`product_quantity` - od.`product_quantity_refunded`)
)'
) as $order) {
$orders_not_sended[] = $order['id_order'];
};
2016-03-16 15:24:12 +01:00
// tri orders par nb product
2016-01-04 12:49:26 +01:00
$order_ordered = array();
foreach ($orders_not_sended as $key => $order) {
$total_ref = Db::getInstance()->getValue('
2016-03-16 15:24:12 +01:00
SELECT COUNT(`id_order_detail`)
FROM `'._DB_PREFIX_.'order_detail`
2016-01-04 12:49:26 +01:00
WHERE `id_order` = '.(int) $order
);
$order_ordered[(int) $order] = $total_ref;
}
asort($order_ordered);
return array_keys($order_ordered);
// return $orders_not_sended;
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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;
}
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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;
}
2016-06-20 12:49:32 +02:00
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;
}
2016-01-04 12:49:26 +01:00
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('
2016-03-16 15:24:12 +01:00
SELECT
p.`id_sale`,
p.`date_start`,
2016-01-04 12:49:26 +01:00
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');
}
2016-06-20 12:49:32 +02:00
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');
}
2016-01-04 12:49:26 +01:00
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;
}
2016-03-16 15:24:12 +01:00
2016-03-21 10:22:14 +01:00
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) {
2016-01-04 12:49:26 +01:00
global $cookie;
$result = array();
$where = array();
$query = '
SELECT p.`id_sale`
FROM `'._DB_PREFIX_.'privatesale` p
';
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
// 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).'"';
}
2016-03-16 15:24:12 +01:00
if($enabled !== NULL OR $logout !== NULL OR $featured !== NULL OR $future !== NULL) {
2016-01-04 12:49:26 +01:00
$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 === 'last') {
$where[] = 'CAST(`date_end` AS DATE) = CAST(NOW() 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;
// }
2016-03-21 10:22:14 +01:00
if ($without_consumable && _SHOP_CATEGORYENABLED!== FALSE) {
2016-03-16 17:56:03 +01:00
$query .= ' AND p.`id_sale` != '.(int) _SHOP_PRIVATESALES_CONSUMABLE;
2016-03-16 15:24:12 +01:00
}
2016-01-04 12:49:26 +01:00
$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;
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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();
2016-03-16 15:24:12 +01:00
$enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE;
2016-01-04 12:49:26 +01:00
$pub != FALSE? '': $where[] = '`public` = 0';
if ($site_version) {
2016-03-16 15:24:12 +01:00
$where[] = ' cv.`version` = "'.pSQL($site_version).'"';
2016-01-04 12:49:26 +01:00
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
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)) {
2016-03-16 15:24:12 +01:00
foreach($sales AS $sale) {
2016-01-04 12:49:26 +01:00
$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);
2016-11-10 16:25:56 +01:00
//mail('marion@antadis.com', '[BBB] Creation category - buildCategoryCache', http_build_query($categories,'',', ')); // check for bad behaviour
2016-01-04 12:49:26 +01:00
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;
}
2016-06-07 16:02:16 +02:00
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'privatesale`
SET `enabled` = 0
WHERE `id_sale` = '.(int) $this->id.'
LIMIT 1
');
2016-06-07 16:02:16 +02:00
// $this->enabled = FALSE;
// $this->save();
2016-01-04 12:49:26 +01:00
$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';
2016-03-16 15:24:12 +01:00
return Db::getInstance()->getValue($sql);
2016-01-04 12:49:26 +01:00
}
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;
}
2016-11-18 09:50:04 +01:00
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);
}
}
2016-01-04 12:49:26 +01:00
}
}