From 2a400dd3f8d25568f95e58152f55c661138bee76 Mon Sep 17 00:00:00 2001 From: Sanket Shah <113279972+Sanket322@users.noreply.github.com> Date: Thu, 23 Jan 2025 11:29:41 +0530 Subject: [PATCH] perf: optimize DB calls with frappe.get_all (#45289) * perf: reduce multiple db queries * fix: use frappe._dict instread of extra iteration --------- Co-authored-by: Sanket322 --- erpnext/controllers/selling_controller.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 62475f73048..c2984839fd6 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -711,6 +711,16 @@ class SellingController(StockController): if self.doctype == "POS Invoice": return + items = [item.item_code for item in self.get("items")] + item_stock_map = frappe._dict( + frappe.get_all( + "Item", + filters={"item_code": ["in", items]}, + fields=["item_code", "is_stock_item"], + as_list=True, + ) + ) + for d in self.get("items"): if self.doctype == "Sales Invoice": stock_items = [ @@ -744,7 +754,7 @@ class SellingController(StockController): frappe.bold(_("Allow Item to Be Added Multiple Times in a Transaction")), get_link_to_form("Selling Settings", "Selling Settings"), ) - if frappe.db.get_value("Item", d.item_code, "is_stock_item") == 1: + if item_stock_map.get(d.item_code): if stock_items in check_list: frappe.throw(duplicate_items_msg) else: