# -*- 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 SaleOrder(models.Model): _inherit = 'sale.order' x_fc_receiving_ids = fields.One2many( 'fp.receiving', 'sale_order_id', string='Receiving Records', ) x_fc_receiving_count = fields.Integer( string='Receiving Count', compute='_compute_receiving_count', ) @api.depends('x_fc_receiving_ids') def _compute_receiving_count(self): for rec in self: rec.x_fc_receiving_count = len(rec.x_fc_receiving_ids) def action_confirm(self): """Override to auto-create receiving record on SO confirmation.""" res = super().action_confirm() for order in self: # Only create if no receiving record exists yet if not order.x_fc_receiving_ids: total_qty = sum(order.order_line.mapped('product_uom_qty')) receiving_vals = { 'sale_order_id': order.id, 'expected_qty': int(total_qty), 'line_ids': [], } # Auto-create lines from SO lines for line in order.order_line: receiving_vals['line_ids'].append((0, 0, { 'part_number': order.x_fc_part_catalog_id.part_number if order.x_fc_part_catalog_id else '', 'description': line.name or '', 'expected_qty': int(line.product_uom_qty), })) self.env['fp.receiving'].create(receiving_vals) return res def action_view_receiving(self): """Smart button action to view receiving records.""" self.ensure_one() if self.x_fc_receiving_count == 1: return { 'type': 'ir.actions.act_window', 'res_model': 'fp.receiving', 'res_id': self.x_fc_receiving_ids[0].id, 'view_mode': 'form', 'target': 'current', } return { 'type': 'ir.actions.act_window', 'res_model': 'fp.receiving', 'view_mode': 'list,form', 'domain': [('sale_order_id', '=', self.id)], 'target': 'current', }