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