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.
This commit is contained in:
Mihir Kandoi
2026-06-23 17:49:12 +05:30
parent c79febf403
commit 297153264b

View File

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