Merge pull request #22144 from atm-quentin/FIX_invoice_theorical_stock

FIX theorical stock when stock calculate on bill
This commit is contained in:
Laurent Destailleur 2022-12-28 19:28:07 +01:00 committed by GitHub
commit 77d09921ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,18 +3065,38 @@ 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 fd ";
$sql .= " JOIN ".$this->db->prefix()."facture f ON fd.fk_facture = f.rowid ";
$sql .= " JOIN ".$this->db->prefix()."element_element el ON el.fk_target = f.rowid and el.targettype = 'facture' and sourcetype = 'commande'";
$sql .= " JOIN ".$this->db->prefix()."commande c ON el.fk_source = c.rowid ";
$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 ";
$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) {
$obj = $this->db->fetch_object($resql);
$adeduire += $obj->count;
}
}
$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
$adeduire = 0;
$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 ';
$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) {