Merge pull request #2553 from neilLasrado/mode-of-payment

mode of payment - company wise default account added
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js
index 5cfa971..07e93b6 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.js
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js
@@ -230,7 +230,7 @@
 		var jvdetail = frappe.model.add_child(doc, "Journal Entry Account", "accounts");
 		$.each(r, function(i, d) {
 			var row = frappe.model.add_child(doc, "Journal Entry Account", "accounts");
-			row.account = d.account;
+			row.account = d.cash_bank_account;
 			row.balance = d.balance;
 		});
 		refresh_field("accounts");
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 946e2b2..05b10f1 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -442,12 +442,20 @@
 						Pending Amount is {2}".format(d.idx, d.against_expense_claim, pending_amount)))
 
 @frappe.whitelist()
-def get_default_bank_cash_account(company, voucher_type):
-	account = frappe.db.get_value("Company", company,
-		voucher_type=="Bank Entry" and "default_bank_account" or "default_cash_account")
+def get_default_bank_cash_account(company, voucher_type, mode_of_payment=None):
+	from erpnext.accounts.doctype.sales_invoice.sales_invoice import get_bank_cash_account
+	if mode_of_payment:
+		account = get_bank_cash_account(mode_of_payment, company)
+		if account.get("bank_cash_account"):
+			account.update({"balance": get_balance_on(account.get("cash_bank_account"))})
+			return account
+
+	account = frappe.db.get_value("Company", company, \
+		voucher_type=="Bank Voucher" and "default_bank_account" or "default_cash_account")
+
 	if account:
 		return {
-			"account": account,
+			"cash_bank_account": account,
 			"balance": get_balance_on(account)
 		}
 
@@ -504,7 +512,7 @@
 	d2 = jv.append("accounts")
 
 	if bank_account:
-		d2.account = bank_account["account"]
+		d2.account = bank_account["cash_bank_account"]
 		d2.balance = bank_account["balance"]
 
 	return jv
diff --git a/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.js b/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.js
index a86da0e..ffd8ea7 100644
--- a/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.js
+++ b/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.js
@@ -1,7 +1,7 @@
 // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
