From b9419a869c43ced535cf41877f563cdffb6fd1e2 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Wed, 8 Jan 2025 13:47:18 +0100 Subject: [PATCH] purchasesjournal: fix empty IN () SQL request `WHERE xx IN ()` is not allowed in PostgreSQL queries, and result in the following fatal error: GROUP BY fk_facture_fourn 33 DoliDBPgsql::query SQL Error message: ERROR: 42601: syntax error at or near ")" LINE 10: AND fk_facture_fourn IN () ^ We can check whether we have valid invoices before running the query, since the query will only check whether the invoices are complete or not. It also fixes the following error on the development PHP output. Fatal error: Uncaught TypeError: pg_num_rows(): Argument #1 ($result) must be of type PgSql\Result, bool given in /var/www/html/core/db/pgsql.class.php:654 Stack trace: #0 /var/www/html/core/db/pgsql.class.php(654): pg_num_rows(false) #1 /var/www/html/accountancy/journal/purchasesjournal.php(418): DoliDBPgsql->num_rows(false) #2 {main} thrown in /var/www/html/core/db/pgsql.class.php on line 654 Fixes #32374 --- .../accountancy/journal/purchasesjournal.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index 1c64cc8934a..437b436b733 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -399,34 +399,34 @@ foreach ($tabfac as $key => $val) { // Loop on each invoice } } */ + // New way, single query, load all unbound lines -$sql = " -SELECT - fk_facture_fourn, - COUNT(fd.rowid) as nb -FROM - " . MAIN_DB_PREFIX . "facture_fourn_det as fd -WHERE - fd.product_type <= 2 - AND fd.fk_code_ventilation <= 0 - AND fd.total_ttc <> 0 - AND fk_facture_fourn IN (".$db->sanitize(implode(",", array_keys($tabfac))).") -GROUP BY fk_facture_fourn -"; -$resql = $db->query($sql); +if (!empty($tabfac)) { + $sql = " + SELECT + fk_facture_fourn, + COUNT(fd.rowid) as nb + FROM + " . MAIN_DB_PREFIX . "facture_fourn_det as fd + WHERE + fd.product_type <= 2 + AND fd.fk_code_ventilation <= 0 + AND fd.total_ttc <> 0 + AND fk_facture_fourn IN (".$db->sanitize(implode(",", array_keys($tabfac))).") + GROUP BY fk_facture_fourn + "; + $resql = $db->query($sql); -$num = $db->num_rows($resql); -$i = 0; -while ($i < $num) { - $obj = $db->fetch_object($resql); - if ($obj->nb > 0) { - $errorforinvoice[$obj->fk_facture_fourn] = 'somelinesarenotbound'; + $num = $db->num_rows($resql); + $i = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + if ($obj->nb > 0) { + $errorforinvoice[$obj->fk_facture_fourn] = 'somelinesarenotbound'; + } + $i++; } - $i++; } -//var_dump($errorforinvoice);exit; - - // Bookkeeping Write if ($action == 'writebookkeeping' && !$error && $user->hasRight('accounting', 'bind', 'write')) {