from odoo import models, api, fields from odoo.exceptions import AccessError, UserError, ValidationError from datetime import timedelta, date import logging _logger = logging.getLogger(__name__) class ShipmentGroup(models.Model): _name = "shipment.group" _description = "Shipment Group" _inherit = ['mail.thread'] _rec_name = 'number' number = fields.Char(string='Document No', index=True, copy=False, readonly=True, tracking=True) shipment_line = fields.One2many('shipment.group.line', 'shipment_id', string='Shipment Group Lines', auto_join=True) scan_invoice = fields.Char(string="Scan Invoice Marketplace") @api.onchange('scan_invoice') def _onchange_scan_invoice(self): if self.scan_invoice: result = self.add_lines_from_scan(self.scan_invoice) self.scan_invoice = False # Reset field setelah scan return result def add_lines_from_scan(self, scan_value): self.ensure_one() picking = self.env['stock.picking'].search([ ('invoice_mp', '=', scan_value) ], limit=1) if not picking: return { 'warning': { 'title': 'Not Found', 'message': f'No picking found with invoice: {scan_value}' } } # Cek duplikat existing_lines = self.shipment_line.filtered(lambda l: l.picking_id == picking) if existing_lines: return { 'warning': { 'title': 'Duplicate', 'message': 'This picking has already been added to the shipment group' } } # Buat line untuk setiap move created_lines = [] for move in picking.move_ids_without_package: line = self.env['shipment.group.line'].create({ 'shipment_id': self.id, 'product_id': move.product_id.id, 'carrier': picking.carrier, 'invoice_marketplace': picking.invoice_mp, 'picking_id': picking.id, }) created_lines.append(line.id) return { 'effect': { 'fadeout': 'slow', 'message': f'Added {len(created_lines)} products from {picking.name}', 'type': 'rainbow_man', } } @api.model def create(self, vals): vals['number'] = self.env['ir.sequence'].next_by_code('shipment.group') or '0' result = super(ShipmentGroup, self).create(vals) return result class ShipmentGroupLine(models.Model): _name = 'shipment.group.line' _description = 'Shipment Group Line' _order = 'shipment_id, id' shipment_id = fields.Many2one('shipment.group', string='Shipment Ref', required=True, ondelete='cascade', index=True, copy=False) product_id = fields.Many2one('product.product', string='Product') carrier = fields.Char(string='Shipping Method') invoice_marketplace = fields.Char(string='Invoice Marketplace') picking_id = fields.Many2one('stock.picking', string='Picking')