diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index cee5c72abea..2bdc950495e 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -1680,17 +1680,19 @@ class Product extends CommonObject
$sql.= " COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
$sql.= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed";
$sql.= ", ".MAIN_DB_PREFIX."commandedet as cd";
+ $sql.= ", ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."expedition as e";
$sql.= ", ".MAIN_DB_PREFIX."societe as s";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE e.rowid = ed.fk_expedition";
+ $sql.= " AND c.rowid = cd.fk_commande";
$sql.= " AND e.fk_soc = s.rowid";
$sql.= " AND e.entity = ".$conf->entity;
$sql.= " AND ed.fk_origin_line = cd.rowid";
$sql.= " AND cd.fk_product = ".$this->id;
if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND e.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
if ($socid > 0) $sql.= " AND e.fk_soc = ".$socid;
- if ($filtrestatut <> '') $sql.= " AND e.fk_statut in (".$filtrestatut.")";
+ if ($filtrestatut <> '') $sql.= " AND c.fk_statut in (".$filtrestatut.")";
$result = $this->db->query($sql);
if ( $result )
@@ -1709,6 +1711,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 +2916,7 @@ class Product extends CommonObject
}
}
$this->db->free($result);
+ $this->load_virtual_stock();
return 1;
}
else
@@ -2881,6 +2926,47 @@ 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,'1,2');
+ 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,'3');
+ 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);
+ //echo $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/stats/commande.php b/htdocs/product/stats/commande.php
index b19c3c7fd68..97e9a612ae1 100644
--- a/htdocs/product/stats/commande.php
+++ b/htdocs/product/stats/commande.php
@@ -118,7 +118,7 @@ if ($id > 0 || ! empty($ref))
$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client, c.rowid, c.total_ht as total_ht, c.ref,";
- $sql.= " c.date_commande, c.fk_statut as statut, c.facture, c.rowid as commandeid";
+ $sql.= " c.date_commande, c.fk_statut as statut, c.facture, c.rowid as commandeid, d.qty";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."commande as c";
@@ -148,6 +148,7 @@ if ($id > 0 || ! empty($ref))
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("OrderDate"),$_SERVER["PHP_SELF"],"c.date_commande","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"c.total_ht","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"c.fk_statut","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print "\n";
@@ -170,6 +171,7 @@ if ($id > 0 || ! empty($ref))
print "
".$objp->code_client." | \n";
print "";
print dol_print_date($db->jdate($objp->date_commande))." | ";
+ print "".$objp->qty." | \n";
print "".price($objp->total_ht)." | \n";
print ''.$commandestatic->LibStatut($objp->statut,$objp->facture,5).' | ';
print "\n";
diff --git a/htdocs/product/stats/commande_fournisseur.php b/htdocs/product/stats/commande_fournisseur.php
index 69845b4b264..22e3e2b30ba 100644
--- a/htdocs/product/stats/commande_fournisseur.php
+++ b/htdocs/product/stats/commande_fournisseur.php
@@ -111,7 +111,7 @@ if ($id > 0 || ! empty($ref))
$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
$sql.= " c.rowid, c.total_ht as total_ht, c.ref,";
- $sql.= " c.date_commande, c.fk_statut as statut, c.rowid as commandeid";
+ $sql.= " c.date_commande, c.fk_statut as statut, c.rowid as commandeid, d.qty";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."commande_fournisseur as c";
@@ -141,6 +141,7 @@ if ($id > 0 || ! empty($ref))
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SupplierCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("OrderDate"),$_SERVER["PHP_SELF"],"c.date_commande","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"c.total_ht","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"c.fk_statut","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print "\n";
@@ -165,6 +166,7 @@ if ($id > 0 || ! empty($ref))
print ''.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,44).' | ';
print "".$objp->code_client." | \n";
print ''.dol_print_date($db->jdate($objp->date_commande))." | ";
+ print "".$objp->qty." | \n";
print ''.price($objp->total_ht)." | \n";
print ''.$commandestatic->getLibStatut(4).' | ';
print "\n";
diff --git a/htdocs/product/stats/facture.php b/htdocs/product/stats/facture.php
index cac2bccc45b..dc65764defd 100644
--- a/htdocs/product/stats/facture.php
+++ b/htdocs/product/stats/facture.php
@@ -122,7 +122,7 @@ if ($id > 0 || ! empty($ref))
$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
$sql.= " f.facnumber, f.total as total_ht,";
- $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
+ $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid, d.qty";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture as f";
@@ -152,6 +152,7 @@ if ($id > 0 || ! empty($ref))
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total_ht","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print "\n";
@@ -174,6 +175,7 @@ if ($id > 0 || ! empty($ref))
print "".$objp->code_client." | \n";
print "";
print dol_print_date($db->jdate($objp->datef),'day')." | ";
+ print "".$objp->qty." | \n";
print "".price($objp->total_ht)." | \n";
print ''.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).' | ';
print "\n";
diff --git a/htdocs/product/stats/facture_fournisseur.php b/htdocs/product/stats/facture_fournisseur.php
index 2a06c30a496..4cbeaec7e76 100644
--- a/htdocs/product/stats/facture_fournisseur.php
+++ b/htdocs/product/stats/facture_fournisseur.php
@@ -123,7 +123,7 @@ if ($id > 0 || ! empty($ref))
$sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client, f.ref, f.total_ht as total_ht,";
- $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
+ $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid, d.qty";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
@@ -153,6 +153,7 @@ if ($id > 0 || ! empty($ref))
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("SupplierCode"),$_SERVER["PHP_SELF"],"s.code_client","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total_ht","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print "\n";
@@ -175,6 +176,7 @@ if ($id > 0 || ! empty($ref))
print "".$objp->code_client." | \n";
print "";
print dol_print_date($db->jdate($objp->datef))." | ";
+ print "".$objp->qty." | \n";
print "".price($objp->total_ht)." | \n";
print ''.$supplierinvoicestatic->LibStatut($objp->paye,$objp->statut,5).' | ';
print "\n";
diff --git a/htdocs/product/stats/propal.php b/htdocs/product/stats/propal.php
index a55673c62b0..e9a78f18b05 100644
--- a/htdocs/product/stats/propal.php
+++ b/htdocs/product/stats/propal.php
@@ -113,7 +113,7 @@ if ($id > 0 || ! empty($ref))
$sql = "SELECT DISTINCT s.nom, s.rowid as socid, p.rowid as propalid, p.ref, p.total_ht as amount,";
- $sql.= "p.datep, p.fk_statut as statut";
+ $sql.= "p.datep, p.fk_statut as statut, d.qty";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ",".MAIN_DB_PREFIX."propal as p";
@@ -141,6 +141,7 @@ if ($id > 0 || ! empty($ref))
print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"p.rowid","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&id=".$product->id,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("DatePropal"),$_SERVER["PHP_SELF"],"p.datep","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&id=".$product->id,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"p.total","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"p.fk_statut","","&id=".$product->id,'align="right"',$sortfield,$sortorder);
print "\n";
@@ -162,6 +163,7 @@ if ($id > 0 || ! empty($ref))
print ''.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,44).' | ';
print '';
print dol_print_date($db->jdate($objp->datep))." | ";
+ print "".$objp->qty." | \n";
print ''.price($objp->amount).' | '."\n";
print ''.$propalstatic->LibStatut($objp->statut,5).' | ';
print ''."\n";
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'].')';
diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php
index 6aee0bada53..817ec5cbf7a 100644
--- a/htdocs/product/stock/replenish.php
+++ b/htdocs/product/stock/replenish.php
@@ -252,36 +252,59 @@ $sql.= ', s.fk_product';
if ($usevirtualstock)
{
- $sqlCommandesCli = "(SELECT SUM(cd.qty) as qty";
+ $sqlCommandesCli = "(SELECT ".$db->ifsql("SUM(cd.qty) IS NULL", "0", "SUM(cd.qty)")." as qty";
$sqlCommandesCli.= " FROM ".MAIN_DB_PREFIX."commandedet as cd";
- $sqlCommandesCli.= ", ".MAIN_DB_PREFIX."commande as c";
- $sqlCommandesCli.= " WHERE c.rowid = cd.fk_commande";
- $sqlCommandesCli.= " AND c.entity = ".$conf->entity;
+ $sqlCommandesCli.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON (c.rowid = cd.fk_commande)";
+ $sqlCommandesCli.= " WHERE c.entity = ".$conf->entity;
$sqlCommandesCli.= " AND cd.fk_product = p.rowid";
- $sqlCommandesCli.= " AND c.fk_statut in (1,2))";
-
- $sqlCommandesFourn = "(SELECT SUM(cd.qty) as qty";
+ $sqlCommandesCli.= " AND c.fk_statut IN (1,2))";
+
+ $sqlExpeditionsCli = "(SELECT ".$db->ifsql("SUM(ed.qty) IS NULL", "0", "SUM(ed.qty)")." as qty";
+ $sqlExpeditionsCli.= " FROM ".MAIN_DB_PREFIX."expedition as e";
+ $sqlExpeditionsCli.= " LEFT JOIN ".MAIN_DB_PREFIX."expeditiondet as ed ON (ed.fk_expedition = e.rowid)";
+ $sqlExpeditionsCli.= " LEFT JOIN ".MAIN_DB_PREFIX."commandedet as cd ON (cd.rowid = ed.fk_origin_line)";
+ $sqlExpeditionsCli.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON (c.rowid = cd.fk_commande)";
+ $sqlExpeditionsCli.= " WHERE e.entity = ".$conf->entity;
+ $sqlExpeditionsCli.= " AND cd.fk_product = p.rowid";
+ $sqlExpeditionsCli.= " AND c.fk_statut IN (1,2))";
+
+ $sqlCommandesFourn = "(SELECT ".$db->ifsql("SUM(cd.qty) IS NULL", "0", "SUM(cd.qty)")." as qty";
$sqlCommandesFourn.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd";
$sqlCommandesFourn.= ", ".MAIN_DB_PREFIX."commande_fournisseur as c";
$sqlCommandesFourn.= " WHERE c.rowid = cd.fk_commande";
$sqlCommandesFourn.= " AND c.entity = ".$conf->entity;
$sqlCommandesFourn.= " AND cd.fk_product = p.rowid";
- $sqlCommandesFourn.= " AND c.fk_statut in (3))";
-
- $sql.= ' HAVING ((p.desiredstock > 0 AND p.desiredstock > (SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')';
- $sql.= ' - '.$db->ifsql($sqlCommandesCli.' IS NULL', '0', $sqlCommandesCli).' + '.$db->ifsql($sqlCommandesFourn.' IS NULL', '0', $sqlCommandesFourn).'))';
-
- $sql.= ' OR (p.seuil_stock_alerte > 0 AND p.seuil_stock_alerte > (SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')';
- $sql.= ' - '.$db->ifsql($sqlCommandesCli.' IS NULL', '0', $sqlCommandesCli).' + '.$db->ifsql($sqlCommandesFourn.' IS NULL', '0', $sqlCommandesFourn).'))';
- $sql.= " )";
+ $sqlCommandesFourn.= " AND c.fk_statut IN (3,4))";
+
+ $sqlReceptionFourn = "(SELECT ".$db->ifsql("SUM(fd.qty) IS NULL", "0", "SUM(fd.qty)")." as qty";
+ $sqlReceptionFourn.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as cf";
+ $sqlReceptionFourn.= " LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as fd ON (fd.fk_commande = cf.rowid)";
+ $sqlReceptionFourn.= " WHERE cf.entity = ".$conf->entity;
+ $sqlReceptionFourn.= " AND fd.fk_product = p.rowid";
+ $sqlReceptionFourn.= " AND cf.fk_statut IN (3,4))";
+
+ $sql.= ' HAVING ((('.$db->ifsql("p.desiredstock IS NULL", "0", "p.desiredstock").' > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')';
+ $sql.= ' - ('.$sqlCommandesCli.' - '.$sqlExpeditionsCli.') + ('.$sqlCommandesFourn.' - '.$sqlReceptionFourn.')))';
+ $sql.= ' OR (p.seuil_stock_alerte >= 0 AND (p.seuil_stock_alerte > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')';
+ $sql.= ' - ('.$sqlCommandesCli.' - '.$sqlExpeditionsCli.') + ('.$sqlCommandesFourn.' - '.$sqlReceptionFourn.'))))';
+
+ if ($salert == 'on') // Option to see when stock is lower than alert
+ {
+ $sql.= ' AND (p.seuil_stock_alerte > 0 AND (p.seuil_stock_alerte > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')';
+ $sql.= ' - ('.$sqlCommandesCli.' - '.$sqlExpeditionsCli.') + ('.$sqlCommandesFourn.' - '.$sqlReceptionFourn.')))';
+ $alertchecked = 'checked="checked"';
+ }
} else {
- $sql.= ' HAVING ((p.desiredstock > 0 AND (p.desiredstock > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").'))) OR (p.seuil_stock_alerte > 0 AND (seuil_stock_alerte > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").'))))';
-}
-if ($salert == 'on') // Option to see when stock is lower than alert
-{
- $sql .= ' AND SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").') < p.seuil_stock_alerte AND p.seuil_stock_alerte is not NULL';
- $alertchecked = 'checked="checked"';
+ $sql.= ' HAVING ((p.desiredstock > 0 AND (p.desiredstock > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')))';
+ $sql.= ' OR (p.seuil_stock_alerte > 0 AND (p.seuil_stock_alerte > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").'))))';
+
+ if ($salert == 'on') // Option to see when stock is lower than alert
+ {
+ $sql.= ' AND (p.seuil_stock_alerte > 0 AND (p.seuil_stock_alerte > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')))';
+ $alertchecked = 'checked="checked"';
+ }
}
+
$sql.= $db->order($sortfield,$sortorder);
$sql.= $db->plimit($limit, $offset);
@@ -368,6 +391,7 @@ print ' |