1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import fields, osv
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
34 class crm_helpdesk(crm.crm_case, osv.osv):
35 """ Helpdesk Cases """
37 _name = "crm.helpdesk"
38 _description = "Helpdesk"
40 _inherit = ['mailgate.thread']
42 'id': fields.integer('ID', readonly=True),
43 'name': fields.char('Name', size=128, required=True),
44 'active': fields.boolean('Active', required=False),
45 'date_action_last': fields.datetime('Last Action', readonly=1),
46 'date_action_next': fields.datetime('Next Action', readonly=1),
47 'description': fields.text('Description'),
48 'create_date': fields.datetime('Creation Date' , readonly=True),
49 'write_date': fields.datetime('Update Date' , readonly=True),
50 'date_deadline': fields.date('Deadline'),
51 'user_id': fields.many2one('res.users', 'Responsible'),
52 'section_id': fields.many2one('crm.case.section', 'Sales Team', \
53 select=True, help='Sales team to which Case belongs to.\
54 Define Responsible user and Email account for mail gateway.'),
55 'company_id': fields.many2one('res.company', 'Company'),
56 'date_closed': fields.datetime('Closed', readonly=True),
57 'partner_id': fields.many2one('res.partner', 'Partner'),
58 'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
59 domain="[('partner_id','=',partner_id)]"),
60 '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"),
61 'email_from': fields.char('Email', size=128, help="These people will receive email."),
62 'date': fields.datetime('Date'),
63 'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
64 'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
65 'canal_id': fields.many2one('res.partner.canal', 'Channel', \
66 help="The channels represent the different communication \
67 modes available with the customer."),
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('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
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],
97 def message_new(self, cr, uid, msg, context=None):
99 Automatically calls when new email message arrives
101 @param self: The object pointer
102 @param cr: the current row, from the database cursor,
103 @param uid: the current user’s ID for security checks
105 mailgate_pool = self.pool.get('email.server.tools')
107 subject = msg.get('subject')
108 body = msg.get('body')
109 msg_from = msg.get('from')
110 priority = msg.get('priority')
114 'email_from': msg_from,
115 'email_cc': msg.get('cc'),
119 if msg.get('priority', False):
120 vals['priority'] = priority
122 res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
126 res = self.create(cr, uid, vals, context)
127 attachents = msg.get('attachments', [])
128 for attactment in attachents or []:
131 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
132 'datas_fname': attactment,
133 'description': 'Mail attachment',
134 'res_model': self._name,
137 self.pool.get('ir.attachment').create(cr, uid, data_attach)
141 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
143 @param self: The object pointer
144 @param cr: the current row, from the database cursor,
145 @param uid: the current user’s ID for security checks,
146 @param ids: List of update mail’s IDs
148 if isinstance(ids, (str, int, long)):
151 if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
152 vals['priority'] = msg.get('priority')
155 'cost':'planned_cost',
156 'revenue': 'planned_revenue',
157 'probability':'probability'
160 for line in msg['body'].split('\n'):
162 res = tools.misc.command_re.match(line)
163 if res and maps.get(res.group(1).lower()):
164 key = maps.get(res.group(1).lower())
165 vls[key] = res.group(2).lower()
168 # Unfortunately the API is based on lists
169 # but we want to update the state based on the
170 # previous state, so we have to loop:
171 for case in self.browse(cr, uid, ids, context=context):
173 if case.state in CRM_HELPDESK_STATES:
174 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
175 res = self.write(cr, uid, [case.id], values, context=context)
178 def msg_send(self, cr, uid, id, *args, **argv):
181 @param self: The object pointer
182 @param cr: the current row, from the database cursor,
183 @param uid: the current user’s ID for security checks,
184 @param ids: List of email’s IDs
185 @param *args: Return Tuple Value
186 @param **args: Return Dictionary of Keyword Value
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: