[IMP] rename is_application to application
[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="[('section_ids','=',section_id)]"),
66         'categ_id': fields.many2one('crm.case.categ', 'Category',\
67                          domain="[('section_id','=',section_id),\
68                         ('object_id.model', '=', 'crm.claim')]", readonly=True),
69         'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
70         'company_id': fields.many2one('res.company', 'Company', readonly=True),
71         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Priority'),
72         'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
73         'date_closed': fields.date('Close Date', readonly=True, select=True),
74         'date_deadline': fields.date('Deadline', readonly=True, select=True),
75         'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
76         'email': fields.integer('# Emails', size=128, readonly=True)
77     }
78
79     def init(self, cr):
80
81         """ Display Number of cases And Section Name
82         @param cr: the current row, from the database cursor,
83          """
84
85         tools.drop_view_if_exists(cr, 'crm_claim_report')
86         cr.execute("""
87             create or replace view crm_claim_report as (
88                 select
89                     min(c.id) as id,
90                     to_char(c.date, 'YYYY') as name,
91                     to_char(c.date, 'MM') as month,
92                     to_char(c.date, 'YYYY-MM-DD') as day,
93                     to_char(c.date_closed, 'YYYY-MM-DD') as date_closed,
94                     to_char(c.date_deadline, 'YYYY-MM-DD') as date_deadline,
95                     c.state,
96                     c.user_id,
97                     c.stage_id,
98                     c.section_id,
99                     c.partner_id,
100                     c.company_id,
101                     c.categ_id,
102                     count(*) as nbr,
103                     c.priority as priority,
104                     c.type_action as type_action,
105                     date_trunc('day',c.create_date) as create_date,
106                     avg(extract('epoch' from (c.date_closed-c.create_date)))/(3600*24) as  delay_close,
107                     (SELECT count(id) FROM mail_message WHERE model='crm.claim' AND res_id=c.id) AS email,
108                     extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24) as  delay_expected
109                 from
110                     crm_claim c
111                 group by to_char(c.date, 'YYYY'), to_char(c.date, 'MM'),to_char(c.date, 'YYYY-MM-DD'),\
112                         c.state, c.user_id,c.section_id, c.stage_id,\
113                         c.categ_id,c.partner_id,c.company_id,c.create_date,
114                         c.priority,c.type_action,c.date_deadline,c.date_closed,c.id
115             )""")
116
117 crm_claim_report()
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: