From 7709c5b84c2d05f8d138ea6430d23f170e8c8317 Mon Sep 17 00:00:00 2001 From: ulferts Date: Tue, 5 May 2020 11:54:07 +0200 Subject: [PATCH] sort by date & id on time entry xls export (#8339) --- .../xls_export/xls_views/cost_entry_table.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb b/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb index 018447934a..80aa133ed4 100644 --- a/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb +++ b/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb @@ -40,7 +40,7 @@ class OpenProject::XlsExport::XlsViews::CostEntryTable < OpenProject::XlsExport: end def build_cost_rows - query.each_direct_result do |result| + sorted_results.each do |result| spreadsheet.add_row(cost_row(result)) end end @@ -93,4 +93,14 @@ class OpenProject::XlsExport::XlsViews::CostEntryTable < OpenProject::XlsExport: def cost_entry_attributes %i[spent_on user_id activity_id work_package_id comments project_id] end + + # Returns the results of the query sorted by date the time was spent on and by id + def sorted_results + query + .each_direct_result + .map(&:itself) + .group_by { |r| DateTime.parse(r.fields['spent_on']) } + .sort + .flat_map { |_, date_results| date_results.sort_by { |r| r.fields['id'] } } + end end