Skip to content

Commit

Permalink
refactor: merge separate loops for calculating group / leaf node totals
Browse files Browse the repository at this point in the history
rename function
remove return statement as the list is  mutated

(cherry picked from commit 1a3b9c5)
  • Loading branch information
akurungadam authored and mergify[bot] committed Jun 19, 2023
1 parent c6885e6 commit a53832e
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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):
Expand All @@ -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
):
Expand Down

0 comments on commit a53832e

Please sign in to comment.