diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py index e36b9c05546..4e138721f77 100644 --- a/erpnext/buying/doctype/supplier/supplier.py +++ b/erpnext/buying/doctype/supplier/supplier.py @@ -20,7 +20,6 @@ from erpnext.controllers.website_list_for_contact import ( add_role_for_portal_user, link_portal_users_to_contacts, ) -from erpnext.stock.doctype.company_restriction.company_restriction import validate_allowed_companies from erpnext.utilities.transaction_base import TransactionBase @@ -154,7 +153,6 @@ class Supplier(TransactionBase): self.validate_internal_supplier() self.add_role_for_user() self.validate_currency_for_receivable_payable_and_advance_account() - validate_allowed_companies(self) @frappe.whitelist() def get_supplier_group_details(self): diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 35f69dae11f..54ab7f03ea0 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -400,6 +400,9 @@ doc_events = { tuple(company_restricted_transaction_doctypes): { "validate": "erpnext.stock.doctype.company_restriction.company_restriction.validate_transaction_company", }, + ("Item", "Customer", "Supplier"): { + "validate": "erpnext.stock.doctype.company_restriction.company_restriction.validate_allowed_companies", + }, "Stock Entry": { "on_submit": "erpnext.stock.doctype.material_request.material_request.update_completed_and_requested_qty", "on_cancel": "erpnext.stock.doctype.material_request.material_request.update_completed_and_requested_qty", diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 6ff2b49a33e..2d7a562715f 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -28,7 +28,6 @@ from erpnext.controllers.website_list_for_contact import ( add_role_for_portal_user, link_portal_users_to_contacts, ) -from erpnext.stock.doctype.company_restriction.company_restriction import validate_allowed_companies from erpnext.utilities.transaction_base import TransactionBase from .mapper import ( @@ -193,7 +192,6 @@ class Customer(TransactionBase): self.validate_internal_customer() self.add_role_for_user() self.validate_currency_for_receivable_payable_and_advance_account() - validate_allowed_companies(self) # set loyalty program tier if not self.is_new() and (customer := self.get_doc_before_save()): diff --git a/erpnext/stock/doctype/company_restriction/company_restriction.py b/erpnext/stock/doctype/company_restriction/company_restriction.py index eb9b3b602ce..e02dafac024 100644 --- a/erpnext/stock/doctype/company_restriction/company_restriction.py +++ b/erpnext/stock/doctype/company_restriction/company_restriction.py @@ -79,7 +79,7 @@ def has_permission(doc, ptype=None, user=None): return any(row.company in allowed_companies for row in doc.get("allowed_companies") or []) -def validate_allowed_companies(doc): +def validate_allowed_companies(doc, method=None): if not doc.get("restrict_to_companies"): doc.set("allowed_companies", []) elif not doc.get("allowed_companies") and not doc.flags.ignore_mandatory: diff --git a/erpnext/stock/doctype/company_restriction/test_company_restriction.py b/erpnext/stock/doctype/company_restriction/test_company_restriction.py index e29bb329a1b..d33f5791830 100644 --- a/erpnext/stock/doctype/company_restriction/test_company_restriction.py +++ b/erpnext/stock/doctype/company_restriction/test_company_restriction.py @@ -54,3 +54,8 @@ class TestCompanyRestriction(ERPNextTestSuite): def test_unrestricted_item_is_not_blocked(self): item = make_item() make_material_request(item_code=item.name) + + def test_allowed_companies_is_mandatory_when_restricted(self): + item = make_item() + item.restrict_to_companies = 1 + self.assertRaises(frappe.MandatoryError, item.save) diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index e00fe9c8fd8..8da6652d1bb 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -30,7 +30,6 @@ from erpnext.controllers.item_variant import ( make_variant_item_code, validate_item_variant_attributes, ) -from erpnext.stock.doctype.company_restriction.company_restriction import validate_allowed_companies from erpnext.stock.doctype.item_default.item_default import ItemDefault from erpnext.stock.serial_batch_bundle import SerialBatchCreation from erpnext.stock.utils import get_valuation_method @@ -246,7 +245,6 @@ class Item(Document): self.validate_serialized_change_with_bundle() self.validate_standard_cost_change() self.validate_item_tax_net_rate_range() - validate_allowed_companies(self) if not self.is_new(): self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group")