diff --git a/yuthon_sps_approval/models/inherit_product_template.py b/yuthon_sps_approval/models/inherit_product_template.py index 891304b08..5d81314c2 100644 --- a/yuthon_sps_approval/models/inherit_product_template.py +++ b/yuthon_sps_approval/models/inherit_product_template.py @@ -9,6 +9,13 @@ class ProductTemplate(models.Model): @api.onchange('is_fixed_assets') def _onchange_fixed_assets(self): + """勾选/取消“是否固定资产”时,限制可选的产品类别。 + + - 勾选:`categ_id` 只允许名称包含“固定资产”的产品类别 + - 取消:恢复 `categ_id` 的默认可选范围 + 同时保留原有“已关联设备不允许取消固定资产”校验。 + """ + # 原有校验逻辑:已作为固定资产使用时不允许取消 if not self.is_fixed_assets and self._origin.is_fixed_assets: related_equipments = self.env['maintenance.equipment'].search([ ('name', '=', self.name) @@ -16,6 +23,16 @@ class ProductTemplate(models.Model): if related_equipments: raise UserError('该产品已关联设备,请先删除固定资产') + # 根据是否固定资产,动态限制产品类别 + if self.is_fixed_assets: + # 只显示名称包含“固定资产”的产品类别 + domain = {'categ_id': [('name', 'ilike', '固定资产')]} + else: + # 取消勾选时,恢复为不加限制 + domain = {'categ_id': []} + return {'domain': domain} + + def update_company_id(self): template_ids = self.env["product.template"].browse(self._context.get('active_ids', self._context.get('active_id'))) company_id = self.env['res.company'].search([('name', '=', '集团本部')])