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 ##############################################################################
28 from tools.translate import _
29 from osv import fields
32 from osv.orm import except_orm
36 class base_action_rule(osv.osv):
37 """ Base Action Rule """
38 _inherit = 'base.action.rule'
39 _description = 'Action Rules'
42 'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'),
43 'trg_max_history': fields.integer('Maximum Communication History'),
44 'trg_categ_id': fields.many2one('crm.case.categ', 'Category'),
45 'regex_history' : fields.char('Regular Expression on Case History', size=128),
46 'act_section_id': fields.many2one('crm.case.section', 'Set Team to'),
47 'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
48 'act_mail_to_partner': fields.boolean('Mail to Partner', help="Check \
49 this if you want the rule to send an email to the partner."),
53 def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
54 email_message_obj = self.pool.get('email.message')
55 body = self.format_mail(obj, body)
57 if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.address_id and obj.user_id.address_id.email:
58 emailfrom = obj.user_id.address_id.email
60 name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
61 emailfrom = tools.ustr(emailfrom)
62 if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.reply_to:
63 reply_to = obj.section_id.reply_to
67 raise osv.except_osv(_('Error!'),
68 _("No E-Mail ID Found for your Company address!"))
69 return email_message_obj.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model='base.action.rule', reply_to=reply_to, openobject_id=str(obj.id))
71 def do_check(self, cr, uid, action, obj, context=None):
72 """ @param self: The object pointer
73 @param cr: the current row, from the database cursor,
74 @param uid: the current user’s ID for security checks,
75 @param context: A standard dictionary for contextual values"""
76 ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
78 if hasattr(obj, 'section_id'):
79 ok = ok and (not action.trg_section_id or action.trg_section_id.id==obj.section_id.id)
80 if hasattr(obj, 'categ_id'):
81 ok = ok and (not action.trg_categ_id or action.trg_categ_id.id==obj.categ_id.id)
84 regex = action.regex_history
88 ptrn = re.compile(str(regex))
89 for history in obj.message_ids:
90 _result = ptrn.search(str(history.name))
95 ok = ok and (not regex or result_history)
98 if action.trg_max_history:
100 history_ids = filter(lambda x: x.history, obj.message_ids)
101 if len(history_ids) <= action.trg_max_history:
103 ok = ok and res_count
106 def do_action(self, cr, uid, action, model_obj, obj, context=None):
107 """ @param self: The object pointer
108 @param cr: the current row, from the database cursor,
109 @param uid: the current user’s ID for security checks,
110 @param context: A standard dictionary for contextual values """
111 res = super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
114 if hasattr(action, 'act_section_id') and action.act_section_id:
115 obj.section_id = action.act_section_id
116 write['section_id'] = action.act_section_id.id
118 if hasattr(obj, 'email_cc') and action.act_email_cc:
119 if '@' in (obj.email_cc or ''):
120 emails = obj.email_cc.split(",")
121 if obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
122 write['email_cc'] = obj.email_cc+','+obj.act_email_cc
124 write['email_cc'] = obj.act_email_cc
126 # Put state change by rule in communication history
127 if hasattr(obj, 'state') and action.act_state:
128 model_obj.history(cr, uid, [obj], _(action.act_state))
130 model_obj.write(cr, uid, [obj.id], write, context)
133 if hasattr(obj, 'email_from') and action.act_mail_to_partner:
134 emails.append(obj.email_from)
135 emails = filter(None, emails)
136 if len(emails) and action.act_mail_body:
137 emails = list(set(emails))
138 self.email_send(cr, uid, obj, emails, action.act_mail_body)
142 def state_get(self, cr, uid, context=None):
143 """Gets available states for crm
144 @param self: The object pointer
145 @param cr: the current row, from the database cursor,
146 @param uid: the current user’s ID for security checks,
147 @param context: A standard dictionary for contextual values """
148 res = super(base_action_rule, self).state_get(cr, uid, context=context)
149 return res + crm.AVAILABLE_STATES
151 def priority_get(self, cr, uid, context=None):
152 """@param self: The object pointer
153 @param cr: the current row, from the database cursor,
154 @param uid: the current user’s ID for security checks,
155 @param context: A standard dictionary for contextual values """
156 res = super(base_action_rule, self).priority_get(cr, uid, context=context)
157 return res + crm.AVAILABLE_PRIORITIES
161 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: