From 595a4c8517a593c498e70b58e6b2eff23f10f4e1 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 17 Jun 2026 15:18:50 +0530 Subject: [PATCH] fix(postgres): replace MySQL IF() with Case in Cheques and Deposits report Co-Authored-By: Claude Opus 4.8 (1M context) --- ...heques_and_deposits_incorrectly_cleared.py | 8 ++++--- ...heques_and_deposits_incorrectly_cleared.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/test_cheques_and_deposits_incorrectly_cleared.py diff --git a/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/cheques_and_deposits_incorrectly_cleared.py b/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/cheques_and_deposits_incorrectly_cleared.py index 891dc2c4bb1..85245d2f89a 100644 --- a/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/cheques_and_deposits_incorrectly_cleared.py +++ b/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/cheques_and_deposits_incorrectly_cleared.py @@ -3,7 +3,7 @@ import frappe from frappe import _, qb -from frappe.query_builder import CustomFunction +from frappe.query_builder import Case from frappe.query_builder.custom import ConstantColumn @@ -93,7 +93,6 @@ def get_amounts_not_reflected_in_system_for_bank_reconciliation_statement(filter .run(as_dict=1) ) - ifelse = CustomFunction("IF", ["condition", "then", "else"]) pe = qb.DocType("Payment Entry") doctype_name = ConstantColumn("Payment Entry") payments = ( @@ -101,7 +100,10 @@ def get_amounts_not_reflected_in_system_for_bank_reconciliation_statement(filter .select( doctype_name.as_("doctype"), pe.name, - ifelse(pe.paid_from.eq(filters.account), pe.paid_amount, pe.received_amount).as_("amount"), + Case() + .when(pe.paid_from.eq(filters.account), pe.paid_amount) + .else_(pe.received_amount) + .as_("amount"), pe.payment_type, pe.party_type, pe.posting_date, diff --git a/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/test_cheques_and_deposits_incorrectly_cleared.py b/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/test_cheques_and_deposits_incorrectly_cleared.py new file mode 100644 index 00000000000..9c18880dcd5 --- /dev/null +++ b/erpnext/accounts/report/cheques_and_deposits_incorrectly_cleared/test_cheques_and_deposits_incorrectly_cleared.py @@ -0,0 +1,24 @@ +# Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +import frappe +from frappe.utils import nowdate + +from erpnext.accounts.report.cheques_and_deposits_incorrectly_cleared.cheques_and_deposits_incorrectly_cleared import ( + execute, +) +from erpnext.tests.utils import ERPNextTestSuite + + +class TestChequesAndDepositsIncorrectlyCleared(ERPNextTestSuite): + def test_report_executes_with_case_amount(self): + # Exercises the Payment Entry branch whose amount column uses a db-aware CASE expression + # (previously a MySQL-only IF()). IF() does not compile on postgres, so running the report + # query guards the portability fix on both databases. + company = frappe.db.get_value("Company", {}, "name") + account = frappe.db.get_value( + "Account", {"account_type": "Bank", "company": company, "is_group": 0}, "name" + ) + columns, data = execute(frappe._dict({"account": account, "report_date": nowdate()})) + self.assertTrue(columns) + self.assertIsInstance(data, list)