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
This commit is contained in:
antonin_tdj 2021-04-28 13:37:05 +02:00 committed by GitHub
parent f5d18a0023
commit dfb4a1c30a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.')';
}