refactor(account): Replace the SQL queries with qb and the frappe ORM (#55396)

This commit is contained in:
Loic Oberle
2026-05-29 10:03:02 +02:00
committed by GitHub
parent 3e9c4aefaf
commit d4f8c033fc

View File

@@ -175,16 +175,19 @@ class Account(NestedSet):
if cint(self.is_group):
db_value = self.get_doc_before_save()
if db_value:
Account = frappe.qb.DocType("Account")
query = frappe.qb.update(Account).where((Account.lft > self.lft) & (Account.rgt < self.rgt))
updated = False
if self.report_type != db_value.report_type:
frappe.db.sql(
"update `tabAccount` set report_type=%s where lft > %s and rgt < %s",
(self.report_type, self.lft, self.rgt),
)
query = query.set(Account.report_type, self.report_type)
updated = True
if self.root_type != db_value.root_type:
frappe.db.sql(
"update `tabAccount` set root_type=%s where lft > %s and rgt < %s",
(self.root_type, self.lft, self.rgt),
)
query = query.set(Account.root_type, self.root_type)
updated = True
if updated:
query.run()
if self.root_type and not self.report_type:
self.report_type = (
@@ -449,11 +452,7 @@ class Account(NestedSet):
return frappe.db.get_value("GL Entry", {"account": self.name})
def check_if_child_exists(self):
return frappe.db.sql(
"""select name from `tabAccount` where parent_account = %s
and docstatus != 2""",
self.name,
)
return frappe.db.exists("Account", {"parent_account": self.name, "docstatus": ["!=", 2]})
def validate_mandatory(self):
if not self.root_type:
@@ -473,14 +472,24 @@ class Account(NestedSet):
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def get_parent_account(doctype: str, txt: str, searchfield: str, start: int, page_len: int, filters: dict):
return frappe.db.sql(
"""select name from tabAccount
where is_group = 1 and docstatus != 2 and company = {}
and {} like {} order by name limit {} offset {}""".format("%s", searchfield, "%s", "%s", "%s"),
(filters["company"], "%%%s%%" % txt, page_len, start),
as_list=1,
Account = frappe.qb.DocType("Account")
search_field_obj = getattr(Account, searchfield)
query = (
frappe.qb.from_(Account)
.select(Account.name)
.where(Account.is_group == 1)
.where(Account.docstatus != 2)
.where(Account.company == filters["company"])
.where(search_field_obj.like(f"%{txt}%"))
.order_by(Account.name)
.limit(page_len)
.offset(start)
)
return query.run(as_list=1)
def get_account_currency(account):
"""Helper function to get account currency"""