Merge pull request #56993 from frappe/mergify/bp/version-15-hotfix/pr-56925

perf: batch bin lookups in delivery note stock update (backport #56925)
This commit is contained in:
Mihir Kandoi
2026-07-09 21:22:59 +05:30
committed by GitHub

View File

@@ -442,22 +442,34 @@ class DeliveryNote(SellingController):
frappe.throw(_("Warehouse required for stock Item {0}").format(d["item_code"])) frappe.throw(_("Warehouse required for stock Item {0}").format(d["item_code"]))
def update_current_stock(self): def update_current_stock(self):
if self.get("_action") and self._action != "update_after_submit": if not (self.get("_action") and self._action != "update_after_submit"):
for d in self.get("items"): return
d.actual_qty = frappe.db.get_value(
"Bin", {"item_code": d.item_code, "warehouse": d.warehouse}, "actual_qty"
)
for d in self.get("packed_items"): warehouse_item_codes = {}
bin_qty = frappe.db.get_value( for d in self.get("items") + self.get("packed_items"):
"Bin", warehouse_item_codes.setdefault(d.warehouse, set()).add(d.item_code)
{"item_code": d.item_code, "warehouse": d.warehouse},
["actual_qty", "projected_qty"], if not warehouse_item_codes:
as_dict=True, return
)
if bin_qty: bin_map = {}
d.actual_qty = flt(bin_qty.actual_qty) for warehouse, item_codes in warehouse_item_codes.items():
d.projected_qty = flt(bin_qty.projected_qty) for b in frappe.get_all(
"Bin",
filters={"item_code": ["in", item_codes], "warehouse": warehouse},
fields=["item_code", "actual_qty", "projected_qty"],
):
bin_map[(b.item_code, warehouse)] = b
for d in self.get("items"):
bin_data = bin_map.get((d.item_code, d.warehouse))
d.actual_qty = bin_data.actual_qty if bin_data else None
for d in self.get("packed_items"):
bin_data = bin_map.get((d.item_code, d.warehouse))
if bin_data:
d.actual_qty = flt(bin_data.actual_qty)
d.projected_qty = flt(bin_data.projected_qty)
def on_submit(self): def on_submit(self):
self.validate_packed_qty() self.validate_packed_qty()