mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-05 05:09:11 +00:00
refactor(account): Replace the SQL queries with qb and the frappe ORM (#55396)
This commit is contained in:
@@ -175,16 +175,19 @@ class Account(NestedSet):
|
|||||||
if cint(self.is_group):
|
if cint(self.is_group):
|
||||||
db_value = self.get_doc_before_save()
|
db_value = self.get_doc_before_save()
|
||||||
if db_value:
|
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:
|
if self.report_type != db_value.report_type:
|
||||||
frappe.db.sql(
|
query = query.set(Account.report_type, self.report_type)
|
||||||
"update `tabAccount` set report_type=%s where lft > %s and rgt < %s",
|
updated = True
|
||||||
(self.report_type, self.lft, self.rgt),
|
|
||||||
)
|
|
||||||
if self.root_type != db_value.root_type:
|
if self.root_type != db_value.root_type:
|
||||||
frappe.db.sql(
|
query = query.set(Account.root_type, self.root_type)
|
||||||
"update `tabAccount` set root_type=%s where lft > %s and rgt < %s",
|
updated = True
|
||||||
(self.root_type, self.lft, self.rgt),
|
|
||||||
)
|
if updated:
|
||||||
|
query.run()
|
||||||
|
|
||||||
if self.root_type and not self.report_type:
|
if self.root_type and not self.report_type:
|
||||||
self.report_type = (
|
self.report_type = (
|
||||||
@@ -449,11 +452,7 @@ class Account(NestedSet):
|
|||||||
return frappe.db.get_value("GL Entry", {"account": self.name})
|
return frappe.db.get_value("GL Entry", {"account": self.name})
|
||||||
|
|
||||||
def check_if_child_exists(self):
|
def check_if_child_exists(self):
|
||||||
return frappe.db.sql(
|
return frappe.db.exists("Account", {"parent_account": self.name, "docstatus": ["!=", 2]})
|
||||||
"""select name from `tabAccount` where parent_account = %s
|
|
||||||
and docstatus != 2""",
|
|
||||||
self.name,
|
|
||||||
)
|
|
||||||
|
|
||||||
def validate_mandatory(self):
|
def validate_mandatory(self):
|
||||||
if not self.root_type:
|
if not self.root_type:
|
||||||
@@ -473,14 +472,24 @@ class Account(NestedSet):
|
|||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@frappe.validate_and_sanitize_search_inputs
|
@frappe.validate_and_sanitize_search_inputs
|
||||||
def get_parent_account(doctype: str, txt: str, searchfield: str, start: int, page_len: int, filters: dict):
|
def get_parent_account(doctype: str, txt: str, searchfield: str, start: int, page_len: int, filters: dict):
|
||||||
return frappe.db.sql(
|
Account = frappe.qb.DocType("Account")
|
||||||
"""select name from tabAccount
|
|
||||||
where is_group = 1 and docstatus != 2 and company = {}
|
search_field_obj = getattr(Account, searchfield)
|
||||||
and {} like {} order by name limit {} offset {}""".format("%s", searchfield, "%s", "%s", "%s"),
|
|
||||||
(filters["company"], "%%%s%%" % txt, page_len, start),
|
query = (
|
||||||
as_list=1,
|
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):
|
def get_account_currency(account):
|
||||||
"""Helper function to get account currency"""
|
"""Helper function to get account currency"""
|
||||||
|
|||||||
Reference in New Issue
Block a user