summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/user_pengajuan_tempo_request.py
blob: 6c04ab7c28148bb968b0b00202de3bfda7f8f5eb (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
from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo.http import request

class UserPengajuanTempoRequest(models.Model):
    _name = 'user.pengajuan.tempo.request'
    _rec_name = 'user_id'

    user_id = fields.Many2one('res.partner', string='User')
    user_company_id = fields.Many2one('res.partner', string='Company')
    user_input = fields.Char(string='User Input')
    is_approve = fields.Selection([
        ('approved', 'Approve'),
        ('rejected', 'Reject'),
    ], string='Approval')
    similar_company_ids = fields.Many2many('res.partner', compute="_compute_similar_companies", string="Similar Companies")
    pengajuan_tempo_id = fields.Many2one('user.pengajuan.tempo', string='Form Tempo')

    @api.depends('user_input')
    def _compute_similar_companies(self):
        for record in self:
            if record.user_input:
                record.similar_company_ids = [(6, 0, self.get_similar_companies(record.user_input))]
            else:
                record.similar_company_ids = [(6, 0, [])]

    # def get_similar_companies(self, user_input):
    #     query = """
    #         SELECT id
    #         FROM res_partner
    #         WHERE levenshtein(name::text, %s) < 3
    #         ORDER BY levenshtein(name::text, %s) ASC
    #     """
    #     self.env.cr.execute(query, (user_input, user_input))
    #     return [row[0] for row in self.env.cr.fetchall()]

    def get_similar_companies(self, user_input):
        query = """
            SELECT id
            FROM res_partner
            WHERE (name ILIKE %s OR levenshtein(name::text, %s) < 3)
            AND active = TRUE AND is_company = TRUE
            ORDER BY levenshtein(name::text, %s) ASC
        """
        # Menggunakan '%' untuk mencocokkan nama perusahaan sebagian
        self.env.cr.execute(query, ('%' + user_input + '%', user_input, user_input))
        company_ids = [row[0] for row in self.env.cr.fetchall()]
        return company_ids

    internal_input = fields.Char(string='Internal Input')
    company_type = fields.Char(string='Company Type', compute='_compute_company_type')

    @api.depends('user_company_id.customer_type')
    def _compute_company_type(self):
        for record in self:
            if record.user_company_id.customer_type == 'nonpkp':
                record.company_type = 'Non PKP'
            elif record.user_company_id.customer_type == 'pkp':
                record.company_type = 'PKP'
            else:
                record.company_type = 'company type belum di set'

    def write(self, vals):
        user = self.get_user_by_email(self.user_id.email)
        user.parent_name = self.user_input
        is_approve = vals.get('is_approve')
        is_internal_input = vals.get('internal_input')
        company_id = ''
        if not self.user_company_id:
            company_id = request.env['res.partner'].search([('id', '=', vals.get('user_company_id'))], limit=1)
        if self.is_approve and is_approve:
            raise UserError('Tidak dapat mengubah approval yang sudah diisi')

        if is_internal_input:
            if self.user_company_id.nama_wajib_pajak == self.user_company_id.name:
                self.user_company_id.nama_wajib_pajak = is_internal_input
            self.user_company_id.name = is_internal_input

        if not self.is_approve and is_approve:
            if is_approve == 'approved':
                self.pengajuan_tempo_id.partner_id = self.user_id.id
                # informasi perusahaan
                company_id.name_tempo = self.pengajuan_tempo_id.name_tempo
                company_id.industry_id_tempo = self.pengajuan_tempo_id.industry_id_tempo
                company_id.street_tempo = self.pengajuan_tempo_id.street_tempo
                company_id.state_id_tempo = self.pengajuan_tempo_id.state_id_tempo
                company_id.city_id_tempo = self.pengajuan_tempo_id.city_id_tempo
                company_id.zip_tempo = self.pengajuan_tempo_id.zip_tempo
                company_id.mobile_tempo = self.pengajuan_tempo_id.mobile_tempo
                company_id.bank_name_tempo = self.pengajuan_tempo_id.bank_name_tempo
                company_id.account_name_tempo = self.pengajuan_tempo_id.account_name_tempo
                company_id.account_number_tempo = self.pengajuan_tempo_id.account_number_tempo
                company_id.website_tempo = self.pengajuan_tempo_id.website_tempo
                company_id.estimasi_tempo = self.pengajuan_tempo_id.estimasi_tempo
                company_id.tempo_duration = self.pengajuan_tempo_id.tempo_duration
                company_id.tempo_limit = self.pengajuan_tempo_id.tempo_limit
                company_id.category_produk_ids = self.pengajuan_tempo_id.category_produk_ids

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

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

                # Referensi
                company_id.supplier_ids = self.pengajuan_tempo_id.supplier_ids

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

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

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

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

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

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

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

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

                company_id.dokumen_tempat_bekerja = self.pengajuan_tempo_id.dokumen_tempat_bekerja
                if company_id.dokumen_tempat_bekerja:
                    company_id.message_post(body='Dokumen Tempat Bekerja',
                                              attachment_ids=[company_id.dokumen_tempat_bekerja.id])
                # self.user_company_id.active = True
                # user.send_company_request_approve_mail()
            else:
                new_company = self.env['res.partner'].create({
                    'name': self.user_input
                })
                # 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])
        ])