chore: better cost center validation for assets (#36477)
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index 252a3dd..04ec7be 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -148,17 +148,33 @@
frappe.throw(_("Item {0} must be a non-stock item").format(self.item_code))
def validate_cost_center(self):
- if not self.cost_center:
- return
-
- cost_center_company = frappe.db.get_value("Cost Center", self.cost_center, "company")
- if cost_center_company != self.company:
- frappe.throw(
- _("Selected Cost Center {} doesn't belongs to {}").format(
- frappe.bold(self.cost_center), frappe.bold(self.company)
- ),
- title=_("Invalid Cost Center"),
+ if self.cost_center:
+ cost_center_company, cost_center_is_group = frappe.db.get_value(
+ "Cost Center", self.cost_center, ["company", "is_group"]
)
+ if cost_center_company != self.company:
+ frappe.throw(
+ _("Cost Center {} doesn't belong to Company {}").format(
+ frappe.bold(self.cost_center), frappe.bold(self.company)
+ ),
+ title=_("Invalid Cost Center"),
+ )
+ if cost_center_is_group:
+ frappe.throw(
+ _(
+ "Cost Center {} is a group cost center and group cost centers cannot be used in transactions"
+ ).format(frappe.bold(self.cost_center)),
+ title=_("Invalid Cost Center"),
+ )
+
+ else:
+ if not frappe.get_cached_value("Company", self.company, "depreciation_cost_center"):
+ frappe.throw(
+ _(
+ "Please set a Cost Center for the Asset or set an Asset Depreciation Cost Center for the Company {}"
+ ).format(frappe.bold(self.company)),
+ title=_("Missing Cost Center"),
+ )
def validate_in_use_date(self):
if not self.available_for_use_date: