You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_helper.rb 7.2KB

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