2010-05-19 06:44:15 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp import tools
|
|
|
|
|
from openerp.osv import fields, osv
|
2012-12-17 15:23:03 +00:00
|
|
|
|
from openerp.addons.decimal_precision import decimal_precision as dp
|
2010-10-05 12:29:35 +00:00
|
|
|
|
|
2010-05-19 06:44:15 +00:00
|
|
|
|
|
2010-08-13 12:20:05 +00:00
|
|
|
|
class campaign_analysis(osv.osv):
|
2010-05-19 06:44:15 +00:00
|
|
|
|
_name = "campaign.analysis"
|
|
|
|
|
_description = "Campaign Analysis"
|
|
|
|
|
_auto = False
|
|
|
|
|
_rec_name = 'date'
|
2010-11-19 13:48:01 +00:00
|
|
|
|
def _total_cost(self, cr, uid, ids, field_name, arg, context=None):
|
2010-06-23 15:37:08 +00:00
|
|
|
|
"""
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
2010-05-19 06:44:15 +00:00
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case and section Data’s IDs
|
2010-06-23 15:37:08 +00:00
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-05-19 06:44:15 +00:00
|
|
|
|
result = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
|
for ca_obj in self.browse(cr, uid, ids, context=context):
|
2010-05-19 06:44:15 +00:00
|
|
|
|
wi_ids = self.pool.get('marketing.campaign.workitem').search(cr, uid,
|
2010-05-21 09:46:08 +00:00
|
|
|
|
[('segment_id.campaign_id', '=', ca_obj.campaign_id.id)])
|
2010-05-19 06:44:15 +00:00
|
|
|
|
total_cost = ca_obj.activity_id.variable_cost + \
|
2011-01-13 11:33:39 +00:00
|
|
|
|
((ca_obj.campaign_id.fixed_cost or 1.00) / len(wi_ids))
|
2010-05-19 06:44:15 +00:00
|
|
|
|
result[ca_obj.id] = total_cost
|
|
|
|
|
return result
|
|
|
|
|
_columns = {
|
2010-08-26 10:03:25 +00:00
|
|
|
|
'res_id' : fields.integer('Resource', readonly=True),
|
2010-05-19 06:44:15 +00:00
|
|
|
|
'year': fields.char('Year', size=4, readonly=True),
|
2010-09-13 00:43:29 +00:00
|
|
|
|
'month': fields.selection([('01','January'), ('02','February'),
|
|
|
|
|
('03','March'), ('04','April'),('05','May'), ('06','June'),
|
|
|
|
|
('07','July'), ('08','August'), ('09','September'),
|
|
|
|
|
('10','October'), ('11','November'), ('12','December')],
|
|
|
|
|
'Month', readonly=True),
|
|
|
|
|
'day': fields.char('Day', size=10, readonly=True),
|
2011-01-17 13:26:54 +00:00
|
|
|
|
'date': fields.date('Date', readonly=True, select=True),
|
2010-08-13 12:20:05 +00:00
|
|
|
|
'campaign_id': fields.many2one('marketing.campaign', 'Campaign',
|
2010-05-19 06:44:15 +00:00
|
|
|
|
readonly=True),
|
|
|
|
|
'activity_id': fields.many2one('marketing.campaign.activity', 'Activity',
|
|
|
|
|
readonly=True),
|
2010-08-13 12:20:05 +00:00
|
|
|
|
'segment_id': fields.many2one('marketing.campaign.segment', 'Segment',
|
2010-05-19 06:44:15 +00:00
|
|
|
|
readonly=True),
|
2010-05-19 07:33:24 +00:00
|
|
|
|
'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
|
2012-03-07 06:46:50 +00:00
|
|
|
|
'country_id': fields.related('partner_id', 'country_id',
|
2010-05-21 09:46:08 +00:00
|
|
|
|
type='many2one', relation='res.country',string='Country'),
|
2011-07-01 23:41:24 +00:00
|
|
|
|
'total_cost' : fields.function(_total_cost, string='Cost',
|
2012-07-10 12:34:00 +00:00
|
|
|
|
type="float", digits_compute=dp.get_precision('Account')),
|
|
|
|
|
'revenue': fields.float('Revenue', readonly=True, digits_compute=dp.get_precision('Account')),
|
2010-06-23 15:01:35 +00:00
|
|
|
|
'count' : fields.integer('# of Actions', readonly=True),
|
2010-09-13 00:43:29 +00:00
|
|
|
|
'state': fields.selection([('todo', 'To Do'),
|
|
|
|
|
('exception', 'Exception'), ('done', 'Done'),
|
2012-05-04 11:57:48 +00:00
|
|
|
|
('cancelled', 'Cancelled')], 'Status', readonly=True),
|
2010-05-19 06:44:15 +00:00
|
|
|
|
}
|
|
|
|
|
def init(self, cr):
|
|
|
|
|
tools.drop_view_if_exists(cr, 'campaign_analysis')
|
|
|
|
|
cr.execute("""
|
2010-06-23 15:01:35 +00:00
|
|
|
|
create or replace view campaign_analysis as (
|
|
|
|
|
select
|
|
|
|
|
min(wi.id) as id,
|
2010-08-26 10:03:25 +00:00
|
|
|
|
min(wi.res_id) as res_id,
|
2010-06-23 15:01:35 +00:00
|
|
|
|
to_char(wi.date::date, 'YYYY') as year,
|
|
|
|
|
to_char(wi.date::date, 'MM') as month,
|
2010-09-13 00:43:29 +00:00
|
|
|
|
to_char(wi.date::date, 'YYYY-MM-DD') as day,
|
2010-06-23 15:01:35 +00:00
|
|
|
|
wi.date::date as date,
|
|
|
|
|
s.campaign_id as campaign_id,
|
|
|
|
|
wi.activity_id as activity_id,
|
|
|
|
|
wi.segment_id as segment_id,
|
|
|
|
|
wi.partner_id as partner_id ,
|
2010-06-23 15:37:08 +00:00
|
|
|
|
wi.state as state,
|
2010-06-23 15:01:35 +00:00
|
|
|
|
sum(act.revenue) as revenue,
|
|
|
|
|
count(*) as count
|
|
|
|
|
from
|
|
|
|
|
marketing_campaign_workitem wi
|
|
|
|
|
left join res_partner p on (p.id=wi.partner_id)
|
|
|
|
|
left join marketing_campaign_segment s on (s.id=wi.segment_id)
|
|
|
|
|
left join marketing_campaign_activity act on (act.id= wi.activity_id)
|
|
|
|
|
group by
|
2010-06-23 15:37:08 +00:00
|
|
|
|
s.campaign_id,wi.activity_id,wi.segment_id,wi.partner_id,wi.state,
|
2010-06-23 15:01:35 +00:00
|
|
|
|
wi.date::date
|
|
|
|
|
)
|
|
|
|
|
""")
|
2011-11-22 08:51:38 +00:00
|
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|