diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 633a231f816..fc2608e30a4 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1025,23 +1025,11 @@ class SalesInvoice(SellingController): frappe.throw(_("Could not update stock, invoice contains drop shipping item.")) def update_current_stock(self): - for d in self.get("items"): - if d.item_code and d.warehouse: - bin = frappe.db.sql( - "select actual_qty from `tabBin` where item_code = %s and warehouse = %s", - (d.item_code, d.warehouse), - as_dict=1, - ) - d.actual_qty = bin and flt(bin[0]["actual_qty"]) or 0 + for item in self.items: + item.set_actual_qty() - for d in self.get("packed_items"): - bin = frappe.db.sql( - "select actual_qty, projected_qty from `tabBin` where item_code = %s and warehouse = %s", - (d.item_code, d.warehouse), - as_dict=1, - ) - d.actual_qty = bin and flt(bin[0]["actual_qty"]) or 0 - d.projected_qty = bin and flt(bin[0]["projected_qty"]) or 0 + for packed_item in self.packed_items: + packed_item.set_actual_and_projected_qty() def update_packing_list(self): if cint(self.update_stock) == 1: diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py index 989a8ca2c9b..dc40bba8e20 100644 --- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py +++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py @@ -102,3 +102,12 @@ class SalesInvoiceItem(Document): frappe.bold(self.idx), frappe.bold(self.cost_center), frappe.bold(company) ) ) + + def set_actual_qty(self): + if self.item_code and self.warehouse: + self.actual_qty = ( + frappe.db.get_value( + "Bin", {"item_code": self.item_code, "warehouse": self.warehouse}, "actual_qty" + ) + or 0 + ) diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py index c115e33e171..bd14e358371 100644 --- a/erpnext/stock/doctype/packed_item/packed_item.py +++ b/erpnext/stock/doctype/packed_item/packed_item.py @@ -51,7 +51,16 @@ class PackedItem(Document): warehouse: DF.Link | None # end: auto-generated types - pass + def set_actual_and_projected_qty(self): + "Set actual and projected qty based on warehouse and item_code" + _bin = frappe.db.get_value( + "Bin", + {"item_code": self.item_code, "warehouse": self.warehouse}, + ["actual_qty", "projected_qty"], + as_dict=True, + ) + self.actual_qty = _bin.actual_qty if _bin else 0 + self.projected_qty = _bin.projected_qty if _bin else 0 def make_packing_list(doc):