[MERGE] Sync with trunk
[odoo/odoo.git] / addons / crm / crm_action_rule.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 re
23 import tools
24
25 from tools.translate import _
26 from tools import ustr
27 from osv import fields
28 from osv import osv
29
30 import crm
31
32 class base_action_rule(osv.osv):
33     """ Base Action Rule """
34     _inherit = 'base.action.rule'
35     _description = 'Action Rules'
36
37     _columns = {
38         'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'),
39         'trg_max_history': fields.integer('Maximum Communication History'),
40         'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'),
41         'regex_history' : fields.char('Regular Expression on Case History', size=128),
42         'act_section_id': fields.many2one('crm.case.section', 'Set Team to'),
43         'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
44         'act_mail_to_partner': fields.boolean('Mail to Partner',
45                                               help="Check this if you want the rule to send an email to the partner."),
46     }
47
48     def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
49         mail_id = super(base_action_rule, self).email_send(cr, uid, obj, emails, body, emailfrom=emailfrom, context=context)
50         if mail_id and hasattr(obj, 'section_id') and obj.section_id and obj.section_id.alias_id:
51             reply_to = obj.section_id.alias_id.name_get()[0][1]
52             self.pool.get('mail.mail').write(cr, uid, [mail_id], {'reply_to': reply_to}, context=context)
53         return mail_id
54
55     def do_check(self, cr, uid, action, obj, context=None):
56         ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
57
58         if hasattr(obj, 'section_id'):
59             ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id)
60         if hasattr(obj, 'categ_id'):
61             ok = ok and (not action.trg_categ_id or action.trg_categ_id.id == obj.categ_id.id)
62
63         #Cheking for history
64         regex = action.regex_history
65         if regex:
66             res = False
67             ptrn = re.compile(ustr(regex))
68             for history in obj.message_ids:
69                 _result = ptrn.search(ustr(history.subject))
70                 if _result:
71                     res = True
72                     break
73             ok = ok and res
74
75         if action.trg_max_history:
76             res_count = False
77             history_ids = filter(lambda x: x.email_from, obj.message_ids)
78             if len(history_ids) <= action.trg_max_history:
79                 res_count = True
80             ok = ok and res_count
81         return ok
82
83     def do_action(self, cr, uid, action, model_obj, obj, context=None):
84         write = {}
85         if hasattr(action, 'act_section_id') and action.act_section_id:
86             obj.section_id = action.act_section_id
87             write['section_id'] = action.act_section_id.id
88
89         if hasattr(obj, 'email_cc') and action.act_email_cc:
90             if '@' in (obj.email_cc or ''):
91                 emails = obj.email_cc.split(",")
92                 if  obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
93                     write['email_cc'] = obj.email_cc + ',' + obj.act_email_cc
94             else:
95                 write['email_cc'] = obj.act_email_cc
96
97         # Put state change by rule in communication history
98         if hasattr(obj, 'state') and hasattr(obj, 'message_post') and action.act_state:
99             model_obj.message_post(cr, uid, [obj], _(action.act_state), context=context)
100
101         model_obj.write(cr, uid, [obj.id], write, context)
102         super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
103         emails = []
104
105         if hasattr(obj, 'email_from') and action.act_mail_to_partner:
106             emails.append(obj.email_from)
107         emails = filter(None, emails)
108         if len(emails) and action.act_mail_body:
109             emails = list(set(emails))
110             self.email_send(cr, uid, obj, emails, action.act_mail_body)
111         return True
112
113
114     def state_get(self, cr, uid, context=None):
115         """Gets available states for crm"""
116         res = super(base_action_rule, self).state_get(cr, uid, context=context)
117         return res + crm.AVAILABLE_STATES
118
119     def priority_get(self, cr, uid, context=None):
120         res = super(base_action_rule, self).priority_get(cr, uid, context=context)
121         return res + crm.AVAILABLE_PRIORITIES
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: