Merge pull request #2035 from nabinhait/hotfix

Account master type patch and Stock RBNB Diff
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 0144108..d1b6584 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -363,3 +363,31 @@
 
 	from frappe.utils import get_number_format_info
 	return get_number_format_info(currency_format)[2]
+
+def get_stock_rbnb_difference(posting_date, company):
+	stock_items = frappe.db.sql_list("""select distinct item_code
+		from `tabStock Ledger Entry` where company=%s""", company)
+
+	pr_valuation_amount = frappe.db.sql("""
+		select sum(ifnull(pr_item.valuation_rate, 0) * ifnull(pr_item.qty, 0) * ifnull(pr_item.conversion_factor, 0))
+		from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr
+	    where pr.name = pr_item.parent and pr.docstatus=1 and pr.company=%s
+		and pr.posting_date <= %s and pr_item.item_code in (%s)""" %
+	    ('%s', '%s', ', '.join(['%s']*len(stock_items))), tuple([company, posting_date] + stock_items))[0][0]
+
+	pi_valuation_amount = frappe.db.sql("""
+		select sum(ifnull(pi_item.valuation_rate, 0) * ifnull(pi_item.qty, 0) * ifnull(pi_item.conversion_factor, 0))
+		from `tabPurchase Invoice Item` pi_item, `tabPurchase Invoice` pi
+	    where pi.name = pi_item.parent and pi.docstatus=1 and pi.company=%s
+		and pi.posting_date <= %s and pi_item.item_code in (%s)""" %
+	    ('%s', '%s', ', '.join(['%s']*len(stock_items))), tuple([company, posting_date] + stock_items))[0][0]
+
+	# Balance should be
+	stock_rbnb = flt(pr_valuation_amount, 2) - flt(pi_valuation_amount, 2)
+
+	# Balance as per system
+	stock_rbnb_account = "Stock Received But Not Billed - " + frappe.db.get_value("Company", company, "abbr")
+	sys_bal = get_balance_on(stock_rbnb_account, posting_date)
+
+	# Amount should be credited
+	return flt(stock_rbnb) + flt(sys_bal)
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 7fc858d..1ae0a95 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -73,3 +73,4 @@
 execute:frappe.delete_doc("Page", "trial-balance") #2014-07-22
 erpnext.patches.v4_2.delete_old_print_formats #2014-07-29
 erpnext.patches.v4_2.toggle_rounded_total #2014-07-30
+erpnext.patches.v4_2.fix_account_master_type
diff --git a/erpnext/patches/v4_2/fix_account_master_type.py b/erpnext/patches/v4_2/fix_account_master_type.py
new file mode 100644
index 0000000..09fa789
--- /dev/null
+++ b/erpnext/patches/v4_2/fix_account_master_type.py
@@ -0,0 +1,12 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	for d in frappe.db.sql("""select name from `tabAccount`
+		where ifnull(master_type, '') not in ('Customer', 'Supplier', 'Employee', '')"""):
+			ac = frappe.get_doc("Account", d[0])
+			ac.master_type = None
+			ac.save()
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 7dca72a..96b2cd5 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -211,10 +211,9 @@
 				},
 				callback: function(r) {
 					if(!r.exc) {
-						var jv_name = frappe.model.make_new_doc_and_get_name('Journal Voucher');
-						var jv = locals["Journal Voucher"][jv_name];
-						$.extend(jv, r.message);
-						loaddoc("Journal Voucher", jv_name);
+						var doclist = frappe.model.sync(r.message);
+						frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
+
 					}
 				}
 			});
@@ -266,20 +265,20 @@
 
 	customer: function() {
 		this.get_party_details({
-			party: this.frm.doc.customer, 
-			party_type:"Customer", 
+			party: this.frm.doc.customer,
+			party_type:"Customer",
 			doctype: this.frm.doc.doctype
 		});
 	},
 
 	supplier: function() {
 		this.get_party_details({
-			party: this.frm.doc.supplier, 
-			party_type:"Supplier", 
+			party: this.frm.doc.supplier,
+			party_type:"Supplier",
 			doctype: this.frm.doc.doctype
 		});
 	},
-	
+
 	get_party_details: function(args) {
 		var me = this;
 		frappe.call({
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 7629c3c..861d967 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -780,14 +780,10 @@
 	from erpnext.accounts.utils import get_balance_on
 	for r in result:
 		jv.append("entries", {
-			"__islocal": 1,
-			"doctype": "Journal Voucher Detail",
-			"parentfield": "entries",
 			"account": r.get("account"),
 			"against_invoice": r.get("against_invoice"),
 			"against_voucher": r.get("against_voucher"),
-			"balance": get_balance_on(r.get("account"), se.posting_date) \
-				if r.get("account") else 0
+			"balance": get_balance_on(r.get("account"), se.posting_date) if r.get("account") else 0
 		})
 
 	return jv