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 ##############################################################################
25 from tools.translate import _
26 from tools import ustr
27 from osv import fields
32 class base_action_rule(osv.osv):
33 """ Base Action Rule """
34 _inherit = 'base.action.rule'
35 _description = 'Action Rules'
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."),
48 def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
49 mail_message = self.pool.get('mail.message')
50 body = self.format_mail(obj, body)
52 if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.user_email:
53 emailfrom = obj.user_id.user_email
55 name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
56 emailfrom = tools.ustr(emailfrom)
57 if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.reply_to:
58 reply_to = obj.section_id.reply_to
62 raise osv.except_osv(_('Error!'), _("No Email Found for your Company address!"))
63 return mail_message.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model=obj._name, reply_to=reply_to, res_id=obj.id)
65 def do_check(self, cr, uid, action, obj, context=None):
66 ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
68 if hasattr(obj, 'section_id'):
69 ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id)
70 if hasattr(obj, 'categ_id'):
71 ok = ok and (not action.trg_categ_id or action.trg_categ_id.id == obj.categ_id.id)
74 regex = action.regex_history
77 ptrn = re.compile(ustr(regex))
78 for history in obj.message_ids:
79 _result = ptrn.search(ustr(history.subject))
85 if action.trg_max_history:
87 history_ids = filter(lambda x: x.email_from, obj.message_ids)
88 if len(history_ids) <= action.trg_max_history:
93 def do_action(self, cr, uid, action, model_obj, obj, context=None):
95 if hasattr(action, 'act_section_id') and action.act_section_id:
96 obj.section_id = action.act_section_id
97 write['section_id'] = action.act_section_id.id
99 if hasattr(obj, 'email_cc') and action.act_email_cc:
100 if '@' in (obj.email_cc or ''):
101 emails = obj.email_cc.split(",")
102 if obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
103 write['email_cc'] = obj.email_cc + ',' + obj.act_email_cc
105 write['email_cc'] = obj.act_email_cc
107 # Put state change by rule in communication history
108 if hasattr(obj, 'state') and hasattr(obj, 'message_append') and action.act_state:
109 model_obj.message_append(cr, uid, [obj], _(action.act_state))
111 model_obj.write(cr, uid, [obj.id], write, context)
112 super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
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)
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
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
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: