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

Shift dates to soonest working day if needed

Christophe Bliarda332a15005f3
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 5cf8171f213..1d9f0a921e1 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -250,6 +250,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def update_dates
unify_dates if work_package_now_milestone?
+ shift_dates_to_soonest_working_days unless work_package.ignore_non_working_days?
min_start = new_start_date
@@ -264,6 +265,11 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
work_package.start_date = work_package.due_date = unified_date
end
+ def shift_dates_to_soonest_working_days
+ work_package.start_date = days.soonest_working_day(work_package.start_date)
+ work_package.due_date = days.soonest_working_day(work_package.due_date)
+ end
+
def update_duration
work_package.duration = 1 if work_package.milestone?
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 97ad239078c..1cf241cd532 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1024,6 +1024,7 @@ describe WorkPackages::SetAttributesService,
let(:tuesday) { monday + 1.day }
let(:wednesday) { monday + 2.days }
let(:friday) { monday + 4.days }
+ let(:sunday) { monday + 6.days }
let(:next_monday) { monday + 7.days }
let(:next_tuesday) { monday + 8.days }
@@ -1125,6 +1126,38 @@ describe WorkPackages::SetAttributesService,
end
end
+ context 'when "ignore non-working days" is switched to false and "start date" is on a non-working day' do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: monday - 1.day, due_date: friday, ignore_non_working_days: true)
+ end
+ let(:call_attributes) { { ignore_non_working_days: false } }
+
+ it_behaves_like 'service call' do
+ it "updates the start date to be on next working day, and due date to accomodate duration" do
+ expect { subject }
+ .to change { work_package.slice(:start_date, :due_date, :duration) }
+ .from(start_date: monday - 1.day, due_date: friday, duration: 6)
+ .to(start_date: monday, due_date: next_monday, duration: 6)
+ end
+ end
+ end
+
+ context 'when "ignore non-working days" is switched to false and "finish date" is on a non-working day' do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: nil, due_date: sunday, ignore_non_working_days: true)
+ end
+ let(:call_attributes) { { ignore_non_working_days: false } }
+
+ it_behaves_like 'service call' do
+ it "updates the finish date to be on next working day" do
+ expect { subject }
+ .to change { work_package.slice(:start_date, :due_date, :duration) }
+ .from(start_date: nil, due_date: sunday, duration: nil)
+ .to(start_date: nil, due_date: next_monday, duration: nil)
+ end
+ end
+ end
+
context 'when "ignore non-working days" is changed AND "finish date" is cleared' do
let(:work_package) do
build_stubbed(:work_package, start_date: monday, due_date: next_monday, ignore_non_working_days: true)