1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields,osv
24 class res_partner(osv.osv):
25 """ Inherits partner and adds CRM information in the partner form """
26 _inherit = 'res.partner'
28 def _opportunity_meeting_count(self, cr, uid, ids, field_name, arg, context=None):
29 res = dict(map(lambda x: (x,{'opportunity_count': 0, 'meeting_count': 0}), ids))
30 # the user may not have access rights for opportunities or meetings
32 for partner in self.browse(cr, uid, ids, context):
34 'opportunity_count': len(partner.opportunity_ids),
35 'meeting_count': len(partner.meeting_ids),
42 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
43 'opportunity_ids': fields.one2many('crm.lead', 'partner_id',\
44 'Leads and Opportunities', domain=[('state','in', ('draft','open','pending'))]),
45 'meeting_ids': fields.many2many('crm.meeting', 'crm_meeting_partner_rel','partner_id', 'meeting_id',
47 'phonecall_ids': fields.one2many('crm.phonecall', 'partner_id',\
49 'opportunity_count': fields.function(_opportunity_meeting_count, string="Opportunity", type='integer', multi='opp_meet'),
50 'meeting_count': fields.function(_opportunity_meeting_count, string="# Meetings", type='integer', multi='opp_meet'),
53 def copy(self, cr, uid, record_id, default=None, context=None):
57 default.update({'opportunity_ids': [], 'meeting_ids' : [], 'phonecall_ids' : []})
59 return super(res_partner, self).copy(cr, uid, record_id, default, context)
61 def redirect_partner_form(self, cr, uid, partner_id, context=None):
62 search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'view_res_partner_filter')
66 'view_mode': 'form,tree',
67 'res_model': 'res.partner',
68 'res_id': int(partner_id),
71 'type': 'ir.actions.act_window',
72 'search_view_id': search_view and search_view[1] or False
76 def make_opportunity(self, cr, uid, ids, opportunity_summary, planned_revenue=0.0, probability=0.0, partner_id=None, context=None):
77 categ_obj = self.pool.get('crm.case.categ')
78 categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
79 lead_obj = self.pool.get('crm.lead')
81 for partner in self.browse(cr, uid, ids, context=context):
83 partner_id = partner.id
84 opportunity_id = lead_obj.create(cr, uid, {
85 'name' : opportunity_summary,
86 'planned_revenue' : planned_revenue,
87 'probability' : probability,
88 'partner_id' : partner_id,
89 'categ_ids' : categ_ids and categ_ids[0:1] or [],
93 opportunity_ids[partner_id] = opportunity_id
94 return opportunity_ids
97 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: