@@ -1,152 +1,152 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine |
|
19 | 19 | module Helpers |
|
20 | 20 | class TimeReport |
|
21 | 21 | attr_reader :criteria, :columns, :hours, :total_hours, :periods |
|
22 | 22 | |
|
23 | 23 | def initialize(project, issue, criteria, columns, time_entry_scope) |
|
24 | 24 | @project = project |
|
25 | 25 | @issue = issue |
|
26 | 26 | |
|
27 | 27 | @criteria = criteria || [] |
|
28 | 28 | @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria} |
|
29 | 29 | @criteria.uniq! |
|
30 | 30 | @criteria = @criteria[0,3] |
|
31 | 31 | |
|
32 | 32 | @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month' |
|
33 | 33 | @scope = time_entry_scope |
|
34 | 34 | |
|
35 | 35 | run |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def available_criteria |
|
39 | 39 | @available_criteria || load_available_criteria |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | private |
|
43 | 43 | |
|
44 | 44 | def run |
|
45 | 45 | unless @criteria.empty? |
|
46 | 46 | time_columns = %w(tyear tmonth tweek spent_on) |
|
47 | 47 | @hours = [] |
|
48 | 48 | @scope.includes(:issue, :activity). |
|
49 | 49 | group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns). |
|
50 | 50 | joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact). |
|
51 | 51 | sum(:hours).each do |hash, hours| |
|
52 | 52 | h = {'hours' => hours} |
|
53 | 53 | (@criteria + time_columns).each_with_index do |name, i| |
|
54 | 54 | h[name] = hash[i] |
|
55 | 55 | end |
|
56 | 56 | @hours << h |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | @hours.each do |row| |
|
60 | 60 | case @columns |
|
61 | 61 | when 'year' |
|
62 | 62 | row['year'] = row['tyear'] |
|
63 | 63 | when 'month' |
|
64 | 64 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" |
|
65 | 65 | when 'week' |
|
66 | 66 | row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}" |
|
67 | 67 | when 'day' |
|
68 | 68 | row['day'] = "#{row['spent_on']}" |
|
69 | 69 | end |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | min = @hours.collect {|row| row['spent_on']}.min |
|
73 | 73 | @from = min ? min.to_date : User.current.today |
|
74 | 74 | |
|
75 | 75 | max = @hours.collect {|row| row['spent_on']}.max |
|
76 | 76 | @to = max ? max.to_date : User.current.today |
|
77 | 77 | |
|
78 | 78 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} |
|
79 | 79 | |
|
80 | 80 | @periods = [] |
|
81 | 81 | # Date#at_beginning_of_ not supported in Rails 1.2.x |
|
82 | 82 | date_from = @from.to_time |
|
83 | 83 | # 100 columns max |
|
84 | 84 | while date_from <= @to.to_time && @periods.length < 100 |
|
85 | 85 | case @columns |
|
86 | 86 | when 'year' |
|
87 | 87 | @periods << "#{date_from.year}" |
|
88 | 88 | date_from = (date_from + 1.year).at_beginning_of_year |
|
89 | 89 | when 'month' |
|
90 | 90 | @periods << "#{date_from.year}-#{date_from.month}" |
|
91 | 91 | date_from = (date_from + 1.month).at_beginning_of_month |
|
92 | 92 | when 'week' |
|
93 | 93 | @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}" |
|
94 | 94 | date_from = (date_from + 7.day).at_beginning_of_week |
|
95 | 95 | when 'day' |
|
96 | 96 | @periods << "#{date_from.to_date}" |
|
97 | 97 | date_from = date_from + 1.day |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def load_available_criteria |
|
104 | 104 | @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", |
|
105 | 105 | :klass => Project, |
|
106 | 106 | :label => :label_project}, |
|
107 | 107 | 'status' => {:sql => "#{Issue.table_name}.status_id", |
|
108 | 108 | :klass => IssueStatus, |
|
109 | 109 | :label => :field_status}, |
|
110 | 110 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", |
|
111 | :klass => Version, | |
|
111 | :klass => ::Version, | |
|
112 | 112 | :label => :label_version}, |
|
113 | 113 | 'category' => {:sql => "#{Issue.table_name}.category_id", |
|
114 | 114 | :klass => IssueCategory, |
|
115 | 115 | :label => :field_category}, |
|
116 | 116 | 'user' => {:sql => "#{TimeEntry.table_name}.user_id", |
|
117 | 117 | :klass => User, |
|
118 | 118 | :label => :label_user}, |
|
119 | 119 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", |
|
120 | 120 | :klass => Tracker, |
|
121 | 121 | :label => :label_tracker}, |
|
122 | 122 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", |
|
123 | 123 | :klass => TimeEntryActivity, |
|
124 | 124 | :label => :label_activity}, |
|
125 | 125 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", |
|
126 | 126 | :klass => Issue, |
|
127 | 127 | :label => :label_issue} |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | # Add time entry custom fields |
|
131 | 131 | custom_fields = TimeEntryCustomField.all |
|
132 | 132 | # Add project custom fields |
|
133 | 133 | custom_fields += ProjectCustomField.all |
|
134 | 134 | # Add issue custom fields |
|
135 | 135 | custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) |
|
136 | 136 | # Add time entry activity custom fields |
|
137 | 137 | custom_fields += TimeEntryActivityCustomField.all |
|
138 | 138 | |
|
139 | 139 | # Add list and boolean custom fields as available criteria |
|
140 | 140 | custom_fields.select {|cf| %w(list bool).include?(cf.field_format) && !cf.multiple?}.each do |cf| |
|
141 | 141 | @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement, |
|
142 | 142 | :joins => cf.join_for_order_statement, |
|
143 | 143 | :format => cf.field_format, |
|
144 | 144 | :custom_field => cf, |
|
145 | 145 | :label => cf.name} |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | @available_criteria |
|
149 | 149 | end |
|
150 | 150 | end |
|
151 | 151 | end |
|
152 | 152 | end |
General Comments 0
You need to be logged in to leave comments.
Login now