[IMP] crm*: update crm modules to new mail module (wip)
[odoo/odoo.git] / addons / crm_claim / report / crm_claim_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
22 from osv import fields,osv
23 import tools
24
25 AVAILABLE_STATES = [
26     ('draft','Draft'),
27     ('open','Open'),
28     ('cancel', 'Cancelled'),
29     ('done', 'Closed'),
30     ('pending','Pending')
31 ]
32
33 AVAILABLE_PRIORITIES = [
34     ('5', 'Lowest'),
35     ('4', 'Low'),
36     ('3', 'Normal'),
37     ('2', 'High'),
38     ('1', 'Highest')
39 ]
40
41
42 class crm_claim_report(osv.osv):
43     """ CRM Claim Report"""
44
45     _name = "crm.claim.report"
46     _auto = False
47     _description = "CRM Claim Report"
48
49     _columns = {
50         'name': fields.char('Year', size=64, required=False, readonly=True),
51         'user_id':fields.many2one('res.users', 'User', readonly=True),
52         'section_id':fields.many2one('crm.case.section', 'Section', readonly=True),
53         'nbr': fields.integer('# of Cases', readonly=True),
54         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
55         'month':fields.selection([('01', 'January'), ('02', 'February'), \
56                                   ('03', 'March'), ('04', 'April'),\
57                                   ('05', 'May'), ('06', 'June'), \
58                                   ('07', 'July'), ('08', 'August'),\
59                                   ('09', 'September'), ('10', 'October'),\
60                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
61         'company_id': fields.many2one('res.company', 'Company', readonly=True),
62         'create_date': fields.datetime('Create Date', readonly=True, select=True),
63         'day': fields.char('Day', size=128, readonly=True),
64         'delay_close': fields.float('Delay to close', digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
65         'stage_id': fields.many2one ('crm.case.stage', 'Stage', readonly=True, domain="[('type','=','claim')]"),
66         'categ_id': fields.many2one('crm.case.categ', 'Category',\
67                          domain="[('section_id','=',section_id),\
68                         ('object_id.model', '=', 'crm.claim')]", readonly=True),
69         'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg"),
70         'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
71         'company_id': fields.many2one('res.company', 'Company', readonly=True),
72         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Priority'),
73         'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
74         'date_closed': fields.date('Close Date', readonly=True, select=True),
75         'date_deadline': fields.date('Deadline', readonly=True, select=True),
76         'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
77         'email': fields.integer('# Emails', size=128, readonly=True),
78          'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg")
79     }
80
81     def init(self, cr):
82
83         """ Display Number of cases And Section Name
84         @param cr: the current row, from the database cursor,
85          """
86
87         tools.drop_view_if_exists(cr, 'crm_claim_report')
88         cr.execute("""
89             create or replace view crm_claim_report as (
90                 select
91                     min(c.id) as id,
92                     to_char(c.date, 'YYYY') as name,
93                     to_char(c.date, 'MM') as month,
94                     to_char(c.date, 'YYYY-MM-DD') as day,
95                     to_char(c.date_closed, 'YYYY-MM-DD') as date_closed,
96                     to_char(c.date_deadline, 'YYYY-MM-DD') as date_deadline,
97                     c.state,
98                     c.user_id,
99                     c.stage_id,
100                     c.section_id,
101                     c.partner_id,
102                     c.company_id,
103                     c.categ_id,
104                     count(*) as nbr,
105                     c.priority as priority,
106                     c.type_action as type_action,
107                     date_trunc('day',c.create_date) as create_date,
108                     avg(extract('epoch' from (c.date_closed-c.create_date)))/(3600*24) as  delay_close,
109                     (SELECT count(id) FROM email_message WHERE model='crm.claim' AND res_id=c.id AND email_from IS NOT NULL) AS email,
110                     (SELECT avg(probability) FROM crm_case_stage WHERE type='claim' AND id=c.stage_id) AS probability,
111                     extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24) as  delay_expected
112                 from
113                     crm_claim c
114                 group by to_char(c.date, 'YYYY'), to_char(c.date, 'MM'),to_char(c.date, 'YYYY-MM-DD'),\
115                         c.state, c.user_id,c.section_id, c.stage_id,\
116                         c.categ_id,c.partner_id,c.company_id,c.create_date,
117                         c.priority,c.type_action,c.date_deadline,c.date_closed,c.id
118             )""")
119
120 crm_claim_report()
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: