[FIX]crm: several calls to _get_id for external id 'categ_phone2' which were not...
[odoo/odoo.git] / addons / crm / crm_phonecall.py
index facd0ba..22582f3 100644 (file)
 #
 ##############################################################################
 
-from base_status.base_state import base_state
+from openerp.addons.base_status.base_state import base_state
 import crm
 from datetime import datetime
-from osv import fields, osv
-import time
-from tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP
-from tools.translate import _
+from openerp.osv import fields, osv
+from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
+from openerp.tools.translate import _
 
 class crm_phonecall(base_state, osv.osv):
     """ Model for CRM phonecalls """
     _name = "crm.phonecall"
     _description = "Phonecall"
     _order = "id desc"
-    _inherit = ['ir.needaction_mixin', 'mail.thread']
+    _inherit = ['mail.thread']
     _columns = {
         # base_state required fields
         'date_action_last': fields.datetime('Last Action', readonly=1),
@@ -41,7 +40,7 @@ class crm_phonecall(base_state, osv.osv):
         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
                         select=True, help='Sales team to which Case belongs to.'),
         'user_id': fields.many2one('res.users', 'Responsible'),
-        'partner_id': fields.many2one('res.partner', 'Partner'),
+        'partner_id': fields.many2one('res.partner', 'Contact'),
         'company_id': fields.many2one('res.company', 'Company'),
         'description': fields.text('Description'),
         'state': fields.selection([ ('draft', 'Draft'),
@@ -49,11 +48,11 @@ class crm_phonecall(base_state, osv.osv):
                                     ('pending', 'Not Held'),
                                     ('cancel', 'Cancelled'),
                                     ('done', 'Held'),],
-                        string='Status', size=16, readonly=True,
-                        help='The state is set to \'Todo\', when a case is created.\
-                                If the case is in progress the state is set to \'Open\'.\
-                                When the call is over, the state is set to \'Held\'.\
-                                If the call needs to be done then the state is set to \'Not Held\'.'),
+                        string='Status', size=16, readonly=True, track_visibility='onchange',
+                        help='The status is set to \'Todo\', when a case is created.\
+                                If the case is in progress the status is set to \'Open\'.\
+                                When the call is over, the status is set to \'Held\'.\
+                                If the call needs to be done then the status is set to \'Not Held\'.'),
         'email_from': fields.char('Email', size=128, help="These people will receive email."),
         'date_open': fields.datetime('Opened', readonly=True),
         # phonecall fields
@@ -81,15 +80,9 @@ class crm_phonecall(base_state, osv.osv):
         'priority': crm.AVAILABLE_PRIORITIES[2][0],
         'state':  _get_default_state,
         'user_id': lambda self,cr,uid,ctx: uid,
+        'active': 1
     }
 
-    def create(self, cr, uid, vals, context=None):
-        obj_id = super(crm_phonecall, self).create(cr, uid, vals, context)
-        for phonecall in self.browse(cr, uid, [obj_id], context=context):
-            if not phonecall.opportunity_id:
-                self.case_open_send_note(cr, uid, [obj_id], context=context)
-        return obj_id
-
     def case_close(self, cr, uid, ids, context=None):
         """ Overrides close for crm_case for setting duration """
         res = True
@@ -118,14 +111,18 @@ class crm_phonecall(base_state, osv.osv):
         model_data = self.pool.get('ir.model.data')
         phonecall_dict = {}
         if not categ_id:
-            res_id = model_data._get_id(cr, uid, 'crm', 'categ_phone2')
-            if res_id:
+            try:
+                res_id = model_data._get_id(cr, uid, 'crm', 'categ_phone2')
                 categ_id = model_data.browse(cr, uid, res_id, context=context).res_id
+            except ValueError:
+                pass
         for call in self.browse(cr, uid, ids, context=context):
             if not section_id:
                 section_id = call.section_id and call.section_id.id or False
             if not user_id:
                 user_id = call.user_id and call.user_id.id or False
+            if not schedule_time:
+                schedule_time = call.date
             vals = {
                     'name' : call_summary,
                     'user_id' : user_id or False,
@@ -154,6 +151,18 @@ class crm_phonecall(base_state, osv.osv):
         })
         return partner_id
 
+    def on_change_opportunity(self, cr, uid, ids, opportunity_id, context=None):
+        values = {}
+        if opportunity_id:
+            opportunity = self.pool.get('crm.lead').browse(cr, uid, opportunity_id, context=context)
+            values = {
+                'section_id' : opportunity.section_id and opportunity.section_id.id or False,
+                'partner_phone' : opportunity.phone,
+                'partner_mobile' : opportunity.mobile,
+                'partner_id' : opportunity.partner_id and opportunity.partner_id.id or False,
+            }
+        return {'value' : values}
+
     def _call_set_partner(self, cr, uid, ids, partner_id, context=None):
         write_res = self.write(cr, uid, ids, {'partner_id' : partner_id}, context=context)
         self._call_set_partner_send_note(cr, uid, ids, context)
