[FIX]: marketing_campaign: Minor fix in reporting query
[odoo/odoo.git] / addons / marketing_campaign / report / campaign_analysis.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 import tools
22 from osv import fields, osv
23
24 class campaign_analysis(osv.osv):
25     _name = "campaign.analysis"
26     _description = "Campaign Analysis"
27     _auto = False
28     _rec_name = 'date'
29     def _total_cost(self, cr, uid, ids, field_name, arg, context={}):
30         """
31             @param cr: the current row, from the database cursor,
32             @param uid: the current user’s ID for security checks,
33             @param ids: List of case and section Data’s IDs
34             @param context: A standard dictionary for contextual values
35         """
36         result = {}
37         for ca_obj in self.browse(cr, uid, ids, context):
38             wi_ids = self.pool.get('marketing.campaign.workitem').search(cr, uid,
39                         [('segment_id.campaign_id', '=', ca_obj.campaign_id.id)])
40             total_cost = ca_obj.activity_id.variable_cost + \
41                                 ((ca_obj.campaign_id.fixed_cost or 0.00) / len(wi_ids))
42             result[ca_obj.id] = total_cost
43         return result
44     _columns = {
45         'year': fields.char('Year', size=4, readonly=True),
46         'month':fields.selection([('01','January'), ('02','February'),
47                     ('03','March'), ('04','April'),('05','May'), ('06','June'),
48                     ('07','July'), ('08','August'), ('09','September'),
49                     ('10','October'), ('11','November'), ('12','December')],
50                     'Month', readonly=True),
51         'date': fields.date('Date', readonly=True),
52         'campaign_id': fields.many2one('marketing.campaign', 'Campaign',
53                                                                 readonly=True),
54         'activity_id': fields.many2one('marketing.campaign.activity', 'Activity',
55                                                                  readonly=True),
56         'segment_id': fields.many2one('marketing.campaign.segment', 'Segment',
57                                                                 readonly=True),
58         'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
59         'country_id': fields.related('partner_id','address', 'country_id',
60                     type='many2one', relation='res.country',string='Country'),
61         'total_cost' : fields.function(_total_cost, string='Cost', method=True,
62                                     type="float" ),
63         'revenue': fields.float('Revenue',digits=(16,2),readonly=True),
64         'count' : fields.integer('# of Actions', readonly=True),
65     }
66     def init(self, cr):
67         tools.drop_view_if_exists(cr, 'campaign_analysis')
68         cr.execute("""
69             create or replace view campaign_analysis as (
70             select
71                 min(wi.id) as id,
72                 to_char(wi.date::date, 'YYYY') as year,
73                 to_char(wi.date::date, 'MM') as month,
74                 wi.date::date as date,
75                 s.campaign_id as campaign_id,
76                 wi.activity_id as activity_id,
77                 wi.segment_id as segment_id,
78                 wi.partner_id as partner_id ,
79                 wi.state as state,
80                 sum(act.revenue) as revenue,
81                 count(*) as count
82             from
83                 marketing_campaign_workitem wi
84                 left join res_partner p on (p.id=wi.partner_id)
85                 left join marketing_campaign_segment s on (s.id=wi.segment_id)
86                 left join marketing_campaign_activity act on (act.id= wi.activity_id)
87             group by
88                 s.campaign_id,wi.activity_id,wi.segment_id,wi.partner_id,wi.state,
89                 wi.date::date
90             )
91         """)
92 campaign_analysis()