[minor] patch for billed in DN and PR
diff --git a/erpnext/change_log/v6/v6.16.3.md b/erpnext/change_log/v6/v6.16.3.md
index ee0279b..7fe1ad5 100644
--- a/erpnext/change_log/v6/v6.16.3.md
+++ b/erpnext/change_log/v6/v6.16.3.md
@@ -1,4 +1,5 @@
 - Track billed status of a Delivery Note (DN) or Purchase Receipt (PR)
+	- Added new status **To Bill** and **Completed** in Delivery Note and Purchase Receipt
 	- The system looks for Invoices directly created from Delivery Note / Purchase Receipt *(Workflow: Order -> DN/PR -> Invoice)* and allocates these amounts directly to the DN/PR
 	- Next, it looks for Invoices not created from Delivery Note / Purchase Receipt, but from an Order *(Workflow: Order -> Invoice, Order -> DN/PR)* and the DN/PR is selected based on FIFO for allocating the billed amount
 - In Stock Entry, re-calculate Additional Costs and Amount on changing Quantity or Rate
diff --git a/erpnext/patches/v6_16/update_billing_status_in_dn_and_pr.py b/erpnext/patches/v6_16/update_billing_status_in_dn_and_pr.py
index 30ddf22..b660d39 100644
--- a/erpnext/patches/v6_16/update_billing_status_in_dn_and_pr.py
+++ b/erpnext/patches/v6_16/update_billing_status_in_dn_and_pr.py
@@ -10,24 +10,33 @@
 		frappe.reload_doctype(dt + " Item")
 
 	# Update billed_amt in DN and PR which are not against any order
-	for d in frappe.db.sql("""select name from `tabDelivery Note Item`
+	for d in frappe.db.sql("""select name from `tabDelivery Note Item` item
 		where (so_detail is null or so_detail = '') and docstatus=1""", as_dict=1):
-			billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
-				where dn_detail=%s and docstatus=1""", d.name)
-			billed_amt = billed_amt and billed_amt[0][0] or 0
-			frappe.db.set_value("Delivery Note Item", d.name, "billed_amt", billed_amt, update_modified=False)
+
+		billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
+			where dn_detail=%s and docstatus=1""", d.name)
+		billed_amt = billed_amt and billed_amt[0][0] or 0
+		frappe.db.set_value("Delivery Note Item", d.name, "billed_amt", billed_amt, update_modified=False)
+
+		frappe.db.commit()
 
 	# Update billed_amt in DN and PR which are not against any order
-	for d in frappe.db.sql("""select name from `tabPurchase Receipt Item`
+	for d in frappe.db.sql("""select name from `tabPurchase Receipt Item` item
 		where (prevdoc_detail_docname is null or prevdoc_detail_docname = '') and docstatus=1""", as_dict=1):
-			billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
-				where pr_detail=%s and docstatus=1""", d.name)
-			billed_amt = billed_amt and billed_amt[0][0] or 0
-			frappe.db.set_value("Purchase Receipt Item", d.name, "billed_amt", billed_amt, update_modified=False)
 
-	# Update billed amt which are against order or invoice
-	# Update billing status for all
-	for d in frappe.db.sql("select name from `tab{0}` where docstatus=1".format(dt), as_dict=1):
-		doc = frappe.get_doc(dt, d.name)
-		doc.update_billing_status(update_modified=False)
-		doc.set_status(update=True, update_modified=False)
+		billed_amt = frappe.db.sql("""select sum(amount) from `tabPurchase Invoice Item`
+			where pr_detail=%s and docstatus=1""", d.name)
+		billed_amt = billed_amt and billed_amt[0][0] or 0
+		frappe.db.set_value("Purchase Receipt Item", d.name, "billed_amt", billed_amt, update_modified=False)
+
+		frappe.db.commit()
+
+	for dt in ("Delivery Note", "Purchase Receipt"):
+		# Update billed amt which are against order or invoice
+		# Update billing status for all
+		for d in frappe.db.sql("select name from `tab{0}` where docstatus=1".format(dt), as_dict=1):
+			doc = frappe.get_doc(dt, d.name)
+			doc.update_billing_status(update_modified=False)
+			doc.set_status(update=True, update_modified=False)
+
+			frappe.db.commit()
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index 2f0550e..c5a3bab 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -47,7 +47,7 @@
 				this.show_general_ledger();
 			}
 			if (this.frm.has_perm("submit") && (doc.status !== "Closed")
-				&& this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
+				&& this.frm.doc.__onload && !this.frm.doc.__onload.has_return_entry) {
 					cur_frm.add_custom_button(__("Close"), this.close_delivery_note)
 			}
 		}
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index 9a24eed..eec1a6d 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -61,7 +61,7 @@
 						 this.make_purchase_invoice).addClass("btn-primary");
 				}
 				if (this.frm.has_perm("submit") &&
-					this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
+					this.frm.doc.__onload && !this.frm.doc.__onload.has_return_entry) {
 						cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt)
 				}
 			}