fix: trial balance opening balance not showing in the debit side for the liability account (#17944)
diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py
index 6b18c5d..b5f0186 100644
--- a/erpnext/accounts/report/trial_balance/trial_balance.py
+++ b/erpnext/accounts/report/trial_balance/trial_balance.py
@@ -180,20 +180,28 @@
if d["root_type"] == "Asset" or d["root_type"] == "Equity" or d["root_type"] == "Expense":
d["opening_debit"] -= d["opening_credit"]
- d["opening_credit"] = 0.0
- total_row["opening_debit"] += d["opening_debit"]
+ d["closing_debit"] -= d["closing_credit"]
+
+ # For opening
+ check_opening_closing_has_negative_value(d, "opening_debit", "opening_credit")
+
+ # For closing
+ check_opening_closing_has_negative_value(d, "closing_debit", "closing_credit")
+
if d["root_type"] == "Liability" or d["root_type"] == "Income":
d["opening_credit"] -= d["opening_debit"]
- d["opening_debit"] = 0.0
- total_row["opening_credit"] += d["opening_credit"]
- if d["root_type"] == "Asset" or d["root_type"] == "Equity" or d["root_type"] == "Expense":
- d["closing_debit"] -= d["closing_credit"]
- d["closing_credit"] = 0.0
- total_row["closing_debit"] += d["closing_debit"]
- if d["root_type"] == "Liability" or d["root_type"] == "Income":
d["closing_credit"] -= d["closing_debit"]
- d["closing_debit"] = 0.0
- total_row["closing_credit"] += d["closing_credit"]
+
+ # For opening
+ check_opening_closing_has_negative_value(d, "opening_credit", "opening_debit")
+
+ # For closing
+ check_opening_closing_has_negative_value(d, "closing_credit", "closing_debit")
+
+ total_row["opening_debit"] += d["opening_debit"]
+ total_row["closing_debit"] += d["closing_debit"]
+ total_row["opening_credit"] += d["opening_credit"]
+ total_row["closing_credit"] += d["closing_credit"]
return total_row
@@ -219,8 +227,6 @@
if d.account_number else d.account_name)
}
- prepare_opening_and_closing(d)
-
for key in value_fields:
row[key] = flt(d.get(key, 0.0), 3)
@@ -295,22 +301,11 @@
}
]
-def prepare_opening_and_closing(d):
- d["closing_debit"] = d["opening_debit"] + d["debit"]
- d["closing_credit"] = d["opening_credit"] + d["credit"]
+def check_opening_closing_has_negative_value(d, dr_or_cr, switch_to_column):
+ # If opening debit has negetive value then move it to opening credit and vice versa.
- if d["root_type"] == "Asset" or d["root_type"] == "Equity" or d["root_type"] == "Expense":
- d["opening_debit"] -= d["opening_credit"]
- d["opening_credit"] = 0.0
-
- if d["root_type"] == "Liability" or d["root_type"] == "Income":
- d["opening_credit"] -= d["opening_debit"]
- d["opening_debit"] = 0.0
-
- if d["root_type"] == "Asset" or d["root_type"] == "Equity" or d["root_type"] == "Expense":
- d["closing_debit"] -= d["closing_credit"]
- d["closing_credit"] = 0.0
-
- if d["root_type"] == "Liability" or d["root_type"] == "Income":
- d["closing_credit"] -= d["closing_debit"]
- d["closing_debit"] = 0.0
+ if d[dr_or_cr] < 0:
+ d[switch_to_column] = abs(d[dr_or_cr])
+ d[dr_or_cr] = 0.0
+ else:
+ d[switch_to_column] = 0.0