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

Fix Excel export of costs

Klaus Zanders8a8a1ad1e1ed
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index 39e1516380f..02fb85e1d66 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -125,6 +125,10 @@ class TimeEntry < ApplicationRecord
end
end
+ def entity_gid
+ entity&.to_gid.to_s
+ end
+
def hours=(value)
super(value.is_a?(String) ? (value.to_hours || value) : value)
end
diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.yml
index 0861ba1bf57..cee5a4b959f 100644
--- a/modules/costs/config/locales/en.yml
+++ b/modules/costs/config/locales/en.yml
@@ -40,7 +40,6 @@ en:
spent: "Spent"
spent_on: "Date"
logged_by: "Logged by"
- entity: Entity
cost_type:
unit: "Unit name"
unit_plural: "Pluralized unit name"
@@ -59,7 +58,6 @@ en:
time_entry:
project: Project
user: User
- entity: Entity
work_package: Work package
subject: Subject
hours: Hours
@@ -101,6 +99,9 @@ en:
units: "Units"
valid_from: "Valid from"
fixed_date: "Fixed date"
+ entity: Entity
+ entity_id: Entity
+ entity_gid: Entity
button_add_rate: "Add rate"
button_log_costs: "Log unit costs"
diff --git a/modules/reporting/app/helpers/reporting_helper.rb b/modules/reporting/app/helpers/reporting_helper.rb
index c102eaba1ab..a834a2ead19 100644
--- a/modules/reporting/app/helpers/reporting_helper.rb
+++ b/modules/reporting/app/helpers/reporting_helper.rb
@@ -121,9 +121,15 @@ module ReportingHelper
budget_link value
when :work_package_id
link_to_work_package(WorkPackage.find(value.to_i))
- when :entity_id # rubocop:disable Lint/DuplicateBranch
- # TODO: Add ability to use other resources here as well
- link_to_work_package(WorkPackage.find(value.to_i))
+ when :entity_gid
+ # TODO: All possible time entry associations need to be checked here
+ entity = GlobalID::Locator.locate(value, only: TimeEntry::ALLOWED_ENTITY_TYPES.map(&:safe_constantize))
+ if entity.is_a?(WorkPacakge)
+ link_to_work_package(entity)
+ else
+ # TODO proper other links
+ entity.inspect
+ end
when :entity_type
# TODO: Skip for now
nil
diff --git a/modules/reporting/app/models/cost_query/result.rb b/modules/reporting/app/models/cost_query/result.rb
index 1f0a309aa6e..41e8328b254 100644
--- a/modules/reporting/app/models/cost_query/result.rb
+++ b/modules/reporting/app/models/cost_query/result.rb
@@ -68,6 +68,11 @@ class CostQuery::Result < Report::Result
start + self["units"].to_f.hours
end
+ def entity_gid
+ # TODO: Find if there is a better way to generate the entity GID
+ "gid://#{GlobalID.app}/#{self['entity_type']}/#{self['entity_id']}"
+ end
+
private
def timestamp(date, time, time_zone)
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 52f3e06cb0e..fb15801fcd9 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
@@ -80,6 +80,8 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie
def cost_row(result)
current_cost_type_id = result.fields["cost_type_id"].to_i
+ # TODO: What would be the correct way here?
+ result.fields["entity_gid"] = result.entity_gid
cost_main_columns(result)
.concat(cost_fields_columns(result))
@@ -125,7 +127,7 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie
end
def cost_entry_attributes
- %i[user_id activity_id work_package_id comments project_id]
+ %i[user_id activity_id entity_gid comments project_id]
end
# Returns the results of the query sorted by date the time was spent on and by id
diff --git a/modules/reporting/spec/features/export_cost_report_spec.rb b/modules/reporting/spec/features/export_cost_report_spec.rb
index 09dc85507bf..e407dfd35d0 100644
--- a/modules/reporting/spec/features/export_cost_report_spec.rb
+++ b/modules/reporting/spec/features/export_cost_report_spec.rb
@@ -37,18 +37,18 @@ RSpec.describe "Cost reports XLS export", :js do
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
- create(:cost_entry, user:, work_package:, project:,
+ create(:cost_entry, user:, entity: work_package, project:,
cost_type:, spent_on: 3.days.ago)
end
shared_let(:time_entry) do
- create(:time_entry, user:, work_package:, project:,
+ create(:time_entry, user:, entity: work_package, project:,
start_time: 1 * 60,
spent_on: 2.days.ago,
hours: 1.95,
time_zone: "UTC")
end
shared_let(:time_entry_long) do
- create(:time_entry, user:, work_package:, project:,
+ create(:time_entry, user:, entity: work_package, project:,
start_time: 1 * 60,
spent_on: 1.day.ago,
hours: 28.0,
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 d3b038de2a2..5d18da1a37e 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
@@ -16,6 +16,7 @@ class OpenProject::XlsExport::XlsViews
when :project_id then project_representation(value)
when :user_id, :assigned_to_id then user_representation(value)
when :work_package_id then work_package_representation(value)
+ when :entity_gid then entity_global_id_representation(value)
else super
end
end
@@ -82,6 +83,16 @@ class OpenProject::XlsExport::XlsViews
end
end
+ def entity_global_id_representation(value)
+ # TODO: All possible time entry associations need to be checked here
+ entity = GlobalID::Locator.locate(value, only: TimeEntry::ALLOWED_ENTITY_TYPES.map(&:safe_constantize))
+ if entity.is_a?(WorkPackage)
+ "#{entity.type} ##{entity.id}: #{entity.subject}"
+ elsif entity.is_a?(Meeting)
+ "#{Meeting.model_name.human} ##{entity.id}: #{entity.title}"
+ end
+ end
+
def ar_presentation(klass, id)
record = klass.find_by(id: id.to_i)