[IMP]: Improvements in crm classes for _inherits(mailgate.thread),
[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 time
23 import re
24 import os
25 import base64
26 import tools
27 import mx.DateTime
28
29 from tools.translate import _
30 from osv import fields
31 from osv import osv
32 from osv import orm
33 from osv.orm import except_orm
34
35 import crm
36
37 class base_action_rule(osv.osv):
38     """ Base Action Rule """
39     _inherit = 'base.action.rule'
40     _description = 'Action Rules'
41     
42     _columns = {
43         'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'),
44         'trg_max_history': fields.integer('Maximum Communication History'),
45         'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'),
46         'regex_history' : fields.char('Regular Expression on Case History', size=128),
47         'act_section_id': fields.many2one('crm.case.section', 'Set Team to'),
48         'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
49         'act_mail_to_partner': fields.boolean('Mail to partner',help="Check \
50 this if you want the rule to send an email to the partner."),
51     }
52     
53
54     def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from',False), context={}):
55         body = self.format_mail(obj, body)
56         if not emailfrom:
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
59             
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
64         else:
65             reply_to = emailfrom
66         if not emailfrom:
67             raise osv.except_osv(_('Error!'),
68                     _("No E-Mail ID Found for your Company address!"))
69         return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id))
70     
71     def do_check(self, cr, uid, action, obj, context={}):
72
73         """ @param self: The object pointer
74         @param cr: the current row, from the database cursor,
75         @param uid: the current user’s ID for security checks,
76         @param context: A standard dictionary for contextual values"""
77
78         ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
79
80         if hasattr(obj, 'section_id'):
81             ok = ok and (not action.trg_section_id or action.trg_section_id.id==obj.section_id.id)
82         if hasattr(obj, 'categ_id'):
83             ok = ok and (not action.trg_categ_id or action.trg_categ_id.id==obj.categ_id.id)
84
85 #    TODO: history_line is removed
86 #        if hasattr(obj, 'history_line'):
87 #            ok = ok and (not action.trg_max_history or action.trg_max_history<=(len(obj.history_line)+1))
88 #            reg_history = action.regex_history
89 #            result_history = True
90 #            if reg_history:
91 #                ptrn = re.compile(str(reg_history))
92 #                if obj.history_line:
93 #                    _result = ptrn.search(str(obj.history_line[0].description))
94 #                    if not _result:
95 #                        result_history = False
96             regex_h = not reg_history or result_history
97             ok = ok and regex_h
98         return ok
99
100     def do_action(self, cr, uid, action, model_obj, obj, context={}):
101
102         """ @param self: The object pointer
103         @param cr: the current row, from the database cursor,
104         @param uid: the current user’s ID for security checks,
105         @param context: A standard dictionary for contextual values """
106
107         res = super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
108         write = {}
109         
110         if hasattr(action, 'act_section_id') and action.act_section_id:
111             obj.section_id = action.act_section_id
112             write['section_id'] = action.act_section_id.id
113
114         if hasattr(obj, 'email_cc') and action.act_email_cc:
115             if '@' in (obj.email_cc or ''):
116                 emails = obj.email_cc.split(",")
117                 if  obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
118                     write['email_cc'] = obj.email_cc+','+obj.act_email_cc
119             else:
120                 write['email_cc'] = obj.act_email_cc
121
122         model_obj.write(cr, uid, [obj.id], write, context)
123         emails = []
124
125         if hasattr(obj, 'email_from') and action.act_mail_to_partner:
126             emails.append(obj.email_from)
127         emails = filter(None, emails)
128         if len(emails) and action.act_mail_body:
129             emails = list(set(emails))
130             self.email_send(cr, uid, obj, emails, action.act_mail_body)
131         return True
132
133
134     def state_get(self, cr, uid, context={}):
135
136         """@param self: The object pointer
137         @param cr: the current row, from the database cursor,
138         @param uid: the current user’s ID for security checks,
139         @param context: A standard dictionary for contextual values """
140
141         res = super(base_action_rule, self).state_get(cr, uid, context=context)
142         return res + [('escalate','Escalate')] + crm.AVAILABLE_STATES
143
144     def priority_get(self, cr, uid, context={}):
145
146         """@param self: The object pointer
147         @param cr: the current row, from the database cursor,
148         @param uid: the current user’s ID for security checks,
149         @param context: A standard dictionary for contextual values """
150
151         res = super(base_action_rule, self).priority_get(cr, uid, context=context)
152         return res + crm.AVAILABLE_PRIORITIES
153
154
155 base_action_rule()
156
157 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: