Payment Tool changes due to multi currency
diff --git a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json
index 11c92e2..c866181 100644
--- a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json
+++ b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json
@@ -93,7 +93,7 @@
    "in_list_view": 0, 
    "label": "Balance in Account Currency", 
    "no_copy": 1, 
-   "options": "currency", 
+   "options": "account_currency", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 1, 
@@ -231,7 +231,7 @@
    "in_list_view": 0, 
    "label": "Party Balance in Account Currency", 
    "no_copy": 1, 
-   "options": "currency", 
+   "options": "account_currency", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 1, 
@@ -269,13 +269,13 @@
    "collapsible": 0, 
    "fieldname": "debit_in_account_currency", 
    "fieldtype": "Currency", 
-   "hidden": 1, 
+   "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 0, 
+   "in_list_view": 1, 
    "label": "Debit in Account Currency", 
    "no_copy": 0, 
-   "options": "currency", 
+   "options": "account_currency", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -295,7 +295,7 @@
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 1, 
+   "in_list_view": 0, 
    "label": "Debit in Company Currency", 
    "no_copy": 1, 
    "oldfieldname": "debit", 
@@ -336,13 +336,13 @@
    "collapsible": 0, 
    "fieldname": "credit_in_account_currency", 
    "fieldtype": "Currency", 
-   "hidden": 1, 
+   "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 0, 
+   "in_list_view": 1, 
    "label": "Credit in Account Currency", 
    "no_copy": 0, 
-   "options": "currency", 
+   "options": "account_currency", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -362,7 +362,7 @@
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
-   "in_list_view": 1, 
+   "in_list_view": 0, 
    "label": "Credit in Company Currency", 
    "no_copy": 1, 
    "oldfieldname": "credit", 
@@ -520,7 +520,7 @@
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 1, 
- "modified": "2015-08-28 17:15:25.180681", 
+ "modified": "2015-08-31 17:50:04.145031", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Journal Entry Account", 
diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.js b/erpnext/accounts/doctype/payment_tool/payment_tool.js
index 6eefdb9..5904b99 100644
--- a/erpnext/accounts/doctype/payment_tool/payment_tool.js
+++ b/erpnext/accounts/doctype/payment_tool/payment_tool.js
@@ -55,6 +55,25 @@
 	}
 })
 
+frappe.ui.form.on("Payment Tool", "party_account", function(frm) {
+	if(frm.doc.party_account) {
+		frm.call({
+			method: "frappe.client.get_value",
+			args: {
+				doctype: "Account",
+				fieldname: "account_currency",
+				filters: { name: frm.doc.party_account },
+			},
+			callback: function(r, rt) {
+				if(r.message) {
+					frm.set_value("party_account_currency", r.message.account_currency);
+					erpnext.payment_tool.check_mandatory_to_set_button(frm);
+				}
+			}
+		});
+	}
+})
+
 frappe.ui.form.on("Payment Tool", "company", function(frm) {
 	erpnext.payment_tool.check_mandatory_to_set_button(frm);
 });
@@ -63,10 +82,6 @@
 	erpnext.payment_tool.check_mandatory_to_set_button(frm);
 });
 
