summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/invoice_reklas_penjualan.py
blob: 2f5ee160807d4941ecc67659a37cb35ee08cf16a (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
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 InvoiceReklasPenjualan(models.TransientModel):
    _name = 'invoice.reklas.penjualan'
    _description = "digunakan untuk reklas Uang Muka Penjualan"

    name = fields.Char(string='Name')
    invoice_reklas_line = fields.One2many('invoice.reklas.penjualan.line', 'invoice_reklas_id', string='Invoice Reklas Line')

    def create_reklas_penjualan(self):
        invoices = self.invoice_reklas_line
        current_time = datetime.now()
        account_move_ids = []

        for line in invoices:
            # Ambil nama SO jika ada
            so_name = line.sale_id.name if line.sale_id else ''

            # Susun referensi nama jurnal
            ref_name = 'REKLAS {} UANG MUKA PENJUALAN {}{} {}'.format(
                line.reklas_id.name or '',
                line.name or '',
                f" - {so_name}" if so_name else '',
                line.partner_id.name or ''
            )

            # Header jurnal
            parameters_header = {
                'ref': ref_name,
                'date': current_time,
                'journal_id': 13,
                # ⬇️ Tambahkan jika tahu invoice asal (name = ID Bill)
                'bill_id': int(line.name) if line.name and line.name.isdigit() else False,
            }

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

            # Simpan info asal (optional)
            account_move.invoice_origin = line.name

            # Simpan juga ke `reklas_misc_id` jika ditemukan invoice valid
            if line.name and line.name.isdigit():
                invoice_id = self.env['account.move'].browse(int(line.name))
                if invoice_id.exists():
                    invoice_id.reklas_misc_id = account_move.id

            # Buat debit kredit line
            debit_line = {
                'move_id': account_move.id,
                'account_id': 668,  # akun penerimaan belum alokasi
                'partner_id': line.partner_id.id,
                'currency_id': 12,
                'debit': line.pay_amt,
                'credit': 0,
                'name': ref_name
            }
            credit_line = {
                'move_id': account_move.id,
                'account_id': 395,  # akun pengurang
                'partner_id': line.partner_id.id,
                'currency_id': 12,
                'debit': 0,
                'credit': line.pay_amt,
                'name': ref_name
            }

            self.env['account.move.line'].create([debit_line, credit_line])
            account_move_ids.append(account_move.id)

            line.unlink()  # bersihkan line setelah selesai

        self.unlink()  # hapus wizard utama setelah selesai

        # Tampilkan hasil jurnal reklas
        return {
            'name': _('Journal Entries'),
            'view_mode': 'tree,form',
            'res_model': 'account.move',
            'target': 'current',
            'type': 'ir.actions.act_window',
            'domain': [('id', 'in', account_move_ids)],
        }


class InvoiceReklasPenjualanLine(models.TransientModel):
    _name = 'invoice.reklas.penjualan.line'
    _description = "digunakan untuk reklas Uang Muka Penjualan"

    invoice_reklas_id = fields.Many2one('invoice.reklas.penjualan', string='Invoice Reklas')
    pay_amt = fields.Float(string='Yang dibayarkan')
    reklas_id = fields.Many2one('account.move', string='Nomor CAB', domain="[('sale_id', '=', sale_id)]")
    sale_id = fields.Many2one('sale.order', string='Sale Order')
    amount_untaxed_signed = fields.Float(string='Tax excluded')
    amount_total_signed = fields.Float(string='Total')
    name = fields.Char(string='Name')
    partner_id = fields.Many2one('res.partner', string='Partner')