refactor(accounting): replace sql with qb in diverse accounting-related files (#55416)

This commit is contained in:
Loic Oberle
2026-06-03 11:49:24 +02:00
committed by GitHub
parent 8b916b40ee
commit d2d28c9e03
3 changed files with 63 additions and 58 deletions

View File

@@ -198,21 +198,9 @@ def add_dimension_to_budget_doctype(df, doc):
def delete_accounting_dimension(doc): def delete_accounting_dimension(doc):
doclist = get_doctypes_with_dimensions() doclist = get_doctypes_with_dimensions()
frappe.db.sql( frappe.db.delete("Custom Field", filters={"fieldname": doc.fieldname, "dt": ["in", doclist]})
"""
DELETE FROM `tabCustom Field`
WHERE fieldname = {}
AND dt IN ({})""".format("%s", ", ".join(["%s"] * len(doclist))), # nosec
tuple([doc.fieldname, *doclist]),
)
frappe.db.sql( frappe.db.delete("Property Setter", filters={"field_name": doc.fieldname, "doc_type": ["in", doclist]})
"""
DELETE FROM `tabProperty Setter`
WHERE field_name = {}
AND doc_type IN ({})""".format("%s", ", ".join(["%s"] * len(doclist))), # nosec
tuple([doc.fieldname, *doclist]),
)
budget_against_property = frappe.get_doc("Property Setter", "Budget-budget_against-options") budget_against_property = frappe.get_doc("Property Setter", "Budget-budget_against-options")
value_list = budget_against_property.value.split("\n")[3:] value_list = budget_against_property.value.split("\n")[3:]
@@ -273,13 +261,27 @@ def get_accounting_dimensions(as_list=True):
def get_checks_for_pl_and_bs_accounts(): def get_checks_for_pl_and_bs_accounts():
return frappe.db.sql( AccountingDimension = frappe.qb.DocType("Accounting Dimension")
"""SELECT p.label, p.disabled, p.fieldname, c.default_dimension, c.company, c.mandatory_for_pl, c.mandatory_for_bs AccountingDimensionDetail = frappe.qb.DocType("Accounting Dimension Detail")
FROM `tabAccounting Dimension`p ,`tabAccounting Dimension Detail` c
WHERE p.name = c.parent AND p.disabled = 0""", query = (
as_dict=1, frappe.qb.from_(AccountingDimension)
.join(AccountingDimensionDetail)
.on(AccountingDimension.name == AccountingDimensionDetail.parent)
.select(
AccountingDimension.label,
AccountingDimension.disabled,
AccountingDimension.fieldname,
AccountingDimensionDetail.default_dimension,
AccountingDimensionDetail.company,
AccountingDimensionDetail.mandatory_for_pl,
AccountingDimensionDetail.mandatory_for_bs,
)
.where(AccountingDimension.disabled == 0)
) )
return query.run(as_dict=1)
def get_dimension_with_children(doctype, dimensions): def get_dimension_with_children(doctype, dimensions):
if isinstance(dimensions, str): if isinstance(dimensions, str):

View File

@@ -43,18 +43,19 @@ class AccountingDimensionFilter(Document):
self.validate_applicable_accounts() self.validate_applicable_accounts()
def validate_applicable_accounts(self): def validate_applicable_accounts(self):
accounts = frappe.db.sql( ApplicableOnAccount = frappe.qb.DocType("Applicable On Account")
""" AccountingDimensionFilter = frappe.qb.DocType("Accounting Dimension Filter")
SELECT a.applicable_on_account as account
FROM `tabApplicable On Account` a, `tabAccounting Dimension Filter` d query = (
WHERE d.name = a.parent frappe.qb.from_(ApplicableOnAccount)
and d.name != %s .join(AccountingDimensionFilter)
and d.accounting_dimension = %s .on(AccountingDimensionFilter.name == ApplicableOnAccount.parent)
""", .select(ApplicableOnAccount.applicable_on_account.as_("account"))
(self.name, self.accounting_dimension), .where(AccountingDimensionFilter.name != self.name)
as_dict=1, .where(AccountingDimensionFilter.accounting_dimension == self.accounting_dimension)
) )
accounts = query.run(as_dict=1)
account_list = [d.account for d in accounts] account_list = [d.account for d in accounts]
for account in self.get("accounts"): for account in self.get("accounts"):
@@ -69,22 +70,28 @@ class AccountingDimensionFilter(Document):
def get_dimension_filter_map(): def get_dimension_filter_map():
filters = frappe.db.sql( ApplicableOnAccount = frappe.qb.DocType("Applicable On Account")
""" AccountingDimensionFilter = frappe.qb.DocType("Accounting Dimension Filter")
SELECT AllowedDimension = frappe.qb.DocType("Allowed Dimension")
a.applicable_on_account, d.dimension_value, p.accounting_dimension,
p.allow_or_restrict, p.fieldname, a.is_mandatory query = (
FROM frappe.qb.from_(AccountingDimensionFilter)
`tabApplicable On Account` a, .join(ApplicableOnAccount)
`tabAccounting Dimension Filter` p .on(AccountingDimensionFilter.name == ApplicableOnAccount.parent)
LEFT JOIN `tabAllowed Dimension` d ON d.parent = p.name .left_join(AllowedDimension)
WHERE .on(AllowedDimension.parent == AccountingDimensionFilter.name)
p.name = a.parent .select(
AND p.disabled = 0 ApplicableOnAccount.applicable_on_account,
""", AllowedDimension.dimension_value,
as_dict=1, AccountingDimensionFilter.accounting_dimension,
AccountingDimensionFilter.allow_or_restrict,
AccountingDimensionFilter.fieldname,
ApplicableOnAccount.is_mandatory,
)
.where(AccountingDimensionFilter.disabled == 0)
) )
filters = query.run(as_dict=1)
dimension_filter_map = {} dimension_filter_map = {}
for f in filters: for f in filters:

View File

@@ -46,23 +46,19 @@ class AccountingPeriod(Document):
self.name = " - ".join([self.period_name, company_abbr]) self.name = " - ".join([self.period_name, company_abbr])
def validate_overlap(self): def validate_overlap(self):
existing_accounting_period = frappe.db.sql( AccountingPeriod = frappe.qb.DocType("Accounting Period")
"""select name from `tabAccounting Period`
where ( query = (
(%(start_date)s between start_date and end_date) frappe.qb.from_(AccountingPeriod)
or (%(end_date)s between start_date and end_date) .select(AccountingPeriod.name)
or (start_date between %(start_date)s and %(end_date)s) .where(AccountingPeriod.start_date <= self.end_date)
or (end_date between %(start_date)s and %(end_date)s) .where(AccountingPeriod.end_date >= self.start_date)
) and name!=%(name)s and company=%(company)s""", .where(AccountingPeriod.name != self.name)
{ .where(AccountingPeriod.company == self.company)
"start_date": self.start_date,
"end_date": self.end_date,
"name": self.name,
"company": self.company,
},
as_dict=True,
) )
existing_accounting_period = query.run(as_dict=True)
if len(existing_accounting_period) > 0: if len(existing_accounting_period) > 0:
frappe.throw( frappe.throw(
_("Accounting Period overlaps with {0}").format(existing_accounting_period[0].get("name")), _("Accounting Period overlaps with {0}").format(existing_accounting_period[0].get("name")),