Files
Odoo-Modules/fusion_plating/fusion_plating_receiving/models/fp_receiving.py
gsinghpal 167c423bf5 feat(plating): close 5 end-to-end automation gaps
E2E test (quote → SO → MO → WOs → ship → invoice → payment) ran clean
but flagged five gaps where the operator was filling in data the
system already knew. Closes all five.

#1  SO CONFIRM → AUTO-CREATE DRAFT MO  (was a workflow blocker)
    bridge_mrp/sale_order.py: action_confirm() override + new
    _fp_auto_create_mo helper. Resolves the manufactured product from
    the configurator's part-catalog → coating-config → FP-WIDGET
    fallback; resolves the recipe from coating_config.recipe_id →
    part_catalog.recipe_id → first installed recipe. Idempotent:
    skips if any MO already exists for the SO. Errors are caught and
    chatter-posted so SO confirm never fails because of an MO glitch.

#2  QUOTE PO → client_order_ref ON SO  (one-line fix)
    configurator/fp_quote_configurator.py: action_create_quotation
    now copies po_number_preliminary into Odoo's standard
    client_order_ref alongside the existing custom x_fc_po_number.
    Portal pages, native reports, and integrations all read the
    standard field; no reason both shouldn't carry the same PO#.

#3  MO DONE → AUTO-RENDER CoC + THICKNESS PDFs
    bridge_mrp/mrp_production.py button_mark_done now calls a new
    _fp_generate_cert_pdf helper after creating each fp.certificate.
    Renders fusion_plating_reports.action_report_coc to PDF, stores
    as ir.attachment, links to cert.attachment_id, AND cross-links
    to portal_job.coc_attachment_id + delivery.coc_attachment_id so
    the customer portal and the shipping email both find it without
    an extra step. Thickness report falls back to the CoC layout
    (which embeds thickness data) until a dedicated report ships.
    Errors are logged but never block MO completion.

#4  RECEIVING received_qty PREFILL
    receiving/fp_receiving.py: create() prefills received_qty from
    expected_qty on draft. Operator only types when the count is
    wrong (the rare case). Field carrier_tracking already exists,
    so #4's 'no inbound tracking field' from the gap report turned
    out to be a false alarm.

#5  DELIVERY scheduled_date + driver PREFILL
    bridge_mrp/mrp_production.py: new _fp_build_delivery_vals
    helper sets scheduled_date from the portal job's target_ship_date
    (or now+2 business days as a sane fallback) and auto-picks
    assigned_driver_id from clocked-in employees tagged is_driver
    (falls back to any active driver if the shift is empty). The
    outbound tracking_ref deliberately stays empty — that's the
    carrier's number, paste it in once UPS/FedEx accepts the package.

Module bumps: configurator 19.0.5.0.0, bridge_mrp 19.0.5.0.0,
receiving 19.0.2.0.0.

Verified on entech: re-ran the E2E test against a fresh quote.
Quote → SO populated client_order_ref, SO confirm auto-created MO,
receiving prefilled received_qty=50, MO done generated CERT-00018.pdf
and linked it to portal job + delivery, delivery's scheduled_date
prefilled to 2026-04-29, full pipeline ended with portal job state
'complete'. The remaining 'gaps' in the static report are script
artefacts (e.g. it flags 'no inbound tracking field' but the field
exists; flags 'no driver auto-pick' but the demo data has zero
drivers tagged is_driver=True).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 00:46:30 -04:00

