Merge pull request #32527 from s-aga-r/refactor/buying/reports

refactor: rewrite buying reports queries in qb
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index ec861a2..c3a9855 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -31,7 +31,7 @@
 		super.onload();
 
 		// Ignore linked advances
-		this.frm.ignore_doctypes_on_cancel_all = ['Journal Entry', 'Payment Entry'];
+		this.frm.ignore_doctypes_on_cancel_all = ['Journal Entry', 'Payment Entry', 'Purchase Invoice'];
 
 		if(!this.frm.doc.__islocal) {
 			// show credit_to in print format
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 5dbe7eb..3d74b8f 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -1415,6 +1415,7 @@
 			"Stock Ledger Entry",
 			"Repost Item Valuation",
 			"Payment Ledger Entry",
+			"Purchase Invoice",
 		)
 		self.update_advance_tax_references(cancel=1)
 
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 67574ca..6667454 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -1368,9 +1368,8 @@
 			frappe.delete_doc("Desktop Icon", icon)
 
 
-def create_payment_ledger_entry(
-	gl_entries, cancel=0, adv_adj=0, update_outstanding="Yes", from_repost=0
-):
+def get_payment_ledger_entries(gl_entries, cancel=0):
+	ple_map = []
 	if gl_entries:
 		ple = None
 
@@ -1410,44 +1409,57 @@
 					dr_or_cr *= -1
 					dr_or_cr_account_currency *= -1
 
-				ple = frappe.get_doc(
-					{
-						"doctype": "Payment Ledger Entry",
-						"posting_date": gle.posting_date,
-						"company": gle.company,
-						"account_type": account_type,
-						"account": gle.account,
-						"party_type": gle.party_type,
-						"party": gle.party,
-						"cost_center": gle.cost_center,
-						"finance_book": gle.finance_book,
-						"due_date": gle.due_date,
-						"voucher_type": gle.voucher_type,
-						"voucher_no": gle.voucher_no,
-						"against_voucher_type": gle.against_voucher_type
-						if gle.against_voucher_type
-						else gle.voucher_type,
-						"against_voucher_no": gle.against_voucher if gle.against_voucher else gle.voucher_no,
-						"account_currency": gle.account_currency,
-						"amount": dr_or_cr,
-						"amount_in_account_currency": dr_or_cr_account_currency,
-						"delinked": True if cancel else False,
-						"remarks": gle.remarks,
-					}
+				ple = frappe._dict(
+					doctype="Payment Ledger Entry",
+					posting_date=gle.posting_date,
+					company=gle.company,
+					account_type=account_type,
+					account=gle.account,
+					party_type=gle.party_type,
+					party=gle.party,
+					cost_center=gle.cost_center,
+					finance_book=gle.finance_book,
+					due_date=gle.due_date,
+					voucher_type=gle.voucher_type,
+					voucher_no=gle.voucher_no,
+					against_voucher_type=gle.against_voucher_type
+					if gle.against_voucher_type
+					else gle.voucher_type,
+					against_voucher_no=gle.against_voucher if gle.against_voucher else gle.voucher_no,
+					account_currency=gle.account_currency,
+					amount=dr_or_cr,
+					amount_in_account_currency=dr_or_cr_account_currency,
+					delinked=True if cancel else False,
+					remarks=gle.remarks,
 				)
 
 				dimensions_and_defaults = get_dimensions()
 				if dimensions_and_defaults:
 					for dimension in dimensions_and_defaults[0]:
-						ple.set(dimension.fieldname, gle.get(dimension.fieldname))
+						ple[dimension.fieldname] = gle.get(dimension.fieldname)
 
-				if cancel:
-					delink_original_entry(ple)
-				ple.flags.ignore_permissions = 1
-				ple.flags.adv_adj = adv_adj
-				ple.flags.from_repost = from_repost
-				ple.flags.update_outstanding = update_outstanding
-				ple.submit()
+				ple_map.append(ple)
+	return ple_map
+
+
+def create_payment_ledger_entry(
+	gl_entries, cancel=0, adv_adj=0, update_outstanding="Yes", from_repost=0
+):
+	if gl_entries:
+		ple_map = get_payment_ledger_entries(gl_entries, cancel=cancel)
+
+		for entry in ple_map:
+
+			ple = frappe.get_doc(entry)
+
+			if cancel:
+				delink_original_entry(ple)
+
+			ple.flags.ignore_permissions = 1
+			ple.flags.adv_adj = adv_adj
+			ple.flags.from_repost = from_repost
+			ple.flags.update_outstanding = update_outstanding
+			ple.submit()
 
 
 def update_voucher_outstanding(voucher_type, voucher_no, account, party_type, party):