438 lines
19 KiB
PHP
438 lines
19 KiB
PHP
|
<?php
|
||
|
|
||
|
/*
|
||
|
* To change this template, choose Tools | Templates
|
||
|
* and open the template in the editor.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Description of Inventory
|
||
|
*
|
||
|
* @author Pierre
|
||
|
*/
|
||
|
|
||
|
class Inventory{
|
||
|
|
||
|
public function getCategoryFormOption($id_category = NULL){
|
||
|
global $cookie;
|
||
|
$option = "";
|
||
|
|
||
|
$categories = CategoryCore::getCategories($cookie->id_lang,FALSE);
|
||
|
krsort($categories[1]);
|
||
|
$option .= "<option>Sélectionner une vente</option>";
|
||
|
foreach($categories[1] as $categorie){
|
||
|
$select = "";
|
||
|
if($id_category == $categorie['infos']['id_category']){
|
||
|
$select = "selected='selected'";
|
||
|
}
|
||
|
$option .= "<option ".$select." value='".$categorie['infos']['id_category']."'>".$categorie['infos']['name']." - ".$categorie['infos']['id_category']."</option>";
|
||
|
}
|
||
|
return $option;
|
||
|
}
|
||
|
public function getCategoryChildren($id_category = NULL,$test = true){
|
||
|
global $cookie;
|
||
|
$html = "";
|
||
|
if(!$id_category){
|
||
|
$categoriesFirst = CategoryCore::getCategories($cookie->id_lang,FALSE);
|
||
|
$i = 0;
|
||
|
foreach($categoriesFirst[1] as $categorie){
|
||
|
if( $i == 0){
|
||
|
$id_category= $categorie['infos']['id_category'];
|
||
|
|
||
|
}
|
||
|
$i++;
|
||
|
}
|
||
|
}
|
||
|
$categories = CategoryCore::getChildren($id_category, $cookie->id_lang,FALSE);
|
||
|
if($test){
|
||
|
$html .= " <a class='open right' href='../modules/inventorytracking/export.php?all=1&id_sale=".$id_category."' target='_blank'/>Exporter</a> <br/><br/>";
|
||
|
}
|
||
|
if($categories){
|
||
|
$i = 0;
|
||
|
foreach($categories as $categorie){
|
||
|
if( $i == 0){
|
||
|
$display = " show";
|
||
|
$plus =" moins";
|
||
|
}else{
|
||
|
$display = " hide";
|
||
|
$plus =" plus";
|
||
|
}
|
||
|
$products = self::getProductsByCat($categorie['id_category']);
|
||
|
if($products){
|
||
|
$y = 0;
|
||
|
$html .= "<div class='ChildHead".$plus."' id='".$categorie['id_category']."' name='children' ><b>".$categorie['name']."</b></div>";
|
||
|
$html .= "<div class='ChildInfo".$display."' id='info_".$categorie['id_category']."' >";
|
||
|
$html .= self::MvtsAllProducts($products)."<br/>";
|
||
|
$html .= "<input class='open' type='button' value='Ouvrir' name='all' id='".$categorie['id_category']."' />
|
||
|
<a class='open right' href='../modules/inventorytracking/export.php?all=0&id_sale=".$id_category."&id_cat=".$categorie['id_category']."' target='_blank'/>Exporter</a>
|
||
|
<br/><br/>";
|
||
|
foreach ($products as $value) {
|
||
|
if( $y == 0 && $i == 0){
|
||
|
$display = " show";
|
||
|
$plus =" moins";
|
||
|
$html .= '<script>
|
||
|
jQuery(function () {
|
||
|
$(document).ready(function() {
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "../modules/inventorytracking/getProductAjax.php",
|
||
|
data: { id_product : "'.$value['id_product'].'" },
|
||
|
success: function(data) {
|
||
|
$("#info_products_'.$value['id_product'].'").html(data);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
</script>';
|
||
|
}else{
|
||
|
$display = " hide";
|
||
|
$plus =" plus";
|
||
|
}
|
||
|
$featuresValue = "";
|
||
|
if($value['features']){
|
||
|
foreach($value['features'] as $feature){
|
||
|
if($feature['name'] == "COULEUR" ){
|
||
|
$featuresValue = $feature['value'];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$html .= "<div category='".$categorie['id_category']."' class='ChildHead".$plus."' id='".$value['id_product']."' name='products' >".$value['reference']." - ".$value['name']." - Couleur : ".$featuresValue." - Ref/Frn : ".$value['supplier_reference']."</div>";
|
||
|
$html .= "<div class='ChildInfo".$display."' name='info_products_".$categorie['id_category']."' id='info_products_".$value['id_product']."' ></div>";
|
||
|
$y++;
|
||
|
}
|
||
|
$html .= "</div>";
|
||
|
$i++;
|
||
|
}else{
|
||
|
$html .= "<div class='ChildHead".$plus."' id='".$categorie['id_category']."' name='children' ><b>".$categorie['name']."</b></div>";
|
||
|
$html .= "<div class='ChildInfo".$display."' id='info_".$categorie['id_category']."' >";
|
||
|
$html .= Inventory::getCategoryChildren($categorie['id_category'],FALSE);
|
||
|
$html .= "</div>";
|
||
|
}
|
||
|
}
|
||
|
}else{
|
||
|
$html .= "";
|
||
|
}
|
||
|
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
public function getProductsByCat($id_cat = NULL){
|
||
|
global $cookie;
|
||
|
$category = new CategoryCore($id_cat);
|
||
|
$products = $category->getProducts( $cookie->id_lang , "1", "1000000000000000000", "id_product",NULL , NULL, FALSE , FALSE );
|
||
|
return $products;
|
||
|
}
|
||
|
|
||
|
public function getProductInfo($id_product = NULL){
|
||
|
global $cookie;
|
||
|
$html = "" ;
|
||
|
$product = new ProductCore($id_product);
|
||
|
$features = $product->getFrontFeatures($cookie->id_lang);
|
||
|
|
||
|
$id_image = $product->getCoverWs();
|
||
|
if($id_image){
|
||
|
$image = new ImageCore($id_image);
|
||
|
$html .= "<div class='InfoLeft'><a href='"._PS_IMG_."p/".$image->getImgFolder().$id_image.".jpg' target='_blank' ><img class='imgm' alt='' src='"._PS_IMG_."p/".$image->getImgFolder().$id_image."-medium.jpg'></a></div>";
|
||
|
}
|
||
|
$html .="<div class='InfoRight'><b>Désignation</b> : ".$product->name[$cookie->id_lang]."<br/>
|
||
|
<b>Ref</b> : ".$product->reference."<br/>
|
||
|
<b>Ref/Frn</b> : ".$product->supplier_reference."<br/>";
|
||
|
if(isset($features)){
|
||
|
foreach($features as $feature){
|
||
|
$html .= "<b>".ucfirst(strtolower($feature['name']))."</b> : ".$feature['value']."<br/>";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
$html .= "
|
||
|
</div>";
|
||
|
$html .= "";
|
||
|
$attributes = $product->getAttributesGroups($cookie->id_lang);
|
||
|
if($attributes){
|
||
|
$html .= self::TabAttributes($attributes,$product);
|
||
|
}else{
|
||
|
$html .= self::TabNoAttributes($product);
|
||
|
}
|
||
|
|
||
|
|
||
|
$html .= "<div class='clear'></div>";
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
public function TabAttributes($attributes,$product){
|
||
|
global $cookie;
|
||
|
|
||
|
$html = "<table class='table'>
|
||
|
<tr>
|
||
|
<th>Déclinaisons</th>
|
||
|
<th>Stock total</th>
|
||
|
<th>Ventes totales</th>
|
||
|
<th>CA</th>
|
||
|
<th>Stock</th>
|
||
|
<th>Vendues</th>
|
||
|
<th>Restantes</th>
|
||
|
</tr>";
|
||
|
$totalRestant = 0;
|
||
|
$totalQuantiteInitial = 0;
|
||
|
$totalQuantiteTotalStock = 0;
|
||
|
$totalQuantiteVendu = 0;
|
||
|
$totalQuantiteTotalVendu = 0;
|
||
|
$totalCA = 0;
|
||
|
foreach($attributes as $attribute){
|
||
|
$mvts = self::getStockMouvements($cookie->id_lang,$product->id,$attribute['id_product_attribute']);
|
||
|
$i=0;
|
||
|
$quantiteInitial = 0;
|
||
|
$quantiteTotalStock = 0;
|
||
|
$quantiteTotalVendu = 0;
|
||
|
$quantiteVendu = 0;
|
||
|
$CA = 0;
|
||
|
foreach($mvts as $mvt){
|
||
|
if($mvt['reason'] == "Augmenter"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
$quantiteTotalStock += $mvt['quantity'];
|
||
|
$quantiteVendu = 0;
|
||
|
}
|
||
|
if($mvt['reason'] == "Commande"){
|
||
|
$quantiteInitial -= $mvt['quantity'];
|
||
|
$quantiteVendu -= $mvt['quantity'];
|
||
|
$quantiteTotalVendu -= $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
if($mvt['reason'] == "Diminuer"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
$quantiteTotalStock = $quantiteVendu + $attribute['quantity'];
|
||
|
|
||
|
$CA = $quantiteTotalVendu * $product->getPrice(true, null, 2);
|
||
|
$html .= "
|
||
|
<tr>
|
||
|
<td>".$attribute['group_name']." : ".$attribute['attribute_name']."</td>
|
||
|
<td class='number'>".$quantiteInitial."</td>
|
||
|
<td class='number'>".$quantiteTotalVendu."</td>
|
||
|
<td class='number'>".$CA." €</td>
|
||
|
<td class='number'>".$quantiteTotalStock."</td>
|
||
|
<td class='number'>".$quantiteVendu."</td>
|
||
|
<td class='number'>".$attribute['quantity']."</td>
|
||
|
</tr>";
|
||
|
$totalRestant += $attribute['quantity'];
|
||
|
$totalQuantiteInitial += $quantiteInitial;
|
||
|
$totalQuantiteTotalStock += $quantiteTotalStock;
|
||
|
$totalQuantiteVendu += $quantiteVendu;
|
||
|
$totalQuantiteTotalVendu += $quantiteTotalVendu;
|
||
|
$totalCA += $CA;
|
||
|
}
|
||
|
|
||
|
$html .= " <tfoot>
|
||
|
<tr>
|
||
|
<td>Totaux</td>
|
||
|
<td class='number'>".$totalQuantiteInitial."</td>
|
||
|
<td class='number'>".$totalQuantiteTotalVendu."</td>
|
||
|
<td class='number'>".$totalCA." €</td>
|
||
|
<td class='number'>".$totalQuantiteTotalStock."</td>
|
||
|
<td class='number'>".$totalQuantiteVendu."</td>
|
||
|
<td class='number'>".$totalRestant."</td>
|
||
|
</tr>
|
||
|
</tfoot></table>";
|
||
|
return $html;
|
||
|
}
|
||
|
public function TabNoAttributes($product){
|
||
|
global $cookie;
|
||
|
|
||
|
$html = "<table class='table'>
|
||
|
<tr>
|
||
|
<th>Stock total</th>
|
||
|
<th>Ventes totales</th>
|
||
|
<th>CA</th>
|
||
|
<th>Stock</th>
|
||
|
<th>Vendues</th>
|
||
|
<th>Restantes</th>
|
||
|
</tr>";
|
||
|
$mvts = self::getStockMouvements($cookie->id_lang,$product->id);
|
||
|
$i=0;
|
||
|
$quantiteInitial = 0;
|
||
|
$quantiteTotalStock = 0;
|
||
|
$quantiteTotalVendu = 0;
|
||
|
$quantiteVendu = 0;
|
||
|
$CA = 0;
|
||
|
foreach($mvts as $mvt){
|
||
|
if($mvt['reason'] == "Augmenter"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
$quantiteTotalStock += $mvt['quantity'];
|
||
|
$quantiteVendu = 0;
|
||
|
}
|
||
|
if($mvt['reason'] == "Commande"){
|
||
|
|
||
|
$quantiteVendu -= $mvt['quantity'];
|
||
|
$quantiteTotalVendu -= $mvt['quantity'];
|
||
|
$quantiteInitial -= $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
if($mvt['reason'] == "Diminuer"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
$quantiteTotalStock = $quantiteVendu + $product->quantity;
|
||
|
$totalQuantiteInitial += $quantiteInitial + $product->quantity;
|
||
|
$CA = $quantiteTotalVendu * $product->getPrice(true, null, 2);
|
||
|
$html .= "
|
||
|
<tr>
|
||
|
<td class='number'>".$totalQuantiteInitial."</td>
|
||
|
<td class='number'>".$quantiteTotalVendu."</td>
|
||
|
<td class='number'>".$CA." €</td>
|
||
|
<td class='number'>".$quantiteTotalStock."</td>
|
||
|
<td class='number'>".$quantiteVendu."</td>
|
||
|
<td class='number'>".$product->quantity."</td>
|
||
|
</tr></table>";
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
public function MvtsAllProducts($products){
|
||
|
global $cookie;
|
||
|
|
||
|
$html = "<table class='table'>
|
||
|
<tr>
|
||
|
<th>Stock total</th>
|
||
|
<th>Ventes totales</th>
|
||
|
<th>CA</th>
|
||
|
<th>Stock</th>
|
||
|
<th>Vendues</th>
|
||
|
<th>Restantes</th>
|
||
|
</tr>";
|
||
|
$totalRestant = 0;
|
||
|
$totalQuantiteInitial = 0;
|
||
|
$totalQuantiteTotalStock = 0;
|
||
|
$totalQuantiteVendu = 0;
|
||
|
$totalQuantiteTotalVendu = 0;
|
||
|
$CA = 0;
|
||
|
foreach($products as $product){
|
||
|
|
||
|
$Obj_product = new ProductCore($product['id_product']);
|
||
|
if($Obj_product->hasAttributes()){
|
||
|
$attribute = true;
|
||
|
}else{
|
||
|
$attribute = false;
|
||
|
}
|
||
|
$mvts = self::getStockMouvements($cookie->id_lang,$product['id_product'],NULL,$attribute);
|
||
|
$i=0;
|
||
|
$quantiteInitial = 0;
|
||
|
$quantiteTotalStock = 0;
|
||
|
$quantiteTotalVendu = 0;
|
||
|
$quantiteVendu = 0;
|
||
|
|
||
|
foreach($mvts as $mvt){
|
||
|
if($mvt['reason'] == "Augmenter"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
$quantiteTotalStock += $mvt['quantity'];
|
||
|
$quantiteVendu = 0;
|
||
|
}
|
||
|
if($mvt['reason'] == "Commande"){
|
||
|
$quantiteInitial -= $mvt['quantity'];
|
||
|
$quantiteVendu -= $mvt['quantity'];
|
||
|
$quantiteTotalVendu -= $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
if($mvt['reason'] == "Diminuer"){
|
||
|
$quantiteInitial += $mvt['quantity'];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
$totalRestant += $Obj_product->getQuantity($product['id_product']);
|
||
|
$totalQuantiteInitial += $quantiteInitial + $Obj_product->getQuantity($product['id_product']);
|
||
|
$totalQuantiteVendu += $quantiteVendu;
|
||
|
$totalQuantiteTotalVendu += $quantiteTotalVendu;
|
||
|
$CA += $quantiteTotalVendu * $Obj_product->getPrice(true, null, 2);
|
||
|
}
|
||
|
|
||
|
$totalQuantiteTotalStock = $totalQuantiteVendu + $totalRestant;
|
||
|
|
||
|
|
||
|
$html .= " <tr>
|
||
|
<td class='number'>".$totalQuantiteInitial."</td>
|
||
|
<td class='number'>".$totalQuantiteTotalVendu."</td>
|
||
|
<td class='number'>".$CA." €</td>
|
||
|
<td class='number'>".$totalQuantiteTotalStock."</td>
|
||
|
<td class='number'>".$totalQuantiteVendu."</td>
|
||
|
<td class='number'>".$totalRestant."</td>
|
||
|
</tr>
|
||
|
</table>";
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
public function calculStockMvt($mvts, $product)
|
||
|
{
|
||
|
$i=0;
|
||
|
$quantiteInitial = 0;
|
||
|
$quantiteTotalStock = 0;
|
||
|
$quantiteTotalVendu = 0;
|
||
|
$quantiteVendu = 0;
|
||
|
|
||
|
foreach($mvts as $mvt){
|
||
|
|
||
|
if($mvt['reason'] == "Commande"){
|
||
|
$quantiteInitial -= $mvt['quantity'];
|
||
|
$quantiteVendu -= $mvt['quantity'];
|
||
|
$quantiteTotalVendu -= $mvt['quantity'];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$array = array();
|
||
|
$array['restant'] = Product::getQuantity($product->id);
|
||
|
$array['quantiteInitial'] = $quantiteInitial;
|
||
|
$array['quantiteVendu'] = $quantiteVendu;
|
||
|
$array['quantiteTotalVendu'] = $quantiteTotalVendu;
|
||
|
return $array;
|
||
|
}
|
||
|
|
||
|
public function getStockMouvements($id_lang, $id_product, $id_attribute = NULL , $all = false, $date_from = null, $date_to = null) {
|
||
|
if($id_attribute){
|
||
|
$attr = ' sm.id_product_attribute='.(int)$id_attribute;
|
||
|
}else{
|
||
|
$attr = ' sm.id_product='.(int)$id_product;
|
||
|
}
|
||
|
$form = "";
|
||
|
if($all){
|
||
|
$attr .= ' AND pa.id_product_attribute = sm.id_product_attribute';
|
||
|
$form .= ", `"._DB_PREFIX_."product_attribute` pa ";
|
||
|
}
|
||
|
if($date_from !== null && $date_to !== null && Validate::isDate($date_from) && Validate::isDate($date_to))
|
||
|
$attr .= ' AND sm.date_add >= "'.$date_from.'" AND sm.date_add <= "'.$date_to.'"';
|
||
|
|
||
|
$sql = '
|
||
|
SELECT sm.id_product ,sm.id_product_attribute , sm.date_add, sm.id_stock_mvt, sm.date_add, sm.quantity, sm.id_order, CONCAT(pl.name, \' \', GROUP_CONCAT(IFNULL(al.name, \'\'), \'\')) product_name, pl.name as product_name_simple, CONCAT(e.lastname, \' \', e.firstname) employee, mrl.name reason FROM `'._DB_PREFIX_.'stock_mvt` sm
|
||
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (sm.id_product = pl.id_product AND pl.id_lang = '.(int)$id_lang.')
|
||
|
LEFT JOIN `'._DB_PREFIX_.'stock_mvt_reason_lang` mrl ON (sm.id_stock_mvt_reason = mrl.id_stock_mvt_reason AND mrl.id_lang = '.(int)$id_lang.')
|
||
|
LEFT JOIN `'._DB_PREFIX_.'employee` e ON (e.id_employee = sm.id_employee) LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.id_product_attribute = sm.id_product_attribute) LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (al.id_attribute = pac.id_attribute AND al.id_lang = '.(int)$id_lang.')'.$form.' WHERE '.$attr.' GROUP BY sm.id_stock_mvt' ;
|
||
|
|
||
|
return Db::getInstance()->ExecuteS($sql);
|
||
|
}
|
||
|
|
||
|
public function getOrdersByIdsProduct($ids_products = array(), $order_states = array(), $date_from = null, $date_to = null)
|
||
|
{
|
||
|
$req = 'SELECT od.*
|
||
|
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.'"' : '' ).'
|
||
|
)
|
||
|
WHERE product_id IN ("'.implode('","', $ids_products).'")';
|
||
|
|
||
|
$res = Db::getInstance()->ExecuteS($req);
|
||
|
return $res;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
?>
|