[ADD,IMP] sale,crm:
[odoo/odoo.git] / addons / crm / res_partner.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import fields,osv
23 from tools.translate import _
24
25 class res_partner(osv.osv):
26     """ Inherits partner and adds CRM information in the partner form """
27     _inherit = 'res.partner'
28     
29     def _total_oppo(self, cr, uid, ids, field_name, arg, context=None):
30         total_oppo={}
31         oppo_pool=self.pool.get('crm.lead')
32         for id in ids:
33             oppo_ids = oppo_pool.search(cr, uid, [('partner_id', '=', id)])
34             total_oppo[id] = len(oppo_ids)
35         return total_oppo
36
37     def _total_meeting(self, cr, uid, ids, field_name, arg, context=None):
38         total_meeting={}
39         meeting_pool=self.pool.get('crm.meeting')
40         for id in ids:
41             meeting_ids = meeting_pool.search(cr, uid, [('partner_id', '=', id)])
42             total_meeting[id] = len(meeting_ids)
43         return total_meeting
44     
45     
46     _columns = {
47         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
48         'opportunity_ids': fields.one2many('crm.lead', 'partner_id',\
49             'Leads and Opportunities'),
50         'meeting_ids': fields.one2many('crm.meeting', 'partner_id',\
51             'Meetings'),
52         'phonecall_ids': fields.one2many('crm.phonecall', 'partner_id',\
53             'Phonecalls'),
54         'total_oppo': fields.function(_total_oppo , type='integer',string="Total Opportunity"),
55         'total_meeting': fields.function(_total_meeting , type='integer',string="Total Meeting"),
56     }
57
58     _defaults = {
59         'total_oppo': 0,
60         'total_meeting': 0,
61     }
62
63     def redirect_partner_form(self, cr, uid, partner_id, context=None):
64         search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'view_res_partner_filter')
65         value = {
66             'domain': "[]",
67             'view_type': 'form',
68             'view_mode': 'form,tree',
69             'res_model': 'res.partner',
70             'res_id': int(partner_id),
71             'view_id': False,
72             'context': context,
73             'type': 'ir.actions.act_window',
74             'search_view_id': search_view and search_view[1] or False
75         }
76         return value
77
78     def get_opportunity(self, cr, uid, ids, context=None):
79         if context is None:
80             context = {}
81         models_data = self.pool.get('ir.model.data')
82
83         form_view = models_data.get_object_reference(cr, uid, 'crm', 'crm_case_form_view_oppor')
84         tree_view = models_data.get_object_reference(cr, uid, 'crm', 'crm_case_tree_view_oppor')
85         search_view = models_data.get_object_reference(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
86         partner_id = self.browse(cr, uid, ids[0], context=context)
87         domain =[('partner_id', '=', partner_id.id)]
88
89         return {
90                 'name': _('Opportunity'),
91                 'view_type': 'form',
92                 'view_mode': 'tree, form',
93                 'res_model': 'crm.lead',
94                 'domain': domain,
95                 'view_id': False,
96                 'views': [(tree_view and tree_view[1] or False, 'tree'),
97                           (form_view and form_view[1] or False, 'form'),
98                           (False, 'calendar'), (False, 'graph')],
99                 'type': 'ir.actions.act_window',
100                 'search_view_id': search_view and search_view[1] or False,
101                 'nodestroy': True,
102         }
103
104     def make_opportunity(self, cr, uid, ids, opportunity_summary, planned_revenue=0.0, probability=0.0, partner_id=None, context=None):
105         categ_obj = self.pool.get('crm.case.categ')
106         categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
107         lead_obj = self.pool.get('crm.lead')
108         opportunity_ids = {}
109         for partner in self.browse(cr, uid, ids, context=context):
110             if not partner_id:
111                 partner_id = partner.id
112             opportunity_id = lead_obj.create(cr, uid, {
113                 'name' : opportunity_summary,
114                 'planned_revenue' : planned_revenue,
115                 'probability' : probability,
116                 'partner_id' : partner_id,
117                 'categ_id' : categ_ids and categ_ids[0] or '',
118                 'state' :'draft',
119                 'type': 'opportunity'
120             }, context=context)
121             opportunity_ids[partner_id] = opportunity_id
122         return opportunity_ids
123 res_partner()
124
125
126 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: