summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/invoice_reklas.py
blob: d10d4c315e07c7ca0cfe7f9cf29748ab3fdab16c (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
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')
    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):
        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:
            if self.reklas_type == 'penjualan':
                ref_name = 'REKLAS '+self.reklas_id.name+" UANG MUKA PENJUALAN "+invoice.name+" "+invoice.partner_id.name
            else:
                ref_name = 'REKLAS '+self.reklas_id.name+" UANG MUKA PEMBELIAN "+invoice.name+" "+invoice.partner_id.name
            if self.reklas_type == 'penjualan':
                parameters_header = {
                    'ref': ref_name,
                    'date': current_time,
                    'journal_id': 13
                }
            else:
                parameters_header = {
                    'ref': ref_name,
                    'date': current_time,
                    'journal_id': 13
                }

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

            if self.reklas_type == 'penjualan':
                parameter_debit = {
                    'move_id': account_move.id,
                    'account_id': 668, # 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': 395,
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': 0,
                    'credit': self.pay_amt,
                    'name': ref_name
                }
            else:
                parameter_debit = {
                    'move_id': account_move.id,
                    'account_id': 438,
                    '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': 669,
                    'partner_id': invoice.partner_id.id,
                    'currency_id': 12,
                    'debit': 0,
                    'credit': self.pay_amt,
                    'name': ref_name
                }
            request.env['account.move.line'].create([parameter_debit, parameter_credit])
            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
            }