-frappe.ui.form.on("Payment Tool", "party", function(frm) {
-	erpnext.payment_tool.check_mandatory_to_set_button(frm);
-});
-
 // Fetch bank/cash account based on payment mode
 frappe.ui.form.on("Payment Tool", "payment_mode", function(frm) {
 	return  frappe.call({
@@ -158,7 +173,9 @@
 		method: 'erpnext.accounts.doctype.payment_tool.payment_tool.get_against_voucher_amount',
 		args: {
 			"against_voucher_type": row.against_voucher_type,
-			"against_voucher_no": row.against_voucher_no
+			"against_voucher_no": row.against_voucher_no,
+			"party_account": self.party_account,
+			"company": self.company
 		},
 		callback: function(r) {
 			if(!r.exc) {
@@ -216,4 +233,4 @@
 	$.each(["Company", "Party Type", "Party", "Received or Paid"], function(i, field) {
 		if(!doc[frappe.model.scrub(field)]) frappe.throw(__("Please select {0} first", [field]));
 	});
-}
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.json b/erpnext/accounts/doctype/payment_tool/payment_tool.json
index e00d9e2..55e7048 100644
--- a/erpnext/accounts/doctype/payment_tool/payment_tool.json
+++ b/erpnext/accounts/doctype/payment_tool/payment_tool.json
@@ -106,7 +106,7 @@
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Column Break 1", 
+   "label": "", 
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
@@ -166,6 +166,29 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "party_account_currency", 
+   "fieldtype": "Link", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Party Account Currency", 
+   "no_copy": 1, 
+   "options": "Currency", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "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": "get_outstanding_vouchers", 
    "fieldtype": "Button", 
    "hidden": 0, 
@@ -306,6 +329,7 @@
    "in_list_view": 0, 
    "label": "Total Payment Amount", 
    "no_copy": 0, 
+   "options": "party_account_currency", 
    "permlevel": 0, 
    "print_hide": 0, 
    "read_only": 1, 
@@ -450,7 +474,7 @@
  "is_submittable": 0, 
  "issingle": 1, 
  "istable": 0, 
- "modified": "2015-06-05 11:17:33.843334", 
+ "modified": "2015-08-31 18:58:21.813054", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Payment Tool", 
diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.py b/erpnext/accounts/doctype/payment_tool/payment_tool.py
index 4c001c5..924fd1e 100644
--- a/erpnext/accounts/doctype/payment_tool/payment_tool.py
+++ b/erpnext/accounts/doctype/payment_tool/payment_tool.py
@@ -3,7 +3,7 @@
 
 from __future__ import unicode_literals
 import frappe
-from frappe import _
+from frappe import _, scrub
 from frappe.utils import flt
 from frappe.model.document import Document
 import json
@@ -38,7 +38,8 @@
 				d1.set("reference_type", v.against_voucher_type)
 				d1.set("reference_name", v.against_voucher_no)
 				d1.set('is_advance', 'Yes' if v.against_voucher_type in ['Sales Order', 'Purchase Order'] else 'No')
-				total_payment_amount = flt(total_payment_amount) + flt(d1.debit) - flt(d1.credit)
+				total_payment_amount = flt(total_payment_amount) + \
+					flt(d1.debit_in_account_currency) - flt(d1.credit_in_account_currency)
 
 		d2 = jv.append("accounts")
 		d2.account = self.payment_account
@@ -57,11 +58,14 @@
 		frappe.throw(_("No permission to use Payment Tool"), frappe.PermissionError)
 
 	args = json.loads(args)
+	
+	party_account_currency = frappe.db.get_value("Account", args.get("party_account"), "account_currency")
+	company_currency = frappe.db.get_value("Company", args.get("company"), "default_currency")
 
 	if args.get("party_type") == "Customer" and args.get("received_or_paid") == "Received":
-		amount_query = "ifnull(debit, 0) - ifnull(credit, 0)"
+		amount_query = "ifnull(debit_in_account_currency, 0) - ifnull(credit_in_account_currency, 0)"
 	elif args.get("party_type") == "Supplier" and args.get("received_or_paid") == "Paid":
-		amount_query = "ifnull(credit, 0) - ifnull(debit, 0)"
+		amount_query = "ifnull(credit_in_account_currency, 0) - ifnull(debit_in_account_currency, 0)"
 	else:
 		frappe.throw(_("Please enter the Against Vouchers manually"))
 
@@ -70,27 +74,34 @@
 		args.get("party_type"), args.get("party"))
 
 	# Get all SO / PO which are not fully billed or aginst which full advance not paid
-	orders_to_be_billed = get_orders_to_be_billed(args.get("party_type"), args.get("party"))
+	orders_to_be_billed = get_orders_to_be_billed(args.get("party_type"), args.get("party"), 
+		party_account_currency, company_currency)
 	return outstanding_invoices + orders_to_be_billed
 
-def get_orders_to_be_billed(party_type, party):
+def get_orders_to_be_billed(party_type, party, party_account_currency, company_currency):
 	voucher_type = 'Sales Order' if party_type == "Customer" else 'Purchase Order'
+	
+	ref_field = "base_grand_total" if party_account_currency == company_currency else "grand_total"
+	
 	orders = frappe.db.sql("""
 		select
 			name as voucher_no,
-			ifnull(base_grand_total, 0) as invoice_amount,
-			(ifnull(base_grand_total, 0) - ifnull(advance_paid, 0)) as outstanding_amount,
+			ifnull({ref_field}, 0) as invoice_amount,
+			(ifnull({ref_field}, 0) - ifnull(advance_paid, 0)) as outstanding_amount,
 			transaction_date as posting_date
 		from
-			`tab%s`
+			`tab{voucher_type}`
 		where
-			%s = %s
+			{party_type} = %s
 			and docstatus = 1
 			and ifnull(status, "") != "Stopped"
-			and ifnull(base_grand_total, 0) > ifnull(advance_paid, 0)
+			and ifnull({ref_field}, 0) > ifnull(advance_paid, 0)
 			and abs(100 - ifnull(per_billed, 0)) > 0.01
-		""" % (voucher_type, 'customer' if party_type == "Customer" else 'supplier', '%s'),
-			party, as_dict = True)
+		""".format(**{
+			"ref_field": ref_field,
+			"voucher_type": voucher_type,
+			"party_type": scrub(party_type)
+		}), party, as_dict = True)
 
 	order_list = []
 	for d in orders:
@@ -100,13 +111,19 @@
 	return order_list
 
 @frappe.whitelist()
-def get_against_voucher_amount(against_voucher_type, against_voucher_no):
+def get_against_voucher_amount(against_voucher_type, against_voucher_no, party_account, company):
+	party_account_currency = frappe.db.get_value("Account", party_account, "account_currency")
+	company_currency = frappe.db.get_value("Company", company, "default_currency")
+	ref_field = "base_grand_total" if party_account_currency == company_currency else "grand_total"
+	
 	if against_voucher_type in ["Sales Order", "Purchase Order"]:
-		select_cond = "base_grand_total as total_amount, ifnull(base_grand_total, 0) - ifnull(advance_paid, 0) as outstanding_amount"
+		select_cond = "{0} as total_amount, ifnull({0}, 0) - ifnull(advance_paid, 0) as outstanding_amount"\
+			.format(ref_field)
 	elif against_voucher_type in ["Sales Invoice", "Purchase Invoice"]:
-		select_cond = "base_grand_total as total_amount, outstanding_amount"
+		select_cond = "{0} as total_amount, outstanding_amount".format(ref_field)
 	elif against_voucher_type == "Journal Entry":
-		select_cond = "total_debit as total_amount"
+		ref_field = "total_debit" if party_account_currency == company_currency else "total_debit/exchange_rate"
+		select_cond = "{0} as total_amount".format(ref_field)
 
 	details = frappe.db.sql("""select {0} from `tab{1}` where name = %s"""
 		.format(select_cond, against_voucher_type), against_voucher_no, as_dict=1)
diff --git a/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json b/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json
index 5221f35..7e14608 100644
--- a/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json
+++ b/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json
@@ -87,6 +87,7 @@
    "in_list_view": 1, 
    "label": "Total Amount", 
    "no_copy": 0, 
+   "options": "party_account_currency", 
    "permlevel": 0, 
    "print_hide": 0, 
    "read_only": 1, 
@@ -108,6 +109,7 @@
    "in_list_view": 1, 
    "label": "Outstanding Amount", 
    "no_copy": 0, 
+   "options": "party_account_currency", 
    "permlevel": 0, 
    "print_hide": 0, 
    "read_only": 1, 
@@ -129,6 +131,7 @@
    "in_list_view": 1, 
    "label": "Payment Amount", 
    "no_copy": 0, 
+   "options": "party_account_currency", 
    "permlevel": 0, 
    "print_hide": 0, 
    "read_only": 0, 
@@ -146,7 +149,7 @@
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 1, 
- "modified": "2014-09-11 08:55:34.384017", 
+ "modified": "2015-08-31 18:58:35.537060", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Payment Tool Detail", 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index e0a8245..2e3794a 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -83,12 +83,12 @@
 				method: "frappe.client.get_value",
 				args: {
 					doctype: "Account",
-					fieldname: "currency",
+					fieldname: "account_currency",
 					filters: { name: me.frm.doc.credit_to },
 				},
 				callback: function(r, rt) {
 					if(r.message) {
-						me.frm.set_value("party_account_currency", r.message.currency);
+						me.frm.set_value("party_account_currency", r.message.account_currency);
 						me.set_dynamic_labels();
 					}
 				}
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index b640dda..4fac121 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -91,13 +91,16 @@
 			throw(_("Conversion rate cannot be 0 or 1"))
 
 	def validate_credit_to_acc(self):
-		account = frappe.db.get_value("Account", self.credit_to, ["account_type", "report_type"], as_dict=True)
+		account = frappe.db.get_value("Account", self.credit_to, 
+			["account_type", "report_type", "account_currency"], as_dict=True)
 
 		if account.report_type != "Balance Sheet":
 			frappe.throw(_("Credit To account must be a Balance Sheet account"))
 
 		if self.supplier and account.account_type != "Payable":
 			frappe.throw(_("Credit To account must be a Payable account"))
+			
+		self.party_account_currency = account.account_currency
 
 	def check_for_stopped_status(self):
 		check_list = []
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 1d44e54..5e8d3a5 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -183,12 +183,12 @@
 				method: "frappe.client.get_value",
 				args: {
 					doctype: "Account",
-					fieldname: "currency",
+					fieldname: "account_currency",
 					filters: { name: me.frm.doc.debit_to },
 				},
 				callback: function(r, rt) {
 					if(r.message) {
-						me.frm.set_value("party_account_currency", r.message.currency);
+						me.frm.set_value("party_account_currency", r.message.account_currency);
 						me.set_dynamic_labels();
 					}
 				}
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 0871a66..bce8fe2 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -271,13 +271,16 @@
 			reconcile_against_document(lst)
 
 	def validate_debit_to_acc(self):
-		account = frappe.db.get_value("Account", self.debit_to, ["account_type", "report_type"], as_dict=True)
+		account = frappe.db.get_value("Account", self.debit_to, 
+			["account_type", "report_type", "account_currency"], as_dict=True)
 
 		if account.report_type != "Balance Sheet":
 			frappe.throw(_("Debit To account must be a Balance Sheet account"))
 
 		if self.customer and account.account_type != "Receivable":
 			frappe.throw(_("Debit To account must be a Receivable account"))
+			
+		self.party_account_currency = account.account_currency
 
 	def validate_fixed_asset_account(self):
 		"""Validate Fixed Asset and whether Income Account Entered Exists"""
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index ad794d6..b1b24cf 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -525,7 +525,7 @@
 			var outstanding_amount = flt(total_amount_to_pay, precision("outstanding_amount"));
 		}
 		
-		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {			
+		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {	
 			this.frm.set_value("outstanding_amount", outstanding_amount);
 		} else {
 			this.frm.set_value("outstanding_amount",