[IMP] hr_expense : After the confirmation of the expense, the button should create...
[odoo/odoo.git] / addons / hr_expense / report / hr_expense_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 import tools
23 from osv import fields,osv
24 from decimal_precision import decimal_precision as dp
25
26
27 class hr_expense_report(osv.osv):
28     _name = "hr.expense.report"
29     _description = "Expenses Statistics"
30     _auto = False
31     _rec_name = 'date'
32     _columns = {
33         'date': fields.date('Date ', readonly=True),
34         'year': fields.char('Year', size=4, readonly=True),
35         'day': fields.char('Day', size=128, readonly=True),
36         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'),
37             ('05','May'), ('06','June'), ('07','July'), ('08','August'), ('09','September'),
38             ('10','October'), ('11','November'), ('12','December')], 'Month',readonly=True),
39         'product_id':fields.many2one('product.product', 'Product', readonly=True),
40         'journal_id': fields.many2one('account.journal', 'Force Journal', readonly=True),
41         'product_qty':fields.float('Qty', readonly=True),
42         'employee_id': fields.many2one('hr.employee', "Employee's Name", readonly=True),
43         'date_confirm': fields.date('Confirmation Date', readonly=True),
44         'date_valid': fields.date('Validation Date', readonly=True),
45         'voucher_id': fields.many2one('account.voucher', 'Receipt', readonly=True),
46         'department_id':fields.many2one('hr.department','Department', readonly=True),
47         'company_id':fields.many2one('res.company', 'Company', readonly=True),
48         'user_id':fields.many2one('res.users', 'Validation User', readonly=True),
49         'currency_id': fields.many2one('res.currency', 'Currency', readonly=True),
50         'price_total':fields.float('Total Price', readonly=True, digits_compute=dp.get_precision('Account')),
51         'delay_valid':fields.float('Delay to Valid', readonly=True),
52         'delay_confirm':fields.float('Delay to Confirm', readonly=True),
53         'analytic_account': fields.many2one('account.analytic.account','Analytic account',readonly=True),
54         'price_average':fields.float('Average Price', readonly=True, digits_compute=dp.get_precision('Account')),
55         'nbr':fields.integer('# of Lines', readonly=True),
56         'no_of_products':fields.integer('# of Products', readonly=True),
57         'no_of_account':fields.integer('# of Accounts', readonly=True),
58         'state': fields.selection([
59             ('draft', 'Draft'),
60             ('confirm', 'Waiting confirmation'),
61             ('accepted', 'Accepted'),
62             ('done', 'Done'),
63             ('cancelled', 'Cancelled')],
64             'Status', readonly=True),
65     }
66     _order = 'date desc'
67     def init(self, cr):
68         tools.drop_view_if_exists(cr, 'hr_expense_report')
69         cr.execute("""
70             create or replace view hr_expense_report as (
71                  select
72                      min(l.id) as id,
73                      date_trunc('day',s.date) as date,
74                      s.employee_id,
75                      s.journal_id,
76                      s.currency_id,
77                      to_date(to_char(s.date_confirm, 'dd-MM-YYYY'),'dd-MM-YYYY') as date_confirm,
78                      to_date(to_char(s.date_valid, 'dd-MM-YYYY'),'dd-MM-YYYY') as date_valid,
79                      s.voucher_id,
80                      s.user_valid as user_id,
81                      s.department_id,
82                      to_char(date_trunc('day',s.create_date), 'YYYY') as year,
83                      to_char(date_trunc('day',s.create_date), 'MM') as month,
84                      to_char(date_trunc('day',s.create_date), 'YYYY-MM-DD') as day,
85                      avg(extract('epoch' from age(s.date_valid,s.date)))/(3600*24) as  delay_valid,
86                      avg(extract('epoch' from age(s.date_valid,s.date_confirm)))/(3600*24) as  delay_confirm,
87                      l.product_id as product_id,
88                      l.analytic_account as analytic_account,
89                      sum(l.unit_quantity * u.factor) as product_qty,
90                      s.company_id as company_id,
91                      sum(l.unit_quantity*l.unit_amount) as price_total,
92                      (sum(l.unit_quantity*l.unit_amount)/sum(case when l.unit_quantity=0 or u.factor=0 then 1 else l.unit_quantity * u.factor end))::decimal(16,2) as price_average,
93                      count(*) as nbr,
94                      (select unit_quantity from hr_expense_line where id=l.id and product_id is not null) as no_of_products,
95                      (select analytic_account from hr_expense_line where id=l.id and analytic_account is not null) as no_of_account,
96                      s.state
97                  from hr_expense_line l
98                  left join hr_expense_expense s on (s.id=l.expense_id)
99                  left join product_uom u on (u.id=l.uom_id)
100                  group by
101                      date_trunc('day',s.date),
102                      to_char(date_trunc('day',s.create_date), 'YYYY'),
103                      to_char(date_trunc('day',s.create_date), 'MM'),
104                      to_char(date_trunc('day',s.create_date), 'YYYY-MM-DD'),
105                      to_date(to_char(s.date_confirm, 'dd-MM-YYYY'),'dd-MM-YYYY'),
106                      to_date(to_char(s.date_valid, 'dd-MM-YYYY'),'dd-MM-YYYY'),
107                      l.product_id,
108                      l.analytic_account,
109                      s.voucher_id,
110                      s.currency_id,
111                      s.user_valid,
112                      s.department_id,
113                      l.uom_id,
114                      l.id,
115                      s.state,
116                      s.journal_id,
117                      s.company_id,
118                      s.employee_id
119             )
120         """)
121
122 hr_expense_report()
123
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: