80 lines
3.4 KiB
Python
80 lines
3.4 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.exceptions import ValidationError
|
|
|
|
class EducationTransportAssignment(models.Model):
|
|
_name = 'education.transport.assignment'
|
|
_description = 'Transport Assignment'
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
|
|
name = fields.Char(string='Reference', required=True, copy=False, readonly=True, default=lambda self: self.env['ir.sequence'].next_by_code('education.transport.assignment') or '/')
|
|
|
|
student_id = fields.Many2one('education.student', string='Student', required=True, tracking=True)
|
|
route_id = fields.Many2one('education.transport.route', string='Route', required=True, tracking=True)
|
|
stop_id = fields.Many2one('education.transport.stop', string='Stop', required=True, tracking=True)
|
|
vehicle_id = fields.Many2one('education.transport.vehicle', string='Vehicle', tracking=True)
|
|
|
|
academic_year_id = fields.Many2one('education.academic.year', string='Academic Year', tracking=True)
|
|
academic_term_id = fields.Many2one('education.academic.term', string='Academic Term')
|
|
|
|
start_date = fields.Date(string='Start Date', required=True, default=fields.Date.context_today)
|
|
end_date = fields.Date(string='End Date')
|
|
|
|
status = fields.Selection([
|
|
('pending', 'Pending'),
|
|
('active', 'Active'),
|
|
('suspended', 'Suspended'),
|
|
('completed', 'Completed'),
|
|
('cancelled', 'Cancelled')
|
|
], string='Status', default='pending', required=True, tracking=True)
|
|
|
|
company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.company)
|
|
|
|
@api.constrains('route_id', 'stop_id')
|
|
def _check_route_stop(self):
|
|
for rec in self:
|
|
if rec.route_id and rec.stop_id:
|
|
route_stop = self.env['education.transport.route.stop'].search([
|
|
('route_id', '=', rec.route_id.id),
|
|
('stop_id', '=', rec.stop_id.id)
|
|
])
|
|
if not route_stop:
|
|
raise ValidationError('The selected stop does not belong to the selected route!')
|
|
|
|
@api.constrains('student_id', 'status')
|
|
def _check_active_assignment(self):
|
|
for rec in self:
|
|
if rec.status == 'active':
|
|
existing = self.env['education.transport.assignment'].search([
|
|
('student_id', '=', rec.student_id.id),
|
|
('status', '=', 'active'),
|
|
('id', '!=', rec.id)
|
|
])
|
|
if existing:
|
|
raise ValidationError('Student already has an active transport assignment!')
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if not vals.get('name') or vals.get('name') == '/':
|
|
vals['name'] = self.env['ir.sequence'].next_by_code('education.transport.assignment') or '/'
|
|
return super().create(vals_list)
|
|
|
|
def action_activate(self):
|
|
for rec in self:
|
|
rec.status = 'active'
|
|
|
|
def action_suspend(self):
|
|
for rec in self:
|
|
rec.status = 'suspended'
|
|
|
|
def action_complete(self):
|
|
for rec in self:
|
|
rec.status = 'completed'
|
|
rec.end_date = fields.Date.today()
|
|
|
|
def action_cancel(self):
|
|
for rec in self:
|
|
rec.status = 'cancelled'
|