1
1
class Timesheet
2
- attr_accessor :date_from , :date_to , :projects , :activities , :users , :groups , :allowed_projects , :period , :period_type
2
+ attr_accessor :date_from , :date_to , :projects , :activities , :users , :groups , :trackers ,
3
+ :allowed_projects , :period , :period_type
3
4
4
5
# Time entries on the Timesheet in the form of:
5
6
# project.name => {:logs => [time entries], :users => [users shown in logs] }
@@ -18,6 +19,7 @@ class Timesheet
18
19
:user => 'User' ,
19
20
:issue => 'Issue' ,
20
21
:group => 'Group' ,
22
+ :tracker => 'Tracker' ,
21
23
:date => 'Date'
22
24
}
23
25
@@ -32,7 +34,8 @@ def initialize(options = { })
32
34
self . potential_time_entry_ids = options [ :potential_time_entry_ids ] || [ ]
33
35
self . allowed_projects = options [ :allowed_projects ] || [ ]
34
36
self . groups = [ ]
35
-
37
+ self . trackers = [ ]
38
+
36
39
unless options [ :activities ] . nil?
37
40
self . activities = options [ :activities ] . collect do |activity_id |
38
41
# Include project-overridden activities
@@ -64,7 +67,16 @@ def initialize(options = { })
64
67
self . groups = Group . all
65
68
end
66
69
end
67
-
70
+
71
+ unless options [ :trackers ] . nil?
72
+ self . trackers = options [ :trackers ] . collect do |tracker_id |
73
+ tracker = Tracker . find ( tracker_id )
74
+ tracker . id if tracker
75
+ end . flatten . uniq . compact
76
+ else
77
+ self . trackers = Tracker . all . collect { |a | a . id . to_i }
78
+ end
79
+
68
80
if !options [ :sort ] . nil? && options [ :sort ] . respond_to? ( :to_sym ) && ValidSortOptions . keys . include? ( options [ :sort ] . to_sym )
69
81
self . sort = options [ :sort ] . to_sym
70
82
else
@@ -95,6 +107,8 @@ def fetch_time_entries
95
107
fetch_time_entries_by_issue
96
108
when :group
97
109
fetch_time_entries_by_group
110
+ when :tracker
111
+ fetch_time_entries_by_tracker
98
112
when :date
99
113
fetch_time_entries_by_date
100
114
else
@@ -201,6 +215,7 @@ def csv_header
201
215
'#' ,
202
216
l ( :label_date ) ,
203
217
l ( :label_member ) ,
218
+ l ( :label_tracker ) ,
204
219
l ( :label_activity ) ,
205
220
l ( :label_project ) ,
206
221
l ( :label_version ) ,
@@ -218,6 +233,7 @@ def time_entry_to_csv(time_entry)
218
233
time_entry . id ,
219
234
time_entry . spent_on ,
220
235
time_entry . user . name ,
236
+ time_entry . issue . tracker . name ,
221
237
time_entry . activity . name ,
222
238
time_entry . project ,
223
239
( time_entry . issue . fixed_version if time_entry . issue ) ,
@@ -232,24 +248,26 @@ def time_entry_to_csv(time_entry)
232
248
233
249
# Array of users to find
234
250
# String of extra conditions to add onto the query (AND)
235
- def conditions ( users , extra_conditions = nil )
251
+ def conditions ( users , trackers , extra_conditions = nil )
236
252
if self . potential_time_entry_ids . empty?
237
253
if self . date_from . present? && self . date_to . present?
238
- conditions = [ "spent_on >= (:from) AND spent_on <= (:to) AND #{ TimeEntry . table_name } .project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities)" ,
254
+ conditions = [ "spent_on >= (:from) AND spent_on <= (:to) AND #{ TimeEntry . table_name } .project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities) AND tracker_id IN (:trackers) " ,
239
255
{
240
256
:from => self . date_from ,
241
257
:to => self . date_to ,
242
258
:projects => self . projects ,
243
259
:activities => self . activities ,
244
260
:groups => self . groups ,
261
+ :trackers => trackers ,
245
262
:users => users
246
263
} ]
247
264
else # All time
248
- conditions = [ "#{ TimeEntry . table_name } .project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities)" ,
265
+ conditions = [ "#{ TimeEntry . table_name } .project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities) AND tracker_id IN (:trackers) " ,
249
266
{
250
267
:projects => self . projects ,
251
268
:activities => self . activities ,
252
269
:groups => self . groups ,
270
+ :trackers => trackers ,
253
271
:users => users
254
272
} ]
255
273
end
@@ -279,46 +297,65 @@ def includes
279
297
280
298
281
299
def time_entries_for_all_users ( project )
282
- return project . time_entries . includes ( self . includes ) .
283
- where ( self . conditions ( self . users ) ) .
300
+ return project . time_entries . includes ( self . includes + [ { :issue => [ :tracker ] } ] ) .
301
+ joins ( :issue ) .
302
+ where ( self . conditions ( self . users , self . trackers ) ) .
284
303
order ( 'spent_on ASC' )
285
304
end
286
-
305
+
287
306
def time_entries_for_all_users_in_group ( group )
288
- return TimeEntry . includes ( self . includes ) .
289
- where ( self . conditions ( group . user_ids ) ) .
307
+ return TimeEntry . includes ( self . includes + [ { :issue => [ :tracker ] } ] ) .
308
+ joins ( :issue ) .
309
+ where ( self . conditions ( group . user_ids , self . trackers ) ) .
290
310
order ( 'spent_on ASC' )
291
- end
292
-
311
+ end
312
+
313
+ def time_entries_for_all_users_in_tracker ( tracker )
314
+ return TimeEntry . includes ( self . includes + [ { :issue => [ :tracker ] } ] ) .
315
+ joins ( :issue ) .
316
+ where ( self . conditions ( self . users , tracker ) ) .
317
+ order ( 'spent_on ASC' )
318
+ end
319
+
293
320
def time_entries_for_current_user ( project )
294
321
return project . time_entries .
295
322
includes ( self . includes + [ :activity , :user , { :issue => [ :tracker , :assigned_to , :priority ] } ] ) .
296
- where ( self . conditions ( User . current . id ) ) .
323
+ joins ( :issue ) .
324
+ where ( self . conditions ( User . current . id , self . trackers ) ) .
325
+ order ( 'spent_on ASC' )
326
+ end
327
+
328
+ def time_entries_for_current_user_in_tracker ( tracker )
329
+ return TimeEntry . includes ( self . includes + [ :activity , :user , { :issue => [ :tracker , :assigned_to , :priority ] } ] ) .
330
+ joins ( :issue ) .
331
+ where ( self . conditions ( User . current . id , tracker ) ) .
297
332
order ( 'spent_on ASC' )
298
333
end
299
334
300
335
def issue_time_entries_for_all_users ( issue )
301
336
return issue . time_entries . includes ( self . includes + [ :activity , :user ] ) .
302
- where ( self . conditions ( self . users ) ) .
337
+ joins ( :issue ) .
338
+ where ( self . conditions ( self . users , self . trackers ) ) .
303
339
order ( 'spent_on ASC' )
304
340
end
305
341
306
342
def issue_time_entries_for_current_user ( issue )
307
343
return issue . time_entries . includes ( self . includes + [ :activity , :user ] ) .
308
- where ( self . conditions ( User . current . id ) ) .
344
+ joins ( :issue ) .
345
+ where ( self . conditions ( User . current . id , self . trackers ) ) .
309
346
order ( 'spent_on ASC' )
310
347
end
311
348
312
- def time_entries_for_user ( user , options = { } )
349
+ def time_entries_for_user ( user , trackers , options = { } )
313
350
extra_conditions = options . delete ( :conditions )
314
351
315
352
return TimeEntry . includes ( self . includes ) .
316
- where ( self . conditions ( [ user ] , extra_conditions ) ) .
353
+ joins ( :issue ) .
354
+ where ( self . conditions ( [ user ] , trackers , extra_conditions ) ) .
317
355
order ( 'spent_on ASC' )
318
356
end
319
357
320
358
def fetch_time_entries_by_project
321
- puts self . projects
322
359
self . projects . each do |project |
323
360
logs = [ ]
324
361
users = [ ]
@@ -372,20 +409,20 @@ def fetch_time_entries_by_group
372
409
end
373
410
end
374
411
end
375
-
412
+
376
413
def fetch_time_entries_by_user
377
414
self . users . each do |user_id |
378
415
logs = [ ]
379
416
if User . current . admin?
380
417
# Administrators can see all time entries
381
- logs = time_entries_for_user ( user_id )
418
+ logs = time_entries_for_user ( user_id , self . trackers )
382
419
elsif User . current . id == user_id
383
420
# Users can see their own their time entries
384
- logs = time_entries_for_user ( user_id )
421
+ logs = time_entries_for_user ( user_id , self . trackers )
385
422
elsif User . current . allowed_to_on_single_potentially_archived_project? ( :see_project_timesheets , nil , :global => true )
386
423
# User can see project timesheets in at least once place, so
387
424
# fetch the user timelogs for those projects
388
- logs = time_entries_for_user ( user_id , :conditions => Project . allowed_to_condition ( User . current , :see_project_timesheets ) )
425
+ logs = time_entries_for_user ( user_id , self . trackers , :conditions => Project . allowed_to_condition ( User . current , :see_project_timesheets ) )
389
426
else
390
427
# Rest can see nothing
391
428
end
@@ -397,7 +434,34 @@ def fetch_time_entries_by_user
397
434
end
398
435
end
399
436
400
-
437
+
438
+ def fetch_time_entries_by_tracker
439
+ trackers = Tracker . where ( :id => self . trackers )
440
+ trackers . each do |tracker |
441
+ logs = [ ]
442
+ users = [ ]
443
+ if User . current . admin?
444
+ # Administrators can see all time entries
445
+ logs = time_entries_for_all_users_in_tracker ( tracker . id )
446
+ else
447
+ # Users with the Role and correct permission can see all time entries
448
+ logs1 = time_entries_for_all_users_in_tracker ( tracker . id ) . select do |te |
449
+ project = Project . find ( te . project_id )
450
+ User . current . allowed_to_on_single_potentially_archived_project? ( :see_project_timesheets , project )
451
+ end
452
+ # Users with permission to see their time entries
453
+ logs2 = time_entries_for_current_user_in_tracker ( tracker . id ) . select do |te |
454
+ project = Project . find ( te . project_id )
455
+ User . current . allowed_to_on_single_potentially_archived_project? ( :view_time_entries , project )
456
+ end
457
+ logs = logs1 + logs2
458
+ end
459
+ unless logs . empty?
460
+ users = logs . collect ( &:user ) . uniq . sort
461
+ self . time_entries [ tracker . name ] = { :logs => logs , :users => users }
462
+ end
463
+ end
464
+ end
401
465
402
466
403
467
# project => { :users => [users shown in logs],
@@ -454,7 +518,7 @@ def fetch_time_entries_by_date
454
518
logs = [ ]
455
519
456
520
# extra_conditions = 'GROUP_BY spent_on'
457
- logs = TimeEntry . includes ( self . includes ) . where ( self . conditions ( self . users ) )
521
+ logs = TimeEntry . includes ( self . includes ) . joins ( :issue ) . where ( self . conditions ( self . users , self . trackers ) )
458
522
459
523
460
524
unless logs . empty?
0 commit comments