@@ -0,0 +1,40 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../test_helper', __FILE__) | |||
|
19 | ||||
|
20 | class TimeEntryQueryTest < ActiveSupport::TestCase | |||
|
21 | fixtures :projects, :users, :enumerations | |||
|
22 | ||||
|
23 | def test_activity_filter_should_consider_system_and_project_activities | |||
|
24 | TimeEntry.delete_all | |||
|
25 | system = TimeEntryActivity.create!(:name => 'Foo') | |||
|
26 | override = TimeEntryActivity.create!(:name => 'Foo', :parent_id => system.id, :project_id => 1) | |||
|
27 | other = TimeEntryActivity.create!(:name => 'Bar') | |||
|
28 | TimeEntry.generate!(:activity => system, :hours => 1.0) | |||
|
29 | TimeEntry.generate!(:activity => override, :hours => 2.0) | |||
|
30 | TimeEntry.generate!(:activity => other, :hours => 4.0) | |||
|
31 | ||||
|
32 | query = TimeEntryQuery.new(:name => '_') | |||
|
33 | query.add_filter('activity_id', '=', [system.id.to_s]) | |||
|
34 | assert_equal 3.0, query.results_scope.sum(:hours) | |||
|
35 | ||||
|
36 | query = TimeEntryQuery.new(:name => '_') | |||
|
37 | query.add_filter('activity_id', '!', [system.id.to_s]) | |||
|
38 | assert_equal 4.0, query.results_scope.sum(:hours) | |||
|
39 | end | |||
|
40 | end |
@@ -1,297 +1,297 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 |
|
20 | |||
21 | before_filter :find_project_for_new_time_entry, :only => [:create] |
|
21 | before_filter :find_project_for_new_time_entry, :only => [:create] | |
22 | before_filter :find_time_entry, :only => [:show, :edit, :update] |
|
22 | before_filter :find_time_entry, :only => [:show, :edit, :update] | |
23 | before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] |
|
23 | before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] | |
24 | before_filter :authorize, :except => [:new, :index, :report] |
|
24 | before_filter :authorize, :except => [:new, :index, :report] | |
25 |
|
25 | |||
26 | before_filter :find_optional_project, :only => [:index, :report] |
|
26 | before_filter :find_optional_project, :only => [:index, :report] | |
27 | before_filter :find_optional_project_for_new_time_entry, :only => [:new] |
|
27 | before_filter :find_optional_project_for_new_time_entry, :only => [:new] | |
28 | before_filter :authorize_global, :only => [:new, :index, :report] |
|
28 | before_filter :authorize_global, :only => [:new, :index, :report] | |
29 |
|
29 | |||
30 | accept_rss_auth :index |
|
30 | accept_rss_auth :index | |
31 | accept_api_auth :index, :show, :create, :update, :destroy |
|
31 | accept_api_auth :index, :show, :create, :update, :destroy | |
32 |
|
32 | |||
33 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
33 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid | |
34 |
|
34 | |||
35 | helper :sort |
|
35 | helper :sort | |
36 | include SortHelper |
|
36 | include SortHelper | |
37 | helper :issues |
|
37 | helper :issues | |
38 | include TimelogHelper |
|
38 | include TimelogHelper | |
39 | helper :custom_fields |
|
39 | helper :custom_fields | |
40 | include CustomFieldsHelper |
|
40 | include CustomFieldsHelper | |
41 | helper :queries |
|
41 | helper :queries | |
42 | include QueriesHelper |
|
42 | include QueriesHelper | |
43 |
|
43 | |||
44 | def index |
|
44 | def index | |
45 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') |
|
45 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') | |
46 |
|
46 | |||
47 | sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria) |
|
47 | sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria) | |
48 | sort_update(@query.sortable_columns) |
|
48 | sort_update(@query.sortable_columns) | |
49 | scope = time_entry_scope(:order => sort_clause). |
|
49 | scope = time_entry_scope(:order => sort_clause). | |
50 |
includes(:project |
|
50 | includes(:project, :user, :issue). | |
51 | preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]) |
|
51 | preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]) | |
52 |
|
52 | |||
53 | respond_to do |format| |
|
53 | respond_to do |format| | |
54 | format.html { |
|
54 | format.html { | |
55 | @entry_count = scope.count |
|
55 | @entry_count = scope.count | |
56 | @entry_pages = Paginator.new @entry_count, per_page_option, params['page'] |
|
56 | @entry_pages = Paginator.new @entry_count, per_page_option, params['page'] | |
57 | @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).all |
|
57 | @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).all | |
58 | @total_hours = scope.sum(:hours).to_f |
|
58 | @total_hours = scope.sum(:hours).to_f | |
59 |
|
59 | |||
60 | render :layout => !request.xhr? |
|
60 | render :layout => !request.xhr? | |
61 | } |
|
61 | } | |
62 | format.api { |
|
62 | format.api { | |
63 | @entry_count = scope.count |
|
63 | @entry_count = scope.count | |
64 | @offset, @limit = api_offset_and_limit |
|
64 | @offset, @limit = api_offset_and_limit | |
65 | @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).all |
|
65 | @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).all | |
66 | } |
|
66 | } | |
67 | format.atom { |
|
67 | format.atom { | |
68 | entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").all |
|
68 | entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").all | |
69 | render_feed(entries, :title => l(:label_spent_time)) |
|
69 | render_feed(entries, :title => l(:label_spent_time)) | |
70 | } |
|
70 | } | |
71 | format.csv { |
|
71 | format.csv { | |
72 | # Export all entries |
|
72 | # Export all entries | |
73 | @entries = scope.all |
|
73 | @entries = scope.all | |
74 | send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
74 | send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv') | |
75 | } |
|
75 | } | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def report |
|
79 | def report | |
80 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') |
|
80 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') | |
81 | scope = time_entry_scope |
|
81 | scope = time_entry_scope | |
82 |
|
82 | |||
83 | @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope) |
|
83 | @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope) | |
84 |
|
84 | |||
85 | respond_to do |format| |
|
85 | respond_to do |format| | |
86 | format.html { render :layout => !request.xhr? } |
|
86 | format.html { render :layout => !request.xhr? } | |
87 | format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
87 | format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') } | |
88 | end |
|
88 | end | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | def show |
|
91 | def show | |
92 | respond_to do |format| |
|
92 | respond_to do |format| | |
93 | # TODO: Implement html response |
|
93 | # TODO: Implement html response | |
94 | format.html { render :nothing => true, :status => 406 } |
|
94 | format.html { render :nothing => true, :status => 406 } | |
95 | format.api |
|
95 | format.api | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def new |
|
99 | def new | |
100 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
100 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) | |
101 | @time_entry.safe_attributes = params[:time_entry] |
|
101 | @time_entry.safe_attributes = params[:time_entry] | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | def create |
|
104 | def create | |
105 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
105 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) | |
106 | @time_entry.safe_attributes = params[:time_entry] |
|
106 | @time_entry.safe_attributes = params[:time_entry] | |
107 |
|
107 | |||
108 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
108 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |
109 |
|
109 | |||
110 | if @time_entry.save |
|
110 | if @time_entry.save | |
111 | respond_to do |format| |
|
111 | respond_to do |format| | |
112 | format.html { |
|
112 | format.html { | |
113 | flash[:notice] = l(:notice_successful_create) |
|
113 | flash[:notice] = l(:notice_successful_create) | |
114 | if params[:continue] |
|
114 | if params[:continue] | |
115 | if params[:project_id] |
|
115 | if params[:project_id] | |
116 | options = { |
|
116 | options = { | |
117 | :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, |
|
117 | :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, | |
118 | :back_url => params[:back_url] |
|
118 | :back_url => params[:back_url] | |
119 | } |
|
119 | } | |
120 | if @time_entry.issue |
|
120 | if @time_entry.issue | |
121 | redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options) |
|
121 | redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options) | |
122 | else |
|
122 | else | |
123 | redirect_to new_project_time_entry_path(@time_entry.project, options) |
|
123 | redirect_to new_project_time_entry_path(@time_entry.project, options) | |
124 | end |
|
124 | end | |
125 | else |
|
125 | else | |
126 | options = { |
|
126 | options = { | |
127 | :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, |
|
127 | :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, | |
128 | :back_url => params[:back_url] |
|
128 | :back_url => params[:back_url] | |
129 | } |
|
129 | } | |
130 | redirect_to new_time_entry_path(options) |
|
130 | redirect_to new_time_entry_path(options) | |
131 | end |
|
131 | end | |
132 | else |
|
132 | else | |
133 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
133 | redirect_back_or_default project_time_entries_path(@time_entry.project) | |
134 | end |
|
134 | end | |
135 | } |
|
135 | } | |
136 | format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } |
|
136 | format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } | |
137 | end |
|
137 | end | |
138 | else |
|
138 | else | |
139 | respond_to do |format| |
|
139 | respond_to do |format| | |
140 | format.html { render :action => 'new' } |
|
140 | format.html { render :action => 'new' } | |
141 | format.api { render_validation_errors(@time_entry) } |
|
141 | format.api { render_validation_errors(@time_entry) } | |
142 | end |
|
142 | end | |
143 | end |
|
143 | end | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | def edit |
|
146 | def edit | |
147 | @time_entry.safe_attributes = params[:time_entry] |
|
147 | @time_entry.safe_attributes = params[:time_entry] | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | def update |
|
150 | def update | |
151 | @time_entry.safe_attributes = params[:time_entry] |
|
151 | @time_entry.safe_attributes = params[:time_entry] | |
152 |
|
152 | |||
153 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
153 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |
154 |
|
154 | |||
155 | if @time_entry.save |
|
155 | if @time_entry.save | |
156 | respond_to do |format| |
|
156 | respond_to do |format| | |
157 | format.html { |
|
157 | format.html { | |
158 | flash[:notice] = l(:notice_successful_update) |
|
158 | flash[:notice] = l(:notice_successful_update) | |
159 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
159 | redirect_back_or_default project_time_entries_path(@time_entry.project) | |
160 | } |
|
160 | } | |
161 | format.api { render_api_ok } |
|
161 | format.api { render_api_ok } | |
162 | end |
|
162 | end | |
163 | else |
|
163 | else | |
164 | respond_to do |format| |
|
164 | respond_to do |format| | |
165 | format.html { render :action => 'edit' } |
|
165 | format.html { render :action => 'edit' } | |
166 | format.api { render_validation_errors(@time_entry) } |
|
166 | format.api { render_validation_errors(@time_entry) } | |
167 | end |
|
167 | end | |
168 | end |
|
168 | end | |
169 | end |
|
169 | end | |
170 |
|
170 | |||
171 | def bulk_edit |
|
171 | def bulk_edit | |
172 | @available_activities = TimeEntryActivity.shared.active |
|
172 | @available_activities = TimeEntryActivity.shared.active | |
173 | @custom_fields = TimeEntry.first.available_custom_fields |
|
173 | @custom_fields = TimeEntry.first.available_custom_fields | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | def bulk_update |
|
176 | def bulk_update | |
177 | attributes = parse_params_for_bulk_time_entry_attributes(params) |
|
177 | attributes = parse_params_for_bulk_time_entry_attributes(params) | |
178 |
|
178 | |||
179 | unsaved_time_entry_ids = [] |
|
179 | unsaved_time_entry_ids = [] | |
180 | @time_entries.each do |time_entry| |
|
180 | @time_entries.each do |time_entry| | |
181 | time_entry.reload |
|
181 | time_entry.reload | |
182 | time_entry.safe_attributes = attributes |
|
182 | time_entry.safe_attributes = attributes | |
183 | call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) |
|
183 | call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) | |
184 | unless time_entry.save |
|
184 | unless time_entry.save | |
185 | logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info |
|
185 | logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info | |
186 | # Keep unsaved time_entry ids to display them in flash error |
|
186 | # Keep unsaved time_entry ids to display them in flash error | |
187 | unsaved_time_entry_ids << time_entry.id |
|
187 | unsaved_time_entry_ids << time_entry.id | |
188 | end |
|
188 | end | |
189 | end |
|
189 | end | |
190 | set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) |
|
190 | set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) | |
191 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
191 | redirect_back_or_default project_time_entries_path(@projects.first) | |
192 | end |
|
192 | end | |
193 |
|
193 | |||
194 | def destroy |
|
194 | def destroy | |
195 | destroyed = TimeEntry.transaction do |
|
195 | destroyed = TimeEntry.transaction do | |
196 | @time_entries.each do |t| |
|
196 | @time_entries.each do |t| | |
197 | unless t.destroy && t.destroyed? |
|
197 | unless t.destroy && t.destroyed? | |
198 | raise ActiveRecord::Rollback |
|
198 | raise ActiveRecord::Rollback | |
199 | end |
|
199 | end | |
200 | end |
|
200 | end | |
201 | end |
|
201 | end | |
202 |
|
202 | |||
203 | respond_to do |format| |
|
203 | respond_to do |format| | |
204 | format.html { |
|
204 | format.html { | |
205 | if destroyed |
|
205 | if destroyed | |
206 | flash[:notice] = l(:notice_successful_delete) |
|
206 | flash[:notice] = l(:notice_successful_delete) | |
207 | else |
|
207 | else | |
208 | flash[:error] = l(:notice_unable_delete_time_entry) |
|
208 | flash[:error] = l(:notice_unable_delete_time_entry) | |
209 | end |
|
209 | end | |
210 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
210 | redirect_back_or_default project_time_entries_path(@projects.first) | |
211 | } |
|
211 | } | |
212 | format.api { |
|
212 | format.api { | |
213 | if destroyed |
|
213 | if destroyed | |
214 | render_api_ok |
|
214 | render_api_ok | |
215 | else |
|
215 | else | |
216 | render_validation_errors(@time_entries) |
|
216 | render_validation_errors(@time_entries) | |
217 | end |
|
217 | end | |
218 | } |
|
218 | } | |
219 | end |
|
219 | end | |
220 | end |
|
220 | end | |
221 |
|
221 | |||
222 | private |
|
222 | private | |
223 | def find_time_entry |
|
223 | def find_time_entry | |
224 | @time_entry = TimeEntry.find(params[:id]) |
|
224 | @time_entry = TimeEntry.find(params[:id]) | |
225 | unless @time_entry.editable_by?(User.current) |
|
225 | unless @time_entry.editable_by?(User.current) | |
226 | render_403 |
|
226 | render_403 | |
227 | return false |
|
227 | return false | |
228 | end |
|
228 | end | |
229 | @project = @time_entry.project |
|
229 | @project = @time_entry.project | |
230 | rescue ActiveRecord::RecordNotFound |
|
230 | rescue ActiveRecord::RecordNotFound | |
231 | render_404 |
|
231 | render_404 | |
232 | end |
|
232 | end | |
233 |
|
233 | |||
234 | def find_time_entries |
|
234 | def find_time_entries | |
235 | @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) |
|
235 | @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) | |
236 | raise ActiveRecord::RecordNotFound if @time_entries.empty? |
|
236 | raise ActiveRecord::RecordNotFound if @time_entries.empty? | |
237 | @projects = @time_entries.collect(&:project).compact.uniq |
|
237 | @projects = @time_entries.collect(&:project).compact.uniq | |
238 | @project = @projects.first if @projects.size == 1 |
|
238 | @project = @projects.first if @projects.size == 1 | |
239 | rescue ActiveRecord::RecordNotFound |
|
239 | rescue ActiveRecord::RecordNotFound | |
240 | render_404 |
|
240 | render_404 | |
241 | end |
|
241 | end | |
242 |
|
242 | |||
243 | def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) |
|
243 | def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) | |
244 | if unsaved_time_entry_ids.empty? |
|
244 | if unsaved_time_entry_ids.empty? | |
245 | flash[:notice] = l(:notice_successful_update) unless time_entries.empty? |
|
245 | flash[:notice] = l(:notice_successful_update) unless time_entries.empty? | |
246 | else |
|
246 | else | |
247 | flash[:error] = l(:notice_failed_to_save_time_entries, |
|
247 | flash[:error] = l(:notice_failed_to_save_time_entries, | |
248 | :count => unsaved_time_entry_ids.size, |
|
248 | :count => unsaved_time_entry_ids.size, | |
249 | :total => time_entries.size, |
|
249 | :total => time_entries.size, | |
250 | :ids => '#' + unsaved_time_entry_ids.join(', #')) |
|
250 | :ids => '#' + unsaved_time_entry_ids.join(', #')) | |
251 | end |
|
251 | end | |
252 | end |
|
252 | end | |
253 |
|
253 | |||
254 | def find_optional_project_for_new_time_entry |
|
254 | def find_optional_project_for_new_time_entry | |
255 | if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? |
|
255 | if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? | |
256 | @project = Project.find(project_id) |
|
256 | @project = Project.find(project_id) | |
257 | end |
|
257 | end | |
258 | if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? |
|
258 | if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? | |
259 | @issue = Issue.find(issue_id) |
|
259 | @issue = Issue.find(issue_id) | |
260 | @project ||= @issue.project |
|
260 | @project ||= @issue.project | |
261 | end |
|
261 | end | |
262 | rescue ActiveRecord::RecordNotFound |
|
262 | rescue ActiveRecord::RecordNotFound | |
263 | render_404 |
|
263 | render_404 | |
264 | end |
|
264 | end | |
265 |
|
265 | |||
266 | def find_project_for_new_time_entry |
|
266 | def find_project_for_new_time_entry | |
267 | find_optional_project_for_new_time_entry |
|
267 | find_optional_project_for_new_time_entry | |
268 | if @project.nil? |
|
268 | if @project.nil? | |
269 | render_404 |
|
269 | render_404 | |
270 | end |
|
270 | end | |
271 | end |
|
271 | end | |
272 |
|
272 | |||
273 | def find_optional_project |
|
273 | def find_optional_project | |
274 | if !params[:issue_id].blank? |
|
274 | if !params[:issue_id].blank? | |
275 | @issue = Issue.find(params[:issue_id]) |
|
275 | @issue = Issue.find(params[:issue_id]) | |
276 | @project = @issue.project |
|
276 | @project = @issue.project | |
277 | elsif !params[:project_id].blank? |
|
277 | elsif !params[:project_id].blank? | |
278 | @project = Project.find(params[:project_id]) |
|
278 | @project = Project.find(params[:project_id]) | |
279 | end |
|
279 | end | |
280 | end |
|
280 | end | |
281 |
|
281 | |||
282 | # Returns the TimeEntry scope for index and report actions |
|
282 | # Returns the TimeEntry scope for index and report actions | |
283 | def time_entry_scope(options={}) |
|
283 | def time_entry_scope(options={}) | |
284 | scope = @query.results_scope(options) |
|
284 | scope = @query.results_scope(options) | |
285 | if @issue |
|
285 | if @issue | |
286 | scope = scope.on_issue(@issue) |
|
286 | scope = scope.on_issue(@issue) | |
287 | end |
|
287 | end | |
288 | scope |
|
288 | scope | |
289 | end |
|
289 | end | |
290 |
|
290 | |||
291 | def parse_params_for_bulk_time_entry_attributes(params) |
|
291 | def parse_params_for_bulk_time_entry_attributes(params) | |
292 | attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} |
|
292 | attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} | |
293 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
293 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} | |
294 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
294 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] | |
295 | attributes |
|
295 | attributes | |
296 | end |
|
296 | end | |
297 | end |
|
297 | end |
@@ -1,124 +1,137 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 TimeEntryQuery < Query |
|
18 | class TimeEntryQuery < Query | |
19 |
|
19 | |||
20 | self.queried_class = TimeEntry |
|
20 | self.queried_class = TimeEntry | |
21 |
|
21 | |||
22 | self.available_columns = [ |
|
22 | self.available_columns = [ | |
23 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
23 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), | |
24 | QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true), |
|
24 | QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true), | |
25 | QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
25 | QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), | |
26 | QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true), |
|
26 | QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true), | |
27 | QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"), |
|
27 | QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"), | |
28 | QueryColumn.new(:comments), |
|
28 | QueryColumn.new(:comments), | |
29 | QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"), |
|
29 | QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"), | |
30 | ] |
|
30 | ] | |
31 |
|
31 | |||
32 | def initialize(attributes=nil, *args) |
|
32 | def initialize(attributes=nil, *args) | |
33 | super attributes |
|
33 | super attributes | |
34 | self.filters ||= {} |
|
34 | self.filters ||= {} | |
35 | add_filter('spent_on', '*') unless filters.present? |
|
35 | add_filter('spent_on', '*') unless filters.present? | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def initialize_available_filters |
|
38 | def initialize_available_filters | |
39 | add_available_filter "spent_on", :type => :date_past |
|
39 | add_available_filter "spent_on", :type => :date_past | |
40 |
|
40 | |||
41 | principals = [] |
|
41 | principals = [] | |
42 | if project |
|
42 | if project | |
43 | principals += project.principals.sort |
|
43 | principals += project.principals.sort | |
44 | unless project.leaf? |
|
44 | unless project.leaf? | |
45 | subprojects = project.descendants.visible.all |
|
45 | subprojects = project.descendants.visible.all | |
46 | if subprojects.any? |
|
46 | if subprojects.any? | |
47 | add_available_filter "subproject_id", |
|
47 | add_available_filter "subproject_id", | |
48 | :type => :list_subprojects, |
|
48 | :type => :list_subprojects, | |
49 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } |
|
49 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } | |
50 | principals += Principal.member_of(subprojects) |
|
50 | principals += Principal.member_of(subprojects) | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 | else |
|
53 | else | |
54 | if all_projects.any? |
|
54 | if all_projects.any? | |
55 | # members of visible projects |
|
55 | # members of visible projects | |
56 | principals += Principal.member_of(all_projects) |
|
56 | principals += Principal.member_of(all_projects) | |
57 | # project filter |
|
57 | # project filter | |
58 | project_values = [] |
|
58 | project_values = [] | |
59 | if User.current.logged? && User.current.memberships.any? |
|
59 | if User.current.logged? && User.current.memberships.any? | |
60 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
60 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] | |
61 | end |
|
61 | end | |
62 | project_values += all_projects_values |
|
62 | project_values += all_projects_values | |
63 | add_available_filter("project_id", |
|
63 | add_available_filter("project_id", | |
64 | :type => :list, :values => project_values |
|
64 | :type => :list, :values => project_values | |
65 | ) unless project_values.empty? |
|
65 | ) unless project_values.empty? | |
66 | end |
|
66 | end | |
67 | end |
|
67 | end | |
68 | principals.uniq! |
|
68 | principals.uniq! | |
69 | principals.sort! |
|
69 | principals.sort! | |
70 | users = principals.select {|p| p.is_a?(User)} |
|
70 | users = principals.select {|p| p.is_a?(User)} | |
71 |
|
71 | |||
72 | users_values = [] |
|
72 | users_values = [] | |
73 | users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
73 | users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
74 | users_values += users.collect{|s| [s.name, s.id.to_s] } |
|
74 | users_values += users.collect{|s| [s.name, s.id.to_s] } | |
75 | add_available_filter("user_id", |
|
75 | add_available_filter("user_id", | |
76 | :type => :list_optional, :values => users_values |
|
76 | :type => :list_optional, :values => users_values | |
77 | ) unless users_values.empty? |
|
77 | ) unless users_values.empty? | |
78 |
|
78 | |||
79 | activities = (project ? project.activities : TimeEntryActivity.shared.active) |
|
79 | activities = (project ? project.activities : TimeEntryActivity.shared.active) | |
80 | add_available_filter("activity_id", |
|
80 | add_available_filter("activity_id", | |
81 | :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]} |
|
81 | :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]} | |
82 | ) unless activities.empty? |
|
82 | ) unless activities.empty? | |
83 |
|
83 | |||
84 | add_available_filter "comments", :type => :text |
|
84 | add_available_filter "comments", :type => :text | |
85 | add_available_filter "hours", :type => :float |
|
85 | add_available_filter "hours", :type => :float | |
86 |
|
86 | |||
87 | add_custom_fields_filters(TimeEntryCustomField) |
|
87 | add_custom_fields_filters(TimeEntryCustomField) | |
88 | add_associations_custom_fields_filters :project, :issue, :user |
|
88 | add_associations_custom_fields_filters :project, :issue, :user | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | def available_columns |
|
91 | def available_columns | |
92 | return @available_columns if @available_columns |
|
92 | return @available_columns if @available_columns | |
93 | @available_columns = self.class.available_columns.dup |
|
93 | @available_columns = self.class.available_columns.dup | |
94 | @available_columns += TimeEntryCustomField.visible.all.map {|cf| QueryCustomFieldColumn.new(cf) } |
|
94 | @available_columns += TimeEntryCustomField.visible.all.map {|cf| QueryCustomFieldColumn.new(cf) } | |
95 | @available_columns += IssueCustomField.visible.all.map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) } |
|
95 | @available_columns += IssueCustomField.visible.all.map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) } | |
96 | @available_columns |
|
96 | @available_columns | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def default_columns_names |
|
99 | def default_columns_names | |
100 | @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours] |
|
100 | @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours] | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def results_scope(options={}) |
|
103 | def results_scope(options={}) | |
104 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
104 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) | |
105 |
|
105 | |||
106 | TimeEntry.visible. |
|
106 | TimeEntry.visible. | |
107 | where(statement). |
|
107 | where(statement). | |
108 | order(order_option). |
|
108 | order(order_option). | |
109 | joins(joins_for_order_statement(order_option.join(','))) |
|
109 | joins(joins_for_order_statement(order_option.join(','))). | |
|
110 | includes(:activity) | |||
|
111 | end | |||
|
112 | ||||
|
113 | def sql_for_activity_id_field(field, operator, value) | |||
|
114 | condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id') | |||
|
115 | condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id') | |||
|
116 | ids = value.map(&:to_i).join(',') | |||
|
117 | table_name = Enumeration.table_name | |||
|
118 | if operator == '=' | |||
|
119 | "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))" | |||
|
120 | else | |||
|
121 | "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))" | |||
|
122 | end | |||
110 | end |
|
123 | end | |
111 |
|
124 | |||
112 | # Accepts :from/:to params as shortcut filters |
|
125 | # Accepts :from/:to params as shortcut filters | |
113 | def build_from_params(params) |
|
126 | def build_from_params(params) | |
114 | super |
|
127 | super | |
115 | if params[:from].present? && params[:to].present? |
|
128 | if params[:from].present? && params[:to].present? | |
116 | add_filter('spent_on', '><', [params[:from], params[:to]]) |
|
129 | add_filter('spent_on', '><', [params[:from], params[:to]]) | |
117 | elsif params[:from].present? |
|
130 | elsif params[:from].present? | |
118 | add_filter('spent_on', '>=', [params[:from]]) |
|
131 | add_filter('spent_on', '>=', [params[:from]]) | |
119 | elsif params[:to].present? |
|
132 | elsif params[:to].present? | |
120 | add_filter('spent_on', '<=', [params[:to]]) |
|
133 | add_filter('spent_on', '<=', [params[:to]]) | |
121 | end |
|
134 | end | |
122 | self |
|
135 | self | |
123 | end |
|
136 | end | |
124 | end |
|
137 | end |
General Comments 0
You need to be logged in to leave comments.
Login now