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] Can compute duration based on defined non working days
Christophe Bliardf21bb85168c6
diff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rbnew file mode 100644index 00000000000..fcdaeb1c1eb--- /dev/null+++ b/app/services/work_packages/shared/all_days.rb@@ -0,0 +1,38 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++module WorkPackages+ module Shared+ class AllDays+ # Returns number of days between two dates.+ def duration(from_date, to_date)+ (from_date..to_date).count+ end+ end+ end+enddiff --git a/app/services/work_packages/shared/days.rb b/app/services/work_packages/shared/days.rbnew file mode 100644index 00000000000..eb25cb41805--- /dev/null+++ b/app/services/work_packages/shared/days.rb@@ -0,0 +1,40 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++module WorkPackages+ module Shared+ class Days+ # Returns the right day computation instance for the given instance.+ def self.for(work_package)+ return AllDays.new unless OpenProject::FeatureDecisions.work_packages_duration_field_active?++ work_package.ignore_non_working_days ? AllDays.new : WorkingDays.new+ end+ end+ end+enddiff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rbnew file mode 100644index 00000000000..6c772f55092--- /dev/null+++ b/app/services/work_packages/shared/working_days.rb@@ -0,0 +1,68 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++module WorkPackages+ module Shared+ class WorkingDays+ # Returns number of working days between two dates, excluding weekend days+ # and non working days.+ def duration(from_date, to_date)+ (from_date..to_date).count { working?(_1) }+ end++ private++ def working?(date)+ working_week_day?(date) && working_specific_date?(date)+ end++ def working_week_day?(date)+ working_week_days[date.wday]+ end++ def working_specific_date?(date)+ non_working_dates.exclude?(date)+ end++ def working_week_days+ return @working_week_days if defined?(@working_week_days)++ @working_week_days = [true] * 8+ WeekDay.pluck(:day, :working).each do |day, working|+ @working_week_days[day] = working+ end+ @working_week_days[0] = @working_week_days[7] # Sunday is 7 in iso or 0 in other implementations+ @working_week_days+ end++ def non_working_dates+ @non_working_dates ||= Set.new(NonWorkingDay.pluck(:date))+ end+ end+ end+enddiff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rbnew file mode 100644index 00000000000..a9c60ddee2e--- /dev/null+++ b/spec/services/work_packages/shared/all_days_spec.rb@@ -0,0 +1,77 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++require 'rails_helper'++# rubocop:disable Naming/VariableNumber+RSpec.describe WorkPackages::Shared::AllDays do+ subject { described_class.new }++ sunday_2022_07_31 = Date.new(2022, 7, 31)++ shared_examples 'it returns duration' do |expected_duration, from_date, to_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++ it "from #{from_date.strftime(from_date_format)} " \+ "to #{to_date.strftime(to_date_format)} " \+ "-> #{expected_duration}" \+ do+ expect(subject.duration(from_date, to_date)).to eq(expected_duration)+ end+ end++ describe '#duration' do+ context 'without any week days created' do+ it 'considers all days as working days and returns the number of days between two dates, inclusive' do+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 6)).to eq(7)+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 50)).to eq(51)+ end+ end++ context 'with Saturday and Sunday as weekend days' do+ let!(:week_days) { create(:week_days) }++ it 'considers all days as working days and returns the number of days between two dates, inclusive' do+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 6)).to eq(7)+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 50)).to eq(51)+ end+ end++ context 'with Christmas 2022-12-25 and new year\'s day 2023-01-01 defined as non working days' do+ let!(:christmas) { create(:non_working_day, date: Date.new(2022, 12, 25)) }+ let!(:new_year_day) { create(:non_working_day, date: Date.new(2023, 1, 1)) }++ include_examples 'it returns duration', 365, Date.new(2022, 1, 1), Date.new(2022, 12, 31)+ include_examples 'it returns duration', 365 * 2, Date.new(2022, 1, 1), Date.new(2023, 12, 31)+ end+ end+end+# rubocop:enable Naming/VariableNumberdiff --git a/spec/services/work_packages/shared/days_spec.rb b/spec/services/work_packages/shared/days_spec.rbnew file mode 100644index 00000000000..af2a5a5896e--- /dev/null+++ b/spec/services/work_packages/shared/days_spec.rb@@ -0,0 +1,64 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++require 'rails_helper'++RSpec.describe WorkPackages::Shared::Days do+ subject { described_class.new }++ describe '.for', with_flag: { work_packages_duration_field_active: true } do+ context 'for a work_package ignoring non working days' do+ let(:work_package) { build_stubbed(:work_package, ignore_non_working_days: true) }++ it 'returns an AllDays instance' do+ expect(described_class.for(work_package)).to be_an_instance_of(WorkPackages::Shared::AllDays)+ end+ end++ context 'for a work_package respecting non working days' do+ let(:work_package) { build_stubbed(:work_package, ignore_non_working_days: false) }++ it 'returns a WorkingDays instance' do+ expect(described_class.for(work_package)).to be_an_instance_of(WorkPackages::Shared::WorkingDays)+ end+ end++ context 'when work packages duration field is inactive', with_flag: { work_packages_duration_field_active: false } do+ it 'always returns an AllDays instance' do+ work_package = build_stubbed(:work_package)+ expect(described_class.for(work_package)).to be_an_instance_of(WorkPackages::Shared::AllDays)++ work_package = build_stubbed(:work_package, ignore_non_working_days: true)+ expect(described_class.for(work_package)).to be_an_instance_of(WorkPackages::Shared::AllDays)++ work_package = build_stubbed(:work_package, ignore_non_working_days: false)+ expect(described_class.for(work_package)).to be_an_instance_of(WorkPackages::Shared::AllDays)+ end+ end+ end+enddiff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rbnew file mode 100644index 00000000000..c294a2fb28f--- /dev/null+++ b/spec/services/work_packages/shared/working_days_spec.rb@@ -0,0 +1,95 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++require 'rails_helper'++# rubocop:disable Naming/VariableNumber+RSpec.describe WorkPackages::Shared::WorkingDays do+ subject { described_class.new }++ sunday_2022_07_31 = Date.new(2022, 7, 31)+ monday_2022_08_01 = Date.new(2022, 8, 1)+ wednesday_2022_08_03 = Date.new(2022, 8, 3)++ shared_examples 'it returns duration' do |expected_duration, from_date, to_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++ it "from #{from_date.strftime(from_date_format)} " \+ "to #{to_date.strftime(to_date_format)} " \+ "-> #{expected_duration}" \+ do+ expect(subject.duration(from_date, to_date)).to eq(expected_duration)+ end+ end++ describe '#duration' do+ context 'without any week days created' do+ it 'considers all days as working days and returns the number of days between two dates, inclusive' do+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 6)).to eq(7)+ expect(subject.duration(sunday_2022_07_31, sunday_2022_07_31 + 50)).to eq(51)+ end+ end++ context 'with Saturday and Sunday as weekend days' do+ let!(:week_days) { create(:week_days) }++ include_examples 'it returns duration', 0, sunday_2022_07_31, sunday_2022_07_31+ include_examples 'it returns duration', 5, sunday_2022_07_31, Date.new(2022, 8, 5)+ include_examples 'it returns duration', 5, sunday_2022_07_31, Date.new(2022, 8, 6)+ include_examples 'it returns duration', 5, sunday_2022_07_31, Date.new(2022, 8, 7)+ include_examples 'it returns duration', 6, sunday_2022_07_31, Date.new(2022, 8, 8)+ include_examples 'it returns duration', 7, sunday_2022_07_31, Date.new(2022, 8, 9)++ include_examples 'it returns duration', 1, monday_2022_08_01, monday_2022_08_01+ include_examples 'it returns duration', 5, monday_2022_08_01, Date.new(2022, 8, 5)+ include_examples 'it returns duration', 5, monday_2022_08_01, Date.new(2022, 8, 6)+ include_examples 'it returns duration', 5, monday_2022_08_01, Date.new(2022, 8, 7)+ include_examples 'it returns duration', 6, monday_2022_08_01, Date.new(2022, 8, 8)+ include_examples 'it returns duration', 7, monday_2022_08_01, Date.new(2022, 8, 9)++ include_examples 'it returns duration', 3, wednesday_2022_08_03, Date.new(2022, 8, 5)+ include_examples 'it returns duration', 3, wednesday_2022_08_03, Date.new(2022, 8, 6)+ include_examples 'it returns duration', 3, wednesday_2022_08_03, Date.new(2022, 8, 7)+ include_examples 'it returns duration', 4, wednesday_2022_08_03, Date.new(2022, 8, 8)+ include_examples 'it returns duration', 5, wednesday_2022_08_03, Date.new(2022, 8, 9)+ end++ context 'with Christmas 2022-12-25 and new year\'s day 2023-01-01 defined as non working days' do+ let!(:christmas) { create(:non_working_day, date: Date.new(2022, 12, 25)) }+ let!(:new_year_day) { create(:non_working_day, date: Date.new(2023, 1, 1)) }++ include_examples 'it returns duration', 0, Date.new(2022, 12, 25), Date.new(2022, 12, 25)+ include_examples 'it returns duration', 1, Date.new(2022, 12, 24), Date.new(2022, 12, 25)+ include_examples 'it returns duration', 8, Date.new(2022, 12, 24), Date.new(2023, 1, 2)+ end+ end+end+# rubocop:enable Naming/VariableNumber