summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/user_pengajuan_tempo_request.py
blob: 2d4875fba3b16fed9d0e4dc0300eaa44b65fcd45 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo.http import request

class RejectReasonWizard(models.TransientModel):
    _name = 'reject.reason.wizard'
    _description = 'Wizard for Reject Reason'

    request_id = fields.Many2one('user.pengajuan.tempo.request', string='Request')
    reason_reject = fields.Text(string='Reason for Rejection', required=True)

    def confirm_reject(self):
        tempo = self.request_id
        if tempo:
            tempo.write({'reason_reject': self.reason_reject})
            tempo.state_tempo = 'reject'
            template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_reject')
            template.send_mail(tempo.id, force_send=True)
        return {'type': 'ir.actions.act_window_close'}
class ConfirmApprovalWizard(models.TransientModel):
    _name = 'confirm.approval.wizard'
    _description = 'Wizard Konfirmasi Approval'

    tempo_id = fields.Many2one('user.pengajuan.tempo.request', string='Tempo', required=True)

    def confirm_approval(self):
        tempo = self.tempo_id
        if tempo.state_tempo == 'draft':
            tempo.state_tempo = 'approval_sales'
            template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_approved_by_sales')
            template.send_mail(tempo.id, force_send=True)
        elif tempo.state_tempo == 'approval_sales':
            tempo.state_tempo = 'approval_finance'
            template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_to_aprove_director')
            template.send_mail(tempo.id, force_send=True)
        elif tempo.state_tempo == 'approval_finance':
            tempo.state_tempo = 'approval_director'