156 lines
6.9 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, _
from odoo.exceptions import UserError
class FpReceiving(models.Model):
"""Parts receiving record.
Created automatically when a sale order is confirmed. Tracks
quantity verification, condition inspection, and damage logging
for customer parts arriving at the shop.
"""
_name = 'fp.receiving'
_description = 'Fusion Plating — Receiving'
_inherit = ['mail.thread', 'mail.activity.mixin']
_order = 'received_date desc, id desc'
name = fields.Char(string='Reference', readonly=True, copy=False, default='New')
sale_order_id = fields.Many2one(
'sale.order', string='Sale Order', required=True, ondelete='restrict',
tracking=True,
)
partner_id = fields.Many2one(
'res.partner', string='Customer', related='sale_order_id.partner_id',
store=True, readonly=True,
)
po_number = fields.Char(
string='Customer PO #', related='sale_order_id.x_fc_po_number',
store=True, readonly=True,
)
received_by_id = fields.Many2one(
'res.users', string='Received By', default=lambda self: self.env.user,
tracking=True,
)
received_date = fields.Datetime(
string='Received Date', default=fields.Datetime.now, tracking=True,
)
state = fields.Selection(
[
('draft', 'Awaiting Parts'),
('inspecting', 'Inspecting'),
('accepted', 'Accepted'),
('discrepancy', 'Discrepancy'),
('resolved', 'Resolved'),
],
string='Status', default='draft', tracking=True, required=True,
)
expected_qty = fields.Integer(string='Expected Qty', help='Total quantity expected from the sale order.')
received_qty = fields.Integer(string='Received Qty', help='Total quantity actually received.')
qty_match = fields.Boolean(
string='Qty Match', compute='_compute_qty_match', store=True,
)
carrier_name = fields.Char(string='Carrier', help='Who delivered the parts (Purolator, customer drop-off, etc.).')
carrier_tracking = fields.Char(string='Inbound Tracking #')
notes = fields.Html(string='Notes')
line_ids = fields.One2many('fp.receiving.line', 'receiving_id', string='Receiving Lines')
damage_ids = fields.One2many('fp.receiving.damage', 'receiving_id', string='Damage Log')
damage_count = fields.Integer(string='Damage Count', compute='_compute_damage_count')
unresolved_damage_count = fields.Integer(
string='Unresolved Damage', compute='_compute_damage_count',
)
attachment_ids = fields.Many2many(
'ir.attachment', 'fp_receiving_attachment_rel', 'receiving_id', 'attachment_id',
string='Photos / Documents',
)
@api.depends('expected_qty', 'received_qty')
def _compute_qty_match(self):
for rec in self:
rec.qty_match = rec.expected_qty > 0 and rec.received_qty == rec.expected_qty
@api.depends('damage_ids', 'damage_ids.resolved')
def _compute_damage_count(self):
for rec in self:
rec.damage_count = len(rec.damage_ids)
rec.unresolved_damage_count = len(rec.damage_ids.filtered(lambda d: not d.resolved))
# -------------------------------------------------------------------------
# Sequence
# -------------------------------------------------------------------------
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get('name', 'New') == 'New':
vals['name'] = self.env['ir.sequence'].next_by_code('fp.receiving') or 'New'
# Prefill received_qty from expected_qty so the operator only
# types when the count is wrong (the common case is "all
# arrived"). Saves a step on every routine receipt.
if vals.get('expected_qty') and not vals.get('received_qty'):
vals['received_qty'] = vals['expected_qty']
return super().create(vals_list)
# -------------------------------------------------------------------------
# State actions
# -------------------------------------------------------------------------
def action_start_inspection(self):
"""Move from draft to inspecting."""
for rec in self:
if rec.state != 'draft':
raise UserError(_('Only draft records can start inspection.'))
rec.state = 'inspecting'
rec.received_by_id = self.env.user
rec.received_date = fields.Datetime.now()
def action_accept(self):
"""Accept the receiving — parts match and condition is OK."""
for rec in self:
if rec.state not in ('inspecting', 'resolved'):
raise UserError(_('Can only accept from Inspecting or Resolved state.'))
if rec.unresolved_damage_count > 0:
raise UserError(_('Cannot accept — there are %d unresolved damage entries.') % rec.unresolved_damage_count)
rec.state = 'accepted'
rec._update_so_receiving_status()
rec.message_post(body=_('Parts accepted — quantity: %d, all checks passed.') % rec.received_qty)
def action_flag_discrepancy(self):
"""Flag a discrepancy — qty mismatch or damage found."""
for rec in self:
if rec.state != 'inspecting':
raise UserError(_('Can only flag discrepancy from Inspecting state.'))
rec.state = 'discrepancy'
rec._update_so_receiving_status()
# Create follow-up activity for the sales team
rec.activity_schedule(
'mail.mail_activity_data_todo',
summary=_('Receiving discrepancy — %s') % rec.name,
note=_('Qty expected: %d, received: %d. Check damage log for details.') % (
rec.expected_qty, rec.received_qty),
)
rec.message_post(body=_('Discrepancy flagged — follow-up required.'))
def action_resolve(self):
"""Resolve a discrepancy after customer follow-up."""
for rec in self:
if rec.state != 'discrepancy':
raise UserError(_('Can only resolve from Discrepancy state.'))
rec.state = 'resolved'
rec._update_so_receiving_status()
rec.message_post(body=_('Discrepancy resolved.'))
def _update_so_receiving_status(self):
"""Update the linked sale order's receiving status."""
for rec in self:
if rec.sale_order_id:
if rec.state in ('accepted', 'resolved'):
rec.sale_order_id.x_fc_receiving_status = 'received'
elif rec.state == 'discrepancy':
rec.sale_order_id.x_fc_receiving_status = 'partial'
elif rec.state == 'inspecting':
rec.sale_order_id.x_fc_receiving_status = 'partial'