2011-12-14 23:56:23 +04:00
|
|
|
# encoding: utf-8
|
|
|
|
#
|
2011-08-31 17:11:39 +04:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
2007-06-24 20:07:06 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-31 17:11:39 +04:00
|
|
|
#
|
2007-06-24 20:07:06 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-31 17:11:39 +04:00
|
|
|
#
|
2007-06-24 20:07:06 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
module TimelogHelper
|
2009-01-24 11:58:03 +03:00
|
|
|
include ApplicationHelper
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2008-08-31 20:34:54 +04:00
|
|
|
def render_timelog_breadcrumb
|
|
|
|
links = []
|
|
|
|
links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
|
|
|
|
links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
|
2009-11-14 17:10:09 +03:00
|
|
|
if @issue
|
|
|
|
if @issue.visible?
|
|
|
|
links << link_to_issue(@issue, :subject => false)
|
|
|
|
else
|
|
|
|
links << "##{@issue.id}"
|
|
|
|
end
|
|
|
|
end
|
2008-08-31 20:34:54 +04:00
|
|
|
breadcrumb links
|
|
|
|
end
|
2009-10-22 02:34:28 +04:00
|
|
|
|
|
|
|
# Returns a collection of activities for a select field. time_entry
|
|
|
|
# is optional and will be used to check if the selected TimeEntryActivity
|
|
|
|
# is active.
|
2009-10-22 02:34:39 +04:00
|
|
|
def activity_collection_for_select_options(time_entry=nil, project=nil)
|
|
|
|
project ||= @project
|
|
|
|
if project.nil?
|
2009-11-21 15:00:49 +03:00
|
|
|
activities = TimeEntryActivity.shared.active
|
2009-10-22 02:34:39 +04:00
|
|
|
else
|
|
|
|
activities = project.activities
|
|
|
|
end
|
|
|
|
|
2008-06-29 16:01:20 +04:00
|
|
|
collection = []
|
2009-10-22 02:34:39 +04:00
|
|
|
if time_entry && time_entry.activity && !time_entry.activity.active?
|
2009-10-22 02:34:28 +04:00
|
|
|
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
|
|
|
|
else
|
|
|
|
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
|
|
|
|
end
|
2008-06-29 16:01:20 +04:00
|
|
|
activities.each { |a| collection << [a.name, a.id] }
|
|
|
|
collection
|
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2007-06-24 20:07:06 +04:00
|
|
|
def select_hours(data, criteria, value)
|
2009-03-31 00:09:57 +04:00
|
|
|
if value.to_s.empty?
|
|
|
|
data.select {|row| row[criteria].blank? }
|
2011-08-31 17:11:39 +04:00
|
|
|
else
|
2010-04-30 16:18:11 +04:00
|
|
|
data.select {|row| row[criteria].to_s == value.to_s}
|
2009-03-31 00:09:57 +04:00
|
|
|
end
|
2007-06-24 20:07:06 +04:00
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2007-06-24 20:07:06 +04:00
|
|
|
def sum_hours(data)
|
|
|
|
sum = 0
|
|
|
|
data.each do |row|
|
|
|
|
sum += row['hours'].to_f
|
|
|
|
end
|
|
|
|
sum
|
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
def options_for_period_select(value)
|
|
|
|
options_for_select([[l(:label_all_time), 'all'],
|
|
|
|
[l(:label_today), 'today'],
|
|
|
|
[l(:label_yesterday), 'yesterday'],
|
|
|
|
[l(:label_this_week), 'current_week'],
|
|
|
|
[l(:label_last_week), 'last_week'],
|
|
|
|
[l(:label_last_n_days, 7), '7_days'],
|
|
|
|
[l(:label_this_month), 'current_month'],
|
|
|
|
[l(:label_last_month), 'last_month'],
|
|
|
|
[l(:label_last_n_days, 30), '30_days'],
|
|
|
|
[l(:label_this_year), 'current_year']],
|
|
|
|
value)
|
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
def entries_to_csv(entries)
|
2008-06-26 23:46:57 +04:00
|
|
|
decimal_separator = l(:general_csv_decimal_separator)
|
2008-07-22 23:02:40 +04:00
|
|
|
custom_fields = TimeEntryCustomField.find(:all)
|
2009-11-04 16:22:26 +03:00
|
|
|
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
2008-02-26 21:15:58 +03:00
|
|
|
# csv header fields
|
|
|
|
headers = [l(:field_spent_on),
|
|
|
|
l(:field_user),
|
|
|
|
l(:field_activity),
|
2008-02-27 23:50:19 +03:00
|
|
|
l(:field_project),
|
2008-02-26 21:15:58 +03:00
|
|
|
l(:field_issue),
|
|
|
|
l(:field_tracker),
|
|
|
|
l(:field_subject),
|
|
|
|
l(:field_hours),
|
|
|
|
l(:field_comments)
|
|
|
|
]
|
2008-07-22 23:02:40 +04:00
|
|
|
# Export custom fields
|
|
|
|
headers += custom_fields.collect(&:name)
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2011-11-16 12:10:53 +04:00
|
|
|
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
|
|
|
|
c.to_s,
|
|
|
|
l(:general_csv_encoding) ) }
|
2008-02-26 21:15:58 +03:00
|
|
|
# csv lines
|
|
|
|
entries.each do |entry|
|
2009-01-24 11:58:03 +03:00
|
|
|
fields = [format_date(entry.spent_on),
|
2008-02-26 21:15:58 +03:00
|
|
|
entry.user,
|
|
|
|
entry.activity,
|
2008-02-27 23:50:19 +03:00
|
|
|
entry.project,
|
2008-02-26 21:15:58 +03:00
|
|
|
(entry.issue ? entry.issue.id : nil),
|
|
|
|
(entry.issue ? entry.issue.tracker : nil),
|
|
|
|
(entry.issue ? entry.issue.subject : nil),
|
2008-06-26 23:46:57 +04:00
|
|
|
entry.hours.to_s.gsub('.', decimal_separator),
|
2008-02-26 21:15:58 +03:00
|
|
|
entry.comments
|
|
|
|
]
|
2012-01-30 02:01:40 +04:00
|
|
|
fields += custom_fields.collect {|f| show_value(entry.custom_field_values.detect {|v| v.custom_field_id == f.id}) }
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2011-11-16 12:10:53 +04:00
|
|
|
csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
|
|
|
|
c.to_s,
|
|
|
|
l(:general_csv_encoding) ) }
|
2008-02-26 21:15:58 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
export
|
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2011-12-02 22:46:43 +04:00
|
|
|
def format_criteria_value(criteria_options, value)
|
2009-11-14 17:10:09 +03:00
|
|
|
if value.blank?
|
2012-01-14 13:55:49 +04:00
|
|
|
"[#{l(:label_none)}]"
|
2011-12-02 22:46:43 +04:00
|
|
|
elsif k = criteria_options[:klass]
|
2009-11-14 17:10:09 +03:00
|
|
|
obj = k.find_by_id(value.to_i)
|
|
|
|
if obj.is_a?(Issue)
|
2011-11-27 12:25:12 +04:00
|
|
|
obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
|
2009-11-14 17:10:09 +03:00
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
else
|
2011-12-02 22:46:43 +04:00
|
|
|
format_value(value, criteria_options[:format])
|
2009-11-14 17:10:09 +03:00
|
|
|
end
|
2008-04-09 21:45:39 +04:00
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2011-12-02 22:46:43 +04:00
|
|
|
def report_to_csv(report)
|
2011-11-27 16:14:20 +04:00
|
|
|
decimal_separator = l(:general_csv_decimal_separator)
|
2009-11-04 16:22:26 +03:00
|
|
|
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
2008-04-07 21:18:09 +04:00
|
|
|
# Column headers
|
2011-12-02 22:46:43 +04:00
|
|
|
headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
|
|
|
|
headers += report.periods
|
2008-04-07 21:18:09 +04:00
|
|
|
headers << l(:label_total)
|
2011-11-16 12:29:18 +04:00
|
|
|
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
|
|
|
|
c.to_s,
|
|
|
|
l(:general_csv_encoding) ) }
|
2008-04-07 21:18:09 +04:00
|
|
|
# Content
|
2011-12-02 22:46:43 +04:00
|
|
|
report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
|
2008-04-07 21:18:09 +04:00
|
|
|
# Total row
|
2011-11-27 14:53:08 +04:00
|
|
|
str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding))
|
2011-12-02 22:46:43 +04:00
|
|
|
row = [ str_total ] + [''] * (report.criteria.size - 1)
|
2008-04-07 21:18:09 +04:00
|
|
|
total = 0
|
2011-12-02 22:46:43 +04:00
|
|
|
report.periods.each do |period|
|
|
|
|
sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
|
2008-04-07 21:18:09 +04:00
|
|
|
total += sum
|
2011-11-27 16:14:20 +04:00
|
|
|
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
|
2008-04-07 21:18:09 +04:00
|
|
|
end
|
2011-11-27 16:14:20 +04:00
|
|
|
row << ("%.2f" % total).gsub('.',decimal_separator)
|
2008-04-07 21:18:09 +04:00
|
|
|
csv << row
|
|
|
|
end
|
|
|
|
export
|
|
|
|
end
|
2011-08-31 17:11:39 +04:00
|
|
|
|
2011-12-02 22:46:43 +04:00
|
|
|
def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
|
2011-11-27 16:14:20 +04:00
|
|
|
decimal_separator = l(:general_csv_decimal_separator)
|
2011-12-02 22:46:43 +04:00
|
|
|
hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
|
|
|
|
hours_for_value = select_hours(hours, criteria[level], value)
|
2008-04-07 21:18:09 +04:00
|
|
|
next if hours_for_value.empty?
|
|
|
|
row = [''] * level
|
2011-11-16 12:29:18 +04:00
|
|
|
row << Redmine::CodesetUtil.from_utf8(
|
2011-12-02 22:46:43 +04:00
|
|
|
format_criteria_value(available_criteria[criteria[level]], value).to_s,
|
2011-11-16 12:29:18 +04:00
|
|
|
l(:general_csv_encoding) )
|
2011-12-02 22:46:43 +04:00
|
|
|
row += [''] * (criteria.length - level - 1)
|
2008-04-07 21:18:09 +04:00
|
|
|
total = 0
|
|
|
|
periods.each do |period|
|
2011-12-02 22:46:43 +04:00
|
|
|
sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
|
2008-04-07 21:18:09 +04:00
|
|
|
total += sum
|
2011-11-27 16:14:20 +04:00
|
|
|
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
|
2008-04-07 21:18:09 +04:00
|
|
|
end
|
2011-11-27 16:14:20 +04:00
|
|
|
row << ("%.2f" % total).gsub('.',decimal_separator)
|
2008-04-07 21:18:09 +04:00
|
|
|
csv << row
|
2011-12-02 22:46:43 +04:00
|
|
|
if criteria.length > level + 1
|
|
|
|
report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
|
2008-04-07 21:18:09 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|