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

2.8 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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.
#++

class WorkPackages::ScheduleDependency::Dependency
  def initialize(work_package, schedule_dependency)
    self.work_package = work_package
    self.schedule_dependency = schedule_dependency
  end

  attr_accessor :work_package,
                :schedule_dependency

  # Returns the work package ids that this work package directly depends on to
  # determine its own dates. This is used for the order of the dates
  # computations.
  #
  # The dates of a work package depend on its descendants and predecessors
  # dates.
  def dependent_ids
    @dependent_ids ||= (descendants + moving_predecessors).map(&:id).uniq
  end

  def moving_predecessors
    @moving_predecessors ||= follows_relations
      .map(&:to)
      .filter { |predecessor| schedule_dependency.moving?(predecessor) }
  end

  def soonest_start_date
    @soonest_start_date ||=
      follows_relations
        .filter_map(&:successor_soonest_start)
        .max
        .then { days.soonest_working_day(it) }
  end

  def start_date
    alive_descendants_dates.min
  end

  def due_date
    alive_descendants_dates.max
  end

  def has_descendants?
    alive_descendants.any?
  end

  def has_direct_or_indirect_predecessors?
    follows_relations.any?
  end

  private

  def descendants
    schedule_dependency.descendants(work_package)
  end

  def alive_descendants
    descendants.reject(&:destroyed?)
  end

  def follows_relations
    schedule_dependency.follows_relations(work_package)
  end

  def alive_descendants_dates
    alive_descendants.filter_map(&:due_date) + alive_descendants.filter_map(&:start_date)
  end

  def days
    WorkPackages::Shared::Days.for(work_package)
  end
end