@@ -1,152 +1,152 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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 | module Redmine |
|
18 | module Redmine | |
19 | module Helpers |
|
19 | module Helpers | |
20 | class TimeReport |
|
20 | class TimeReport | |
21 | attr_reader :criteria, :columns, :hours, :total_hours, :periods |
|
21 | attr_reader :criteria, :columns, :hours, :total_hours, :periods | |
22 |
|
22 | |||
23 | def initialize(project, issue, criteria, columns, time_entry_scope) |
|
23 | def initialize(project, issue, criteria, columns, time_entry_scope) | |
24 | @project = project |
|
24 | @project = project | |
25 | @issue = issue |
|
25 | @issue = issue | |
26 |
|
26 | |||
27 | @criteria = criteria || [] |
|
27 | @criteria = criteria || [] | |
28 | @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria} |
|
28 | @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria} | |
29 | @criteria.uniq! |
|
29 | @criteria.uniq! | |
30 | @criteria = @criteria[0,3] |
|
30 | @criteria = @criteria[0,3] | |
31 |
|
31 | |||
32 | @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month' |
|
32 | @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month' | |
33 | @scope = time_entry_scope |
|
33 | @scope = time_entry_scope | |
34 |
|
34 | |||
35 | run |
|
35 | run | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def available_criteria |
|
38 | def available_criteria | |
39 | @available_criteria || load_available_criteria |
|
39 | @available_criteria || load_available_criteria | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | private |
|
42 | private | |
43 |
|
43 | |||
44 | def run |
|
44 | def run | |
45 | unless @criteria.empty? |
|
45 | unless @criteria.empty? | |
46 | time_columns = %w(tyear tmonth tweek spent_on) |
|
46 | time_columns = %w(tyear tmonth tweek spent_on) | |
47 | @hours = [] |
|
47 | @hours = [] | |
48 | @scope.includes(:issue, :activity). |
|
48 | @scope.includes(:issue, :activity). | |
49 | group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns). |
|
49 | group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns). | |
50 | joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact). |
|
50 | joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact). | |
51 | sum(:hours).each do |hash, hours| |
|
51 | sum(:hours).each do |hash, hours| | |
52 | h = {'hours' => hours} |
|
52 | h = {'hours' => hours} | |
53 | (@criteria + time_columns).each_with_index do |name, i| |
|
53 | (@criteria + time_columns).each_with_index do |name, i| | |
54 | h[name] = hash[i] |
|
54 | h[name] = hash[i] | |
55 | end |
|
55 | end | |
56 | @hours << h |
|
56 | @hours << h | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | @hours.each do |row| |
|
59 | @hours.each do |row| | |
60 | case @columns |
|
60 | case @columns | |
61 | when 'year' |
|
61 | when 'year' | |
62 | row['year'] = row['tyear'] |
|
62 | row['year'] = row['tyear'] | |
63 | when 'month' |
|
63 | when 'month' | |
64 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" |
|
64 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" | |
65 | when 'week' |
|
65 | when 'week' | |
66 | row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}" |
|
66 | row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}" | |
67 | when 'day' |
|
67 | when 'day' | |
68 | row['day'] = "#{row['spent_on']}" |
|
68 | row['day'] = "#{row['spent_on']}" | |
69 | end |
|
69 | end | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | min = @hours.collect {|row| row['spent_on']}.min |
|
72 | min = @hours.collect {|row| row['spent_on']}.min | |
73 | @from = min ? min.to_date : User.current.today |
|
73 | @from = min ? min.to_date : User.current.today | |
74 |
|
74 | |||
75 | max = @hours.collect {|row| row['spent_on']}.max |
|
75 | max = @hours.collect {|row| row['spent_on']}.max | |
76 | @to = max ? max.to_date : User.current.today |
|
76 | @to = max ? max.to_date : User.current.today | |
77 |
|
77 | |||
78 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} |
|
78 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} | |
79 |
|
79 | |||
80 | @periods = [] |
|
80 | @periods = [] | |
81 | # Date#at_beginning_of_ not supported in Rails 1.2.x |
|
81 | # Date#at_beginning_of_ not supported in Rails 1.2.x | |
82 | date_from = @from.to_time |
|
82 | date_from = @from.to_time | |
83 | # 100 columns max |
|
83 | # 100 columns max | |
84 | while date_from <= @to.to_time && @periods.length < 100 |
|
84 | while date_from <= @to.to_time && @periods.length < 100 | |
85 | case @columns |
|
85 | case @columns | |
86 | when 'year' |
|
86 | when 'year' | |
87 | @periods << "#{date_from.year}" |
|
87 | @periods << "#{date_from.year}" | |
88 | date_from = (date_from + 1.year).at_beginning_of_year |
|
88 | date_from = (date_from + 1.year).at_beginning_of_year | |
89 | when 'month' |
|
89 | when 'month' | |
90 | @periods << "#{date_from.year}-#{date_from.month}" |
|
90 | @periods << "#{date_from.year}-#{date_from.month}" | |
91 | date_from = (date_from + 1.month).at_beginning_of_month |
|
91 | date_from = (date_from + 1.month).at_beginning_of_month | |
92 | when 'week' |
|
92 | when 'week' | |
93 | @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}" |
|
93 | @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}" | |
94 | date_from = (date_from + 7.day).at_beginning_of_week |
|
94 | date_from = (date_from + 7.day).at_beginning_of_week | |
95 | when 'day' |
|
95 | when 'day' | |
96 | @periods << "#{date_from.to_date}" |
|
96 | @periods << "#{date_from.to_date}" | |
97 | date_from = date_from + 1.day |
|
97 | date_from = date_from + 1.day | |
98 | end |
|
98 | end | |
99 | end |
|
99 | end | |
100 | end |
|
100 | end | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def load_available_criteria |
|
103 | def load_available_criteria | |
104 | @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", |
|
104 | @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", | |
105 | :klass => Project, |
|
105 | :klass => Project, | |
106 | :label => :label_project}, |
|
106 | :label => :label_project}, | |
107 | 'status' => {:sql => "#{Issue.table_name}.status_id", |
|
107 | 'status' => {:sql => "#{Issue.table_name}.status_id", | |
108 | :klass => IssueStatus, |
|
108 | :klass => IssueStatus, | |
109 | :label => :field_status}, |
|
109 | :label => :field_status}, | |
110 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", |
|
110 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", | |
111 | :klass => Version, |
|
111 | :klass => ::Version, | |
112 | :label => :label_version}, |
|
112 | :label => :label_version}, | |
113 | 'category' => {:sql => "#{Issue.table_name}.category_id", |
|
113 | 'category' => {:sql => "#{Issue.table_name}.category_id", | |
114 | :klass => IssueCategory, |
|
114 | :klass => IssueCategory, | |
115 | :label => :field_category}, |
|
115 | :label => :field_category}, | |
116 | 'user' => {:sql => "#{TimeEntry.table_name}.user_id", |
|
116 | 'user' => {:sql => "#{TimeEntry.table_name}.user_id", | |
117 | :klass => User, |
|
117 | :klass => User, | |
118 | :label => :label_user}, |
|
118 | :label => :label_user}, | |
119 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", |
|
119 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", | |
120 | :klass => Tracker, |
|
120 | :klass => Tracker, | |
121 | :label => :label_tracker}, |
|
121 | :label => :label_tracker}, | |
122 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", |
|
122 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", | |
123 | :klass => TimeEntryActivity, |
|
123 | :klass => TimeEntryActivity, | |
124 | :label => :label_activity}, |
|
124 | :label => :label_activity}, | |
125 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", |
|
125 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", | |
126 | :klass => Issue, |
|
126 | :klass => Issue, | |
127 | :label => :label_issue} |
|
127 | :label => :label_issue} | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | # Add time entry custom fields |
|
130 | # Add time entry custom fields | |
131 | custom_fields = TimeEntryCustomField.all |
|
131 | custom_fields = TimeEntryCustomField.all | |
132 | # Add project custom fields |
|
132 | # Add project custom fields | |
133 | custom_fields += ProjectCustomField.all |
|
133 | custom_fields += ProjectCustomField.all | |
134 | # Add issue custom fields |
|
134 | # Add issue custom fields | |
135 | custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) |
|
135 | custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) | |
136 | # Add time entry activity custom fields |
|
136 | # Add time entry activity custom fields | |
137 | custom_fields += TimeEntryActivityCustomField.all |
|
137 | custom_fields += TimeEntryActivityCustomField.all | |
138 |
|
138 | |||
139 | # Add list and boolean custom fields as available criteria |
|
139 | # Add list and boolean custom fields as available criteria | |
140 | custom_fields.select {|cf| %w(list bool).include?(cf.field_format) && !cf.multiple?}.each do |cf| |
|
140 | custom_fields.select {|cf| %w(list bool).include?(cf.field_format) && !cf.multiple?}.each do |cf| | |
141 | @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement, |
|
141 | @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement, | |
142 | :joins => cf.join_for_order_statement, |
|
142 | :joins => cf.join_for_order_statement, | |
143 | :format => cf.field_format, |
|
143 | :format => cf.field_format, | |
144 | :custom_field => cf, |
|
144 | :custom_field => cf, | |
145 | :label => cf.name} |
|
145 | :label => cf.name} | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | @available_criteria |
|
148 | @available_criteria | |
149 | end |
|
149 | end | |
150 | end |
|
150 | end | |
151 | end |
|
151 | end | |
152 | end |
|
152 | end |
General Comments 0
You need to be logged in to leave comments.
Login now