-cur_frm.set_query("default_account", function(doc) {
+cur_frm.set_query("default_account", "mode_of_payment_details", function(doc, cdt, cdn) {
 	return{
 		filters: [
 			['Account', 'account_type', 'in', 'Bank, Cash'],
diff --git a/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.json b/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.json
index d1f1677..8a1f5e1 100644
--- a/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.json
+++ b/erpnext/accounts/doctype/mode_of_payment/mode_of_payment.json
@@ -19,29 +19,17 @@
    "reqd": 1
   }, 
   {
-   "fieldname": "company", 
-   "fieldtype": "Link", 
-   "in_list_view": 1, 
-   "label": "Company", 
-   "options": "Company", 
+   "fieldname": "accounts", 
+   "fieldtype": "Table", 
+   "label": "Accounts", 
+   "options": "Mode of Payment Account", 
    "permlevel": 0, 
-   "read_only": 0
-  }, 
-  {
-   "description": "Default Bank / Cash account will be automatically updated in POS Invoice when this mode is selected.", 
-   "fieldname": "default_account", 
-   "fieldtype": "Link", 
-   "ignore_user_permissions": 1, 
-   "in_list_view": 1, 
-   "label": "Default Account", 
-   "options": "Account", 
-   "permlevel": 0, 
-   "read_only": 0
+   "precision": ""
   }
  ], 
  "icon": "icon-credit-card", 
  "idx": 1, 
- "modified": "2015-01-05 11:13:54.446006", 
+ "modified": "2015-01-06 17:21:12.485997", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Mode of Payment", 
diff --git a/erpnext/accounts/doctype/mode_of_payment/test_mode_of_payment.py b/erpnext/accounts/doctype/mode_of_payment/test_mode_of_payment.py
new file mode 100644
index 0000000..2008f98
--- /dev/null
+++ b/erpnext/accounts/doctype/mode_of_payment/test_mode_of_payment.py
@@ -0,0 +1,10 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors and Contributors
+# See license.txt
+
+import frappe
+import unittest
+
+test_records = frappe.get_test_records('Mode of Payment')
+
+class TestModeofPayment(unittest.TestCase):
+	pass
diff --git a/erpnext/accounts/doctype/mode_of_payment/test_records.json b/erpnext/accounts/doctype/mode_of_payment/test_records.json
new file mode 100644
index 0000000..f1ff01c
--- /dev/null
+++ b/erpnext/accounts/doctype/mode_of_payment/test_records.json
@@ -0,0 +1,6 @@
+[
+	{
+		"doctype": "Mode of Payment",
+		"name": "_Test Mode of Payment 1"
+	}
+]
diff --git a/erpnext/accounts/doctype/mode_of_payment_account/__init__.py b/erpnext/accounts/doctype/mode_of_payment_account/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/doctype/mode_of_payment_account/__init__.py
diff --git a/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.json b/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.json
new file mode 100644
index 0000000..bfe961f
--- /dev/null
+++ b/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.json
@@ -0,0 +1,71 @@
+{
+ "allow_copy": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "creation": "2015-01-05 14:17:53.101432", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "fields": [
+  {
+   "allow_on_submit": 0, 
+   "fieldname": "company", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Company", 
+   "no_copy": 0, 
+   "options": "Company", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "description": "Default Bank / Cash account will be automatically updated in POS Invoice when this mode is selected.", 
+   "fieldname": "default_account", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Default Account", 
+   "no_copy": 0, 
+   "options": "Account", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }
+ ], 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "in_create": 0, 
+ "in_dialog": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 1, 
+ "modified": "2015-01-06 17:26:57.053474", 
+ "modified_by": "Administrator", 
+ "module": "Accounts", 
+ "name": "Mode of Payment Account", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [], 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC"
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.py b/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.py
new file mode 100644
index 0000000..933d0a2
--- /dev/null
+++ b/erpnext/accounts/doctype/mode_of_payment_account/mode_of_payment_account.py
@@ -0,0 +1,9 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+
+class ModeofPaymentAccount(Document):
+	pass
diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.js b/erpnext/accounts/doctype/payment_tool/payment_tool.js
index fd6867a..0cb51d3 100644
--- a/erpnext/accounts/doctype/payment_tool/payment_tool.js
+++ b/erpnext/accounts/doctype/payment_tool/payment_tool.js
@@ -63,7 +63,22 @@
 });
 
 // Fetch bank/cash account based on payment mode
-cur_frm.add_fetch("payment_mode", "default_account", "payment_account");
+frappe.ui.form.on("Payment Tool", "payment_mode", function(frm) {
+	return  frappe.call({
+		method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
+		args: {
+				"mode_of_payment": frm.doc.mode_of_payment,
+				"company": frm.doc.company
+		},
+		callback: function(r, rt) {
+			if(r.message) {
+				frm.doc.set_value("payment_account", r.message['bank_cash_account']
+);
+			}
+		}
+	});
+});
+
 
 erpnext.payment_tool.check_mandatory_to_set_button = function(frm) {
 	if (frm.doc.company && frm.doc.party_type && frm.doc.party && frm.doc.received_or_paid) {
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index ff39939..fac9442 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -246,11 +246,15 @@
 
 
 cur_frm.cscript.mode_of_payment = function(doc) {
-	console.log("mode of payment!");
-	return cur_frm.call({
-		method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
-		args: { mode_of_payment: doc.mode_of_payment },
-	});
+	if(doc.is_pos) {
+		return cur_frm.call({
+			method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
+			args: { 
+				"mode_of_payment": doc.mode_of_payment,
+				"company": doc.company
+			 },
+		});
+	 }
 }
 
 cur_frm.cscript.update_stock = function(doc, dt, dn) {
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 2de2c10..63aef26 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -577,14 +577,16 @@
 				)
 
 @frappe.whitelist()
-def get_bank_cash_account(mode_of_payment):
-	val = frappe.db.get_value("Mode of Payment", mode_of_payment, "default_account")
-	if not val:
+def get_bank_cash_account(mode_of_payment, company):
+	account = frappe.db.get_value("Mode of Payment Account", {"parent": mode_of_payment, "company": company}, \
+		"default_account")
+	if not account:
 		frappe.msgprint(_("Please set default Cash or Bank account in Mode of Payment {0}").format(mode_of_payment))
 	return {
-		"cash_bank_account": val
+		"cash_bank_account": account
 	}
 
+
 @frappe.whitelist()
 def get_income_account(doctype, txt, searchfield, start, page_len, filters):
 	from erpnext.controllers.queries import get_match_cond
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 7648370..9721646 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -103,3 +103,4 @@
 erpnext.patches.v4_1.fix_jv_remarks
 erpnext.patches.v5_0.recalculate_total_amount_in_jv
 erpnext.patches.v5_0.remove_shopping_cart_app
+erpnext.patches.v5_0.update_companywise_payment_account
diff --git a/erpnext/patches/v5_0/update_companywise_payment_account.py b/erpnext/patches/v5_0/update_companywise_payment_account.py
new file mode 100644
index 0000000..71d7552
--- /dev/null
+++ b/erpnext/patches/v5_0/update_companywise_payment_account.py
@@ -0,0 +1,20 @@
+
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+import frappe
+
+def execute():
+	frappe.reload_doc('accounts', 'doctype', 'mode_of_payment')
+
+	mode_of_payment_list = frappe.db.sql("""select name, default_account
+		from `tabMode of Payment`""", as_dict=1)
+
+	for d in mode_of_payment_list:
+		if d.get("default_account"):
+			parent_doc = frappe.get_doc("Mode of Payment", d.get("name"))
+
+			parent_doc.set("mode_of_payment_details",
+				[{"company": frappe.db.get_value("Account", d.get("default_account"), "company"),
+				"default_account": d.get("default_account")}])
+			parent_doc.save()