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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Keep % complete consistent with work and remaining work
Christophe Bliard179dd587d083
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex a4edd1cc54e..68d8d8d4bfd 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -290,6 +290,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributeselsif only_percent_complete_initially_set?update_remaining_hours_from_percent_completeelse+ update_estimated_hoursupdate_remaining_hoursupdate_done_ratioend@@ -302,11 +303,8 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributeswork_package.estimated_hours_changed? && work_package.estimated_hours.present?end- def only_remaining_work_initially_set?- return false if work_package.remaining_hours.nil?- return false if work_package.done_ratio.present?-- work_package.estimated_hours_was.nil?+ def work_was_unset_and_remaining_work_is_set?+ work_package.estimated_hours_was.nil? && work_package.remaining_hours.present?end# Compute and update +done_ratio+ if its dependent attributes are being modified.@@ -341,12 +339,40 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributesenddef compute_done_ratio+ # do not change done ratio if the values are invalid+ if invalid_progress_values?+ return work_package.done_ratio+ end+completed_work = work_package.estimated_hours - work_package.remaining_hourscompletion_ratio = completed_work.to_f / work_package.estimated_hours(completion_ratio * 100).round(2)end+ def invalid_progress_values?+ work = work_package.estimated_hours+ remaining_work = work_package.remaining_hours++ return true if work.negative?+ return true if remaining_work.negative?++ work && remaining_work && remaining_work > work+ end++ # rubocop:disable Metrics/AbcSize+ def update_estimated_hours+ return unless WorkPackage.use_field_for_done_ratio?+ return if work_package.estimated_hours_changed?+ return if work_package.estimated_hours.present?+ return unless work_package.remaining_hours_changed?++ if work_package.remaining_hours.present? && work_package.done_ratio.present?+ work_package.estimated_hours = estimated_hours_from_done_ratio_and_remaining_hours+ end+ end+ # rubocop:enable Metrics/AbcSize+# When in "Status-based" mode for % Complete, remaining hours are based# on the computation of it derived from the status's default done ratio# and the estimated hours. If the estimated hours are unset, then also@@ -358,7 +384,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributeselsif WorkPackage.use_field_for_done_ratio? &&work_package.estimated_hours_changed?return if work_package.remaining_hours_changed?- return if only_remaining_work_initially_set?+ return if work_was_unset_and_remaining_work_is_set? # remaining work is kept and % complete will be setif work_package.estimated_hours.nil? || work_package.remaining_hours.nil?work_package.remaining_hours = work_package.estimated_hours@@ -370,6 +396,11 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributesend# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity+ def estimated_hours_from_done_ratio_and_remaining_hours+ remaining_ratio = 1.0 - (work_package.done_ratio / 100.0)+ work_package.remaining_hours / remaining_ratio+ end+def remaining_hours_from_done_ratio_and_estimated_hoursreturn nil if work_package.done_ratio.nil? || work_package.estimated_hours.nil?diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex daac840a8b0..a57540730ba 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -35,6 +35,7 @@ RSpec.describe WorkPackages::BaseContract dotype:,done_ratio: 50,estimated_hours: 6.0,+ remaining_hours: 3.0,project:)endlet(:project) { build_stubbed(:project) }@@ -294,6 +295,7 @@ RSpec.describe WorkPackages::BaseContract dobefore dowork_package.estimated_hours = estimated_hours+ work_package.remaining_hours = nilendcontext "when > 0" dodiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 4464a40b12f..0d1f36d5f95 100644--- a/spec/services/work_packages/set_attributes_service_spec.rb+++ b/spec/services/work_packages/set_attributes_service_spec.rb@@ -89,11 +89,21 @@ RSpec.describe WorkPackages::SetAttributesService,endit description || "sets the value" do- next if !defined?(expected_attributes) || expected_attributes.blank?+ all_expected_attributes = {}+ all_expected_attributes.merge!(expected_attributes) if defined?(expected_attributes)+ if defined?(expected_kept_attributes)+ kept = work_package.attributes.slice(*expected_kept_attributes)+ if kept.size != expected_kept_attributes.size+ raise ArgumentError, "expected_kept_attributes contains attributes that are not present in the work_package: " \+ "#{expected_kept_attributes - kept.keys} not present in #{work_package.attributes}"+ end+ all_expected_attributes.merge!(kept)+ end+ next if all_expected_attributes.blank?subject- expect(work_package).to have_attributes(expected_attributes)+ expect(work_package).to have_attributes(all_expected_attributes)endit "does not persist the work_package" do@@ -313,17 +323,30 @@ RSpec.describe WorkPackages::SetAttributesService,context "when remaining work is changed" dolet(:call_attributes) { { remaining_hours: 2 } }let(:expected_attributes) { { done_ratio: 80 } }+ let(:expected_kept_attributes) { %w[estimated_hours] }it_behaves_like "service call", description: "updates % complete accordingly"end- context "when remaining work is changed to a value greater than work", skip: "TODO: not implemented yet" do+ context "when work and remaining work are both changed to negative values" do+ let(:call_attributes) { { estimated_hours: -10, remaining_hours: -5 } }+ let(:expected_kept_attributes) { %w[done_ratio] }++ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"+ end++ context "when remaining work is changed to a value greater than work" dolet(:call_attributes) { { remaining_hours: 200.0 } }- let(:expected_attributes) { "error" }+ let(:expected_kept_attributes) { %w[done_ratio] }- # open question: should it be capped or produce an error?- # I would opt for the error which would then be displayed in the popover- it_behaves_like "service call", description: "produces an error"+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"+ end++ context "when remaining work is changed to a negative value" do+ let(:call_attributes) { { remaining_hours: -1.0 } }+ let(:expected_kept_attributes) { %w[done_ratio] }++ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"endcontext "when both work and remaining work are changed" do@@ -334,6 +357,56 @@ RSpec.describe WorkPackages::SetAttributesService,endend+ context "given a work package with work and % complete being set, and remaining work being unset" do+ before do+ work_package.estimated_hours = 10+ work_package.remaining_hours = nil+ work_package.done_ratio = 30+ work_package.send(:clear_changes_information)+ end++ context "when work is changed" do+ let(:call_attributes) { { estimated_hours: 20.0 } }+ let(:expected_attributes) { { remaining_hours: 14.0 } }+ let(:expected_kept_attributes) { %w[done_ratio] }++ it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"+ end++ context "when remaining work is set" do+ let(:call_attributes) { { remaining_hours: 1.0 } }+ let(:expected_attributes) { call_attributes.merge(done_ratio: 90.0) }+ let(:expected_kept_attributes) { %w[estimated_hours] }++ it_behaves_like "service call", description: "work is kept and % complete is updated accordingly"+ end+ end++ context "given a work package with remaining work and % complete being set, and work being unset" do+ before do+ work_package.estimated_hours = nil+ work_package.remaining_hours = 2.0+ work_package.done_ratio = 50+ work_package.send(:clear_changes_information)+ end++ context "when remaining work is changed" do+ let(:call_attributes) { { remaining_hours: 10.0 } }+ let(:expected_attributes) { call_attributes.merge(estimated_hours: 20.0) }+ let(:expected_kept_attributes) { %w[done_ratio] }++ 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) }+ let(:expected_kept_attributes) { %w[remaining_hours] }++ it_behaves_like "service call", description: "remaining work is kept and % complete is updated accordingly"+ end+ end+context "given a work package with work being set, and remaining work and % complete being unset" dobefore dowork_package.estimated_hours = 10@@ -360,7 +433,8 @@ RSpec.describe WorkPackages::SetAttributesService,context "when work is set" dolet(:call_attributes) { { estimated_hours: 10.0 } }- let(:expected_attributes) { { remaining_hours: 6.0, done_ratio: 40 } }+ let(:expected_attributes) { { done_ratio: 40 } }+ let(:expected_kept_attributes) { %w[remaining_hours] }it_behaves_like "service call",description: "remaining work is kept to the same value and % complete is updated accordingly"@@ -377,12 +451,18 @@ RSpec.describe WorkPackages::SetAttributesService,context "when work is set" dolet(:call_attributes) { { estimated_hours: 10.0 } }- let(:expected_attributes) do- { remaining_hours: 4.0, done_ratio: 60 }- end+ let(:expected_attributes) { { remaining_hours: 4.0 } }+ let(:expected_kept_attributes) { %w[done_ratio] }it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"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) }++ it_behaves_like "service call", description: "% complete is updated accordingly"+ endendcontext "given a work package with work, remaining work, and % complete being unset" do