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

[#42761] Date formatting in XLS export from Cost Report is wrong

as-opd73d46bdda4e
diff --git a/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
index fb15801fcd9..72840564c83 100644
--- a/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
+++ b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
@@ -41,7 +41,8 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie
def build_cost_rows
sorted_results.each do |result|
- spreadsheet.add_row(cost_row(result))
+ row = spreadsheet.add_row(cost_row(result))
+ row.set_format 0, date_format
end
end
@@ -73,7 +74,7 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie
end
def cost_main_columns(result)
- main_cols = [show_field(:spent_on, result.fields["spent_on"])]
+ main_cols = [result.fields["spent_on"].to_date]
main_cols.concat cost_main_times_columns(result) if with_times_column?
main_cols
end
diff --git a/modules/reporting/spec/features/export_cost_report_spec.rb b/modules/reporting/spec/features/export_cost_report_spec.rb
index e407dfd35d0..d7ec813a1bc 100644
--- a/modules/reporting/spec/features/export_cost_report_spec.rb
+++ b/modules/reporting/spec/features/export_cost_report_spec.rb
@@ -33,7 +33,7 @@ require_relative "support/pages/cost_report_page"
RSpec.describe "Cost reports XLS export", :js do
shared_let(:project) { create(:project) }
- shared_let(:user) { create(:admin) }
+ shared_let(:user) { create(:admin, preferences: { time_zone: "UTC" }) }
shared_let(:cost_type) { create(:cost_type, name: "Post-war", unit: "cap", unit_plural: "caps") }
shared_let(:work_package) { create(:work_package, project:, subject: "Some task") }
shared_let(:cost_entry) do
@@ -72,9 +72,13 @@ RSpec.describe "Cost reports XLS export", :js do
DownloadList.clear
end
- def expect_custom_cost_entry(cost_entry_row)
+ def excel_float_to_date(excel_float)
+ (DateTime.new(1899, 12, 30) + excel_float).to_date
+ end
+
+ def expect_custom_cost_entry(cost_entry_row, entry)
date, user_ref, _, wp_ref, _, project_ref, costs, type, = cost_entry_row
- expect(date).to eq(cost_entry.spent_on.iso8601)
+ expect(excel_float_to_date(date)).to eq(entry.spent_on)
expect(user_ref).to eq(user.name)
expect(wp_ref).to include "Some task"
expect(project_ref).to eq project.name
@@ -84,7 +88,7 @@ RSpec.describe "Cost reports XLS export", :js do
def expect_labor_cost_entry(cost_entry_row, entry)
date, user_ref, _, wp_ref, _, project_ref, costs, type, = cost_entry_row
- expect(date).to eq(entry.spent_on.iso8601)
+ expect(excel_float_to_date(date)).to eq(entry.spent_on)
expect(user_ref).to eq(user.name)
expect(wp_ref).to include "Some task"
expect(project_ref).to eq project.name
@@ -94,7 +98,7 @@ RSpec.describe "Cost reports XLS export", :js do
def expect_time_entry(time_entry_row, entry, start_time_value, end_time_value)
date, start_time, end_time, user_ref, _, wp_ref, _, project_ref, costs, type, = time_entry_row
- expect(date).to eq(entry.spent_on.iso8601)
+ expect(excel_float_to_date(date)).to eq(entry.spent_on)
expect(start_time).to eq start_time_value
expect(end_time).to eq end_time_value
expect(user_ref).to eq(user.name)
@@ -111,7 +115,7 @@ RSpec.describe "Cost reports XLS export", :js do
def expect_cost_entries_sheet
title, _, cost_entry_row, time_entry_row, time_entry_long_row = subject.first.rows
expect_sheet_title title
- expect_custom_cost_entry cost_entry_row
+ expect_custom_cost_entry cost_entry_row, cost_entry
expect_labor_cost_entry time_entry_row, time_entry
expect_labor_cost_entry time_entry_long_row, time_entry_long
end
@@ -130,9 +134,9 @@ RSpec.describe "Cost reports XLS export", :js do
end
def expect_custom_type_entries_sheet
- title, _, cost_entry = subject.third.rows
+ title, _, cost_entry_row = subject.third.rows
expect_sheet_title title
- expect_custom_cost_entry cost_entry
+ expect_custom_cost_entry cost_entry_row, cost_entry
end
def export_xls
diff --git a/modules/xls_export/lib/open_project/xls_export/xls_views.rb b/modules/xls_export/lib/open_project/xls_export/xls_views.rb
index 5d18da1a37e..c357a5bb2e2 100644
--- a/modules/xls_export/lib/open_project/xls_export/xls_views.rb
+++ b/modules/xls_export/lib/open_project/xls_export/xls_views.rb
@@ -69,6 +69,14 @@ class OpenProject::XlsExport::XlsViews
"0.00"
end
+ def date_format
+ @date_format ||= Spreadsheet::Format.new(
+ number_format: "yyyy-mm-dd",
+ horizontal_align: :left,
+ vertical_align: :top
+ )
+ end
+
def project_representation(value)
ar_presentation(Project, value, &:name)
end