Merge pull request #4208 from nabinhait/pcv

Period Closing Voucher as per multi currency
diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.js b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.js
index c087500..51d8117 100644
--- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.js
+++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.js
@@ -13,7 +13,7 @@
 	return{
 		filters:{
 			"company": doc.company,
-			"report_type": "Balance Sheet",
+			"root_type": "Liability",
 			"freeze_account": "No",
 			"is_group": 0
 		}
diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json
index 7827fbe..2295956 100644
--- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json
+++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.json
@@ -198,29 +198,6 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "coa_help", 
-   "fieldtype": "HTML", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "in_filter": 0, 
-   "in_list_view": 0, 
-   "label": "CoA Help", 
-   "no_copy": 0, 
-   "oldfieldtype": "HTML", 
-   "options": "<a href=\"#!Accounts Browser/Account\">To manage Account Head, click here</a>", 
-   "permlevel": 0, 
-   "print_hide": 0, 
-   "read_only": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
-  }, 
-  {
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
    "fieldname": "remarks", 
    "fieldtype": "Small Text", 
    "hidden": 0, 
@@ -250,7 +227,7 @@
  "is_submittable": 1, 
  "issingle": 0, 
  "istable": 0, 
- "modified": "2015-10-02 07:39:00.056337", 
+ "modified": "2015-10-21 12:40:58.278256", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Period Closing Voucher", 
diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py
index 1615a13..aba43a6 100644
--- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py
+++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py
@@ -5,6 +5,7 @@
 import frappe
 from frappe.utils import flt
 from frappe import _
+from erpnext.accounts.utils import get_account_currency
 from erpnext.controllers.accounts_controller import AccountsController
 
 class PeriodClosingVoucher(AccountsController):
@@ -20,51 +21,75 @@
 			where voucher_type = 'Period Closing Voucher' and voucher_no=%s""", self.name)
 
 	def validate_account_head(self):
-		if frappe.db.get_value("Account", self.closing_account_head, "report_type") \
-				!= "Balance Sheet":
-			frappe.throw(_("Closing Account {0} must be of type 'Liability'").format(self.closing_account_head))
+		closing_account_type = frappe.db.get_value("Account", self.closing_account_head, "root_type")
+		
+		if closing_account_type != "Liability":
+			frappe.throw(_("Closing Account {0} must be of type 'Liability'")
+				.format(self.closing_account_head))
+
+		account_currency = get_account_currency(self.closing_account_head)
+		company_currency = frappe.db.get_value("Company", self.company, "default_currency")		
+		if account_currency != company_currency:
+			frappe.throw(_("Currency of the Closing Account must be {0}").format(company_currency))
 
 	def validate_posting_date(self):
-		from erpnext.accounts.utils import get_fiscal_year
+		from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year
+		
+		validate_fiscal_year(self.posting_date, self.fiscal_year, label=_("Posting Date"), doc=self)
+		
 		self.year_start_date = get_fiscal_year(self.posting_date, self.fiscal_year)[1]
 
 		pce = frappe.db.sql("""select name from `tabPeriod Closing Voucher`
 			where posting_date > %s and fiscal_year = %s and docstatus = 1""",
 			(self.posting_date, self.fiscal_year))
 		if pce and pce[0][0]:
-			frappe.throw(_("Another Period Closing Entry {0} has been made after {1}").format(pce[0][0], self.posting_date))
-
-	def get_pl_balances(self):
-		"""Get balance for pl accounts"""
-		return frappe.db.sql("""
-			select t1.account, sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) as balance
-			from `tabGL Entry` t1, `tabAccount` t2
-			where t1.account = t2.name and ifnull(t2.report_type, '') = 'Profit and Loss'
-			and t2.docstatus < 2 and t2.company = %s
-			and t1.posting_date between %s and %s
-			group by t1.account
-		""", (self.company, self.get("year_start_date"), self.posting_date), as_dict=1)
+			frappe.throw(_("Another Period Closing Entry {0} has been made after {1}")
+				.format(pce[0][0], self.posting_date))
 
 	def make_gl_entries(self):
 		gl_entries = []
 		net_pl_balance = 0
 		pl_accounts = self.get_pl_balances()
+		
 		for acc in pl_accounts:
-			if flt(acc.balance):
+			if flt(acc.balance_in_company_currency):
 				gl_entries.append(self.get_gl_dict({
 					"account": acc.account,
-					"debit": abs(flt(acc.balance)) if flt(acc.balance) < 0 else 0,
-					"credit": abs(flt(acc.balance)) if flt(acc.balance) > 0 else 0,
+					"account_currency": acc.account_currency,
+					"debit_in_account_currency": abs(flt(acc.balance_in_account_currency)) \
+						if flt(acc.balance_in_account_currency) < 0 else 0,
+					"debit": abs(flt(acc.balance_in_company_currency)) \
+						if flt(acc.balance_in_company_currency) < 0 else 0,
+					"credit_in_account_currency": abs(flt(acc.balance_in_account_currency)) \
+						if flt(acc.balance_in_account_currency) > 0 else 0,
+					"credit": abs(flt(acc.balance_in_company_currency)) \
+						if flt(acc.balance_in_company_currency) > 0 else 0
 				}))
 
-				net_pl_balance += flt(acc.balance)
-
+				net_pl_balance += flt(acc.balance_in_company_currency)
+		
 		if net_pl_balance:
 			gl_entries.append(self.get_gl_dict({
 				"account": self.closing_account_head,
+				"debit_in_account_currency": abs(net_pl_balance) if net_pl_balance > 0 else 0,
 				"debit": abs(net_pl_balance) if net_pl_balance > 0 else 0,
+				"credit_in_account_currency": abs(net_pl_balance) if net_pl_balance < 0 else 0,
 				"credit": abs(net_pl_balance) if net_pl_balance < 0 else 0
 			}))
-
+		
 		from erpnext.accounts.general_ledger import make_gl_entries
 		make_gl_entries(gl_entries)
+
+	def get_pl_balances(self):
+		"""Get balance for pl accounts"""
+		return frappe.db.sql("""
+			select 
+				t1.account, t2.account_currency,  				sum(ifnull(t1.debit_in_account_currency,0))-sum(ifnull(t1.credit_in_account_currency,0)) 
+					as balance_in_account_currency,
+				sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) as balance_in_company_currency
+			from `tabGL Entry` t1, `tabAccount` t2
+			where t1.account = t2.name and ifnull(t2.report_type, '') = 'Profit and Loss'
+			and t2.docstatus < 2 and t2.company = %s
+			and t1.posting_date between %s and %s
+			group by t1.account
+		""", (self.company, self.get("year_start_date"), self.posting_date), as_dict=1)
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py
index 0b59746..db4dad4 100644
--- a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py
+++ b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py
@@ -5,42 +5,74 @@
 from __future__ import unicode_literals
 import unittest
 import frappe
-from frappe.utils import flt
+from frappe.utils import flt, today
+from erpnext.accounts.utils import get_fiscal_year
 from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry
 
 class TestPeriodClosingVoucher(unittest.TestCase):
 	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", submit=True)
 		
 		make_journal_entry("_Test Account Cost for Goods Sold - _TC", 
 			"_Test Bank - _TC", 600, "_Test Cost Center - _TC", submit=True)
 			
+		random_expense_account = frappe.db.sql("""
+			select t1.account, 
+				sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) as balance,
+				sum(ifnull(t1.debit_in_account_currency,0))-sum(ifnull(t1.credit_in_account_currency,0)) \
+					as balance_in_account_currency
+			from `tabGL Entry` t1, `tabAccount` t2
+			where t1.account = t2.name and ifnull(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(ifnull(t1.debit,0)) > sum(ifnull(t1.credit,0))
+			limit 1""", (year_start_date, today()), as_dict=True)
+			
 		profit_or_loss = frappe.db.sql("""select sum(ifnull(t1.debit,0))-sum(ifnull(t1.credit,0)) as balance
 			from `tabGL Entry` t1, `tabAccount` t2
 			where t1.account = t2.name and ifnull(t2.report_type, '') = 'Profit and Loss'
 			and t2.docstatus < 2 and t2.company = '_Test Company'
-			and t1.posting_date between '2013-01-01' and '2013-12-31'""")
+			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()
 		
-		gle_value = frappe.db.sql("""select ifnull(debit, 0) - ifnull(credit, 0)
+		# Check value for closing account
+		gle_amount_for_closing_account = frappe.db.sql("""select ifnull(debit, 0) - ifnull(credit, 0)
 			from `tabGL Entry` where voucher_type='Period Closing Voucher' and voucher_no=%s
 			and account = '_Test Account Reserves and Surplus - _TC'""", pcv.name)
 			
-		gle_value = flt(gle_value[0][0]) if gle_value else 0
+		gle_amount_for_closing_account = flt(gle_amount_for_closing_account[0][0]) \
+			if gle_amount_for_closing_account else 0
 
-		self.assertEqual(gle_value, profit_or_loss)
+		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 ifnull(debit, 0) - ifnull(credit, 0) as amount,
+					ifnull(debit_in_account_currency, 0) - ifnull(credit_in_account_currency, 0) 
+						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 make_period_closing_voucher(self):
 		pcv = frappe.get_doc({
 			"doctype": "Period Closing Voucher",
 			"closing_account_head": "_Test Account Reserves and Surplus - _TC",
 			"company": "_Test Company",
-			"fiscal_year": "_Test Fiscal Year 2013",
-			"posting_date": "2013-12-31",
+			"fiscal_year": get_fiscal_year(today())[0],
+			"posting_date": today(),
 			"remarks": "test"
 		})
 		pcv.insert()
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index daf3377..2ed5fa8 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -221,7 +221,7 @@
 		if not account_currency:
 			account_currency = get_account_currency(gl_dict.account)
 
-		if self.doctype != "Journal Entry":
+		if self.doctype not in ["Journal Entry", "Period Closing Voucher"]:
 			self.validate_account_currency(gl_dict.account, account_currency)
 			self.set_balance_in_account_currency(gl_dict, account_currency)