fix: Gross and Net Profit Report - incorrect calculation of totals

* fix: account group totals calculation to consider include_in_gross

(cherry picked from commit 8dcb9302b417618505ea24e5566c017eff451c1e)

* refactor: remove unused parameters

(cherry picked from commit 50822f207ec5272d4d71a2b6579693da2088105d)

* 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)

* fix: add total col for gross and net profit

(cherry picked from commit cb9b4fbb91f4b73916416167932064ef5965eed1)

---------

Co-authored-by: Anoop Kurungadam <anoop@earthianslive.com>
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 cd5f366..f0ca405 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,14 @@
 
 	data_to_be_removed = True
 	while data_to_be_removed:
-		revenue, data_to_be_removed = remove_parent_with_no_child(revenue, period_list)
-	revenue = adjust_account(revenue, period_list)
+		revenue, data_to_be_removed = remove_parent_with_no_child(revenue)
+
+	adjust_account_totals(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,16 +149,19 @@
 	return data, data_to_be_removed
 
 
-def adjust_account(data, period_list, consolidated=False):
-	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 data:
-		for period in period_list:
-			key = period if consolidated else period.key
-			d["total"] = totals[d["account"]]
-	return data
+	for d in reversed(data):
+		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"]]
 
 
 def set_total(node, value, complete_list, totals):
@@ -191,6 +196,9 @@
 
 		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
@@ -229,6 +237,9 @@
 
 		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