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

[54310] Fix remaining work exceeding work when rounding

Christophe Bliardeb4a6f7190a7
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 5ca5ba983ff..35fdd992259 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -408,7 +408,8 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
return nil if work_package.done_ratio.nil? || work_package.estimated_hours.nil?
completed_work = work_package.estimated_hours * work_package.done_ratio / 100.0
- (work_package.estimated_hours - completed_work).round(2)
+ remaining_hours = (work_package.estimated_hours - completed_work).round(2)
+ remaining_hours.clamp(0.0, work_package.estimated_hours)
end
def set_version_to_nil
diff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rb
index a287b0bb505..aee74fe0955 100644
--- a/app/workers/work_packages/update_progress_job.rb
+++ b/app/workers/work_packages/update_progress_job.rb
@@ -145,7 +145,12 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
def derive_unset_remaining_work_from_work_and_p_complete
execute(<<~SQL.squish)
UPDATE temp_wp_progress_values
- SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
+ SET remaining_hours =
+ GREATEST(0,
+ LEAST(estimated_hours,
+ ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
+ )
+ )
WHERE estimated_hours IS NOT NULL
AND remaining_hours IS NULL
AND done_ratio IS NOT NULL
@@ -155,7 +160,12 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
def derive_remaining_work_from_work_and_p_complete
execute(<<~SQL.squish)
UPDATE temp_wp_progress_values
- SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
+ SET remaining_hours =
+ GREATEST(0,
+ LEAST(estimated_hours,
+ ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
+ )
+ )
WHERE estimated_hours IS NOT NULL
AND done_ratio IS NOT NULL
SQL
@@ -164,7 +174,11 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
def derive_unset_work_from_remaining_work_and_p_complete
execute(<<~SQL.squish)
UPDATE temp_wp_progress_values
- SET estimated_hours = ROUND((remaining_hours * 100 / (100 - done_ratio))::numeric, 2)
+ SET estimated_hours =
+ CASE done_ratio
+ WHEN 0 THEN remaining_hours
+ ELSE ROUND((remaining_hours * 100 / (100 - done_ratio))::numeric, 2)
+ END
WHERE estimated_hours IS NULL
AND remaining_hours IS NOT NULL
AND done_ratio IS NOT NULL
diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rb
index 104330cccc0..e5cccd2cb1f 100644
--- a/spec/migrations/update_progress_calculation_spec.rb
+++ b/spec/migrations/update_progress_calculation_spec.rb
@@ -358,14 +358,17 @@ RSpec.describe UpdateProgressCalculation, type: :model do
it "derives Remaining work from Work and % Complete" do
expect_migrates(
from: <<~TABLE,
- subject | work | remaining work | % complete
- wp both w and pc set | 10h | | 60%
- wp both w and pc set 0h | 0h | | 0%
+ subject | work | remaining work | % complete
+ wp w and pc set | 10h | | 60%
+ wp w and pc set 0h | 0h | | 0%
+ wp w and pc set 5.678h | 5.678h | | 0%
TABLE
to: <<~TABLE
- subject | work | remaining work | % complete
- wp both w and pc set | 10h | 4h | 60%
- wp both w and pc set 0h | 0h | 0h |
+ subject | work | remaining work | % complete
+ wp w and pc set | 10h | 4h | 60%
+ wp w and pc set 0h | 0h | 0h |
+ # no rounding if rounding remaining work would exceed work
+ wp w and pc set 5.678h | 5.678h | 5.678h | 0%
TABLE
)
end
@@ -376,17 +379,20 @@ RSpec.describe UpdateProgressCalculation, type: :model do
expect_migrates(
from: <<~TABLE,
subject | work | remaining work | % complete
- wp both rw and pc set | | 4h | 60%
- wp both rw and pc set 0% | | 4h | 0%
- wp both rw and pc set dec | | 4h | 67%
- wp both rw and pc set 0h | | 0h | 0%
+ wp rw and pc set | | 4h | 60%
+ wp rw and pc set 0% | | 4h | 0%
+ wp rw and pc set dec | | 4h | 67%
+ wp rw and pc set 0h | | 0h | 0%
+ wp rw and pc set 5.678h | | 5.678h | 0%
TABLE
to: <<~TABLE
subject | work | remaining work | % complete
- wp both rw and pc set | 10h | 4h | 60%
- wp both rw and pc set 0% | 4h | 4h | 0%
- wp both rw and pc set dec | 12.12h | 4h | 67%
- wp both rw and pc set 0h | 0h | 0h |
+ wp rw and pc set | 10h | 4h | 60%
+ wp rw and pc set 0% | 4h | 4h | 0%
+ wp rw and pc set dec | 12.12h | 4h | 67%
+ wp rw and pc set 0h | 0h | 0h |
+ # no rounding when % complete is 0%
+ wp rw and pc set 5.678h | 5.678h | 5.678h | 0%
TABLE
)
end
@@ -575,20 +581,22 @@ RSpec.describe UpdateProgressCalculation, type: :model do
it "sets % Complete value to the status value, and derives Remaining work" do
expect_migrates(
from: <<~TABLE,
- subject | status | work | remaining work | % complete
- wp w 0% | To do (0%) | 10h | |
- wp w 30% | Doing (30%) | 10h | |
- wp w 100% | Done (100%) | 10h | |
- wp w 0% 0h | To do (0%) | 0h | |
- wp w 100% 0h| Done (100%) | 0h | |
+ subject | status | work | remaining work | % complete
+ wp w 0% | To do (0%) | 10h | |
+ wp w 0% dec | To do (0%) | 5.678h | |
+ wp w 30% | Doing (30%) | 10h | |
+ wp w 100% | Done (100%) | 10h | |
+ wp w 0% 0h | To do (0%) | 0h | |
+ wp w 100% 0h| Done (100%) | 0h | |
TABLE
to: <<~TABLE
- subject | status | work | remaining work | % complete
- wp w 0% | To do (0%) | 10h | 10h | 0%
- wp w 30% | Doing (30%) | 10h | 7h | 30%
- wp w 100% | Done (100%) | 10h | 0h | 100%
- wp w 0% 0h | To do (0%) | 0h | 0h | 0%
- wp w 100% 0h| Done (100%) | 0h | 0h | 100%
+ subject | status | work | remaining work | % complete
+ wp w 0% | To do (0%) | 10h | 10h | 0%
+ wp w 0% dec | To do (0%) | 5.678h | 5.678h | 0%
+ wp w 30% | Doing (30%) | 10h | 7h | 30%
+ wp w 100% | Done (100%) | 10h | 0h | 100%
+ wp w 0% 0h | To do (0%) | 0h | 0h | 0%
+ wp w 100% 0h| Done (100%) | 0h | 0h | 100%
TABLE
)
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index c7b4279c2f8..9c754ca54d5 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -233,6 +233,14 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call",
description: "is an error state (to be detected by contract), and remaining work is kept"
end
+
+ 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 } }
+
+ it_behaves_like "service call",
+ description: "remaining work is set to the same value"
+ end
end
end
end
@@ -435,6 +443,19 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "% complete is kept and work is updated accordingly"
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(:expected_kept_attributes) { %w[done_ratio] }
+
+ before do
+ work_package.done_ratio = 0
+ work_package.send(:clear_changes_information)
+ end
+
+ it_behaves_like "service call", description: "% complete is kept and work is updated accordingly"
+ end
+
context "when work is set" do
let(:call_attributes) { { estimated_hours: 10.0 } }
let(:expected_attributes) { call_attributes.merge(done_ratio: 80.0) }