[IMP] convert to opporunity on phone call fix
[odoo/odoo.git] / addons / crm_partner_assign / report / crm_partner_report.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 from openerp.osv import fields,osv
22 from openerp import tools
23
24
25 class crm_partner_report_assign(osv.osv):
26     """ CRM Lead Report """
27     _name = "crm.partner.report.assign"
28     _auto = False
29     _description = "CRM Partner Report"
30     _columns = {
31         'partner_id': fields.many2one('res.partner', 'Partner', required=False, readonly=True),
32         'grade_id':fields.many2one('res.partner.grade', 'Grade', readonly=True),
33         'activation' : fields.many2one('res.partner.activation', 'Activation', select=1),
34         'user_id':fields.many2one('res.users', 'User', readonly=True),
35         'date_review' : fields.date('Latest Partner Review'),
36         'date_partnership' : fields.date('Partnership Date'),
37         'country_id':fields.many2one('res.country', 'Country', readonly=True),
38         'section_id':fields.many2one('crm.case.section', 'Sales Team', readonly=True),
39         'opp': fields.integer('# of Opportunity', readonly=True),
40         'turnover': fields.float('Turnover', readonly=True),
41         'period_id': fields.many2one('account.period', 'Invoice Period', readonly=True),
42     }
43     def init(self, cr):
44         """
45             CRM Lead Report
46             @param cr: the current row, from the database cursor
47         """
48         tools.drop_view_if_exists(cr, 'crm_partner_report_assign')
49         cr.execute("""
50             CREATE OR REPLACE VIEW crm_partner_report_assign AS (
51                 SELECT
52                     coalesce(i.id, p.id - 1000000000) as id,
53                     p.id as partner_id,
54                     (SELECT country_id FROM res_partner a WHERE a.parent_id=p.id AND country_id is not null limit 1) as country_id,
55                     p.grade_id,
56                     p.activation,
57                     p.date_review,
58                     p.date_partnership,
59                     p.user_id,
60                     p.section_id,
61                     (SELECT count(id) FROM crm_lead WHERE partner_assigned_id=p.id) AS opp,
62                     i.price_total as turnover,
63                     i.period_id
64                 FROM
65                     res_partner p
66                     left join account_invoice_report i
67                         on (i.partner_id=p.id and i.type in ('out_invoice','out_refund') and i.state in ('open','paid'))
68             )""")
69
70 crm_partner_report_assign()
71
72 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: