Per-model counter fields on sale.order renamed to x_fc_pn_*_count to avoid collision with pre-existing compute fields of the same short name in bridge_mrp / receiving / configurator (silent compute-override was suppressing the storage). 4 child models (fp.certificate, fp.receiving, fusion.plating.delivery, fusion.plating.pickup.request) now derive names as PFX-<parent> with -NN suffix from the 2nd onward. fusion.plating.pickup.request gains a sale_order_id field (optional) so pickups created against an SO get parent-derived names, while standalone pickups (pre-SO) fall back to PU/YYYY/NNNN. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
234 lines
7.4 KiB
Python
234 lines
7.4 KiB
Python
# -*- 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 FpPickupRequest(models.Model):
|
|
"""Customer-initiated request for pickup of parts to be processed.
|
|
|
|
Lifecycle:
|
|
|
|
new → scheduled → en_route → picked_up → received → (cancelled)
|
|
|
|
A pickup request is created when a customer phones or emails asking
|
|
for parts to be collected. Dispatch schedules a driver + vehicle,
|
|
the driver updates status on the road, and the receiving facility
|
|
confirms arrival of the parts. Chain of custody events are written
|
|
automatically as the state transitions.
|
|
"""
|
|
_name = 'fusion.plating.pickup.request'
|
|
_description = 'Fusion Plating — Pickup Request'
|
|
_inherit = ['mail.thread', 'mail.activity.mixin', 'fp.parent.numbered.mixin']
|
|
_order = 'requested_date desc, id desc'
|
|
|
|
name = fields.Char(
|
|
string='Reference',
|
|
required=True,
|
|
copy=False,
|
|
default='New',
|
|
tracking=True,
|
|
)
|
|
sale_order_id = fields.Many2one(
|
|
'sale.order',
|
|
string='Sale Order',
|
|
ondelete='set null',
|
|
index=True,
|
|
help='Sale order this pickup is associated with. Pickup may be '
|
|
'created BEFORE the SO exists; in that case the '
|
|
'parent-number naming falls back to the standalone '
|
|
'PU/YYYY/NNNN sequence and the link can be set later.',
|
|
)
|
|
partner_id = fields.Many2one(
|
|
'res.partner',
|
|
string='Customer',
|
|
required=True,
|
|
tracking=True,
|
|
)
|
|
pickup_address_id = fields.Many2one(
|
|
'res.partner',
|
|
string='Pickup Address',
|
|
help='Leave blank to use the customer default address.',
|
|
)
|
|
contact_name = fields.Char(
|
|
string='Contact Name',
|
|
)
|
|
contact_phone = fields.Char(
|
|
string='Contact Phone',
|
|
)
|
|
requested_date = fields.Datetime(
|
|
string='Requested Date',
|
|
default=fields.Datetime.now,
|
|
tracking=True,
|
|
)
|
|
scheduled_date = fields.Datetime(
|
|
string='Scheduled Date',
|
|
tracking=True,
|
|
)
|
|
assigned_driver_id = fields.Many2one(
|
|
'hr.employee',
|
|
string='Assigned Driver',
|
|
tracking=True,
|
|
domain=[('x_fc_is_driver', '=', True)],
|
|
)
|
|
vehicle_id = fields.Many2one(
|
|
'fusion.plating.vehicle',
|
|
string='Vehicle',
|
|
tracking=True,
|
|
)
|
|
destination_facility_id = fields.Many2one(
|
|
'fusion.plating.facility',
|
|
string='Destination Facility',
|
|
tracking=True,
|
|
)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
related='destination_facility_id.company_id',
|
|
store=True,
|
|
readonly=True,
|
|
)
|
|
item_description = fields.Html(
|
|
string='Items to Pick Up',
|
|
)
|
|
estimated_weight_kg = fields.Float(
|
|
string='Est. Weight (kg)',
|
|
)
|
|
tdg_required = fields.Boolean(
|
|
string='TDG Required',
|
|
tracking=True,
|
|
help='Check if the load qualifies as Transportation of Dangerous '
|
|
'Goods. Only TDG-certified drivers and vehicles may be '
|
|
'assigned.',
|
|
)
|
|
state = fields.Selection(
|
|
[
|
|
('new', 'New'),
|
|
('scheduled', 'Scheduled'),
|
|
('en_route', 'En Route'),
|
|
('picked_up', 'Picked Up'),
|
|
('received', 'Received at Facility'),
|
|
('cancelled', 'Cancelled'),
|
|
],
|
|
string='Status',
|
|
default='new',
|
|
required=True,
|
|
tracking=True,
|
|
)
|
|
picked_up_at = fields.Datetime(
|
|
string='Picked Up At',
|
|
readonly=True,
|
|
)
|
|
received_at = fields.Datetime(
|
|
string='Received At',
|
|
readonly=True,
|
|
)
|
|
notes = fields.Html(
|
|
string='Notes',
|
|
)
|
|
custody_event_ids = fields.One2many(
|
|
'fusion.plating.chain.of.custody',
|
|
'pickup_request_id',
|
|
string='Custody Events',
|
|
)
|
|
custody_event_count = fields.Integer(
|
|
compute='_compute_custody_count',
|
|
)
|
|
|
|
# ------------------------------------------------------------------
|
|
# Parent-numbered mixin hooks
|
|
# ------------------------------------------------------------------
|
|
def _fp_parent_sale_order(self):
|
|
return self.sale_order_id
|
|
|
|
def _fp_name_prefix(self):
|
|
return 'PU'
|
|
|
|
def _fp_parent_counter_field(self):
|
|
return 'x_fc_pn_pickup_count'
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if not vals.get('name'):
|
|
vals['name'] = 'New'
|
|
records = super().create(vals_list)
|
|
for rec in records:
|
|
if rec.name and rec.name != 'New':
|
|
continue
|
|
if not rec._fp_assign_parent_name():
|
|
seq = self.env['ir.sequence'].next_by_code('fusion.plating.pickup.request') or 'New'
|
|
self.env.cr.execute(
|
|
"UPDATE fusion_plating_pickup_request SET name = %s WHERE id = %s",
|
|
(seq, rec.id),
|
|
)
|
|
rec.invalidate_recordset(['name'])
|
|
return records
|
|
|
|
@api.model
|
|
def _default_name(self):
|
|
"""Retained for legacy callers; new flow uses the create() override."""
|
|
seq = self.env['ir.sequence'].next_by_code('fusion.plating.pickup.request')
|
|
return seq or '/'
|
|
|
|
def _compute_custody_count(self):
|
|
for rec in self:
|
|
rec.custody_event_count = len(rec.custody_event_ids)
|
|
|
|
def _log_custody_event(self, event_type, from_party=None, to_party=None):
|
|
self.ensure_one()
|
|
self.env['fusion.plating.chain.of.custody'].create({
|
|
'event_datetime': fields.Datetime.now(),
|
|
'event_type': event_type,
|
|
'from_party': from_party or '',
|
|
'to_party': to_party or '',
|
|
'pickup_request_id': self.id,
|
|
'facility_id': self.destination_facility_id.id,
|
|
'recorded_by_id': self.env.user.id,
|
|
})
|
|
|
|
# ==========================================================================
|
|
# Actions
|
|
# ==========================================================================
|
|
def action_schedule(self):
|
|
self.write({'state': 'scheduled'})
|
|
|
|
def action_start_route(self):
|
|
self.write({'state': 'en_route'})
|
|
|
|
def action_mark_picked_up(self):
|
|
for rec in self:
|
|
rec.write({
|
|
'state': 'picked_up',
|
|
'picked_up_at': fields.Datetime.now(),
|
|
})
|
|
rec._log_custody_event(
|
|
'received_from_customer',
|
|
from_party=rec.partner_id.display_name,
|
|
to_party=(rec.assigned_driver_id.display_name
|
|
or rec.vehicle_id.display_name
|
|
or 'Driver'),
|
|
)
|
|
|
|
def action_mark_received(self):
|
|
for rec in self:
|
|
rec.write({
|
|
'state': 'received',
|
|
'received_at': fields.Datetime.now(),
|
|
})
|
|
rec._log_custody_event(
|
|
'entered_facility',
|
|
from_party=(rec.assigned_driver_id.display_name
|
|
or rec.vehicle_id.display_name
|
|
or 'Driver'),
|
|
to_party=(rec.destination_facility_id.display_name
|
|
or 'Facility'),
|
|
)
|
|
|
|
def action_cancel(self):
|
|
self.write({'state': 'cancelled'})
|
|
|
|
def action_reset_to_new(self):
|
|
self.write({'state': 'new'})
|