Fix : virtual stock was not correctly calculated because reception was not included.

This commit is contained in:
Maxime Kohlhaas 2014-07-03 10:24:29 +02:00
parent c1d33af50d
commit 959c41e8db
2 changed files with 85 additions and 19 deletions

View File

@ -1709,6 +1709,48 @@ class Product extends CommonObject
}
}
/**
* Charge tableau des stats réception fournisseur pour le produit/service
*
* @param int $socid Id societe pour filtrer sur une societe
* @param int $filtrestatut Id statut pour filtrer sur un statut
* @return array Tableau des stats
*/
function load_stats_reception($socid=0,$filtrestatut='')
{
global $conf,$user;
$sql = "SELECT COUNT(DISTINCT cf.fk_soc) as nb_customers, COUNT(DISTINCT cf.rowid) as nb,";
$sql.= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as fd";
$sql.= ", ".MAIN_DB_PREFIX."commande_fournisseur as cf";
$sql.= ", ".MAIN_DB_PREFIX."societe as s";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE cf.rowid = fd.fk_commande";
$sql.= " AND cf.fk_soc = s.rowid";
$sql.= " AND cf.entity = ".$conf->entity;
$sql.= " AND fd.fk_product = ".$this->id;
if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
if ($socid > 0) $sql.= " AND cf.fk_soc = ".$socid;
if ($filtrestatut <> '') $sql.= " AND cf.fk_statut in (".$filtrestatut.")";
$result = $this->db->query($sql);
if ( $result )
{
$obj=$this->db->fetch_object($result);
$this->stats_reception['suppliers']=$obj->nb_customers;
$this->stats_reception['nb']=$obj->nb;
$this->stats_reception['rows']=$obj->nb_rows;
$this->stats_reception['qty']=$obj->qty?$obj->qty:0;
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
/**
* Charge tableau des stats contrat pour le produit/service
*
@ -2872,6 +2914,7 @@ class Product extends CommonObject
}
}
$this->db->free($result);
$this->load_virtual_stock();
return 1;
}
else
@ -2881,6 +2924,46 @@ class Product extends CommonObject
}
}
/**
* Load information about virtual stock of a product
*
* @return int < 0 if KO, > 0 if OK
*/
function load_virtual_stock()
{
global $conf;
if (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT))
{
$stock_commande_client=$stock_commande_fournisseur=0;
$stock_sending_client=$stock_reception_fournisseur=0;
if (! empty($conf->commande->enabled))
{
$result=$this->load_stats_commande(0,'1,2');
if ($result < 0) dol_print_error($db,$this->error);
$stock_commande_client=$this->stats_commande['qty'];
}
if (! empty($conf->expedition->enabled))
{
$result=$this->load_stats_sending(0,'');
if ($result < 0) dol_print_error($db,$this->error);
$stock_sending_client=$this->stats_expedition['qty'];
}
if (! empty($conf->fournisseur->enabled))
{
$result=$this->load_stats_commande_fournisseur(0,'3');
if ($result < 0) dol_print_error($db,$this->error);
$stock_commande_fournisseur=$this->stats_commande_fournisseur['qty'];
$result=$this->load_stats_reception(0,'');
if ($result < 0) dol_print_error($db,$this->error);
$stock_reception_fournisseur=$this->stats_reception['qty'];
}
$this->stock_theorique=$this->stock_reel-($stock_commande_client-$stock_sending_client)+($stock_commande_fournisseur-$stock_reception_fournisseur);
}
}
/**
* Deplace fichier uploade sous le nom $files dans le repertoire sdir

View File

@ -321,23 +321,6 @@ if ($id > 0 || $ref)
// If stock if stock increment is done on real sending
if (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT))
{
$stock_commande_client=$stock_commande_fournisseur=0;
if (! empty($conf->commande->enabled))
{
$result=$product->load_stats_commande(0,'1,2');
if ($result < 0) dol_print_error($db,$product->error);
$stock_commande_client=$product->stats_commande['qty'];
}
if (! empty($conf->fournisseur->enabled))
{
$result=$product->load_stats_commande_fournisseur(0,'3');
if ($result < 0) dol_print_error($db,$product->error);
$stock_commande_fournisseur=$product->stats_commande_fournisseur['qty'];
}
$product->stock_theorique=$product->stock_reel-($stock_commande_client+$stock_sending_client)+$stock_commande_fournisseur;
// Stock theorique
print '<tr><td>'.$langs->trans("VirtualStock").'</td>';
print "<td>".$product->stock_theorique;
@ -360,7 +343,7 @@ if ($id > 0 || $ref)
if (! empty($conf->commande->enabled))
{
if ($found) print '<br>'; else $found=1;
print $langs->trans("CustomersOrdersRunning").': '.($stock_commande_client+$stock_sending_client);
print $langs->trans("CustomersOrdersRunning").': '.($product->stats_commande['qty']-$product->stats_sendings['qty']);
$result=$product->load_stats_commande(0,'0');
if ($result < 0) dol_print_error($db,$product->error);
print ' ('.$langs->trans("Draft").': '.$product->stats_commande['qty'].')';
@ -372,7 +355,7 @@ if ($id > 0 || $ref)
if (! empty($conf->fournisseur->enabled))
{
if ($found) print '<br>'; else $found=1;
print $langs->trans("SuppliersOrdersRunning").': '.$stock_commande_fournisseur;
print $langs->trans("SuppliersOrdersRunning").': '.($product->stats_commande_fournisseur['qty']-$product->stats_reception['qty']);
$result=$product->load_stats_commande_fournisseur(0,'0,1,2');
if ($result < 0) dol_print_error($db,$product->error);
print ' ('.$langs->trans("DraftOrWaitingApproved").': '.$product->stats_commande_fournisseur['qty'].')';