[IMP] crm*: update crm modules to new mail module (wip)
[odoo/odoo.git] / addons / crm_partner_assign / wizard / crm_forward_to_partner.py
index 0075d5c..3d64ab4 100644 (file)
@@ -30,10 +30,10 @@ from tools.translate import _
 class crm_lead_forward_to_partner(osv.osv_memory):
     """Forwards lead history"""
     _name = 'crm.lead.forward.to.partner'
-    _inherit = "crm.send.mail"
+    _inherit = "mail.compose.message"
 
     _columns = {
-        'name': fields.selection([('user', 'User'), ('partner', 'Partner'), \
+        'send_to': fields.selection([('user', 'User'), ('partner', 'Partner'), \
                          ('email', 'Email Address')], 'Send to', required=True),
         'user_id': fields.many2one('res.users', "User"),
         'partner_id' : fields.many2one('res.partner', 'Partner'),
@@ -42,7 +42,7 @@ class crm_lead_forward_to_partner(osv.osv_memory):
     }
 
     _defaults = {
-        'name' : 'email',
+        'send_to' : 'email',
         'history': 'latest',
         'email_from': lambda self, cr, uid, *a: self.pool.get('res.users')._get_email_from(cr, uid, uid)[uid],
     }
@@ -70,13 +70,13 @@ class crm_lead_forward_to_partner(osv.osv_memory):
         @param hist_id: Id of latest history
         @param context: A standard dictionary for contextual values
         """
-        log_pool = self.pool.get('mailgate.message')
+        log_pool = self.pool.get('mail.message')
         hist = log_pool.browse(cr, uid, hist_id, context=context)
         header = '-------- Original Message --------'
         sender = 'From: %s' %(hist.email_from or '')
         to = 'To: %s' % (hist.email_to or '')
         sentdate = 'Date: %s' % (hist.date or '')
-        desc = '\n%s'%(hist.description)
+        desc = '\n%s'%(hist.body_text)
         original = [header, sender, to, sentdate, desc]
         original = '\n'.join(original)
         return original
@@ -111,7 +111,7 @@ class crm_lead_forward_to_partner(osv.osv_memory):
         res_id = context.get('active_id')
         msg_val = self._get_case_history(cr, uid, history_type, res_id, context=context)
         if msg_val:
-            res = {'value': {'body' : '\n\n' + msg_val}}
+            res = {'value': {'body_text' : '\n\n' + msg_val}}
         return res
 
     def _get_case_history(self, cr, uid, history_type, res_id, context=None):
@@ -127,12 +127,12 @@ class crm_lead_forward_to_partner(osv.osv_memory):
 
         elif history_type == 'whole':
             log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
-            log_ids = map(lambda x: x.id, filter(lambda x: x.history, log_ids))
+            log_ids = map(lambda x: x.id, filter(lambda x: x.email_from, log_ids))
             msg_val = case_info + '\n\n' + self.get_whole_history(cr, uid, log_ids, context=context)
 
         elif history_type == 'latest':
             log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
-            log_ids = filter(lambda x: x.history and x.id, log_ids)
+            log_ids = filter(lambda x: x.email_from and x.id, log_ids)
             if not log_ids:
                 msg_val = case_info
             else:
@@ -172,6 +172,13 @@ class crm_lead_forward_to_partner(osv.osv_memory):
             email = self.pool.get('res.partner.address').browse(cr, uid, address_id).email
         return {'value': {'email_to' : email}}
 
+    def send_mail(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        super(crm_lead_forward_to_partner, self).send_mail(cr, uid, ids, context=context)
+        self.action_forward(cr, uid, ids, context)
+        return {'type': 'ir.actions.act_window_close'}
+
     def action_forward(self, cr, uid, ids, context=None):
         """
         Forward the lead to a partner
@@ -182,15 +189,13 @@ class crm_lead_forward_to_partner(osv.osv_memory):
         case_pool = self.pool.get(context.get('active_model'))
         res_id = context and context.get('active_id', False) or False
         case = case_pool.browse(cr, uid, res_id, context=context)
-
         context.update({'mail': 'forward'})
-        super(crm_lead_forward_to_partner, self).action_send(cr, uid, ids, context=context)
 
         to_write = {'date_assign': time.strftime('%Y-%m-%d')}
-        if (this.name == 'partner' and this.partner_id):
+        if (this.send_to == 'partner' and this.partner_id):
             to_write['partner_assigned_id'] = this.partner_id.id
 
-        if this.name == 'user':
+        if this.send_to == 'user':
             to_write.update({'user_id' : this.user_id.id})
         email_re = r'([^ ,<@]+@[^> ,]+)'
         email_cc = re.findall(email_re, case.email_cc or '')
@@ -204,24 +209,10 @@ class crm_lead_forward_to_partner(osv.osv_memory):
                 new_cc.append(to)
         to_write.update({'email_cc' : ', '.join(new_cc) })
         case_pool.write(cr, uid, case.id, to_write, context=context)
-
         return {'type': 'ir.actions.act_window_close'}
 
     def get_lead_details(self, cr, uid, lead_id, context=None):
-        body = ["""Dear,
-
-Below is possibly an interesting lead for you.
-
-OpenERP Leads are now forwarded to our trusted partners, through our OpenERP CRM.
-We hope that this one will provide you an interesting project, as they've recently contacted us showing interest in our software.
-
-Please keep your account manager informed about the advancements of this lead or if you are not able to answer to its requests by replying to this email. This way, we can keep track of closed leads or forward them to other partners.
-
-Please don't forget to propose our OpenERP Publisher's Warranty at the beginning of your implementation projects, together with your services quotation. The Warranty will provide unlimited bugfixing that will avoid you waste time on bugs detected during the implementation. It also provides free migration services for the current stable version at the time of signature; otherwise if we released a new version during your implementation, the customer would not always be able to easily migrate to newer versions. Please find all related information via http://www.openerp.com/services/pricing
-
-Kind regards, OpenERP Team
-
-            """, "\n\n"]
+        body = []
         lead_proxy = self.pool.get('crm.lead')
         lead = lead_proxy.browse(cr, uid, lead_id, context=context)
         if not lead.type or lead.type == 'lead' or not lead.partner_address_id:
@@ -250,7 +241,7 @@ Kind regards, OpenERP Team
                     body.append("%s: %s" % (field_definition.string, value or ''))
         elif lead.type == 'opportunity':
             pa = lead.partner_address_id
-            body = [
+            body += [
                 "Partner: %s" % (lead.partner_id and lead.partner_id.name_get()[0][1]),
                 "Contact: %s" % (pa.name or ''),
                 "Title: %s" % (pa.title or ''),
@@ -274,6 +265,7 @@ Kind regards, OpenERP Team
         """
         This function gets default values
         """
+
         if context is None:
             context = {}
 
@@ -283,15 +275,52 @@ Kind regards, OpenERP Team
             return defaults
 
         lead_proxy = self.pool.get('crm.lead')
+        partner_obj = self.pool.get('res.partner')
         lead = lead_proxy.browse(cr, uid, active_id, context=context)
 
+        email_cc = ''
+        email = ''
+        if lead.partner_assigned_id:
+            partner = partner_obj.browse(cr, uid, [lead.partner_assigned_id.id])
+            user_id = partner and partner[0].user_id or False
+            email_cc = user_id and user_id.user_email or ''
+
+            addr = partner_obj.address_get(cr, uid, [partner[0].id], ['contact'])
+            email = self.pool.get('res.partner.address').browse(cr, uid, addr['contact']).email
+
         body = self._get_case_history(cr, uid, defaults.get('history', 'latest'), lead.id, context=context)
         defaults.update({
-            'subject' : '%s: %s' % (_('Fwd'), lead.name),
-            'body' : body,
+            'subject' : '%s: %s - %s' % (_('Fwd'), 'Openerp lead forward', lead.name),
+            'body_text' : body,
+            'email_cc' : email_cc,
+            'email_to' : email or 'dummy@dummy.ly'
         })
         return defaults
 
-crm_lead_forward_to_partner()
+class crm_lead_mass_forward_to_partner(osv.osv_memory):
+    _name = 'crm.lead.mass.forward.to.partner'
+    _inherit = 'crm.lead.forward.to.partner'
+
+    def action_mass_forward(self, cr, uid, ids, context=None):
+        if not context:
+            context = {}
+
+        active_ids = context.get('active_ids')
+        case_obj = self.pool.get('crm.lead')
+        for case in case_obj.browse(cr, uid, active_ids, context=context):
+            if not case.partner_assigned_id:
+                case_obj.assign_partner(cr,uid, [case.id], context=context)
+                case = case_obj.browse(cr, uid, case.id, context=context)
+
+            if not case.partner_assigned_id:
+                continue
+
+            context.update({'active_id' : case.id})
+            value = self.default_get(cr, uid, ['body_text', 'email_to', 'email_cc', 'subject', 'history'], context=context)
+            self.write(cr, uid, ids, value, context=context)
+            self.action_forward(cr,uid, ids, context=context)
+
+        return {'type': 'ir.actions.act_window_close'}
+
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: