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

(cherry picked from commit db6e089109)

# Conflicts:
#	erpnext/stock/doctype/warehouse/test_warehouse.py
This commit is contained in:
Afsal Syed
2026-09-19 13:06:06 +05:30
committed by Mergify
parent 0fbea6021b
commit 72306e7dc6
3 changed files with 68 additions and 0 deletions

View File

@@ -228,6 +228,57 @@ class TestWarehouse(ERPNextTestSuite):
self.assertNotIn("account", warehouse.get_onload())
<<<<<<< HEAD
=======
def test_stock_accounts_are_fetched_once_per_company(self):
from unittest.mock import patch
from erpnext.stock import get_company_stock_accounts, get_warehouse_account_map
company, warehouse = create_ambiguous_inventory_account_warehouse()
other_warehouse = frappe.get_all(
"Warehouse",
filters={"company": company, "is_group": 0, "name": ["!=", warehouse.name]},
pluck="name",
limit=1,
)[0]
frappe.db.set_value("Warehouse", other_warehouse, "account", None)
with patch(
"erpnext.stock.get_company_stock_accounts", wraps=get_company_stock_accounts
) as fetch_stock_accounts:
get_warehouse_account_map(company)
fetch_stock_accounts.assert_called_once_with(company)
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()
>>>>>>> db6e089 (fix(stock): validate warehouse account belongs to selected company (#59191))
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()