class UserPengajuanTempoRequest(models.Model):
    _name = 'user.pengajuan.tempo.request'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _rec_name = 'user_id'

    user_id = fields.Many2one('res.partner', string='User')
    user_company_id = fields.Many2one('res.partner', string='Company')
    pengajuan_tempo_id = fields.Many2one('user.pengajuan.tempo', string='Form Tempo')
    tempo_duration = fields.Many2one('account.payment.term', string='Durasi Tempo', tracking=3, )
    tempo_limit = fields.Char(string='Limit Tempo', tracking=3)
    state_tempo = fields.Selection([
        ('draft', 'Pengajuan Tempo'),
        ('approval_sales', 'Approved by Sales Manager'),
        ('approval_finance', 'Approved by Finance'),
        ('approval_director', 'Approved by Director'),
        ('reject', 'Rejected'),
    ], string='Status', readonly=True, copy=False, index=True, track_visibility='onchange', default='draft')
    reason_reject = fields.Char(string='Limit Tempo')

    @api.onchange('tempo_duration')
    def _tempo_duration_change(self, vals):
        for tempo in self:
            if tempo.env.user.id not in (7, 377):
                raise UserError("Durasi tempo hanya bisa di ubah oleh Sales Manager atau Direktur")

    @api.onchange('tempo_limit')
    def _onchange_tempo_limit(self):
        for tempo in self:
            if tempo.env.user.id not in (7, 377, 12182):
                raise UserError("Limit tempo hanya bisa diubah oleh Sales Manager atau Direktur")

    def button_approve(self):
        for tempo in self:
            if tempo.state_tempo == 'draft':
                if tempo.env.user.id in (688, 28, 7):
                    raise UserError("Pengajuan tempo harus di approve oleh sales manager terlebih dahulu")
                else:
                    # if tempo.env.user.id != 377:
                    if tempo.env.user.id != 12182:
                        raise UserError("Pengajuan tempo hanya bisa di approve oleh sales manager")
                    else:
                        return {
                            'type': 'ir.actions.act_window',
                            'name': 'Konfirmasi Approve',
                            'res_model': 'confirm.approval.wizard',
                            'view_mode': 'form',
                            'target': 'new',
                            'context': {
                                'default_tempo_id': tempo.id,
                            },
                        }

            elif tempo.state_tempo == 'approval_sales':
                if tempo.env.user.id == 7:
                    raise UserError("Pengajuan tempo harus di approve oleh Finence terlebih dahulu")
                else:
                    # if tempo.env.user.id not in (688, 28):
                    if tempo.env.user.id not in (101,288,28,12182):
                        raise UserError("Pengajuan tempo hanya bisa di approve oleh Finence")
                    else:
                        return {
                            'type': 'ir.actions.act_window',
                            'name': 'Konfirmasi Approve',
                            'res_model': 'confirm.approval.wizard',
                            'view_mode': 'form',
                            'target': 'new',
                            'context': {
                                'default_tempo_id': tempo.id,
                            },
                        }

            elif tempo.state_tempo == 'approval_finance':
                # if tempo.env.user.id != 7:
                if tempo.env.user.id != 12182:
                    raise UserError("Pengajuan tempo hanya bisa di approve oleh Direktur")
                else:
                    return {
                        'type': 'ir.actions.act_window',
                        'name': 'Konfirmasi Approve',
                        'res_model': 'confirm.approval.wizard',
                        'view_mode': 'form',
                        'target': 'new',
                        'context': {
                            'default_tempo_id': tempo.id,
                        },
                    }

    def button_reject(self):
        return {
            'type': 'ir.actions.act_window',
            'name': _('Reject Reason'),
            'res_model': 'reject.reason.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {'default_request_id': self.id},
        }

    def write(self, vals):
        is_approve = True if self.state_tempo == 'approval_director' or vals.get('state_tempo') == 'approval_director' else False
        # if self.is_approve and is_approve:
        #     raise UserError('Tidak dapat mengubah approval yang sudah diisi')
        limit_tempo = ''
        if vals.get('tempo_limit'):
            limit_tempo = vals.get('tempo_limit')
        elif self.tempo_limit:
            limit_tempo = self.tempo_limit
        else:
            limit_tempo = self.pengajuan_tempo_id.tempo_limit

        tempo_duration = ''
        if vals.get('tempo_duration'):
            tempo_duration = vals.get('tempo_duration')
        elif self.tempo_duration:
            tempo_duration = self.tempo_duration
        else:
            tempo_duration = self.pengajuan_tempo_id.tempo_duration
        if is_approve:
            self.pengajuan_tempo_id.partner_id = self.user_id.id
            # informasi perusahaan
            self.user_company_id.name_tempo = self.pengajuan_tempo_id.name_tempo
            self.user_company_id.industry_id_tempo = self.pengajuan_tempo_id.industry_id_tempo
            self.user_company_id.street_tempo = self.pengajuan_tempo_id.street_tempo
            self.user_company_id.state_id_tempo = self.pengajuan_tempo_id.state_id_tempo
            self.user_company_id.city_id_tempo = self.pengajuan_tempo_id.city_id_tempo
            self.user_company_id.zip_tempo = self.pengajuan_tempo_id.zip_tempo
            self.user_company_id.mobile_tempo = self.pengajuan_tempo_id.mobile_tempo
            self.user_company_id.bank_name_tempo = self.pengajuan_tempo_id.bank_name_tempo
            self.user_company_id.account_name_tempo = self.pengajuan_tempo_id.account_name_tempo
            self.user_company_id.account_number_tempo = self.pengajuan_tempo_id.account_number_tempo
            self.user_company_id.website_tempo = self.pengajuan_tempo_id.website_tempo
            self.user_company_id.estimasi_tempo = self.pengajuan_tempo_id.estimasi_tempo
            self.user_company_id.tempo_duration = tempo_duration
            self.user_company_id.tempo_limit = limit_tempo
            self.user_company_id.category_produk_ids = self.pengajuan_tempo_id.category_produk_ids

            # Kontak Perusahaan
            self.user_company_id.direktur_name = self.pengajuan_tempo_id.direktur_name
            self.user_company_id.direktur_mobile = self.pengajuan_tempo_id.direktur_mobile
            self.user_company_id.direktur_email = self.pengajuan_tempo_id.direktur_email
            self.user_company_id.purchasing_name = self.pengajuan_tempo_id.purchasing_name
            self.user_company_id.purchasing_mobile = self.pengajuan_tempo_id.purchasing_mobile
            self.user_company_id.purchasing_email = self.pengajuan_tempo_id.purchasing_email
            self.user_company_id.finance_name = self.pengajuan_tempo_id.finance_name
            self.user_company_id.finance_mobile = self.pengajuan_tempo_id.finance_mobile
            self.user_company_id.finance_email = self.pengajuan_tempo_id.finance_email

            # Pengiriman
            self.user_company_id.pic_name = self.pengajuan_tempo_id.pic_name
            self.user_company_id.street_pengiriman = self.pengajuan_tempo_id.street_pengiriman
            self.user_company_id.state_id_pengiriman = self.pengajuan_tempo_id.state_id_pengiriman
            self.user_company_id.city_id_pengiriman = self.pengajuan_tempo_id.city_id_pengiriman
            self.user_company_id.zip_pengiriman = self.pengajuan_tempo_id.zip_pengiriman
            self.user_company_id.invoice_pic = self.pengajuan_tempo_id.invoice_pic
            self.user_company_id.street_invoice = self.pengajuan_tempo_id.street_invoice
            self.user_company_id.state_id_invoice = self.pengajuan_tempo_id.state_id_invoice
            self.user_company_id.city_id_invoice = self.pengajuan_tempo_id.city_id_invoice
            self.user_company_id.tukar_invoice = self.pengajuan_tempo_id.tukar_invoice
            self.user_company_id.jadwal_bayar = self.pengajuan_tempo_id.jadwal_bayar
            self.user_company_id.dokumen_pengiriman = self.pengajuan_tempo_id.dokumen_pengiriman
            self.user_company_id.dokumen_invoice = self.pengajuan_tempo_id.dokumen_invoice

            # Referensi
            self.user_company_id.supplier_ids = self.pengajuan_tempo_id.supplier_ids

            # Dokumen
            self.user_company_id.dokumen_nib = self.pengajuan_tempo_id.dokumen_nib[0]
            if self.user_company_id.dokumen_nib:
                self.user_company_id.message_post(body='Dokumen NIB', attachment_ids=[self.user_company_id.dokumen_nib.id])

            self.user_company_id.dokumen_npwp = self.pengajuan_tempo_id.dokumen_npwp[0]
            if self.user_company_id.dokumen_npwp:
                self.user_company_id.message_post(body='Dokumen NPWP', attachment_ids=[self.user_company_id.dokumen_npwp.id])

            self.user_company_id.dokumen_sppkp = self.pengajuan_tempo_id.dokumen_sppkp[0]
            if self.user_company_id.dokumen_sppkp:
                self.user_company_id.message_post(body='Dokumen SPPKP', attachment_ids=[self.user_company_id.dokumen_sppkp.id])

            self.user_company_id.dokumen_akta_perubahan = self.pengajuan_tempo_id.dokumen_akta_perubahan[0] if self.pengajuan_tempo_id.dokumen_akta_perubahan else []
            if self.user_company_id.dokumen_akta_perubahan:
                self.user_company_id.message_post(body='Dokumen Akta Perubahan',
                                              attachment_ids=[self.user_company_id.dokumen_akta_perubahan.id])

            self.user_company_id.dokumen_ktp_dirut = self.pengajuan_tempo_id.dokumen_ktp_dirut[0] if self.pengajuan_tempo_id.dokumen_ktp_dirut else []
            if self.user_company_id.dokumen_ktp_dirut:
                self.user_company_id.message_post(body='Dokumen Ktp Dirut',
                                              attachment_ids=[self.user_company_id.dokumen_ktp_dirut.id])

            self.user_company_id.dokumen_akta_pendirian = self.pengajuan_tempo_id.dokumen_akta_pendirian[0] if self.pengajuan_tempo_id.dokumen_akta_pendirian else []
            if self.user_company_id.dokumen_akta_pendirian:
                self.user_company_id.message_post(body='Dokumen Akta Pendirian',
                                              attachment_ids=[self.user_company_id.dokumen_akta_pendirian.id])

            self.user_company_id.dokumen_laporan_keuangan = self.pengajuan_tempo_id.dokumen_laporan_keuangan[0] if self.pengajuan_tempo_id.dokumen_laporan_keuangan else []
            if self.user_company_id.dokumen_laporan_keuangan:
                self.user_company_id.message_post(body='Dokumen Laporan Keuangan',
                                              attachment_ids=[self.user_company_id.dokumen_laporan_keuangan.id])

            self.user_company_id.dokumen_foto_kantor = self.pengajuan_tempo_id.dokumen_foto_kantor[0] if self.pengajuan_tempo_id.dokumen_foto_kantor else []
            if self.user_company_id.dokumen_foto_kantor:
                self.user_company_id.message_post(body='Dokumen Foto Kantor',
                                              attachment_ids=[self.user_company_id.dokumen_foto_kantor.id])

            self.user_company_id.dokumen_tempat_bekerja = self.pengajuan_tempo_id.dokumen_tempat_bekerja[0] if self.pengajuan_tempo_id.dokumen_tempat_bekerja else []
            if self.user_company_id.dokumen_tempat_bekerja:
                self.user_company_id.message_post(body='Dokumen Tempat Bekerja',
                                              attachment_ids=[self.user_company_id.dokumen_tempat_bekerja.id])
                # self.user_company_id.active = True
                # user.send_company_request_approve_mail()
            self.user_company_id.property_payment_term_id = self.pengajuan_tempo_id.tempo_duration.id
            self.user_company_id.active_limit = True
            self.user_company_id.warning_stage = float(self.pengajuan_tempo_id.tempo_limit) - (float(self.pengajuan_tempo_id.tempo_limit)/2)
            self.user_company_id.blocking_stage = self.pengajuan_tempo_id.tempo_limit
            # template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_approved')
            # tempo = self.pengajuan_tempo_id
            # template.send_mail(tempo.id, force_send=True)
            template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_approved')
            template.send_mail(self.id, force_send=True)
                # self.user_id.parent_id = new_company.id
                # user.send_company_request_reject_mail()
        return super(UserPengajuanTempoRequest, self).write(vals)

    def get_user_by_email(self, email):
        return request.env['res.users'].search([
            ('login', '=', email),
            ('active', 'in', [True, False])
        ])

    def format_currency(self, number):
        number = int(number)
        return "{:,}".format(number).replace(',', '.')