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
This commit is contained in:
Alexandre Janniaux 2025-01-08 13:47:18 +01:00
parent 8febf249ce
commit b9419a869c

View file

@ -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')) {