Marion Muszynski 4acaacaaa8 other modif
2016-07-12 17:59:18 +02:00

357 lines
16 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 , FALSE, 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, $id_sale = 0)
{
$arrayTmp = array();
$all_multi = array();
$all_m1 = array();
$g = 0;
if ($id_sale && (int)$id_sale>2) {
$sale = new Sale((int) $id_sale);
$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'];
}
//$all_multi = $sale->getMultiForStats($ids_od, 3);
//$all_m2 = $sale->getMultiForStats($ids_od, 2, true);
$all_multi = $sale->getMultiForStats($ids_od, 2);
$all_m1 = $sale->getMultiForStats($ids_od, 1, true);
}
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);
}
if (isset($line['product_quantity'])) {
$quantity = $line['product_quantity'] - $line['product_quantity_reinjected'];
} else {
$quantity = 0;
}
$stock = (isset($line['attribute_quantity']))?$line['attribute_quantity']:$line['stock'];
if($by_product_price) {
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['total'] += $quantity;
$arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['stock'] = $stock;
} else {
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['total'] += $quantity;
$arrayTmp[$line['product_id']][$line['product_attribute_id']]['stock'] = $stock;
}
if ($id_sale && (int)$id_sale>2) {
// multi (order contenant au moins 2 ventes differentes)
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)
/*if(!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;
} 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;
}*/
// multi 1 + multi 2 (produit dans order contenant uniquement 1 vente et order contenant 2 ventes)
// $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'];
}
}
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, pd.quantity as stock
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
LEFT OUTER JOIN `'._DB_PREFIX_.'product` pd
ON pd.id_product = od.product_id
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]['attribute_quantity'] = $attribute['quantity'];
}
}
$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;
}
// 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
od.*,
p.name as product_name_base,
pd.quantity as stock
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
LEFT OUTER JOIN `'._DB_PREFIX_.'product` pd
ON pd.id_product = od.product_id
WHERE product_id IN ("'.implode('","', $ids_products).'")';
$res = Db::getInstance()->ExecuteS($req);
$req2 = 'SELECT
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
AND pl.id_lang = 2
LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product` = p.`id_product`)
WHERE p.id_product IN ("'.implode('","', $ids_products).'")';
$res2 = Db::getInstance()->ExecuteS($req2);
$ids_in_order = array();
foreach($res as $k => $r) {
$ids_in_order[$res[$k]['product_id']] = $res[$k]['product_attribute_id'];
}
foreach ($res2 as $k => $value) {
if(isset($value['product_attribute_id']) && in_array($value['product_attribute_id'], $ids_in_order)) {
unset($res2[$k]);
} elseif (!isset($value['product_attribute_id'])
&& array_key_exists($value['product_id'], $ids_in_order)
&& $ids_in_order[$value['product_id']] == 0
){
unset($res2[$k]);
}
}
$res = array_merge($res,$res2);
$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]['attribute_quantity'] = $attribute['quantity'];
$res[$k]['product_ean13'] = $attribute['ean13'];
}
}
$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;
}
}