1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import fields,osv
24 from decimal_precision import decimal_precision as dp
27 class hr_expense_report(osv.osv):
28 _name = "hr.expense.report"
29 _description = "Expenses Statistics"
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([
60 ('confirm', 'Waiting confirmation'),
61 ('accepted', 'Accepted'),
63 ('cancelled', 'Cancelled')],
64 'Status', readonly=True),
68 tools.drop_view_if_exists(cr, 'hr_expense_report')
70 create or replace view hr_expense_report as (
73 date_trunc('day',s.date) as date,
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,
80 s.user_valid as user_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,
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,
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)
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'),
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: