Merge pull request #4643 from nabinhait/status_button

[fix] Close button visible in all sumitted PR and DN
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index d4e7932..0b6f47e 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -50,14 +50,12 @@
 			if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
 				this.show_general_ledger();
 			}
-			if (this.frm.has_perm("submit") && (doc.status !== "Closed")
-				&& this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
-					cur_frm.add_custom_button(__("Close"), this.close_delivery_note, __("Status"))
+			if (this.frm.has_perm("submit") && doc.status !== "Closed") {
+				cur_frm.add_custom_button(__("Close"), this.close_delivery_note, __("Status"))
 			}
 		}
 
-		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1
-				&& !doc.is_return && doc.status!="Closed") {
+		if(doc.docstatus==1 && !doc.is_return && doc.status!="Closed" && flt(doc.per_billed) < 100) {
 			// show Make Invoice button only if Delivery Note is not created from Sales Invoice
 			var from_sales_invoice = false;
 			from_sales_invoice = cur_frm.doc.items.some(function(item) {
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index b553054..7cb855f 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -61,10 +61,6 @@
 			'extra_cond': """ and exists (select name from `tabDelivery Note` where name=`tabDelivery Note Item`.parent and is_return=1)"""
 		}]
 
-	def onload(self):
-		self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note",
-			"is_return": 1, "return_against": self.name, "docstatus": 1})))
-
 	def before_print(self):
 		def toggle_print_hide(meta, fieldname):
 			df = meta.get_field(fieldname)
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index d66f1a1..7cf7ae9 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -55,14 +55,13 @@
 			}
 
 			if(this.frm.doc.docstatus == 1 && this.frm.doc.status!="Closed") {
-				if (this.frm.has_perm("submit") && 
-					this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
-						cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt, __("Status"))
+				if (this.frm.has_perm("submit")) {
+					cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt, __("Status"))
 				}
 				
 				cur_frm.add_custom_button(__('Return'), this.make_purchase_return, __("Make"));
 				
-				if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
+				if(flt(this.frm.doc.per_billed) < 100) {
 					cur_frm.add_custom_button(__('Invoice'), this.make_purchase_invoice, __("Make"));
 				}
 				cur_frm.page.set_inner_btn_group_as_primary(__("Make"));
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index df62f7d..1d87238 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -46,16 +46,6 @@
 			'extra_cond': """ and exists (select name from `tabPurchase Receipt` where name=`tabPurchase Receipt Item`.parent and is_return=1)"""
 		}]
 
-	def onload(self):
-		billed_qty = frappe.db.sql("""select sum(qty) from `tabPurchase Invoice Item`
-			where purchase_receipt=%s and docstatus=1""", self.name)
-		if billed_qty:
-			total_qty = sum((item.qty for item in self.get("items")))
-			self.set_onload("billing_complete", (billed_qty[0][0] == total_qty))
-
-		self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Purchase Receipt",
-			"is_return": 1, "return_against": self.name, "docstatus": 1})))
-
 	def validate(self):
 		super(PurchaseReceipt, self).validate()