[IMP] sale order line invisible type
[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 osv import fields, osv
25 import tools
26 from tools.translate import _
27
28 CRM_HELPDESK_STATES = (
29     crm.AVAILABLE_STATES[2][0], # Cancelled
30     crm.AVAILABLE_STATES[3][0], # Done
31     crm.AVAILABLE_STATES[4][0], # Pending
32 )
33
34 class crm_helpdesk(base_state, osv.osv):
35     """ Helpdesk Cases """
36
37     _name = "crm.helpdesk"
38     _description = "Helpdesk"
39     _order = "id desc"
40     _inherit = ['mail.thread']
41     _mail_compose_message = True
42     _columns = {
43             'id': fields.integer('ID', readonly=True),
44             'name': fields.char('Name', size=128, required=True),
45             'active': fields.boolean('Active', required=False),
46             'date_action_last': fields.datetime('Last Action', readonly=1),
47             'date_action_next': fields.datetime('Next Action', readonly=1),
48             'description': fields.text('Description'),
49             'create_date': fields.datetime('Creation Date' , readonly=True),
50             'write_date': fields.datetime('Update Date' , readonly=True),
51             'date_deadline': fields.date('Deadline'),
52             'user_id': fields.many2one('res.users', 'Responsible'),
53             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
54                             select=True, help='Sales team to which Case belongs to.\
55                                  Define Responsible user and Email account for mail gateway.'),
56             'company_id': fields.many2one('res.company', 'Company'),
57             'date_closed': fields.datetime('Closed', readonly=True),
58             'partner_id': fields.many2one('res.partner', 'Partner'),
59             '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"),
60             'email_from': fields.char('Email', size=128, help="These people will receive email."),
61             'date': fields.datetime('Date'),
62             'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
63             'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
64             'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."),
65             'planned_revenue': fields.float('Planned Revenue'),
66             'planned_cost': fields.float('Planned Costs'),
67             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
68             'probability': fields.float('Probability (%)'),
69             'categ_id': fields.many2one('crm.case.categ', 'Category', \
70                             domain="['|',('section_id','=',False),('section_id','=',section_id),\
71                             ('object_id.model', '=', 'crm.helpdesk')]"), 
72             'duration': fields.float('Duration', states={'done': [('readonly', True)]}), 
73             'state': fields.selection(crm.AVAILABLE_STATES, 'Status', size=16, readonly=True, 
74                                   help='The state is set to \'Draft\', when a case is created.\
75                                   \nIf the case is in progress the state is set to \'Open\'.\
76                                   \nWhen the case is over, the state is set to \'Done\'.\
77                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
78     }
79
80     _defaults = {
81         'active': lambda *a: 1,
82         'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c),
83         'partner_id': lambda s, cr, uid, c: s._get_default_partner(cr, uid, c),
84         'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c),
85         'state': lambda *a: 'draft',
86         'date': lambda *a: fields.datetime.now(),
87         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
88         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
89     }
90
91     def create(self, cr, uid, vals, context=None):
92         obj_id = super(crm_helpdesk, self).create(cr, uid, vals, context)
93         self.create_send_note(cr, uid, [obj_id], context=context)
94         return obj_id
95
96     # -------------------------------------------------------
97     # Mail gateway
98     # -------------------------------------------------------
99
100     def message_new(self, cr, uid, msg, custom_values=None, context=None):
101         """ Overrides mail_thread message_new that is called by the mailgateway
102             through message_process.
103             This override updates the document according to the email.
104         """
105         if custom_values is None: custom_values = {}
106         custom_values.update({
107             'name': msg.get('subject') or _("No Subject"),
108             'description': msg.get('body_text'),
109             'email_from': msg.get('from'),
110             'email_cc': msg.get('cc'),
111             'user_id': False,
112         })
113         custom_values.update(self.message_partner_by_email(cr, uid, msg.get('from'), context=context))
114         return super(crm_helpdesk,self).message_new(cr, uid, msg, custom_values=custom_values, context=context)
115
116     def message_update(self, cr, uid, ids, msg, update_vals=None, context=None):
117         """ Overrides mail_thread message_update that is called by the mailgateway
118             through message_process.
119             This method updates the document according to the email.
120         """
121         if isinstance(ids, (str, int, long)):
122             ids = [ids]
123         if update_vals is None: update_vals = {}
124
125         if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
126             update_vals['priority'] = msg.get('priority')
127
128         maps = {
129             'cost':'planned_cost',
130             'revenue': 'planned_revenue',
131             'probability':'probability'
132         }
133         for line in msg['body_text'].split('\n'):
134             line = line.strip()
135             res = tools.misc.command_re.match(line)
136             if res and maps.get(res.group(1).lower()):
137                 key = maps.get(res.group(1).lower())
138                 update_vals[key] = res.group(2).lower()
139
140         return super(crm_helpdesk,self).message_update(cr, uid, ids, msg, update_vals=update_vals, context=context)
141
142     # ******************************
143     # OpenChatter
144     # ******************************
145
146     def case_get_note_msg_prefix(self, cr, uid, id, context=None):
147         """ override of default base_state method. """
148         return 'Case'
149
150     def create_send_note(self, cr, uid, ids, context=None):
151         msg = _('Case has been <b>created</b>.')
152         self.message_append_note(cr, uid, ids, body=msg, context=context)
153         return True
154
155
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: