[Fix] Per billed showing as 100% in the sales order even if sales return has made against the sales order (#14899)

* [Fix] Per billed showing as 100% in the sales order even if sales return has made against the sales order

* Added test cases
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index 75792f8..deb78ab 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -643,6 +643,66 @@
    "allow_bulk_edit": 0,
    "allow_on_submit": 0,
    "bold": 0,
+   "collapsible": 0,
+   "columns": 0,
+   "fieldname": "column_break_21",
+   "fieldtype": "Column Break",
+   "hidden": 0,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_standard_filter": 0,
+   "length": 0,
+   "no_copy": 0,
+   "permlevel": 0,
+   "precision": "",
+   "print_hide": 0,
+   "print_hide_if_no_value": 0,
+   "read_only": 0,
+   "remember_last_selected_value": 0,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "set_only_once": 0,
+   "unique": 0
+  },
+  {
+   "allow_bulk_edit": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "columns": 0,
+   "depends_on": "eval: doc.is_return && doc.return_against",
+   "fieldname": "update_billed_amount_in_sales_order",
+   "fieldtype": "Check",
+   "hidden": 0,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_standard_filter": 0,
+   "label": "Update Billed Amount in Sales Order",
+   "length": 0,
+   "no_copy": 0,
+   "permlevel": 0,
+   "precision": "",
+   "print_hide": 0,
+   "print_hide_if_no_value": 0,
+   "read_only": 0,
+   "remember_last_selected_value": 0,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "set_only_once": 0,
+   "unique": 0
+  },
+  {
+   "allow_bulk_edit": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
    "collapsible": 1,
    "collapsible_depends_on": "po_no",
    "columns": 0,
@@ -4713,7 +4773,7 @@
  "istable": 0,
  "max_attachments": 0,
  "menu_index": 0,
- "modified": "2018-07-06 12:09:02.039783",
+ "modified": "2018-07-17 13:46:52.449142",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Sales Invoice",
@@ -4804,7 +4864,7 @@
  "quick_entry": 1,
  "read_only": 0,
  "read_only_onload": 1,
- "search_fields": "posting_date, due_date, customer, base_grand_total, outstanding_amount",
+ "search_fields": "posting_date,due_date,customer,base_grand_total,outstanding_amount",
  "show_name_in_global_search": 1,
  "sort_field": "modified",
  "sort_order": "DESC",
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 56767c2..27d35eb 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -116,7 +116,7 @@
 
 		self.check_prev_docstatus()
 
-		if self.is_return:
+		if self.is_return and not self.update_billed_amount_in_sales_order:
 			# NOTE status updating bypassed for is_return
 			self.status_updater = []
 
@@ -161,7 +161,7 @@
 		if frappe.db.get_single_value('Accounts Settings', 'unlink_payment_on_cancellation_of_invoice'):
 			unlink_ref_doc_from_payment_entries(self)
 
-		if self.is_return:
+		if self.is_return and not self.update_billed_amount_in_sales_order:
 			# NOTE status updating bypassed for is_return
 			self.status_updater = []
 
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index 5e5c50d..bd03bfd 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -60,6 +60,21 @@
 		si1 = make_sales_invoice(so.name)
 		self.assertEquals(len(si1.get("items")), 0)
 
+	def test_so_billed_amount_against_return_entry(self):
+		from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_sales_return
+		so = make_sales_order(do_not_submit=True)
+		so.submit()
+
+		si = make_sales_invoice(so.name)
+		si.insert()
+		si.submit()
+
+		si1 = make_sales_return(si.name)
+		si1.update_billed_amount_in_sales_order = 1
+		si1.submit()
+		so.load_from_db()
+		self.assertEquals(so.per_billed, 0)
+
 	def test_make_sales_invoice_with_terms(self):
 		so = make_sales_order(do_not_submit=True)