Merge pull request #39048 from barredterra/fix-pl-balance-sheet

fix: calculation of P/L in balance sheet
diff --git a/erpnext/accounts/report/balance_sheet/balance_sheet.py b/erpnext/accounts/report/balance_sheet/balance_sheet.py
index 5d6ca23..d45dc07 100644
--- a/erpnext/accounts/report/balance_sheet/balance_sheet.py
+++ b/erpnext/accounts/report/balance_sheet/balance_sheet.py
@@ -124,11 +124,11 @@
 			key = period if consolidated else period.key
 			effective_liability = 0.0
 			if liability:
-				effective_liability += flt(liability[-2].get(key))
+				effective_liability += flt(liability[0].get(key))
 			if equity:
-				effective_liability += flt(equity[-2].get(key))
+				effective_liability += flt(equity[0].get(key))
 
-			provisional_profit_loss[key] = flt(asset[-2].get(key)) - effective_liability
+			provisional_profit_loss[key] = flt(asset[0].get(key)) - effective_liability
 			total_row[key] = effective_liability + provisional_profit_loss[key]
 
 			if provisional_profit_loss[key]:
@@ -193,11 +193,11 @@
 	for period in period_list:
 		key = period if consolidated else period.key
 		if asset:
-			net_asset += asset[-2].get(key)
+			net_asset += asset[0].get(key)
 		if liability:
-			net_liability += liability[-2].get(key)
+			net_liability += liability[0].get(key)
 		if equity:
-			net_equity += equity[-2].get(key)
+			net_equity += equity[0].get(key)
 		if provisional_profit_loss:
 			net_provisional_profit_loss += provisional_profit_loss.get(key)
 
diff --git a/erpnext/accounts/report/balance_sheet/test_balance_sheet.py b/erpnext/accounts/report/balance_sheet/test_balance_sheet.py
index 3cb6efe..7c67ecb 100644
--- a/erpnext/accounts/report/balance_sheet/test_balance_sheet.py
+++ b/erpnext/accounts/report/balance_sheet/test_balance_sheet.py
@@ -3,49 +3,135 @@
 
 import frappe
 from frappe.tests.utils import FrappeTestCase
-from frappe.utils import today
+from frappe.utils.data import today
 
 from erpnext.accounts.report.balance_sheet.balance_sheet import execute
 
+COMPANY = "_Test Company 6"
+COMPANY_SHORT_NAME = "_TC6"
+
+test_dependencies = ["Company"]
+
 
 class TestBalanceSheet(FrappeTestCase):
 	def test_balance_sheet(self):
-		from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
-		from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import (
-			create_sales_invoice,
-			make_sales_invoice,
-		)
-		from erpnext.accounts.utils import get_fiscal_year
+		frappe.db.sql(f"delete from `tabJournal Entry` where company='{COMPANY}'")
+		frappe.db.sql(f"delete from `tabGL Entry` where company='{COMPANY}'")
 
-		frappe.db.sql("delete from `tabPurchase Invoice` where company='_Test Company 6'")
-		frappe.db.sql("delete from `tabSales Invoice` where company='_Test Company 6'")
-		frappe.db.sql("delete from `tabGL Entry` where company='_Test Company 6'")
+		create_account("VAT Liabilities", f"Duties and Taxes - {COMPANY_SHORT_NAME}", COMPANY)
+		create_account("Advance VAT Paid", f"Duties and Taxes - {COMPANY_SHORT_NAME}", COMPANY)
+		create_account("My Bank", f"Bank Accounts - {COMPANY_SHORT_NAME}", COMPANY)
 
