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

import logging, math

_logger = logging.getLogger(__name__)


class UangmukaPembelian(models.TransientModel):
    _name = 'uangmuka.pembelian'
    _description = 'digunakan untuk membuat Uang Muka Pembelian'

    pay_amt = fields.Float(string='Uang Muka', help='berapa nilai yang terbentuk untuk COA Uang Muka Pembelian')
    account_id = fields.Many2one('account.account', string='Bank Intransit', default=389, help='pilih COA intransit bank')
    ongkir_amt = fields.Float(string='Ongkir', help='masukan nilai yang akan menjadi Pendapatan Ongkos Kirim')
    selisih_amt = fields.Float(string='Selisih', help='masukan nilai yang akan menjadi Selisih Pembayaran')
    total_amt = fields.Float(string='Total', help='Total yang akan masuk di journal entries')
    
    @api.onchange('pay_amt', 'ongkir_amt', 'selisih_amt')
    def _compute_total_amt(self):
        for o in self:
            o.total_amt = o.pay_amt + o.ongkir_amt + o.selisih_amt

    def create_uangmukapembelian(self):
        if not self.account_id:
            raise UserError('Bank Intransit harus diisi')

        orders = self.env['purchase.order'].browse(self._context.get('active_ids',[]))
        current_time = datetime.now()

        is_have_ongkir = is_have_selisih = False
        if self.ongkir_amt > 0:
            is_have_ongkir = True
        if not math.isclose(self.selisih_amt, 0):
            is_have_selisih = True

        for order in orders:
            # if order.is_create_uangmuka == True:
            #     action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_purchase_order_multi_uangmuka2')
            #     action['context'] = {
            #         'order_ids': [x.id for x in order]
            #     }
            #     return action
            partner_name = order.partner_id.name
            if order.partner_id.parent_id:
                partner_name = order.partner_id.parent_id.name
            ref_label = 'UANG MUKA PEMBELIAN '+order.name+' '+partner_name
            param_header = {
                'ref': ref_label,
                'date': current_time,
                'journal_id': 21,
                'purchase_order_id': order.id
            }

            account_move = request.env['account.move'].create([param_header])
            _logger.info('Success Create Uang Muka Pembelian %s' % account_move.name)
            account_move.purchase_order_id = order.id # isi field purchase_order_id
            
            if order.partner_id.parent_id:
                partner_id = order.partner_id.parent_id.id
            else:
                partner_id = order.partner_id.id

            param_debit = {
                'move_id': account_move.id,
                'account_id': 579, # uang muka persediaan barang dagang
                'partner_id': partner_id,
                'currency_id': 12,
                'debit': self.pay_amt,
                'credit': 0,
                'name': ref_label,
            }
            param_debit_ongkir = {
                'move_id': account_move.id,
                'account_id': 758, # biaya ongkos kirim
                'partner_id': partner_id,
                'currency_id': 12,
                'debit': self.ongkir_amt,
                'credit': 0,
                'name': ref_label,
            }
            param_debit_selisih = {
                'move_id': account_move.id,
                'account_id': 767, # selisih pembayaran
                'partner_id': partner_id,
                'currency_id': 12,
                'debit': self.selisih_amt,
                'credit': 0,
                'name': ref_label,
            }

            param_credit = {
                'move_id': account_move.id,
                'account_id': self.account_id.id, # bank in transit
                'partner_id': partner_id,
                'currency_id': 12,
                'debit': 0,
                'credit': self.pay_amt + self.ongkir_amt + self.selisih_amt,
                'name': ref_label,
            }
            if is_have_ongkir and is_have_selisih:  
                request.env['account.move.line'].create([param_debit, param_debit_ongkir, param_debit_selisih, param_credit])
            elif is_have_ongkir:
                request.env['account.move.line'].create([param_debit, param_debit_ongkir, param_credit])
            elif is_have_selisih:
                request.env['account.move.line'].create([param_debit, param_debit_selisih, param_credit])
            else:
                request.env['account.move.line'].create([param_debit, param_credit])
            # order.is_create_uangmuka = True
            order.move_entry_id = account_move.id
            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
            }