Rewrite SQL to match Dolibarr best practices

This commit is contained in:
ldestailleur
2025-08-06 03:51:09 +02:00
parent 5a39714ce3
commit f4c0cfa4e3

View File

@@ -494,7 +494,8 @@ $sql .= ' p.import_key,';
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
$sql .= ' p.fk_unit, cu.label as cu_label,';
}
$sql .= ' MIN(pfp.unitprice) as bestpurchaseprice';
//$sql .= ' MIN(pfp.unitprice) as bestpurchaseprice';
$sql .= '(SELECT MIN(pfp.unitprice) FROM llx_product_fournisseur_price as pfp WHERE p.rowid = pfp.fk_product) as bestpurchaseprice';
if (isModEnabled('variants')) {
$sql .= ', pac.rowid as prod_comb_id';
$sql .= ', pac.fk_product_parent';
@@ -523,8 +524,8 @@ if (getDolGlobalString('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)";
}
$linktopfp = " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
$sql .= $linktopfp;
//$linktopfp = " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
//$sql .= $linktopfp;
// multilang
if (getDolGlobalInt('MAIN_MULTILANGS')) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND pl.lang = '".$db->escape($langs->getDefaultLang())."'";
@@ -680,7 +681,7 @@ include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
$parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
$sql .= " GROUP BY p.rowid, p.ref, p.ref_ext, p.description, p.label, p.barcode, p.price, p.tva_tx, p.price_ttc, p.price_base_type,";
/*$sql .= " GROUP BY p.rowid, p.ref, p.ref_ext, p.description, p.label, p.barcode, p.price, p.tva_tx, p.price_ttc, p.price_base_type,";
$sql .= " p.fk_product_type, p.duration, p.finished, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock,";
$sql .= ' p.datec, p.tms, p.entity, p.tobatch, p.pmp, p.cost_price, p.stock,';
if (!getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
@@ -712,14 +713,13 @@ $parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListGroupBy', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
//if (GETPOST("toolowstock")) $sql.= " HAVING SUM(s.reel) < p.seuil_stock_alerte"; // Not used yet
print $sql;
*/
$nbtotalofrecords = '';
if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
/* The fast and low memory method to get and count full list converts the sql into a sql count */
$sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
$sqlforcount = preg_replace('/'.preg_quote($linktopfp, '/').'/', '', $sqlforcount);
//$sqlforcount = preg_replace('/'.preg_quote($linktopfp, '/').'/', '', $sqlforcount);
$sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
$resql = $db->query($sqlforcount);
@@ -743,6 +743,8 @@ if ($limit) {
$sql .= $db->plimit($limit + 1, $offset);
}
//print $sql;
$resql = $db->query($sql);
if (!$resql) {
dol_print_error($db);