[FIX] crm_helpdesk: removed a default value; added a chatter method.
[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 base_status.base_state import base_state
23 from crm import crm
24 from crm import wizard
25 from osv import fields, osv
26 import tools
27 from tools.translate import _
28
29 CRM_HELPDESK_STATES = (
30     crm.AVAILABLE_STATES[2][0], # Cancelled
31     crm.AVAILABLE_STATES[3][0], # Done
32     crm.AVAILABLE_STATES[4][0], # Pending
33 )
34
35 wizard.mail_compose_message.SUPPORTED_MODELS.append('crm.helpdesk')
36
37 class crm_helpdesk(base_state, osv.osv):
38     """ Helpdesk Cases """
39
40     _name = "crm.helpdesk"
41     _description = "Helpdesk"
42     _order = "id desc"
43     _inherit = ['mail.thread']
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='Sales team to which Case belongs to.\
57                                  Define Responsible user and Email account for mail gateway.'),
58             'company_id': fields.many2one('res.company', 'Company'),
59             'date_closed': fields.datetime('Closed', readonly=True),
60             'partner_id': fields.many2one('res.partner', 'Partner'),
61             '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"),
62             'email_from': fields.char('Email', size=128, help="These people will receive email."),
63             'date': fields.datetime('Date'),
64             'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
65             'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
66             'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."),
67             'planned_revenue': fields.float('Planned Revenue'),
68             'planned_cost': fields.float('Planned Costs'),
69             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
70             'probability': fields.float('Probability (%)'),
71             'categ_id': fields.many2one('crm.case.categ', 'Category', \
72                             domain="['|',('section_id','=',False),('section_id','=',section_id),\
73                             ('object_id.model', '=', 'crm.helpdesk')]"), 
74             'duration': fields.float('Duration', states={'done': [('readonly', True)]}), 
75             'state': fields.selection(crm.AVAILABLE_STATES, 'Status', size=16, readonly=True, 
76                                   help='The state is set to \'Draft\', when a case is created.\
77                                   \nIf the case is in progress the state is set to \'Open\'.\
78                                   \nWhen the case is over, the state is set to \'Done\'.\
79                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
80             'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
81     }
82
83     _defaults = {
84         'active': lambda *a: 1,
85         'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c),
86         'partner_id': lambda s, cr, uid, c: s._get_default_partner(cr, uid, c),
87         'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c),
88         'state': lambda *a: 'draft',
89         'date': lambda *a: fields.datetime.now(),
90         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
91         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
92     }
93
94     def message_new(self, cr, uid, msg_dict, custom_values=None, context=None):
95         """Automatically called when new email message arrives"""
96         res_id = super(crm_helpdesk,self).message_new(cr, uid, msg_dict, custom_values=custom_values, context=context)
97         subject = msg_dict.get('subject')  or _("No Subject")
98         body = msg_dict.get('body_text')
99         msg_from = msg_dict.get('from')
100         vals = {
101             'name': subject,
102             'email_from': msg_from,
103             'email_cc': msg_dict.get('cc'),
104             'description': body,
105             'user_id': False,
106         }
107         vals.update(self.message_partner_by_email(cr, uid, msg_from))
108         self.write(cr, uid, [res_id], vals, context)
109         return res_id
110
111     def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
112         if isinstance(ids, (str, int, long)):
113             ids = [ids]
114
115         super(crm_helpdesk,self).message_update(cr, uid, ids, msg, context=context)
116
117         if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
118             vals['priority'] = msg.get('priority')
119
120         maps = {
121             'cost':'planned_cost',
122             'revenue': 'planned_revenue',
123             'probability':'probability'
124         }
125         vls = {}
126         for line in msg['body_text'].split('\n'):
127             line = line.strip()
128             res = tools.misc.command_re.match(line)
129             if res and maps.get(res.group(1).lower()):
130                 key = maps.get(res.group(1).lower())
131                 vls[key] = res.group(2).lower()
132         vals.update(vls)
133
134         # Unfortunately the API is based on lists
135         # but we want to update the state based on the
136         # previous state, so we have to loop:
137         for case in self.browse(cr, uid, ids, context=context):
138             values = dict(vals)
139             if case.state in CRM_HELPDESK_STATES:
140                 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
141             res = self.write(cr, uid, [case.id], values, context=context)
142         return res
143
144     # ******************************
145     # OpenChatter
146     # ******************************
147     
148         def case_get_note_msg_prefix(self, cr, uid, id, context=None):
149                 return 'Helpdesk'
150
151 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
152