-		pi = make_purchase_invoice(
-			company="_Test Company 6",
-			warehouse="Finished Goods - _TC6",
-			expense_account="Cost of Goods Sold - _TC6",
-			cost_center="Main - _TC6",
-			qty=10,
-			rate=100,
+		# 1000 equity paid to bank account
+		make_journal_entry(
+			[
+				dict(
+					account_name="My Bank",
+					debit_in_account_currency=1000,
+					credit_in_account_currency=0,
+				),
+				dict(
+					account_name="Capital Stock",
+					debit_in_account_currency=0,
+					credit_in_account_currency=1000,
+				),
+			]
 		)
-		si = create_sales_invoice(
-			company="_Test Company 6",
-			debit_to="Debtors - _TC6",
-			income_account="Sales - _TC6",
-			cost_center="Main - _TC6",
-			qty=5,
-			rate=110,
+
+		# 110 income paid to bank account (100 revenue + 10 VAT)
+		make_journal_entry(
+			[
+				dict(
+					account_name="My Bank",
+					debit_in_account_currency=110,
+					credit_in_account_currency=0,
+				),
+				dict(
+					account_name="Sales",
+					debit_in_account_currency=0,
+					credit_in_account_currency=100,
+				),
+				dict(
+					account_name="VAT Liabilities",
+					debit_in_account_currency=0,
+					credit_in_account_currency=10,
+				),
+			]
 		)
+
+		# offset VAT Liabilities with intra-year advance payment
+		make_journal_entry(
+			[
+				dict(
+					account_name="My Bank",
+					debit_in_account_currency=0,
+					credit_in_account_currency=10,
+				),
+				dict(
+					account_name="Advance VAT Paid",
+					debit_in_account_currency=10,
+					credit_in_account_currency=0,
+				),
+			]
+		)
+
 		filters = frappe._dict(
-			company="_Test Company 6",
+			company=COMPANY,
 			period_start_date=today(),
 			period_end_date=today(),
 			periodicity="Yearly",
 		)
-		result = execute(filters)[1]
-		for account_dict in result:
-			if account_dict.get("account") == "Current Liabilities - _TC6":
-				self.assertEqual(account_dict.total, 1000)
-			if account_dict.get("account") == "Current Assets - _TC6":
-				self.assertEqual(account_dict.total, 550)
+		results = execute(filters)
+		name_and_total = {
+			account_dict["account_name"]: account_dict["total"]
+			for account_dict in results[1]
+			if "total" in account_dict and "account_name" in account_dict
+		}
+
+		self.assertNotIn("Sales", name_and_total)
+
+		self.assertIn("My Bank", name_and_total)
+		self.assertEqual(name_and_total["My Bank"], 1100)
+
+		self.assertIn("VAT Liabilities", name_and_total)
+		self.assertEqual(name_and_total["VAT Liabilities"], 10)
+
+		self.assertIn("Advance VAT Paid", name_and_total)
+		self.assertEqual(name_and_total["Advance VAT Paid"], -10)
+
+		self.assertIn("Duties and Taxes", name_and_total)
+		self.assertEqual(name_and_total["Duties and Taxes"], 0)
+
+		self.assertIn("Application of Funds (Assets)", name_and_total)
+		self.assertEqual(name_and_total["Application of Funds (Assets)"], 1100)
+
+		self.assertIn("Equity", name_and_total)
+		self.assertEqual(name_and_total["Equity"], 1000)
+
+		self.assertIn("'Provisional Profit / Loss (Credit)'", name_and_total)
+		self.assertEqual(name_and_total["'Provisional Profit / Loss (Credit)'"], 100)
+
+
+def make_journal_entry(rows):
+	jv = frappe.new_doc("Journal Entry")
+	jv.posting_date = today()
+	jv.company = COMPANY
+	jv.user_remark = "test"
+
+	for row in rows:
+		row["account"] = row.pop("account_name") + " - " + COMPANY_SHORT_NAME
+		jv.append("accounts", row)
+
+	jv.insert()
+	jv.submit()
+
+
+def create_account(account_name: str, parent_account: str, company: str):
+	if frappe.db.exists("Account", {"account_name": account_name, "company": company}):
+		return
+
+	acc = frappe.new_doc("Account")
+	acc.account_name = account_name
+	acc.company = COMPANY
+	acc.parent_account = parent_account
+	acc.insert()