def test_closing_entry(self):
		year_start_date = get_fiscal_year(today())[1]

		make_journal_entry("_Test Bank - _TC", "Sales - _TC", 400,
			"_Test Cost Center - _TC", posting_date=now(), submit=True)

		make_journal_entry("_Test Account Cost for Goods Sold - _TC",
			"_Test Bank - _TC", 600, "_Test Cost Center - _TC", posting_date=now(), submit=True)

		random_expense_account = frappe.db.sql("""
			select t1.account,
				sum(t1.debit) - sum(t1.credit) as balance,
				sum(t1.debit_in_account_currency) - sum(t1.credit_in_account_currency) \
					as balance_in_account_currency
			from `tabGL Entry` t1, `tabAccount` t2
			where t1.account = t2.name and t2.root_type = 'Expense'
				and t2.docstatus < 2 and t2.company = '_Test Company'
				and t1.posting_date between %s and %s
			group by t1.account
			having sum(t1.debit) > sum(t1.credit)
			limit 1""", (year_start_date, today()), as_dict=True)

		profit_or_loss = frappe.db.sql("""select sum(t1.debit) - sum(t1.credit) as balance
			from `tabGL Entry` t1, `tabAccount` t2
			where t1.account = t2.name and t2.report_type = 'Profit and Loss'
			and t2.docstatus < 2 and t2.company = '_Test Company'
			and t1.posting_date between %s and %s""", (year_start_date, today()))

		profit_or_loss = flt(profit_or_loss[0][0]) if profit_or_loss else 0

		pcv = self.make_period_closing_voucher()

		# Check value for closing account
		gle_amount_for_closing_account = frappe.db.sql("""select debit - credit
			from `tabGL Entry` where voucher_type='Period Closing Voucher' and voucher_no=%s
			and account = '_Test Account Reserves and Surplus - _TC'""", pcv.name)

		gle_amount_for_closing_account = flt(gle_amount_for_closing_account[0][0]) \
			if gle_amount_for_closing_account else 0

		self.assertEqual(gle_amount_for_closing_account, profit_or_loss)

		if random_expense_account:
			# Check posted value for teh above random_expense_account
			gle_for_random_expense_account = frappe.db.sql("""
				select debit - credit as amount,
					debit_in_account_currency - credit_in_account_currency
						as amount_in_account_currency
				from `tabGL Entry`
				where voucher_type='Period Closing Voucher' and voucher_no=%s and account =%s""",
				(pcv.name, random_expense_account[0].account), as_dict=True)

			self.assertEqual(gle_for_random_expense_account[0].amount, -1*random_expense_account[0].balance)
			self.assertEqual(gle_for_random_expense_account[0].amount_in_account_currency,
				-1*random_expense_account[0].balance_in_account_currency)
	def test_closing_entry(self):
		frappe.db.sql("delete from `tabGL Entry` where company='Test PCV Company'")

		company = create_company()
		cost_center = create_cost_center("Test Cost Center 1")

		jv1 = make_journal_entry(
			amount=400,
			account1="Cash - TPC",
			account2="Sales - TPC",
			cost_center=cost_center,
			posting_date=now(),
			save=False,
		)
		jv1.company = company
		jv1.save()
		jv1.submit()

		jv2 = make_journal_entry(
			amount=600,
			account1="Cost of Goods Sold - TPC",
			account2="Cash - TPC",
			cost_center=cost_center,
			posting_date=now(),
			save=False,
		)
		jv2.company = company
		jv2.save()
		jv2.submit()

		pcv = self.make_period_closing_voucher()
		surplus_account = pcv.closing_account_head

		expected_gle = (
			("Cost of Goods Sold - TPC", 0.0, 600.0),
			(surplus_account, 600.0, 400.0),
			("Sales - TPC", 400.0, 0.0),
		)

		pcv_gle = frappe.db.sql(
			"""
			select account, debit, credit from `tabGL Entry` where voucher_no=%s order by account
		""",
			(pcv.name),
		)

		self.assertEqual(pcv_gle, expected_gle)
	def test_period_closing_with_finance_book_entries(self):
		frappe.db.sql("delete from `tabGL Entry` where company='Test PCV Company'")

		company = create_company()
		surplus_account = create_account()
		cost_center = create_cost_center("Test Cost Center 1")

		si = create_sales_invoice(
			company=company,
			income_account="Sales - TPC",
			expense_account="Cost of Goods Sold - TPC",
			cost_center=cost_center,
			rate=400,
			debit_to="Debtors - TPC",
			currency="USD",
			customer="_Test Customer USD",
		)

		jv = make_journal_entry(
			account1="Cash - TPC",
			account2="Sales - TPC",
			amount=400,
			cost_center=cost_center,
			posting_date=now(),
		)
		jv.company = company
		jv.finance_book = create_finance_book().name
		jv.save()
		jv.submit()

		pcv = self.make_period_closing_voucher()
		surplus_account = pcv.closing_account_head

		expected_gle = (
			(surplus_account, 0.0, 400.0, None),
			(surplus_account, 0.0, 400.0, jv.finance_book),
			("Sales - TPC", 400.0, 0.0, None),
			("Sales - TPC", 400.0, 0.0, jv.finance_book),
		)

		pcv_gle = frappe.db.sql(
			"""
			select account, debit, credit, finance_book
			from `tabGL Entry` where voucher_no=%s
			order by account, finance_book
		""",
			(pcv.name),
		)

		self.assertEqual(pcv_gle, expected_gle)