# -*- coding: utf-8 -*- # Copyright 2024-2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) from markupsafe import Markup from odoo import fields, models class FusionTechnicianTaskRepairs(models.Model): """Adds the back-link from fusion.technician.task to repair.order so repairs and tasks share one timeline. Also hooks task completion to roll a linked maintenance contract to its next cycle. """ _inherit = 'fusion.technician.task' x_fc_repair_order_id = fields.Many2one( 'repair.order', string='Repair Order', ondelete='set null', index=True, tracking=True, help='Repair order this task fulfils. Set automatically when the intake ' 'wizard auto-creates a draft task for urgent / safety calls.', ) x_fc_repair_intake_session_id = fields.Char( related='x_fc_repair_order_id.x_fc_intake_session_id', string='Intake Session', store=True, index=True, ) def write(self, vals): """When a maintenance task transitions to 'completed', roll the linked contract to its next cycle. Failure to roll never blocks the underlying task write. """ res = super().write(vals) if vals.get('status') == 'completed': for task in self: if task.task_type != 'maintenance': continue repair = task.x_fc_repair_order_id contract = repair.x_fc_maintenance_contract_id if repair else False if not contract: continue try: contract.last_service_date = fields.Date.context_today(task) contract.roll_next_due_date() contract.message_post(body=Markup( 'Rolled forward after maintenance task ' '%s completed. Next due %s.' ) % (task.name or '', str(contract.next_due_date or ''))) except Exception: # Never let a contract roll failure block the task write. pass return res def action_view_repair_order(self): self.ensure_one() if not self.x_fc_repair_order_id: return False return { 'type': 'ir.actions.act_window', 'name': self.x_fc_repair_order_id.name, 'res_model': 'repair.order', 'view_mode': 'form', 'res_id': self.x_fc_repair_order_id.id, }