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