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

Round progress values to 2 decimals

Christophe Bliard67df322099a3
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 35fdd992259..4d4e14a2d89 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -294,6 +294,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
update_remaining_hours
update_done_ratio
end
+ round_progress_values
end
def only_percent_complete_initially_set?
@@ -330,6 +331,11 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
end
+ def round_progress_values
+ work_package.estimated_hours = work_package.estimated_hours&.round(2)
+ work_package.remaining_hours = work_package.remaining_hours&.round(2)
+ end
+
def update_remaining_hours_from_percent_complete
return if work_package.estimated_hours&.negative?
diff --git a/spec/features/work_packages/progress_modal_spec.rb b/spec/features/work_packages/progress_modal_spec.rb
index a46fbc63b6b..044ce6f54ad 100644
--- a/spec/features/work_packages/progress_modal_spec.rb
+++ b/spec/features/work_packages/progress_modal_spec.rb
@@ -315,7 +315,7 @@ RSpec.describe "Progress modal", :js, :with_cuprite do
describe "field value format" do
context "with all values set" do
- before { update_work_package_with(work_package, estimated_hours: 10.0, remaining_hours: 2.543) }
+ before { update_work_package_with(work_package, estimated_hours: 10.0, remaining_hours: 2.12345) }
it "populates fields with correctly values formatted " \
"with the minimum fractional part if present, and 2 decimals max" do
@@ -329,8 +329,40 @@ RSpec.describe "Progress modal", :js, :with_cuprite do
work_edit_field.activate!
work_edit_field.expect_modal_field_value("10")
- remaining_work_edit_field.expect_modal_field_value("2.54")
- percent_complete_edit_field.expect_modal_field_value("74", readonly: true)
+ remaining_work_edit_field.expect_modal_field_value("2.12")
+ percent_complete_edit_field.expect_modal_field_value("78", readonly: true)
+ end
+ end
+
+ context "with % complete set and setting long decimal values in modal" do
+ before do
+ work_package.attributes = {
+ estimated_hours: nil, remaining_hours: nil, done_ratio: 89
+ }
+ work_package.save(validate: false)
+ end
+
+ it "does not lose precision due to conversion from ISO duration to hours" do
+ work_package_table.visit_query(progress_query)
+ work_package_table.expect_work_package_listed(work_package)
+
+ work_edit_field = ProgressEditField.new(work_package_row, :estimatedTime)
+ remaining_work_edit_field = ProgressEditField.new(work_package_row, :remainingTime)
+
+ # set work to 2.5567
+ work_edit_field.activate!
+ work_edit_field.update("2.5567")
+ work_package_table.expect_and_dismiss_toaster(message: "Successful update.")
+
+ # work should have been set to 2.56 and remaining work to 0.28
+ work_package.reload
+ expect(work_package.estimated_hours).to eq(2.56)
+ expect(work_package.remaining_hours).to eq(0.28)
+
+ # work should be displayed as 2.56, and remaining work as 0.28
+ work_edit_field.activate!
+ work_edit_field.expect_modal_field_value("2.56")
+ remaining_work_edit_field.expect_modal_field_value("0.28")
end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 9c754ca54d5..5614e21e3d5 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -236,10 +236,10 @@ RSpec.describe WorkPackages::SetAttributesService,
context "when work is set with 2nd decimal rounding up" do
let(:call_attributes) { { estimated_hours: 3.567 } }
- let(:expected_attributes) { { remaining_hours: 3.567 } }
+ let(:expected_attributes) { { estimated_hours: 3.57, remaining_hours: 3.57 } }
it_behaves_like "service call",
- description: "remaining work is set to the same value"
+ description: "values are rounded up to 2 decimals and set to the same value"
end
end
end
@@ -363,6 +363,14 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
end
+ context "when work and remaining work are both changed to values with more than 2 decimals" do
+ let(:call_attributes) { { estimated_hours: 10.123456, remaining_hours: 5.6789 } }
+ let(:expected_attributes) { { estimated_hours: 10.12, remaining_hours: 5.68, done_ratio: 43 } }
+
+ it_behaves_like "service call", description: "rounds work and remaining work to 2 decimals " \
+ "and updates % complete accordingly"
+ end
+
context "when remaining work is changed to a value greater than work" do
let(:call_attributes) { { remaining_hours: 200.0 } }
let(:expected_kept_attributes) { %w[done_ratio] }
@@ -444,8 +452,8 @@ RSpec.describe WorkPackages::SetAttributesService,
end
context "when % complete is 0% and remaining work is changed to a decimal rounded up" do
- let(:call_attributes) { { remaining_hours: 5.678 } }
- let(:expected_attributes) { call_attributes.merge(estimated_hours: 5.678) }
+ let(:call_attributes) { { remaining_hours: 5.679 } }
+ let(:expected_attributes) { { estimated_hours: 5.68, remaining_hours: 5.68 } }
let(:expected_kept_attributes) { %w[done_ratio] }
before do
@@ -453,7 +461,8 @@ RSpec.describe WorkPackages::SetAttributesService,
work_package.send(:clear_changes_information)
end
- it_behaves_like "service call", description: "% complete is kept and work is updated accordingly"
+ it_behaves_like "service call",
+ description: "% complete is kept, values are rounded, and work is updated accordingly"
end
context "when work is set" do
@@ -550,6 +559,16 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"
end
+ context "when work is set to a number with with 4 decimals" do
+ let(:call_attributes) { { estimated_hours: 2.5678 } }
+ let(:expected_attributes) { { estimated_hours: 2.57, remaining_hours: 1.03 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept, work is rounded to 2 decimals, " \
+ "and remaining work is updated and rounded to 2 decimals"
+ end
+
context "when work and remaining work are set" do
let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }
let(:expected_attributes) { call_attributes.merge(done_ratio: 100) }