From f22969d2665bd47e1b9f6428f197025f52334d4c Mon Sep 17 00:00:00 2001 From: Anoop Kurungadam Date: Mon, 8 May 2023 20:41:38 +0530 Subject: [PATCH 1/4] fix: account group totals calculation to consider include_in_gross (cherry picked from commit 8dcb9302b417618505ea24e5566c017eff451c1e) --- .../gross_and_net_profit_report.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py index cd5f3667071..7f2877989bd 100644 --- a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py +++ b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py @@ -152,10 +152,17 @@ def adjust_account(data, period_list, consolidated=False): totals = {} for node in leaf_nodes: set_total(node, node["total"], data, totals) - for d in data: + + for d in reversed(data): for period in period_list: - key = period if consolidated else period.key + if d.get("is_group"): + # reset totals for group accounts as totals set by get_data doesn't consider include_in_gross check + d[period.key] = sum( + item[period.key] for item in data if item.get("parent_account") == d.get("account") + ) + d["total"] = totals[d["account"]] + return data @@ -170,7 +177,6 @@ def set_total(node, value, complete_list, totals): next(item for item in complete_list if item["account"] == parent), value, complete_list, totals ) - def get_profit( gross_income, gross_expense, period_list, company, profit_type, currency=None, consolidated=False ): From c6885e678944f406e9ee5adcf6e4ac882e9950da Mon Sep 17 00:00:00 2001 From: Anoop Kurungadam Date: Mon, 8 May 2023 20:48:43 +0530 Subject: [PATCH 2/4] refactor: remove unused parameters (cherry picked from commit 50822f207ec5272d4d71a2b6579693da2088105d) --- .../gross_and_net_profit_report.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py index 7f2877989bd..f2412819c4e 100644 --- a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py +++ b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py @@ -125,12 +125,12 @@ def get_revenue(data, period_list, include_in_gross=1): data_to_be_removed = True while data_to_be_removed: - revenue, data_to_be_removed = remove_parent_with_no_child(revenue, period_list) + revenue, data_to_be_removed = remove_parent_with_no_child(revenue) revenue = adjust_account(revenue, period_list) return copy.deepcopy(revenue) -def remove_parent_with_no_child(data, period_list): +def remove_parent_with_no_child(data): data_to_be_removed = False for parent in data: if "is_group" in parent and parent.get("is_group") == 1: @@ -147,7 +147,7 @@ def remove_parent_with_no_child(data, period_list): return data, data_to_be_removed -def adjust_account(data, period_list, consolidated=False): +def adjust_account(data, period_list): leaf_nodes = [item for item in data if item["is_group"] == 0] totals = {} for node in leaf_nodes: From a53832e16e45909cdf87d205b31c7bbd2a3400bd Mon Sep 17 00:00:00 2001 From: Anoop Kurungadam Date: Tue, 9 May 2023 09:16:10 +0530 Subject: [PATCH 3/4] refactor: merge separate loops for calculating group / leaf node totals rename function remove return statement as the list is mutated (cherry picked from commit 1a3b9c5bdfb9fac04a3a7d8724e6b3c3b593ec19) --- .../gross_and_net_profit_report.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py index f2412819c4e..a7b7f270cfd 100644 --- a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py +++ b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py @@ -126,7 +126,9 @@ def get_revenue(data, period_list, include_in_gross=1): data_to_be_removed = True while data_to_be_removed: revenue, data_to_be_removed = remove_parent_with_no_child(revenue) - revenue = adjust_account(revenue, period_list) + + adjust_account_totals(revenue, period_list) + return copy.deepcopy(revenue) @@ -147,23 +149,19 @@ def remove_parent_with_no_child(data): return data, data_to_be_removed -def adjust_account(data, period_list): - leaf_nodes = [item for item in data if item["is_group"] == 0] +def adjust_account_totals(data, period_list): totals = {} - for node in leaf_nodes: - set_total(node, node["total"], data, totals) - for d in reversed(data): - for period in period_list: - if d.get("is_group"): + if d.get("is_group"): + for period in period_list: # reset totals for group accounts as totals set by get_data doesn't consider include_in_gross check d[period.key] = sum( item[period.key] for item in data if item.get("parent_account") == d.get("account") ) + else: + set_total(d, d["total"], data, totals) - d["total"] = totals[d["account"]] - - return data + d["total"] = totals[d["account"]] def set_total(node, value, complete_list, totals): @@ -177,6 +175,7 @@ def set_total(node, value, complete_list, totals): next(item for item in complete_list if item["account"] == parent), value, complete_list, totals ) + def get_profit( gross_income, gross_expense, period_list, company, profit_type, currency=None, consolidated=False ): From e899c30428ce26e2a175708f968cccac49253eef Mon Sep 17 00:00:00 2001 From: Anoop Kurungadam Date: Wed, 10 May 2023 12:34:15 +0530 Subject: [PATCH 4/4] fix: add total col for gross and net profit (cherry picked from commit cb9b4fbb91f4b73916416167932064ef5965eed1) --- .../gross_and_net_profit_report.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py index a7b7f270cfd..f0ca405401d 100644 --- a/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py +++ b/erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py @@ -196,6 +196,9 @@ def get_profit( if profit_loss[key]: has_value = True + if not profit_loss.get("total"): + profit_loss["total"] = 0 + profit_loss["total"] += profit_loss[key] if has_value: return profit_loss @@ -234,6 +237,9 @@ def get_net_profit( if profit_loss[key]: has_value = True + if not profit_loss.get("total"): + profit_loss["total"] = 0 + profit_loss["total"] += profit_loss[key] if has_value: return profit_loss