fix(stock): validate new warehouse inventory account after naming

Move the insert-time check from before_insert to validate. before_insert
runs before set_new_name, so the validation message rendered the
warehouse name as None. validate runs after naming and only applies to
new documents via is_new().

Resolve inheritance through the parent's lft/rgt bounds instead of the
request-cached warehouse account map. The cached map can be stale within
a request (a parent created moments earlier is missing from it), which
made get_warehouse_account trigger a full nested-set rebuild_tree and
could falsely reject a child whose parent carries a valid account.
rebuild_tree enables auto_commit_on_many_writes, which must not run
inside a document insert.
This commit is contained in:
Mihir Kandoi
2026-08-11 18:26:08 +05:30
parent 0dfa54f812
commit fce0eb1577

View File

@@ -60,14 +60,6 @@ class Warehouse(NestedSet):
self.name = self.warehouse_name
def before_insert(self):
if (
self.company
and not self.flags.ignore_inventory_account_validation
and frappe.get_cached_value("Company", self.company, "enable_perpetual_inventory")
):
get_warehouse_account(self, get_warehouse_account_map(self.company))
def onload(self):
if self.company and cint(frappe.db.get_value("Company", self.company, "enable_perpetual_inventory")):
account = self.account or get_warehouse_account(self, raise_error=False)
@@ -78,8 +70,28 @@ class Warehouse(NestedSet):
self.set_onload("stock_exists", self.check_if_sle_exists(non_cancelled_only=True))
def validate(self):
self.validate_inventory_account()
self.warn_about_multiple_warehouse_account()
def validate_inventory_account(self):
if (
not self.is_new()
or not self.company
or self.flags.ignore_inventory_account_validation
or not frappe.get_cached_value("Company", self.company, "enable_perpetual_inventory")
):
return
warehouse = frappe._dict(self.as_dict())
if not self.account and self.parent_warehouse:
parent_bounds = frappe.db.get_value(
"Warehouse", self.parent_warehouse, ["lft", "rgt"], as_dict=True
)
if parent_bounds:
warehouse.update(parent_bounds)
get_warehouse_account(warehouse)
def on_update(self):
self.update_nsm_model()