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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
changed according to the discussion in a call (sorting, balanced tables, weekday after date, sum rows in details)
as-op8659dea4ed18
diff --git a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rbindex 35773689164..d6e1843a982 100644--- a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb+++ b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb@@ -40,6 +40,7 @@ class CostQuery::PDF::TimesheetGeneratorH1_FONT_SIZE = 26H1_MARGIN_BOTTOM = 2HR_MARGIN_BOTTOM = 16+ PARAGRAPH_MARGIN_BOTTOM = 16TABLE_MARGIN_BOTTOM = 28TABLE_CELL_FONT_SIZE = 10TABLE_CELL_BORDER_COLOR = "BBBBBB"@@ -50,11 +51,11 @@ class CostQuery::PDF::TimesheetGeneratorSUM_TABLE_FIRST_COLUMN_WIDTH = 90SUM_TABLE_MAX_USER_COLUMNS = 6- COLUMN_DATE_WIDTH = 66+ COLUMN_DATE_WIDTH = 90COLUMN_ACTIVITY_WIDTH = 100COLUMN_HOURS_WIDTH = 60COLUMN_TIME_WIDTH = 110- COLUMN_WP_WIDTH = 190+ COLUMN_WP_WIDTH = 164attr_accessor :pdf@@ -150,20 +151,21 @@ class CostQuery::PDF::TimesheetGeneratorall_entries.uniq { |entry| entry.user.id }.map(&:user)+ .sort_by { |user| user.name }enddef all_entries@all_entries ||= begin- ids = query- .each_direct_result- .filter { |r| r.fields["type"] == "TimeEntry" }- .flat_map { |r| r.fields["id"] }+ ids = query+ .each_direct_result+ .filter { |r| r.fields["type"] == "TimeEntry" }+ .flat_map { |r| r.fields["id"] }- TimeEntry.where(id: ids).includes(%i[user activity work_package project])- end+ TimeEntry.where(id: ids).includes(%i[user activity work_package project])+ endend- def build_table_rows(entries)+ def build_table_rows(entries, wants_sum_row)rows = [table_header_columns]entries.group_by(&:spent_on)@@ -171,25 +173,26 @@ class CostQuery::PDF::TimesheetGenerator.each do |spent_on, lines|rows.concat(build_table_day_rows(spent_on, lines))end- rows.push(build_table_row_sum(entries))+ rows.push(build_table_row_sum(entries)) if wants_sum_rowrowsenddef 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, total_for_day))- if entry.comments.present?- day_rows.push(build_table_row_comment(entry))- end+ day_rows.push(build_table_row(spent_on, entry))+ day_rows.push(build_table_row_comment(entry)) if entry.comments.present?end+ day_rows.push(build_table_row_day_sum(spent_on, entries)) if entries.length > 1day_rowsend- def build_table_row(spent_on, entry, total_for_day)+ def build_table_row(spent_on, entry)[- { content: "#{format_date(spent_on)}\n#{format_hours(total_for_day)}", rowspan: entry.comments.present? ? 2 : 1 },+ {+ content: format_date_with_weekday(spent_on),+ rowspan: (entry.comments.present? ? 2 : 1)+ },build_table_subject_cell(entry),with_times_column? ? format_spent_on_time(entry) : nil,{ content: format_hours(entry.hours || 0), align: :right },@@ -197,6 +200,16 @@ class CostQuery::PDF::TimesheetGenerator].compactend+ def build_table_row_day_sum(spent_on, entries)+ [+ { content: format_date_with_weekday(spent_on), rowspan: 1 },+ "",+ with_times_column? ? "" : nil,+ { content: format_sum_time_entries(entries), font_style: :bold, align: :right },+ ""+ ].compact+ end+def build_table_subject_cell(entry)return "" if entry.work_package.nil?@@ -227,11 +240,11 @@ class CostQuery::PDF::TimesheetGeneratordef build_table_row_comment(entry)[{- content: entry.comments,- text_color: COMMENT_FONT_COLOR,- font_style: :italic,- colspan: table_columns_widths.size- }]+ content: entry.comments,+ text_color: COMMENT_FONT_COLOR,+ font_style: :italic,+ colspan: table_columns_widths.size+ }]enddef table_header_columns@@ -313,8 +326,8 @@ class CostQuery::PDF::TimesheetGeneratorendend- def split_group_rows(table_rows)- measure_table = build_table(table_rows, true)+ def split_group_rows(table_rows, has_sum_row)+ measure_table = build_table(table_rows, has_sum_row)groups = []index = 0while index < table_rows.length@@ -333,14 +346,15 @@ class CostQuery::PDF::TimesheetGeneratorenddef write_table(user, entries)- rows = build_table_rows(entries)+ wants_sum_row = entries.map(&:spent_on).uniq.length > 1+ rows = build_table_rows(entries, wants_sum_row)# prawn-table does not support splitting a rowspan cell on page break, so we have to merge the first column manually# for easier handling existing rowspan cells are grouped as one row- grouped_rows = split_group_rows(rows)+ grouped_rows = split_group_rows(rows, wants_sum_row)# start a new page if the username would be printed alone at the end of the pagepdf.start_new_page if available_space_from_bottom < grouped_rows[0][:height] + grouped_rows[1][:height] + username_heightwrite_username(user)- write_grouped_tables(grouped_rows)+ write_grouped_tables(grouped_rows, wants_sum_row)enddef available_space_from_bottom@@ -348,7 +362,7 @@ class CostQuery::PDF::TimesheetGeneratorpdf.y - margin_bottomend- def write_grouped_tables(grouped_rows)+ def write_grouped_tables(grouped_rows, has_sum_row)header_row = grouped_rows[0]current_table = []current_table_height = 0@@ -363,7 +377,7 @@ class CostQuery::PDF::TimesheetGeneratorcurrent_table.push(grouped_row)current_table_height += grouped_row_heightend- write_grouped_row_table(current_table, true)+ write_grouped_row_table(current_table, has_sum_row)pdf.move_down(TABLE_MARGIN_BOTTOM)end@@ -412,29 +426,34 @@ class CostQuery::PDF::TimesheetGeneratorwrite_heading!write_hr!+ write_total_sum!start_date, end_date = all_entries.map(&:spent_on).minmax+ num_groups = (users.length / SUM_TABLE_MAX_USER_COLUMNS.to_f).ceilusers- .each_slice(SUM_TABLE_MAX_USER_COLUMNS) do |users_chunk|- write_sum_table!(users_chunk, start_date, end_date)+ .in_groups(num_groups, false) do |users_chunk|+ write_sum_table!(users_chunk.compact, start_date, end_date)pdf.move_down(TABLE_MARGIN_BOTTOM)endstart_new_page_if_neededend+ def write_total_sum!+ pdf.formatted_text(+ [+ { text: "#{I18n.t('export.timesheet.total_sum')}: ", size: TABLE_CELL_FONT_SIZE },+ { text: format_sum_time_entries(all_entries), size: TABLE_CELL_FONT_SIZE, styles: [:bold] }+ ]+ )+ pdf.move_down(PARAGRAPH_MARGIN_BOTTOM)+ end+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]end@@ -443,15 +462,22 @@ class CostQuery::PDF::TimesheetGeneratorusers.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 } : "")+ row.push(sum > 0 ? build_sum_table_sum_cell(sum, user, date) : "")endreturn nil unless row.any? { |column| !column.empty? }[format_date_with_weekday(date)] + rowend+ def build_sum_table_sum_cell(sum, user, date)+ {+ content: format_hours(sum),+ align: :right, inline_format: true+ }+ end+def format_date_with_weekday(date)- "#{I18n.l(date.to_date, format: '%a')}, #{format_date(date)}"+ "#{format_date(date)}, #{I18n.l(date.to_date, format: '%a')}"enddef calc_sum_for_user_on_day(user, date)@@ -484,7 +510,7 @@ class CostQuery::PDF::TimesheetGeneratorreturn if rows.empty?rows.unshift(build_sum_table_header_row(users))- pdf.make_table(+ pdf.table(rows,header: true,width: pdf.bounds.width,@@ -496,7 +522,7 @@ class CostQuery::PDF::TimesheetGeneratorborders: %i[top bottom left right],padding: [TABLE_CELL_PADDING, TABLE_CELL_PADDING, TABLE_CELL_PADDING + 2, TABLE_CELL_PADDING]}- ).draw+ )enddef sum_table_column_widths(users)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.rbindex 220c923c37d..9dd89aa1f5e 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@@ -124,9 +124,8 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do[generator.format_hours(sum)]end- 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?+ def expected_entry_row(t_entry, with_times_column)+ result = [generator.format_date_with_weekday(t_entry.spent_on)]result.concat(expected_entry_columns(t_entry, with_times_column))end@@ -145,6 +144,9 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator dodef expected_overview_page_content[query.name,+ "#{I18n.t('export.timesheet.total_sum')}: ",+ generator.format_hours(time_entries.sum(&:hours)),+TimeEntry.human_attribute_name(:spent_on),user.name,time_entry_user.name,@@ -160,10 +162,7 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator doI18n.t("export.timesheet.sums_hours"),generator.format_hours(time_entries.select { |entry| entry.user == user }.sum(&:hours)),- generator.format_hours(time_entries.select { |entry| entry.user == time_entry_user }.sum(&:hours)),-- "#{I18n.t('export.timesheet.total_sum')}:",- generator.format_hours(time_entries.sum(&:hours))+ generator.format_hours(time_entries.select { |entry| entry.user == time_entry_user }.sum(&:hours))]end@@ -171,8 +170,7 @@ RSpec.describe CostQuery::PDF::TimesheetGenerator do[user.name,*expected_table_header(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)+ *expected_entry_row(user_time_entry, with_times_column)]end@@ -180,12 +178,12 @@ 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),+ generator.format_date_with_weekday(time_entry.spent_on), # merged date rows*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, 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)),+ generator.format_hours(time_entry.hours + other_time_entry.hours),+ *expected_entry_row(time_entry_with_comment, with_times_column),+ *expected_entry_row(time_entry_without_time, with_times_column),*expected_sum_row(time_entry.user, with_times_column)]end