diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index 675d49aab22..a764a006b0c 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -7,6 +7,35 @@ const SALES_DOCTYPES = ["Quotation", "Sales Order", "Delivery Note", "Sales Invo const PURCHASE_DOCTYPES = ["Purchase Order", "Purchase Receipt", "Purchase Invoice"]; frappe.ui.form.on("Item", { + valuation_method(frm) { + if (!frm.is_new() && frm.doc.valuation_method === "Moving Average") { + let stock_exists = frm.doc.__onload && frm.doc.__onload.stock_exists ? 1 : 0; + let current_valuation_method = frm.doc.__onload.current_valuation_method; + + if (stock_exists && current_valuation_method !== frm.doc.valuation_method) { + let msg = __( + "Changing the valuation method to Moving Average will affect new transactions. If backdated entries are added, earlier FIFO-based entries will be reposted, which may change closing balances." + ); + msg += "
"; + msg += __( + "Also you can't switch back to FIFO after setting the valuation method to Moving Average for this item." + ); + msg += "
"; + msg += __("Do you want to change valuation method?"); + + frappe.confirm( + msg, + () => { + frm.set_value("valuation_method", "Moving Average"); + }, + () => { + frm.set_value("valuation_method", current_valuation_method); + } + ); + } + } + }, + setup: function (frm) { frm.add_fetch("attribute", "numeric_values", "numeric_values"); frm.add_fetch("attribute", "from_range", "from_range"); diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 25d2e5ec614..fe40f748646 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -33,6 +33,7 @@ from erpnext.controllers.item_variant import ( validate_item_variant_attributes, ) from erpnext.stock.doctype.item_default.item_default import ItemDefault +from erpnext.stock.utils import get_valuation_method class DuplicateReorderRows(frappe.ValidationError): @@ -153,6 +154,7 @@ class Item(Document): def onload(self): self.set_onload("stock_exists", self.stock_ledger_created()) self.set_onload("asset_naming_series", get_asset_naming_series()) + self.set_onload("current_valuation_method", get_valuation_method(self.name)) def autoname(self): if frappe.db.get_default("item_naming_by") == "Naming Series":