diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php index 5164b72fa4a..427a8a18785 100644 --- a/htdocs/product/stock/replenish.php +++ b/htdocs/product/stock/replenish.php @@ -195,6 +195,7 @@ if ($action == 'order' && isset($_POST['valid'])) //we now know how many orders we need and what lines they have $i = 0; + $fail = 0; $orders = array(); $suppliersid = array_keys($suppliers); foreach ($suppliers as $supplier) @@ -311,6 +312,7 @@ if (!empty($conf->global->STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE) && $fk_entre $sql .= ' pse.desiredstock as desiredstockpse, pse.seuil_stock_alerte as seuil_stock_alertepse,'; } $sql .= ' '.$sqldesiredtock.' as desiredstockcombined, '.$sqlalertstock.' as seuil_stock_alertecombined,'; +$sql .= ' s.fk_product,'; $sql .= ' SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").') as stock_physique'; // Add fields from hooks @@ -319,8 +321,8 @@ $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // N $sql .= $hookmanager->resPrint; $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p'; -$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s'; -$sql .= ' ON (p.rowid = s.fk_product AND s.fk_entrepot IN (SELECT ent.rowid FROM '.MAIN_DB_PREFIX.'entrepot AS ent WHERE ent.entity IN('.getEntity('stock').')))'; +$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s ON p.rowid = s.fk_product'; +$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot AS ent ON s.fk_entrepot = ent.rowid AND ent.entity IN('.getEntity('stock').')'; if ($fk_supplier > 0) { $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_fournisseur_price pfp ON (pfp.fk_product = p.rowid AND pfp.fk_soc = '.$fk_supplier.')'; } @@ -360,36 +362,34 @@ $sql .= ', s.fk_product'; if ($usevirtualstock) { - $sqlCommandesCli = "(SELECT ".$db->ifsql("SUM(cd.qty) IS NULL", "0", "SUM(cd.qty)")." as qty"; - $sqlCommandesCli .= " FROM ".MAIN_DB_PREFIX."commandedet as cd"; - $sqlCommandesCli .= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON (c.rowid = cd.fk_commande)"; - $sqlCommandesCli .= " WHERE c.entity IN (".getEntity('commande').")"; - $sqlCommandesCli .= " AND cd.fk_product = p.rowid"; - $sqlCommandesCli .= " AND c.fk_statut IN (1,2))"; + $sqlCommandesCli = "(SELECT ".$db->ifsql("SUM(cd1.qty) IS NULL", "0", "SUM(cd1.qty)")." as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL + $sqlCommandesCli .= " FROM ".MAIN_DB_PREFIX."commandedet as cd1, ".MAIN_DB_PREFIX."commande as c1"; + $sqlCommandesCli .= " WHERE c1.rowid = cd1.fk_commande AND c1.entity IN (".getEntity('commande').")"; + $sqlCommandesCli .= " AND cd1.fk_product = p.rowid"; + $sqlCommandesCli .= " AND c1.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 IN (".getEntity('expedition').")"; - $sqlExpeditionsCli .= " AND cd.fk_product = p.rowid"; - $sqlExpeditionsCli .= " AND e.fk_statut IN (1,2))"; + $sqlExpeditionsCli = "(SELECT ".$db->ifsql("SUM(ed2.qty) IS NULL", "0", "SUM(ed2.qty)")." as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL + $sqlExpeditionsCli .= " FROM ".MAIN_DB_PREFIX."expedition as e2,"; + $sqlExpeditionsCli .= " ".MAIN_DB_PREFIX."expeditiondet as ed2,"; + $sqlExpeditionsCli .= " ".MAIN_DB_PREFIX."commandedet as cd2"; + $sqlExpeditionsCli .= " WHERE ed2.fk_expedition = e2.rowid AND cd2.rowid = ed2.fk_origin_line AND e2.entity IN (".getEntity('expedition').")"; + $sqlExpeditionsCli .= " AND cd2.fk_product = p.rowid"; + $sqlExpeditionsCli .= " AND e2.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 IN (".getEntity('supplier_order').")"; - $sqlCommandesFourn .= " AND cd.fk_product = p.rowid"; - $sqlCommandesFourn .= " AND c.fk_statut IN (3,4))"; + $sqlCommandesFourn = "(SELECT ".$db->ifsql("SUM(cd3.qty) IS NULL", "0", "SUM(cd3.qty)")." as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL + $sqlCommandesFourn .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd3,"; + $sqlCommandesFourn .= " ".MAIN_DB_PREFIX."commande_fournisseur as c3"; + $sqlCommandesFourn .= " WHERE c3.rowid = cd3.fk_commande"; + $sqlCommandesFourn .= " AND c3.entity IN (".getEntity('supplier_order').")"; + $sqlCommandesFourn .= " AND cd3.fk_product = p.rowid"; + $sqlCommandesFourn .= " AND c3.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 IN (".getEntity('supplier_order').")"; - $sqlReceptionFourn .= " AND fd.fk_product = p.rowid"; - $sqlReceptionFourn .= " AND cf.fk_statut IN (3,4))"; + $sqlReceptionFourn = "(SELECT ".$db->ifsql("SUM(fd4.qty) IS NULL", "0", "SUM(fd4.qty)")." as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL + $sqlReceptionFourn .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as cf4,"; + $sqlReceptionFourn .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as fd4"; + $sqlReceptionFourn .= " WHERE fd4.fk_commande = cf4.rowid AND cf4.entity IN (".getEntity('supplier_order').")"; + $sqlReceptionFourn .= " AND fd4.fk_product = p.rowid"; + $sqlReceptionFourn .= " AND cf4.fk_statut IN (3,4))"; $sql .= ' HAVING (('.$sqldesiredtock.' >= 0 AND ('.$sqldesiredtock.' > SUM('.$db->ifsql("s.reel IS NULL", "0", "s.reel").')'; $sql .= ' - ('.$sqlCommandesCli.' - '.$sqlExpeditionsCli.') + ('.$sqlCommandesFourn.' - '.$sqlReceptionFourn.')))';