summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/wati.py
blob: fbe350a5e85719146d395c2ce6dd2c5de826096f (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
from odoo import fields, models, api
import logging
import json

_logger = logging.getLogger(__name__)


class WatiNotification(models.Model):
    _name = 'wati.notification'

    json_raw = fields.Char(string='JSON Raw Text')
    is_lead = fields.Boolean(string='To Leads', help='apakah sudah ter-convert jadi leads')

    def _convert_to_leads(self):
        query = [
            ('is_lead', '=', False)
        ]
        watis = self.env['wati.notification'].search(query, order='id')

        for wati in watis:
            _logger.info('Convert Lead ID %s' % wati.id)
            ticket_id = json.loads(wati.json_raw)['ticketId']
            text = json.loads(wati.json_raw)['text']

            current_lead = self.env['crm.lead'].search([('ticket_id', '=', ticket_id)], limit=1)
            operator_email = json.loads(wati.json_raw)['operatorEmail']
            operator_name = json.loads(wati.json_raw)['operatorName']

            if current_lead:
                # must append internal notes as a reply here
                current_lead.description = str(current_lead.description) + "|" +str(text)
                current_lead.operator_email = operator_email
                current_lead.operator_name = operator_name
                wati.is_lead = True
            else:
                # create new leads
                contact_name = json.loads(wati.json_raw)['senderName']
                phone = json.loads(wati.json_raw)['waId']
                name = 'Ada pesan dari WATI '+str(phone)+' '+str(contact_name)

                self.env['crm.lead'].create([{
                    'name': name,
                    'ticket_id': ticket_id,
                    'operator_email': operator_email,
                    'operator_name': operator_name,
                    'contact_name': contact_name,
                    'phone': phone,
                    'description': text
                }])
                wati.is_lead = True