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
|
from odoo import fields, models, api
import logging
import random
from odoo.exceptions import AccessError, UserError, ValidationError
from datetime import datetime, timedelta
_logger = logging.getLogger(__name__)
class CrmLead(models.Model):
_inherit = "crm.lead"
html_description = fields.Html(string="Descriptions")
file_npwp = fields.Binary(string="Nomor Pokok Wajib Pajak")
file_nib = fields.Binary(string="Nomor Induk Berusaha")
file_tdp = fields.Binary(string="Tanda Daftar Perusahaan")
file_siup = fields.Binary(string="Surat Izin Usaha Perdagangan")
file_quotation = fields.Binary(string='Dokumen Quotation')
body_html_lead = fields.Text('Body HTML', compute='compute_body_leads')
# for wati only
wati_notification_id = fields.One2many('wati.notification', 'lead_id', string='Wati Notification')
ticket_id = fields.Char('Ticket ID', help='Ticket ID yang ada di WATI')
operator_email = fields.Char('Operator Email', help='Operator yang membalas')
operator_name = fields.Char('Operator Name', help='Operator yang membalas')
order_id = fields.Many2one('sale.order', string='Sales Order', help='Link ke sales order id')
@api.model
def create(self, vals):
rec = super(CrmLead, self).create(vals)
if rec.email_from == 'api.noreply@altama.co.id' and rec.name.startswith('INDOTEKNIK|ODOO|'):
rec.user_id = 20 # User ID: Nabila Rahmawati
if not rec.user_id:
rec.user_id = 2 # User ID: Sales
return rec
@api.onchange('user_id')
def _change_salesperson_so(self):
if self.order_id:
self.order_id.user_id = self.user_id
def revert_to_leads(self):
opportunities = self.env['crm.lead'].search([
('type', '=', 'opportunity'),
('active', '=', True),
('user_id', '=', False),
])
for opportunity in opportunities:
opportunity.type = 'lead'
@api.onchange('stage_id')
def update_stars(self):
for lead in self:
lead.priority = 0
def compute_body_leads(self):
for lead in self:
mail_message = self.env['mail.message'].search([
('res_id', '=', lead.id),
('model', '=', 'crm.lead'),
('message_type', '=', 'email')
], limit=1)
lead.body_html_lead = mail_message.body or ''
def _update_tags_leads(self):
leads = self.env['crm.lead'].search([
('active', '=', True),
('type', '=', 'lead'),
('tag_ids', '=', False),
], limit=1000)
for lead in leads:
_logger.info('processing tagged lead %s' % lead.id)
input_tags = []
if lead.order_id:
last_manufacture_id = 0
for line in lead.order_id.order_line:
tag = self.env['crm.tag'].search([('name', 'ilike', line.product_id.product_tmpl_id.x_manufacture.x_name)], limit=1)
if tag and tag.id != last_manufacture_id:
last_manufacture_id = tag.id
input_tags.append(tag.id)
input_tags.append(1509) #Website hastag
lead.tag_ids = input_tags
else:
tags = self.env['crm.tag'].search([('id', '>', 0)])
for tag in tags:
if tag.name.lower() in lead.body_html_lead.lower():
input_tags.append(tag.id)
if input_tags:
lead.tag_ids = input_tags
else:
input_tags.append(1510) #no tag
lead.tag_ids = input_tags
salesperson_id = 2
partner = lead.partner_id.parent_id or lead.partner_id
if partner.user_id and partner.user_id.id not in [25]:
salesperson_id = partner.user_id.id
lead.user_id = salesperson_id
def _update_pipeline(self, delta=48, limit=100):
# Get the current time
current_time = datetime.now()
# Calculate the time 24 hours ago
time_48_hours_ago = current_time - timedelta(hours=delta)
# Define the allowed states
allowed_states = ['sale', 'done']
# Search for sale orders with date_order greater than 24 hours ago and opportunity_id is null
orders = self.env['sale.order'].search([
('write_date', '>=', time_48_hours_ago),
('opportunity_id', '!=', False),
('state', 'in', allowed_states)
], limit=limit)
for order in orders:
order.opportunity_id.stage_id = 4
_logger.info('finish order stage pipeline %s' % order.id)
def _cancel_pipeline(self, delta=48, limit=100):
# Get the current time
current_time = datetime.now()
# Calculate the time 24 hours ago
time_48_hours_ago = current_time - timedelta(hours=delta)
# Define the allowed states
allowed_states = ['cancel']
# Search for sale orders with date_order greater than 24 hours ago and opportunity_id is null
orders = self.env['sale.order'].search([
('write_date', '>=', time_48_hours_ago),
('opportunity_id', '!=', False),
('state', 'in', allowed_states)
], limit=limit)
for order in orders:
order.opportunity_id.stage_id = 7
_logger.info('cancel order stage pipeline %s' % order.id)
def _convert_to_pipeline(self, delta=48, limit=100):
# Get the current time
current_time = datetime.now()
# Calculate the time 24 hours ago
time_48_hours_ago = current_time - timedelta(hours=delta)
# Define the allowed states
allowed_states = ['draft', 'done', 'sale', 'sent', 'cancel']
# Search for sale orders with date_order greater than 24 hours ago and opportunity_id is null
orders = self.env['sale.order'].search([
('write_date', '>=', time_48_hours_ago),
('opportunity_id', '=', False),
('state', 'in', allowed_states)
], limit=limit)
# stage
# 1 potensi baru, 2 proses quotation, 3 proses lain visit, 4 proses berhasil, 5 proses negosiasi, 7 tidak terpakai / gagal
for order in orders:
# stage_id = 2
if order.state == 'sale' or order.state == 'done':
stage_id = 4
elif order.state == 'sent':
stage_id = 5
elif order.state == 'cancel':
stage_id = 7
else:
stage_id = 2
crm_lead = self.env['crm.lead'].create([{
'email_normalized': order.email,
'name': order.name,
'user_id': order.user_id.id,
'company_id': 1,
'type': 'opportunity',
'priority': 0,
'team_id': order.team_id.id,
'stage_id': stage_id,
'expected_revenue': order.amount_untaxed,
'partner_id': order.partner_id.parent_id.id or order.partner_id.id,
'contact_name': order.partner_id.name,
'partner_name': order.partner_id.parent_id.name or order.partner_id.name,
'phone': order.partner_id.mobile,
'street': order.partner_id.street,
'street2': order.partner_id.street2,
'zip': order.partner_id.zip,
'order_id': order.id
}])
order.opportunity_id = crm_lead.id
_logger.info('convert order to opportunity %s' % crm_lead.id)
|