# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) import logging from datetime import timedelta from odoo import api, fields, models _logger = logging.getLogger(__name__) class MaintenanceRequest(models.Model): """Extend standard maintenance.request with plating-specific fields.""" _inherit = 'maintenance.request' x_fc_plan_id = fields.Many2one( 'fp.maintenance.plan', string='Plan', ) x_fc_node_id = fields.Many2one( 'fp.maintenance.node', string='Checklist Item', ) x_fc_labour_cost = fields.Monetary( string='Labour Cost', currency_field='x_fc_currency_id', ) x_fc_currency_id = fields.Many2one( 'res.currency', string='Currency', default=lambda self: self.env.company.currency_id, ) x_fc_completed_at = fields.Datetime( string='Completed At', readonly=True, ) x_fc_from_last = fields.Boolean( string='From Last Maintenance', help='When checked, the next recurrence is scheduled relative to ' 'completion date instead of a fixed calendar interval.', ) x_fc_recurrence_days = fields.Integer( string='Recurrence Days', help='Number of days after completion to schedule the next event ' '(only used with "From Last Maintenance").', ) def write(self, vals): res = super().write(vals) if 'stage_id' in vals: for request in self: if request.stage_id.done and not request.x_fc_completed_at: request.x_fc_completed_at = fields.Datetime.now() self._maybe_schedule_from_last(request) elif not request.stage_id.done: request.x_fc_completed_at = False return res def _maybe_schedule_from_last(self, request): """Schedule next maintenance from completion date.""" if not request.x_fc_from_last or not request.x_fc_recurrence_days: return next_date = fields.Datetime.now() + timedelta( days=request.x_fc_recurrence_days, ) request.copy({ 'schedule_date': next_date, 'x_fc_completed_at': False, 'stage_id': self.env['maintenance.stage'].search( [('done', '=', False)], order='sequence', limit=1, ).id, }) _logger.info( 'Scheduled next from-last maintenance for %s on %s', request.name, next_date, )