-
Notifications
You must be signed in to change notification settings - Fork 1
/
crm_intervention.py
195 lines (176 loc) · 10.8 KB
/
crm_intervention.py
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
192
193
194
195
# -*- coding: utf-8 -*-
##############################################################################
#
# crm_intervention module for OpenERP, Managing intervention in CRM
# Copyright (C) 2011 SYLEAM Info Services (<http://www.Syleam.fr/>)
# Sebastien LANGE <[email protected]>
# Sylvain GARANCHER <[email protected]>
#
# This file is a part of crm_intervention
#
# crm_intervention is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# crm_intervention is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.addons.crm import crm_stage
from openerp import models, api, fields
from datetime import timedelta
class crm_intervention(models.Model):
_name = 'crm.intervention'
_inherit = ['mail.thread', 'ir.needaction_mixin']
_description = 'Intervention'
_order = 'id desc'
name = fields.Char(size=128, required=True, states={'done': [('readonly', True)]})
active = fields.Boolean(default=True)
date_action_last = fields.Datetime(string='Last Action', readonly=True)
date_action_next = fields.Datetime(string='Next Action', readonly=True)
description = fields.Text(states={'done': [('readonly', True)]}, copy='')
user_id = fields.Many2one('res.users', string='Responsible', required=True, default=lambda self: self.env.user, states={'done': [('readonly', True)]})
section_id = fields.Many2one(
'crm.team', string='Interventions Team',
default=lambda self: self.env['crm.team'].search([('code', '=', 'inter')]),
states={'done': [('readonly', True)]},
help='Interventions team to which Case belongs to. Define Responsible user and Email account for mail gateway.',
)
company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env['res.company']._company_default_get('crm.intervention'))
date_closed = fields.Datetime(string='Closed on', readonly=True)
email_from = fields.Char(string='Email', size=128, states={'done': [('readonly', True)]}, help='These people will receive email.')
priority = fields.Selection(crm_stage.AVAILABLE_PRIORITIES, default='2', states={'done': [('readonly', True)]})
categ_id = fields.Many2one('crm.lead.tag', string='Category', copy=False, domain="[('team_id', '=', section_id)]", states={'done': [('readonly', True)]})
number_request = fields.Char(string='Number Request', size=64, default=lambda self: self.env['ir.sequence'].next_by_code('crm.intervention'), states={'done': [('readonly', True)]}, copy=False)
customer_information = fields.Text(string='Customer_information', states={'done': [('readonly', True)]})
intervention_todo = fields.Text(string='Intervention to do', states={'done': [('readonly', True)]}, help='Indicate the description of this intervention to do')
date_planned_start = fields.Datetime(string='Planned Start Date', states={'done': [('readonly', True)]}, help='Indicate the date of begin intervention planned')
date_planned_end = fields.Datetime(string='Planned End Date', states={'done': [('readonly', True)]}, help='Indicate the date of end intervention planned')
date_effective_start = fields.Datetime(string='Effective start date', copy=False, states={'done': [('readonly', True)]}, help='Indicate the date of begin intervention')
date_effective_end = fields.Datetime(string='Effective end date', copy=False, states={'done': [('readonly', True)]}, help='Indicate the date of end intervention')
duration_planned = fields.Float(string='Planned duration', states={'done': [('readonly', True)]}, help='Indicate estimated to do the intervention.')
duration_effective = fields.Float(string='Effective duration', copy=0.0, states={'done': [('readonly', True)]}, help='Indicate real time to do the intervention.')
partner_id = fields.Many2one('res.partner', string='Customer', domain="[('parent_id', '=', False)]", required=True, states={'done': [('readonly', True)]}, change_default=True, index=True)
partner_invoice_id = fields.Many2one(
'res.partner', string='Invoice Address',
domain="[('parent_id', '!=', False), ('parent_id', '=', partner_id)]",
required=True, states={'done': [('readonly', True)]},
# default=lambda self: self.env.context.get('partner_id', False) and self.env['res.partner'].address_get([self.env.context['partner_id']], ['invoice'])['invoice'],
help='The name and address for the invoice',
)
partner_order_id = fields.Many2one(
'res.partner', string='Intervention Contact',
domain="[('parent_id', '!=', False), ('parent_id', '=', partner_id)]",
required=True, change_default=True, states={'done': [('readonly', True)]},
# default=lambda self: self.env.context.get('partner_id', False) and self.env['res.partner'].address_get([self.env.context['partner_id']], ['contact'])['contact'],
help='The name and address of the contact that requested the intervention.',
)
partner_shipping_id = fields.Many2one(
'res.partner', string='Intervention Address',
required=True, states={'done': [('readonly', True)]},
# default=lambda self: self.env.context.get('partner_id', False) and self.env['res.partner'].address_get([self.env.context['partner_id']], ['delivery'])['delivery'],
)
partner_address_phone = fields.Char(string='Phone', states={'done': [('readonly', True)]}, size=64)
partner_address_mobile = fields.Char(string='Mobile', states={'done': [('readonly', True)]}, size=64)
state = fields.Selection(
[('draft', 'Draft'), ('open', 'Open'), ('cancel', 'Cancelled'), ('done', 'Done'), ('pending', 'Pending')],
readonly=True, default='draft',
help="""The state is set to 'Draft', when a case is created.
If the case is in progress the state is set to 'Open'.
When the case is over, the state is set to 'Done'.
If the case needs to be reviewed then the state is set to 'Pending'.""",
)
# FIXME : Change to new API when fixed
_defaults = {
'partner_invoice_id': lambda self, cr, uid, context=None: context.get('partner_id', False) and self.pool['res.partner'].address_get([context['partner_id']], ['invoice'])['invoice'],
'partner_order_id': lambda self, cr, uid, context=None: context.get('partner_id', False) and self.pool['res.partner'].address_get([context['partner_id']], ['contact'])['contact'],
'partner_shipping_id': lambda self, cr, uid, context=None: context.get('partner_id', False) and self.pool['res.partner'].address_get([context['partner_id']], ['delivery'])['delivery'],
}
@api.onchange('partner_id')
def onchange_partner_id(self):
if not self.partner_id:
return {'value': {'partner_invoice_id': False, 'partner_shipping_id': False, 'partner_order_id': False, 'email_from': False, 'partner_address_phone': False, 'partner_address_mobile': False}}
address = self.partner_id.address_get(['default', 'delivery', 'invoice', 'contact'])
self.partner_invoice_id = address['invoice']
self.partner_order_id = address['contact']
self.partner_shipping_id = address['delivery']
delivery_address = self.env['res.partner'].browse([address['contact']])
self.email_from = delivery_address.email
self.partner_address_phone = delivery_address.phone
self.partner_address_mobile = delivery_address.mobile
@api.onchange('partner_order_id')
def onchange_partner_order_id(self):
if not self.partner_order_id:
return {'value': {'email_from': False, 'partner_address_phone': False, 'partner_address_mobile': False}}
self.email_from = self.partner_order_id.email
self.partner_address_phone = self.partner_order_id.phone
self.partner_address_mobile = self.partner_order_id.mobile
@api.onchange('duration_planned')
def onchange_planned_duration(self):
if not self.duration_planned:
return {'value': {'date_planned_end': False}}
start_date = fields.Datetime.from_string(self.date_planned_start)
self.date_planned_end = fields.Datetime.to_string(start_date + timedelta(hours=self.duration_planned))
@api.onchange('date_planned_end')
def onchange_planned_end_date(self):
if not self.date_planned_start or not self.date_planned_end:
return
start_date = fields.Datetime.from_string(self.date_planned_start)
end_date = fields.Datetime.from_string(self.date_planned_end)
difference = end_date - start_date
minutes, secondes = divmod(difference.seconds, 60)
hours, minutes = divmod(minutes, 60)
self.duration_planned = float(difference.days * 24) + float(hours) + float(minutes) / float(60)
@api.onchange('duration_effective')
def onchange_effective_duration(self):
if not self.duration_effective:
return {'value': {'date_effective_end': False}}
start_date = fields.Datetime.from_string(self.date_effective_start)
self.date_effective_end = fields.Datetime.to_string(start_date + timedelta(hours=self.duration_effective))
@api.onchange('date_effective_end')
def onchange_effective_end_date(self):
if not self.date_effective_start or not self.date_effective_end:
return
start_date = fields.Datetime.from_string(self.date_effective_start)
end_date = fields.Datetime.from_string(self.date_effective_end)
difference = end_date - start_date
minutes, secondes = divmod(difference.seconds, 60)
hours, minutes = divmod(minutes, 60)
self.duration_effective = float(difference.days * 24) + float(hours) + float(minutes) / float(60)
@api.one
def set_cancel(self):
"""
Set the state to Cancel
"""
self.state = 'cancel'
@api.one
def set_open(self):
"""
Set the state to Open
"""
self.state = 'open'
@api.one
def set_pending(self):
"""
Set the state to Pending
"""
self.state = 'pending'
@api.one
def set_done(self):
"""
Set the state to done
"""
self.state = 'done'
@api.one
def set_draft(self):
"""
Set the state to draft
"""
self.state = 'draft'
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: