191 lines
6.6 KiB
PHP
Executable File
191 lines
6.6 KiB
PHP
Executable File
<?php
|
|
class MakeStats {
|
|
|
|
public static function getProductsByCat($id_cat = NULL)
|
|
{
|
|
global $cookie;
|
|
$category = new Category($id_cat);
|
|
$products = $category->getProductsStats( $cookie->id_lang , "1", "1000000000000000000", "reference",NULL , NULL, FALSE , FALSE );
|
|
|
|
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--) {
|
|
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)
|
|
{
|
|
$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 record_sort($records, $field, $reverse=false)
|
|
{
|
|
$hash = array();
|
|
|
|
foreach($records as $record)
|
|
{
|
|
$hash[$record[$field]] = $record;
|
|
}
|
|
|
|
($reverse)? krsort($hash) : ksort($hash);
|
|
|
|
$records = array();
|
|
|
|
foreach($hash as $record)
|
|
{
|
|
$records []= $record;
|
|
}
|
|
|
|
return $records;
|
|
}
|
|
|
|
public static function getArrayLines($lines, $by_product_price = false)
|
|
{
|
|
$arrayTmp = array();
|
|
$g = 0;
|
|
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();
|
|
|
|
$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);
|
|
}
|
|
|
|
|
|
$quantity = $line['product_quantity'] - $line['product_quantity_reinjected'];
|
|
|
|
if($by_product_price)
|
|
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['total'] += $quantity;
|
|
else
|
|
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['total'] += $quantity;
|
|
}
|
|
|
|
return $lines = $arrayTmp;
|
|
}
|
|
|
|
public static function getOrdersByIdsProduct($ids_products = array(), $order_states = array(), $date_from = null, $date_to = null)
|
|
{
|
|
$req = 'SELECT od.*, p.name as product_name_base
|
|
FROM '._DB_PREFIX_.'order_detail od
|
|
RIGHT JOIN '._DB_PREFIX_.'orders o
|
|
ON (
|
|
od.id_order = o.id_order
|
|
AND (SELECT id_order_state
|
|
FROM '._DB_PREFIX_.'order_history oh
|
|
WHERE o.id_order = oh.id_order
|
|
ORDER BY id_order_history DESC
|
|
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
|
|
AND p.id_lang = 2
|
|
WHERE product_id IN ("'.implode('","', $ids_products).'")';
|
|
|
|
$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'])
|
|
&& $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]['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;
|
|
}
|
|
} |