Show Close button in Delivery Note and Purchase Receipt, only if it is returned and user has submit permission
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index 9cd4936..4bef0d8 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -46,13 +46,14 @@
 			if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
 				this.show_general_ledger();
 			}
-
-			if(doc.status !== "Closed") {
-				cur_frm.add_custom_button(__("Close"), this.close_delivery_note)
+			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)
 			}
 		}
 
-		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1 && !doc.is_return && doc.status!="Closed") {
+		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1 
+				&& !doc.is_return && doc.status!="Closed") {
 			// 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) {
@@ -63,7 +64,7 @@
 				cur_frm.add_custom_button(__('Invoice'), this.make_sales_invoice).addClass("btn-primary");
 		}
 
-		if(doc.docstatus==1 && doc.status === "Closed") {
+		if(doc.docstatus==1 && doc.status === "Closed" && this.frm.has_perm("submit")) {
 			cur_frm.add_custom_button(__('Re-open'), this.reopen_delivery_note)
 		}
 		erpnext.stock.delivery_note.set_print_hide(doc, dt, dn);
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 6346c63..60cc430 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -69,7 +69,10 @@
 			where docstatus=1 and delivery_note=%s""", self.name)
 		if billed_qty:
 			total_qty = sum((item.qty for item in self.get("items")))
-			self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
+			self.set_onload("billing_complete", (billed_qty[0][0] == total_qty))
+			
+		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):
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index acf6809..af29cb8 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -57,14 +57,18 @@
 			if(this.frm.doc.docstatus == 1 && this.frm.doc.status!="Closed") {
 				cur_frm.add_custom_button(__('Return'), this.make_purchase_return);
 				if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
-					cur_frm.add_custom_button(__('Invoice'), this.make_purchase_invoice).addClass("btn-primary");
+					cur_frm.add_custom_button(__('Invoice'),
+						 this.make_purchase_invoice).addClass("btn-primary");
 				}
-				cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt)
+				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)
+				}
 			}
 		}
 
 
-		if(this.frm.doc.docstatus==1 && this.frm.doc.status === "Closed") {
+		if(this.frm.doc.docstatus==1 && this.frm.doc.status === "Closed" && this.frm.has_perm("submit")) {
 			cur_frm.add_custom_button(__('Re-open'), this.reopen_purchase_receipt)
 		}
 
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index c1316f9..edd7be8 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -51,7 +51,10 @@
 			where purchase_receipt=%s and docstatus=1""", self.name)
 		if billed_qty:
 			total_qty = sum((item.qty for item in self.get("items")))
-			self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
+			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()