From dfb4a1c30a9bb9c53a9c80e08bcf686d0f38ce05 Mon Sep 17 00:00:00 2001 From: antonin_tdj <50403308+ibuiv@users.noreply.github.com> Date: Wed, 28 Apr 2021 13:37:05 +0200 Subject: [PATCH] lint (code) Apply remarks and upgrade codee - Sets List of all Visible Warehouses at the beginning with getEntity method (removed the call to global $mc) - Better check for warehouse auto select (`$count == 1 && (empty($fk_entrepot) || $fk_entrepot <= 0) && ...`) - Better SQL applying eldy's remarks --- htdocs/product/stock/replenish.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php index 7421e71f05c..73124fd7bad 100644 --- a/htdocs/product/stock/replenish.php +++ b/htdocs/product/stock/replenish.php @@ -66,18 +66,22 @@ $fourn_id = GETPOST('fourn_id', 'int'); $fk_supplier = GETPOST('fk_supplier', 'int'); $fk_entrepot = GETPOST('fk_entrepot', 'int'); +//List all visible warehouses +$resWar = $db->query("SELECT rowid FROM " . MAIN_DB_PREFIX . "entrepot WHERE entity IN (" . $db->sanitize(getEntity('stock')) .")"); +$listofqualifiedwarehousesid = ""; +$count = 0; +while ($tmpobj = $db->fetch_object($resWar)) { + if (!empty($listofqualifiedwarehousesid)) { + $listofqualifiedwarehousesid .= ","; + } + $listofqualifiedwarehousesid .= $tmpobj->rowid; + $lastWarehouseID = $tmpobj->rowid; + $count++; +}; + //MultiCompany : If only 1 Warehouse is visible, filter will automatically be set to it. -if ($fk_entrepot == "-1" && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) { - global $mc; - $visibleWarehousesEntities = $conf->entity; - if (isset($mc->sharings['stock']) && !empty($mc->sharings['stock'])) { - $visibleWarehousesEntities .= "," . implode(",", $mc->sharings['stock']); - } - $resWar = $db->query("SELECT rowid FROM " . MAIN_DB_PREFIX . "entrepot WHERE entity IN (" . $db->sanitize($visibleWarehousesEntities) .")"); - if ($db->num_rows($resWar) == 1) { - $tmpobj = $db->fetch_object($resWar); - $fk_entrepot = $tmpobj->rowid; - } +if ($count == 1 && (empty($fk_entrepot) || $fk_entrepot <= 0) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) { + $fk_entrepot = $lastWarehouseID; }; $texte = ''; @@ -340,8 +344,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 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').')'; +$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s ON p.rowid = s.fk_product AND s.fk_entrepot IN ('.$db->sanitize($listofqualifiedwarehousesid).')'; +//$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.')'; }