diff --git a/erpnext/stock/doctype/purchase_receipt/mapper.py b/erpnext/stock/doctype/purchase_receipt/mapper.py index 9ea7371554a..273afcec7a9 100644 --- a/erpnext/stock/doctype/purchase_receipt/mapper.py +++ b/erpnext/stock/doctype/purchase_receipt/mapper.py @@ -21,18 +21,15 @@ from erpnext.stock.serial_batch_bundle import ( def get_invoiced_qty_map(purchase_receipt: str) -> dict: """returns a map: {pr_detail: invoiced_qty}""" - invoiced_qty_map = {} + pi_item = frappe.qb.DocType("Purchase Invoice Item") + query = ( + frappe.qb.from_(pi_item) + .select(pi_item.pr_detail, Sum(pi_item.qty).as_("qty")) + .where((pi_item.purchase_receipt == purchase_receipt) & (pi_item.docstatus == 1)) + .groupby(pi_item.pr_detail) + ).run(as_list=1) - for pr_detail, qty in frappe.db.sql( - """select pr_detail, qty from `tabPurchase Invoice Item` - where purchase_receipt=%s and docstatus=1""", - purchase_receipt, - ): - if not invoiced_qty_map.get(pr_detail): - invoiced_qty_map[pr_detail] = 0 - invoiced_qty_map[pr_detail] += qty - - return invoiced_qty_map + return frappe._dict(query) if query else frappe._dict() def get_returned_qty_map(purchase_receipt: str) -> dict: