bugfix:central journal report
[odoo/odoo.git] / addons / report_timesheet / report_timesheet.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24
25 import tools.sql
26
27 class report_timesheet_user(osv.osv):
28     _name = "report_timesheet.user"
29     _description = "Timesheet per day"
30     _auto = False
31     _columns = {
32         'name': fields.date('Date', readonly=True),
33         'user_id':fields.many2one('res.users', 'User', readonly=True),
34         'quantity': fields.float('Quantity', readonly=True),
35         'cost': fields.float('Cost', readonly=True)
36     }
37     _order = 'name desc,user_id desc'
38     def init(self, cr):
39         tools.sql.drop_view_if_exists(cr, 'report_timesheet_user')
40         cr.execute("""
41             create or replace view report_timesheet_user as (
42                 select
43                     min(l.id) as id,
44                     l.date as name,
45                     l.user_id,
46                     sum(l.unit_amount) as quantity,
47                     sum(l.amount) as cost
48                 from
49                     account_analytic_line l
50                 where
51                     user_id is not null
52                 group by l.date, l.user_id
53             )
54         """)
55 report_timesheet_user()
56
57 class report_timesheet_account(osv.osv):
58     _name = "report_timesheet.account"
59     _description = "Timesheet per account"
60     _auto = False
61     _columns = {
62         'name': fields.date('Month', readonly=True),
63         'user_id':fields.many2one('res.users', 'User', readonly=True),
64         'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
65         'quantity': fields.float('Quantity', readonly=True),
66     }
67     _order = 'name desc,account_id desc,user_id desc'
68     def init(self, cr):
69         cr.execute("""
70             create or replace view report_timesheet_account as (
71                 select
72                     min(id) as id,
73                     to_char(create_date, 'YYYY-MM-01') as name,
74                     user_id,
75                     account_id,
76                     sum(unit_amount) as quantity
77                 from
78                     account_analytic_line
79                 group by
80                     to_char(create_date, 'YYYY-MM-01'), user_id, account_id
81             )
82         """)
83 report_timesheet_account()
84
85
86 class report_timesheet_account_date(osv.osv):
87     _name = "report_timesheet.account.date"
88     _description = "Daily timesheet per account"
89     _auto = False
90     _columns = {
91         'name': fields.date('Date', readonly=True),
92         'user_id':fields.many2one('res.users', 'User', readonly=True),
93         'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
94         'quantity': fields.float('Quantity', readonly=True),
95     }
96     _order = 'name desc,account_id desc,user_id desc'
97     
98     def init(self, cr):
99         cr.execute("""
100             create or replace view report_timesheet_account_date as (
101                 select
102                     min(id) as id,
103                     date as name,
104                     user_id,
105                     account_id,
106                     sum(unit_amount) as quantity
107                 from
108                     account_analytic_line
109                 group by
110                     date, user_id, account_id
111             )
112         """)
113 report_timesheet_account_date()
114
115
116 class report_timesheet_invoice(osv.osv):
117     _name = "report_timesheet.invoice"
118     _description = "Costs to invoice"
119     _auto = False
120     _columns = {
121         'user_id':fields.many2one('res.users', 'User', readonly=True),
122         'account_id':fields.many2one('account.analytic.account', 'Project', readonly=True),
123         'manager_id':fields.many2one('res.users', 'Manager', readonly=True),
124         'quantity': fields.float('Quantity', readonly=True),
125         'amount_invoice': fields.float('To invoice', readonly=True)
126     }
127     _rec_name = 'user_id'
128     _order = 'user_id desc'
129     def init(self, cr):
130         cr.execute("""
131             create or replace view report_timesheet_invoice as (
132                 select
133                     min(l.id) as id,
134                     l.user_id as user_id,
135                     l.account_id as account_id,
136                     a.user_id as manager_id,
137                     sum(l.unit_amount) as quantity,
138                     sum(l.unit_amount * t.list_price) as revenue
139                 from account_analytic_line l
140                     left join hr_timesheet_invoice_factor f on (l.to_invoice=f.id)
141                     left join account_analytic_account a on (l.account_id=a.id)
142                     left join product_product p on (l.to_invoice=f.id)
143                     left join product_template t on (l.to_invoice=f.id)
144                 where
145                     l.to_invoice is not null and
146                     l.invoice_id is null
147                 group by
148                     l.user_id,
149                     l.account_id,
150                     a.user_id
151             )
152         """)
153 report_timesheet_invoice()
154
155 class report_random_timsheet(osv.osv):
156     _name = "report.random.timesheet"
157     _description = "Random Timesheet Report"
158     _auto = False
159     
160     _columns = {
161         'analytic_account_id' : fields.many2one('account.analytic.account','Analytic Account', readonly=True),
162         'name': fields.char('Description', size=64, readonly=True),
163         'quantity' : fields.float('Quantity', readonly=True),
164         'date': fields.date('Date', readonly=True),
165         'user_id' : fields.many2one('res.users', 'User', readonly=True)
166     }
167     _order = "date desc"
168     
169     def __init__(self, pool, cr):
170         super(report_random_timsheet, self).__init__(pool, cr)
171         self.called = False
172     
173     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False):
174         """ To call the init() method timely
175         """
176         if not self.called:
177             self.init(cr, user)
178         self.called = True # To make sure that init doesn't get called multiple times
179         
180         res = super(report_random_timsheet, self).fields_view_get(cr, user, view_id, view_type, context, toolbar)
181         return res
182     
183     def init(self, cr, uid=1):
184         
185         cr.execute("""create or replace view report_random_timesheet as (
186
187             select 
188                 line.id as id, line.account_id as analytic_account_id, line.name as name,
189                 line.unit_amount as quantity, line.date as date, line.user_id as user_id
190             from 
191                 account_analytic_line line, hr_department dept,hr_department_user_rel dept_user
192             where
193                 (dept.id = dept_user.department_id AND dept_user.user_id=line.user_id AND line.user_id is not null)
194                 AND (dept.manager_id = """ + str(uid) + """ ) 
195                 AND (line.date <= CURRENT_DATE AND line.date > (CURRENT_DATE-3))
196             LIMIT 10
197             )
198             """ )
199
200 report_random_timsheet()
201
202 class random_timesheet_lines(osv.osv):
203     _name = "random.timesheet.lines"
204     _description = "Random Timesheet Lines"
205     _auto = False
206     
207     _columns = {
208         'date': fields.date('Date', readonly=True),     
209         'name': fields.char('Description', size=64, readonly=True),
210         'user_id' : fields.many2one('res.users', 'User', readonly=True),
211         'quantity' : fields.float('Quantity', readonly=True),
212         'product_id' : fields.many2one('product.product', 'Product', readonly=True), 
213         'analytic_account_id' : fields.many2one('account.analytic.account','Analytic Account', readonly=True),
214         'uom_id' : fields.many2one('product.uom', 'UoM', readonly=True),
215         'amount' : fields.float('Amount', readonly=True),
216         'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Invoicing', readonly=True),
217         'general_account_id' : fields.many2one('account.account', 'General Account', readonly=True)
218     }
219     
220     _order = "date desc"
221     
222     def init(self, cr):
223         
224         cr.execute("""create or replace view random_timesheet_lines as (
225             select 
226                 line.id as id, line.date as date, line.name as name, line.unit_amount as quantity,
227                 line.product_id as product_id, line.account_id as analytic_account_id,
228                 line.product_uom_id as uom_id, line.amount as amount, line.to_invoice as to_invoice,
229                 line.general_account_id as general_account_id, line.user_id as user_id 
230             from 
231                 account_analytic_line line
232             where
233                 (line.date <= CURRENT_DATE AND line.date > (CURRENT_DATE-15))
234             )
235             """ )
236
237 random_timesheet_lines()
238
239 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
240