2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +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-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
module TimelogHelper
|
2009-01-24 11:58:03 +03:00
|
|
|
include ApplicationHelper
|
2011-05-30 22:52:25 +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-05-30 22:52:25 +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-05-30 22:52:25 +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-05-30 22:52:25 +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-05-30 22:52:25 +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-05-30 22:52:25 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
def entries_to_csv(entries)
|
2011-05-30 22:52:25 +04:00
|
|
|
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
|
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-05-30 22:52:25 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
|
|
|
# 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
|
|
|
|
]
|
2008-07-22 23:02:40 +04:00
|
|
|
fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
export
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-04-09 21:45:39 +04:00
|
|
|
def format_criteria_value(criteria, value)
|
2009-11-14 17:10:09 +03:00
|
|
|
if value.blank?
|
|
|
|
l(:label_none)
|
|
|
|
elsif k = @available_criterias[criteria][:klass]
|
|
|
|
obj = k.find_by_id(value.to_i)
|
|
|
|
if obj.is_a?(Issue)
|
|
|
|
obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
|
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format_value(value, @available_criterias[criteria][:format])
|
|
|
|
end
|
2008-04-09 21:45:39 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-04-07 21:18:09 +04:00
|
|
|
def report_to_csv(criterias, periods, hours)
|
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
|
|
|
|
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
|
|
|
|
headers += periods
|
|
|
|
headers << l(:label_total)
|
2011-05-07 03:38:13 +04:00
|
|
|
csv << headers.collect {|c| to_utf8_for_timelogs(c) }
|
2008-04-07 21:18:09 +04:00
|
|
|
# Content
|
|
|
|
report_criteria_to_csv(csv, criterias, periods, hours)
|
|
|
|
# Total row
|
|
|
|
row = [ l(:label_total) ] + [''] * (criterias.size - 1)
|
|
|
|
total = 0
|
|
|
|
periods.each do |period|
|
|
|
|
sum = sum_hours(select_hours(hours, @columns, period.to_s))
|
|
|
|
total += sum
|
|
|
|
row << (sum > 0 ? "%.2f" % sum : '')
|
|
|
|
end
|
|
|
|
row << "%.2f" %total
|
|
|
|
csv << row
|
|
|
|
end
|
|
|
|
export
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-04-07 21:18:09 +04:00
|
|
|
def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
|
2008-04-09 21:45:39 +04:00
|
|
|
hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
|
2008-04-07 21:18:09 +04:00
|
|
|
hours_for_value = select_hours(hours, criterias[level], value)
|
|
|
|
next if hours_for_value.empty?
|
|
|
|
row = [''] * level
|
2011-05-07 03:38:13 +04:00
|
|
|
row << to_utf8_for_timelogs(format_criteria_value(criterias[level], value))
|
2008-04-07 21:18:09 +04:00
|
|
|
row += [''] * (criterias.length - level - 1)
|
|
|
|
total = 0
|
|
|
|
periods.each do |period|
|
|
|
|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
|
|
|
|
total += sum
|
|
|
|
row << (sum > 0 ? "%.2f" % sum : '')
|
|
|
|
end
|
|
|
|
row << "%.2f" %total
|
|
|
|
csv << row
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-04-07 21:18:09 +04:00
|
|
|
if criterias.length > level + 1
|
|
|
|
report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2011-05-07 03:38:13 +04:00
|
|
|
def to_utf8_for_timelogs(s)
|
2008-04-07 21:18:09 +04:00
|
|
|
@ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
|
|
|
|
begin; @ic.iconv(s.to_s); rescue; s.to_s; end
|
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|