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

[59539] Reschedule when switching parent to automatic mode

Christophe Bliard737d9280932a
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 5826c4fa926..4ab9a9c5cfb 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -228,6 +228,7 @@ module WorkPackages
def validate_after_soonest_start(date_attribute)
return if model.schedule_manually?
+ return if model.children.any?
if before_soonest_start?(date_attribute)
message = I18n.t("activerecord.errors.models.work_package.attributes.start_date.violates_relationships",
diff --git a/app/services/work_packages/get_rescheduled_children_dates_service.rb b/app/services/work_packages/get_rescheduled_children_dates_service.rb
new file mode 100644
index 00000000000..7c5ef3ae43b
--- /dev/null
+++ b/app/services/work_packages/get_rescheduled_children_dates_service.rb
@@ -0,0 +1,156 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+# Service to get the new dates of an automatically scheduled parent from its
+# children once the children have been rescheduled.
+#
+# It simulates a rescheduling and then returns the dates the parent should have:
+# the earliest start date and the latest due date of the children.
+class WorkPackages::GetRescheduledChildrenDatesService
+ attr_accessor :work_package
+
+ ParentDates = Data.define(:start_date, :due_date)
+
+ def initialize(work_package:)
+ self.work_package = Array(work_package).first
+ end
+
+ def call(changed_attributes = %i(start_date due_date))
+ if %i(start_date due_date parent parent_id).intersect?(changed_attributes)
+ schedule_following
+ end
+
+ children = @dependencies.children_by_parent_id(work_package.id)
+ start_date = children.min_by(&:start_date).start_date
+ due_date = children.max_by(&:due_date).due_date
+
+ ServiceResult.success(result: ParentDates.new(start_date:, due_date:))
+ end
+
+ private
+
+ # Finds all work packages that need to be rescheduled because of a
+ # rescheduling of the service's work package and reschedules them.
+ #
+ # The order of the rescheduling is important as successors' dates are
+ # calculated based on their predecessors' dates and ancestors' dates based on
+ # their children's dates.
+ #
+ # Thus, the work packages following (having a follows relation, direct or
+ # transitively) the service's work package are first all loaded, and then
+ # sorted by their need to be scheduled before one another:
+ #
+ # - predecessors are scheduled before their successors
+ # - children/descendants are scheduled before their parents/ancestors
+ #
+ # Manually scheduled work packages are not encountered at this point as they
+ # are filtered out when fetching the work packages eligible for rescheduling.
+ def schedule_following
+ work_packages = [work_package]
+ work_packages << work_package.parent if work_package.parent && work_package.parent_id_changed?
+ @dependencies = WorkPackages::ScheduleDependency.new(work_packages, switching_to_automatic_mode: [work_package])
+ @dependencies.in_schedule_order do |scheduled, dependency|
+ reschedule(scheduled, dependency)
+ end
+ end
+
+ # Schedules work packages based on either
+ # - their descendants if they are parents
+ # - their predecessors (or predecessors of their ancestors) if they are
+ # leaves
+ def reschedule(scheduled, dependency)
+ if dependency.has_descendants?
+ reschedule_by_descendants(scheduled, dependency)
+ else
+ reschedule_by_predecessors(scheduled, dependency)
+ end
+ end
+
+ # Inherits the start/due_date from the descendants of this work package.
+ #
+ # Only parent work packages are scheduled like this. start_date receives the
+ # minimum of the dates (start_date and due_date) of the descendants due_date
+ # receives the maximum of the dates (start_date and due_date) of the
+ # descendants
+ def reschedule_by_descendants(scheduled, dependency)
+ set_dates(scheduled, dependency.start_date, dependency.due_date)
+ end
+
+ # Calculates the dates of a work package based on its follows relations.
+ #
+ # The start date of a work package is constrained by its direct and indirect
+ # predecessors, as it must start strictly after all predecessors finish.
+ #
+ # The follows relations of ancestors are considered to be equal to own follows
+ # relations as they inhibit moving a work package just the same. Only leaf
+ # work packages are calculated like this.
+ #
+ # work package is moved to a later date:
+ # - following work packages are moved forward only to ensure they start
+ # after their predecessor's finish date. They may not need to move at all
+ # when there a time buffer between a follower and its predecessors
+ # (predecessors can also be acquired transitively by ancestors)
+ #
+ # work package moved to an earlier date:
+ # - following work packages do not move at all.
+ def reschedule_by_predecessors(scheduled, dependency)
+ return unless dependency.soonest_start_date
+
+ new_start_date = dependency.soonest_start_date
+ new_due_date = determine_due_date(scheduled, new_start_date)
+ set_dates(scheduled, new_start_date, new_due_date)
+ end
+
+ def determine_due_date(work_package, start_date)
+ # due date is set only if the moving work package already has one or has a
+ # duration. If not, due date is nil (and duration will be nil too).
+ return unless work_package.due_date || work_package.duration
+
+ due_date =
+ if work_package.duration
+ days(work_package).due_date(start_date, work_package.duration)
+ else
+ work_package.due_date
+ end
+
+ # if due date is before start date, then start is used as due date.
+ [start_date, due_date].max
+ end
+
+ def set_dates(work_package, start_date, due_date)
+ work_package.start_date = start_date
+ work_package.due_date = due_date
+ work_package.duration = days(work_package).duration(start_date, due_date)
+ end
+
+ def days(work_package)
+ WorkPackages::Shared::Days.for(work_package)
+ end
+end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 81551ed1cc4..b1aee6855d3 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -266,7 +266,24 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
def update_dates
unify_milestone_dates
+ if work_package.children.any?
+ update_dates_from_rescheduled_children
+ else
+ update_dates_from_self
+ end
+ end
+
+ def update_dates_from_rescheduled_children
+ return if work_package.schedule_manually?
+ # do a schedule call to get the children rescheduled before getting their dates
+ service = WorkPackages::GetRescheduledChildrenDatesService.new(work_package:)
+ parent_dates = service.call.result
+ work_package.start_date = parent_dates.start_date
+ work_package.due_date = parent_dates.due_date
+ end
+
+ def update_dates_from_self
min_start = new_start_date
return unless min_start
@@ -370,9 +387,6 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
days.soonest_working_day(new_start_date_from_parent)
else
- # current_start_date = work_package.start_date || work_package.due_date
- # return if current_start_date.nil?
-
min_start = new_start_date_from_parent || new_start_date_from_self
days.soonest_working_day(min_start)
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 2c6769487a1..80a9eb61f9c 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1801,21 +1801,26 @@ RSpec.describe WorkPackages::SetAttributesService,
end
context "when switching back to automatic scheduling" do
+ shared_let(:project) { create(:project) }
+ let(:predecessor) do
+ create(:work_package,
+ subject: "predecessor",
+ project:,
+ ignore_non_working_days: true,
+ schedule_manually: true,
+ start_date: soonest_start - 1.day,
+ due_date: soonest_start - 1.day)
+ end
let(:work_package) do
- wp = build_stubbed(:work_package,
- project:,
- ignore_non_working_days: true,
- schedule_manually: true,
- start_date: Time.zone.today,
- due_date: Time.zone.today + 5.days)
- wp.type = build_stubbed(:type)
- wp.clear_changes_information
-
- allow(wp)
- .to receive(:soonest_start)
- .and_return(soonest_start)
-
- wp
+ create(:work_package,
+ subject: "work_package",
+ project:,
+ ignore_non_working_days: true,
+ schedule_manually: true,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 5.days) do |wp|
+ create(:follows_relation, from: wp, to: predecessor)
+ end
end
let(:call_attributes) { { schedule_manually: false } }
let(:expected_attributes) { {} }
@@ -1881,8 +1886,11 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
- context "when the soonest start date is nil" do
- let(:soonest_start) { nil }
+ context "when the soonest start date is nil (no predecessors)" do
+ before do
+ work_package # create the relation
+ predecessor.destroy # destroy the predecessor AND the relation
+ end
include_examples "service call", description: "sets the start date to the soonest possible start date" do
let(:expected_attributes) do
@@ -1894,45 +1902,41 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
- context "when the work package also has a child" do
- let(:child) do
- build_stubbed(:work_package,
- subject: "child",
- start_date: child_start_date,
- due_date: child_due_date)
+ context "when the work package also has a manually scheduled child" do
+ let!(:child) do
+ create(:work_package,
+ subject: "child",
+ parent: work_package,
+ schedule_manually: true,
+ start_date: child_start_date,
+ due_date: child_due_date)
end
+ let(:child_start_date) { Time.zone.today + 2.days }
let(:child_due_date) { Time.zone.today + 10.days }
- before do
- allow(work_package)
- .to receive(:children)
- .and_return([child])
- end
-
- context "when the child's start date is after soonest_start" do
- let(:child_start_date) { Time.zone.today + 2.days }
- let(:soonest_start) { Time.zone.today + 1.day }
+ context "when the soonest start is before the child's start date" do
+ let(:soonest_start) { child_start_date - 1.day }
- include_examples "service call", description: "sets the dates to the child dates" do
+ include_examples "service call",
+ description: "sets the dates to the child dates, without moving parent to soonest start" do
let(:expected_attributes) do
{
- start_date: Time.zone.today + 2.days,
- due_date: Time.zone.today + 10.days
+ start_date: child_start_date,
+ due_date: child_due_date
}
end
end
end
- context "when the child's start date is before soonest_start" do
- let(:child_start_date) { Time.zone.today + 2.days }
- let(:soonest_start) { Time.zone.today + 3.days }
+ context "when the soonest start is after the child's start date" do
+ let(:soonest_start) { child_start_date + 1.day }
- # TODO: Update this: this is no longer true. Now a parent always inherits dates from its children.
- include_examples "service call", description: "sets the dates to soonest date and to the duration of the child" do
+ include_examples "service call",
+ description: "sets the dates to the child dates, regardless of the soonest date" do
let(:expected_attributes) do
{
- start_date: Time.zone.today + 3.days,
- due_date: Time.zone.today + 11.days
+ start_date: child_start_date,
+ due_date: child_due_date
}
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 05dadfd4671..b172deca465 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1267,6 +1267,122 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
end
end
+ context "when switching scheduling mode to automatic" do
+ let(:attributes) { { schedule_manually: false } }
+
+ before do
+ set_factory_default(:priority, priority)
+ set_factory_default(:project_with_types, project)
+ set_factory_default(:status, status)
+ set_factory_default(:type, type)
+ set_factory_default(:user, user)
+
+ set_non_working_week_days("saturday", "sunday")
+ end
+
+ context "when the work package has a manually scheduled child " \
+ "and a predecessor restricting it moving to an earlier date" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode | predecessors
+ | predecessor | XX | manual |
+ | work_package | XXX | manual | predecessor
+ | child | X | manual |
+ TABLE
+
+ it "sets the dates to the child dates, despite the predecessor" do
+ expect(subject).to be_success
+
+ expect_work_packages_after_reload([work_package, predecessor, child], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | predecessor | XX | manual
+ | work_package | X | automatic
+ | child | X | manual
+ TABLE
+ end
+ end
+
+ context "when the work package has an automatically scheduled child " \
+ "and a predecessor restricting it moving to an earlier date" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode | predecessors
+ | predecessor | XX | manual |
+ | child_predecessor | X | manual |
+ | work_package | XXXX | manual | predecessor
+ | child | XXX | automatic | child_predecessor
+ TABLE
+
+ it "sets the dates to start after the predecessor" do
+ expect(subject).to be_success
+
+ expect_work_packages_after_reload([predecessor, child_predecessor, work_package, child], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | predecessor | XX | manual
+ | child_predecessor | X | manual
+ | work_package | X..XX | automatic
+ | child | X..XX | automatic
+ TABLE
+ end
+ end
+
+ context "when the work package has an automatically scheduled child, " \
+ "a second manually scheduled child and a predecessor restricting it moving to an earlier date" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode | predecessors
+ | predecessor | XX | manual |
+ | work_package | XXXX | manual | predecessor
+ | child1 | X | manual |
+ | child2 | XX | automatic | child1
+ TABLE
+
+ it "reschedule the automatic child to start after the predecessor and parent dates span over both children dates" do
+ expect(subject).to be_success
+
+ expect_work_packages_after_reload([predecessor, work_package, child1, child2], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | predecessor | XX | manual
+ | work_package | XXXXX..X | automatic
+ | child1 | X | manual
+ | child2 | X..X | automatic
+ TABLE
+ end
+ end
+ end
+
+ context "when setting dates" do
+ before do
+ set_factory_default(:priority, priority)
+ set_factory_default(:project_with_types, project)
+ set_factory_default(:status, status)
+ set_factory_default(:type, type)
+ set_factory_default(:user, user)
+
+ set_non_working_week_days("saturday", "sunday")
+ end
+
+ context "on a manually scheduled parent having a predecessor" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode | predecessors
+ | predecessor | XX | manual |
+ | work_package | XXX | manual | predecessor
+ | child | X | manual |
+ TABLE
+
+ # change due date of work package from Wednesday to Friday
+ let(:attributes) { { due_date: work_package.due_date + 2.days } }
+
+ it "sets the dates to the given dates" do
+ expect(subject).to be_success
+
+ expect_work_packages_after_reload([work_package, predecessor, child], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | predecessor | XX | manual
+ | work_package | XXXXX | manual
+ | child | X | manual
+ TABLE
+ end
+ end
+ end
+
describe "removing the parent on a work package which precedes its sibling" do
let(:work_package_attributes) do
{ project_id: project.id,