Files
repair/odoo/addons/dsrpt_repair_technicians/models/technician.py
2026-02-13 18:36:40 +07:00

92 lines
3.3 KiB
Python

from odoo import api, fields, models
from odoo.exceptions import ValidationError
class RepairTechnician(models.Model):
_name = "repair.technician"
_description = "Repair Technician"
_order = "name"
_inherit = ["mail.thread", "mail.activity.mixin"]
name = fields.Char(required=True, tracking=True)
user_id = fields.Many2one("res.users", string="User", tracking=True)
zone_ids = fields.Many2many("repair.fsm.zone", string="FSM Zones", tracking=True)
work_type_ids = fields.Many2many("repair.work.type", string="Work Types", tracking=True)
available_until = fields.Date(string="Available Until", tracking=True)
state = fields.Selection(
selection=[
("draft", "Draft"),
("active", "Active"),
("archived", "Archived"),
],
default="draft",
tracking=True,
group_expand="_group_expand_states",
)
active = fields.Boolean(default=True, tracking=True)
schedule_ids = fields.One2many("repair.technician.schedule", "technician_id", string="Weekly Schedule")
exception_ids = fields.One2many("repair.technician.exception", "technician_id", string="Exceptions")
def _group_expand_states(self, states, domain, order):
return [key for key, _label in self._fields["state"].selection]
def action_set_active(self):
self.write({"state": "active", "active": True})
def action_archive(self):
self.write({"state": "archived", "active": False})
def action_reset_draft(self):
self.write({"state": "draft", "active": True})
class RepairTechnicianSchedule(models.Model):
_name = "repair.technician.schedule"
_description = "Repair Technician Weekly Schedule"
_order = "technician_id, day_of_week, hour_from"
technician_id = fields.Many2one("repair.technician", required=True, ondelete="cascade")
day_of_week = fields.Selection(
selection=[
("0", "Monday"),
("1", "Tuesday"),
("2", "Wednesday"),
("3", "Thursday"),
("4", "Friday"),
("5", "Saturday"),
("6", "Sunday"),
],
required=True,
)
hour_from = fields.Float(required=True)
hour_to = fields.Float(required=True)
@api.constrains("hour_from", "hour_to")
def _check_hours(self):
for rec in self:
if rec.hour_from < 0 or rec.hour_to > 24 or rec.hour_to <= rec.hour_from:
raise ValidationError("Schedule hours must be between 0 and 24, and hour_to must be greater than hour_from.")
class RepairTechnicianException(models.Model):
_name = "repair.technician.exception"
_description = "Repair Technician Exception"
_order = "start_datetime desc"
technician_id = fields.Many2one("repair.technician", required=True, ondelete="cascade")
name = fields.Char(required=True)
exception_type = fields.Selection(
selection=[("positive", "Positive"), ("negative", "Negative")],
required=True,
default="negative",
)
start_datetime = fields.Datetime(required=True)
end_datetime = fields.Datetime(required=True)
note = fields.Text()
@api.constrains("start_datetime", "end_datetime")
def _check_dates(self):
for rec in self:
if rec.end_datetime <= rec.start_datetime:
raise ValidationError("Exception end must be after start.")