[IMP] improves the reporting view Expenses Analysis (add some measures to the table...
[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 import openerp
23 from openerp.addons.crm import crm
24 from openerp.osv import fields, osv
25 from openerp import tools
26 from openerp.tools.translate import _
27 from openerp.tools import html2plaintext
28
29
30 class crm_helpdesk(osv.osv):
31     """ Helpdesk Cases """
32
33     _name = "crm.helpdesk"
34     _description = "Helpdesk"
35     _order = "id desc"
36     _inherit = ['mail.thread']
37
38     _columns = {
39             'id': fields.integer('ID', readonly=True),
40             'name': fields.char('Name', size=128, required=True),
41             'active': fields.boolean('Active', required=False),
42             'date_action_last': fields.datetime('Last Action', readonly=1),
43             'date_action_next': fields.datetime('Next Action', readonly=1),
44             'description': fields.text('Description'),
45             'create_date': fields.datetime('Creation Date' , readonly=True),
46             'write_date': fields.datetime('Update Date' , readonly=True),
47             'date_deadline': fields.date('Deadline'),
48             'user_id': fields.many2one('res.users', 'Responsible'),
49             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
50                             select=True, help='Responsible sales team. Define Responsible user and Email account for mail gateway.'),
51             'company_id': fields.many2one('res.company', 'Company'),
52             'date_closed': fields.datetime('Closed', readonly=True),
53             'partner_id': fields.many2one('res.partner', 'Partner'),
54             '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"),
55             'email_from': fields.char('Email', size=128, help="Destination email for email gateway"),
56             'date': fields.datetime('Date'),
57             'ref': fields.reference('Reference', selection=openerp.addons.base.res.res_request.referencable_models),
58             'ref2': fields.reference('Reference 2', selection=openerp.addons.base.res.res_request.referencable_models),
59             'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."),
60             'planned_revenue': fields.float('Planned Revenue'),
61             'planned_cost': fields.float('Planned Costs'),
62             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
63             'probability': fields.float('Probability (%)'),
64             'categ_id': fields.many2one('crm.case.categ', 'Category', \
65                             domain="['|',('section_id','=',False),('section_id','=',section_id),\
66                             ('object_id.model', '=', 'crm.helpdesk')]"),
67             'duration': fields.float('Duration', states={'done': [('readonly', True)]}),
68             'state': fields.selection(
69                 [('draft', 'New'),
70                  ('open', 'In Progress'),
71                  ('pending', 'Pending'),
72                  ('done', 'Closed'),
73                  ('cancel', 'Cancelled')], 'Status', size=16, readonly=True, track_visibility='onchange',
74                                   help='The status is set to \'Draft\', when a case is created.\
75                                   \nIf the case is in progress the status is set to \'Open\'.\
76                                   \nWhen the case is over, the status is set to \'Done\'.\
77                                   \nIf the case needs to be reviewed then the status is set to \'Pending\'.'),
78     }
79
80     _defaults = {
81         'active': lambda *a: 1,
82         'user_id': lambda s, cr, uid, c: uid,
83         'state': lambda *a: 'draft',
84         'date': fields.datetime.now,
85         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
86         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
87     }
88
89     def on_change_partner_id(self, cr, uid, ids, partner_id, context=None):
90         values = {}
91         if partner_id:
92             partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
93             values = {
94                 'email_from': partner.email,
95             }
96         return {'value': values}
97
98     def write(self, cr, uid, ids, values, context=None):
99         """ Override to add case management: open/close dates """
100         if values.get('state'):
101             if values.get('state') in ['draft', 'open'] and not values.get('date_open'):
102                 values['date_open'] = fields.datetime.now()
103             elif values.get('state') == 'close' and not values.get('date_closed'):
104                 values['date_closed'] = fields.datetime.now()
105         return super(crm_helpdesk, self).write(cr, uid, ids, values, context=context)
106
107     def case_escalate(self, cr, uid, ids, context=None):
108         """ Escalates case to parent level """
109         data = {'active': True}
110         for case in self.browse(cr, uid, ids, context=context):
111             if case.section_id and case.section_id.parent_id:
112                 parent_id = case.section_id.parent_id
113                 data['section_id'] = parent_id.id
114                 if parent_id.change_responsible and parent_id.user_id:
115                     data['user_id'] = parent_id.user_id.id
116             else:
117                 raise osv.except_osv(_('Error!'), _('You can not escalate, you are already at the top level regarding your sales-team category.'))
118             self.write(cr, uid, [case.id], data, context=context)
119         return True
120
121     # -------------------------------------------------------
122     # Mail gateway
123     # -------------------------------------------------------
124
125     def message_new(self, cr, uid, msg, custom_values=None, context=None):
126         """ Overrides mail_thread message_new that is called by the mailgateway
127             through message_process.
128             This override updates the document according to the email.
129         """
130         if custom_values is None:
131             custom_values = {}
132         desc = html2plaintext(msg.get('body')) if msg.get('body') else ''
133         defaults = {
134             'name': msg.get('subject') or _("No Subject"),
135             'description': desc,
136             'email_from': msg.get('from'),
137             'email_cc': msg.get('cc'),
138             'user_id': False,
139             'partner_id': msg.get('author_id', False),
140         }
141         defaults.update(custom_values)
142         return super(crm_helpdesk, self).message_new(cr, uid, msg, custom_values=defaults, context=context)
143
144 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: