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

[52233] Derive work and/or remaining work when % complete changes

Christophe Bliarddbec35237f60
diff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb
index e838ef49221..75b1ab90996 100644
--- a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb
+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb
@@ -51,6 +51,10 @@ class WorkPackages::SetAttributesService
work_package.estimated_hours_was
end
+ def work_set?
+ work.present?
+ end
+
def work_unset?
work.nil?
end
@@ -75,6 +79,10 @@ class WorkPackages::SetAttributesService
work_package.remaining_hours = value
end
+ def remaining_work_set?
+ remaining_work.present?
+ end
+
def remaining_work_unset?
remaining_work.nil?
end
diff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
index 240e02313fe..eb861402dbb 100644
--- a/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
@@ -44,7 +44,7 @@ class WorkPackages::SetAttributesService
def only_percent_complete_initially_set?
return false if percent_complete_unset?
- return false if remaining_work.present?
+ return false if remaining_work_set?
work_changed? && work.present?
end
@@ -58,8 +58,9 @@ class WorkPackages::SetAttributesService
# considered nil.
def update_percent_complete
return unless remaining_work_changed? || work_changed?
+ return if work_unset?
- self.percent_complete = if percent_complete_dependent_attribute_unset?
+ self.percent_complete = if remaining_work_unset?
nil
else
compute_percent_complete
@@ -73,10 +74,6 @@ class WorkPackages::SetAttributesService
self.remaining_work = remaining_work_from_percent_complete_and_work
end
- def percent_complete_dependent_attribute_unset?
- remaining_work_unset? || work_unset?
- end
-
def compute_percent_complete
# do not change % complete if the progress values are invalid
if invalid_progress_values?
@@ -98,9 +95,9 @@ class WorkPackages::SetAttributesService
def update_work
return if work_came_from_user?
- return unless remaining_work_changed?
+ return unless remaining_work_changed? || percent_complete_changed?
- return if work.present?
+ return if work.present? && !(remaining_work_changed? && percent_complete_changed?)
return if remaining_work_unset? || remaining_work.negative?
self.work = work_from_percent_complete_and_remaining_work
@@ -108,16 +105,21 @@ class WorkPackages::SetAttributesService
# rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
def update_remaining_work
- return unless work_changed?
+ return unless work_changed? || percent_complete_changed?
return if remaining_work_came_from_user?
return if work&.negative?
- return if work_was_unset? && remaining_work.present? # remaining work is kept and % complete will be set
+ return if work_unset? && percent_complete_unset?
+ return if work_was_unset? && remaining_work_set? # remaining work is kept and % complete will be set
- if work_unset? || remaining_work.nil?
+ if work_set? && remaining_work_unset? && percent_complete_unset?
self.remaining_work = work
- else
+ elsif work_unset? || percent_complete_unset?
+ self.remaining_work = nil
+ elsif work_changed? && !percent_complete_changed?
delta = work - work_was
self.remaining_work = (remaining_work + delta).clamp(0.0, work)
+ else
+ self.remaining_work = remaining_work_from_percent_complete_and_work
end
end
# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity
diff --git a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
index b1b06192af3..a2403449ce5 100644
--- a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
+++ b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
@@ -63,6 +63,7 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
aggregate_failures do
expect(work_package).to have_attributes(all_expected_attributes)
+ expect(work_package).to have_attributes(set_attributes.except(*all_expected_attributes.keys))
# work package is not saved and no errors are created
expect(work_package).not_to have_received(:save)
expect(work_package.errors).to be_empty
@@ -80,23 +81,47 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when work is unset" do
let(:set_attributes) { { estimated_hours: nil } }
- let(:expected_derived_attributes) { { remaining_hours: nil, done_ratio: nil } }
+ let(:expected_derived_attributes) { { remaining_hours: nil } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
- include_examples "update progress values", description: "unsets remaining work and % complete"
+ include_examples "update progress values", description: "keeps % complete, and unsets remaining work"
end
context "when remaining work is unset" do
let(:set_attributes) { { remaining_hours: nil } }
- let(:expected_derived_attributes) { { estimated_hours: 10.0, done_ratio: nil } }
+ let(:expected_derived_attributes) { { done_ratio: nil } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
include_examples "update progress values", description: "keeps work, and unsets % complete"
end
+ context "when % complete is unset" do
+ let(:set_attributes) { { done_ratio: nil } }
+ let(:expected_derived_attributes) { { remaining_hours: nil } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values", description: "keeps work, and unsets remaining work"
+ end
+
context "when both work and remaining work are unset" do
let(:set_attributes) { { estimated_hours: nil, remaining_hours: nil } }
- let(:expected_derived_attributes) { { done_ratio: nil } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
- include_examples "update progress values", description: "unsets % complete"
+ include_examples "update progress values", description: "keeps % complete"
+ end
+
+ context "when both work and percent complete are unset" do
+ let(:set_attributes) { { estimated_hours: nil, done_ratio: nil } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ include_examples "update progress values", description: "keeps remaining work"
+ end
+
+ context "when both remaining work and percent complete are unset" do
+ let(:set_attributes) { { remaining_hours: nil, done_ratio: nil } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values", description: "keeps work"
end
context "when work is increased" do
@@ -159,6 +184,13 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
include_examples "update progress values", description: "updates % complete accordingly"
end
+ context "when remaining work and % complete are both changed" do
+ let(:set_attributes) { { remaining_hours: 12.0, done_ratio: 40 } }
+ let(:expected_derived_attributes) { { estimated_hours: 20.0 } }
+
+ include_examples "update progress values", description: "work is updated accordingly"
+ end
+
context "when work and remaining work are both changed to negative values" do
let(:set_attributes) { { estimated_hours: -10, remaining_hours: -5 } }
let(:expected_kept_attributes) { %w[done_ratio] }
@@ -193,7 +225,7 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when both work and remaining work are changed" do
let(:set_attributes) { { estimated_hours: 20, remaining_hours: 2 } }
- let(:expected_derived_attributes) { set_attributes.merge(done_ratio: 90) }
+ let(:expected_derived_attributes) { { done_ratio: 90 } }
include_examples "update progress values", description: "updates % complete accordingly"
end
@@ -216,6 +248,29 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
include_examples "update progress values",
description: "remaining work is kept (not increased), and % complete is updated accordingly"
end
+
+ context "when % complete is changed" do
+ let(:set_attributes) { { done_ratio: 40 } }
+ let(:expected_derived_attributes) { { remaining_hours: 6.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values", description: "work is kept, and remaining work is updated accordingly"
+ end
+
+ context "when work, remaining work, and % complete are all changed to consistent values" do
+ let(:set_attributes) { { estimated_hours: 20, remaining_hours: 12.0, done_ratio: 40 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }
+
+ include_examples "update progress values", description: "they are all kept"
+ end
+
+ context "when work, remaining work, and % complete are all changed to inconsistent values" do
+ let(:set_attributes) { { estimated_hours: 5, remaining_hours: -3.0, done_ratio: 42 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), and all values are kept"
+ end
end
context "given a work package with work and % complete being set, and remaining work being unset" do
@@ -245,11 +300,19 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when remaining work is set" do
let(:set_attributes) { { remaining_hours: 1.0 } }
- let(:expected_derived_attributes) { set_attributes.merge(done_ratio: 90.0) }
+ let(:expected_derived_attributes) { { done_ratio: 90.0 } }
let(:expected_kept_attributes) { %w[estimated_hours] }
include_examples "update progress values", description: "work is kept and % complete is updated accordingly"
end
+
+ context "when % complete is set" do
+ let(:set_attributes) { { done_ratio: 90 } }
+ let(:expected_derived_attributes) { { remaining_hours: 1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values", description: "work is kept and remaining work is updated accordingly"
+ end
end
context "given a work package with remaining work and % complete being set, and work being unset" do
@@ -262,7 +325,7 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when remaining work is changed" do
let(:set_attributes) { { remaining_hours: 10.0 } }
- let(:expected_derived_attributes) { set_attributes.merge(estimated_hours: 20.0) }
+ let(:expected_derived_attributes) { { estimated_hours: 20.0 } }
let(:expected_kept_attributes) { %w[done_ratio] }
include_examples "update progress values", description: "% complete is kept and work is updated accordingly"
@@ -284,11 +347,19 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when work is set" do
let(:set_attributes) { { estimated_hours: 10.0 } }
- let(:expected_derived_attributes) { set_attributes.merge(done_ratio: 80.0) }
+ let(:expected_derived_attributes) { { done_ratio: 80.0 } }
let(:expected_kept_attributes) { %w[remaining_hours] }
include_examples "update progress values", description: "remaining work is kept and % complete is updated accordingly"
end
+
+ context "when % complete is changed" do
+ let(:set_attributes) { { done_ratio: 80 } }
+ let(:expected_derived_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ include_examples "update progress values", description: "remaining work is kept and work is updated accordingly"
+ end
end
context "given a work package with work being set, and remaining work and % complete being unset" do
@@ -325,6 +396,15 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
description: "is an error state (to be detected by contract), " \
"and % complete and remaining work are kept"
end
+
+ context "when % complete is set" do
+ let(:set_attributes) { { done_ratio: 100 } }
+ let(:expected_derived_attributes) { { remaining_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values",
+ description: "work is kept and remaining work is updated accordingly"
+ end
end
context "given a work package with remaining work being set, and work and % complete being unset" do
@@ -369,6 +449,14 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
description: "is an error state (to be detected by contract), " \
"and % complete and work are kept"
end
+
+ context "when % complete is set" do
+ let(:set_attributes) { { done_ratio: 40 } }
+ let(:expected_derived_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ include_examples "update progress values", description: "work is updated accordingly"
+ end
end
context "given a work package with work and remaining work unset, and % complete being set" do
@@ -412,7 +500,7 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
context "when work and remaining work are set" do
let(:set_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }
- let(:expected_derived_attributes) { set_attributes.merge(done_ratio: 100) }
+ let(:expected_derived_attributes) { { done_ratio: 100 } }
include_examples "update progress values", description: "% complete is updated accordingly"
end
@@ -432,7 +520,15 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
let(:expected_kept_attributes) { %w[done_ratio] }
include_examples "update progress values",
- description: "is an error state (to be detected by contract), and % Complete is kept"
+ description: "is an error state (to be detected by contract), and % complete is kept"
+ end
+
+ context "when % complete is changed" do
+ let(:set_attributes) { { done_ratio: 80 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }
+
+ include_examples "update progress values",
+ description: "work and remaining work are kept unset"
end
end
@@ -488,5 +584,13 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
description: "% complete is kept and work is kept unset and not recomputed" \
"(error state to be detected by contract)"
end
+
+ context "when % complete is set" do
+ let(:set_attributes) { { done_ratio: 80 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }
+
+ include_examples "update progress values",
+ description: "work and remaining work are kept unset"
+ end
end
end