From 04f5a72e0847f66da120e0f81fd2aefc7eac94cf Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 23 Jan 2025 13:01:12 +0530 Subject: [PATCH] perf: optimize DB calls with frappe.get_all (backport #45289) (#45391) 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 (cherry picked from commit 2a400dd3f8d25568f95e58152f55c661138bee76) Co-authored-by: Sanket Shah <113279972+Sanket322@users.noreply.github.com> --- 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 b704cb30791..a9258204b39 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -714,6 +714,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 = [ @@ -747,7 +757,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: