revert: "Merge pull request #29290 from s-aga-r/fix/delivery-note/billed-amount" (#29782) (#29807)

* Revert "Merge pull request #29290 from s-aga-r/fix/delivery-note/billed-amount"

This reverts commit 038f94955006c88209f9df28e3a785c59a4ddb28, reversing
changes made to c7b491843476bca89be02851ccafb7e409876609.

* fix: linter

(cherry picked from commit 7fa46f77e0bdbc516b3c0cb0fb20594ee7fa398b)

# Conflicts:
#	erpnext/patches.txt

Co-authored-by: Sagar Sharma <sagarsharma.s312@gmail.com>
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 573da27..862ac81 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -1255,14 +1255,14 @@
 	def update_billing_status_in_dn(self, update_modified=True):
 		updated_delivery_notes = []
 		for d in self.get("items"):
-			if d.so_detail:
-				updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail, update_modified)
-			elif d.dn_detail:
+			if d.dn_detail:
 				billed_amt = frappe.db.sql("""select sum(amount) from `tabSales Invoice Item`
 					where dn_detail=%s and docstatus=1""", d.dn_detail)
 				billed_amt = billed_amt and billed_amt[0][0] or 0
 				frappe.db.set_value("Delivery Note Item", d.dn_detail, "billed_amt", billed_amt, update_modified=update_modified)
 				updated_delivery_notes.append(d.delivery_note)
+			elif d.so_detail:
+				updated_delivery_notes += update_billed_amount_based_on_so(d.so_detail, update_modified)
 
 		for dn in set(updated_delivery_notes):
 			frappe.get_doc("Delivery Note", dn).update_billing_percentage(update_modified=update_modified)
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 2a4d639..fbcc803 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -342,25 +342,21 @@
 	from frappe.query_builder.functions import Sum
 
 	# Billed against Sales Order directly
-	si = frappe.qb.DocType("Sales Invoice").as_("si")
 	si_item = frappe.qb.DocType("Sales Invoice Item").as_("si_item")
 	sum_amount = Sum(si_item.amount).as_("amount")
 
-	billed_against_so = frappe.qb.from_(si).from_(si_item).select(sum_amount).where(
-		(si_item.parent == si.name) &
+	billed_against_so = frappe.qb.from_(si_item).select(sum_amount).where(
 		(si_item.so_detail == so_detail) &
 		((si_item.dn_detail.isnull()) | (si_item.dn_detail == '')) &
-		(si_item.docstatus == 1) &
-		(si.update_stock == 0)
+		(si_item.docstatus == 1)
 	).run()
 	billed_against_so = billed_against_so and billed_against_so[0][0] or 0
 
 	# Get all Delivery Note Item rows against the Sales Order Item row
-
 	dn = frappe.qb.DocType("Delivery Note").as_("dn")
 	dn_item = frappe.qb.DocType("Delivery Note Item").as_("dn_item")
 
-	dn_details = frappe.qb.from_(dn).from_(dn_item).select(dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent, dn_item.stock_qty, dn_item.returned_qty).where(
+	dn_details = frappe.qb.from_(dn).from_(dn_item).select(dn_item.name, dn_item.amount, dn_item.si_detail, dn_item.parent).where(
 		(dn.name == dn_item.parent) &
 		(dn_item.so_detail == so_detail) &
 		(dn.docstatus == 1) &
@@ -385,11 +381,7 @@
 
 		# Distribute billed amount directly against SO between DNs based on FIFO
 		if billed_against_so and billed_amt_agianst_dn < dnd.amount:
-			if dnd.returned_qty:
-				pending_to_bill = flt(dnd.amount) * (dnd.stock_qty - dnd.returned_qty) / dnd.stock_qty
-			else:
-				pending_to_bill = flt(dnd.amount)
-			pending_to_bill -= billed_amt_agianst_dn
+			pending_to_bill = flt(dnd.amount) - billed_amt_agianst_dn
 			if pending_to_bill <= billed_against_so:
 				billed_amt_agianst_dn += pending_to_bill
 				billed_against_so -= pending_to_bill