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

Update total % Complete when changing parent work or remaining work

Christophe Bliard0992eb14de49
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 22f730f442a..65cd1c017c1 100644
--- a/app/services/work_packages/update_ancestors_service.rb
+++ b/app/services/work_packages/update_ancestors_service.rb
@@ -99,9 +99,8 @@ class WorkPackages::UpdateAncestorsService
# or the derived remaining hours, depending on the % Complete mode
# currently active.
#
- %i[estimated_hours remaining_hours] => :derive_estimated_hours,
- %i[remaining_hours estimated_hours] => :derive_remaining_hours,
- %i[estimated_hours done_ratio status status_id] => :derive_done_ratio,
+ %i[estimated_hours remaining_hours] => :derive_total_estimated_and_remaining_hours,
+ %i[estimated_hours remaining_hours done_ratio status status_id] => :derive_done_ratio,
%i[ignore_non_working_days] => :derive_ignore_non_working_days
}.each do |derivative_attributes, method|
if attributes.intersect?(derivative_attributes + %i[parent parent_id])
@@ -117,7 +116,6 @@ class WorkPackages::UpdateAncestorsService
end
def derive_done_ratio(ancestor, loader)
- return if initiator?(ancestor)
return if WorkPackage.done_ratio_disabled?
ancestor.derived_done_ratio = compute_derived_done_ratio(ancestor, loader) || 0
@@ -152,15 +150,10 @@ class WorkPackages::UpdateAncestorsService
ancestor.ignore_non_working_days = descendant_value
end
- def derive_estimated_hours(work_package, loader)
+ def derive_total_estimated_and_remaining_hours(work_package, loader)
descendants = loader.descendants_of(work_package)
work_package.derived_estimated_hours = not_zero(all_estimated_hours([work_package] + descendants).sum.to_f)
- end
-
- def derive_remaining_hours(work_package, loader)
- descendants = loader.descendants_of(work_package)
-
work_package.derived_remaining_hours = not_zero(all_remaining_hours([work_package] + descendants).sum.to_f)
end
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index 040c67d2689..26508b3ce86 100644
--- a/spec/services/work_packages/update_ancestors_service_spec.rb
+++ b/spec/services/work_packages/update_ancestors_service_spec.rb
@@ -862,7 +862,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.and change(new_parent, :derived_remaining_hours).to(new_remaining_hours)
end
- it "updates the derived_done_ratio, erived_estimated_hours, and derived_remaining_hours of the grandparent" do
+ it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the grandparent" do
expect do
subject
grandparent.reload
@@ -1064,6 +1064,54 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
+ describe "done_ratio propagation" do
+ shared_let(:parent) { create(:work_package, subject: "parent") }
+
+ context "given child with work, when remaining work being set on parent" do
+ let_work_packages(<<~TABLE)
+ hierarchy | work | total work | remaining work | total remaining work
+ parent | | 10h | 7h |
+ child1 | 10h | 10h | |
+ TABLE
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: parent)
+ .call(%i(remaining_hours))
+ end
+
+ it "sets parent total remaining work and updates total % complete accordingly" do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.all_results
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ subject | total remaining work | total % complete
+ parent | 7h | 30%
+ TABLE
+ end
+ end
+
+ context "given child with remaining work, when work being set on parent" do
+ let_work_packages(<<~TABLE)
+ hierarchy | work | total work | remaining work | total remaining work
+ parent | 10h | | | 7h
+ child1 | | | 7h | 7h
+ TABLE
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: parent)
+ .call(%i(estimated_hours))
+ end
+
+ it "sets parent total work and updates total % complete accordingly" do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.all_results
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ subject | total work | total % complete
+ parent | 10h | 30%
+ TABLE
+ end
+ end
+ end
+
describe "ignore_non_working_days propagation" do
shared_let(:grandgrandparent) do
create(:work_package,
diff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rb
index d4753d0f447..cbf25500e63 100644
--- a/spec/support/table_helpers/column.rb
+++ b/spec/support/table_helpers/column.rb
@@ -46,12 +46,16 @@ module TableHelpers
raise ArgumentError, 'Please use "remaining work" instead of "remaining hours"'
when /\A\s*work/i
Duration.new(header:, attribute: :estimated_hours)
- when /derived work/i
+ when /derived work|total work/i
Duration.new(header:, attribute: :derived_estimated_hours)
when /\A\s*remaining work/i
Duration.new(header:, attribute: :remaining_hours)
- when /derived remaining work/i
+ when /derived remaining work|total remaining work/i
Duration.new(header:, attribute: :derived_remaining_hours)
+ when /\A\s*% complete/i
+ Percentage.new(header:, attribute: :done_ratio)
+ when /total % complete/i
+ Percentage.new(header:, attribute: :derived_done_ratio)
when /end date/i
Generic.new(header:, attribute: :due_date)
when /subject/
@@ -144,6 +148,24 @@ module TableHelpers
end
end
+ class Percentage < Generic
+ def text_align
+ :rjust
+ end
+
+ def format(value)
+ if value.nil?
+ ""
+ else
+ "%s%%" % value.to_i
+ end
+ end
+
+ def parse(raw_value)
+ raw_value.blank? ? nil : raw_value.to_i
+ end
+ end
+
class Subject < Generic
include Identifiable
end