# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # Part of the Fusion Plating product family. from odoo import api, fields, models class FpRoute(models.Model): """A single run combining pickups and deliveries for one driver. Lifecycle: draft → planned → in_progress → completed → (cancelled) Dispatch drops pickup requests and deliveries on the route as stops, re-orders them using the `sequence` handle, then hands the route to the driver. The driver ticks off stops as they work, and the route captures total distance (km) and elapsed time for costing. """ _name = 'fusion.plating.route' _description = 'Fusion Plating — Route' _inherit = ['mail.thread', 'mail.activity.mixin'] _order = 'route_date desc, id desc' name = fields.Char( string='Reference', required=True, copy=False, default=lambda self: self._default_name(), tracking=True, ) route_date = fields.Date( string='Route Date', required=True, default=fields.Date.context_today, tracking=True, ) driver_id = fields.Many2one( 'hr.employee', string='Driver', required=True, tracking=True, domain=[('x_fc_is_driver', '=', True)], ) vehicle_id = fields.Many2one( 'fusion.plating.vehicle', string='Vehicle', required=True, tracking=True, ) company_id = fields.Many2one( 'res.company', related='vehicle_id.company_id', store=True, readonly=True, ) state = fields.Selection( [ ('draft', 'Draft'), ('planned', 'Planned'), ('in_progress', 'In Progress'), ('completed', 'Completed'), ('cancelled', 'Cancelled'), ], string='Status', default='draft', required=True, tracking=True, ) start_time = fields.Datetime( string='Start Time', ) end_time = fields.Datetime( string='End Time', ) total_km = fields.Float( string='Total Distance (km)', ) stop_ids = fields.One2many( 'fusion.plating.route.stop', 'route_id', string='Stops', ) stop_count = fields.Integer( compute='_compute_stop_count', ) notes = fields.Html( string='Notes', ) @api.model def _default_name(self): seq = self.env['ir.sequence'].next_by_code('fusion.plating.route') return seq or '/' def _compute_stop_count(self): for rec in self: rec.stop_count = len(rec.stop_ids) # ========================================================================== # Actions # ========================================================================== def action_plan(self): self.write({'state': 'planned'}) def action_start(self): self.write({ 'state': 'in_progress', 'start_time': fields.Datetime.now(), }) def action_complete(self): self.write({ 'state': 'completed', 'end_time': fields.Datetime.now(), }) def action_cancel(self): self.write({'state': 'cancelled'}) def action_reset_to_draft(self): self.write({'state': 'draft'})