summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/stock_move.py
blob: 320c175f9607a380a5a24e46fd12e370930bbd4a (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from odoo import fields, models, api
from odoo.tools.misc import format_date, OrderedSet
from odoo.exceptions import UserError
import logging
from odoo.tools.float_utils import float_compare, float_is_zero, float_round


_logger = logging.getLogger(__name__)

class StockMove(models.Model):
    _inherit = 'stock.move'

    line_no = fields.Integer('No', default=0)
    sale_id = fields.Many2one('sale.order', string='SO')
    print_barcode = fields.Boolean(
        string="Print Barcode",
        default=lambda self: self.product_id.print_barcode,
    )
    qr_code_variant = fields.Binary("QR Code Variant", compute='_compute_qr_code_variant')
    barcode = fields.Char(string='Barcode', related='product_id.barcode')
    vendor_id = fields.Many2one('res.partner' ,string='Vendor')
    hold_outgoingg = fields.Boolean('Hold Outgoing', default=False)
    product_image = fields.Binary(related="product_id.image_128", string="Product Image", readonly=True)
    partial = fields.Boolean('Partial?', default=False)
    # Ambil product uom dari SO line
    @api.model
    def create(self, vals):
        if vals.get('sale_line_id'):
            sale_line = self.env['sale.order.line'].browse(vals['sale_line_id'])
            vals['product_uom'] = sale_line.product_uom.id
        return super().create(vals)
    
    def _update_reserved_quantity(
        self, need, available_quantity, location_id,
        lot_id=None, package_id=None, owner_id=None, strict=True
    ):
        self.ensure_one()

        picking = self.picking_id
        if picking and 'BU/PICK' in (picking.name or ''):
            _logger.info(f"[LocatorLogic] Running custom locator logic for {picking.name}")

            # Ambil semua lokasi anak dari source location (ex: BU/Stock)
            locations = self.env['stock.location'].search([
                ('id', 'child_of', self.location_id.id),
                ('usage', '=', 'internal'),
                ('is_locked', '=', False),
                # ('id', '!=', 57),
            ], order='rack_level asc')

            total_reserved = 0.0
            remaining_need = need

            for loc in locations:
                if remaining_need <= 0:
                    break 

                quants = self.env['stock.quant']._gather(self.product_id, loc)
                for quant in quants:
                    if quant.available_quantity <= 0:
                        continue

                    qty_to_take = min(quant.available_quantity, remaining_need)
                    _logger.info(
                        f"[LocatorLogic] Reserving {qty_to_take}/{remaining_need} "
                        f"from {loc.display_name} (avail={quant.available_quantity})"
                    )

                    reserved_now = super(StockMove, self)._update_reserved_quantity(
                        qty_to_take, quant.available_quantity, quant.location_id,
                        lot_id, package_id, owner_id, strict
                    )

                    total_reserved += reserved_now
                    remaining_need -= reserved_now

                    if remaining_need <= 0:
                        break

            if total_reserved > 0:
                _logger.info(f"[LocatorLogic] Total reserved: {total_reserved} / {need}")
                return total_reserved
            else:
                _logger.info("[LocatorLogic] No available stock found in unlocked locations by level order.")
                return 0

        return super(StockMove, self)._update_reserved_quantity(
            need, available_quantity, location_id, lot_id, package_id, owner_id, strict
        )

    
    # @api.model_create_multi
    # def create(self, vals_list):
    #     moves = super(StockMove, self).create(vals_list)
        
    #     for move in moves:
    #         if move.product_id and move.location_id.id == 58 and move.location_dest_id.id == 57 and move.picking_type_id.id == 75:
    #             po_line = self.env['purchase.order.line'].search([
    #                 ('product_id', '=', move.product_id.id),
    #                 ('order_id.name', '=', move.origin)
    #             ], limit=1)
    #             if po_line:
    #                 move.write({'purchase_line_id': po_line.id})
        
        # return moves

    @api.constrains('product_id')
    def constrains_product_to_fill_vendor(self):
        for rec in self:
            if rec.product_id and rec.bom_line_id:
                if rec.product_id.x_manufacture.override_vendor_id:
                    rec.vendor_id = rec.product_id.x_manufacture.override_vendor_id.id
                else:
                    purchase_pricelist = self.env['purchase.pricelist'].search(
                    [('product_id', '=', rec.product_id.id),
                    ('is_winner', '=', True)],
                    limit=1)
                    if purchase_pricelist:
                        rec.vendor_id = purchase_pricelist.vendor_id.id

    def _compute_qr_code_variant(self):
        for rec in self:
            if rec.picking_id.picking_type_code == 'outgoing' and rec.picking_id and rec.picking_id.origin and rec.picking_id.origin.startswith('SO/'):
                rec.qr_code_variant = rec.product_id.qr_code_variant
                rec.print_barcode = True
            if rec.print_barcode and rec.print_barcode == True and rec.product_id and rec.product_id.qr_code_variant:
                rec.qr_code_variant = rec.product_id.qr_code_variant
            else:
                rec.qr_code_variant = False


    def write(self, vals):
        res = super(StockMove, self).write(vals)
        if 'print_barcode' in vals:
            for line in self:
                if line.product_id:
                    line.product_id.print_barcode = vals['print_barcode']
        return res

    def _do_unreserve(self, product=None, quantity=False):
        moves_to_unreserve = OrderedSet()
        for move in self:
            if move.state == 'cancel' or (move.state == 'done' and move.scrapped):
                continue
            elif move.state == 'done':
                raise UserError("You cannot unreserve a stock move that has been set to 'Done'.")

            if product and move.product_id != product:
                continue  # Skip moves that don't match the specified product
            moves_to_unreserve.add(move.id)
            
        moves_to_unreserve = self.env['stock.move'].browse(moves_to_unreserve)

        ml_to_update, ml_to_unlink = OrderedSet(), OrderedSet()
        moves_not_to_recompute = OrderedSet()
        
        for ml in moves_to_unreserve.move_line_ids:
            if product and ml.product_id != product:
                continue  # Only affect the specified product

            if quantity and quantity > 0:
                # Only reduce by the specified quantity if it is greater than zero
                ml_to_update.add(ml.id)
                remaining_qty = ml.product_uom_qty - quantity
                ml.write({'product_uom_qty': remaining_qty if remaining_qty > 0 else 0})
                quantity = 0  # Set to zero to prevent further unreserving in the same loop
            elif ml.qty_done:
                ml_to_update.add(ml.id)
            else:
                ml_to_unlink.add(ml.id)
                moves_not_to_recompute.add(ml.move_id.id)
                
        ml_to_update, ml_to_unlink = self.env['stock.move.line'].browse(ml_to_update), self.env['stock.move.line'].browse(ml_to_unlink)
        moves_not_to_recompute = self.env['stock.move'].browse(moves_not_to_recompute)

        ml_to_unlink.unlink()
        (moves_to_unreserve - moves_not_to_recompute)._recompute_state()
        return True


    def _prepare_account_move_line_from_mr(self, po_line, qty, move=False):
        po_line.ensure_one()
        aml_currency = move and move.currency_id or po_line.currency_id
        date = move and move.date or fields.Date.today()
        res = {
            'display_type': po_line.display_type,
            'sequence': po_line.sequence,
            'name': '%s: %s' % (po_line.order_id.name, po_line.name),
            'product_id': po_line.product_id.id,
            'product_uom_id': po_line.product_uom.id,
            'quantity': qty,
            'price_unit': po_line.currency_id._convert(po_line.price_unit, aml_currency, po_line.company_id, date, round=False),
            'tax_ids': [(6, 0, po_line.taxes_id.ids)],
            'analytic_account_id': po_line.account_analytic_id.id,
            'analytic_tag_ids': [(6, 0, po_line.analytic_tag_ids.ids)],
            'purchase_line_id': po_line.id,
        }
        if not move:
            return res

        if self.currency_id == move.company_id.currency_id:
            currency = False
        else:
            currency = move.currency_id

        res.update({
            'move_id': move.id,
            'currency_id': currency and currency.id or False,
            'date_maturity': move.invoice_date_due,
            'partner_id': move.partner_id.id,
        })
        return res

    def _create_account_move_line(self, credit_account_id, debit_account_id, journal_id, qty, description, svl_id, cost):
        self.ensure_one()
        if self.picking_id.is_internal_use:
            AccountMove = self.env['account.move'].with_context(default_journal_id=journal_id)

            # 538 is static id for "Biaya Umum Lain-Lain" on account.account model
            # 440 is static id for "PPN Keluaran" on account.account model
            debit_account_id = self.picking_id.account_id.id if self.picking_id.account_id.id else 538

            tax = cost * (11 / 100)
            move_lines = self._prepare_account_move_line(qty, cost, credit_account_id, debit_account_id, description)
            move_lines += self._prepare_account_move_line(qty, tax, 440, debit_account_id, description)

            if move_lines:
                date = self._context.get('force_period_date', fields.Date.context_today(self))
                new_account_move = AccountMove.sudo().create({
                    'journal_id': journal_id,
                    'line_ids': move_lines,
                    'date': date,
                    'ref': description,
                    'stock_move_id': self.id,
                    'stock_valuation_layer_ids': [(6, None, [svl_id])],
                    'move_type': 'entry',
                })
                new_account_move._post()
            return True
        return super(StockMove, self)._create_account_move_line(credit_account_id, debit_account_id, journal_id, qty, description, svl_id, cost)


class StockMoveLine(models.Model):
    _inherit = 'stock.move.line'

    line_no = fields.Integer('No', default=0)
    note = fields.Char('Note')
    manufacture = fields.Many2one('x_manufactures', string="Brands", related="product_id.x_manufacture", store=True)
    qty_yang_mau_dikirim = fields.Float(
        string='Qty yang Mau Dikirim',
        compute='_compute_delivery_status_detail',
        store=False
    )
    qty_terkirim = fields.Float(
        string='Qty Terkirim',
        compute='_compute_delivery_status_detail',
        store=False
    )
    qty_gantung = fields.Float(
        string='Qty Gantung',
        compute='_compute_delivery_status_detail',
        store=False
    )
    delivery_status = fields.Selection([
        ('none', 'No Movement'),
        ('partial', 'Partial'),
        ('partial_final', 'Partial Final'),
        ('full', 'Full'),
    ], string='Delivery Status', compute='_compute_delivery_status_detail', store=False)

    @api.depends('qty_done', 'product_uom_qty', 'picking_id.state')
    def _compute_delivery_status_detail(self):
        for picking in self:
            # Default values
            picking.qty_yang_mau_dikirim = 0.0
            picking.qty_terkirim = 0.0
            picking.qty_gantung = 0.0
            picking.delivery_status = 'none'

            # Hanya berlaku untuk pengiriman (BU/OUT)
            if picking.picking_id.picking_type_id.code != 'outgoing':
                continue

            if picking.picking_id.name not in ['BU/OUT']:
                continue

            move_lines = picking
            if not move_lines:
                continue

            # ======================
            # HITUNG QTY
            # ======================
            total_qty = move_lines.product_uom_qty

            done_qty_total = move_lines.move_id.sale_line_id.qty_delivered 
            order_qty_total = move_lines.move_id.sale_line_id.product_uom_qty 
            gantung_qty_total = order_qty_total - done_qty_total - total_qty

            picking.qty_yang_mau_dikirim = total_qty
            picking.qty_terkirim = done_qty_total
            picking.qty_gantung = gantung_qty_total

            # if total_qty == 0:
            #     picking.delivery_status = 'none'
            #     continue

            # if done_qty_total == 0:
            #     picking.delivery_status = 'none'
            #     continue

            # ======================
            # CEK BU/OUT LAIN (BACKORDER)
            # ======================
            # has_other_out = self.env['stock.picking'].search_count([
            #     ('group_id', '=', picking.group_id.id),
            #     ('name', 'ilike', 'BU/OUT'),
            #     ('id', '!=', picking.id),
            #     ('state', 'in', ['assigned', 'waiting', 'confirmed', 'done']),
            # ])

            # ======================
            # LOGIKA STATUS
            # ======================
            if gantung_qty_total == 0 and done_qty_total == 0:
                # Semua barang udah terkirim, ga ada picking lain
                picking.delivery_status = 'full'

            elif gantung_qty_total > 0 and total_qty > 0 and done_qty_total == 0:
                # Masih ada picking lain dan sisa gantung → proses masih jalan
                picking.delivery_status = 'partial'

            # elif gantung_qty_total > 0:
            #     # Ini picking terakhir, tapi qty belum full
            #     picking.delivery_status = 'partial_final'

            elif gantung_qty_total == 0 and done_qty_total > 0 and total_qty > 0:
                # Udah kirim semua tapi masih ada picking lain (rare case)
                picking.delivery_status = 'partial_final'

            else:
                picking.delivery_status = 'none'

    # Ambil uom dari stock move
    @api.model
    def create(self, vals):
        if 'move_id' in vals and 'product_uom_id' not in vals:
            move = self.env['stock.move'].browse(vals['move_id'])
            if move.product_uom:
                vals['product_uom_id'] = move.product_uom.id
        return super().create(vals)
    def _action_done(self):
        for line in self:
            if line.location_dest_id and line.location_dest_id.is_locked:
                raise UserError(f"Lokasi '{line.location_dest_id.display_name}' sedang dikunci dan tidak bisa menerima barang.")
            if line.location_id and line.location_id.is_locked:
                raise UserError(f"Lokasi '{line.location_id.display_name}' sedang dikunci dan tidak bisa reserve barang.")
        return super(StockMoveLine, self)._action_done()