summaryrefslogtreecommitdiff
path: root/fixco_custom/models/shipment_group.py
blob: 3d6084011d7791ca66f2525938c8b34a7ab6b364 (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
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)
    picking_lines = fields.One2many('picking.line', 'shipment_id', string='Picking Lines', auto_join=True)
    
    @api.constrains('picking_lines')
    def _check_picking_lines(self):
        for record in self:
            record.shipment_line.unlink()
            
            for picking_line in record.picking_lines:
                if not picking_line.picking_id:
                    continue
                
                for move in picking_line.picking_id.move_ids_without_package:
                    existing_line = record.shipment_line.filtered(
                        lambda l: l.product_id == move.product_id and
                        l.invoice_marketplace == picking_line.scan_invoice_marketplace and
                        l.carrier == picking_line.carrier
                    )
                    
                    if not existing_line:
                        self.env['shipment.group.line'].create({
                            'shipment_id': record.id,
                            'product_id': move.product_id.id,
                            'carrier': picking_line.picking_id.carrier,
                            'invoice_marketplace': picking_line.picking_id.invoice_mp,
                            'picking_id': picking_line.picking_id.id
                        })


    @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')
    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')


class PickingLine(models.Model):
    _name = 'picking.line'
    _description = 'Picking Line'
    _order = 'shipment_id, id'

    shipment_id = fields.Many2one('shipment.group', string='Shipment Ref', required=True, ondelete='cascade')
    picking_id = fields.Many2one('stock.picking', string='Picking')
    scan_invoice_marketplace = fields.Char(string="Scan Invoice Marketplace")
    carrier = fields.Char(string='Ekspedisi')

    @api.onchange('scan_invoice_marketplace')
    def _onchange_scan_invoice_marketplace(self):
        for line in self:
            if line.scan_invoice_marketplace:
                picking = self.env['stock.picking'].search([('invoice_mp', '=', line.scan_invoice_marketplace)], limit=1)
                line.picking_id = picking.id
                line.carrier = picking.carrier