1e69a73c5fac6ed0caf84cd6bdf616aadf1f7414
[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 osv import fields,osv
22 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     }
42
43     def init(self, cr):
44
45         """
46             CRM Lead Report
47             @param cr: the current row, from the database cursor
48         """
49         tools.drop_view_if_exists(cr, 'crm_partner_report_assign')
50         cr.execute("""
51             CREATE OR REPLACE VIEW crm_partner_report_assign AS (
52                 SELECT
53                     p.id,
54                     p.id as partner_id,
55                     (SELECT country_id FROM res_partner_address a WHERE a.partner_id=p.id AND country_id is not null limit 1) as country_id,
56                     p.grade_id,
57                     p.activation,
58                     p.date_review,
59                     p.date_partnership,
60                     p.user_id,
61                     p.section_id,
62                     (SELECT count(id) FROM crm_lead WHERE partner_assigned_id=p.id) AS opp,
63                     (SELECT
64                         sum(price_total)
65                      FROM
66                         account_invoice_report
67                      WHERE
68                         partner_id=p.id and
69                         type in ('out_invoice','out_refund') and
70                         state in ('open','paid')) AS turnover
71                 FROM
72                     res_partner p
73
74             )""")
75
76 crm_partner_report_assign()
77
78 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: