Skip to content

Commit

Permalink
perf: optimize DB calls with frappe.get_all (backport #45289) (#45391)
Browse files Browse the repository at this point in the history
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 <shahsanket322003.com>
(cherry picked from commit 2a400dd)

Co-authored-by: Sanket Shah <[email protected]>
  • Loading branch information
mergify[bot] and Sanket322 authored Jan 23, 2025
1 parent 412e22f commit 04f5a72
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion erpnext/controllers/selling_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ def validate_for_duplicate_items(self):
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 = [
Expand Down Expand Up @@ -747,7 +757,7 @@ def validate_for_duplicate_items(self):
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:
Expand Down

0 comments on commit 04f5a72

Please sign in to comment.