137 lines
4.3 KiB
Python
137 lines
4.3 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationBatch(models.Model):
|
|
_name = 'education.batch'
|
|
_description = 'Academic Batch'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(required=True, string='Batch Name')
|
|
code = fields.Char(string='Batch Code', required=True, index=True)
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
|
|
_sql_constraints = [
|
|
('uniq_batch_code_company', 'unique(code, company_id)', 'Batch code must be unique per company.'),
|
|
]
|
|
|
|
@api.constrains('name', 'company_id')
|
|
def _check_name_unique(self):
|
|
for rec in self:
|
|
similar = self.search([
|
|
('company_id', '=', rec.company_id.id),
|
|
('name', '=ilike', rec.name),
|
|
('id', '!=', rec.id),
|
|
])
|
|
if similar:
|
|
raise ValidationError('Batch name must be unique within a company.')
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().create(vals_list)
|
|
|
|
def write(self, vals):
|
|
if 'code' in vals and vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().write(vals)
|
|
|
|
|
|
class EducationSemester(models.Model):
|
|
_name = 'education.semester'
|
|
_description = 'Academic Semester'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(required=True, string='Semester Name')
|
|
code = fields.Char(string='Semester Code', required=True, index=True)
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
|
|
_sql_constraints = [
|
|
('uniq_semester_code_company', 'unique(code, company_id)', 'Semester code must be unique per company.'),
|
|
]
|
|
|
|
@api.constrains('name', 'company_id')
|
|
def _check_name_unique(self):
|
|
for rec in self:
|
|
similar = self.search([
|
|
('company_id', '=', rec.company_id.id),
|
|
('name', '=ilike', rec.name),
|
|
('id', '!=', rec.id),
|
|
])
|
|
if similar:
|
|
raise ValidationError('Semester name must be unique within a company.')
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().create(vals_list)
|
|
|
|
def write(self, vals):
|
|
if 'code' in vals and vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().write(vals)
|
|
|
|
|
|
class EducationSection(models.Model):
|
|
_name = 'education.section'
|
|
_description = 'Academic Section'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(required=True, string='Section Name')
|
|
code = fields.Char(string='Section Code', required=True, index=True)
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
|
|
_sql_constraints = [
|
|
('uniq_section_code_company', 'unique(code, company_id)', 'Section code must be unique per company.'),
|
|
]
|
|
|
|
@api.constrains('name', 'company_id')
|
|
def _check_name_unique(self):
|
|
for rec in self:
|
|
similar = self.search([
|
|
('company_id', '=', rec.company_id.id),
|
|
('name', '=ilike', rec.name),
|
|
('id', '!=', rec.id),
|
|
])
|
|
if similar:
|
|
raise ValidationError('Section name must be unique within a company.')
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().create(vals_list)
|
|
|
|
def write(self, vals):
|
|
if 'code' in vals and vals.get('code'):
|
|
vals['code'] = vals['code'].strip().upper()
|
|
return super().write(vals)
|