Skip to content

Commit

Permalink
purchasesjournal: fix empty IN () SQL request
Browse files Browse the repository at this point in the history
`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) Dolibarr#2 {main} thrown in
	/var/www/html/core/db/pgsql.class.php on line 654

Fixes Dolibarr#32374
  • Loading branch information
alexandre-janniaux committed Jan 8, 2025
1 parent 8febf24 commit b9419a8
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions htdocs/accountancy/journal/purchasesjournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,34 +399,34 @@
}
}
*/

// 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);

$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';
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';
}
$i++;
}
$i++;
}
//var_dump($errorforinvoice);exit;



// Bookkeeping Write
if ($action == 'writebookkeeping' && !$error && $user->hasRight('accounting', 'bind', 'write')) {
Expand Down

0 comments on commit b9419a8

Please sign in to comment.