2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-08-31 13:11:39 +00:00
|
|
|
# Redmine - project management software
|
2022-01-02 05:29:10 +00:00
|
|
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
2007-06-24 16:07:06 +00: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 13:11:39 +00:00
|
|
|
#
|
2007-06-24 16:07:06 +00: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 13:11:39 +00:00
|
|
|
#
|
2007-06-24 16:07:06 +00: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 12:22:31 +00:00
|
|
|
module TimelogHelper
|
2009-01-24 08:58:03 +00:00
|
|
|
include ApplicationHelper
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2009-10-21 22:34:28 +00: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-21 22:34:39 +00:00
|
|
|
def activity_collection_for_select_options(time_entry=nil, project=nil)
|
2015-05-30 08:38:35 +00:00
|
|
|
project ||= time_entry.try(:project)
|
2009-10-21 22:34:39 +00:00
|
|
|
project ||= @project
|
2022-07-17 00:16:31 +00:00
|
|
|
activities = TimeEntryActivity.available_activities(project)
|
2009-10-21 22:34:39 +00:00
|
|
|
|
2008-06-29 12:01:20 +00:00
|
|
|
collection = []
|
2009-10-21 22:34:39 +00:00
|
|
|
if time_entry && time_entry.activity && !time_entry.activity.active?
|
2020-12-10 13:34:38 +00:00
|
|
|
collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
|
2009-10-21 22:34:28 +00:00
|
|
|
else
|
2020-12-10 13:34:38 +00:00
|
|
|
unless activities.detect(&:is_default)
|
|
|
|
|
collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
|
|
|
|
|
end
|
2009-10-21 22:34:28 +00:00
|
|
|
end
|
2020-09-21 13:03:45 +00:00
|
|
|
activities.each {|a| collection << [a.name, a.id]}
|
2008-06-29 12:01:20 +00:00
|
|
|
collection
|
|
|
|
|
end
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2018-12-16 16:28:22 +00:00
|
|
|
def user_collection_for_select_options(time_entry)
|
|
|
|
|
collection = time_entry.assignable_users
|
2020-12-10 13:34:38 +00:00
|
|
|
if time_entry.user && !collection.include?(time_entry.user)
|
|
|
|
|
collection << time_entry.user
|
|
|
|
|
end
|
2020-02-09 00:23:18 +00:00
|
|
|
principals_options_for_select(collection, time_entry.user_id.to_s)
|
2018-12-16 16:28:22 +00:00
|
|
|
end
|
|
|
|
|
|
2007-06-24 16:07:06 +00:00
|
|
|
def select_hours(data, criteria, value)
|
2013-02-16 13:03:54 +00:00
|
|
|
if value.to_s.empty?
|
2020-09-21 13:03:45 +00:00
|
|
|
data.select {|row| row[criteria].blank?}
|
2011-08-31 13:11:39 +00:00
|
|
|
else
|
2013-02-16 13:03:54 +00:00
|
|
|
data.select {|row| row[criteria].to_s == value.to_s}
|
2009-03-30 20:09:57 +00:00
|
|
|
end
|
2007-06-24 16:07:06 +00:00
|
|
|
end
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2007-06-24 16:07:06 +00:00
|
|
|
def sum_hours(data)
|
|
|
|
|
sum = 0
|
|
|
|
|
data.each do |row|
|
|
|
|
|
sum += row['hours'].to_f
|
|
|
|
|
end
|
|
|
|
|
sum
|
|
|
|
|
end
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2018-06-18 06:13:18 +00:00
|
|
|
def format_criteria_value(criteria_options, value, html=true)
|
2009-11-14 14:10:09 +00:00
|
|
|
if value.blank?
|
2012-01-14 09:55:49 +00:00
|
|
|
"[#{l(:label_none)}]"
|
2011-12-02 18:46:43 +00:00
|
|
|
elsif k = criteria_options[:klass]
|
2009-11-14 14:10:09 +00:00
|
|
|
obj = k.find_by_id(value.to_i)
|
2020-03-04 07:10:07 +00:00
|
|
|
if obj.is_a?(Issue)
|
|
|
|
|
if obj.visible?
|
|
|
|
|
html ? link_to_issue(obj) : "#{obj.tracker} ##{obj.id}: #{obj.subject}"
|
|
|
|
|
else
|
|
|
|
|
"##{obj.id}"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
format_object(obj, html)
|
|
|
|
|
end
|
2013-12-14 08:22:43 +00:00
|
|
|
elsif cf = criteria_options[:custom_field]
|
|
|
|
|
format_value(value, cf)
|
2009-11-14 14:10:09 +00:00
|
|
|
else
|
2013-12-14 08:22:43 +00:00
|
|
|
value.to_s
|
2009-11-14 14:10:09 +00:00
|
|
|
end
|
2008-04-09 17:45:39 +00:00
|
|
|
end
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2011-12-02 18:46:43 +00:00
|
|
|
def report_to_csv(report)
|
2018-05-07 01:13:12 +00:00
|
|
|
Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
|
2008-04-07 17:18:09 +00:00
|
|
|
# Column headers
|
2020-12-10 13:34:38 +00:00
|
|
|
headers =
|
|
|
|
|
report.criteria.collect do |criteria|
|
|
|
|
|
l_or_humanize(report.available_criteria[criteria][:label])
|
|
|
|
|
end
|
2011-12-02 18:46:43 +00:00
|
|
|
headers += report.periods
|
2013-03-10 13:01:46 +00:00
|
|
|
headers << l(:label_total_time)
|
2015-06-13 07:33:20 +00:00
|
|
|
csv << headers
|
2008-04-07 17:18:09 +00:00
|
|
|
# Content
|
2020-12-10 13:34:38 +00:00
|
|
|
report_criteria_to_csv(csv, report.available_criteria, report.columns,
|
|
|
|
|
report.criteria, report.periods, report.hours)
|
2008-04-07 17:18:09 +00:00
|
|
|
# Total row
|
2015-06-13 07:33:20 +00:00
|
|
|
str_total = l(:label_total_time)
|
2020-12-10 13:34:38 +00:00
|
|
|
row = [str_total] + [''] * (report.criteria.size - 1)
|
2008-04-07 17:18:09 +00:00
|
|
|
total = 0
|
2011-12-02 18:46:43 +00:00
|
|
|
report.periods.each do |period|
|
|
|
|
|
sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
|
2008-04-07 17:18:09 +00:00
|
|
|
total += sum
|
2015-06-13 07:33:20 +00:00
|
|
|
row << (sum > 0 ? sum : '')
|
2008-04-07 17:18:09 +00:00
|
|
|
end
|
2015-06-13 07:33:20 +00:00
|
|
|
row << total
|
2008-04-07 17:18:09 +00:00
|
|
|
csv << row
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-08-31 13:11:39 +00:00
|
|
|
|
2011-12-02 18:46:43 +00:00
|
|
|
def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
|
|
|
|
|
hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
|
|
|
|
|
hours_for_value = select_hours(hours, criteria[level], value)
|
2008-04-07 17:18:09 +00:00
|
|
|
next if hours_for_value.empty?
|
2020-11-03 14:55:17 +00:00
|
|
|
|
2008-04-07 17:18:09 +00:00
|
|
|
row = [''] * level
|
2018-06-18 06:13:18 +00:00
|
|
|
row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
|
2011-12-02 18:46:43 +00:00
|
|
|
row += [''] * (criteria.length - level - 1)
|
2008-04-07 17:18:09 +00:00
|
|
|
total = 0
|
|
|
|
|
periods.each do |period|
|
2011-12-02 18:46:43 +00:00
|
|
|
sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
|
2008-04-07 17:18:09 +00:00
|
|
|
total += sum
|
2015-06-13 07:33:20 +00:00
|
|
|
row << (sum > 0 ? sum : '')
|
2008-04-07 17:18:09 +00:00
|
|
|
end
|
2015-06-13 07:33:20 +00:00
|
|
|
row << total
|
2008-04-07 17:18:09 +00:00
|
|
|
csv << row
|
2011-12-02 18:46:43 +00:00
|
|
|
if criteria.length > level + 1
|
|
|
|
|
report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
|
2008-04-07 17:18:09 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-09-26 07:47:59 +00:00
|
|
|
|
2018-09-26 07:49:48 +00:00
|
|
|
def cancel_button_tag_for_time_entry(project)
|
2018-09-26 07:47:59 +00:00
|
|
|
fallback_path = project ? project_time_entries_path(project) : time_entries_path
|
2018-09-26 07:49:48 +00:00
|
|
|
cancel_button_tag(fallback_path)
|
2018-09-26 07:47:59 +00:00
|
|
|
end
|
|
|
|
|
|
2007-03-23 12:22:31 +00:00
|
|
|
end
|