Clean code: Replace join on category for filter on categ with EXISTS

This commit is contained in:
Laurent Destailleur 2022-10-10 23:49:41 +02:00
parent 532bfe3f92
commit 1f051493e1
2 changed files with 67 additions and 44 deletions

View File

@ -439,9 +439,6 @@ if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_extrafields as ef on (p.rowid = ef.fk_object)";
}
if (!empty($searchCategoryProductList) || !empty($catid)) {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_product as cp ON p.rowid = cp.fk_product"; // We'll need this table joined to the select in order to filter by categ
}
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
// multilang
if (getDolGlobalInt('MAIN_MULTILANGS')) {
@ -506,30 +503,32 @@ if ($catid > 0) {
if ($catid == -2) {
$sql .= " AND cp.fk_categorie IS NULL";
}
$searchCategoryProductSqlList = array();
if ($searchCategoryProductOperator == 1) {
// Search for tag/category ($searchCategoryProductList is an array of ID)
if (!empty($searchCategoryProductList)) {
$searchCategoryProductSqlList = array();
$listofcategoryid = '';
foreach ($searchCategoryProductList as $searchCategoryProduct) {
if (intval($searchCategoryProduct) == -2) {
$searchCategoryProductSqlList[] = "cp.fk_categorie IS NULL";
$searchCategoryProductSqlList[] = "NOT EXISTS (SELECT ck.fk_product FROM ".MAIN_DB_PREFIX."categorie_product as ck WHERE p.rowid = ck.fk_product)";
} elseif (intval($searchCategoryProduct) > 0) {
$searchCategoryProductSqlList[] = "cp.fk_categorie = ".$db->escape($searchCategoryProduct);
$listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryProduct);
}
}
if (!empty($searchCategoryProductSqlList)) {
$sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")";
if ($listofcategoryid) {
$searchCategoryProductSqlList[] = " EXISTS (SELECT ck.fk_product FROM ".MAIN_DB_PREFIX."categorie_product as ck WHERE p.rowid = ck.fk_product AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))";
}
} else {
foreach ($searchCategoryProductList as $searchCategoryProduct) {
if (intval($searchCategoryProduct) == -2) {
$searchCategoryProductSqlList[] = "cp.fk_categorie IS NULL";
} elseif (intval($searchCategoryProduct) > 0) {
$searchCategoryProductSqlList[] = "p.rowid IN (SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product WHERE fk_categorie = ".((int) $searchCategoryProduct).")";
if ($searchCategoryProductOperator == 1) {
if (!empty($searchCategoryProductSqlList)) {
$sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")";
}
} else {
if (!empty($searchCategoryProductSqlList)) {
$sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")";
}
}
if (!empty($searchCategoryProductSqlList)) {
$sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")";
}
}
if ($fourn_id > 0) {
$sql .= " AND pfp.fk_soc = ".((int) $fourn_id);
}

View File

@ -478,13 +478,6 @@ $sql .= " region.code_region as region_code, region.nom as region_name";
if ($search_sale && $search_sale != '-1') {
$sql .= ", sc.fk_soc, sc.fk_user";
}
// We'll need these fields in order to filter by categ
if ($search_categ_cus && $search_categ_cus != -1) {
$sql .= ", cc.fk_categorie, cc.fk_soc";
}
if ($search_categ_sup && $search_categ_sup != -1) {
$sql .= ", cs.fk_categorie, cs.fk_soc";
}
// Add fields from extrafields
if (!empty($extrafields->attributes[$object->table_element]['label'])) {
foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
@ -505,13 +498,6 @@ $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_typent as typent on (typent.id = s.fk_ty
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_effectif as staff on (staff.id = s.fk_effectif)";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as state on (state.rowid = s.fk_departement)";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as region on (region.code_region = state.fk_region)";
// We'll need this table joined to the select in order to filter by categ
if (!empty($search_categ_cus) && $search_categ_cus != '-1') {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_societe as cc ON s.rowid = cc.fk_soc"; // We'll need this table joined to the select in order to filter by categ
}
if (!empty($search_categ_sup) && $search_categ_sup != '-1') {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_fournisseur as cs ON s.rowid = cs.fk_soc"; // We'll need this table joined to the select in order to filter by categ
}
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."c_stcomm as st ON s.fk_stcomm = st.id";
// We'll need this table joined to the select in order to filter by sale
if ($search_sale == -2) {
@ -540,26 +526,64 @@ if ($search_sale == -2) {
} elseif ($search_sale > 0) {
$sql .= " AND sc.fk_user = ".((int) $search_sale);
}
if ($search_categ_cus > 0) {
$sql .= " AND cc.fk_categorie = ".((int) $search_categ_cus);
$searchCategoryCustomerList = array($search_categ_cus);
$searchCategoryCustomerOperator = 0;
// Search for tag/category ($searchCategoryCustomerList is an array of ID)
if (!empty($searchCategoryCustomerList)) {
$searchCategoryCustomerSqlList = array();
$listofcategoryid = '';
foreach ($searchCategoryCustomerList as $searchCategoryCustomer) {
if (intval($searchCategoryCustomer) == -2) {
$searchCategoryCustomerSqlList[] = "NOT EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe as ck WHERE s.rowid = ck.fk_soc)";
} elseif (intval($searchCategoryCustomer) > 0) {
$listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryCustomer);
}
}
if ($listofcategoryid) {
$searchCategoryCustomerSqlList[] = " EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe as ck WHERE s.rowid = ck.fk_soc AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))";
}
if ($searchCategoryCustomerOperator == 1) {
if (!empty($searchCategoryCustomerSqlList)) {
$sql .= " AND (".implode(' OR ', $searchCategoryCustomerSqlList).")";
}
} else {
if (!empty($searchCategoryCustomerSqlList)) {
$sql .= " AND (".implode(' AND ', $searchCategoryCustomerSqlList).")";
}
}
}
if ($search_categ_sup > 0) {
$sql .= " AND cs.fk_categorie = ".((int) $search_categ_sup);
$searchCategorySupplierList = array($search_categ_sup);
$searchCategorySupplierOperator = 0;
// Search for tag/category ($searchCategorySupplierList is an array of ID)
if (!empty($searchCategorySupplierList)) {
$searchCategorySupplierSqlList = array();
$listofcategoryid = '';
foreach ($searchCategorySupplierList as $searchCategorySupplier) {
if (intval($searchCategorySupplier) == -2) {
$searchCategorySupplierSqlList[] = "NOT EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_fournisseur as ck WHERE s.rowid = ck.fk_soc)";
} elseif (intval($searchCategorySupplier) > 0) {
$listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategorySupplier);
}
}
if ($listofcategoryid) {
$searchCategorySupplierSqlList[] = " EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_fournisseur as ck WHERE s.rowid = ck.fk_soc AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))";
}
if ($searchCategorySupplierOperator == 1) {
if (!empty($searchCategorySupplierSqlList)) {
$sql .= " AND (".implode(' OR ', $searchCategorySupplierSqlList).")";
}
} else {
if (!empty($searchCategorySupplierSqlList)) {
$sql .= " AND (".implode(' AND ', $searchCategorySupplierSqlList).")";
}
}
}
if ($search_categ_cus == -2) {
$sql .= " AND cc.fk_categorie IS NULL";
}
if ($search_categ_sup == -2) {
$sql .= " AND cs.fk_categorie IS NULL";
}
if ($search_all) {
$sql .= natural_search(array_keys($fieldstosearchall), $search_all);
}
if (strlen($search_cti)) {
$sql .= natural_search('s.phone', $search_cti);
}
if ($search_id > 0) {
$sql .= natural_search("s.rowid", $search_id, 1);
}