[fix] journal entry get_query
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js
index 38f3e0c..eac8f44 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.js
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js
@@ -77,13 +77,21 @@
 
 			frappe.model.validate_missing(jvd, "party_type");
 			frappe.model.validate_missing(jvd, "party");
-			return {
+
+			var out = {
 				filters: [
-					[jvd.reference_type, jvd.reference_type.indexOf("Sales")==1 ? "customer" : "supplier", "=", jvd.party],
+					[jvd.reference_type, jvd.reference_type.indexOf("Sales")===0 ? "customer" : "supplier", "=", jvd.party],
 					[jvd.reference_type, "docstatus", "=", 1],
-					[jvd.reference_type, "outstanding_amount", "!=", 0]
 				]
 			};
+
+			if(in_list(["Sales Invoice", "Purchase Invoice"], jvd.reference_type)) {
+				out.filters.push([jvd.reference_type, "outstanding_amount", "!=", 0]);
+			} else {
+				out.filters.push([jvd.reference_type, "per_billed", "<", 100]);
+			}
+
+			return out;
 		});
 
 
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index b48e46c..00e2e91 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -118,7 +118,7 @@
 
 	def validate_against_jv(self):
 		for d in self.get('accounts'):
-			if d.reference_type=="Journal Voucher":
+			if d.reference_type=="Journal Entry":
 				account_root_type = frappe.db.get_value("Account", d.account, "root_type")
 				if account_root_type == "Asset" and flt(d.debit) > 0:
 					frappe.throw(_("For {0}, only credit accounts can be linked against another debit entry")
@@ -132,7 +132,7 @@
 
 				against_entries = frappe.db.sql("""select * from `tabJournal Entry Account`
 					where account = %s and docstatus = 1 and parent = %s
-					and ifnull(reference_type, '') = '' and ifnull(reference_name, '') = ''
+					and ifnull(reference_type, '') in ("", "Sales Order", "Purchase Order")
 					""", (d.account, d.reference_name), as_dict=True)
 
 				if not against_entries:
@@ -163,8 +163,8 @@
 		for d in self.get("accounts"):
 			if not d.reference_type:
 				d.reference_name = None
-			if not d.reference_type:
-				d.reference_name = None
+			if not d.reference_name:
+				d.reference_type = None
 			if d.reference_type and d.reference_name and (d.reference_type in field_dict.keys()):
 				dr_or_cr = "credit" if d.reference_type in ("Sales Order", "Sales Invoice") \
 					else "debit"
diff --git a/erpnext/patches/v5_4/cleanup_journal_entry.py b/erpnext/patches/v5_4/cleanup_journal_entry.py
index 9968e3c..b8edbaa 100644
--- a/erpnext/patches/v5_4/cleanup_journal_entry.py
+++ b/erpnext/patches/v5_4/cleanup_journal_entry.py
@@ -2,10 +2,10 @@
 
 def execute():
 	for doctype, fieldname in (
-		("Sales Invoice", "against_invoice"),
-		("Purchase Invoice", "against_voucher"),
 		("Sales Order", "against_sales_order"),
 		("Purchase Order", "against_purchase_order"),
+		("Sales Invoice", "against_invoice"),
+		("Purchase Invoice", "against_voucher"),
 		("Journal Entry", "against_jv"),
 		("Expense Claim", "against_expense_claim"),
 	):