fix: AR/AP report based on payment terms (#36574)

* fix: AR/AP report based on payment terms

* fix: AR/AP report based on payment terms
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 11bbb6f..f78a840 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -436,12 +436,11 @@
 	def allocate_outstanding_based_on_payment_terms(self, row):
 		self.get_payment_terms(row)
 		for term in row.payment_terms:
-
-			# update "paid" and "oustanding" for this term
+			# update "paid" and "outstanding" for this term
 			if not term.paid:
 				self.allocate_closing_to_term(row, term, "paid")
 
-			# update "credit_note" and "oustanding" for this term
+			# update "credit_note" and "outstanding" for this term
 			if term.outstanding:
 				self.allocate_closing_to_term(row, term, "credit_note")
 
@@ -453,7 +452,8 @@
 			"""
 			select
 				si.name, si.party_account_currency, si.currency, si.conversion_rate,
-				ps.due_date, ps.payment_term, ps.payment_amount, ps.description, ps.paid_amount, ps.discounted_amount
+				si.total_advance, ps.due_date, ps.payment_term, ps.payment_amount, ps.base_payment_amount,
+				ps.description, ps.paid_amount, ps.discounted_amount
 			from `tab{0}` si, `tabPayment Schedule` ps
 			where
 				si.name = ps.parent and
@@ -469,6 +469,10 @@
 		original_row = frappe._dict(row)
 		row.payment_terms = []
 
+		# Advance allocated during invoicing is not considered in payment terms
+		# Deduct that from paid amount pre allocation
+		row.paid -= flt(payment_terms_details[0].total_advance)
+
 		# If no or single payment terms, no need to split the row
 		if len(payment_terms_details) <= 1:
 			return
@@ -483,7 +487,7 @@
 		) and d.currency == d.party_account_currency:
 			invoiced = d.payment_amount
 		else:
-			invoiced = flt(flt(d.payment_amount) * flt(d.conversion_rate), self.currency_precision)
+			invoiced = d.base_payment_amount
 
 		row.payment_terms.append(
 			term.update(