From dd8c3d5462c97f34d03f76f6e797391ad401b56d Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Wed, 12 Jul 2023 10:00:18 +0530 Subject: [PATCH 1/3] feat: filter based on accounting dimension in profitability analysis --- .../profitability_analysis.js | 15 ++++++++++++++- .../profitability_analysis.py | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.js b/erpnext/accounts/report/profitability_analysis/profitability_analysis.js index 889ede5a82..4412212d2e 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.js +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.js @@ -16,10 +16,23 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "fieldname": "based_on", "label": __("Based On"), "fieldtype": "Select", - "options": ["Cost Center", "Project"], + "options": ["Cost Center", "Project", "Accounting Dimension"], "default": "Cost Center", "reqd": 1 }, + { + "fieldname": "accounting_dimension", + "label": __("Accounting Dimension"), + "fieldtype": "Link", + "options": "Accounting Dimension", + "get_query": () =>{ + return { + filters: { + "disabled": 0 + } + } + } + }, { "fieldname": "fiscal_year", "label": __("Fiscal Year"), diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py index 183e279fe5..c05aa94457 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py @@ -16,8 +16,8 @@ value_fields = ("income", "expense", "gross_profit_loss") def execute(filters=None): - if not filters.get("based_on"): - filters["based_on"] = "Cost Center" + if filters.get("based_on") == "Accounting Dimension" and not filters.get("accounting_dimension"): + frappe.throw(_("Select Accounting Dimension.")) based_on = filters.based_on.replace(" ", "_").lower() validate_filters(filters) @@ -37,6 +37,8 @@ def get_accounts_data(based_on, company): ) elif based_on == "project": return frappe.get_all("Project", fields=["name"], filters={"company": company}, order_by="name") + elif based_on == "accounting_dimension": + return frappe.get_all("Accounting Dimension", fields=["name"], order_by="name") else: filters = {} doctype = frappe.unscrub(based_on) @@ -60,7 +62,9 @@ def get_data(accounts, filters, based_on): filters.get("company"), filters.get("from_date"), filters.get("to_date"), - based_on, + based_on + if based_on != "accounting_dimension" + else filters.accounting_dimension.replace(" ", "_").lower(), gl_entries_by_account, ignore_closing_entries=not flt(filters.get("with_period_closing_entry")), ) From 21c993a7b3129696ac4efa3d2dd2ac3f4d3a5ec2 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Wed, 12 Jul 2023 20:32:08 +0530 Subject: [PATCH 2/3] fix: clear accounting dimension value when based on field changes --- .../profitability_analysis/profitability_analysis.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.js b/erpnext/accounts/report/profitability_analysis/profitability_analysis.js index 4412212d2e..b45fe6fe5d 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.js +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.js @@ -18,7 +18,15 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "fieldtype": "Select", "options": ["Cost Center", "Project", "Accounting Dimension"], "default": "Cost Center", - "reqd": 1 + "reqd": 1, + "on_change": function(query_report){ + let based_on = query_report.get_values().based_on; + if(based_on!='Accounting Dimension'){ + frappe.query_report.set_filter_value({ + accounting_dimension: '' + }); + } + } }, { "fieldname": "accounting_dimension", From 1c5c310f5a16fb87e5c741156ad7dc567cbd8400 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 21 Jul 2023 13:37:48 +0530 Subject: [PATCH 3/3] fix: fetch acc dimension fieldname --- .../profitability_analysis.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py index c05aa94457..3d6e9b5428 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py @@ -6,6 +6,7 @@ import frappe from frappe import _ from frappe.utils import cstr, flt +from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions from erpnext.accounts.report.financial_statements import ( filter_accounts, filter_out_zero_value_rows, @@ -19,7 +20,9 @@ def execute(filters=None): if filters.get("based_on") == "Accounting Dimension" and not filters.get("accounting_dimension"): frappe.throw(_("Select Accounting Dimension.")) - based_on = filters.based_on.replace(" ", "_").lower() + based_on = ( + filters.based_on if filters.based_on != "Accounting Dimension" else filters.accounting_dimension + ) validate_filters(filters) accounts = get_accounts_data(based_on, filters.get("company")) data = get_data(accounts, filters, based_on) @@ -28,17 +31,15 @@ def execute(filters=None): def get_accounts_data(based_on, company): - if based_on == "cost_center": + if based_on == "Cost Center": return frappe.db.sql( """select name, parent_cost_center as parent_account, cost_center_name as account_name, lft, rgt from `tabCost Center` where company=%s order by name""", company, as_dict=True, ) - elif based_on == "project": + elif based_on == "Project": return frappe.get_all("Project", fields=["name"], filters={"company": company}, order_by="name") - elif based_on == "accounting_dimension": - return frappe.get_all("Accounting Dimension", fields=["name"], order_by="name") else: filters = {} doctype = frappe.unscrub(based_on) @@ -58,13 +59,17 @@ def get_data(accounts, filters, based_on): gl_entries_by_account = {} + accounting_dimensions = get_dimensions(with_cost_center_and_project=True)[0] + fieldname = "" + for dimension in accounting_dimensions: + if dimension["document_type"] == based_on: + fieldname = dimension["fieldname"] + set_gl_entries_by_account( filters.get("company"), filters.get("from_date"), filters.get("to_date"), - based_on - if based_on != "accounting_dimension" - else filters.accounting_dimension.replace(" ", "_").lower(), + fieldname, gl_entries_by_account, ignore_closing_entries=not flt(filters.get("with_period_closing_entry")), )