X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=addons%2Fcrm%2Fcrm_phonecall.py;h=22582f3132f14de070fbc3132c8cc2bec727f141;hb=8be0a5a8e1e624c3da22c5e13c7c7abaeeb8d710;hp=4246fa43a98fc8f4a1cfe5b4b7fc0b6e7dcfe014;hpb=bc1bb2423bbc71cabd3e8e616d49d9f1ebea02c5;p=odoo%2Fodoo.git diff --git a/addons/crm/crm_phonecall.py b/addons/crm/crm_phonecall.py index 4246fa4..22582f3 100644 --- a/addons/crm/crm_phonecall.py +++ b/addons/crm/crm_phonecall.py @@ -2,7 +2,7 @@ ############################################################################## # # OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). +# Copyright (C) 2004-today OpenERP SA () # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -19,62 +19,55 @@ # ############################################################################## -from crm import crm_base -from osv import fields, osv -from tools.translate import _ +from openerp.addons.base_status.base_state import base_state import crm -import time from datetime import datetime +from openerp.osv import fields, osv +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT +from openerp.tools.translate import _ -class crm_phonecall(crm_base, osv.osv): - """ Phonecall Cases """ - +class crm_phonecall(base_state, osv.osv): + """ Model for CRM phonecalls """ _name = "crm.phonecall" _description = "Phonecall" _order = "id desc" + _inherit = ['mail.thread'] _columns = { - # From crm.case - 'id': fields.integer('ID', readonly=True), - 'name': fields.char('Call Summary', size=64, required=True), - 'active': fields.boolean('Active', required=False), + # base_state required fields 'date_action_last': fields.datetime('Last Action', readonly=1), 'date_action_next': fields.datetime('Next Action', readonly=1), 'create_date': fields.datetime('Creation Date' , readonly=True), '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_address_id': fields.many2one('res.partner.address', 'Partner Contact', \ - domain="[('partner_id','=',partner_id)]"), + 'partner_id': fields.many2one('res.partner', 'Contact'), 'company_id': fields.many2one('res.company', 'Company'), 'description': fields.text('Description'), - 'state': fields.selection([ - ('draft', 'Draft'), - ('open', 'Todo'), - ('cancel', 'Cancelled'), - ('done', 'Held'), + 'state': fields.selection([ ('draft', 'Draft'), + ('open', 'Confirmed'), ('pending', 'Not Held'), - ], 'State', size=16, readonly=True, - help='The state is set to \'Todo\', when a case is created.\ - \nIf the case is in progress the state is set to \'Open\'.\ - \nWhen the call is over, the state is set to \'Held\'.\ - \nIf the call needs to be done then the state is set to \'Not Held\'.'), + ('cancel', 'Cancelled'), + ('done', '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 + 'name': fields.char('Call Summary', size=64, required=True), + 'active': fields.boolean('Active', required=False), 'duration': fields.float('Duration', help="Duration in Minutes"), 'categ_id': fields.many2one('crm.case.categ', 'Category', \ domain="['|',('section_id','=',section_id),('section_id','=',False),\ ('object_id.model', '=', 'crm.phonecall')]"), 'partner_phone': fields.char('Phone', size=32), - 'partner_contact': fields.related('partner_address_id', 'name', \ - type="char", string="Contact", size=128), 'partner_mobile': fields.char('Mobile', size=32), 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), - 'date_closed': fields.datetime('Closed', readonly=True), - 'date': fields.datetime('Date'), - 'opportunity_id': fields.many2one ('crm.lead', 'Lead/Opportunity'), - 'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)]), + 'date_closed': fields.datetime('Closed', readonly=True), + 'date': fields.datetime('Date'), + 'opportunity_id': fields.many2one ('crm.lead', 'Lead/Opportunity'), } def _get_default_state(self, cr, uid, context=None): @@ -83,50 +76,31 @@ class crm_phonecall(crm_base, osv.osv): return 'open' _defaults = { - 'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), - 'priority': crm.AVAILABLE_PRIORITIES[2][0], - 'state': _get_default_state, + 'date': fields.datetime.now, + 'priority': crm.AVAILABLE_PRIORITIES[2][0], + 'state': _get_default_state, 'user_id': lambda self,cr,uid,ctx: uid, - 'active': 1, + 'active': 1 } - # From crm.case - def onchange_partner_address_id(self, cr, uid, ids, add, email=False): - res = super(crm_phonecall, self).onchange_partner_address_id(cr, uid, ids, add, email) - res.setdefault('value', {}) - if add: - address = self.pool.get('res.partner.address').browse(cr, uid, add) - res['value']['partner_phone'] = address.phone - res['value']['partner_mobile'] = address.mobile - return res - - def case_close(self, cr, uid, ids, *args): - """Overrides close for crm_case for setting close date - """ + def case_close(self, cr, uid, ids, context=None): + """ Overrides close for crm_case for setting duration """ res = True - for phone in self.browse(cr, uid, ids): + for phone in self.browse(cr, uid, ids, context=context): phone_id = phone.id - data = {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')} + data = {} if phone.duration <=0: - duration = datetime.now() - datetime.strptime(phone.date, '%Y-%m-%d %H:%M:%S') - data.update({'duration': duration.seconds/float(60)}) - res = super(crm_phonecall, self).case_close(cr, uid, [phone_id], args) - self.write(cr, uid, [phone_id], data) + duration = datetime.now() - datetime.strptime(phone.date, DEFAULT_SERVER_DATETIME_FORMAT) + data['duration'] = duration.seconds/float(60) + res = super(crm_phonecall, self).case_close(cr, uid, [phone_id], context=context) + self.write(cr, uid, [phone_id], data, context=context) return res - def case_reset(self, cr, uid, ids, *args): + def case_reset(self, cr, uid, ids, context=None): """Resets case as Todo """ - res = super(crm_phonecall, self).case_reset(cr, uid, ids, args, 'crm.phonecall') - self.write(cr, uid, ids, {'duration': 0.0, 'state':'open'}) - return res - - - def case_open(self, cr, uid, ids, *args): - """Overrides cancel for crm_case for setting Open Date - """ - res = super(crm_phonecall, self).case_open(cr, uid, ids, *args) - self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')}) + res = super(crm_phonecall, self).case_reset(cr, uid, ids, context) + self.write(cr, uid, ids, {'duration': 0.0, 'state':'open'}, context=context) return res def schedule_another_phonecall(self, cr, uid, ids, schedule_time, call_summary, \ @@ -137,14 +111,18 @@ class crm_phonecall(crm_base, 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, @@ -153,14 +131,11 @@ class crm_phonecall(crm_base, osv.osv): 'date' : schedule_time, 'section_id' : section_id or False, 'partner_id': call.partner_id and call.partner_id.id or False, - 'partner_address_id': call.partner_address_id and call.partner_address_id.id or False, 'partner_phone' : call.partner_phone, 'partner_mobile' : call.partner_mobile, 'priority': call.priority, } - new_id = self.create(cr, uid, vals, context=context) - self.case_open(cr, uid, [new_id]) if action == 'log': self.case_close(cr, uid, [new_id]) phonecall_dict[call.id] = new_id @@ -176,31 +151,51 @@ class crm_phonecall(crm_base, 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): - return self.write(cr, uid, ids, {'partner_id' : partner_id}, context=context) + write_res = self.write(cr, uid, ids, {'partner_id' : partner_id}, context=context) + self._call_set_partner_send_note(cr, uid, ids, context) + return write_res def _call_create_partner_address(self, cr, uid, phonecall, partner_id, context=None): - address = self.pool.get('res.partner.address') + address = self.pool.get('res.partner') return address.create(cr, uid, { - 'partner_id': partner_id, + 'parent_id': partner_id, 'name': phonecall.name, '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 @@ -224,10 +219,8 @@ class crm_phonecall(crm_base, 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') - address = self.pool.get('res.partner.address') opportunity = self.pool.get('crm.lead') opportunity_dict = {} default_contact = False @@ -237,20 +230,19 @@ class crm_phonecall(crm_base, osv.osv): if partner_id: address_id = partner.address_get(cr, uid, [partner_id])['default'] if address_id: - default_contact = address.browse(cr, uid, address_id, context=context) + default_contact = partner.browse(cr, uid, address_id, context=context) opportunity_id = opportunity.create(cr, uid, { 'name': opportunity_summary or call.name, 'planned_revenue': planned_revenue, 'probability': probability, 'partner_id': partner_id or False, - 'partner_address_id': default_contact and default_contact.id, - 'phone': default_contact and default_contact.phone, 'mobile': default_contact and default_contact.mobile, 'section_id': call.section_id and call.section_id.id or False, 'description': call.description or False, 'priority': call.priority, - 'type': 'opportunity', + 'type': 'opportunity', 'phone': call.partner_phone or False, + 'email_from': default_contact and default_contact.email, }) vals = { 'partner_id': partner_id, @@ -260,54 +252,43 @@ class crm_phonecall(crm_base, osv.osv): self.case_close(cr, uid, [call.id]) opportunity.case_open(cr, uid, [opportunity_id]) opportunity_dict[call.id] = opportunity_id - return opportunity_dict + 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 """ - value = {} - for phonecall in self.browse(cr, uid, ids, context=context): - data_obj = self.pool.get('ir.model.data') - - # Get meeting views - result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter') - res = data_obj.read(cr, uid, result, ['res_id']) - id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet') - id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet') - id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet') - if id1: - id1 = data_obj.browse(cr, uid, id1, context=context).res_id - if id2: - id2 = data_obj.browse(cr, uid, id2, context=context).res_id - if id3: - id3 = data_obj.browse(cr, uid, id3, context=context).res_id + 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) + res['context'] = { + 'default_phonecall_id': phonecall.id, + 'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False, + 'default_user_id': uid, + 'default_email_from': phonecall.email_from, + 'default_state': 'open', + 'default_name': phonecall.name, + } + return res - context = { - 'default_phonecall_id': phonecall.id, - 'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False, - 'default_email': phonecall.email_from , - 'default_name': phonecall.name - } + def action_button_convert2opportunity(self, cr, uid, ids, context=None): + """ + Convert a phonecall into an opp and then redirect to the opp view. - value = { - 'name': _('Meetings'), - 'domain' : "[('user_id','=',%s)]" % (uid), - 'context': context, - 'view_type': 'form', - 'view_mode': 'calendar,form,tree', - 'res_model': 'crm.meeting', - 'view_id': False, - 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')], - 'type': 'ir.actions.act_window', - 'search_view_id': res['res_id'], - 'nodestroy': True - } + :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.')) - return value + 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) -crm_phonecall() + # ---------------------------------------- + # OpenChatter + # ---------------------------------------- + def _call_set_partner_send_note(self, cr, uid, ids, context=None): + return self.message_post(cr, uid, ids, body=_("Partner has been created."), context=context) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: