summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/stock_move.py
blob: ac2e3cc00e151f92fe4e82d8bfbd1a9132d2eb4f (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
from odoo import fields, models, api
from odoo.tools.misc import format_date, OrderedSet
from odoo.exceptions import UserError

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

    line_no = fields.Integer('No', default=0)
    sale_id = fields.Many2one('sale.order', string='SO')

    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)