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

class UserCompanyRequest(models.Model):
    _name = 'user.company.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")

    @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')
        is_company_id = vals.get('user_company_id')
        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 is_company_id:
            self.user_company_id.customer_type = is_company_id.customer_type
            self.user_company_id.npwp = is_company_id.npwp
            self.user_company_id.sppkp = is_company_id.sppkp
            self.user_company_id.nama_wajib_pajak = is_company_id.nama_wajib_pajak
            self.user_company_id.alamat_lengkap_text = is_company_id.alamat_lengkap_text
            self.user_company_id.industry_id.id = is_company_id.industry_id.id
            self.user_company_id.company_type_id.id = is_company_id.company_type_id.id
            self.user_company_id.user_id = is_company_id.user_id
            self.user_company_id.property_account_receivable_id = is_company_id.property_account_receivable_id
            self.user_company_id.property_account_payable_id = is_company_id.property_account_payable_id

        if not self.is_approve and is_approve:
            if is_approve == 'approved':
                self.user_id.parent_id = is_company_id if is_company_id else self.user_company_id.id
                self.user_id.customer_type = self.user_company_id.customer_type
                self.user_id.npwp = self.user_company_id.npwp
                self.user_id.sppkp = self.user_company_id.sppkp
                self.user_id.nama_wajib_pajak = self.user_company_id.nama_wajib_pajak
                self.user_id.alamat_lengkap_text = self.user_company_id.alamat_lengkap_text
                self.user_id.industry_id = self.user_company_id.industry_id.id
                self.user_id.company_type_id = self.user_company_id.company_type_id.id
                self.user_id.user_id = self.user_company_id.user_id
                self.user_id.property_account_receivable_id = self.user_company_id.property_account_receivable_id
                self.user_id.property_account_payable_id = self.user_company_id.property_account_payable_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(UserCompanyRequest, self).write(vals)

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