From 1be94efcfaeff64ade3579f15e59b46ef01e440a Mon Sep 17 00:00:00 2001
From: Nabin Hait <nabinhait@gmail.com>
Date: Fri, 17 Jul 2015 17:25:52 +0530
Subject: [PATCH] [fix] Fetch debit/credit from reference journal entry

---
 erpnext/accounts/doctype/journal_entry/journal_entry.js | 5 +++--
 erpnext/accounts/doctype/journal_entry/journal_entry.py | 8 +++++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js
index 3ea92e1b19..1ecde6493e 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.js
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js
@@ -109,7 +109,7 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({
 
 	against_jv: function(doc, cdt, cdn) {
 		var d = frappe.get_doc(cdt, cdn);
-		if (d.against_jv && d.party && !flt(d.credit) && !flt(d.debit)) {
+		if (d.against_jv && !flt(d.credit) && !flt(d.debit)) {
 			this.get_outstanding('Journal Entry', d.against_jv, d);
 		}
 	},
@@ -119,7 +119,8 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({
 		var args = {
 			"doctype": doctype,
 			"docname": docname,
-			"party": child.party
+			"party": child.party,
+			"account": child.account
 		}
 
 		return this.frm.call({
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 23d4c7380e..dc620edcf0 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -545,12 +545,14 @@ def get_against_jv(doctype, txt, searchfield, start, page_len, filters):
 @frappe.whitelist()
 def get_outstanding(args):
 	args = eval(args)
-	if args.get("doctype") == "Journal Entry" and args.get("party"):
+	if args.get("doctype") == "Journal Entry":
+		condition = " and party=%(party)s" if args.get("party") else ""
+			
 		against_jv_amount = frappe.db.sql("""
 			select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
-			from `tabJournal Entry Account` where parent=%s and party=%s
+			from `tabJournal Entry Account` where parent=%(docname)s and account=%(account)s {0}
 			and ifnull(against_invoice, '')='' and ifnull(against_voucher, '')=''
-			and ifnull(against_jv, '')=''""", (args['docname'], args['party']))
+			and ifnull(against_jv, '')=''""".format(condition), args)
 
 		against_jv_amount = flt(against_jv_amount[0][0]) if against_jv_amount else 0
 		if against_jv_amount > 0: