From 297153264b694324011c71bee49a83fbc0439b8c Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 23 Jun 2026 17:49:12 +0530 Subject: [PATCH] fix(accounts): guard last-GLE exchange-rate division against a zero divisor (Postgres) calculate_exchange_rate_using_last_gle divides (debit - credit) by (debit_in_account_currency - credit_in_account_currency). The GL row is re-selected by (voucher_type, voucher_no, account) ordered by posting_date WITHOUT the "(debit_in_account_currency > 0) | (credit_in_account_currency > 0)" filter the first query used, so the chosen row can have equal/zero account- currency amounts, making the divisor 0. MariaDB returns NULL for x/0 (the caller maps it via `or 0.0`); PostgreSQL raises `division by zero` and aborts. Wrapping the divisor in NullIf(divisor, 0) yields NULL on both engines, so MariaDB output is unchanged and PostgreSQL no longer errors. --- .../exchange_rate_revaluation/exchange_rate_revaluation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py index a5d5b5b3be1..d9ddf9290c5 100644 --- a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py +++ b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py @@ -605,7 +605,10 @@ def calculate_exchange_rate_using_last_gle(company, account, party_type, party): last_exchange_rate = ( qb.from_(gl) - .select((gl.debit - gl.credit) / (gl.debit_in_account_currency - gl.credit_in_account_currency)) + .select( + (gl.debit - gl.credit) + / NullIf(gl.debit_in_account_currency - gl.credit_in_account_currency, 0) + ) .where( (gl.voucher_type == voucher_type) & (gl.voucher_no == voucher_no) & (gl.account == account) )