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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#41821] Calculate due date from start date and duration
Christophe Bliard8e224102b7eb
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rbindex ba4e798b8cc..75209d11f24 100644--- a/app/services/work_packages/set_schedule_service.rb+++ b/app/services/work_packages/set_schedule_service.rb@@ -143,9 +143,14 @@ class WorkPackages::SetScheduleServicedef reschedule_to_date(scheduled, date)new_start_date = [scheduled.start_date, date].compact.max- set_dates(scheduled,- new_start_date,- (scheduled.due_date && !scheduled.duration.nil?) && (new_start_date + scheduled.duration - 1))+ # a new due date is set only if the moving work package already has one+ if scheduled.due_date+ new_due_date = WorkPackages::Shared::Days.for(scheduled).due_date(new_start_date, scheduled.duration)+ # Set new due date to new start date if work package previously had a due date and new due date is still not set+ new_due_date ||= new_start_date+ end++ set_dates(scheduled, new_start_date, new_due_date)enddef schedule_on_missing_dates(scheduled, min_start_date)diff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rbindex d8089b796ea..3a191fbb334 100644--- a/app/services/work_packages/shared/all_days.rb+++ b/app/services/work_packages/shared/all_days.rb@@ -30,10 +30,17 @@ module WorkPackagesmodule Sharedclass AllDays# Returns number of days between two dates, inclusive.- def duration(from_date, to_date)- return no_duration if from_date.nil? || to_date.nil?+ def duration(start_date, due_date)+ return no_duration if start_date.nil? || due_date.nil?- (from_date..to_date).count+ (start_date..due_date).count+ end++ def due_date(start_date, duration)+ return nil unless start_date && duration+ raise ArgumentError, 'duration must be strictly positive' if duration.is_a?(Integer) && duration <= 0++ start_date + duration - 1enddef add_days(date, count)diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rbindex cec65c00cec..a0e0ac1a3ec 100644--- a/app/services/work_packages/shared/working_days.rb+++ b/app/services/work_packages/shared/working_days.rb@@ -31,10 +31,22 @@ module WorkPackagesclass WorkingDays# Returns number of working days between two dates, excluding weekend days# and non working days.- def duration(from_date, to_date)- return no_duration if from_date.nil? || to_date.nil?+ def duration(start_date, due_date)+ return no_duration unless start_date && due_date- (from_date..to_date).count { working?(_1) }+ (start_date..due_date).count { working?(_1) }+ end++ def due_date(start_date, 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+ until duration <= 1 && working?(due_date)+ due_date += 1+ duration -= 1 if working?(due_date)+ end+ due_dateenddef add_days(date, count)@@ -50,6 +62,8 @@ module WorkPackagesenddef soonest_working_day(date)+ return unless date+until working?(date)date += 1enddiff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rbindex 642886cf752..fc7deb48350 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@@ -40,55 +40,55 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pacsubject { instance.call(changed_attributes) }- context 'when moving successor covers non-working days' do- let_schedule(<<~CHART, ignore_non_working_days: false)- days | MTWTFSS |- work_package | XX |- follower | XXX | follows work_package- CHART-- before do- change_schedule([work_package], <<~CHART)+ context 'with a single successor' do+ context 'when moving successor will cover non-working days' do+ let_schedule(<<~CHART, ignore_non_working_days: false)days | MTWTFSS |- work_package | XXXX |+ work_package | XX |+ follower | XXX | follows work_packageCHART- end- it 'extends to a later due date to keep the same duration' do- expect_schedule(subject.all_results, <<~CHART)- days | MTWTFSS |- work_package | XXXX |- follower | X..XX |- CHART- expect(follower.duration).to eq(3)- end- end+ before do+ change_schedule([work_package], <<~CHART)+ days | MTWTFSS |+ work_package | XXXX |+ CHART+ end- context 'when moved predecessor covers non-working days' do- let_schedule(<<~CHART, ignore_non_working_days: false)- days | MTWTFSS |- work_package | XX |- follower | XXX | follows work_package- CHART-- before do- change_schedule([work_package], <<~CHART)- days | MTWTFSS |- work_package | XX..XX |- CHART+ it 'extends to a later due date to keep the same duration' do+ expect_schedule(subject.all_results, <<~CHART)+ days | MTWTFSS |+ work_package | XXXX |+ follower | X..XX |+ CHART+ expect(follower.duration).to eq(3)+ endend- it 'extends to a later due date to keep the same duration' do- expect_schedule(subject.all_results, <<~CHART)+ context 'when moved predecessor covers non-working days' do+ let_schedule(<<~CHART, ignore_non_working_days: false)days | MTWTFSS |- work_package | XX..XX |- follower | XXX |+ work_package | XX |+ follower | XXX | follows work_packageCHART- expect(follower.duration).to eq(3)++ before do+ change_schedule([work_package], <<~CHART)+ days | MTWTFSS |+ work_package | XX..XX |+ CHART+ end++ it 'extends to a later due date to keep the same duration' do+ expect_schedule(subject.all_results, <<~CHART)+ days | MTWTFSS |+ work_package | XX..XX |+ follower | XXX |+ CHART+ expect(follower.duration).to eq(3)+ endend- end- context 'with a single successor' docontext 'when predecessor moved forward' docontext 'on a day in the middle on working days with the follower having only start date' dolet_schedule(<<~CHART, ignore_non_working_days: false)@@ -394,63 +394,160 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pacendend- # context 'when removing the dates on the predecessor' do- # before do- # work_package.start_date = work_package.due_date = nil- # end+ context 'when removing the dates on the moved predecessor' do+ context 'with the follower having start and due dates' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | XXX | follows work_package+ CHART- # it_behaves_like 'reschedules' do- # # The follower will keep its dates- # let(:expected) do- # {}- # end- # end+ before do+ change_schedule([work_package], <<~CHART)+ days | MTWTFSS |+ work_package | |+ CHART+ end- # context 'when the follower has no start date but a due date' do- # let(:follower1_start_date) { nil }- # let(:follower1_due_date) { Time.zone.today + 15.days }+ it 'does not reschedule and follower keeps its dates' do+ expect_schedule(subject.all_results, <<~CHART)+ days | MTWTFSS |+ work_package | |+ follower | XXX |+ CHART+ end+ end- # it_behaves_like 'reschedules' do- # # Nothing should be rescheduled- # let(:expected) do- # {}- # end- # end- # end- # end+ context 'with the follower having only a due date' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | ] | follows work_package+ CHART- # context 'when not moving and the successor not having start & due date (e.g. creating relation)' do- # let(:follower1_start_date) { nil }- # let(:follower1_due_date) { nil }+ before do+ change_schedule([work_package], <<~CHART)+ days | MTWTFSS |+ work_package | |+ CHART+ end- # it_behaves_like 'reschedules' do- # let(:expected) do- # { following_work_package1 => [work_package.due_date + 1.day, nil] }- # end- # end- # end+ it 'does not reschedule and follower keeps its dates' do+ expect_schedule(subject.all_results, <<~CHART)+ days | MTWTFSS |+ work_package | |+ follower | ] |+ CHART+ end+ end+ end- # context 'when not moving and the successor having due before predecessor due date (e.g. creating relation)' do- # let(:follower1_start_date) { nil }- # let(:follower1_due_date) { work_package_due_date - 5.days }+ context 'when only creating the relation between predecessor and follower' do+ context 'with follower having no dates' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | |+ CHART- # it_behaves_like 'reschedules' do- # let(:expected) do- # { following_work_package1 => [work_package.due_date + 1.day, work_package.due_date + 1.day] }- # end- # end- # end+ before do+ create(:follows_relation, from: follower, to: work_package)+ end- # context 'when not moving and the successor having start before predecessor due date (e.g. creating relation)' do- # let(:follower1_start_date) { work_package_due_date - 5.days }- # let(:follower1_due_date) { nil }+ it 'schedules follower to start right after its predecessor and does not set the due date' do+ expect(subject.all_results).to match_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | XX |+ follower | [ |+ CHART+ end+ end- # it_behaves_like 'reschedules' do- # let(:expected) do- # { following_work_package1 => [work_package.due_date + 1.day, nil] }- # end- # end- # end+ context 'with follower having only due date before predecessor due date' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | ] |+ CHART++ before do+ create(:follows_relation, from: follower, to: work_package)+ end++ it 'reschedules follower to start right after its predecessor and end the same day' do+ expect(subject.all_results).to match_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | XX |+ follower | X |+ CHART+ end+ end++ context 'with follower having only start date before predecessor due date' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | [ |+ CHART++ before do+ create(:follows_relation, from: follower, to: work_package)+ end++ it 'reschedules follower to start right after its predecessor and leaves the due date unset' do+ expect(subject.all_results).to match_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | XX |+ follower | [ |+ CHART+ end+ end++ context 'with follower having both start and due dates before predecessor due date' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | mtwtfssMTWTFSS |+ work_package | XX |+ follower | XXXX |+ CHART++ before do+ create(:follows_relation, from: follower, to: work_package)+ end++ it 'reschedules follower to start right after its predecessor and keeps the duration' do+ expect(subject.all_results).to match_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | XX |+ follower | XXX..X |+ CHART+ end+ end++ context 'with follower having due date long after predecessor due date' do+ let_schedule(<<~CHART, ignore_non_working_days: false)+ days | MTWTFSS |+ work_package | XX |+ follower | ] |+ CHART++ before do+ create(:follows_relation, from: follower, to: work_package)+ end++ it 'reschedules follower to start right after its predecessor and end the same day' do+ # TODO: is this correct? I would have expected this instead:+ # days | MTWTFSS |+ # work_package | XX |+ # follower | XXX |+ # Question sent to Niels to check if ok or not.+ expect(subject.all_results).to match_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | XX |+ follower | X |+ CHART+ end+ end+ end# context 'when not moving and the successor having start and due before predecessor due date (e.g. creating relation)' do# let(:follower1_start_date) { work_package_due_date - 5.days }diff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rbindex 237d1adc97c..fc1b75dec18 100644--- a/spec/services/work_packages/shared/all_days_spec.rb+++ b/spec/services/work_packages/shared/all_days_spec.rb@@ -54,7 +54,7 @@ RSpec.describe WorkPackages::Shared::AllDays doinclude_examples 'it returns duration', 365 * 2, Date.new(2022, 1, 1), Date.new(2023, 12, 31)end- context 'without from date', with_flag: { work_packages_duration_field_active: true } do+ context 'without start date', with_flag: { work_packages_duration_field_active: true } doit 'returns nil' doexpect(subject.duration(nil, sunday_2022_07_31)).to be_nilend@@ -66,7 +66,7 @@ RSpec.describe WorkPackages::Shared::AllDays doendend- context 'without to date', with_flag: { work_packages_duration_field_active: true } do+ context 'without due date', with_flag: { work_packages_duration_field_active: true } doit 'returns nil' doexpect(subject.duration(sunday_2022_07_31, nil)).to be_nilend@@ -79,6 +79,39 @@ RSpec.describe WorkPackages::Shared::AllDays doendend+ describe '#due_date' do+ it 'returns the due date for a start date and a duration' do+ expect(subject.due_date(sunday_2022_07_31, 1)).to eq(sunday_2022_07_31)+ expect(subject.due_date(sunday_2022_07_31, 10)).to eq(sunday_2022_07_31 + 9.days)+ end++ it 'raises an error if duration is 0 or negative' do+ expect { subject.due_date(sunday_2022_07_31, 0) }+ .to raise_error ArgumentError, 'duration must be strictly positive'+ expect { subject.due_date(sunday_2022_07_31, -10) }+ .to raise_error ArgumentError, 'duration must be strictly positive'+ end++ it 'returns nil if start_date is nil' do+ expect(subject.due_date(nil, 1)).to be_nil+ end++ it 'returns nil if duration is nil' do+ expect(subject.due_date(sunday_2022_07_31, nil)).to be_nil+ end++ context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do+ include_examples 'due_date', start_date: sunday_2022_07_31, duration: 1, expected: sunday_2022_07_31+ include_examples 'due_date', start_date: sunday_2022_07_31, duration: 5, expected: sunday_2022_07_31 + 4.days+ include_examples 'due_date', start_date: sunday_2022_07_31, duration: 10, expected: sunday_2022_07_31 + 9.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+ include_examples 'due_date', start_date: Date.new(2022, 1, 1), duration: 365, expected: Date.new(2022, 12, 31)+ include_examples 'due_date', start_date: Date.new(2022, 1, 1), duration: 365 * 2, expected: Date.new(2023, 12, 31)+ end+ end+describe '#add_days' doit 'adds the number of days to the date' doexpect(subject.add_days(sunday_2022_07_31, 7)).to eq(Date.new(2022, 8, 7))@@ -114,6 +147,10 @@ RSpec.describe WorkPackages::Shared::AllDays doexpect(subject.soonest_working_day(sunday_2022_07_31)).to eq(sunday_2022_07_31)end+ it 'returns nil if given date is nil' do+ expect(subject.soonest_working_day(nil)).to be_nil+ end+context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday doit 'returns the given day' doexpect(subject.soonest_working_day(sunday_2022_07_31)).to eq(sunday_2022_07_31)diff --git a/spec/services/work_packages/shared/shared_examples_days.rb b/spec/services/work_packages/shared/shared_examples_days.rbindex 379341b9cd5..0cd07aca54b 100644--- a/spec/services/work_packages/shared/shared_examples_days.rb+++ b/spec/services/work_packages/shared/shared_examples_days.rb@@ -26,6 +26,8 @@# See COPYRIGHT and LICENSE files for more details.#+++Date::DATE_FORMATS[:wday_date] = '%a %-d %b %Y' # Fri 5 Aug 2022+RSpec.shared_context 'with weekend days Saturday and Sunday' doshared_let(:week_days) { create(:week_days) }end@@ -40,45 +42,44 @@ RSpec.configure do |rspec|rspec.include_context 'with non working days Christmas 2022 and new year 2023', :christmas_2022_new_year_2023end-RSpec.shared_examples 'it returns duration' do |expected_duration, from_date, to_date|+RSpec.shared_examples 'it returns duration' do |expected_duration, start_date, due_date|from_date_format = '%a %-d'- to_date_format = '%a %-d %b %Y'- from_date_format += ' %b' if [from_date.month, from_date.year] != [to_date.month, to_date.year]- from_date_format += ' %Y' if from_date.year != to_date.year+ from_date_format += ' %b' if [start_date.month, start_date.year] != [due_date.month, due_date.year]+ from_date_format += ' %Y' if start_date.year != due_date.year- it "from #{from_date.strftime(from_date_format)} " \- "to #{to_date.strftime(to_date_format)} " \+ it "from #{start_date.strftime(from_date_format)} " \+ "to #{due_date.to_fs(:wday_date)} " \"=> #{expected_duration}" \do- expect(subject.duration(from_date, to_date)).to eq(expected_duration)+ expect(subject.duration(start_date, due_date)).to eq(expected_duration)endend-RSpec.shared_examples 'add_days returns date' do |date:, count:, expected:|- date_format = '%a %-d %b %Y'+RSpec.shared_examples 'due_date' do |start_date:, duration:, expected:|+ it "due_date(#{start_date.to_fs(:wday_date)}, #{duration}) => #{expected.to_fs(:wday_date)}" do+ expect(subject.due_date(start_date, duration)).to eq(expected)+ end+end- it "add_days(#{date.strftime(date_format)}, #{count}) => #{expected.strftime(date_format)}" do+RSpec.shared_examples 'add_days returns date' do |date:, count:, expected:|+ it "add_days(#{date.to_fs(:wday_date)}, #{count}) => #{expected.to_fs(:wday_date)}" doexpect(subject.add_days(date, count)).to eq(expected)endendRSpec.shared_examples 'soonest working day' do |date:, expected:|- date_format = '%a %-d %b %Y'-- it "soonest_working_day(#{date.strftime(date_format)}) => #{expected.strftime(date_format)}" do+ it "soonest_working_day(#{date.to_fs(:wday_date)}) => #{expected.to_fs(:wday_date)}" doexpect(subject.soonest_working_day(date)).to eq(expected)endendRSpec.shared_examples 'delta' do |previous:, current:, expected:|- date_format = '%a %-d %b %Y'-- it "delta(previous: #{previous.strftime(date_format)}, current: #{current.strftime(date_format)}) => #{expected} days" do+ it "delta(previous: #{previous.to_fs(:wday_date)}, current: #{current.to_fs(:wday_date)}) => #{expected} days" doexpect(subject.delta(previous:, current:)).to eq(expected)end# check inverse: delta(a, b) == -delta(b, a)- it "delta(previous: #{current.strftime(date_format)}, current: #{previous.strftime(date_format)}) => #{-expected} days" do+ it "delta(previous: #{current.to_fs(:wday_date)}, current: #{previous.to_fs(:wday_date)}) => #{-expected} days" doexpect(subject.delta(previous: current, current: previous)).to eq(-expected)endenddiff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rbindex 5e116408004..e6fde150ad9 100644--- a/spec/services/work_packages/shared/working_days_spec.rb+++ b/spec/services/work_packages/shared/working_days_spec.rb@@ -39,6 +39,10 @@ RSpec.describe WorkPackages::Shared::WorkingDays dowednesday_2022_08_03 = Date.new(2022, 8, 3)describe '#duration' do+ it 'returns the duration for a given start date and due date' do+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 6)).to eq(7)+ end+context 'without any week days created' doit 'considers all days as working days and returns the number of days between two dates, inclusive' doexpect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 6)).to eq(7)@@ -74,7 +78,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'it returns duration', 8, Date.new(2022, 12, 24), Date.new(2023, 1, 2)end- context 'without from date', with_flag: { work_packages_duration_field_active: true } do+ context 'without start date', with_flag: { work_packages_duration_field_active: true } doit 'returns nil' doexpect(subject.duration(nil, sunday_2022_07_31)).to be_nilend@@ -86,7 +90,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doendend- context 'without to date', with_flag: { work_packages_duration_field_active: true } do+ context 'without due date', with_flag: { work_packages_duration_field_active: true } doit 'returns nil' doexpect(subject.duration(sunday_2022_07_31, nil)).to be_nilend@@ -99,6 +103,48 @@ RSpec.describe WorkPackages::Shared::WorkingDays doendend+ describe '#due_date' do+ it 'returns the due date for a start date and a duration' do+ expect(subject.due_date(monday_2022_08_01, 1)).to eq(monday_2022_08_01)+ end++ it 'raises an error if duration is 0 or negative' do+ expect { subject.due_date(monday_2022_08_01, 0) }+ .to raise_error ArgumentError, 'duration must be strictly positive'+ expect { subject.due_date(monday_2022_08_01, -10) }+ .to raise_error ArgumentError, 'duration must be strictly positive'+ end++ it 'returns nil if start_date is nil' do+ expect(subject.due_date(nil, 1)).to be_nil+ end++ it 'returns nil if duration is nil' do+ expect(subject.due_date(monday_2022_08_01, nil)).to be_nil+ end++ context 'without any week days created' do+ it 'returns the due date considering all days as working days' do+ expect(subject.due_date(monday_2022_08_01, 1)).to eq(monday_2022_08_01)+ expect(subject.due_date(monday_2022_08_01, 7)).to eq(monday_2022_08_01 + 6) # Sunday of same week+ end+ end++ context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do+ include_examples 'due_date', start_date: monday_2022_08_01, duration: 1, expected: monday_2022_08_01+ 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.+ include_examples 'due_date', start_date: saturday_2022_07_30, duration: 1, expected: monday_2022_08_01+ end++ context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ include_examples 'due_date', start_date: Date.new(2022, 12, 24), duration: 2, expected: Date.new(2022, 12, 26)+ include_examples 'due_date', start_date: Date.new(2022, 12, 24), duration: 8, expected: Date.new(2023, 1, 2)+ end+ end+describe '#add_days' doit 'when positive, adds the number of working days to the date, ignoring non-working days' docreate(:week_day, day: 5, working: false)@@ -158,6 +204,10 @@ RSpec.describe WorkPackages::Shared::WorkingDays doexpect(subject.soonest_working_day(sunday_2022_07_31)).to eq(sunday_2022_07_31)end+ it 'returns nil if given date is nil' do+ expect(subject.soonest_working_day(nil)).to be_nil+ end+context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday doinclude_examples 'soonest working day', date: friday_2022_07_29, expected: friday_2022_07_29include_examples 'soonest working day', date: saturday_2022_07_30, expected: monday_2022_08_01