[IMP] point_of_sale: pos config view improvement
[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', 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             'team_id': fields.many2one('crm.team', 'Sales Team', oldname='section_id',\
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.tracking.medium', 'Channel', help="Communication channel."),
60             'planned_revenue': fields.float('Planned Revenue'),
61             'planned_cost': fields.float('Planned Costs'),
62             'priority': fields.selection([('0','Low'), ('1','Normal'), ('2','High')], 'Priority'),
63             'probability': fields.float('Probability (%)'),
64             'categ_id': fields.many2one('crm.helpdesk.category', 'Category'),
65             'duration': fields.float('Duration', states={'done': [('readonly', True)]}),
66             'state': fields.selection(
67                 [('draft', 'New'),
68                  ('open', 'In Progress'),
69                  ('pending', 'Pending'),
70                  ('done', 'Closed'),
71                  ('cancel', 'Cancelled')], 'Status', readonly=True, track_visibility='onchange',
72                                   help='The status is set to \'Draft\', when a case is created.\
73                                   \nIf the case is in progress the status is set to \'Open\'.\
74                                   \nWhen the case is over, the status is set to \'Done\'.\
75                                   \nIf the case needs to be reviewed then the status is set to \'Pending\'.'),
76     }
77
78     _defaults = {
79         'active': lambda *a: 1,
80         'user_id': lambda s, cr, uid, c: uid,
81         'state': lambda *a: 'draft',
82         'date': fields.datetime.now,
83         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
84         'priority': '1',
85     }
86
87     def on_change_partner_id(self, cr, uid, ids, partner_id, context=None):
88         values = {}
89         if partner_id:
90             partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
91             values = {
92                 'email_from': partner.email,
93             }
94         return {'value': values}
95
96     def write(self, cr, uid, ids, values, context=None):
97         """ Override to add case management: open/close dates """
98         if values.get('state'):
99             if values.get('state') in ['draft', 'open'] and not values.get('date_open'):
100                 values['date_open'] = fields.datetime.now()
101             elif values.get('state') == 'close' and not values.get('date_closed'):
102                 values['date_closed'] = fields.datetime.now()
103         return super(crm_helpdesk, self).write(cr, uid, ids, values, context=context)
104
105     def case_escalate(self, cr, uid, ids, context=None):
106         """ Escalates case to parent level """
107         data = {'active': True}
108         for case in self.browse(cr, uid, ids, context=context):
109             if case.team_id and case.team_id.parent_id:
110                 parent_id = case.team_id.parent_id
111                 data['team_id'] = parent_id.id
112                 if parent_id.change_responsible and parent_id.user_id:
113                     data['user_id'] = parent_id.user_id.id
114             else:
115                 raise osv.except_osv(_('Error!'), _('You can not escalate, you are already at the top level regarding your sales-team category.'))
116             self.write(cr, uid, [case.id], data, context=context)
117         return True
118
119     # -------------------------------------------------------
120     # Mail gateway
121     # -------------------------------------------------------
122
123     def message_new(self, cr, uid, msg, custom_values=None, context=None):
124         """ Overrides mail_thread message_new that is called by the mailgateway
125             through message_process.
126             This override updates the document according to the email.
127         """
128         if custom_values is None:
129             custom_values = {}
130         desc = html2plaintext(msg.get('body')) if msg.get('body') else ''
131         defaults = {
132             'name': msg.get('subject') or _("No Subject"),
133             'description': desc,
134             'email_from': msg.get('from'),
135             'email_cc': msg.get('cc'),
136             'user_id': False,
137             'partner_id': msg.get('author_id', False),
138         }
139         defaults.update(custom_values)
140         return super(crm_helpdesk, self).message_new(cr, uid, msg, custom_values=defaults, context=context)
141
142 class crm_helpdesk_category(osv.Model):
143     _name = "crm.helpdesk.category"
144     _description = "Helpdesk Category"
145     _columns = {
146         'name': fields.char('Name', required=True, translate=True),
147         'team_id': fields.many2one('crm.team', 'Sales Team'),
148     }
149
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: