5ed1c0406273dc13a3c1926f0134105e84356df1
[odoo/odoo.git] / addons / crm_helpdesk / crm_helpdesk.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.addons.base_status.base_state import base_state
23 from openerp.addons.base_status.base_stage import base_stage
24 from openerp.addons.crm import crm
25 from openerp.osv import fields, osv
26 from openerp import tools
27 from openerp.tools.translate import _
28 from openerp.tools import html2plaintext
29
30 CRM_HELPDESK_STATES = (
31     crm.AVAILABLE_STATES[2][0], # Cancelled
32     crm.AVAILABLE_STATES[3][0], # Done
33     crm.AVAILABLE_STATES[4][0], # Pending
34 )
35
36 class crm_helpdesk(base_state, base_stage, osv.osv):
37     """ Helpdesk Cases """
38
39     _name = "crm.helpdesk"
40     _description = "Helpdesk"
41     _order = "id desc"
42     _inherit = ['mail.thread']
43
44     _columns = {
45             'id': fields.integer('ID', readonly=True),
46             'name': fields.char('Name', size=128, required=True),
47             'active': fields.boolean('Active', required=False),
48             'date_action_last': fields.datetime('Last Action', readonly=1),
49             'date_action_next': fields.datetime('Next Action', readonly=1),
50             'description': fields.text('Description'),
51             'create_date': fields.datetime('Creation Date' , readonly=True),
52             'write_date': fields.datetime('Update Date' , readonly=True),
53             'date_deadline': fields.date('Deadline'),
54             'user_id': fields.many2one('res.users', 'Responsible'),
55             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
56                             select=True, help='Responsible sales team. Define Responsible user and Email account for mail gateway.'),
57             'company_id': fields.many2one('res.company', 'Company'),
58             'date_closed': fields.datetime('Closed', readonly=True),
59             'partner_id': fields.many2one('res.partner', 'Partner'),
60             'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
61             'email_from': fields.char('Email', size=128, help="Destination email for email gateway"),
62             'date': fields.datetime('Date'),
63             'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
64             'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
65             'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."),
66             'planned_revenue': fields.float('Planned Revenue'),
67             'planned_cost': fields.float('Planned Costs'),
68             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
69             'probability': fields.float('Probability (%)'),
70             'categ_id': fields.many2one('crm.case.categ', 'Category', \
71                             domain="['|',('section_id','=',False),('section_id','=',section_id),\
72                             ('object_id.model', '=', 'crm.helpdesk')]"),
73             'duration': fields.float('Duration', states={'done': [('readonly', True)]}),
74             'state': fields.selection(crm.AVAILABLE_STATES, 'Status', size=16, readonly=True,
75                                   help='The status is set to \'Draft\', when a case is created.\
76                                   \nIf the case is in progress the status is set to \'Open\'.\
77                                   \nWhen the case is over, the status is set to \'Done\'.\
78                                   \nIf the case needs to be reviewed then the status is set to \'Pending\'.'),
79     }
80
81     _defaults = {
82         'active': lambda *a: 1,
83         'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c),
84         'partner_id': lambda s, cr, uid, c: s._get_default_partner(cr, uid, c),
85         'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c),
86         'state': lambda *a: 'draft',
87         'date': lambda *a: fields.datetime.now(),
88         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
89         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
90     }
91
92     # -------------------------------------------------------
93     # Mail gateway
94     # -------------------------------------------------------
95
96     def message_new(self, cr, uid, msg, custom_values=None, context=None):
97         """ Overrides mail_thread message_new that is called by the mailgateway
98             through message_process.
99             This override updates the document according to the email.
100         """
101         if custom_values is None: custom_values = {}
102         desc = html2plaintext(msg.get('body')) if msg.get('body') else ''
103         defaults = {
104             'name': msg.get('subject') or _("No Subject"),
105             'description': desc,
106             'email_from': msg.get('from'),
107             'email_cc': msg.get('cc'),
108             'user_id': False,
109         }
110         defaults.update(custom_values)
111         return super(crm_helpdesk,self).message_new(cr, uid, msg, custom_values=defaults, context=context)
112
113     def message_update(self, cr, uid, ids, msg, update_vals=None, context=None):
114         """ Overrides mail_thread message_update that is called by the mailgateway
115             through message_process.
116             This method updates the document according to the email.
117         """
118         if isinstance(ids, (str, int, long)):
119             ids = [ids]
120         if update_vals is None: update_vals = {}
121
122         if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
123             update_vals['priority'] = msg.get('priority')
124
125         maps = {
126             'cost':'planned_cost',
127             'revenue': 'planned_revenue',
128             'probability':'probability'
129         }
130         for line in msg['body'].split('\n'):
131             line = line.strip()
132             res = tools.command_re.match(line)
133             if res and maps.get(res.group(1).lower()):
134                 key = maps.get(res.group(1).lower())
135                 update_vals[key] = res.group(2).lower()
136
137         return super(crm_helpdesk,self).message_update(cr, uid, ids, msg, update_vals=update_vals, context=context)
138
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: