diff --git a/erpnext/stock/doctype/batch/batch.js b/erpnext/stock/doctype/batch/batch.js index a485f849639..7c9c0eeb09a 100644 --- a/erpnext/stock/doctype/batch/batch.js +++ b/erpnext/stock/doctype/batch/batch.js @@ -22,6 +22,17 @@ frappe.ui.form.on("Batch", { frappe.set_route("query-report", "Stock Ledger"); }); frm.trigger("make_dashboard"); + + frm.add_custom_button(__("Recalculate Batch Qty"), () => { + frm.call({ + method: "recalculate_batch_qty", + doc: frm.doc, + freeze: true, + callback: () => { + frm.reload_doc(); + }, + }); + }); } }, item: (frm) => { diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 29ebb710923..ad8218cfcfb 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -156,6 +156,17 @@ class Batch(Document): if frappe.db.get_value("Item", self.item, "has_batch_no") == 0: frappe.throw(_("The selected item cannot have Batch")) + @frappe.whitelist() + def recalculate_batch_qty(self): + batches = get_batch_qty(batch_no=self.name, item_code=self.item) + batch_qty = 0.0 + if batches: + for row in batches: + batch_qty += row.get("qty") + + self.db_set("batch_qty", batch_qty) + frappe.msgprint(_("Batch Qty updated to {0}").format(batch_qty), alert=True) + def set_batchwise_valuation(self): from erpnext.stock.utils import get_valuation_method