From a09e8751095eb247ea689367992012c5b755e04f Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 17 Jun 2026 19:01:34 +0530 Subject: [PATCH] refactor(postgres): port Loyalty Point Entry doctype queries to the query builder Co-Authored-By: Claude Opus 4.8 (1M context) --- .../loyalty_point_entry.py | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/erpnext/accounts/doctype/loyalty_point_entry/loyalty_point_entry.py b/erpnext/accounts/doctype/loyalty_point_entry/loyalty_point_entry.py index 5b4c0d2df3f..d1c21973f5e 100644 --- a/erpnext/accounts/doctype/loyalty_point_entry/loyalty_point_entry.py +++ b/erpnext/accounts/doctype/loyalty_point_entry/loyalty_point_entry.py @@ -39,28 +39,32 @@ def get_loyalty_point_entries(customer, loyalty_program, company, expiry_date=No if not expiry_date: expiry_date = today() - return frappe.db.sql( - """ - select name, loyalty_points, expiry_date, loyalty_program_tier, invoice_type, invoice - from `tabLoyalty Point Entry` - where customer=%s and loyalty_program=%s - and expiry_date>=%s and loyalty_points>0 and company=%s - order by expiry_date - """, - (customer, loyalty_program, expiry_date, company), - as_dict=1, + return frappe.get_all( + "Loyalty Point Entry", + filters={ + "customer": customer, + "loyalty_program": loyalty_program, + "expiry_date": [">=", expiry_date], + "loyalty_points": [">", 0], + "company": company, + }, + fields=["name", "loyalty_points", "expiry_date", "loyalty_program_tier", "invoice_type", "invoice"], + order_by="expiry_date", ) def get_redemption_details(customer, loyalty_program, company): return frappe._dict( - frappe.db.sql( - """ - select redeem_against, sum(loyalty_points) - from `tabLoyalty Point Entry` - where customer=%s and loyalty_program=%s and loyalty_points<0 and company=%s - group by redeem_against - """, - (customer, loyalty_program, company), + frappe.get_all( + "Loyalty Point Entry", + filters={ + "customer": customer, + "loyalty_program": loyalty_program, + "loyalty_points": ["<", 0], + "company": company, + }, + fields=["redeem_against", {"SUM": "loyalty_points", "as": "loyalty_points"}], + group_by="redeem_against", + as_list=True, ) )