from odoo import api, fields, models from odoo.exceptions import ValidationError class EducationProgram(models.Model): _name = 'education.program' _description = 'Program' _order = 'name' name = fields.Char(required=True, string='Program Name') code = fields.Char(required=True, string='Program Code', index=True) level_id = fields.Many2one( 'education.academic.level', string='Academic Level', required=True, ondelete='restrict', index=True, ) department_id = fields.Many2one( 'education.department', string='Department', required=True, ondelete='restrict', index=True, ) company_id = fields.Many2one( 'res.company', related='department_id.company_id', store=True, readonly=True, index=True, ) duration = fields.Integer(string='Duration', default=1) duration_unit = fields.Selection([ ('years', 'Years'), ('months', 'Months'), ('semesters', 'Semesters'), ], default='years', required=True) active = fields.Boolean(default=True) description = fields.Text(string='Description') _sql_constraints = [ ('uniq_program_code_company', 'unique(code, company_id)', 'Program code must be unique per company.'), ] @api.constrains('duration') def _check_duration(self): for rec in self: if rec.duration <= 0: raise ValidationError('Program duration must be greater than zero.') @api.constrains('name', 'company_id') def _check_name_unique(self): for rec in self: if self.search_count([ ('company_id', '=', rec.company_id.id), ('name', '=ilike', rec.name), ('id', '!=', rec.id), ]): raise ValidationError('Program name must be unique within a company.') @api.onchange('department_id') def _onchange_department_id(self): if self.department_id: self.company_id = self.department_id.company_id.id if self.level_id and self.level_id.company_id != self.company_id: self.level_id = False