@@ -167,20 +176,26 @@ class crm_phonecall(base_state, osv.osv):
                     'phone': phonecall.partner_phone,
         })
 
-    def convert_partner(self, cr, uid, ids, action='create', partner_id=False, context=None):
+    def handle_partner_assignation(self, cr, uid, ids, action='create', partner_id=False, context=None):
         """
-        This function convert partner based on action.
+        Handle partner assignation during a lead conversion.
         if action is 'create', create new partner with contact and assign lead to new partner_id.
         otherwise assign lead to specified partner_id
+
+        :param list ids: phonecalls ids to process
+        :param string action: what has to be done regarding partners (create it, assign an existing one, or nothing)
+        :param int partner_id: partner to assign if any
+        :return dict: dictionary organized as followed: {lead_id: partner_assigned_id}
         """
-        if context is None:
-            context = {}
+        #TODO this is a duplication of the handle_partner_assignation method of crm_lead
         partner_ids = {}
+        # If a partner_id is given, force this partner for all elements
+        force_partner_id = partner_id
         for call 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':
-               if not partner_id:
-                   partner_id = self._call_create_partner(cr, uid, call, context=context)
-               self._call_create_partner_address(cr, uid, call, partner_id, context=context)
+                partner_id = force_partner_id or self._call_create_partner(cr, uid, call, context=context)
+                self._call_create_partner_address(cr, uid, call, partner_id, context=context)
             self._call_set_partner(cr, uid, [call.id], partner_id, context=context)
             partner_ids[call.id] = partner_id
         return partner_ids
@@ -204,7 +219,6 @@ class crm_phonecall(base_state, osv.osv):
         }
         return value
 
-
     def convert_opportunity(self, cr, uid, ids, opportunity_summary=False, partner_id=False, planned_revenue=0.0, probability=0.0, context=None):
         partner = self.pool.get('res.partner')
         opportunity = self.pool.get('crm.lead')
@@ -241,8 +255,9 @@ class crm_phonecall(base_state, osv.osv):
         return opportunity_dict
 
     def action_make_meeting(self, cr, uid, ids, context=None):
-        """ This opens Meeting's calendar view to schedule meeting on current Phonecall
-            @return : Dictionary value for created Meeting view
+        """
+        Open meeting's calendar view to schedule a meeting on current phonecall.
+        :return dict: dictionary value for created meeting view
         """
         phonecall = 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)
@@ -255,36 +270,25 @@ class crm_phonecall(base_state, osv.osv):
             'default_name': phonecall.name,
         }
         return res
-    
+
+    def action_button_convert2opportunity(self, cr, uid, ids, context=None):
+        """
+        Convert a phonecall into an opp and then redirect to the opp view.
+
+        :param list ids: list of calls ids to convert (typically contains a single id)
+        :return dict: containing view information
+        """
+        if len(ids) != 1:
+            raise osv.except_osv(_('Warning!'),_('It\'s only possible to convert one phonecall at a time.'))
+
+        opportunity_dict = self.convert_opportunity(cr, uid, ids, context=context)
+        return self.pool.get('crm.lead').redirect_opportunity_view(cr, uid, opportunity_dict[ids[0]], context)
+
     # ----------------------------------------
     # OpenChatter
     # ----------------------------------------
 
-    def case_get_note_msg_prefix(self, cr, uid, id, context=None):
-        return 'Phonecall'
-    
-    def case_reset_send_note(self, cr, uid, ids, context=None):
-        message = _('Phonecall has been <b>reset and set as open</b>.')
-        return self.message_append_note(cr, uid, ids, body=message, context=context)
-
-    def case_open_send_note(self, cr, uid, ids, context=None):
-        lead_obj = self.pool.get('crm.lead')
-        for phonecall in self.browse(cr, uid, ids, context=context):
-            phonecall.message_subscribe([phonecall.user_id.id], context=context)
-            if phonecall.opportunity_id:
-                lead = phonecall.opportunity_id
-                # convert datetime field to a datetime, using server format, then
-                # convert it to the user TZ and re-render it with %Z to add the timezone
-                phonecall_datetime = fields.DT.datetime.strptime(phonecall.date, DEFAULT_SERVER_DATETIME_FORMAT)
-                phonecall_date_str = fields.datetime.context_timestamp(cr, uid, phonecall_datetime, context=context).strftime(DATETIME_FORMATS_MAP['%+'] + " (%Z)")
-                message = _("Phonecall linked to the opportunity <em>%s</em> has been <b>created</b> and <b>scheduled</b> on <em>%s</em>.") % (lead.name, phonecall_date_str)
-            else:
-                message = _("Phonecall has been <b>created and opened</b>.")
-            phonecall.message_append_note(body=message)
-        return True
-
     def _call_set_partner_send_note(self, cr, uid, ids, context=None):
-        return self.message_append_note(cr, uid, ids, body=_("Partner has been <b>created</b>"), context=context)
-
+        return self.message_post(cr, uid, ids, body=_("Partner has been <b>created</b>."), context=context)
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: