Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#61896] Overview of time logged per day per user in PDF timesheet

as-opc8f427403986
diff --git a/app/models/work_package/pdf_export/common/common.rb b/app/models/work_package/pdf_export/common/common.rb
index 4fe9273eed3..bd6a0130475 100644
--- a/app/models/work_package/pdf_export/common/common.rb
+++ b/app/models/work_package/pdf_export/common/common.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -85,6 +87,10 @@ module WorkPackage::PDFExport::Common::Common
"<link anchor=\"#{anchor}\">#{caption}</link>"
end
+ def make_link_href(href, caption)
+ "<link href=\"#{href}\">#{caption}</link>"
+ end
+
def link_target_at_current_y(id)
pdf_dest = pdf.dest_xyz(0, pdf.y)
pdf.add_dest(id.to_s, pdf_dest)
@@ -295,7 +301,7 @@ module WorkPackage::PDFExport::Common::Common
end
def make_link_href_cell(href, caption)
- "<color rgb='#{styles.link_color}'><link href='#{href}'>#{caption}</link></color>"
+ "<color rgb='#{styles.link_color}'>#{make_link_href(href, caption)}</color>"
end
def get_id_column_cell(work_package, value)
diff --git a/lib/chronic_duration.rb b/lib/chronic_duration.rb
index 94500124811..4c3f4a901d7 100644
--- a/lib/chronic_duration.rb
+++ b/lib/chronic_duration.rb
@@ -105,6 +105,11 @@ module ChronicDuration
if opts[:format] == :hours_only
hours = seconds / 3600.0
seconds = 0
+ elsif opts[:format] == :hours_and_minutes
+ hours = (seconds / hour).to_i
+ minutes = (seconds % hour / minute).to_i
+ minutes += 1 if (seconds % hour % minute) > 0
+ seconds = 0
elsif seconds >= SECONDS_PER_YEAR && seconds % year < seconds % month
years = seconds / year
months = seconds % year / month
@@ -184,6 +189,25 @@ module ChronicDuration
hours = hours.round(2)
hours_int = hours.to_i
hours = hours_int if hours - hours_int == 0 # if hours end with .0
+ when :hours_and_minutes
+ dividers = {
+ hours: ":", minutes: "", keep_zero: true
+ }
+ process = lambda do |str|
+ # Pad zeros on minutes
+ divider = ":"
+ result = str.split(divider).each_with_index.map do |n, index|
+ if index > 0 && n.length == 1
+ "0#{n}"
+ elsif index > 0 && n.empty?
+ "00"
+ else
+ n
+ end
+ end.join(divider)
+ "#{result} h"
+ end
+ joiner = ""
when :chrono
dividers = {
years: ":", months: ":", weeks: ":", days: ":", hours: ":", minutes: ":", seconds: ":", keep_zero: true
diff --git a/modules/reporting/app/workers/cost_query/pdf/export_timesheet_job.rb b/modules/reporting/app/workers/cost_query/pdf/export_timesheet_job.rb
index 058cb206136..f92c1677680 100644
--- a/modules/reporting/app/workers/cost_query/pdf/export_timesheet_job.rb
+++ b/modules/reporting/app/workers/cost_query/pdf/export_timesheet_job.rb
@@ -1,3 +1,33 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# 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.
+#
+# 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.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
require "active_storage/filename"
class CostQuery::PDF::ExportTimesheetJob < Exports::ExportJob
diff --git a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb
index 57a603151a7..f908459e3a3 100644
--- a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb
+++ b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb
@@ -1,3 +1,33 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# 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.
+#
+# 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.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
class CostQuery::PDF::TimesheetGenerator
include WorkPackage::PDFExport::Common::Common
include WorkPackage::PDFExport::Common::Attachments
@@ -113,6 +143,12 @@ class CostQuery::PDF::TimesheetGenerator
.group_by(&:user)
end
+ def all_users
+ all_entries
+ .uniq { |entry| entry.user.id }
+ .map(&:user)
+ end
+
def all_entries
@all_entries ||= begin
ids = query
@@ -137,9 +173,10 @@ class CostQuery::PDF::TimesheetGenerator
end
def build_table_day_rows(spent_on, entries)
+ total_for_day = entries.sum(&:hours)
day_rows = []
entries.each do |entry|
- day_rows.push(build_table_row(spent_on, entry))
+ day_rows.push(build_table_row(spent_on, entry, total_for_day))
if entry.comments.present?
day_rows.push(build_table_row_comment(entry))
end
@@ -147,22 +184,32 @@ class CostQuery::PDF::TimesheetGenerator
day_rows
end
- def build_table_row(spent_on, entry)
+ def build_table_row(spent_on, entry, total_for_day)
[
- { content: format_date(spent_on), rowspan: entry.comments.present? ? 2 : 1 },
- entry.work_package&.subject || "",
+ { content: "#{format_date(spent_on)}\n#{format_hours(total_for_day)}", rowspan: entry.comments.present? ? 2 : 1 },
+ build_table_subject_cell(entry),
with_times_column? ? format_spent_on_time(entry) : nil,
- format_hours(entry.hours || 0),
+ { content: format_hours(entry.hours || 0), align: :right },
entry.activity&.name || ""
].compact
end
+ def build_table_subject_cell(entry)
+ return "" if entry.work_package.nil?
+
+ href = url_helpers.work_package_url(entry.work_package)
+ {
+ content: "#{make_link_href(href, "##{entry.work_package.id}")} #{entry.work_package.subject || ''}",
+ inline_format: true
+ }
+ end
+
def build_table_row_sum(entries)
[
{ content: "", rowspan: 1 },
"",
with_times_column? ? "" : nil,
- { content: format_sum_time_entries(entries), font_style: :bold },
+ { content: format_sum_time_entries(entries), font_style: :bold, align: :right },
""
].compact
end
@@ -189,7 +236,7 @@ class CostQuery::PDF::TimesheetGenerator
{ content: TimeEntry.human_attribute_name(:spent_on), rowspan: 1 },
I18n.t(:"activerecord.models.work_package"),
with_times_column? ? I18n.t(:"export.timesheet.time") : nil,
- TimeEntry.human_attribute_name(:hours),
+ { content: TimeEntry.human_attribute_name(:hours), align: :right },
TimeEntry.human_attribute_name(:activity)
].compact
end
@@ -357,17 +404,75 @@ class CostQuery::PDF::TimesheetGenerator
end
def write_overview!
- groups = grouped_by_user_entries
- return if groups.size <= 1
+ users = all_users
+ return if users.size <= 1
write_heading!
write_hr!
- write_overview_table!(overview_table_rows(groups))
+
+ start_date, end_date = all_entries.map(&:spent_on).minmax
+ users
+ .each_slice(6) do |users_chunk|
+ write_sum_table!(users_chunk, start_date, end_date)
+ end
start_new_page_if_needed
end
- def write_overview_table!(rows)
+ def build_sum_table_footer_rows(users, users_sums)
+ total = users_sums.to_a.sum(&:last)
+ [
+ [{ content: I18n.t("export.timesheet.sums_hours"), font_style: :bold }] + users.map do |user|
+ { content: format_hours(users_sums[user.id]), align: :right, font_style: :bold }
+ end,
+ [
+ {
+ content: "#{I18n.t('export.timesheet.total_sum')}: #{format_hours(total)}",
+ align: :right, font_style: :bold, colspan: 1 + users.length
+ }
+ ]
+ ]
+ end
+
+ def build_sum_table_row(date, users, users_sums)
+ row = []
+ users.each do |user|
+ sum = calc_sum_for_user_on_day(user, date)
+ users_sums[user.id] = (users_sums[user.id] || 0) + sum
+ row.push(sum > 0 ? { content: format_hours(sum), align: :right } : "")
+ end
+ return nil unless row.any? { |column| !column.empty? }
+
+ [format_date(date)] + row
+ end
+
+ def calc_sum_for_user_on_day(user, date)
+ all_entries.select { |entry| entry.user == user && entry.spent_on == date }.sum(&:hours)
+ end
+
+ def build_sum_table_rows(users, start_date, end_date)
+ rows = []
+ users_sums = {}
+ (start_date..end_date).each do |date|
+ row = build_sum_table_row(date, users, users_sums)
+ rows.push(row) unless row.nil?
+ end
+ rows = rows + build_sum_table_footer_rows(users, users_sums) unless rows.empty?
+ rows
+ end
+
+ def write_sum_table!(users, start_date, end_date)
+ rows = build_sum_table_rows(users, start_date, end_date)
+ unless rows.empty?
+ header_row = [{ content: TimeEntry.human_attribute_name(:spent_on), font_style: :bold }] +
+ users.map do |user|
+ {
+ content: make_link_anchor("user_#{user.id}", user.name),
+ inline_format: true, align: :right, font_style: :bold
+ }
+ end
+ rows.unshift(header_row)
+ end
pdf.make_table(
rows,
header: true,
@@ -379,9 +484,7 @@ class CostQuery::PDF::TimesheetGenerator
borders: %i[top bottom left right],
padding: [TABLE_CELL_PADDING, TABLE_CELL_PADDING, TABLE_CELL_PADDING + 2, TABLE_CELL_PADDING]
}
- ) do |table|
- adjust_overview_border_sum_row(table)
- end.draw
+ ).draw
end
def adjust_overview_border_sum_row(table)
@@ -390,22 +493,6 @@ class CostQuery::PDF::TimesheetGenerator
row.columns(-1).style { |c| c.borders = c.borders - [:left] }
end
- def overview_table_rows(groups)
- rows = [
- [
- { content: TimeEntry.human_attribute_name(:user), font_style: :bold },
- { content: I18n.t("export.timesheet.sum_hours"), font_style: :bold }
- ]
- ]
- groups.each do |user, entries|
- rows.push([user.name, format_sum_time_entries(entries)])
- end
-
- total = groups.sum { |_user, entries| entries.sum(&:hours) }
- rows.push(["", { content: format_hours(total), font_style: :bold }])
- rows
- end
-
def write_heading!
pdf.formatted_text([{ text: heading, size: H1_FONT_SIZE, style: :bold }])
pdf.move_down(H1_MARGIN_BOTTOM)
@@ -416,6 +503,7 @@ class CostQuery::PDF::TimesheetGenerator
end
def write_username(user)
+ link_target_at_current_y("user_#{user.id}")
pdf.formatted_text([{ text: user.name, size: H2_FONT_SIZE }])
pdf.move_down(H2_MARGIN_BOTTOM)
end
@@ -431,7 +519,7 @@ class CostQuery::PDF::TimesheetGenerator
def format_hours(hours)
return "" if hours.nil? || hours < 0
- DurationConverter.output(hours)
+ DurationConverter.output(hours, format: :hours_and_minutes)
end
def format_spent_on_time(entry)
diff --git a/modules/reporting/config/locales/en.yml b/modules/reporting/config/locales/en.yml
index 27a86d35c3c..9646f9414eb 100644
--- a/modules/reporting/config/locales/en.yml
+++ b/modules/reporting/config/locales/en.yml
@@ -112,7 +112,8 @@ en:
button: "Export PDF timesheet"
timesheet: "Timesheet"
time: "Time"
- sum_hours: Sum
+ sums_hours: Sums
+ total_sum: Total sum
cost_reports:
title: "Your Cost Reports XLS export"
start_time: "Start time"
diff --git a/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb b/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb
index 3b0a6ed3e7a..3c6e7eae87e 100644
--- a/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb
+++ b/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb
@@ -47,6 +47,7 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do
user: user,
spent_on: Date.new(2024, 12, 0o1),
start_time: 8 * 60,
+ hours: 28,
time_zone: "UTC")
end
let(:time_entry) do
@@ -123,13 +124,16 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do
[generator.format_hours(sum)]
end
- def expected_entry_row(t_entry, with_times_column)
- [format_date(t_entry.spent_on)].concat(expected_entry_columns(t_entry, with_times_column))
+ def expected_entry_row(t_entry, with_times_column, sum_per_day)
+ result = [format_date(t_entry.spent_on)]
+ result.push(sum_per_day) unless sum_per_day.nil?
+ result.concat(expected_entry_columns(t_entry, with_times_column))
end
def expected_entry_columns(t_entry, with_times_column)
time_column = generator.format_spent_on_time(t_entry)
[
+ "##{t_entry.work_package.id} ",
t_entry.work_package&.subject || "",
with_times_column && time_column.present? ? time_column : nil,
generator.format_hours(t_entry.hours),
@@ -139,20 +143,35 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do
end
def expected_overview_page_content
- result = [query.name,
- TimeEntry.human_attribute_name(:user), I18n.t("export.timesheet.sum_hours")]
- time_entries.group_by(&:user).each do |user, entries|
- result << user.name
- result << generator.format_hours(entries.sum(&:hours))
- end
- result << generator.format_hours(time_entries.sum(&:hours))
+ [
+ query.name,
+ TimeEntry.human_attribute_name(:spent_on),
+ user.name,
+ time_entry_user.name,
+
+ format_date(user_time_entry.spent_on),
+ generator.format_hours(user_time_entry.hours), generator.format_hours(time_entry.hours + other_time_entry.hours),
+
+ format_date(time_entry_with_comment.spent_on),
+ generator.format_hours(time_entry_with_comment.hours),
+
+ format_date(time_entry_without_time.spent_on),
+ generator.format_hours(time_entry_without_time.hours),
+
+ I18n.t("export.timesheet.sums_hours"),
+ generator.format_hours(time_entries.select { |entry| entry.user == user }.map(&:hours).sum),
+ generator.format_hours(time_entries.select { |entry| entry.user == time_entry_user }.map(&:hours).sum),
+
+ "#{I18n.t('export.timesheet.total_sum')}:",
+ generator.format_hours(time_entries.map(&:hours).sum),
+ ]
end
def expected_first_user_table(with_times_column)
[
user.name,
*expected_table_header(with_times_column),
- *expected_entry_row(user_time_entry, with_times_column),
+ *expected_entry_row(user_time_entry, with_times_column, generator.format_hours(user_time_entry.hours)),
*expected_sum_row(user, with_times_column)
]
end
@@ -162,10 +181,11 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do
time_entry.user.name,
*expected_table_header(with_times_column),
format_date(time_entry.spent_on), # merged date rows
+ generator.format_hours(time_entry.hours + other_time_entry.hours),
*expected_entry_columns(time_entry, with_times_column),
*expected_entry_columns(other_time_entry, with_times_column),
- *expected_entry_row(time_entry_with_comment, with_times_column),
- *expected_entry_row(time_entry_without_time, with_times_column),
+ *expected_entry_row(time_entry_with_comment, with_times_column, generator.format_hours(time_entry_with_comment.hours)),
+ *expected_entry_row(time_entry_without_time, with_times_column, generator.format_hours(time_entry_without_time.hours)),
*expected_sum_row(time_entry.user, with_times_column)
]
end
diff --git a/spec/lib/chronic_duration_spec.rb b/spec/lib/chronic_duration_spec.rb
index b47039765c5..32d868e8340 100644
--- a/spec/lib/chronic_duration_spec.rb
+++ b/spec/lib/chronic_duration_spec.rb
@@ -31,7 +31,7 @@
require "rspec_helper"
require "chronic_duration"
-INACCURATE_FORMATS = %i[days_and_hours hours_only].freeze
+INACCURATE_FORMATS = %i[days_and_hours hours_only hours_and_minutes].freeze
RSpec.describe ChronicDuration do
describe ".parse" do
@@ -151,6 +151,7 @@ RSpec.describe ChronicDuration do
long: "1 minute 20 seconds",
days_and_hours: "0.02h",
hours_only: "0.02h",
+ hours_and_minutes: "0:02 h",
chrono: "1:20"
},
(60 + 20.51) =>
@@ -161,6 +162,7 @@ RSpec.describe ChronicDuration do
long: "1 minute 20.51 seconds",
days_and_hours: "0.02h",
hours_only: "0.02h",
+ hours_and_minutes: "0:02 h",
chrono: "1:20.51"
},
(60 + 20.51928) =>
@@ -171,6 +173,7 @@ RSpec.describe ChronicDuration do
long: "1 minute 20.51928 seconds",
days_and_hours: "0.02h",
hours_only: "0.02h",
+ hours_and_minutes: "0:02 h",
chrono: "1:20.51928"
},
((4 * 3600) + 60 + 1) =>
@@ -181,6 +184,7 @@ RSpec.describe ChronicDuration do
long: "4 hours 1 minute 1 second",
days_and_hours: "4.02h",
hours_only: "4.02h",
+ hours_and_minutes: "4:02 h",
chrono: "4:01:01"
},
((2 * 3600) + (20 * 60)) =>
@@ -191,6 +195,7 @@ RSpec.describe ChronicDuration do
long: "2 hours 20 minutes",
days_and_hours: "2.33h",
hours_only: "2.33h",
+ hours_and_minutes: "2:20 h",
chrono: "2:20:00"
},
((8 * 24 * 3600) + (3 * 3600) + (30 * 60)) =>
@@ -201,6 +206,7 @@ RSpec.describe ChronicDuration do
long: "8 days 3 hours 30 minutes",
days_and_hours: "8d 3.5h",
hours_only: "195.5h",
+ hours_and_minutes: "195:30 h",
chrono: "8:03:30:00"
},
((6 * 30 * 24 * 3600) + (24 * 3600)) =>
@@ -211,6 +217,7 @@ RSpec.describe ChronicDuration do
long: "6 months 1 day",
days_and_hours: "181d 0h",
hours_only: "4344h",
+ hours_and_minutes: "4344:00 h",
chrono: "6:01:00:00:00" # Yuck. FIXME
},
((365.25 * 24 * 3600) + (24 * 3600)).to_i =>
@@ -221,6 +228,7 @@ RSpec.describe ChronicDuration do
long: "1 year 1 day",
days_and_hours: "366d 0h",
hours_only: "8790h",
+ hours_and_minutes: "8790:00 h",
chrono: "1:00:01:00:00:00"
},
((3 * 365.25 * 24 * 3600) + (24 * 3600)).to_i =>
@@ -231,6 +239,7 @@ RSpec.describe ChronicDuration do
long: "3 years 1 day",
days_and_hours: "1096d 0h",
hours_only: "26322h",
+ hours_and_minutes: "26322:00 h",
chrono: "3:00:01:00:00:00"
},
((6 * 365.25 * 24 * 3600) + (3 * 3600)).to_i =>
@@ -241,6 +250,7 @@ RSpec.describe ChronicDuration do
long: "6 years 3 hours",
days_and_hours: "2191d 3h",
hours_only: "52599h",
+ hours_and_minutes: "52599:00 h",
chrono: "6:00:00:03:00:00"
},
(3600 * 24 * 30 * 18) =>
@@ -251,6 +261,7 @@ RSpec.describe ChronicDuration do
long: "18 months",
days_and_hours: "540d 0h",
hours_only: "12960h",
+ hours_and_minutes: "12960:00 h",
chrono: "18:00:00:00:00"
}
}
@@ -271,6 +282,7 @@ RSpec.describe ChronicDuration do
default: "0 secs",
long: "0 seconds",
days_and_hours: "0h",
+ hours_and_minutes: "0:00 h",
chrono: "0"
},
false =>
@@ -280,6 +292,7 @@ RSpec.describe ChronicDuration do
default: nil,
long: nil,
days_and_hours: "0h",
+ hours_and_minutes: "0:00 h",
chrono: "0"
}
}