From 1833add141e439011e56691b6f038907ba7adfc3 Mon Sep 17 00:00:00 2001 From: Quentin VIAL-GOUTEYRON Date: Thu, 8 Sep 2022 10:12:40 +0200 Subject: [PATCH 1/8] FIX theorical stock when stock calculate on bill --- htdocs/product/class/product.class.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 0900f11ed29..bb04f6e9359 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -2990,7 +2990,6 @@ class Product extends CommonObject $sql .= " JOIN ".MAIN_DB_PREFIX."commande c ON el.fk_source = c.rowid "; $sql .= " WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).") AND c.facture = 0 AND fd.fk_product = ".((int) $this->id); dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); - $resql = $this->db->query($sql); if ($resql) { if ($this->db->num_rows($resql) > 0) { @@ -2999,6 +2998,24 @@ class Product extends CommonObject } } + $this->stats_commande['qty'] -= $adeduire; + } else { + //For every order having invoice already validated we need to decrease stock cause it's in physical stock + $adeduire = 0; + $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet fd '; + $sql .= ' JOIN '.MAIN_DB_PREFIX.'facture f ON fd.fk_facture = f.rowid '; + $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element el ON el.fk_target = f.rowid and el.targettype = 'facture' and sourcetype = 'commande'"; + $sql .= ' JOIN '.MAIN_DB_PREFIX.'commande c ON el.fk_source = c.rowid '; + $sql .= ' WHERE c.fk_statut IN ('.$this->db->sanitize($filtrestatut).') AND f.fk_statut > '.Facture::STATUS_DRAFT.' AND fd.fk_product = '.((int) $this->id); + dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); + $resql = $this->db->query($sql); + if($resql) { + if($this->db->num_rows($resql) > 0) { + $obj = $this->db->fetch_object($resql); + $adeduire += $obj->count; + } + } + $this->stats_commande['qty'] -= $adeduire; } } From c4793fdb06a6f59f85defe372621c8c270df999a Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 8 Sep 2022 08:32:08 +0000 Subject: [PATCH 2/8] Fixing style errors. --- htdocs/product/class/product.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index bb04f6e9359..5a1385deb6f 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3009,8 +3009,8 @@ class Product extends CommonObject $sql .= ' WHERE c.fk_statut IN ('.$this->db->sanitize($filtrestatut).') AND f.fk_statut > '.Facture::STATUS_DRAFT.' AND fd.fk_product = '.((int) $this->id); dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); $resql = $this->db->query($sql); - if($resql) { - if($this->db->num_rows($resql) > 0) { + if ($resql) { + if ($this->db->num_rows($resql) > 0) { $obj = $this->db->fetch_object($resql); $adeduire += $obj->count; } From 4fcfe966903ad783639492bc2c0039e2c7553ef9 Mon Sep 17 00:00:00 2001 From: Quentin VIAL-GOUTEYRON Date: Wed, 14 Sep 2022 09:45:18 +0200 Subject: [PATCH 3/8] fix two sides --- htdocs/product/class/product.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index bb04f6e9359..670b4548228 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3004,7 +3004,7 @@ class Product extends CommonObject $adeduire = 0; $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet fd '; $sql .= ' JOIN '.MAIN_DB_PREFIX.'facture f ON fd.fk_facture = f.rowid '; - $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element el ON el.fk_target = f.rowid and el.targettype = 'facture' and sourcetype = 'commande'"; + $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; $sql .= ' JOIN '.MAIN_DB_PREFIX.'commande c ON el.fk_source = c.rowid '; $sql .= ' WHERE c.fk_statut IN ('.$this->db->sanitize($filtrestatut).') AND f.fk_statut > '.Facture::STATUS_DRAFT.' AND fd.fk_product = '.((int) $this->id); dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); From 18be0b4f271d428e13949c2fc9441b9576c5a6a6 Mon Sep 17 00:00:00 2001 From: Quentin VIAL-GOUTEYRON Date: Tue, 20 Sep 2022 14:44:43 +0200 Subject: [PATCH 4/8] FIX missing facture class inclusion --- htdocs/product/class/product.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 705081484fe..ca2418a0ab1 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -2926,7 +2926,6 @@ class Product extends CommonObject { // phpcs:enable global $conf, $user, $hookmanager; - $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,"; $sql .= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty"; $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd"; @@ -3000,6 +2999,8 @@ class Product extends CommonObject $this->stats_commande['qty'] -= $adeduire; } else { + 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 $adeduire = 0; $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet fd '; From be03dcb8ceeb94f03958509e7070caa1ac1a8da5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Dec 2022 19:06:16 +0100 Subject: [PATCH 5/8] Fix strange condition --- htdocs/product/class/product.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ca2418a0ab1..5e5198d6b4c 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -2985,7 +2985,7 @@ class Product extends CommonObject $adeduire = 0; $sql = "SELECT sum(fd.qty) as count FROM ".MAIN_DB_PREFIX."facturedet fd "; $sql .= " JOIN ".MAIN_DB_PREFIX."facture f ON fd.fk_facture = f.rowid "; - $sql .= " JOIN ".MAIN_DB_PREFIX."element_element el ON el.fk_target = f.rowid and el.targettype = 'facture' and sourcetype = 'commande'"; + $sql .= " JOIN ".MAIN_DB_PREFIX."element_element el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; $sql .= " JOIN ".MAIN_DB_PREFIX."commande c ON el.fk_source = c.rowid "; $sql .= " WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).") AND c.facture = 0 AND fd.fk_product = ".((int) $this->id); dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); From 934d7b46d01c3ae93d7fe50179dfb1c0376fab7f Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Dec 2022 19:15:07 +0100 Subject: [PATCH 6/8] Update product.class.php --- htdocs/product/class/product.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 9cb7187a0ad..d9e09ce788d 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3091,9 +3091,9 @@ class Product extends CommonObject // For every order having invoice already validated we need to decrease stock cause it's in physical stock $adeduire = 0; $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet fd '; - $sql .= ' JOIN '.MAIN_DB_PREFIX.'facture f ON fd.fk_facture = f.rowid '; - $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; - $sql .= ' JOIN '.MAIN_DB_PREFIX.'commande c ON el.fk_source = c.rowid '; + $sql .= ' JOIN '.MAIN_DB_PREFIX.'facture as f ON fd.fk_facture = f.rowid '; + $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element as el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; + $sql .= ' JOIN '.MAIN_DB_PREFIX.'commande as c ON el.fk_source = c.rowid '; $sql .= ' WHERE c.fk_statut IN ('.$this->db->sanitize($filtrestatut).') AND f.fk_statut > '.Facture::STATUS_DRAFT.' AND fd.fk_product = '.((int) $this->id); dol_syslog(__METHOD__.":: sql $sql", LOG_NOTICE); $resql = $this->db->query($sql); From 572ba6d3e06b591c51dd7c0dd3d0ec7f6592248e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Dec 2022 19:15:46 +0100 Subject: [PATCH 7/8] Update product.class.php --- htdocs/product/class/product.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index d9e09ce788d..0271e0c0d2c 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3070,7 +3070,7 @@ class Product extends CommonObject if (!empty($conf->global->STOCK_CALCULATE_ON_BILL)) { if (!empty($conf->global->DECREASE_ONLY_UNINVOICEDPRODUCTS)) { $adeduire = 0; - $sql = "SELECT sum(fd.qty) as count FROM ".$this->db->prefix()."facturedet fd "; + $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 "; $sql .= " JOIN ".$this->db->prefix()."element_element as el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; $sql .= " JOIN ".$this->db->prefix()."commande as c ON el.fk_source = c.rowid "; @@ -3090,7 +3090,7 @@ class Product extends CommonObject // For every order having invoice already validated we need to decrease stock cause it's in physical stock $adeduire = 0; - $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet fd '; + $sql = 'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.'facturedet as fd '; $sql .= ' JOIN '.MAIN_DB_PREFIX.'facture as f ON fd.fk_facture = f.rowid '; $sql .= ' JOIN '.MAIN_DB_PREFIX."element_element as el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))"; $sql .= ' JOIN '.MAIN_DB_PREFIX.'commande as c ON el.fk_source = c.rowid '; From 582d9ac514bd94632a8eea47fe39e25844cb0365 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Dec 2022 19:22:34 +0100 Subject: [PATCH 8/8] Update product.class.php --- htdocs/product/class/product.class.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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