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

[#41821] Set dates during working days when setting parent

Christophe Bliard25bf903c3dbd
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index d2ff0270ea3..30438043919 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -56,14 +56,23 @@ class WorkPackages::SetScheduleService
private
+ # rubocop:disable Metrics/AbcSize
def schedule_by_parent
work_packages
.select { |wp| wp.start_date.nil? && wp.parent }
.each do |wp|
- wp.start_date = wp.parent.soonest_start
- wp.due_date = [wp.start_date, wp.due_date].compact.max
+ days = WorkPackages::Shared::Days.for(wp)
+ wp.start_date = days.soonest_working_day(wp.parent.soonest_start)
+ if wp.due_date || wp.duration
+ wp.due_date = [
+ wp.start_date,
+ days.due_date(wp.start_date, wp.duration),
+ wp.due_date
+ ].compact.max
+ end
end
end
+ # rubocop:enable Metrics/AbcSize
# Finds all work packages that need to be rescheduled because of a
# rescheduling of the service's work package and reschedules them.
diff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
index 90c897f4c9d..6d2a06512f9 100644
--- a/spec/services/work_packages/set_schedule_service_working_days_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
@@ -934,176 +934,267 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pac
CHART
end
end
+ end
+
+ context 'with a chain of followers' do
+ context 'when moving forward' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | XXX | follows work_package
+ follower2 | X..XXXX | follows follower1
+ follower3 | X..XXXX | follows follower2
+ follower4 | X..X | follows follower3
+ CHART
+
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | ] |
+ CHART
+ end
+
+ it 'reschedules each follower forward by the same delta' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | X..XX | follows work_package
+ follower2 | XXX..XX | follows follower1
+ follower3 | XXXX..X | follows follower2
+ follower4 | XX | follows follower3
+ CHART
+ end
+ end
+
+ context 'when moving forward with some space between the followers' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | XXX | follows work_package
+ follower2 | XXXX | follows follower1
+ follower3 | XXX..XX | follows follower2
+ follower4 | XX | follows follower3
+ CHART
+
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | ] |
+ CHART
+ end
+
+ it 'reschedules only the first followers as the others don\'t need to move' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSSm sm |
+ work_package | ] |
+ follower1 | X..XX |
+ follower2 | XXX..X |
+ CHART
+ end
+ end
+
+ context 'when moving backwards' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | XXX | follows work_package
+ follower2 | X..XXX | follows follower1
+ follower3 | XXX..XX | follows follower2
+ follower4 | XX | follows follower3
+ CHART
+
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | m sMTWTFSS |
+ work_package | ] |
+ CHART
+ end
+
+ it 'reschedules every follower backwards by the same delta' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | m sMTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | X..XX |
+ follower2 | XXX..X |
+ follower3 | XXXXX |
+ follower4 | XX |
+ CHART
+ end
+ end
+ end
+
+ context 'with a chain of followers with two paths leading to the same follower in the end' do
+ context 'when moving forward' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSm sm |
+ work_package | ] |
+ follower1 | XXX | follows work_package
+ follower2 | X..XXXX | follows follower1
+ follower3 | XX..X | follows work_package
+ follower4 | X..XX | follows follower2, follows follower3
+ CHART
- # context 'with a chain of successors' do
- # let(:follower1_start_date) { Time.zone.today + 1.day }
- # let(:follower1_due_date) { Time.zone.today + 3.days }
- # let(:follower2_start_date) { Time.zone.today + 4.days }
- # let(:follower2_due_date) { Time.zone.today + 8.days }
- # let(:follower3_start_date) { Time.zone.today + 9.days }
- # let(:follower3_due_date) { Time.zone.today + 10.days }
-
- # let!(:following) do
- # [following_work_package1,
- # following_work_package2,
- # following_work_package3]
- # end
-
- # context 'when moving forward' do
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
-
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today + 6.days, Time.zone.today + 8.days],
- # following_work_package2 => [Time.zone.today + 9.days, Time.zone.today + 13.days],
- # following_work_package3 => [Time.zone.today + 14.days, Time.zone.today + 15.days] }
- # end
- # end
- # end
-
- # context 'when moving forward with some space between the followers' do
- # let(:follower1_start_date) { Time.zone.today + 1.day }
- # let(:follower1_due_date) { Time.zone.today + 3.days }
- # let(:follower2_start_date) { Time.zone.today + 7.days }
- # let(:follower2_due_date) { Time.zone.today + 10.days }
- # let(:follower3_start_date) { Time.zone.today + 17.days }
- # let(:follower3_due_date) { Time.zone.today + 18.days }
-
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
-
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today + 6.days, Time.zone.today + 8.days],
- # following_work_package2 => [Time.zone.today + 9.days, Time.zone.today + 12.days] }
- # end
- # end
- # end
-
- # context 'when moving backwards' do
- # before do
- # work_package.due_date = Time.zone.today - 5.days
- # end
-
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today - 4.days, Time.zone.today - 2.days],
- # following_work_package2 => [Time.zone.today - 1.day, Time.zone.today + 3.days],
- # following_work_package3 => [Time.zone.today + 4.days, Time.zone.today + 5.days] }
- # end
- # end
- # end
- # end
-
- # context 'with a chain of successors with two paths leading to the same work package in the end' do
- # let_schedule(<<~CHART)
- # | MTWTFSS |
- # work_package | X |
- # follower1 | XXX | follows work_package
- # follower2 | XXXXX | follows follower1
- # follower3 | XXXX | follows work_package
- # follower4 | XX | follows follower2, after follower3
- # CHART
-
- # context 'when moving forward' do
- # before do
- # change_schedule(<<~CHART)
- # | MTWTFSS |
- # work_package | X |
- # CHART
- # end
-
- # it 'reschedules' do
- # expect_schedule(subject.all_results, <<~CHART)
- # | MTWTFSS |
- # work_package | X |
- # follower1 | XXX |
- # follower2 | XXXXX |
- # follower3 | XXXX |
- # follower4 | XX |
- # CHART
- # end
- # end
-
- # context 'when moving backwards' do
- # before do
- # change_schedule(<<~CHART)
- # | MTWTFSS |
- # work_package | X |
- # CHART
- # end
-
- # it 'reschedules' do
- # expect_schedule(subject.all_results, <<~CHART)
- # | MTWTFSS |
- # work_package | X |
- # follower1 | XXX |
- # follower2 | XXXXX |
- # follower3 | XXXX |
- # follower4 | XX |
- # CHART
- # end
- # end
- # end
-
- # context 'when setting the parent' do
- # let(:new_parent_work_package) { create(:work_package) }
- # let(:changed_attributes) { [:parent] }
-
- # before do
- # allow(new_parent_work_package)
- # .to receive(:soonest_start)
- # .and_return(soonest_date)
- # allow(work_package)
- # .to receive(:parent)
- # .and_return(new_parent_work_package)
- # end
-
- # context "with the parent being restricted in it's ability to be moved" do
- # let(:soonest_date) { Time.zone.today + 3.days }
-
- # it 'sets the start date to the earliest possible date' do
- # subject
-
- # expect(work_package.start_date).to eql(Time.zone.today + 3.days)
- # end
- # end
-
- # context 'with the parent being restricted but work package already having dates set' do
- # let(:soonest_date) { Time.zone.today + 3.days }
-
- # before do
- # work_package.start_date = Time.zone.today + 4.days
- # work_package.due_date = Time.zone.today + 5.days
- # end
-
- # it 'sets the dates to provided dates' do
- # subject
-
- # expect(work_package.start_date).to eql(Time.zone.today + 4.days)
- # expect(work_package.due_date).to eql(Time.zone.today + 5.days)
- # end
- # end
-
- # context 'with the parent being restricted but the attributes define an earlier date' do
- # let(:soonest_date) { Time.zone.today + 3.days }
-
- # before do
- # work_package.start_date = Time.zone.today + 1.day
- # work_package.due_date = Time.zone.today + 2.days
- # end
-
- # # This would be invalid but the dates should be set nevertheless
- # # so we can have a correct error handling.
- # it 'sets the dates to provided dates' do
- # subject
-
- # expect(work_package.start_date).to eql(Time.zone.today + 1.day)
- # expect(work_package.due_date).to eql(Time.zone.today + 2.days)
- # end
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | ] |
+ CHART
+ end
+
+ it 'reschedules followers while satisfying all constraints' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSSm sm sm |
+ work_package | ] |
+ follower1 | XXX |
+ follower2 | XX..XXX |
+ follower3 | XXX |
+ follower4 | XX..X |
+ CHART
+ end
+ end
+
+ context 'when moving backwards' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSm sm |
+ work_package | ] |
+ follower1 | XXX | follows work_package
+ follower2 | X..XXXX | follows follower1
+ follower3 | XX..X | follows work_package
+ follower4 | X..XX | follows follower2, follows follower3
+ CHART
+
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | m sMTWTFSS |
+ work_package | ] |
+ CHART
+ end
+
+ it 'reschedules every follower backwards by the same delta' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | m sMTWTFSSm |
+ work_package | ] |
+ follower1 | XX..X |
+ follower2 | XXXX..X |
+ follower3 | XXX |
+ follower4 | XXX |
+ CHART
+ end
+ end
+ end
+
+ context 'when setting the parent' do
+ let(:changed_attributes) { [:parent] }
+
+ context 'without dates and with the parent being restricted in its ability to be moved' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | |
+ new_parent | | follows new_parent_predecessor with delay 3
+ new_parent_predecessor | X |
+ CHART
+
+ before do
+ work_package.parent = new_parent
+ work_package.save
+ end
+
+ it 'schedules parent to start and end at soonest working start date and the child to start at the parent start' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | [ |
+ new_parent | X |
+ CHART
+ end
+ end
+
+ context 'without dates, with a duration and with the parent being restricted in its ability to be moved' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | | duration 4
+ new_parent | | follows new_parent_predecessor with delay 3
+ new_parent_predecessor | X |
+ CHART
+
+ before do
+ work_package.parent = new_parent
+ work_package.save
+ end
+
+ it 'schedules the moved work package to start at the parent soonest date and sets due date to keep the same duration ' \
+ 'and schedules the parent dates to match the child dates' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | XXXX |
+ new_parent | XXXX |
+ CHART
+ end
+ end
+
+ context 'with the parent being restricted in its ability to be moved and with a due date before parent constraint' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | ] |
+ new_parent | | follows new_parent_predecessor with delay 3
+ new_parent_predecessor | X |
+ CHART
+
+ before do
+ work_package.parent = new_parent
+ work_package.save
+ end
+
+ it 'schedules the moved work package to start and end at the parent soonest working start date' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | X |
+ new_parent | X |
+ CHART
+ end
+ end
+
+ context 'with the parent being restricted in its ability to be moved and with a due date after parent constraint' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | ] |
+ new_parent | | follows new_parent_predecessor with delay 3
+ new_parent_predecessor | X |
+ CHART
+
+ before do
+ work_package.parent = new_parent
+ work_package.save
+ end
+
+ it 'schedules the moved work package to start at the parent soonest working start date and keep the due date' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | X..XX |
+ new_parent | X..XX |
+ CHART
+ end
+ end
+
+ context 'with the parent being restricted but work package already has both dates set' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | XX |
+ new_parent | | follows new_parent_predecessor with delay 3
+ new_parent_predecessor | X |
+ CHART
+
+ before do
+ work_package.parent = new_parent
+ work_package.save
+ end
+
+ it 'does not reschedule the moved work package, and sets new parent dates to child dates' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | XX |
+ new_parent | XX |
+ CHART
+ end
+ end
end
end
diff --git a/spec/support/schedule_helpers/chart.rb b/spec/support/schedule_helpers/chart.rb
index d0d02cb9c88..4b4dafe5a73 100644
--- a/spec/support/schedule_helpers/chart.rb
+++ b/spec/support/schedule_helpers/chart.rb
@@ -130,6 +130,20 @@ module ScheduleHelpers
work_packages_attributes << attributes.merge(name: attributes[:subject].to_sym)
end
+ def set_duration(name, duration)
+ unless duration.is_a?(Integer) && duration > 0
+ raise ArgumentError, "unable to set duration for #{name}: " \
+ "duration must be a positive integer (got #{duration.inspect})"
+ end
+ attributes = work_package_attributes(name.to_sym)
+ dates_attributes = attributes.slice(:start_date, :due_date).compact
+ if dates_attributes.any?(&:present?)
+ raise ArgumentError, "unable to set duration for #{name}: " \
+ "#{dates_attributes.keys.join(' and ')} is set"
+ end
+ attributes[:duration] = duration
+ end
+
def add_follows_relation(predecessor:, follower:, delay:)
predecessors_by_follower(follower) << predecessor
delays_between[[predecessor, follower]] = delay
@@ -195,7 +209,7 @@ module ScheduleHelpers
in { start_date: nil, due_date: }
spaced_at(due_date, ']')
in { start_date:, due_date: }
- days = WorkPackages::Shared::WorkingDays.new
+ days = days_for(attributes)
span = (start_date..due_date).map do |date|
days.working?(date) ? 'X' : '.'
end.join
@@ -203,6 +217,14 @@ module ScheduleHelpers
end
end
+ def days_for(attributes)
+ if attributes[:ignore_non_working_days]
+ WorkPackages::Shared::AllDays.new
+ else
+ WorkPackages::Shared::WorkingDays.new
+ end
+ end
+
def next_monday
date = Time.zone.today
date += 1.day while date.wday != 1
diff --git a/spec/support/schedule_helpers/chart_builder.rb b/spec/support/schedule_helpers/chart_builder.rb
index f6fdc818e06..275ced89d40 100644
--- a/spec/support/schedule_helpers/chart_builder.rb
+++ b/spec/support/schedule_helpers/chart_builder.rb
@@ -59,7 +59,7 @@ module ScheduleHelpers
def use_work_packages(work_packages)
work_packages.each do |work_package|
- chart.add_work_package(work_package.slice(:subject, :start_date, :due_date))
+ chart.add_work_package(work_package.slice(:subject, :start_date, :due_date, :ignore_non_working_days))
end
chart
end
@@ -112,6 +112,8 @@ module ScheduleHelpers
parent: $1.to_sym,
child: name.to_sym
)
+ when /^duration (\d+)/
+ chart.set_duration(name, $1.to_i)
else
raise "unable to parse property #{property.inspect} for line #{name.inspect}"
end
diff --git a/spec/support_spec/schedule_helpers/chart_builder_spec.rb b/spec/support_spec/schedule_helpers/chart_builder_spec.rb
index 1ab3c85305b..4c29735777f 100644
--- a/spec/support_spec/schedule_helpers/chart_builder_spec.rb
+++ b/spec/support_spec/schedule_helpers/chart_builder_spec.rb
@@ -146,6 +146,16 @@ describe ScheduleHelpers::ChartBuilder do
expect(chart.parent(:main)).to eq(:parent)
end
end
+
+ describe 'duration <int>' do
+ it 'sets the duration of the work package' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | | duration 3
+ CHART
+ expect(chart.work_package_attributes(:main)).to include(duration: 3)
+ end
+ end
end
describe 'error handling' do
diff --git a/spec/support_spec/schedule_helpers/chart_spec.rb b/spec/support_spec/schedule_helpers/chart_spec.rb
index 468b53c7d99..7941e2f0206 100644
--- a/spec/support_spec/schedule_helpers/chart_spec.rb
+++ b/spec/support_spec/schedule_helpers/chart_spec.rb
@@ -119,7 +119,44 @@ describe ScheduleHelpers::Chart do
end
end
- describe '#to_s' do
+ describe '#set_duration' do
+ it 'sets the duration for a work package' do
+ chart.add_work_package(subject: 'wp')
+ chart.set_duration('wp', 3)
+ expect(chart.work_package_attributes('wp')).to include(duration: 3)
+ end
+
+ it 'must set the duration to a positive integer' do
+ chart.add_work_package(subject: 'wp')
+ expect { chart.set_duration('wp', 0) }
+ .to raise_error(ArgumentError, 'unable to set duration for wp: duration must be a positive integer (got 0)')
+
+ expect { chart.set_duration('wp', -5) }
+ .to raise_error(ArgumentError, 'unable to set duration for wp: duration must be a positive integer (got -5)')
+
+ expect { chart.set_duration('wp', 'hello') }
+ .to raise_error(ArgumentError, 'unable to set duration for wp: duration must be a positive integer (got "hello")')
+
+ expect { chart.set_duration('wp', '42') }
+ .to raise_error(ArgumentError, 'unable to set duration for wp: duration must be a positive integer (got "42")')
+ end
+
+ it 'cannot set the duration if the work package has dates' do
+ chart.add_work_package(subject: 'wp_start', start_date: monday)
+ expect { chart.set_duration('wp_start', 3) }
+ .to raise_error(ArgumentError, 'unable to set duration for wp_start: start_date is set')
+
+ chart.add_work_package(subject: 'wp_due', due_date: monday)
+ expect { chart.set_duration('wp_due', 3) }
+ .to raise_error(ArgumentError, 'unable to set duration for wp_due: due_date is set')
+
+ chart.add_work_package(subject: 'wp_both', start_date: monday, due_date: monday)
+ expect { chart.set_duration('wp_both', 3) }
+ .to raise_error(ArgumentError, 'unable to set duration for wp_both: start_date and due_date is set')
+ end
+ end
+
+ describe '#to_s', with_flag: { work_packages_duration_field_active: true } do
let!(:week_days) { create(:week_days) }
context 'with a chart built from ascii representation' do
@@ -150,7 +187,14 @@ describe ScheduleHelpers::Chart do
context 'with a chart built from real work packages' do
let(:work_package1) { build_stubbed(:work_package, subject: 'main', start_date: monday, due_date: tuesday) }
- let(:work_package2) { build_stubbed(:work_package, subject: 'other', start_date: tuesday, due_date: monday + 7.days) }
+ let(:work_package2) do
+ build_stubbed(:work_package, subject: 'working_days', ignore_non_working_days: false,
+ start_date: tuesday, due_date: monday + 7.days)
+ end
+ let(:work_package2bis) do
+ build_stubbed(:work_package, subject: 'all_days', ignore_non_working_days: true,
+ start_date: tuesday, due_date: monday + 7.days)
+ end
let(:work_package3) { build_stubbed(:work_package, subject: 'start_only', start_date: monday - 3.days) }
let(:work_package4) { build_stubbed(:work_package, subject: 'due_only', due_date: wednesday) }
let(:work_package5) { build_stubbed(:work_package, subject: 'no_dates') }
@@ -159,6 +203,7 @@ describe ScheduleHelpers::Chart do
[
work_package1,
work_package2,
+ work_package2bis,
work_package3,
work_package4,
work_package5
@@ -168,12 +213,13 @@ describe ScheduleHelpers::Chart do
it 'returns the same ascii representation without properties information' do
expect(chart.to_s).to eq(<<~CHART.chomp)
- days | MTWTFSS |
- main | XX |
- other | XXXX..X |
- start_only | [ |
- due_only | ] |
- no_dates | |
+ days | MTWTFSS |
+ main | XX |
+ working_days | XXXX..X |
+ all_days | XXXXXXX |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
CHART
end
end