summaryrefslogtreecommitdiff
path: root/fixco_custom/models/shipment_group.py
blob: 5f6c3155f56492bfd9c8b79a3a50ec4d75a6fd20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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')