mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-24 05:47:15 +00:00
fix(stock): validate warehouse account belongs to selected company (#59191)
(cherry picked from commit db6e089109)
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user