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

Set done_ratio when updating remaining_hours or estimated_hours

Aaron Contreras8413bb8bbe1b
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index aef4758227b..cb5787b2421 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -62,6 +62,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
shift_dates_to_soonest_working_days
update_duration
+ update_done_ratio
update_derivable
update_project_dependent_attributes
reassign_invalid_status_if_type_changed
@@ -281,6 +282,35 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
work_package.duration = 1 if work_package.milestone?
end
+ # Compute and update +done_ratio+ if its dependent attributes are being modified.
+ # The dependent attributes for +done_ratio+ are
+ # - +remaining_hours+
+ # - +estimated_hours+
+ #
+ # Unless both +remaining_hours+ and +estimated_hours+ are set, +done_ratio+ will be
+ # considered 0.
+ def update_done_ratio
+ return if WorkPackage.use_status_for_done_ratio?
+ return unless work_package.remaining_hours_changed? || work_package.estimated_hours_changed?
+
+ work_package.done_ratio = if done_ratio_dependent_attribute_unset?
+ 0
+ else
+ compute_done_ratio
+ end
+ end
+
+ def done_ratio_dependent_attribute_unset?
+ work_package.remaining_hours.nil? || work_package.estimated_hours.nil?
+ end
+
+ def compute_done_ratio
+ completed_work = work_package.estimated_hours - work_package.remaining_hours
+ completion_ratio = completed_work.to_f / work_package.estimated_hours
+
+ (completion_ratio * 100).round(2)
+ end
+
def set_version_to_nil
if work_package.version &&
work_package.project &&
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index dd93d5750ba..db5ae2f1742 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -145,6 +145,68 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like 'service call'
end
+ context 'for done_ratio' do
+ context 'with the status being used for done_ratio computations',
+ with_settings: { work_package_done_ratio: 'status' } do
+ let!(:status) { create(:status, default_done_ratio: 99) }
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 5.0 } }
+ let(:expected_attributes) { { estimated_hours: 10.0, remaining_hours: 5.0, done_ratio: 99 } }
+
+ before do
+ allow(work_package).to receive(:done_ratio=) # Spying-purposes
+
+ work_package.status = status
+ end
+
+ it_behaves_like 'service call' do
+ it 'does not try to compute and assign done_ratio' do
+ subject
+
+ expect(work_package)
+ .not_to have_received(:done_ratio=)
+ end
+ end
+ end
+
+ describe 'is "unset"' do
+ context 'when estimated_hours is unset' do
+ let(:call_attributes) { { estimated_hours: nil } }
+ let(:expected_attributes) do
+ { estimated_hours: nil, done_ratio: 0 }
+ end
+
+ it_behaves_like 'service call'
+ end
+
+ context 'when remaining_hours is unset' do
+ let(:call_attributes) { { remaining_hours: nil } }
+ let(:expected_attributes) do
+ { remaining_hours: nil, done_ratio: 0 }
+ end
+
+ it_behaves_like 'service call'
+ end
+
+ context 'when both estimated_hours and remaining_hours are unset' do
+ let(:call_attributes) { { estimated_hours: nil, remaining_hours: nil } }
+ let(:expected_attributes) do
+ { estimated_hours: nil, remaining_hours: nil, done_ratio: 0 }
+ end
+
+ it_behaves_like 'service call'
+ end
+ end
+
+ context 'when both estimated_hours and remaining_hours are set' do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 5.0 } }
+ let(:expected_attributes) do
+ { estimated_hours: 10.0, remaining_hours: 5.0, done_ratio: 50 }
+ end
+
+ it_behaves_like 'service call'
+ end
+ end
+
context 'for status' do
let(:default_status) { build_stubbed(:default_status) }
let(:other_status) { build_stubbed(:status) }