From 03b06fc3ff1ab13956f53841c0f7b601a3703a57 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Mon, 30 Dec 2024 17:56:17 +0530 Subject: [PATCH 1/2] fix: add monthly distributation and write query in qb (cherry picked from commit 27195c7c9640b06eb548a8505dd6f83e623d552d) --- erpnext/accounts/doctype/budget/budget.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index 145480138d6..fa711e2edbf 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -490,13 +490,19 @@ def get_actual_expense(args): def get_accumulated_monthly_budget(monthly_distribution, posting_date, fiscal_year, annual_budget): distribution = {} if monthly_distribution: - for d in frappe.db.sql( - """select mdp.month, mdp.percentage_allocation - from `tabMonthly Distribution Percentage` mdp, `tabMonthly Distribution` md - where mdp.parent=md.name and md.fiscal_year=%s""", - fiscal_year, - as_dict=1, - ): + mdp = frappe.qb.DocType("Monthly Distribution Percentage") + md = frappe.qb.DocType("Monthly Distribution") + + query = ( + frappe.qb.from_(mdp) + .join(md) + .on(mdp.parent == md.name) + .select(mdp.month, mdp.percentage_allocation) + .where(md.fiscal_year == fiscal_year) + .where(md.name == monthly_distribution) + ) + + for d in query.run(as_dict=True): distribution.setdefault(d.month, d.percentage_allocation) dt = frappe.get_cached_value("Fiscal Year", fiscal_year, "year_start_date") From 2e67a3341228207bcdde6132d9faecbf77661674 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 31 Dec 2024 17:04:37 +0530 Subject: [PATCH 2/2] refactor: store result in variable before enumeration helps to inspect result while debugging (cherry picked from commit b60bd17d1ddc9cc61d9fba44eb495bec174d66b2) --- erpnext/accounts/doctype/budget/budget.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index fa711e2edbf..2f2b549cf46 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -493,16 +493,17 @@ def get_accumulated_monthly_budget(monthly_distribution, posting_date, fiscal_ye mdp = frappe.qb.DocType("Monthly Distribution Percentage") md = frappe.qb.DocType("Monthly Distribution") - query = ( + res = ( frappe.qb.from_(mdp) .join(md) .on(mdp.parent == md.name) .select(mdp.month, mdp.percentage_allocation) .where(md.fiscal_year == fiscal_year) .where(md.name == monthly_distribution) + .run(as_dict=True) ) - for d in query.run(as_dict=True): + for d in res: distribution.setdefault(d.month, d.percentage_allocation) dt = frappe.get_cached_value("Fiscal Year", fiscal_year, "year_start_date")