diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 0271e0c0d2c..f17a128544a 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3005,7 +3005,7 @@ class Product extends CommonObject * * @param int $socid Id societe pour filtrer sur une societe * @param string $filtrestatut Id statut pour filtrer sur un statut - * @param int $forVirtualStock Ignore rights filter for virtual stock calculation. + * @param int $forVirtualStock Ignore rights filter for virtual stock calculation. Set when load_stats_commande is used for virtual stock calculation. * @return integer Array of stats in $this->stats_commande (nb=nb of order, qty=qty ordered), <0 if ko or >0 if ok */ public function load_stats_commande($socid = 0, $filtrestatut = '', $forVirtualStock = 0) @@ -3065,10 +3065,10 @@ class Product extends CommonObject } // If stock decrease is on invoice validation, the theorical stock continue to - // count the orders to ship in theorical stock when some are already removed b invoice validation. - // If option DECREASE_ONLY_UNINVOICEDPRODUCTS is on, we make a compensation. - if (!empty($conf->global->STOCK_CALCULATE_ON_BILL)) { + // count the orders to ship in theorical stock when some are already removed by invoice validation. + if ($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) { if (!empty($conf->global->DECREASE_ONLY_UNINVOICEDPRODUCTS)) { + // If option DECREASE_ONLY_UNINVOICEDPRODUCTS is on, we make a compensation but only if order not yet invoice. $adeduire = 0; $sql = "SELECT sum(fd.qty) as count FROM ".$this->db->prefix()."facturedet as fd "; $sql .= " JOIN ".$this->db->prefix()."facture as f ON fd.fk_facture = f.rowid "; @@ -3086,6 +3086,7 @@ class Product extends CommonObject $this->stats_commande['qty'] -= $adeduire; } else { + // If option DECREASE_ONLY_UNINVOICEDPRODUCTS is off, we make a compensation with lines of invoices linked to the order include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; // For every order having invoice already validated we need to decrease stock cause it's in physical stock