summaryrefslogtreecommitdiff
path: root/fixco_custom/models/invoice_reklas.py
blob: d97d868ce822500b34d9d4ecc49349e309e5f6a6 (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
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from datetime import datetime
from odoo.http import request

import logging

_logger = logging.getLogger(__name__)


class InvoiceReklas(models.TransientModel):
    _name = 'invoice.reklas'
    _description = "digunakan untuk reklas Uang Muka Penjualan"
    reklas_id = fields.Many2one('account.move', string='Nomor CAB', domain="[('move_type','=','entry')]")
    pay_amt = fields.Float(string='Yang dibayarkan')
    reklas_type = fields.Selection([
        ('penjualan', 'Penjualan'),
        ('pembelian', 'Pembelian'),
    ], string='Reklas Tipe')

    @api.onchange('reklas_type')
    def _onchange_reklas_type(self):
        active_ids = self._context.get('active_ids', [])
        if not active_ids:
            return

        move = self.env['account.move'].browse(active_ids[0])
        cab = False

        # Deteksi dari mana asal CAB
        if move.move_type == 'entry':
            cab = move
        elif move.move_type == 'in_invoice':
            if move.reklas_misc_id:
                cab = move.reklas_misc_id
            elif move.purchase_id and move.purchase_id.move_id:
                cab = move.purchase_id.move_id

        # Isi field Nomor CAB jika ditemukan
        if cab:
            self.reklas_id = cab.id

        # Nilai yang dibayarkan harus tetap ambil dari invoice/bill
        self.pay_amt = move.amount_total


    @api.model
    def default_get(self, fields):
        res = super().default_get(fields)
        active_ids = self._context.get('active_ids', [])
        if active_ids:
            move = self.env['account.move'].browse(active_ids[0])
            cab = False

            if move.move_type == 'entry':
                cab = move
            elif move.move_type == 'in_invoice':
                if move.reklas_misc_id:
                    cab = move.reklas_misc_id
                elif move.purchase_id and move.purchase_id.move_id:
                    cab = move.purchase_id.move_id

            if cab:
                res['reklas_id'] = cab.id

            res['pay_amt'] = move.amount_total
        return res


    # @api.onchange('reklas_type')
    # def _onchange_reklas_type(self):
    #     if self.reklas_type == 'penjualan':
    #         invoices = self.env['account.move'].browse(self._context.get('active_ids', []))
    #         self.pay_amt = invoices.amount_total

    def create_reklas(self):
        if not self.reklas_type:
            raise UserError('Reklas Tipe harus diisi')
        if not self.reklas_id:
            raise UserError('Nomor CAB harus diisi')

        invoices = self.env['account.move'].browse(self._context.get('active_ids', []))
        current_time = datetime.now()

        for invoice in invoices:
            # Ambil nama PO jika ada
            po_name = invoice.purchase_id.name if invoice.purchase_id else ''

            # Susun nama referensi dengan aman
            ref_name = 'REKLAS {} UANG MUKA {}{}{} {}'.format(
                self.reklas_id.name or '',
                'PENJUALAN' if self.reklas_type == 'penjualan' else 'PEMBELIAN',
                f" {invoice.name}" if invoice.name != self.reklas_id.name else '',
                f" - {po_name}" if po_name else '',
                invoice.partner_id.name or ''
            )

            # Header jurnal reklas
            parameters_header = {
                'ref': ref_name,
                'date': current_time,
                'journal_id': 19
            }

            if invoice.purchase_id:
                parameters_header['purchase_id'] = invoice.purchase_id.id

            account_move = request.env['account.move'].create([parameters_header])
            _logger.info('Success Reklas with %s' % account_move.name)

            # ✅ Set Bill asal sebagai source document
            if invoice.move_type == 'in_invoice':
                account_move.bill_id = invoice.id
            
            # Tambahkan info asal invoice ke jurnal (opsional)
            account_move.invoice_origin = invoice.name

            # Simpan hubungan balik ke invoice
            invoice.reklas_misc_id = account_move.id

            # Buat line debit dan kredit
            if self.reklas_type == 'penjualan':
                parameter_debit = {
                    'move_id': account_move.id,
                    'account_id': 578,  # penerimaan belum alokasi
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': self.pay_amt,
                    'credit': 0,
                    'name': ref_name
                }
                parameter_credit = {
                    'move_id': account_move.id,
                    'account_id': 347,
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': 0,
                    'credit': self.pay_amt,
                    'name': ref_name
                }
            else:  # pembelian
                parameter_debit = {
                    'move_id': account_move.id,
                    'account_id': 388,
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': self.pay_amt,
                    'credit': 0,
                    'name': ref_name
                }
                parameter_credit = {
                    'move_id': account_move.id,
                    'account_id': 579,
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': 0,
                    'credit': self.pay_amt,
                    'name': ref_name
                }

            # Simpan journal lines
            request.env['account.move.line'].create([parameter_debit, parameter_credit])

            # Tampilkan hasil jurnal reklas
            return {
                'name': _('Journal Entries'),
                'view_mode': 'form',
                'res_model': 'account.move',
                'target': 'current',
                'view_id': False,
                'type': 'ir.actions.act_window',
                'res_id': account_move.id
            }