-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtimesheet_controller.rb
165 lines (137 loc) · 4.4 KB
/
timesheet_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class TimesheetController < ApplicationController
unloadable
layout 'base'
before_filter :get_list_size
before_filter :get_precision
before_filter :get_activities
helper :sort
include SortHelper
helper :issues
include ApplicationHelper
helper :timelog
SessionKey = 'timesheet_filter'
verify :method => :delete, :only => :reset, :render => {:nothing => true, :status => :method_not_allowed }
def index
load_filters_from_session
unless @timesheet
@timesheet ||= Timesheet.new
end
@timesheet.allowed_projects = allowed_projects
if @timesheet.allowed_projects.empty?
render :action => 'no_projects'
return
end
end
def report
if params && params[:timesheet]
@timesheet = Timesheet.new( params[:timesheet] )
else
redirect_to :action => 'index'
return
end
@timesheet.allowed_projects = allowed_projects
if @timesheet.allowed_projects.empty?
render :action => 'no_projects'
return
end
if !params[:timesheet][:projects].blank?
@timesheet.projects = @timesheet.allowed_projects.find_all { |project|
params[:timesheet][:projects].include?(project.id.to_s)
}
else
@timesheet.projects = @timesheet.allowed_projects
end
call_hook(:plugin_timesheet_controller_report_pre_fetch_time_entries, { :timesheet => @timesheet, :params => params })
save_filters_to_session(@timesheet)
@timesheet.fetch_time_entries
# Sums
@total = { }
unless @timesheet.sort == :issue
@timesheet.time_entries.each do |project,logs|
@total[project] = 0
if logs[:logs]
logs[:logs].each do |log|
@total[project] += log.hours
end
end
end
else
@timesheet.time_entries.each do |project, project_data|
@total[project] = 0
if project_data[:issues]
project_data[:issues].each do |issue, issue_data|
@total[project] += issue_data.collect(&:hours).sum
end
end
end
end
@grand_total = @total.collect{|k,v| v}.inject{|sum,n| sum + n}
respond_to do |format|
format.html { render :action => 'details', :layout => false if request.xhr? }
format.csv { send_data @timesheet.to_csv, :filename => 'timesheet.csv', :type => "text/csv" }
end
end
def context_menu
@time_entries = TimeEntry.find(:all, :conditions => ['id IN (?)', params[:ids]])
render :layout => false
end
def reset
clear_filters_from_session
redirect_to :action => 'index'
end
private
def get_list_size
@list_size = Setting.plugin_timesheet_plugin['list_size'].to_i
end
def get_precision
precision = Setting.plugin_timesheet_plugin['precision']
if precision.blank?
# Set precision to a high number
@precision = 10
else
@precision = precision.to_i
end
end
def get_activities
@activities = TimeEntryActivity.all(:conditions => 'parent_id IS NULL')
end
def allowed_projects
if User.current.admin?
Project.timesheet_order_by_name
elsif Setting.plugin_timesheet_plugin['project_status'] == 'all'
Project.timesheet_order_by_name.timesheet_with_membership(User.current)
else
Project.timesheet_order_by_name.all(:conditions => Project.visible_condition(User.current))
end
end
def clear_filters_from_session
session[SessionKey] = nil
end
def load_filters_from_session
if session[SessionKey]
@timesheet = Timesheet.new(session[SessionKey])
# Default to free period
@timesheet.period_type = Timesheet::ValidPeriodType[:free_period]
end
if session[SessionKey] && session[SessionKey]['projects']
@timesheet.projects = allowed_projects.find_all { |project|
session[SessionKey]['projects'].include?(project.id.to_s)
}
end
end
def save_filters_to_session(timesheet)
if params[:timesheet]
# Check that the params will fit in the session before saving
# prevents an ActionController::Session::CookieStore::CookieOverflow
encoded = Base64.encode64(Marshal.dump(params[:timesheet]))
if encoded.size < 2.kilobytes # Only use 2K of the cookie
session[SessionKey] = params[:timesheet]
end
end
if timesheet
session[SessionKey] ||= {}
session[SessionKey]['date_from'] = timesheet.date_from
session[SessionKey]['date_to'] = timesheet.date_to
end
end
end