mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-12 17:23:38 +00:00
Merge pull request #52644 from ruthra-kumar/use_query_builder_for_profitability_analysis
refactor: use query builder for profitability analysis
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
|
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _, qb
|
||||||
|
from frappe.query_builder import Criterion
|
||||||
from frappe.utils import cstr, flt
|
from frappe.utils import cstr, flt
|
||||||
|
|
||||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
|
||||||
@@ -33,11 +34,19 @@ def execute(filters=None):
|
|||||||
|
|
||||||
def get_accounts_data(based_on, company):
|
def get_accounts_data(based_on, company):
|
||||||
if based_on == "Cost Center":
|
if based_on == "Cost Center":
|
||||||
return frappe.db.sql(
|
cc = qb.DocType("Cost Center")
|
||||||
"""select name, parent_cost_center as parent_account, cost_center_name as account_name, lft, rgt
|
return (
|
||||||
from `tabCost Center` where company=%s order by name""",
|
qb.from_(cc)
|
||||||
company,
|
.select(
|
||||||
as_dict=True,
|
cc.name,
|
||||||
|
cc.parent_cost_center.as_("parent_account"),
|
||||||
|
cc.cost_center_name.as_("account_name"),
|
||||||
|
cc.lft,
|
||||||
|
cc.rgt,
|
||||||
|
)
|
||||||
|
.where(cc.company.eq(company))
|
||||||
|
.orderby(cc.name)
|
||||||
|
.run(as_dict=True)
|
||||||
)
|
)
|
||||||
elif based_on == "Project":
|
elif based_on == "Project":
|
||||||
return frappe.get_all("Project", fields=["name"], filters={"company": company}, order_by="name")
|
return frappe.get_all("Project", fields=["name"], filters={"company": company}, order_by="name")
|
||||||
@@ -206,27 +215,38 @@ def set_gl_entries_by_account(
|
|||||||
company, from_date, to_date, based_on, gl_entries_by_account, ignore_closing_entries=False
|
company, from_date, to_date, based_on, gl_entries_by_account, ignore_closing_entries=False
|
||||||
):
|
):
|
||||||
"""Returns a dict like { "account": [gl entries], ... }"""
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
||||||
additional_conditions = []
|
gl = qb.DocType("GL Entry")
|
||||||
|
acc = qb.DocType("Account")
|
||||||
|
|
||||||
|
conditions = []
|
||||||
|
conditions.append(gl.company.eq(company))
|
||||||
|
conditions.append(gl[based_on].notnull())
|
||||||
|
conditions.append(gl.is_cancelled.eq(0))
|
||||||
|
|
||||||
|
if from_date and to_date:
|
||||||
|
conditions.append(gl.posting_date.between(from_date, to_date))
|
||||||
|
elif from_date and not to_date:
|
||||||
|
conditions.append(gl.posting_date.gte(from_date))
|
||||||
|
elif not from_date and to_date:
|
||||||
|
conditions.append(gl.posting_date.lte(to_date))
|
||||||
|
|
||||||
if ignore_closing_entries:
|
if ignore_closing_entries:
|
||||||
additional_conditions.append("and voucher_type !='Period Closing Voucher'")
|
conditions.append(gl.voucher_type.ne("Period Closing Voucher"))
|
||||||
|
|
||||||
if from_date:
|
root_subquery = qb.from_(acc).select(acc.root_type).where(acc.name.eq(gl.account))
|
||||||
additional_conditions.append("and posting_date >= %(from_date)s")
|
gl_entries = (
|
||||||
|
qb.from_(gl)
|
||||||
gl_entries = frappe.db.sql(
|
.select(
|
||||||
"""select posting_date, {based_on} as based_on, debit, credit,
|
gl.posting_date,
|
||||||
is_opening, (select root_type from `tabAccount` where name = account) as type
|
gl[based_on].as_("based_on"),
|
||||||
from `tabGL Entry` where company=%(company)s
|
gl.debit,
|
||||||
{additional_conditions}
|
gl.credit,
|
||||||
and posting_date <= %(to_date)s
|
gl.is_opening,
|
||||||
and {based_on} is not null
|
root_subquery.as_("type"),
|
||||||
and is_cancelled = 0
|
)
|
||||||
order by {based_on}, posting_date""".format(
|
.where(Criterion.all(conditions))
|
||||||
additional_conditions="\n".join(additional_conditions), based_on=based_on
|
.orderby(gl[based_on], gl.posting_date)
|
||||||
),
|
.run(as_dict=True)
|
||||||
{"company": company, "from_date": from_date, "to_date": to_date},
|
|
||||||
as_dict=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for entry in gl_entries:
|
for entry in gl_entries:
|
||||||
|
|||||||
Reference in New Issue
Block a user