From 959c41e8db19c8577233042c9b2617630fc68f2d Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Thu, 3 Jul 2014 10:24:29 +0200 Subject: [PATCH] Fix : virtual stock was not correctly calculated because reception was not included. --- htdocs/product/class/product.class.php | 83 ++++++++++++++++++++++++++ htdocs/product/stock/product.php | 21 +------ 2 files changed, 85 insertions(+), 19 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index cee5c72abea..61b885f02c7 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -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 diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index 3f26d8c0980..338169a6154 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -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 ''.$langs->trans("VirtualStock").''; print "".$product->stock_theorique; @@ -360,7 +343,7 @@ if ($id > 0 || $ref) if (! empty($conf->commande->enabled)) { if ($found) print '
'; 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 '
'; 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'].')';