374 lines
17 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
class MakeStats {
2016-01-04 12:49:26 +01:00
public static function getProductsByCat($id_cat = NULL)
{
global $cookie;
$category = new Category($id_cat);
2016-04-06 13:09:54 +02:00
$products = $category->getProductsStats( $cookie->id_lang , "1", "1000000000000000000", "reference",NULL , FALSE, FALSE , FALSE );
2016-01-04 12:49:26 +01:00
return $products;
}
public static function pushLine(&$line, $product, $id_product_attribute = null, $date_from = null, $date_to = null)
{
global $cookie;
$stockMvt = Inventory::getStockMouvements($cookie->id_lang, $product->id, $id_product_attribute, false, $date_from, $date_to);
if(count($stockMvt) == 0)
return;
$array = Inventory::calculStockMvt($stockMvt, $product);
if($array['quantiteVendu'] > 0)
$line[] = array_merge($array, $stockMvt[0]);
}
public static function orderMultiArray($multi_array, $key)
{
$array = self::orderForReorder($multi_array);
$size = count($array);
for ($i=0; $i < $size; $i++) {
for ($j=$size-1; $j >= $i ; $j--) {
2016-01-04 12:49:26 +01:00
if($array[$j+1]['total_ht'] > $array[$j]['total_ht'] ) {
$temp = $array[$j+1];
$array[$j+1] = $array[$j];
$array[$j] = $temp;
}
}
}
return $array;
}
public static function orderForReorder($array)
2016-01-04 12:49:26 +01:00
{
$array_tmp = array();
$i = 0;
foreach ($array as $ids => $data) {
$ids = explode('-', $ids);
$array_id = array();
$array_id['id_product'] = $ids[0];
// $array_id['id_product_attribute'] = $ids[1];
$array_tmp[$i] = array_merge($array_id, $data);
$i++;
}
return $array_tmp;
}
public static function getProductPriceWithReduction($values)
{
return (float)( $values['product_price']*( 1-($values['reduction_percent']/100) ) );
}
public static function getItemPackSoldQuantity($id_item)
{
$query = Db::getInstance()->getRow('
SELECT SUM(od.`product_quantity` - od.`product_quantity_reinjected`) as quantity_sold , p.`quantity`
FROM `'._DB_PREFIX_.'pack` p
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON (od.`product_id` = p.`id_product_pack`)
LEFT JOIN `'._DB_PREFIX_.'order_state_current` oc ON (oc.`id_order`=od.`id_order`)
WHERE oc.`id_order_state` IN (13,4,17,9,1,11,10,5,14,12,2,3,18)
AND p.`id_product_item` = '.(int)$id_item.'
');
return ($query['quantity_sold'] * $query['quantity']);
}
2016-01-04 12:49:26 +01:00
public static function record_sort($records, $field, $reverse=false)
{
$hash = array();
2016-01-04 12:49:26 +01:00
foreach($records as $record)
{
$hash[$record[$field]] = $record;
}
2016-01-04 12:49:26 +01:00
($reverse)? krsort($hash) : ksort($hash);
2016-01-04 12:49:26 +01:00
$records = array();
2016-01-04 12:49:26 +01:00
foreach($hash as $record)
{
$records []= $record;
}
2016-01-04 12:49:26 +01:00
return $records;
}
2016-04-05 18:15:35 +02:00
public static function getArrayLines($lines, $by_product_price = false, $id_sale = 0)
2016-01-04 12:49:26 +01:00
{
2016-06-20 11:01:39 +02:00
$arrayTmp = array();
$all_multi = array();
$all_m1 = array();
2016-01-04 12:49:26 +01:00
$g = 0;
2016-04-05 18:15:35 +02:00
2016-04-06 17:32:04 +02:00
if ($id_sale && (int)$id_sale>2) {
2016-04-06 13:09:54 +02:00
2016-04-05 18:15:35 +02:00
$sale = new Sale((int) $id_sale);
2016-04-06 13:09:54 +02:00
$id_products = $sale->getProducts();
$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(',', $id_products).'
-- SELECT `id_product`
-- FROM `'._DB_PREFIX_.'product_ps_cache`
-- WHERE `id_sale` = '.(int) $id_sale.'
)
AND o.`valid` = 1
');
$ids_od = array();
foreach ($ids as $key => $id) {
$ids_od[] = (int)$id['id_order'];
}
2016-06-20 11:01:39 +02:00
//$all_multi = $sale->getMultiForStats($ids_od, 3);
//$all_m2 = $sale->getMultiForStats($ids_od, 2, true);
$all_multi = $sale->getMultiForStats($ids_od, 2);
2016-04-06 13:09:54 +02:00
$all_m1 = $sale->getMultiForStats($ids_od, 1, true);
2016-04-05 18:15:35 +02:00
}
2016-01-04 12:49:26 +01:00
foreach($lines as $line)
{
// if( ($line['product_quantity'] - $line['product_quantity_reinjected']) == 0)
// continue;
if(!isset($arrayTmp[$line['product_id']]))
$arrayTmp[$line['product_id']] = array();
2016-04-05 18:15:35 +02:00
$product_price = MakeStats::getProductPriceWithReduction($line);
if($by_product_price)
{
if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]))
$arrayTmp[$line['product_id']][$line['product_attribute_id']] = array();
if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]))
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price] = array('line' => $line, 'total' => 0);
}
else
{
if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]))
$arrayTmp[$line['product_id']][$line['product_attribute_id']] = array('line' => $line, 'total' => 0);
}
2016-01-04 12:49:26 +01:00
2016-02-09 10:46:07 +01:00
if (isset($line['product_quantity'])) {
$quantity = $line['product_quantity'] - $line['product_quantity_reinjected'];
} else {
if(Pack::isPacked($line['product_id'])){
$quantity = self::getItemPackSoldQuantity($line['product_id']);
} else {
$quantity = 0;
}
2016-02-09 10:46:07 +01:00
}
$stock = (isset($line['attribute_quantity']))?$line['attribute_quantity']:$line['stock'];
2016-01-04 12:49:26 +01:00
2016-04-05 18:15:35 +02:00
if($by_product_price) {
2016-02-09 10:46:07 +01:00
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['total'] += $quantity;
2016-04-05 18:15:35 +02:00
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['stock'] = $stock;
} else {
2016-02-09 10:46:07 +01:00
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['total'] += $quantity;
2016-04-05 18:15:35 +02:00
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['stock'] = $stock;
2016-02-09 10:46:07 +01:00
}
2016-04-05 18:15:35 +02:00
2016-04-06 17:32:04 +02:00
if ($id_sale && (int)$id_sale>2) {
2016-06-20 11:01:39 +02:00
// multi (order contenant au moins 2 ventes differentes)
2016-04-06 13:09:54 +02:00
if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]['multi']) && in_array($line['id_order'], $all_multi)) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['multi'] = $quantity;
} elseif (isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]) && in_array($line['id_order'], $all_multi)) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['multi'] += $quantity;
}
// multi 1 (order contenant uniquement 1 vente)
if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1']) && in_array($line['id_order'], $all_m1)) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1'] = $quantity;
} elseif (isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1']) && in_array($line['id_order'], $all_m1)) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1'] += $quantity;
}
// multi 2 (order contenant uniquement 2 ventes)
2016-06-20 11:01:39 +02:00
/*if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]['m2']) && in_array($line['id_order'], $all_m2)) {
2016-04-06 13:09:54 +02:00
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['m2'] = $quantity;
} elseif (isset($arrayTmp[$line['product_id']][$line['product_attribute_id']]['m2']) && in_array($line['id_order'], $all_m2)) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['m2'] += $quantity;
2016-06-20 11:01:39 +02:00
}*/
2016-04-06 13:09:54 +02:00
// multi 1 + multi 2 (produit dans order contenant uniquement 1 vente et order contenant 2 ventes)
2016-06-20 11:01:39 +02:00
// $arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1_m2'] = $arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1'] + $arrayTmp[$line['product_id']][$line['product_attribute_id']]['m2'];
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['total_m'] = $arrayTmp[$line['product_id']][$line['product_attribute_id']]['m1'] + $arrayTmp[$line['product_id']][$line['product_attribute_id']]['multi'];
2016-04-05 18:15:35 +02:00
}
}
2016-02-09 10:46:07 +01:00
return $lines = $arrayTmp;
2016-01-04 12:49:26 +01:00
}
public static function getOrdersByIdsProduct($ids_products = array(), $order_states = array(), $date_from = null, $date_to = null)
{
2016-02-09 10:46:07 +01:00
$req = 'SELECT od.*, p.name as product_name_base, pd.quantity as stock
2016-01-04 12:49:26 +01:00
FROM '._DB_PREFIX_.'order_detail od
RIGHT JOIN '._DB_PREFIX_.'orders o
2016-01-04 12:49:26 +01:00
ON (
od.id_order = o.id_order
AND (SELECT id_order_state
2016-01-04 12:49:26 +01:00
FROM '._DB_PREFIX_.'order_history oh
WHERE o.id_order = oh.id_order
ORDER BY id_order_history DESC
2016-01-04 12:49:26 +01:00
LIMIT 1)
IN ("'.implode('","', $order_states).'")
'.($date_from !== null && $date_to !== null && Validate::isDate($date_from) && Validate::isDate($date_to) ?
'AND o.date_add >= "'.$date_from.'" AND o.date_add <= "'.$date_to.'"' : '' ).'
)
LEFT OUTER JOIN `'._DB_PREFIX_.'product_lang` p
ON p.id_product = od.product_id
2016-01-04 12:49:26 +01:00
AND p.id_lang = 2
LEFT OUTER JOIN `'._DB_PREFIX_.'product` pd
ON pd.id_product = od.product_id
2016-01-04 12:49:26 +01:00
WHERE product_id IN ("'.implode('","', $ids_products).'")';
2016-01-04 12:49:26 +01:00
$res = Db::getInstance()->ExecuteS($req);
$id_lang = 2;
foreach($res as $k => $r)
{
// override product_name
$res[$k]['product_name'] = $res[$k]['product_name_base'];
$r['product_name'] = $r['product_name_base'];
if (!empty($r['product_attribute_id'])
2016-01-04 12:49:26 +01:00
&& $r['product_attribute_id'] != 0) {
$product_attribute = Db::getInstance()->ExecuteS('
SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, pa.`unit_price_impact`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)($id_lang).')
WHERE pa.`id_product` = '.(int)($r['product_id']).'
AND pa.`id_product_attribute` = '.(int)($r['product_attribute_id']).'
ORDER BY pa.`id_product_attribute`
');
foreach ($product_attribute as $key => $attribute) {
if ($key != 0) {
$r['product_name'].=',';
} else {
$r['product_name'].=' -';
}
$r['product_name'] .= ' '.$attribute['group_name'] .' : '. $attribute['attribute_name'];
2016-02-09 10:46:07 +01:00
$res[$k]['attribute_quantity'] = $attribute['quantity'];
}
}
2016-01-04 12:49:26 +01:00
$res[$k]['product_attribute_name_base'] = '';
if(!empty($r['product_name_base']))
{
$res[$k]['product_attribute_name_base'] = str_replace($r['product_name_base'].' - ', '', $r['product_name']);
if($res[$k]['product_attribute_name_base'] == $r['product_name_base'])
$res[$k]['product_attribute_name_base'] = '';
}
}
return $res;
}
2016-02-09 10:46:07 +01:00
// Adding - fev 2016 (récupération de Tous les produits de la vente et pas seulement ceux vendus)
public static function getDetailsByIdsProduct($ids_products = array(), $order_states = array(), $date_from = null, $date_to = null)
{
$req = 'SELECT
2016-02-09 10:46:07 +01:00
od.*,
p.name as product_name_base,
pd.quantity as stock
FROM '._DB_PREFIX_.'order_detail od
RIGHT JOIN '._DB_PREFIX_.'orders o
2016-02-09 10:46:07 +01:00
ON (
od.id_order = o.id_order
AND (SELECT id_order_state
2016-02-09 10:46:07 +01:00
FROM '._DB_PREFIX_.'order_history oh
WHERE o.id_order = oh.id_order
ORDER BY id_order_history DESC
2016-02-09 10:46:07 +01:00
LIMIT 1)
IN ("'.implode('","', $order_states).'")
'.($date_from !== null && $date_to !== null && Validate::isDate($date_from) && Validate::isDate($date_to) ?
'AND o.date_add >= "'.$date_from.'" AND o.date_add <= "'.$date_to.'"' : '' ).'
)
LEFT OUTER JOIN `'._DB_PREFIX_.'product_lang` p
ON p.id_product = od.product_id
2016-02-09 10:46:07 +01:00
AND p.id_lang = 2
LEFT OUTER JOIN `'._DB_PREFIX_.'product` pd
ON pd.id_product = od.product_id
2016-02-09 10:46:07 +01:00
WHERE product_id IN ("'.implode('","', $ids_products).'")';
$res = Db::getInstance()->ExecuteS($req);
$req2 = 'SELECT
2016-02-09 10:46:07 +01:00
p.id_product as product_id, p.price as product_price, p.price as product_price, p.ean13 as product_ean13, p.reference as product_reference, p.supplier_reference as product_supplier_reference,
p.weight as product_weight, pl.name as product_name_base,
p.quantity as stock,
pa.id_product_attribute as product_attribute_id
FROM '._DB_PREFIX_.'product p
LEFT OUTER JOIN `'._DB_PREFIX_.'product_lang` pl
ON pl.id_product = p.id_product
2016-02-09 10:46:07 +01:00
AND pl.id_lang = 2
LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product` = p.`id_product`)
2016-07-12 16:23:23 +02:00
WHERE p.id_product IN ("'.implode('","', $ids_products).'")';
2016-02-09 10:46:07 +01:00
$res2 = Db::getInstance()->ExecuteS($req2);
2016-07-12 17:59:18 +02:00
$ids_in_order = array();
foreach($res as $k => $r) {
2016-07-12 17:59:18 +02:00
$ids_in_order[$res[$k]['product_id']] = $res[$k]['product_attribute_id'];
}
2016-07-12 16:23:23 +02:00
foreach ($res2 as $k => $value) {
2016-07-12 17:59:18 +02:00
if(isset($value['product_attribute_id']) && in_array($value['product_attribute_id'], $ids_in_order)) {
2016-07-12 16:23:23 +02:00
unset($res2[$k]);
} elseif (!isset($value['product_attribute_id'])
2016-07-12 17:59:18 +02:00
&& array_key_exists($value['product_id'], $ids_in_order)
&& $ids_in_order[$value['product_id']] == 0
2016-07-12 16:59:11 +02:00
){
unset($res2[$k]);
2016-07-12 16:23:23 +02:00
}
}
2016-02-09 10:46:07 +01:00
$res = array_merge($res,$res2);
$id_lang = 2;
foreach($res as $k => $r)
{
2016-02-09 10:46:07 +01:00
// override product_name
$res[$k]['product_name'] = $res[$k]['product_name_base'];
$r['product_name'] = $r['product_name_base'];
if (!empty($r['product_attribute_id'])
2016-02-09 10:46:07 +01:00
&& $r['product_attribute_id'] != 0) {
$product_attribute = Db::getInstance()->ExecuteS('
SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, pa.`unit_price_impact`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)($id_lang).')
WHERE pa.`id_product` = '.(int)($r['product_id']).'
AND pa.`id_product_attribute` = '.(int)($r['product_attribute_id']).'
ORDER BY pa.`id_product_attribute`
');
foreach ($product_attribute as $key => $attribute) {
if ($key != 0) {
$r['product_name'].=',';
} else {
$r['product_name'].=' -';
}
$r['product_name'] .= ' '.$attribute['group_name'] .' : '. $attribute['attribute_name'];
$res[$k]['attribute_quantity'] = $attribute['quantity'];
2016-07-12 16:23:23 +02:00
$res[$k]['product_ean13'] = $attribute['ean13'];
2016-02-09 10:46:07 +01:00
}
}
$res[$k]['product_attribute_name_base'] = '';
if(!empty($r['product_name_base']))
{
$res[$k]['product_attribute_name_base'] = str_replace($r['product_name_base'].' - ', '', $r['product_name']);
if($res[$k]['product_attribute_name_base'] == $r['product_name_base'])
$res[$k]['product_attribute_name_base'] = '';
}
}
return $res;
}
2016-01-04 12:49:26 +01:00
}