[FIX] crm: typos
[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 crm import crm
23 from osv import fields, osv
24 import time
25 from crm import wizard
26 import binascii
27 import tools
28 from tools.translate import _
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(crm.crm_case, osv.osv):
37     """ Helpdesk Cases """
38
39     _name = "crm.helpdesk"
40     _description = "Helpdesk"
41     _order = "id desc"
42     _inherit = ['mail.thread']
43     _columns = {
44             'id': fields.integer('ID', readonly=True),
45             'name': fields.char('Name', size=128, required=True),
46             'active': fields.boolean('Active', required=False),
47             'date_action_last': fields.datetime('Last Action', readonly=1),
48             'date_action_next': fields.datetime('Next Action', readonly=1),
49             'description': fields.text('Description'),
50             'create_date': fields.datetime('Creation Date' , readonly=True),
51             'write_date': fields.datetime('Update Date' , readonly=True),
52             'date_deadline': fields.date('Deadline'),
53             'user_id': fields.many2one('res.users', 'Responsible'),
54             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
55                             select=True, help='Sales team to which Case belongs to.\
56                                  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             'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
61                                  domain="[('partner_id','=',partner_id)]"),
62             '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"),
63             'email_from': fields.char('Email', size=128, help="These people will receive email."),
64             'date': fields.datetime('Date'),
65             'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
66             'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
67             'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."),
68             'planned_revenue': fields.float('Planned Revenue'),
69             'planned_cost': fields.float('Planned Costs'),
70             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
71             'probability': fields.float('Probability (%)'),
72             'categ_id': fields.many2one('crm.case.categ', 'Category', \
73                             domain="[('section_id','=',section_id),\
74                             ('object_id.model', '=', 'crm.helpdesk')]"),
75             'duration': fields.float('Duration', states={'done': [('readonly', True)]}),
76             'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
77                                   help='The state is set to \'Draft\', when a case is created.\
78                                   \nIf the case is in progress the state is set to \'Open\'.\
79                                   \nWhen the case is over, the state is set to \'Done\'.\
80                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
81             'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
82     }
83
84     _defaults = {
85         'active': lambda *a: 1,
86         'user_id': crm.crm_case._get_default_user,
87         'partner_id': crm.crm_case._get_default_partner,
88         'partner_address_id': crm.crm_case._get_default_partner_address,
89         'email_from': crm.crm_case. _get_default_email,
90         'state': lambda *a: 'draft',
91         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
92         'section_id': crm.crm_case. _get_section,
93         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
94         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
95     }
96
97     def message_new(self, cr, uid, msg_dict, custom_values=None, context=None):
98         """Automatically called when new email message arrives"""
99         res_id = super(crm_helpdesk,self).message_new(cr, uid, msg_dict, custom_values=custom_values, context=context)
100         subject = msg_dict.get('subject')  or _("No Subject")
101         body = msg_dict.get('body_text')
102         msg_from = msg_dict.get('from')
103         vals = {
104             'name': subject,
105             'email_from': msg_from,
106             'email_cc': msg_dict.get('cc'),
107             'description': body,
108             'user_id': False,
109         }
110         vals.update(self.message_partner_by_email(cr, uid, msg_dict.get('from', False)))
111         self.write(cr, uid, [res_id], vals, context)
112         return res_id
113
114     def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
115         if isinstance(ids, (str, int, long)):
116             ids = [ids]
117
118         super(crm_helpdesk,self).message_update(cr, uid, ids, msg, context=context)
119
120         if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
121             vals['priority'] = msg.get('priority')
122
123         maps = {
124             'cost':'planned_cost',
125             'revenue': 'planned_revenue',
126             'probability':'probability'
127         }
128         vls = {}
129         for line in msg['body_text'].split('\n'):
130             line = line.strip()
131             res = tools.misc.command_re.match(line)
132             if res and maps.get(res.group(1).lower()):
133                 key = maps.get(res.group(1).lower())
134                 vls[key] = res.group(2).lower()
135         vals.update(vls)
136
137         # Unfortunately the API is based on lists
138         # but we want to update the state based on the
139         # previous state, so we have to loop:
140         for case in self.browse(cr, uid, ids, context=context):
141             values = dict(vals)
142             if case.state in CRM_HELPDESK_STATES:
143                 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
144             res = self.write(cr, uid, [case.id], values, context=context)
145         return res
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
148