summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/res_users.py
blob: b0864f2cabdd0758ef9dcefcc6f3d5e452683012 (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
from odoo import models, fields
from datetime import datetime
from pytz import UTC
import random, string


class ResUsers(models.Model):
    _inherit = 'res.users'

    reset_password_token = fields.Char(string="Reset Password Token")
    activation_token = fields.Char(string="Activation Token")
    otp_code = fields.Char(string='OTP Code')
    otp_create_date = fields.Datetime(string='OTP Create Date')
    payment_terms_id = fields.Many2one('account.payment.term', related='partner_id.property_payment_term_id', string='Payment Terms')


    def _generate_otp(self):
        for user in self:
            user.otp_code = '{:04d}'.format(random.randint(0, 9999))
            user.otp_create_date = fields.Datetime.now()
            
    def _generate_activation_token(self):
        for user in self:
            token_source = string.ascii_letters + string.digits
            user.activation_token = ''.join(random.choice(token_source) for i in range(21))
            
    def send_activation_mail(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_activation_request')
        for user in self:
            user._generate_otp()
            user._generate_activation_token()
            template.send_mail(user.id, force_send=True)

    def send_company_request_mail(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_request')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def send_company_request_mail_switch(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_request_switch')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def send_company_request_approve_mail(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_request_approve')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def send_company_switch_approve_mail(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_switch_approve')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def send_company_request_reject_mail(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_request_reject')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def send_company_request_tempo_review(self):
        template = self.env.ref('indoteknik_custom.mail_template_res_user_company_tempo_review')
        for user in self:
            template.send_mail(user.id, force_send=True)

    def get_activation_token_url(self):
        base_url = self.env['ir.config_parameter'].get_param('site.base.url')
        return f'{base_url}/register?activation=token&token={self.activation_token}'
    
    def get_voucher_code(self, type):
        if type == 'activation':
            vouchers = self.env['voucher'].get_active_voucher([('show_on_email', '=', 'user_activation')])
            if not vouchers: return None
            return ', '.join(x.code for x in vouchers)
        if type == 'switch_account':
            vouchers = self.env['voucher'].get_active_voucher([('excl_pricelist_ids', 'not in', [1]), ('apply_type', 'in', ['all', 'brand']), ('account_type', 'in', ['all', 'company']), ('visibility', 'in', ['public'])])
            if not vouchers: return None
            return ', '.join(x.code for x in vouchers)
        return None
    
    def check_access(self, model, mode):
        assert mode in ('read', 'write', 'create', 'unlink', 'import', 'export'), 'Invalid access mode'
        
        self._cr.execute("""
            SELECT MAX(CASE WHEN perm_{mode} THEN 1 ELSE 0 END)
            FROM ir_model_access a
            JOIN ir_model m ON (m.id = a.model_id)
            JOIN res_groups_users_rel gu ON (gu.gid = a.group_id)
            WHERE m.model = %s
            AND gu.uid = %s
            AND a.active IS TRUE
        """.format(mode=mode), (model, self._uid,))
        r = self._cr.fetchone()[0]
        
        return bool(r)