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

[62891] Automatically readjust automatic start date if wrong

Christophe Bliardb90906456c05
diff --git a/app/models/work_package/scheduling_rules.rb b/app/models/work_package/scheduling_rules.rb
index fea115a96a2..5aa8aa84abd 100644
--- a/app/models/work_package/scheduling_rules.rb
+++ b/app/models/work_package/scheduling_rules.rb
@@ -59,13 +59,16 @@ module WorkPackage::SchedulingRules
#
# The soonest start for this work package is the maximum of these values: 2017/07/28.
def soonest_start
- # eager load `to` to avoid n+1 in #successor_soonest_start
@scheduling_relations_soonest_start ||=
Relation
.used_for_scheduling_of(self)
- .includes(:to)
+ .includes(:to) # eager load `to` to avoid n+1 in #successor_soonest_start
.filter_map(&:successor_soonest_start)
.max
+
+ # The final result should not be cached as it depends on
+ # ignore_non_working_days value, which can change between consecutive calls
+ # to #soonest_start
WorkPackages::Shared::Days.for(self)
.soonest_working_day(@scheduling_relations_soonest_start)
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 7713b0b7be7..aab4b85e677 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -398,11 +398,13 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
days.soonest_working_day(new_start_date_from_parent)
else
- min_start = new_start_date_from_parent || new_start_date_from_self
+ min_start = new_start_date_from_parent || work_package.soonest_start
days.soonest_working_day(min_start)
end
end
+ # Returns the soonest start date from the parent if the parent has changed.
+ # If the parent has changed, #soonest_start would be inaccurate.
def new_start_date_from_parent
return unless work_package.parent_id_changed? &&
work_package.parent
@@ -410,13 +412,6 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
work_package.parent.soonest_start
end
- def new_start_date_from_self
- # this method is only called by #new_start_date when there are no children
- return unless work_package.schedule_manually_changed? || work_package.ignore_non_working_days_changed?
-
- work_package.soonest_start
- end
-
def new_due_date(min_start)
# this method is only called by #update_dates_from_self when there are no children
if work_package.due_date_came_from_user?
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index def7666656d..83b99119f1c 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1478,6 +1478,80 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
end
+
+ context "without changing anything, when scheduling mode is automatic " \
+ "and start date initial date is different from the calculated soonest start" do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: monday,
+ due_date: friday,
+ ignore_non_working_days: true)
+ end
+ let(:call_attributes) { {} }
+
+ before do
+ allow(work_package).to receive(:soonest_start).and_return(wednesday)
+ end
+
+ context "when scheduling mode is automatic" do
+ before do
+ work_package.schedule_manually = false
+ end
+
+ it_behaves_like "service call" do
+ it "moves the start date to the calculated soonest start, keeps duration and adjusts due date" do
+ expect { subject }
+ .to change { work_package.slice(:start_date, :due_date, :duration) }
+ .from(start_date: monday, due_date: friday, duration: 5)
+ .to(start_date: wednesday, due_date: sunday, duration: 5)
+ end
+ end
+ end
+
+ context "when scheduling mode is manual" do
+ before do
+ work_package.schedule_manually = true
+ end
+
+ it_behaves_like "service call" do
+ it "does not change any dates" do
+ expect { subject }
+ .not_to change { work_package.slice(:start_date, :due_date, :duration) }
+ end
+ end
+ end
+
+ context "when scheduling mode is initially manual and is changed to automatic" do
+ let(:call_attributes) { { schedule_manually: false } }
+
+ before do
+ work_package.schedule_manually = true
+ end
+
+ it_behaves_like "service call" do
+ it "moves the start date to the calculated soonest start, keeps duration and adjusts due date" do
+ expect { subject }
+ .to change { work_package.slice(:start_date, :due_date, :duration) }
+ .from(start_date: monday, due_date: friday, duration: 5)
+ .to(start_date: wednesday, due_date: sunday, duration: 5)
+ end
+ end
+ end
+
+ context "when scheduling mode is initially automatic and is changed to manual" do
+ let(:call_attributes) { { schedule_manually: true } }
+
+ before do
+ work_package.schedule_manually = false
+ end
+
+ it_behaves_like "service call" do
+ it "does not change any dates" do
+ expect { subject }
+ .not_to change { work_package.slice(:start_date, :due_date, :duration) }
+ end
+ end
+ end
+ end
end
end
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index 8529ccd28a3..19efb856ea4 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1378,6 +1378,33 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
TABLE
end
end
+
+ context "on an automatically scheduled work package with a wrong automatically scheduled start date " \
+ "even without setting any attributes" do
+ # There was a bug in previous versions where the lag was all-days when
+ # successor's ignore_non_working_days was true, leading to a start being
+ # too early. The correct behavior for lag is to be working days only, even
+ # if the successor ignores non-working days.
+ #
+ # The update service must be able to fix these wrong dates.
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSSmtwt | scheduling mode | days counting | successors
+ | predecessor | XX | manual | working days only | work_package with lag 3
+ | work_package | XX | automatic | all days |
+ TABLE
+ let(:attributes) { {} }
+
+ it "moves the start date to the correct date " \
+ "and moves the due date too to keep same duration" do
+ expect(subject).to be_success
+
+ expect_work_packages_after_reload([work_package, predecessor], <<~TABLE)
+ | subject | MTWTFSSmtwt | scheduling mode | days counting
+ | predecessor | XX | manual | working days only
+ | work_package | XX | automatic | all days
+ TABLE
+ end
+ end
end
context "when changing the type of a work package with children into a milestone" do