2026-09-25 18:00:58 +05:30

71 lines
2.5 KiB
Python

from odoo import api, fields, models
from odoo.exceptions import ValidationError
class AcademicYear(models.Model):
_name = 'education.academic.year'
_description = 'Academic Year'
_order = 'start_date desc, name desc'
name = fields.Char(required=True, string='Academic Year')
code = fields.Char(string='Code', required=True, index=True)
start_date = fields.Date(string='Start Date', required=True)
end_date = fields.Date(string='End Date', required=True)
active = fields.Boolean(default=True)
company_id = fields.Many2one(
'res.company',
string='Company',
required=True,
default=lambda self: self.env.company,
index=True,
)
current_year = fields.Boolean(string='Current Year', default=False)
state = fields.Selection([
('draft', 'Draft'),
('active', 'Active'),
('closed', 'Closed'),
], default='draft', required=True, tracking=True)
term_ids = fields.One2many('education.academic.term', 'academic_year_id', string='Terms')
_sql_constraints = [
('uniq_year_code_company', 'unique(code, company_id)', 'Academic year code must be unique per company.'),
]
@api.constrains('start_date', 'end_date')
def _check_year_dates(self):
for rec in self:
if rec.start_date and rec.end_date and rec.start_date > rec.end_date:
raise ValidationError('End date must be later than start date.')
@api.constrains('current_year')
def _check_single_current_year(self):
for rec in self:
if rec.current_year:
existing = self.search([
('company_id', '=', rec.company_id.id),
('current_year', '=', True),
('id', '!=', rec.id),
])
if existing:
raise ValidationError('Only one academic year can be marked as current for a company.')
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get('state') == 'active':
vals['current_year'] = True
return super().create(vals_list)
def action_activate(self):
for rec in self:
if rec.start_date and rec.end_date and rec.start_date > rec.end_date:
raise ValidationError('Academic year dates are invalid.')
rec.state = 'active'
rec.current_year = True
def action_close(self):
self.write({'state': 'closed'})
def action_set_draft(self):
self.write({'state': 'draft'})