[IMP]sale,crm,stock : Remove comments and improve kanaban view link with action
[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 _opportunity_count(self, cr, uid, ids, field_name, arg, context=None):
30         count = dict.fromkeys(ids, 0)
31         opportunity_pool=self.pool.get('crm.lead')
32         opportunity_ids = opportunity_pool.search(cr, uid, [('partner_id', 'in', ids)])
33         for opportunity in opportunity_pool.browse(cr, uid, opportunity_ids):
34             count[opportunity.partner_id.id] += 1
35         return count
36
37     def _meeting_count(self, cr, uid, ids, field_name, arg, context=None):
38         count = dict.fromkeys(ids, 0)
39         meeting_pool=self.pool.get('crm.meeting')
40         meeting_ids = meeting_pool.search(cr, uid, [('partner_id', 'in', ids)])
41         for meeting in meeting_pool.browse(cr, uid, meeting_ids):
42             count[meeting.partner_id.id] += 1
43         return count
44
45     _columns = {
46         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
47         'opportunity_ids': fields.one2many('crm.lead', 'partner_id',\
48             'Leads and Opportunities'),
49         'meeting_ids': fields.one2many('crm.meeting', 'partner_id',\
50             'Meetings'),
51         'phonecall_ids': fields.one2many('crm.phonecall', 'partner_id',\
52             'Phonecalls'),
53         'opportunity_count': fields.function(_opportunity_count , type='integer',string="Opportunity"),
54         'meeting_count': fields.function(_meeting_count , type='integer',string="Meeting"),
55     }
56
57     _defaults = {
58         'opportunity_count': 0,
59         'meeting_count': 0,
60     }
61
62     def redirect_partner_form(self, cr, uid, partner_id, context=None):
63         search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'view_res_partner_filter')
64         value = {
65             'domain': "[]",
66             'view_type': 'form',
67             'view_mode': 'form,tree',
68             'res_model': 'res.partner',
69             'res_id': int(partner_id),
70             'view_id': False,
71             'context': context,
72             'type': 'ir.actions.act_window',
73             'search_view_id': search_view and search_view[1] or False
74         }
75         return value
76
77     def make_opportunity(self, cr, uid, ids, opportunity_summary, planned_revenue=0.0, probability=0.0, partner_id=None, context=None):
78         categ_obj = self.pool.get('crm.case.categ')
79         categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
80         lead_obj = self.pool.get('crm.lead')
81         opportunity_ids = {}
82         for partner in self.browse(cr, uid, ids, context=context):
83             if not partner_id:
84                 partner_id = partner.id
85             opportunity_id = lead_obj.create(cr, uid, {
86                 'name' : opportunity_summary,
87                 'planned_revenue' : planned_revenue,
88                 'probability' : probability,
89                 'partner_id' : partner_id,
90                 'categ_id' : categ_ids and categ_ids[0] or '',
91                 'state' :'draft',
92                 'type': 'opportunity'
93             }, context=context)
94             opportunity_ids[partner_id] = opportunity_id
95         return opportunity_ids
96 res_partner()
97
98
99 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: