# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # Part of the Fusion Plating product family. from odoo import api, fields, models class FpValueSet(models.Model): """A named collection of values owned by a company. Each shop loads its own set. One shop might call theirs "Entech Fundamentals" with 24 items, another "Our Way" with 10. A shop may have several sets (historical, draft, archived) but usually only one is marked primary. The module ships NO seed data — this is a generic framework. """ _name = 'fusion.plating.value.set' _description = 'Fusion Plating — Value Set' _inherit = ['mail.thread', 'mail.activity.mixin'] _order = 'is_primary desc, name' name = fields.Char( string='Name', required=True, tracking=True, help='Display name of the set, e.g. "Entech Fundamentals", "Our Way".', ) code = fields.Char( string='Code', required=True, tracking=True, help='Unique slug identifier, e.g. "entech_fundamentals".', ) description = fields.Html( string='Description', help='Long-form description of what this set represents and how ' 'the shop uses it.', ) company_id = fields.Many2one( 'res.company', string='Company', required=True, default=lambda self: self.env.company, tracking=True, ) value_ids = fields.One2many( 'fusion.plating.value', 'set_id', string='Values', ) value_count = fields.Integer( string='Value Count', compute='_compute_value_count', ) is_primary = fields.Boolean( string='Primary Set', default=False, tracking=True, help='Marks this as the shop\'s primary culture set. Only one set ' 'per company should be primary at a time.', ) active = fields.Boolean( string='Active', default=True, ) _sql_constraints = [ ( 'fp_value_set_code_company_uniq', 'unique(code, company_id)', 'Value set code must be unique within a company.', ), ] @api.depends('value_ids') def _compute_value_count(self): for rec in self: rec.value_count = len(rec.value_ids) def write(self, vals): """When flipping a set to primary, demote any other primary set in the same company so there is only one primary per company. """ res = super().write(vals) if vals.get('is_primary'): for rec in self: if rec.is_primary: others = self.search([ ('id', '!=', rec.id), ('company_id', '=', rec.company_id.id), ('is_primary', '=', True), ]) if others: others.write({'is_primary': False}) return res @api.model_create_multi def create(self, vals_list): recs = super().create(vals_list) for rec in recs: if rec.is_primary: others = self.search([ ('id', '!=', rec.id), ('company_id', '=', rec.company_id.id), ('is_primary', '=', True), ]) if others: others.write({'is_primary': False}) return recs