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>
This commit is contained in:
Sanket Shah
2025-01-23 11:29:41 +05:30
committed by GitHub
parent 551fa500e8
commit 2a400dd3f8

View File

@@ -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: