60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
from odoo import api, fields, models
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationConfiguration(models.Model):
|
|
_name = 'education.configuration'
|
|
_description = 'Education Configuration'
|
|
_rec_name = 'school_name'
|
|
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
string='Company',
|
|
required=True,
|
|
default=lambda self: self.env.company,
|
|
index=True,
|
|
)
|
|
school_name = fields.Char(string='School Name', required=True)
|
|
school_code = fields.Char(string='School Code', required=True)
|
|
default_campus_id = fields.Many2one('education.campus', string='Default Campus')
|
|
default_academic_year_id = fields.Many2one('education.academic.year', string='Default Academic Year')
|
|
default_term_id = fields.Many2one('education.academic.term', string='Default Term')
|
|
default_currency_id = fields.Many2one('res.currency', string='Default Currency')
|
|
logo = fields.Binary(string='Logo')
|
|
contact_email = fields.Char(string='Contact Email')
|
|
contact_phone = fields.Char(string='Contact Phone')
|
|
active = fields.Boolean(default=True)
|
|
|
|
_sql_constraints = [
|
|
('uniq_company_config', 'unique(company_id)', 'Only one education configuration can exist per company.'),
|
|
]
|
|
|
|
@api.constrains('default_campus_id', 'company_id')
|
|
def _check_default_campus_in_company(self):
|
|
for rec in self:
|
|
if rec.default_campus_id and rec.default_campus_id.company_id != rec.company_id:
|
|
raise ValidationError('Default campus must belong to the same company.')
|
|
|
|
@api.constrains('default_academic_year_id', 'company_id')
|
|
def _check_default_academic_year_in_company(self):
|
|
for rec in self:
|
|
if rec.default_academic_year_id and rec.default_academic_year_id.company_id != rec.company_id:
|
|
raise ValidationError('Default academic year must belong to the same company.')
|
|
|
|
@api.constrains('default_term_id', 'company_id')
|
|
def _check_default_term_in_company(self):
|
|
for rec in self:
|
|
if rec.default_term_id and rec.default_term_id.company_id != rec.company_id:
|
|
raise ValidationError('Default term must belong to the same company.')
|
|
|
|
@api.model
|
|
def get_default_values(self):
|
|
config = self.search([('company_id', '=', self.env.company.id)], limit=1)
|
|
if config:
|
|
return config
|
|
return self.create({
|
|
'company_id': self.env.company.id,
|
|
'school_name': self.env.company.name,
|
|
'school_code': self.env.company.name[:5].upper(),
|
|
})
|