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

Use days and hours formatter in work package exports

Christophe Bliard72751b332006
diff --git a/app/models/work_package/exports/formatters/compound_hours.rb b/app/models/work_package/exports/formatters/compound_hours.rb
new file mode 100644
index 00000000000..83fc4940327
--- /dev/null
+++ b/app/models/work_package/exports/formatters/compound_hours.rb
@@ -0,0 +1,57 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+module WorkPackage::Exports
+ module Formatters
+ class CompoundHours < ::Exports::Formatters::Default
+ def self.apply?(name, _export_format)
+ name.to_sym == key
+ end
+
+ def format(work_package, **)
+ hours = format_value(work_package.public_send(attribute))
+ derived_hours = total_prefix(format_value(work_package.public_send(:"derived_#{attribute}")))
+
+ [hours, derived_hours].compact.join(" ").presence
+ end
+
+ def format_value(value, _options = nil)
+ DurationConverter.output(value)
+ end
+
+ private
+
+ def attribute
+ self.class.key
+ end
+
+ def total_prefix(value)
+ value && "· Σ #{value}"
+ end
+ end
+ end
+end
diff --git a/app/models/work_package/exports/formatters/estimated_hours.rb b/app/models/work_package/exports/formatters/estimated_hours.rb
index 99a1b4d1f8a..48c61e7584f 100644
--- a/app/models/work_package/exports/formatters/estimated_hours.rb
+++ b/app/models/work_package/exports/formatters/estimated_hours.rb
@@ -27,37 +27,7 @@
#++
module WorkPackage::Exports
module Formatters
- class EstimatedHours < ::Exports::Formatters::Default
- def self.apply?(name, _export_format)
- name.to_sym == :estimated_hours
- end
-
- def format(work_package, **)
- estimated_hours = formatted_hours(work_package.estimated_hours)
- derived_hours = formatted_derived_hours(work_package)
-
- if estimated_hours.nil? || derived_hours.nil?
- return estimated_hours || derived_hours
- end
-
- "#{estimated_hours} #{derived_hours}"
- end
-
- def format_value(value, _options)
- formatted_hours(value)
- end
-
- private
-
- def formatted_hours(value)
- value.nil? ? nil : "#{value} #{I18n.t('export.units.hours')}"
- end
-
- def formatted_derived_hours(work_package)
- if (derived_estimated_value = work_package.derived_estimated_hours)
- "· Σ #{formatted_hours(derived_estimated_value)}"
- end
- end
+ class EstimatedHours < ::WorkPackage::Exports::Formatters::CompoundHours
end
end
end
diff --git a/app/models/work_package/exports/formatters/hours.rb b/app/models/work_package/exports/formatters/hours.rb
index 5b5b415517e..29419f559bd 100644
--- a/app/models/work_package/exports/formatters/hours.rb
+++ b/app/models/work_package/exports/formatters/hours.rb
@@ -29,17 +29,11 @@ module WorkPackage::Exports
module Formatters
class Hours < ::Exports::Formatters::Default
def self.apply?(name, export_format)
- %i[remaining_hours spent_hours].include?(name.to_sym) && export_format == :pdf
+ name.to_sym == :spent_hours && export_format == :pdf
end
def format_value(value, _options)
- formatted_hours(value)
- end
-
- private
-
- def formatted_hours(value)
- value.nil? ? "" : "#{value} #{I18n.t('export.units.hours')}"
+ DurationConverter.output(value)
end
end
end
diff --git a/app/models/work_package/exports/formatters/remaining_hours.rb b/app/models/work_package/exports/formatters/remaining_hours.rb
new file mode 100644
index 00000000000..c1e2e880f4b
--- /dev/null
+++ b/app/models/work_package/exports/formatters/remaining_hours.rb
@@ -0,0 +1,33 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+module WorkPackage::Exports
+ module Formatters
+ class RemainingHours < ::WorkPackage::Exports::Formatters::CompoundHours
+ end
+ end
+end
diff --git a/config/initializers/export_formats.rb b/config/initializers/export_formats.rb
index ce0dc9e68db..ae50c624cb3 100644
--- a/config/initializers/export_formats.rb
+++ b/config/initializers/export_formats.rb
@@ -7,6 +7,7 @@ Rails.application.configure do |application|
single WorkPackage, WorkPackage::PDFExport::WorkPackageToPdf
formatter WorkPackage, WorkPackage::Exports::Formatters::EstimatedHours
+ formatter WorkPackage, WorkPackage::Exports::Formatters::RemainingHours
formatter WorkPackage, WorkPackage::Exports::Formatters::SpentUnits
formatter WorkPackage, WorkPackage::Exports::Formatters::Hours
formatter WorkPackage, WorkPackage::Exports::Formatters::Days
diff --git a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
index 7b35482de9a..e72be4e5c6f 100644
--- a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
+++ b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
@@ -2,9 +2,10 @@ require "spec_helper"
require "spreadsheet"
RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
- let(:project) { create(:project) }
+ shared_let(:project) { create(:project) }
+ shared_let(:admin) { create(:admin) }
- let(:current_user) { create(:admin) }
+ let(:current_user) { admin }
let(:column_names) { %w[type id subject status assigned_to priority] }
let(:query) do
@@ -203,7 +204,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
.and_return("costs_currency" => "EUR", "costs_currency_format" => "%n %u")
end
- it "successfullies export the work packages with a cost column" do
+ it "exports successfully the work packages with a cost column" do
expect(sheet.rows.size).to eq(4 + 1)
cost_column = sheet.columns.last.to_a
@@ -215,7 +216,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
context "with german locale" do
let(:current_user) { create(:admin, language: :de) }
- it "successfullies export the work packages with a cost column localized" do
+ it "exports successfully the work packages with a cost column localized" do
I18n.with_locale :de do
sheet
end
@@ -233,7 +234,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
# Check row after header row
hours = sheet.rows[1].values_at(2)
- expect(hours).to include("27.5 h")
+ expect(hours).to include("3d 3.5h")
end
end
@@ -325,26 +326,29 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
it "adapts the datetime fields to the user time zone" do
work_package.reload
estimated_cell = sheet.rows.last.to_a.last
- expect(estimated_cell).to eq "· Σ 15.0 h"
+ expect(estimated_cell).to eq "· Σ 1d 7h"
end
end
- describe "with derived estimated hours and estimated_hours set to zero" do
+ describe "with estimated and remaining hours set to zero and their derived value set" do
let(:work_package) do
create(:work_package,
project:,
- derived_estimated_hours: 15.0,
estimated_hours: 0.0,
+ derived_estimated_hours: 15.0,
+ remaining_hours: 0.0,
+ derived_remaining_hours: 1225.89,
type: project.types.first)
end
let(:work_packages) { [work_package] }
- let(:column_names) { %w[subject status updated_at estimated_hours] }
+ let(:column_names) { %w[subject status updated_at estimated_hours remaining_hours] }
it "outputs both values" do
work_package.reload
- estimated_cell = sheet.rows.last.to_a.last
- expect(estimated_cell).to eq "0.0 h · Σ 15.0 h"
+ estimated_cell, remaining_cell = sheet.rows.last.to_a.last(2)
+ expect(estimated_cell).to eq "0h · Σ 1d 7h"
+ expect(remaining_cell).to eq "0h · Σ 1d 0h"
end
end
end
diff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rb
index 337bca713f7..f0565c4ce13 100644
--- a/spec/models/work_package/exporter/csv_integration_spec.rb
+++ b/spec/models/work_package/exporter/csv_integration_spec.rb
@@ -75,6 +75,6 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do
expect(data.last).to include(work_package.description)
expect(data.last).to include(current_user.name)
expect(data.last).to include(work_package.updated_at.localtime.strftime("%m/%d/%Y %I:%M %p"))
- expect(data.last).to include("· Σ 15.0 h")
+ expect(data.last).to include("· Σ 1d 7h")
end
end
diff --git a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb
index 6078c2c6378..a1b4b5e8e8c 100644
--- a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb
+++ b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb
@@ -39,19 +39,20 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
let(:parent_project) do
create(:project, name: "Parent project")
end
- let(:project_custom_field_bool) { create(:project_custom_field, :boolean,
- name: "Boolean project custom field") }
- let(:project_custom_field_string) {
+ let(:project_custom_field_bool) do
+ create(:project_custom_field, :boolean,
+ name: "Boolean project custom field")
+ end
+ let(:project_custom_field_string) do
create(:project_custom_field, :string,
name: "Secret string", default_value: "admin eyes only",
visible: false)
- }
- let(:project_custom_field_long_text) {
+ end
+ let(:project_custom_field_long_text) do
create(:project_custom_field, :text,
name: "Rich text project custom field",
- default_value: "rich text field value"
- )
- }
+ default_value: "rich text field value")
+ end
let(:project) do
create(:project,
name: "Foo Bla. Report No. 4/2021 with/for Case 42",
@@ -62,7 +63,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
parent: parent_project,
custom_field_values: {
project_custom_field_bool.id => true,
- project_custom_field_long_text.id => "foo",
+ project_custom_field_long_text.id => "foo"
},
work_package_custom_fields: [cf_long_text, cf_disabled_in_project, cf_global_bool],
work_package_custom_field_ids: [cf_long_text.id, cf_global_bool.id]) # cf_disabled_in_project.id is disabled
@@ -96,8 +97,10 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
let(:image_attachment) { Attachment.new author: user, file: File.open(image_path) }
let(:attachments) { [image_attachment] }
let(:cf_long_text_description) { "" }
- let(:cf_long_text) { create(:issue_custom_field, :text,
- name: "Work Package Custom Field Long Text") }
+ let(:cf_long_text) do
+ create(:issue_custom_field, :text,
+ name: "Work Package Custom Field Long Text")
+ end
let!(:cf_disabled_in_project) do
# NOT enabled by project.work_package_custom_field_ids => NOT in PDF
create(:float_wp_custom_field, name: "DisabledCustomField")
@@ -173,12 +176,12 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
created_at: export_time,
updated_at: export_time,
author: another_user,
- assigned_to: another_user
- ).tap do |wp|
- allow(wp)
- .to receive(:attachments)
- .and_return attachments
- end
+ assigned_to: another_user)
+ .tap do |wp|
+ allow(wp)
+ .to receive(:attachments)
+ .and_return attachments
+ end
end
let(:options) { {} }
let(:exporter) do
@@ -221,6 +224,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
describe "with a request for a PDF" do
describe "with rich text and images" do
let(:cf_long_text_description) { "foo" }
+
it "contains correct data" do
result = pdf[:strings]
expected_result = [
@@ -248,12 +252,12 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
["category", category.name],
["createdAt", export_time_formatted],
["updatedAt", export_time_formatted],
- ["estimatedTime", "10.0 h"],
- ["remainingTime", "9.0 h"],
+ ["estimatedTime", "1d 2h"],
+ ["remainingTime", "1d 1h"],
["version", version.name],
["responsible", user.name],
["dueDate", "05/30/2024"],
- ["spentTime", "0.0 h"],
+ ["spentTime", "0h"],
["startDate", "05/30/2024"],
["parent", "#{type.name} ##{parent_work_package.id}: #{parent_work_package.name}"],
["priority", priority.name],
@@ -298,6 +302,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
workPackageValue:#{forbidden_work_package.id}:assignee
DESCRIPTION
end
+
it "contains resolved attributes and labels" do
result = pdf[:strings]
expected_result = [
@@ -317,7 +322,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
"workPackageValue:3:assignee", "workPackageLabel:assignee",
"Work package not found: ",
"[#{I18n.t('export.macro.error', message:
- I18n.t('export.macro.resource_not_found', resource: "WorkPackage 1234567890"))}] ",
+ I18n.t('export.macro.resource_not_found', resource: 'WorkPackage 1234567890'))}] ",
"Access denied: ",
"[#{I18n.t('export.macro.error', message:
I18n.t('export.macro.resource_not_found', resource: "WorkPackage #{forbidden_work_package.id}"))}]",
@@ -382,6 +387,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
projectValue:"#{forbidden_project.identifier}":active
DESCRIPTION
end
+
it "contains resolved attributes and labels" do
result = pdf[:strings]
expected_result = [
@@ -406,13 +412,13 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
"Project by identifier:", " ", I18n.t(:general_text_Yes),
"Project not found: ",
"[#{I18n.t('export.macro.error', message:
- I18n.t('export.macro.resource_not_found', resource: "Project 1234567890"))}] ",
+ I18n.t('export.macro.resource_not_found', resource: 'Project 1234567890'))}] ",
"Access denied: ",
"[#{I18n.t('export.macro.error', message:
I18n.t('export.macro.resource_not_found', resource: "Project #{forbidden_project.id}"))}] ",
"Access denied by identifier:", " ", "[Macro error, resource not found: Project", "forbidden-project]",
- "2", export_time_formatted, project.name,
+ "2", export_time_formatted, project.name
].flatten
expect(result.join(" ")).to eq(expected_result.join(" "))
end