408 lines
14 KiB
PHP
408 lines
14 KiB
PHP
<?php
|
|
if(isset($_SERVER['REMOTE_ADDR'])) {
|
|
header('Location: http://www.bebeboutik.com/');
|
|
exit;
|
|
}
|
|
|
|
$_SERVER['HTTP_PORT'] = 80;
|
|
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
|
|
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
|
|
include dirname(__FILE__).'/../../config/config.inc.php';
|
|
include dirname(__FILE__).'/../../modules/privatesales/Sale.php';
|
|
|
|
// Db::getInstance()->ExecuteS('
|
|
// INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` (
|
|
// SELECT `id_product`, `id_sale`
|
|
// FROM `'._DB_PREFIX_.'product` p
|
|
// LEFT JOIN `'._DB_PREFIX_.'privatesale_category` c
|
|
// ON p.`id_category_default` = c.`id_category`
|
|
// )
|
|
// ');
|
|
|
|
// echo date('Y-m-d', strtotime(date('Y-m-d 00:00:00').' - '.(int) $argv[1].' day'))."\n";
|
|
|
|
$day_from = date('Y-m-d', strtotime(date('Y-m-d 00:00:00').' - '.(int) $argv[1].' day'));
|
|
$day_to = date('Y-m-d', strtotime(date('Y-m-d 00:00:00').' - '.(int) $argv[1].' day'));
|
|
|
|
$langs = array('fr' => 2, 'es' => 3);
|
|
|
|
|
|
/**
|
|
* PART 1
|
|
* Data by lang
|
|
*/
|
|
|
|
$ids_total = array();
|
|
$multi_total = array();
|
|
foreach ($langs as $iso => $id_lang) {
|
|
$sales = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT p.*, l.`name`
|
|
FROM `'._DB_PREFIX_.'privatesale` p
|
|
LEFT JOIN `'._DB_PREFIX_.'category_lang` l
|
|
ON p.`id_category` = l.`id_category`
|
|
RIGHT JOIN `'._DB_PREFIX_.'privatesale_site_version` v ON v.`id_sale` = p.`id_sale` AND v.`version` = "'.$iso.'"
|
|
WHERE ((CAST(p.`date_start` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_start` AS DATE) <= "'.pSQL($day_to).'")
|
|
OR (CAST(p.`date_end` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) <= "'.pSQL($day_to).'")
|
|
OR (CAST(p.`date_start` AS DATE) <= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) >= "'.pSQL($day_to).'"))
|
|
AND l.`id_lang` = 2
|
|
GROUP BY p.`id_sale`
|
|
ORDER BY p.`date_start` DESC
|
|
') AS $row) {
|
|
$sales[(int) $row['id_sale']] = $row;
|
|
}
|
|
|
|
$period = array();
|
|
$ids_actual = array();
|
|
|
|
foreach($sales as $sale) {
|
|
$period[(int) $sale['id_sale']] = array(0, 0);
|
|
|
|
$ids_product = array();
|
|
foreach (Db::getInstance()->ExecuteS('
|
|
SELECT `id_product`
|
|
FROM `'._DB_PREFIX_.'product_ps_cache`
|
|
WHERE `id_sale` = '.(int) $sale['id_sale'].'
|
|
') as $key => $value) {
|
|
$ids_product[] = $value['id_product'];
|
|
}
|
|
|
|
$total = Db::getInstance()->getRow('
|
|
SELECT SUM(d.`product_quantity`) AS `total_products`, SUM(ROUND(((d.`product_price` * (1 - d.`reduction_percent` / 100) - d.`reduction_amount`) * (1 - d.`group_reduction` / 100) * (1 + d.`tax_rate` / 100)) * d.`product_quantity`, 6)) AS `total_sales_wt`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT JOIN `'._DB_PREFIX_.'orders` o
|
|
ON d.`id_order` = o.`id_order`
|
|
WHERE d.`product_id` IN ('.implode(',', $ids_product).')
|
|
AND o.`valid` = 1
|
|
AND o.`id_lang` = '.(int) $id_lang.'
|
|
AND o.`date_add` >= "'.pSQL(date('Y-m-d 00:00:00', strtotime($day_from))).'"
|
|
AND o.`date_add` < DATE_ADD("'.pSQL(date('Y-m-d 00:00:00', strtotime($day_from))).'", INTERVAL 1 DAY)
|
|
');
|
|
|
|
$ids_total[(int) $id_lang][(int) $sale['id_sale']] = array();
|
|
$ids_actual[(int) $sale['id_sale']] = array();
|
|
|
|
$ids = Db::getInstance()->ExecuteS('
|
|
SELECT DISTINCT d.`id_order`, CAST(o.`date_add` as DATE) as `date_add`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT JOIN `'._DB_PREFIX_.'orders` o
|
|
ON d.`id_order` = o.`id_order`
|
|
WHERE d.`product_id` IN ('.implode(',', $ids_product).')
|
|
AND o.`valid` = 1
|
|
AND o.`id_lang` = '.(int) $id_lang.'
|
|
');
|
|
|
|
foreach ($ids as $key => $id) {
|
|
$ids_total[(int) $id_lang][(int) $sale['id_sale']][] = $id['id_order'];
|
|
if ($id['date_add'] == $day_from) {
|
|
$ids_actual[(int) $sale['id_sale']][] = $id['id_order'];
|
|
}
|
|
}
|
|
|
|
// multi orders by sale
|
|
$sale_obj = new Sale((int) $sale['id_sale']);
|
|
$statuts = array(17, Configuration::get('PS_OS_PAYMENT'));
|
|
$all_multi = $sale_obj->getMultiForStats($ids_total[(int) $id_lang][(int) $sale['id_sale']], 2);
|
|
|
|
$multi_total[(int) $id_lang][(int) $sale['id_sale']] = array();
|
|
$multi_total[(int) $id_lang][(int) $sale['id_sale']] = $all_multi;
|
|
|
|
$multi = array();
|
|
if ($multi_total[(int) $id_lang][(int) $sale['id_sale']]) {
|
|
foreach ($multi_total[(int) $id_lang][(int) $sale['id_sale']] as $key => $id_order) {
|
|
if (in_array($id_order, $ids_actual[(int) $sale['id_sale']])) {
|
|
$multi[] = $id_order;
|
|
}
|
|
}
|
|
}
|
|
|
|
$period[(int) $sale['id_sale']][0] = (int) $total['total_products'];
|
|
$period[(int) $sale['id_sale']][1] = (float) $total['total_sales_wt'];
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
DELETE FROM `'._DB_PREFIX_.'privatesale_livestats`
|
|
WHERE `id_sale` = '.(int) $sale['id_sale'].'
|
|
AND `day` = "'.pSQL(date('Y-m-d', strtotime($day_from))).'"
|
|
AND `id_lang` = '.(int) $id_lang.'
|
|
');
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
INSERT INTO `'._DB_PREFIX_.'privatesale_livestats`
|
|
VALUES (
|
|
'.(int) $sale['id_sale'].',
|
|
'.(int) $id_lang.',
|
|
"'.pSQL(date('Y-m-d', strtotime($day_from))).'",
|
|
'.(float) $total['total_sales_wt'].',
|
|
'.(int) $total['total_products'].',
|
|
'.count($ids_actual[(int) $sale['id_sale']]).',
|
|
'.count($multi).',
|
|
NOW()
|
|
)
|
|
');
|
|
|
|
// stats for each lang (total_sales_wt, total_products)
|
|
$total = Db::getInstance()->getRow('
|
|
SELECT SUM(d.`product_quantity`) AS `total_products`, SUM(ROUND(((d.`product_price` * (1 - d.`reduction_percent` / 100) - d.`reduction_amount`) * (1 - d.`group_reduction` / 100) * (1 + d.`tax_rate` / 100)) * d.`product_quantity`, 6)) AS `total_sales_wt`, SUM(d.`product_quantity`) AS `total_products`, SUM(ROUND(((d.`product_price` * (1 - d.`reduction_percent` / 100) - d.`reduction_amount`) * (1 - d.`group_reduction` / 100)), 6)) AS `total_unit_sales`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
INNER JOIN `'._DB_PREFIX_.'orders` o ON o.id_order = d.id_order
|
|
WHERE d.`product_id` IN ('.implode(',', $ids_product).')
|
|
AND o.valid = 1
|
|
AND o.`id_lang` = '.(int) $id_lang.'
|
|
');
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
DELETE FROM `'._DB_PREFIX_.'privatesale_livestats_sale_lang`
|
|
WHERE `id_sale` = '.(int) $sale['id_sale'].'
|
|
AND `id_lang` = '.(int) $id_lang
|
|
);
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
INSERT INTO `'._DB_PREFIX_.'privatesale_livestats_sale_lang`
|
|
VALUES (
|
|
'.(int) $sale['id_sale'].',
|
|
'.(int) $id_lang.',
|
|
'.(float) $total['total_sales_wt'].',
|
|
'.(int) $total['total_products'].',
|
|
NOW()
|
|
)
|
|
');
|
|
}
|
|
// foreach sales
|
|
}
|
|
// fin foreach lang
|
|
|
|
/**
|
|
* PART 2
|
|
* global @data
|
|
*/
|
|
$sales = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT p.*, l.`name`
|
|
FROM `'._DB_PREFIX_.'privatesale` p
|
|
LEFT JOIN `'._DB_PREFIX_.'category_lang` l
|
|
ON p.`id_category` = l.`id_category`
|
|
WHERE ((CAST(p.`date_start` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_start` AS DATE) <= "'.pSQL($day_to).'")
|
|
OR (CAST(p.`date_end` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) <= "'.pSQL($day_to).'")
|
|
OR (CAST(p.`date_start` AS DATE) <= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) >= "'.pSQL($day_to).'"))
|
|
AND l.`id_lang` = 2
|
|
GROUP BY p.`id_sale`
|
|
ORDER BY p.`date_start` DESC
|
|
') AS $row) {
|
|
$sales[(int) $row['id_sale']] = $row;
|
|
}
|
|
|
|
$stocks = array();
|
|
$stocks_sold = array();
|
|
$current_stocks = array();
|
|
$detail_stocks = array();
|
|
$detail_sales = array();
|
|
$global = array();
|
|
foreach($sales as $sale) {
|
|
$oos = 0;
|
|
$oos_sold = 0;
|
|
$total = 0;
|
|
$total_sold = 0;
|
|
$quantities = 0;
|
|
$id_attributes = array();
|
|
$detail_stocks[(int) $sale['id_sale']] = array();
|
|
$detail_sales[(int) $sale['id_sale']] = array();
|
|
|
|
|
|
$ids_product = array();
|
|
foreach (Db::getInstance()->ExecuteS('
|
|
SELECT `id_product`
|
|
FROM `'._DB_PREFIX_.'product_ps_cache`
|
|
WHERE `id_sale` = '.(int) $sale['id_sale'].'
|
|
') as $key => $value) {
|
|
$ids_product[] = $value['id_product'];
|
|
}
|
|
|
|
|
|
$sold_products = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT DISTINCT `product_id`, `product_attribute_id`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
INNER JOIN `'._DB_PREFIX_.'orders` o ON d.id_order = o.id_order
|
|
WHERE a.`id_product` IN ('.implode(',', $ids_product).')
|
|
AND o.valid = 1
|
|
') as $row) {
|
|
$sold_products[] = (int) $row['product_id'].'-'.(int) $row['product_attribute_id'];
|
|
}
|
|
|
|
$query = Db::getInstance()->ExecuteS('
|
|
SELECT a.`id_product_attribute`, a.`id_product`, a.`quantity`
|
|
FROM `'._DB_PREFIX_.'product_attribute` a
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p
|
|
ON p.`id_product` = a.`id_product`
|
|
WHERE a.`id_product` IN ('.implode(',', $ids_product).')
|
|
GROUP BY a.`id_product_attribute`
|
|
');
|
|
|
|
$total_sold += count($sold_products);
|
|
|
|
foreach($query as $row) {
|
|
if((int) $row['quantity'] <= 0) {
|
|
$oos++;
|
|
if(in_array((int) $row['id_product'].'-'.(int) $row['id_product_attribute'], $sold_products)) {
|
|
$oos_sold++;
|
|
$total++;
|
|
}
|
|
$detail_stocks[(int) $sale['id_sale']][(int) $row['id_product'].'-'.(int) $row['id_product_attribute']] = 0;
|
|
} else {
|
|
$quantities += (int) $row['quantity'];
|
|
$detail_stocks[(int) $sale['id_sale']][(int) $row['id_product'].'-'.(int) $row['id_product_attribute']] = (int) $row['quantity'];
|
|
$total++;
|
|
}
|
|
|
|
$id_attributes[] = (int) $row['id_product'];
|
|
}
|
|
|
|
$query = Db::getInstance()->ExecuteS('
|
|
SELECT p.`quantity`, p.`id_product`
|
|
FROM `'._DB_PREFIX_.'product` p
|
|
WHERE p.`id_product` IN ('.implode(',', $ids_product).')
|
|
'.((count($id_attributes) > 0)? ' AND p.`id_product` NOT IN ('.implode(', ', $id_attributes).')': '').'
|
|
GROUP BY p.`id_product`
|
|
');
|
|
|
|
foreach($query as $row) {
|
|
if((int) $row['quantity'] <= 0) {
|
|
$oos++;
|
|
if(in_array((int) $row['id_product'].'-0', $sold_products)) {
|
|
$oos_sold++;
|
|
$total++;
|
|
}
|
|
$detail_stocks[(int) $sale['id_sale']][(int) $row['id_product'].'-0'] = 0;
|
|
} else {
|
|
$quantities += (int) $row['quantity'];
|
|
$detail_stocks[(int) $sale['id_sale']][(int) $row['id_product'].'-0'] = (int) $row['quantity'];
|
|
$total++;
|
|
}
|
|
}
|
|
|
|
$stocks[(int) $sale['id_sale']] = ceil($oos_sold * 100 / $total);
|
|
$stocks_sold[(int) $sale['id_sale']] = ceil($oos_sold * 100 / $total_sold);
|
|
$current_stocks[(int) $sale['id_sale']] = $quantities;
|
|
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT `product_id`, `product_attribute_id`, SUM(`product_quantity`) AS `quantity`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
INNER JOIN `'._DB_PREFIX_.'orders` o ON d.id_order = o.id_order
|
|
WHERE `product_id` IN ('.implode(',', $ids_product).')
|
|
AND o.valid = 1
|
|
GROUP BY `product_id`, `product_attribute_id`
|
|
') as $row) {
|
|
$detail_sales[(int) $sale['id_sale']][(int) $row['product_id'].'-'.(int) $row['product_attribute_id']] = (int) $row['quantity'];
|
|
}
|
|
|
|
$warn = 0;
|
|
$trigger = 0;
|
|
|
|
if(time() - strtotime($sale['date_start']) < 8 * 3600) {
|
|
foreach($detail_stocks[(int) $sale['id_sale']] as $k => $v) {
|
|
if(isset($detail_sales[(int) $sale['id_sale']][$k])) {
|
|
if($detail_sales[(int) $sale['id_sale']][$k] * 100 / ($v + $detail_sales[(int) $sale['id_sale']][$k]) > 20) {
|
|
$trigger++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if($trigger * 100 / count($detail_stocks[(int) $sale['id_sale']]) > 5) {
|
|
$warn = 1;
|
|
}
|
|
} elseif(time() - strtotime($sale['date_start']) < 29 * 3600) {
|
|
foreach($detail_stocks[(int) $sale['id_sale']] as $k => $v) {
|
|
if(isset($detail_sales[(int) $sale['id_sale']][$k])) {
|
|
if($detail_sales[(int) $sale['id_sale']][$k] * 100 / ($v + $detail_sales[(int) $sale['id_sale']][$k]) > 30) {
|
|
$trigger++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if($trigger * 100 / count($detail_stocks[(int) $sale['id_sale']]) > 15) {
|
|
$warn = 2;
|
|
}
|
|
} elseif(time() - strtotime($sale['date_start']) < 53 * 3600) {
|
|
foreach($detail_stocks[(int) $sale['id_sale']] as $k => $v) {
|
|
if(isset($detail_sales[(int) $sale['id_sale']][$k])) {
|
|
if($detail_sales[(int) $sale['id_sale']][$k] * 100 / ($v + $detail_sales[(int) $sale['id_sale']][$k]) > 40) {
|
|
$trigger++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if($trigger * 100 / count($detail_stocks[(int) $sale['id_sale']]) > 20) {
|
|
$warn = 3;
|
|
}
|
|
} else {
|
|
foreach($detail_stocks[(int) $sale['id_sale']] as $k => $v) {
|
|
if(isset($detail_sales[(int) $sale['id_sale']][$k])) {
|
|
if($detail_sales[(int) $sale['id_sale']][$k] * 100 / ($v + $detail_sales[(int) $sale['id_sale']][$k]) > 50) {
|
|
$trigger++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if($trigger * 100 / count($detail_stocks[(int) $sale['id_sale']]) > 25) {
|
|
$warn = 3;
|
|
}
|
|
}
|
|
|
|
$total = Db::getInstance()->getRow('
|
|
SELECT SUM(d.`product_quantity`) AS `total_products`, SUM(ROUND(((d.`product_price` * (1 - d.`reduction_percent` / 100) - d.`reduction_amount`) * (1 - d.`group_reduction` / 100) * (1 + d.`tax_rate` / 100)) * d.`product_quantity`, 6)) AS `total_sales_wt`, SUM(d.`product_quantity`) AS `total_products`, SUM(ROUND(((d.`product_price` * (1 - d.`reduction_percent` / 100) - d.`reduction_amount`) * (1 - d.`group_reduction` / 100)), 6)) AS `total_unit_sales`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
INNER JOIN `'._DB_PREFIX_.'orders` o ON o.id_order = d.id_order
|
|
WHERE d.`product_id` IN ('.implode(',', $ids_product).')
|
|
AND o.valid = 1
|
|
');
|
|
|
|
$total_wholesale = (float) Db::getInstance()->getValue('
|
|
SELECT SUM(IF(a.`wholesale_price` = 0 OR a.`wholesale_price` IS NULL, p.`wholesale_price`, a.`wholesale_price`))
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
INNER JOIN `'._DB_PREFIX_.'orders` o ON o.id_order = d.id_order
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p
|
|
ON d.`product_id` = p.`id_product`
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` a
|
|
ON d.`product_attribute_id` = a.`id_product_attribute`
|
|
WHERE p.`id_product` IN ('.implode(',', $ids_product).')
|
|
AND o.`valid` = 1
|
|
');
|
|
|
|
if($total_wholesale == 0) {
|
|
$margin = 0;
|
|
} else {
|
|
$margin = ($total['total_unit_sales'] - $total_wholesale) / $total['total_unit_sales'];
|
|
}
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
DELETE FROM `'._DB_PREFIX_.'privatesale_livestats_sale`
|
|
WHERE `id_sale` = '.(int) $sale['id_sale'].'
|
|
');
|
|
|
|
$total_sale = 0;
|
|
$multi_total_sale = 0;
|
|
foreach ($langs as $key => $id_lang) {
|
|
$multi_total_sale += count($multi_total[(int) $id_lang][(int) $sale['id_sale']]);
|
|
$total_sale += count($ids_total[(int) $id_lang][(int) $sale['id_sale']]);
|
|
}
|
|
|
|
Db::getInstance()->ExecuteS('
|
|
INSERT INTO `'._DB_PREFIX_.'privatesale_livestats_sale`
|
|
VALUES (
|
|
'.(int) $sale['id_sale'].',
|
|
'.((int) $total['total_products'] + (int) $current_stocks[(int) $sale['id_sale']]).',
|
|
'.(float) $total['total_sales_wt'].',
|
|
'.(int) $total['total_products'].',
|
|
'.(int) $stocks[(int) $sale['id_sale']].',
|
|
'.(int) $stocks_sold[(int) $sale['id_sale']].',
|
|
'.(float) $margin.',
|
|
'.(int) $warn.',
|
|
'.$total_sale.',
|
|
'.$multi_total_sale.',
|
|
NOW()
|
|
)
|
|
');
|
|
}
|
|
|
|
Configuration::updateValue('PRIVATESALES_LIVESTATS_DATE', time());
|