Skip to content

Commit 8658956

Browse files
fix: Trial Balance and Consolidated Trial Balance total row calculation (backport #53014) (#53015)
Co-authored-by: diptanilsaha <diptanil@frappe.io>
1 parent 862659e commit 8658956

3 files changed

Lines changed: 43 additions & 49 deletions

File tree

erpnext/accounts/report/consolidated_trial_balance/consolidated_trial_balance.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ frappe.query_reports["Consolidated Trial Balance"] = {
8686
fieldtype: "Check",
8787
default: 1,
8888
},
89+
{
90+
fieldname: "show_net_values",
91+
label: __("Show net values in opening and closing columns"),
92+
fieldtype: "Check",
93+
default: 1,
94+
},
8995
{
9096
fieldname: "show_group_accounts",
9197
label: __("Show Group Accounts"),

erpnext/accounts/report/consolidated_trial_balance/consolidated_trial_balance.py

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from erpnext.accounts.report.trial_balance.trial_balance import (
1616
accumulate_values_into_parents,
17+
calculate_total_row,
1718
calculate_values,
1819
get_opening_balances,
1920
hide_group_accounts,
@@ -44,7 +45,6 @@ def execute(filters: dict | None = None):
4445

4546
def validate_filters(filters):
4647
validate_companies(filters)
47-
filters.show_net_values = True
4848
tb_validate_filters(filters)
4949

5050

@@ -99,16 +99,20 @@ def get_data(filters) -> list[list]:
9999
tb_data = get_company_wise_tb_data(company_filter, reporting_currency, ignore_reporting_currency)
100100
consolidate_trial_balance_data(data, tb_data)
101101

102-
for d in data:
103-
prepare_opening_closing(d)
104-
105-
total_row = calculate_total_row(data, reporting_currency)
106-
107-
data.extend([{}, total_row])
102+
if filters.get("show_net_values"):
103+
prepare_opening_closing_for_ctb(data)
108104

109105
if not filters.get("show_group_accounts"):
110106
data = hide_group_accounts(data)
111107

108+
total_row = calculate_total_row(
109+
data, reporting_currency, show_group_accounts=filters.get("show_group_accounts")
110+
)
111+
112+
calculate_foreign_currency_translation_reserve(total_row, data, filters=filters)
113+
114+
data.extend([total_row])
115+
112116
if filters.get("presentation_currency"):
113117
update_to_presentation_currency(
114118
data,
@@ -207,10 +211,6 @@ def prepare_companywise_tb_data(accounts, filters, parent_children_map, reportin
207211
data = []
208212

209213
for d in accounts:
210-
# Prepare opening closing for group account
211-
if parent_children_map.get(d.account) and filters.get("show_net_values"):
212-
prepare_opening_closing(d)
213-
214214
has_value = False
215215
row = {
216216
"account": d.name,
@@ -242,35 +242,9 @@ def prepare_companywise_tb_data(accounts, filters, parent_children_map, reportin
242242
return data
243243

244244

245-
def calculate_total_row(data, reporting_currency):
246-
total_row = {
247-
"account": "'" + _("Total") + "'",
248-
"account_name": "'" + _("Total") + "'",
249-
"warn_if_negative": True,
250-
"opening_debit": 0.0,
251-
"opening_credit": 0.0,
252-
"debit": 0.0,
253-
"credit": 0.0,
254-
"closing_debit": 0.0,
255-
"closing_credit": 0.0,
256-
"parent_account": None,
257-
"indent": 0,
258-
"has_value": True,
259-
"currency": reporting_currency,
260-
}
261-
262-
for d in data:
263-
if not d.get("parent_account"):
264-
for field in value_fields:
265-
total_row[field] += d[field]
266-
267-
if data:
268-
calculate_foreign_currency_translation_reserve(total_row, data)
269-
270-
return total_row
271-
272-
273-
def calculate_foreign_currency_translation_reserve(total_row, data):
245+
def calculate_foreign_currency_translation_reserve(total_row, data, filters):
246+
if not data or not total_row:
247+
return
274248
opening_dr_cr_diff = total_row["opening_debit"] - total_row["opening_credit"]
275249
dr_cr_diff = total_row["debit"] - total_row["credit"]
276250

@@ -289,15 +263,16 @@ def calculate_foreign_currency_translation_reserve(total_row, data):
289263
"root_type": data[idx].get("root_type"),
290264
"account_type": "Equity",
291265
"parent_account": data[idx].get("account"),
292-
"indent": data[idx].get("indent") + 1,
266+
"indent": data[idx].get("indent") + 1 if filters.get("show_group_accounts") else 0,
293267
"has_value": True,
294268
"currency": total_row.get("currency"),
295269
}
296270

297271
fctr_row["closing_debit"] = fctr_row["opening_debit"] + fctr_row["debit"]
298272
fctr_row["closing_credit"] = fctr_row["opening_credit"] + fctr_row["credit"]
299273

300-
prepare_opening_closing(fctr_row)
274+
if filters.get("show_net_values"):
275+
prepare_opening_closing(fctr_row)
301276

302277
data.insert(idx + 1, fctr_row)
303278

@@ -396,6 +371,11 @@ def update_to_presentation_currency(data, from_currency, to_currency, date, igno
396371
d.update(currency=to_currency)
397372

398373

374+
def prepare_opening_closing_for_ctb(data):
375+
for d in data:
376+
prepare_opening_closing(d)
377+
378+
399379
def get_columns():
400380
return [
401381
{

erpnext/accounts/report/trial_balance/trial_balance.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def calculate_values(
390390
prepare_opening_closing(d)
391391

392392

393-
def calculate_total_row(accounts, company_currency):
393+
def calculate_total_row(data, company_currency, show_group_accounts=True):
394394
total_row = {
395395
"account": "'" + _("Total") + "'",
396396
"account_name": "'" + _("Total") + "'",
@@ -407,10 +407,16 @@ def calculate_total_row(accounts, company_currency):
407407
"currency": company_currency,
408408
}
409409

410-
for d in accounts:
411-
if not d.parent_account:
412-
for field in value_fields:
413-
total_row[field] += d[field]
410+
def sum_value_fields(row):
411+
for field in value_fields:
412+
total_row[field] += row[field]
413+
414+
for d in data:
415+
if not show_group_accounts:
416+
sum_value_fields(d)
417+
418+
elif show_group_accounts and not d.get("parent_account"):
419+
sum_value_fields(d)
414420

415421
return total_row
416422

@@ -456,11 +462,13 @@ def prepare_data(accounts, filters, parent_children_map, company_currency):
456462
row["has_value"] = has_value
457463
data.append(row)
458464

459-
total_row = calculate_total_row(accounts, company_currency)
460-
461465
if not filters.get("show_group_accounts"):
462466
data = hide_group_accounts(data)
463467

468+
total_row = calculate_total_row(
469+
data, company_currency, show_group_accounts=filters.get("show_group_accounts")
470+
)
471+
464472
data.extend([{}, total_row])
465473

466474
return data

0 commit comments

Comments
 (0)