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

Handle computed soonest start being a non-working day

Christophe Bliard3b7bfa375907
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 1d9f0a921e1..e29ff6a5ae4 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -337,6 +337,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
return unless current_start_date && work_package.schedule_automatically?
min_start = new_start_date_from_parent || new_start_date_from_self
+ min_start = days.soonest_working_day(min_start)
if min_start && (min_start > current_start_date || work_package.schedule_manually_changed?)
min_start
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rb
index c05f36c2c8d..e0a9372757d 100644
--- a/app/services/work_packages/shared/working_days.rb
+++ b/app/services/work_packages/shared/working_days.rb
@@ -38,10 +38,10 @@ module WorkPackages
end
def start_date(due_date, duration)
+ assert_strictly_positive_duration(duration)
return nil unless due_date && duration
- raise ArgumentError, 'duration must be strictly positive' if duration.is_a?(Integer) && duration <= 0
- start_date = due_date
+ start_date = latest_working_day(due_date)
until duration <= 1 && working?(start_date)
start_date -= 1
duration -= 1 if working?(start_date)
@@ -50,10 +50,10 @@ module WorkPackages
end
def due_date(start_date, duration)
+ assert_strictly_positive_duration(duration)
return nil unless start_date && duration
- raise ArgumentError, 'duration must be strictly positive' if duration.is_a?(Integer) && duration <= 0
- due_date = start_date
+ due_date = soonest_working_day(start_date)
until duration <= 1 && working?(due_date)
due_date += 1
duration -= 1 if working?(due_date)
@@ -106,6 +106,19 @@ module WorkPackages
OpenProject::FeatureDecisions.work_packages_duration_field_active? ? nil : 1
end
+ def assert_strictly_positive_duration(duration)
+ raise ArgumentError, 'duration must be strictly positive' if duration.is_a?(Integer) && duration <= 0
+ end
+
+ def latest_working_day(date)
+ return unless date
+
+ until working?(date)
+ date -= 1
+ end
+ date
+ end
+
def working_week_day?(date)
working_week_days[date.wday]
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 1cf241cd532..38b3eda92ee 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1592,6 +1592,7 @@ describe WorkPackages::SetAttributesService,
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)
@@ -1621,6 +1622,28 @@ describe WorkPackages::SetAttributesService,
end
end
+ context 'when the soonest start date is a non-working day' do
+ let(:saturday) { Time.zone.today.beginning_of_week.next_occurring(:saturday) }
+ let(:next_monday) { saturday.next_occurring(:monday) }
+ let(:soonest_start) { saturday }
+
+ before do
+ create(:week_days)
+ work_package.ignore_non_working_days = false
+ end
+
+ it_behaves_like 'service call' do
+ it 'sets the start date to the soonest possible start date being a working day' do
+ subject
+
+ expect(work_package).to have_attributes(
+ start_date: next_monday,
+ due_date: next_monday + 7.days
+ )
+ end
+ end
+ end
+
context 'when the soonest start date is before the current start date' do
let(:soonest_start) { Time.zone.today - 3.days }
diff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rb
index ee7bf9ff659..c0d2ef0ebb7 100644
--- a/spec/services/work_packages/shared/working_days_spec.rb
+++ b/spec/services/work_packages/shared/working_days_spec.rb
@@ -135,8 +135,10 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
include_examples 'start_date', due_date: monday_2022_08_01, duration: 5, expected: monday_2022_08_01 - 6.days
include_examples 'start_date', due_date: wednesday_2022_08_03, duration: 10, expected: wednesday_2022_08_03 - 13.days
- # really contrived one... Unlikely to happen.
+ # contrived one... But can happen when date is coming from an external entity, like soonest start.
include_examples 'start_date', due_date: saturday_2022_07_30, duration: 1, expected: friday_2022_07_29
+ include_examples 'start_date', due_date: saturday_2022_07_30, duration: 2, expected: friday_2022_07_29 - 1.day
+ include_examples 'start_date', due_date: saturday_2022_07_30, duration: 6, expected: friday_2022_07_29 - 7.days
end
context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do
@@ -177,8 +179,10 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
include_examples 'due_date', start_date: monday_2022_08_01, duration: 5, expected: monday_2022_08_01 + 4.days
include_examples 'due_date', start_date: wednesday_2022_08_03, duration: 10, expected: wednesday_2022_08_03 + 13.days
- # really contrived one... Unlikely to happen.
+ # contrived one... But can happen when date is coming from an external entity, like soonest start.
include_examples 'due_date', start_date: saturday_2022_07_30, duration: 1, expected: monday_2022_08_01
+ include_examples 'due_date', start_date: saturday_2022_07_30, duration: 2, expected: monday_2022_08_01 + 1.day
+ include_examples 'due_date', start_date: saturday_2022_07_30, duration: 6, expected: monday_2022_08_01 + 7.days
end
context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do