Forward port of branch 7.0 up to 1933e92
[odoo/odoo.git] / addons / crm / crm_lead.py
index eaf5129..a5315ab 100644 (file)
@@ -153,6 +153,11 @@ class crm_lead(format_address, osv.osv):
         return result, fold
 
     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
+        if view_type == 'form' and context and context.get('opportunity_id'):
+            # TODO: replace by get_formview_action call
+            lead_type = self.browse(cr, user, context['opportunity_id'], context=context).type
+            view_lead_xml_id = 'crm_case_form_view_oppor' if lead_type == 'opportunity' else 'crm_case_form_view_leads'
+            _, view_id = self.pool['ir.model.data'].get_object_reference(cr, user, 'crm', view_lead_xml_id)
         res = super(crm_lead, self).fields_view_get(cr, user, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
         if view_type == 'form':
             res['arch'] = self.fields_view_get_address(cr, user, res['arch'], context=context)
@@ -241,10 +246,10 @@ class crm_lead(format_address, osv.osv):
         'opt_out': fields.boolean('Opt-Out', oldname='optout',
             help="If opt-out is checked, this contact has refused to receive emails for mass mailing and marketing campaign. "
                     "Filter 'Available for Mass Mailing' allows users to filter the leads when performing mass mailing."),
-        'type':fields.selection([ ('lead','Lead'), ('opportunity','Opportunity'), ],'Type', help="Type is used to separate Leads and Opportunities"),
+        'type': fields.selection([ ('lead','Lead'), ('opportunity','Opportunity'), ],'Type', select=True, help="Type is used to separate Leads and Opportunities"),
         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority', select=True),
         'date_closed': fields.datetime('Closed', readonly=True),
-        'stage_id': fields.many2one('crm.case.stage', 'Stage', track_visibility='onchange',
+        'stage_id': fields.many2one('crm.case.stage', 'Stage', track_visibility='onchange', select=True,
                         domain="['&', ('section_ids', '=', section_id), '|', ('type', '=', type), ('type', '=', 'both')]"),
         'user_id': fields.many2one('res.users', 'Salesperson', select=True, track_visibility='onchange'),
         'referred': fields.char('Referred By', size=64),
@@ -313,7 +318,10 @@ class crm_lead(format_address, osv.osv):
         stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context=context)
         if not stage.on_change:
             return {'value': {}}
-        return {'value': {'probability': stage.probability}}
+        vals = {'probability': stage.probability}
+        if stage.probability >= 100 or (stage.probability == 0 and stage.sequence > 1):
+                vals['date_closed'] = fields.datetime.now()
+        return {'value': vals}
 
     def on_change_partner_id(self, cr, uid, ids, partner_id, context=None):
         values = {}
@@ -330,6 +338,7 @@ class crm_lead(format_address, osv.osv):
                 'phone': partner.phone,
                 'mobile': partner.mobile,
                 'fax': partner.fax,
+                'zip': partner.zip,
             }
         return {'value': values}
 
@@ -761,24 +770,6 @@ class crm_lead(format_address, osv.osv):
             )
         return partner_id
 
-    def _lead_set_partner(self, cr, uid, lead, partner_id, context=None):
-        """
-        Assign a partner to a lead.
-
-        :param object lead: browse record of the lead to process
-        :param int partner_id: identifier of the partner to assign
-        :return bool: True if the partner has properly been assigned
-        """
-        res = False
-        res_partner = self.pool.get('res.partner')
-        if partner_id:
-            res_partner.write(cr, uid, partner_id, {'section_id': lead.section_id and lead.section_id.id or False})
-            contact_id = res_partner.address_get(cr, uid, [partner_id])['default']
-            res = lead.write({'partner_id': partner_id}, context=context)
-            message = _("<b>Partner</b> set to <em>%s</em>." % (lead.partner_id.name))
-            self.message_post(cr, uid, [lead.id], body=message, context=context)
-        return res
-
     def handle_partner_assignation(self, cr, uid, ids, action='create', partner_id=False, context=None):
         """
         Handle partner assignation during a lead conversion.
@@ -792,13 +783,16 @@ class crm_lead(format_address, osv.osv):
         """
         #TODO this is a duplication of the handle_partner_assignation method of crm_phonecall
         partner_ids = {}
-        # If a partner_id is given, force this partner for all elements
-        force_partner_id = partner_id
         for lead in self.browse(cr, uid, ids, context=context):
             # If the action is set to 'create' and no partner_id is set, create a new one
-            if action == 'create':
-                partner_id = force_partner_id or self._create_lead_partner(cr, uid, lead, context)
-            self._lead_set_partner(cr, uid, lead, partner_id, context=context)
+            if lead.partner_id:
+                partner_ids[lead.id] = lead.partner_id.id
+                continue
+            if not partner_id and action == 'create':
+                partner_id = self._create_lead_partner(cr, uid, lead, context)
+                self.pool['res.partner'].write(cr, uid, partner_id, {'section_id': lead.section_id and lead.section_id.id or False})
+            if partner_id:
+                lead.write({'partner_id': partner_id}, context=context)
             partner_ids[lead.id] = partner_id
         return partner_ids
 
@@ -914,7 +908,7 @@ class crm_lead(format_address, osv.osv):
         :return dict: dictionary value for created Meeting view
         """
         opportunity = self.browse(cr, uid, ids[0], context)
-        res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'base_calendar', 'action_crm_meeting', context)
+        res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'calendar', 'action_calendar_event', context)
         res['context'] = {
             'default_opportunity_id': opportunity.id,
             'default_partner_id': opportunity.partner_id and opportunity.partner_id.id or False,
@@ -1049,11 +1043,13 @@ class crm_lead(format_address, osv.osv):
     def schedule_phonecall_send_note(self, cr, uid, ids, phonecall_id, action, context=None):
         phonecall = self.pool.get('crm.phonecall').browse(cr, uid, [phonecall_id], context=context)[0]
         if action == 'log':
-            prefix = 'Logged'
+            message = _('Logged a call for %(date)s. %(description)s')
         else:
-            prefix = 'Scheduled'
-        suffix = ' %s' % phonecall.description
-        message = _("%s a call for %s.%s") % (prefix, phonecall.date, suffix)
+            message = _('Scheduled a call for %(date)s. %(description)s')
+        phonecall_date = datetime.strptime(phonecall.date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
+        phonecall_usertime = fields.datetime.context_timestamp(cr, uid, phonecall_date, context=context).strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
+        html_time = "<time datetime='%s+00:00'>%s</time>" % (phonecall.date, phonecall_usertime)
+        message = message % dict(date=html_time, description=phonecall.description)
         return self.message_post(cr, uid, ids, body=message, context=context)
 
     def log_meeting(self, cr, uid, ids, meeting_subject, meeting_date, duration, context=None):