fix(stock): validate warehouse account belongs to selected company (#59191)

(cherry picked from commit db6e089109)
This commit is contained in:
Afsal Syed
2026-09-19 13:06:06 +05:30
committed by Afsal Syed
parent 0fbea6021b
commit 38bdb4f9c0
3 changed files with 44 additions and 0 deletions

View File

@@ -228,6 +228,33 @@ class TestWarehouse(ERPNextTestSuite):
self.assertNotIn("account", warehouse.get_onload())
def test_warehouse_account_company_validation(self):
company_1 = "_Test Company"
company_2 = "_Test Company 1"
account_company_2 = frappe.db.get_value(
"Account", {"company": company_2, "account_type": "Stock", "is_group": 0}, "name"
)
warehouse = frappe.get_doc(
{
"doctype": "Warehouse",
"warehouse_name": "Test Company Account Mismatch",
"company": company_1,
"account": account_company_2,
}
)
self.assertRaisesRegex(frappe.ValidationError, "does not belong to Company", warehouse.insert)
warehouse.account = None
warehouse.insert()
warehouse.account = account_company_2
self.assertRaisesRegex(frappe.ValidationError, "does not belong to Company", warehouse.save)
warehouse.delete()
def create_inventory_fallback_company():
company = "_Test Company Inventory Fallback"

View File

@@ -33,6 +33,12 @@ frappe.ui.form.on("Warehouse", {
});
},
company: function (frm) {
if (frm.doc.account) {
frm.set_value("account", "");
}
},
refresh: function (frm) {
frm.toggle_display("warehouse_name", frm.doc.__islocal);
frm.toggle_display(["address_html", "contact_html"], !frm.doc.__islocal);

View File

@@ -70,9 +70,20 @@ class Warehouse(NestedSet):
self.set_onload("stock_exists", self.check_if_sle_exists(non_cancelled_only=True))
def validate(self):
self.validate_warehouse_account()
self.validate_inventory_account()
self.warn_about_multiple_warehouse_account()
def validate_warehouse_account(self):
if self.account and self.company:
account_company = frappe.get_cached_value("Account", self.account, "company")
if account_company and account_company != self.company:
frappe.throw(
_("Account {0} does not belong to Company {1}").format(
frappe.bold(self.account), frappe.bold(self.company)
)
)
def validate_inventory_account(self):
if (
not self.is_new()