From 37a6ebd4317fb36e0b9ef1ae2bc944e3cb868bfd Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 17 Jun 2026 19:01:33 +0530 Subject: [PATCH] refactor(postgres): port POS Profile doctype queries to the query builder Co-Authored-By: Claude Opus 4.8 (1M context) --- .../doctype/pos_profile/pos_profile.py | 85 ++++++++++--------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/erpnext/accounts/doctype/pos_profile/pos_profile.py b/erpnext/accounts/doctype/pos_profile/pos_profile.py index 3b7801bf11e..e9676ac917e 100644 --- a/erpnext/accounts/doctype/pos_profile/pos_profile.py +++ b/erpnext/accounts/doctype/pos_profile/pos_profile.py @@ -118,14 +118,21 @@ class POSProfile(Document): def validate_default_profile(self): for row in self.applicable_for_users: - res = frappe.db.sql( - """select pf.name - from - `tabPOS Profile User` pfu, `tabPOS Profile` pf - where - pf.name = pfu.parent and pfu.user = %s and pf.name != %s and pf.company = %s - and pfu.default=1 and pf.disabled = 0""", - (row.user, self.name, self.company), + pfu = frappe.qb.DocType("POS Profile User") + pf = frappe.qb.DocType("POS Profile") + res = ( + frappe.qb.from_(pfu) + .inner_join(pf) + .on(pf.name == pfu.parent) + .select(pf.name) + .where( + (pfu.user == row.user) + & (pf.name != self.name) + & (pf.company == self.company) + & (pfu.default == 1) + & (pf.disabled == 0) + ) + .run() ) if row.default and res: @@ -265,10 +272,11 @@ def get_permitted_nodes(group_type): def get_child_nodes(group_type, root): lft, rgt = frappe.db.get_value(group_type, root, ["lft", "rgt"]) - return frappe.db.sql( - f""" Select name, lft, rgt from `tab{group_type}` where - lft >= {lft} and rgt <= {rgt} order by lft""", - as_dict=1, + return frappe.get_all( + group_type, + filters={"lft": [">=", lft], "rgt": ["<=", rgt]}, + fields=["name", "lft", "rgt"], + order_by="lft", ) @@ -278,40 +286,33 @@ def pos_profile_query(doctype: str, txt: str, searchfield: str, start: int, page user = frappe.session["user"] company = filters.get("company") or frappe.defaults.get_user_default("company") - args = { - "user": user, - "start": start, - "company": company, - "page_len": page_len, - "txt": "%%%s%%" % txt, - } + pf = frappe.qb.DocType("POS Profile") + pfu = frappe.qb.DocType("POS Profile User") - pos_profile = frappe.db.sql( - """select pf.name - from - `tabPOS Profile` pf, `tabPOS Profile User` pfu - where - pfu.parent = pf.name and pfu.user = %(user)s and pf.company = %(company)s - and (pf.name like %(txt)s) - and pf.disabled = 0 limit %(page_len)s offset %(start)s""", - args, + pos_profile = ( + frappe.qb.from_(pf) + .inner_join(pfu) + .on(pfu.parent == pf.name) + .select(pf.name) + .where((pfu.user == user) & (pf.company == company) & pf.name.like(f"%{txt}%") & (pf.disabled == 0)) + .limit(page_len) + .offset(start) + .run() ) if not pos_profile: - del args["user"] - - pos_profile = frappe.db.sql( - """select pf.name - from - `tabPOS Profile` pf left join `tabPOS Profile User` pfu - on - pf.name = pfu.parent - where - ifnull(pfu.user, '') = '' - and pf.company = %(company)s - and pf.name like %(txt)s - and pf.disabled = 0""", - args, + pos_profile = ( + frappe.qb.from_(pf) + .left_join(pfu) + .on(pf.name == pfu.parent) + .select(pf.name) + .where( + (pfu.user.isnull() | (pfu.user == "")) + & (pf.company == company) + & pf.name.like(f"%{txt}%") + & (pf.disabled == 0) + ) + .run() ) return pos_profile