@@ -1,290 +1,293 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class TimelogController < ApplicationController |
|
18 | class TimelogController < ApplicationController | |
19 | menu_item :issues |
|
19 | menu_item :issues | |
20 | before_filter :find_project, :authorize, :only => [:edit, :destroy] |
|
20 | before_filter :find_project, :authorize, :only => [:edit, :destroy] | |
21 | before_filter :find_optional_project, :only => [:report, :details] |
|
21 | before_filter :find_optional_project, :only => [:report, :details] | |
22 |
|
22 | |||
23 | verify :method => :post, :only => :destroy, :redirect_to => { :action => :details } |
|
23 | verify :method => :post, :only => :destroy, :redirect_to => { :action => :details } | |
24 |
|
24 | |||
25 | helper :sort |
|
25 | helper :sort | |
26 | include SortHelper |
|
26 | include SortHelper | |
27 | helper :issues |
|
27 | helper :issues | |
28 | include TimelogHelper |
|
28 | include TimelogHelper | |
29 | helper :custom_fields |
|
29 | helper :custom_fields | |
30 | include CustomFieldsHelper |
|
30 | include CustomFieldsHelper | |
31 |
|
31 | |||
32 | def report |
|
32 | def report | |
33 | @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", |
|
33 | @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", | |
34 | :klass => Project, |
|
34 | :klass => Project, | |
35 | :label => :label_project}, |
|
35 | :label => :label_project}, | |
36 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", |
|
36 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", | |
37 | :klass => Version, |
|
37 | :klass => Version, | |
38 | :label => :label_version}, |
|
38 | :label => :label_version}, | |
39 | 'category' => {:sql => "#{Issue.table_name}.category_id", |
|
39 | 'category' => {:sql => "#{Issue.table_name}.category_id", | |
40 | :klass => IssueCategory, |
|
40 | :klass => IssueCategory, | |
41 | :label => :field_category}, |
|
41 | :label => :field_category}, | |
42 | 'member' => {:sql => "#{TimeEntry.table_name}.user_id", |
|
42 | 'member' => {:sql => "#{TimeEntry.table_name}.user_id", | |
43 | :klass => User, |
|
43 | :klass => User, | |
44 | :label => :label_member}, |
|
44 | :label => :label_member}, | |
45 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", |
|
45 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", | |
46 | :klass => Tracker, |
|
46 | :klass => Tracker, | |
47 | :label => :label_tracker}, |
|
47 | :label => :label_tracker}, | |
48 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", |
|
48 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", | |
49 | :klass => Enumeration, |
|
49 | :klass => Enumeration, | |
50 | :label => :label_activity}, |
|
50 | :label => :label_activity}, | |
51 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", |
|
51 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", | |
52 | :klass => Issue, |
|
52 | :klass => Issue, | |
53 | :label => :label_issue} |
|
53 | :label => :label_issue} | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | # Add list and boolean custom fields as available criterias |
|
56 | # Add list and boolean custom fields as available criterias | |
57 | custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) |
|
57 | custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) | |
58 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| |
|
58 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
59 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)", |
|
59 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)", | |
60 | :format => cf.field_format, |
|
60 | :format => cf.field_format, | |
61 | :label => cf.name} |
|
61 | :label => cf.name} | |
62 | end if @project |
|
62 | end if @project | |
63 |
|
63 | |||
64 | # Add list and boolean time entry custom fields |
|
64 | # Add list and boolean time entry custom fields | |
65 | TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| |
|
65 | TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
66 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)", |
|
66 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)", | |
67 | :format => cf.field_format, |
|
67 | :format => cf.field_format, | |
68 | :label => cf.name} |
|
68 | :label => cf.name} | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | @criterias = params[:criterias] || [] |
|
71 | @criterias = params[:criterias] || [] | |
72 | @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria} |
|
72 | @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria} | |
73 | @criterias.uniq! |
|
73 | @criterias.uniq! | |
74 | @criterias = @criterias[0,3] |
|
74 | @criterias = @criterias[0,3] | |
75 |
|
75 | |||
76 | @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month' |
|
76 | @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month' | |
77 |
|
77 | |||
78 | retrieve_date_range |
|
78 | retrieve_date_range | |
79 |
|
79 | |||
80 | unless @criterias.empty? |
|
80 | unless @criterias.empty? | |
81 | sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ') |
|
81 | sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ') | |
82 | sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ') |
|
82 | sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ') | |
83 |
|
83 | |||
84 | sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours" |
|
84 | sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours" | |
85 | sql << " FROM #{TimeEntry.table_name}" |
|
85 | sql << " FROM #{TimeEntry.table_name}" | |
86 | sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id" |
|
86 | sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id" | |
87 | sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id" |
|
87 | sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id" | |
88 | sql << " WHERE" |
|
88 | sql << " WHERE" | |
89 | sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project |
|
89 | sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project | |
90 | sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries) |
|
90 | sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries) | |
91 | sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)] |
|
91 | sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)] | |
92 | sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on" |
|
92 | sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on" | |
93 |
|
93 | |||
94 | @hours = ActiveRecord::Base.connection.select_all(sql) |
|
94 | @hours = ActiveRecord::Base.connection.select_all(sql) | |
95 |
|
95 | |||
96 | @hours.each do |row| |
|
96 | @hours.each do |row| | |
97 | case @columns |
|
97 | case @columns | |
98 | when 'year' |
|
98 | when 'year' | |
99 | row['year'] = row['tyear'] |
|
99 | row['year'] = row['tyear'] | |
100 | when 'month' |
|
100 | when 'month' | |
101 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" |
|
101 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" | |
102 | when 'week' |
|
102 | when 'week' | |
103 | row['week'] = "#{row['tyear']}-#{row['tweek']}" |
|
103 | row['week'] = "#{row['tyear']}-#{row['tweek']}" | |
104 | when 'day' |
|
104 | when 'day' | |
105 | row['day'] = "#{row['spent_on']}" |
|
105 | row['day'] = "#{row['spent_on']}" | |
106 | end |
|
106 | end | |
107 | end |
|
107 | end | |
108 |
|
108 | |||
109 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} |
|
109 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} | |
110 |
|
110 | |||
111 | @periods = [] |
|
111 | @periods = [] | |
112 | # Date#at_beginning_of_ not supported in Rails 1.2.x |
|
112 | # Date#at_beginning_of_ not supported in Rails 1.2.x | |
113 | date_from = @from.to_time |
|
113 | date_from = @from.to_time | |
114 | # 100 columns max |
|
114 | # 100 columns max | |
115 | while date_from <= @to.to_time && @periods.length < 100 |
|
115 | while date_from <= @to.to_time && @periods.length < 100 | |
116 | case @columns |
|
116 | case @columns | |
117 | when 'year' |
|
117 | when 'year' | |
118 | @periods << "#{date_from.year}" |
|
118 | @periods << "#{date_from.year}" | |
119 | date_from = (date_from + 1.year).at_beginning_of_year |
|
119 | date_from = (date_from + 1.year).at_beginning_of_year | |
120 | when 'month' |
|
120 | when 'month' | |
121 | @periods << "#{date_from.year}-#{date_from.month}" |
|
121 | @periods << "#{date_from.year}-#{date_from.month}" | |
122 | date_from = (date_from + 1.month).at_beginning_of_month |
|
122 | date_from = (date_from + 1.month).at_beginning_of_month | |
123 | when 'week' |
|
123 | when 'week' | |
124 | @periods << "#{date_from.year}-#{date_from.to_date.cweek}" |
|
124 | @periods << "#{date_from.year}-#{date_from.to_date.cweek}" | |
125 | date_from = (date_from + 7.day).at_beginning_of_week |
|
125 | date_from = (date_from + 7.day).at_beginning_of_week | |
126 | when 'day' |
|
126 | when 'day' | |
127 | @periods << "#{date_from.to_date}" |
|
127 | @periods << "#{date_from.to_date}" | |
128 | date_from = date_from + 1.day |
|
128 | date_from = date_from + 1.day | |
129 | end |
|
129 | end | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 |
|
132 | |||
133 | respond_to do |format| |
|
133 | respond_to do |format| | |
134 | format.html { render :layout => !request.xhr? } |
|
134 | format.html { render :layout => !request.xhr? } | |
135 | format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
135 | format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') } | |
136 | end |
|
136 | end | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | def details |
|
139 | def details | |
140 | sort_init 'spent_on', 'desc' |
|
140 | sort_init 'spent_on', 'desc' | |
141 | sort_update 'spent_on' => 'spent_on', |
|
141 | sort_update 'spent_on' => 'spent_on', | |
142 | 'user' => 'user_id', |
|
142 | 'user' => 'user_id', | |
143 | 'activity' => 'activity_id', |
|
143 | 'activity' => 'activity_id', | |
144 | 'project' => "#{Project.table_name}.name", |
|
144 | 'project' => "#{Project.table_name}.name", | |
145 | 'issue' => 'issue_id', |
|
145 | 'issue' => 'issue_id', | |
146 | 'hours' => 'hours' |
|
146 | 'hours' => 'hours' | |
147 |
|
147 | |||
148 | cond = ARCondition.new |
|
148 | cond = ARCondition.new | |
149 | if @project.nil? |
|
149 | if @project.nil? | |
150 | cond << Project.allowed_to_condition(User.current, :view_time_entries) |
|
150 | cond << Project.allowed_to_condition(User.current, :view_time_entries) | |
151 | elsif @issue.nil? |
|
151 | elsif @issue.nil? | |
152 | cond << @project.project_condition(Setting.display_subprojects_issues?) |
|
152 | cond << @project.project_condition(Setting.display_subprojects_issues?) | |
153 | else |
|
153 | else | |
154 | cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id] |
|
154 | cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id] | |
155 | end |
|
155 | end | |
156 |
|
156 | |||
157 | retrieve_date_range |
|
157 | retrieve_date_range | |
158 | cond << ['spent_on BETWEEN ? AND ?', @from, @to] |
|
158 | cond << ['spent_on BETWEEN ? AND ?', @from, @to] | |
159 |
|
159 | |||
160 | TimeEntry.visible_by(User.current) do |
|
160 | TimeEntry.visible_by(User.current) do | |
161 | respond_to do |format| |
|
161 | respond_to do |format| | |
162 | format.html { |
|
162 | format.html { | |
163 | # Paginate results |
|
163 | # Paginate results | |
164 | @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions) |
|
164 | @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions) | |
165 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
|
165 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] | |
166 | @entries = TimeEntry.find(:all, |
|
166 | @entries = TimeEntry.find(:all, | |
167 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
167 | :include => [:project, :activity, :user, {:issue => :tracker}], | |
168 | :conditions => cond.conditions, |
|
168 | :conditions => cond.conditions, | |
169 | :order => sort_clause, |
|
169 | :order => sort_clause, | |
170 | :limit => @entry_pages.items_per_page, |
|
170 | :limit => @entry_pages.items_per_page, | |
171 | :offset => @entry_pages.current.offset) |
|
171 | :offset => @entry_pages.current.offset) | |
172 | @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f |
|
172 | @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f | |
173 |
|
173 | |||
174 | render :layout => !request.xhr? |
|
174 | render :layout => !request.xhr? | |
175 | } |
|
175 | } | |
176 | format.atom { |
|
176 | format.atom { | |
177 | entries = TimeEntry.find(:all, |
|
177 | entries = TimeEntry.find(:all, | |
178 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
178 | :include => [:project, :activity, :user, {:issue => :tracker}], | |
179 | :conditions => cond.conditions, |
|
179 | :conditions => cond.conditions, | |
180 | :order => "#{TimeEntry.table_name}.created_on DESC", |
|
180 | :order => "#{TimeEntry.table_name}.created_on DESC", | |
181 | :limit => Setting.feeds_limit.to_i) |
|
181 | :limit => Setting.feeds_limit.to_i) | |
182 | render_feed(entries, :title => l(:label_spent_time)) |
|
182 | render_feed(entries, :title => l(:label_spent_time)) | |
183 | } |
|
183 | } | |
184 | format.csv { |
|
184 | format.csv { | |
185 | # Export all entries |
|
185 | # Export all entries | |
186 | @entries = TimeEntry.find(:all, |
|
186 | @entries = TimeEntry.find(:all, | |
187 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
|
187 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], | |
188 | :conditions => cond.conditions, |
|
188 | :conditions => cond.conditions, | |
189 | :order => sort_clause) |
|
189 | :order => sort_clause) | |
190 | send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
190 | send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') | |
191 | } |
|
191 | } | |
192 | end |
|
192 | end | |
193 | end |
|
193 | end | |
194 | end |
|
194 | end | |
195 |
|
195 | |||
196 | def edit |
|
196 | def edit | |
197 | render_403 and return if @time_entry && !@time_entry.editable_by?(User.current) |
|
197 | render_403 and return if @time_entry && !@time_entry.editable_by?(User.current) | |
198 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) |
|
198 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) | |
199 | @time_entry.attributes = params[:time_entry] |
|
199 | @time_entry.attributes = params[:time_entry] | |
|
200 | ||||
|
201 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |||
|
202 | ||||
200 | if request.post? and @time_entry.save |
|
203 | if request.post? and @time_entry.save | |
201 | flash[:notice] = l(:notice_successful_update) |
|
204 | flash[:notice] = l(:notice_successful_update) | |
202 | redirect_back_or_default :action => 'details', :project_id => @time_entry.project |
|
205 | redirect_back_or_default :action => 'details', :project_id => @time_entry.project | |
203 | return |
|
206 | return | |
204 | end |
|
207 | end | |
205 | end |
|
208 | end | |
206 |
|
209 | |||
207 | def destroy |
|
210 | def destroy | |
208 | render_404 and return unless @time_entry |
|
211 | render_404 and return unless @time_entry | |
209 | render_403 and return unless @time_entry.editable_by?(User.current) |
|
212 | render_403 and return unless @time_entry.editable_by?(User.current) | |
210 | @time_entry.destroy |
|
213 | @time_entry.destroy | |
211 | flash[:notice] = l(:notice_successful_delete) |
|
214 | flash[:notice] = l(:notice_successful_delete) | |
212 | redirect_to :back |
|
215 | redirect_to :back | |
213 | rescue ::ActionController::RedirectBackError |
|
216 | rescue ::ActionController::RedirectBackError | |
214 | redirect_to :action => 'details', :project_id => @time_entry.project |
|
217 | redirect_to :action => 'details', :project_id => @time_entry.project | |
215 | end |
|
218 | end | |
216 |
|
219 | |||
217 | private |
|
220 | private | |
218 | def find_project |
|
221 | def find_project | |
219 | if params[:id] |
|
222 | if params[:id] | |
220 | @time_entry = TimeEntry.find(params[:id]) |
|
223 | @time_entry = TimeEntry.find(params[:id]) | |
221 | @project = @time_entry.project |
|
224 | @project = @time_entry.project | |
222 | elsif params[:issue_id] |
|
225 | elsif params[:issue_id] | |
223 | @issue = Issue.find(params[:issue_id]) |
|
226 | @issue = Issue.find(params[:issue_id]) | |
224 | @project = @issue.project |
|
227 | @project = @issue.project | |
225 | elsif params[:project_id] |
|
228 | elsif params[:project_id] | |
226 | @project = Project.find(params[:project_id]) |
|
229 | @project = Project.find(params[:project_id]) | |
227 | else |
|
230 | else | |
228 | render_404 |
|
231 | render_404 | |
229 | return false |
|
232 | return false | |
230 | end |
|
233 | end | |
231 | rescue ActiveRecord::RecordNotFound |
|
234 | rescue ActiveRecord::RecordNotFound | |
232 | render_404 |
|
235 | render_404 | |
233 | end |
|
236 | end | |
234 |
|
237 | |||
235 | def find_optional_project |
|
238 | def find_optional_project | |
236 | if !params[:issue_id].blank? |
|
239 | if !params[:issue_id].blank? | |
237 | @issue = Issue.find(params[:issue_id]) |
|
240 | @issue = Issue.find(params[:issue_id]) | |
238 | @project = @issue.project |
|
241 | @project = @issue.project | |
239 | elsif !params[:project_id].blank? |
|
242 | elsif !params[:project_id].blank? | |
240 | @project = Project.find(params[:project_id]) |
|
243 | @project = Project.find(params[:project_id]) | |
241 | end |
|
244 | end | |
242 | deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) |
|
245 | deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) | |
243 | end |
|
246 | end | |
244 |
|
247 | |||
245 | # Retrieves the date range based on predefined ranges or specific from/to param dates |
|
248 | # Retrieves the date range based on predefined ranges or specific from/to param dates | |
246 | def retrieve_date_range |
|
249 | def retrieve_date_range | |
247 | @free_period = false |
|
250 | @free_period = false | |
248 | @from, @to = nil, nil |
|
251 | @from, @to = nil, nil | |
249 |
|
252 | |||
250 | if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) |
|
253 | if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) | |
251 | case params[:period].to_s |
|
254 | case params[:period].to_s | |
252 | when 'today' |
|
255 | when 'today' | |
253 | @from = @to = Date.today |
|
256 | @from = @to = Date.today | |
254 | when 'yesterday' |
|
257 | when 'yesterday' | |
255 | @from = @to = Date.today - 1 |
|
258 | @from = @to = Date.today - 1 | |
256 | when 'current_week' |
|
259 | when 'current_week' | |
257 | @from = Date.today - (Date.today.cwday - 1)%7 |
|
260 | @from = Date.today - (Date.today.cwday - 1)%7 | |
258 | @to = @from + 6 |
|
261 | @to = @from + 6 | |
259 | when 'last_week' |
|
262 | when 'last_week' | |
260 | @from = Date.today - 7 - (Date.today.cwday - 1)%7 |
|
263 | @from = Date.today - 7 - (Date.today.cwday - 1)%7 | |
261 | @to = @from + 6 |
|
264 | @to = @from + 6 | |
262 | when '7_days' |
|
265 | when '7_days' | |
263 | @from = Date.today - 7 |
|
266 | @from = Date.today - 7 | |
264 | @to = Date.today |
|
267 | @to = Date.today | |
265 | when 'current_month' |
|
268 | when 'current_month' | |
266 | @from = Date.civil(Date.today.year, Date.today.month, 1) |
|
269 | @from = Date.civil(Date.today.year, Date.today.month, 1) | |
267 | @to = (@from >> 1) - 1 |
|
270 | @to = (@from >> 1) - 1 | |
268 | when 'last_month' |
|
271 | when 'last_month' | |
269 | @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 |
|
272 | @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 | |
270 | @to = (@from >> 1) - 1 |
|
273 | @to = (@from >> 1) - 1 | |
271 | when '30_days' |
|
274 | when '30_days' | |
272 | @from = Date.today - 30 |
|
275 | @from = Date.today - 30 | |
273 | @to = Date.today |
|
276 | @to = Date.today | |
274 | when 'current_year' |
|
277 | when 'current_year' | |
275 | @from = Date.civil(Date.today.year, 1, 1) |
|
278 | @from = Date.civil(Date.today.year, 1, 1) | |
276 | @to = Date.civil(Date.today.year, 12, 31) |
|
279 | @to = Date.civil(Date.today.year, 12, 31) | |
277 | end |
|
280 | end | |
278 | elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) |
|
281 | elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) | |
279 | begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end |
|
282 | begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end | |
280 | begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end |
|
283 | begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end | |
281 | @free_period = true |
|
284 | @free_period = true | |
282 | else |
|
285 | else | |
283 | # default |
|
286 | # default | |
284 | end |
|
287 | end | |
285 |
|
288 | |||
286 | @from, @to = @to, @from if @from && @to && @from > @to |
|
289 | @from, @to = @to, @from if @from && @to && @from > @to | |
287 | @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1 |
|
290 | @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1 | |
288 | @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) |
|
291 | @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) | |
289 | end |
|
292 | end | |
290 | end |
|
293 | end |
General Comments 0
You need to be logged in to leave comments.
Login now