From 3f2f676e582003b26a85aa1faaa568c7b8a4a2cb Mon Sep 17 00:00:00 2001 From: atm-corentin <165782689+atm-corentin@users.noreply.github.com> Date: Wed, 29 Oct 2025 22:12:42 +0100 Subject: [PATCH] Fix : Add 'printFieldListGroupBy' hook to stockatdate.php for module flexibility (#35996) * Refactor stock SQL query logic to enhance readability, support hook extensibility, and maintain DRY principles. * Pass `$object` and `$action` to hookmanager in stock listing queries for improved hook extensibility. --- htdocs/product/stock/stockatdate.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/htdocs/product/stock/stockatdate.php b/htdocs/product/stock/stockatdate.php index 3080bb1262b..4903231365c 100644 --- a/htdocs/product/stock/stockatdate.php +++ b/htdocs/product/stock/stockatdate.php @@ -287,7 +287,7 @@ if (!empty($search_fk_warehouse)) { } // Add fields from hooks $parameters = array(); -$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook +$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action); // Note that $action and $object may have been modified by hook $sql .= $hookmanager->resPrint; $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p'; @@ -296,7 +296,7 @@ if (!empty($search_fk_warehouse)) { } // Add fields from hooks $parameters = array(); -$reshook = $hookmanager->executeHooks('printFieldListJoin', $parameters); // Note that $action and $object may have been modified by hook +$reshook = $hookmanager->executeHooks('printFieldListJoin', $parameters, $object, $action); // Note that $action and $object may have been modified by hook $sql .= $hookmanager->resPrint; $sql .= ' WHERE p.entity IN ('.getEntity('product').')'; if ($productid > 0) { @@ -314,8 +314,24 @@ if ($search_ref) { if ($search_nom) { $sql .= natural_search('p.label', $search_nom); } -$sql .= ' GROUP BY p.rowid, p.ref, p.label, p.description, p.price, p.pmp, p.price_ttc, p.price_base_type, p.fk_product_type, p.desiredstock, p.seuil_stock_alerte,'; -$sql .= ' p.tms, p.duration, p.tobuy, p.stock'; + +$sqlGroupBy = ' GROUP BY p.rowid, p.ref, p.label, p.description, p.price, p.pmp, p.price_ttc, p.price_base_type, p.fk_product_type, p.desiredstock, p.seuil_stock_alerte,'; +$sqlGroupBy .= ' p.tms, p.duration, p.tobuy, p.stock'; + +$parameters = array('sqlGroupBy' => $sqlGroupBy); +$reshook = $hookmanager->executeHooks('printFieldListGroupBy', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + +if ($reshook == 0) { + // Allows the hook to add things (old behavior) + $sql .= $hookmanager->resPrint; + // Allows the hook to REPLACE the clause (new behavior) + if (!empty($hookmanager->resArray['sqlGroupBy'])) { + $sqlGroupBy = $hookmanager->resArray['sqlGroupBy']; + } +} + +$sql .= $sqlGroupBy; + // Add where from hooks $parameters = array(); $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook