@@ -1,156 +1,141 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
4 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2014 Jean-Philippe Lang | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; either version 2 |
|
8 | # as published by the Free Software Foundation; either version 2 | |
9 | # of the License, or (at your option) any later version. |
|
9 | # of the License, or (at your option) any later version. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 |
|
19 | |||
20 | module TimelogHelper |
|
20 | module TimelogHelper | |
21 | include ApplicationHelper |
|
21 | include ApplicationHelper | |
22 |
|
22 | |||
23 | def render_timelog_breadcrumb |
|
23 | def render_timelog_breadcrumb | |
24 | links = [] |
|
24 | links = [] | |
25 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) |
|
25 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) | |
26 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project |
|
26 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project | |
27 | if @issue |
|
27 | if @issue | |
28 | if @issue.visible? |
|
28 | if @issue.visible? | |
29 | links << link_to_issue(@issue, :subject => false) |
|
29 | links << link_to_issue(@issue, :subject => false) | |
30 | else |
|
30 | else | |
31 | links << "##{@issue.id}" |
|
31 | links << "##{@issue.id}" | |
32 | end |
|
32 | end | |
33 | end |
|
33 | end | |
34 | breadcrumb links |
|
34 | breadcrumb links | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Returns a collection of activities for a select field. time_entry |
|
37 | # Returns a collection of activities for a select field. time_entry | |
38 | # is optional and will be used to check if the selected TimeEntryActivity |
|
38 | # is optional and will be used to check if the selected TimeEntryActivity | |
39 | # is active. |
|
39 | # is active. | |
40 | def activity_collection_for_select_options(time_entry=nil, project=nil) |
|
40 | def activity_collection_for_select_options(time_entry=nil, project=nil) | |
41 | project ||= @project |
|
41 | project ||= @project | |
42 | if project.nil? |
|
42 | if project.nil? | |
43 | activities = TimeEntryActivity.shared.active |
|
43 | activities = TimeEntryActivity.shared.active | |
44 | else |
|
44 | else | |
45 | activities = project.activities |
|
45 | activities = project.activities | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | collection = [] |
|
48 | collection = [] | |
49 | if time_entry && time_entry.activity && !time_entry.activity.active? |
|
49 | if time_entry && time_entry.activity && !time_entry.activity.active? | |
50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] |
|
50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] | |
51 | else |
|
51 | else | |
52 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) |
|
52 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) | |
53 | end |
|
53 | end | |
54 | activities.each { |a| collection << [a.name, a.id] } |
|
54 | activities.each { |a| collection << [a.name, a.id] } | |
55 | collection |
|
55 | collection | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def select_hours(data, criteria, value) |
|
58 | def select_hours(data, criteria, value) | |
59 | if value.to_s.empty? |
|
59 | if value.to_s.empty? | |
60 | data.select {|row| row[criteria].blank? } |
|
60 | data.select {|row| row[criteria].blank? } | |
61 | else |
|
61 | else | |
62 | data.select {|row| row[criteria].to_s == value.to_s} |
|
62 | data.select {|row| row[criteria].to_s == value.to_s} | |
63 | end |
|
63 | end | |
64 | end |
|
64 | end | |
65 |
|
65 | |||
66 | def sum_hours(data) |
|
66 | def sum_hours(data) | |
67 | sum = 0 |
|
67 | sum = 0 | |
68 | data.each do |row| |
|
68 | data.each do |row| | |
69 | sum += row['hours'].to_f |
|
69 | sum += row['hours'].to_f | |
70 | end |
|
70 | end | |
71 | sum |
|
71 | sum | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def options_for_period_select(value) |
|
|||
75 | options_for_select([[l(:label_all_time), 'all'], |
|
|||
76 | [l(:label_today), 'today'], |
|
|||
77 | [l(:label_yesterday), 'yesterday'], |
|
|||
78 | [l(:label_this_week), 'current_week'], |
|
|||
79 | [l(:label_last_week), 'last_week'], |
|
|||
80 | [l(:label_last_n_weeks, 2), 'last_2_weeks'], |
|
|||
81 | [l(:label_last_n_days, 7), '7_days'], |
|
|||
82 | [l(:label_this_month), 'current_month'], |
|
|||
83 | [l(:label_last_month), 'last_month'], |
|
|||
84 | [l(:label_last_n_days, 30), '30_days'], |
|
|||
85 | [l(:label_this_year), 'current_year']], |
|
|||
86 | value) |
|
|||
87 | end |
|
|||
88 |
|
||||
89 | def format_criteria_value(criteria_options, value) |
|
74 | def format_criteria_value(criteria_options, value) | |
90 | if value.blank? |
|
75 | if value.blank? | |
91 | "[#{l(:label_none)}]" |
|
76 | "[#{l(:label_none)}]" | |
92 | elsif k = criteria_options[:klass] |
|
77 | elsif k = criteria_options[:klass] | |
93 | obj = k.find_by_id(value.to_i) |
|
78 | obj = k.find_by_id(value.to_i) | |
94 | if obj.is_a?(Issue) |
|
79 | if obj.is_a?(Issue) | |
95 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" |
|
80 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" | |
96 | else |
|
81 | else | |
97 | obj |
|
82 | obj | |
98 | end |
|
83 | end | |
99 | elsif cf = criteria_options[:custom_field] |
|
84 | elsif cf = criteria_options[:custom_field] | |
100 | format_value(value, cf) |
|
85 | format_value(value, cf) | |
101 | else |
|
86 | else | |
102 | value.to_s |
|
87 | value.to_s | |
103 | end |
|
88 | end | |
104 | end |
|
89 | end | |
105 |
|
90 | |||
106 | def report_to_csv(report) |
|
91 | def report_to_csv(report) | |
107 | decimal_separator = l(:general_csv_decimal_separator) |
|
92 | decimal_separator = l(:general_csv_decimal_separator) | |
108 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
93 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
109 | # Column headers |
|
94 | # Column headers | |
110 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } |
|
95 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } | |
111 | headers += report.periods |
|
96 | headers += report.periods | |
112 | headers << l(:label_total_time) |
|
97 | headers << l(:label_total_time) | |
113 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
98 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( | |
114 | c.to_s, |
|
99 | c.to_s, | |
115 | l(:general_csv_encoding) ) } |
|
100 | l(:general_csv_encoding) ) } | |
116 | # Content |
|
101 | # Content | |
117 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) |
|
102 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) | |
118 | # Total row |
|
103 | # Total row | |
119 | str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding)) |
|
104 | str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding)) | |
120 | row = [ str_total ] + [''] * (report.criteria.size - 1) |
|
105 | row = [ str_total ] + [''] * (report.criteria.size - 1) | |
121 | total = 0 |
|
106 | total = 0 | |
122 | report.periods.each do |period| |
|
107 | report.periods.each do |period| | |
123 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) |
|
108 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) | |
124 | total += sum |
|
109 | total += sum | |
125 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
|
110 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') | |
126 | end |
|
111 | end | |
127 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
112 | row << ("%.2f" % total).gsub('.',decimal_separator) | |
128 | csv << row |
|
113 | csv << row | |
129 | end |
|
114 | end | |
130 | export |
|
115 | export | |
131 | end |
|
116 | end | |
132 |
|
117 | |||
133 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) |
|
118 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) | |
134 | decimal_separator = l(:general_csv_decimal_separator) |
|
119 | decimal_separator = l(:general_csv_decimal_separator) | |
135 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| |
|
120 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| | |
136 | hours_for_value = select_hours(hours, criteria[level], value) |
|
121 | hours_for_value = select_hours(hours, criteria[level], value) | |
137 | next if hours_for_value.empty? |
|
122 | next if hours_for_value.empty? | |
138 | row = [''] * level |
|
123 | row = [''] * level | |
139 | row << Redmine::CodesetUtil.from_utf8( |
|
124 | row << Redmine::CodesetUtil.from_utf8( | |
140 | format_criteria_value(available_criteria[criteria[level]], value).to_s, |
|
125 | format_criteria_value(available_criteria[criteria[level]], value).to_s, | |
141 | l(:general_csv_encoding) ) |
|
126 | l(:general_csv_encoding) ) | |
142 | row += [''] * (criteria.length - level - 1) |
|
127 | row += [''] * (criteria.length - level - 1) | |
143 | total = 0 |
|
128 | total = 0 | |
144 | periods.each do |period| |
|
129 | periods.each do |period| | |
145 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) |
|
130 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) | |
146 | total += sum |
|
131 | total += sum | |
147 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
|
132 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') | |
148 | end |
|
133 | end | |
149 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
134 | row << ("%.2f" % total).gsub('.',decimal_separator) | |
150 | csv << row |
|
135 | csv << row | |
151 | if criteria.length > level + 1 |
|
136 | if criteria.length > level + 1 | |
152 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) |
|
137 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) | |
153 | end |
|
138 | end | |
154 | end |
|
139 | end | |
155 | end |
|
140 | end | |
156 | end |
|
141 | end |
General Comments 0
You need to be logged in to leave comments.
Login now