Merge branch 'develop' of https://github.com/frappe/erpnext into party_account_currency_check
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 2d19391..da0eeac 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
@@ -82,7 +82,8 @@
 			income_account="Sales - TPC",
 			expense_account="Cost of Goods Sold - TPC",
 			rate=400,
-			debit_to="Debtors - TPC"
+			debit_to="Debtors - TPC",
+			currency="USD"
 		)
 		create_sales_invoice(
 			company=company,
@@ -90,7 +91,8 @@
 			income_account="Sales - TPC",
 			expense_account="Cost of Goods Sold - TPC",
 			rate=200,
-			debit_to="Debtors - TPC"
+			debit_to="Debtors - TPC",
+			currency="USD"
 		)
 
 		pcv = frappe.get_doc({
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 9f82af9..ce6a2a4 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -14,7 +14,7 @@
 from erpnext.utilities.transaction_base import TransactionBase
 from erpnext.buying.utils import update_last_purchase_rate
 from erpnext.controllers.sales_and_purchase_return import validate_return
-from erpnext.accounts.party import get_party_account_currency, validate_party_frozen_disabled
+from erpnext.accounts.party import get_party_account_currency, validate_party_frozen_disabled, get_party_gle_currency
 from erpnext.accounts.doctype.pricing_rule.utils import (apply_pricing_rule_on_transaction,
 	apply_pricing_rule_for_free_items, get_applied_pricing_rules)
 from erpnext.exceptions import InvalidCurrency
@@ -113,6 +113,7 @@
 
 		self.validate_party()
 		self.validate_currency()
+		self.validate_party_account_currency()
 
 		if self.doctype == 'Purchase Invoice':
 			self.calculate_paid_amount()
@@ -1109,6 +1110,22 @@
 				# at quotation / sales order level and we shouldn't stop someone
 				# from creating a sales invoice if sales order is already created
 
+	def validate_party_account_currency(self):
+		if self.doctype not in ('Sales Invoice', 'Purchase Invoice'):
+			return
+
+		if self.is_opening == 'Yes':
+			return
+
+		party_type, party = self.get_party()
+		party_gle_currency = get_party_gle_currency(party_type, party, self.company)
+		party_account = self.get('debit_to') if self.doctype == 'Sales Invoice' else self.get('credit_to')
+		party_account_currency = get_account_currency(party_account)
+
+		if not party_gle_currency and (party_account_currency != self.currency):
+			frappe.throw(_("Party Account {0} currency and document currency should be same").format(frappe.bold(party_account)))
+
+
 	def delink_advance_entries(self, linked_doc_name):
 		total_allocated_amount = 0
 		for adv in self.advances:
diff --git a/erpnext/healthcare/doctype/lab_test/test_lab_test.py b/erpnext/healthcare/doctype/lab_test/test_lab_test.py
index c9f0029..c3847ea 100644
--- a/erpnext/healthcare/doctype/lab_test/test_lab_test.py
+++ b/erpnext/healthcare/doctype/lab_test/test_lab_test.py
@@ -147,6 +147,7 @@
 	sales_invoice.customer = frappe.db.get_value('Patient', patient, 'customer')
 	sales_invoice.due_date = getdate()
 	sales_invoice.company = '_Test Company'
+	sales_invoice.currency = 'INR'
 	sales_invoice.debit_to = get_receivable_account('_Test Company')
 
 	tests = [insulin_resistance_template, blood_test_template]
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
index 05e2cd3..ac09537 100755
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
@@ -14,6 +14,7 @@
 from erpnext.hr.doctype.employee.employee import is_holiday
 from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account, get_income_account
 from erpnext.healthcare.utils import check_fee_validity, get_service_item_and_practitioner_charge, manage_fee_validity
+from erpnext import get_company_currency
 
 class PatientAppointment(Document):
 	def validate(self):
@@ -160,6 +161,8 @@
 	sales_invoice = frappe.new_doc('Sales Invoice')
 	sales_invoice.patient = appointment_doc.patient
 	sales_invoice.customer = frappe.get_value('Patient', appointment_doc.patient, 'customer')
+	sales_invoice.currency = frappe.get_value('Customer', sales_invoice.customer, 'default_currency') \
+		or get_company_currency(appointment_doc.company)
 	sales_invoice.appointment = appointment_doc.name
 	sales_invoice.due_date = getdate()
 	sales_invoice.company = appointment_doc.company
diff --git a/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
index 9c3392c..ac80612 100644
--- a/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
@@ -252,6 +252,7 @@
 		patient = frappe.new_doc('Patient')
 		patient.first_name = '_Test Patient'
 		patient.sex = 'Female'
+		patient.default_currency = 'INR'
 		patient.save(ignore_permissions=True)
 		patient = patient.name
 	return patient
diff --git a/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py b/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
index e209660..c29f6a0 100644
--- a/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
+++ b/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
@@ -6,6 +6,7 @@
 import frappe
 from frappe.model.document import Document
 from frappe.utils import flt, today
+from erpnext import get_company_currency
 
 class TherapyPlan(Document):
 	def validate(self):
@@ -73,6 +74,8 @@
 	si.company = company
 	si.patient = patient
 	si.customer = frappe.db.get_value('Patient', patient, 'customer')
+	si.currency = frappe.get_value('Customer', si.customer, 'default_currency') \
+		or get_company_currency(si.company)
 
 	item = frappe.db.get_value('Therapy Plan Template', therapy_plan_template, 'linked_item')
 	price_list, price_list_currency = frappe.db.get_values('Price List', {'selling': 1}, ['name', 'currency'])[0]
diff --git a/erpnext/non_profit/doctype/membership/membership.py b/erpnext/non_profit/doctype/membership/membership.py
index b584116..88284f8 100644
--- a/erpnext/non_profit/doctype/membership/membership.py
+++ b/erpnext/non_profit/doctype/membership/membership.py
@@ -14,6 +14,7 @@
 from erpnext.non_profit.doctype.member.member import create_member
 from frappe import _
 import erpnext
+from erpnext import get_company_currency
 
 class Membership(Document):
 	def validate(self):
@@ -176,7 +177,7 @@
 		"doctype": "Sales Invoice",
 		"customer": member.customer,
 		"debit_to": settings.membership_debit_account,
-		"currency": membership.currency,
+		"currency": membership.currency or get_company_currency(settings.company),
 		"company": settings.company,
 		"is_pos": 0,
 		"items": [
diff --git a/erpnext/non_profit/doctype/membership/test_membership.py b/erpnext/non_profit/doctype/membership/test_membership.py
index 5ad2088..5c876df 100644
--- a/erpnext/non_profit/doctype/membership/test_membership.py
+++ b/erpnext/non_profit/doctype/membership/test_membership.py
@@ -80,7 +80,7 @@
 		"member": member,
 		"membership_status": "Current",
 		"membership_type": "_rzpy_test_milythm",
-		"currency": "INR",
+		"currency": "USD",
 		"paid": 1,
 		"from_date": nowdate(),
 		"amount": 100