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

8.9 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::UpdateAncestorsService < BaseServices::BaseCallable
  attr_accessor :user,
                :initiator_work_package

  def initialize(user:, work_package:)
    super()
    self.user = user
    self.initiator_work_package = work_package
  end

  def perform(attributes)
    updated_work_packages = update_current_and_former_ancestors(attributes)

    set_journal_note(ancestors(updated_work_packages))
    success = save_updated_work_packages(updated_work_packages)

    result = ServiceResult.new(success:, result: initiator_work_package)

    ancestors(updated_work_packages).each do |wp|
      result.add_dependent!(ServiceResult.new(success: !wp.changed?, result: wp))
    end

    result
  end

  private

  def initiator?(work_package)
    work_package == initiator_work_package
  end

  def ancestors(work_packages)
    work_packages.reject { initiator?(it) }
  end

  def update_current_and_former_ancestors(attributes)
    include_former_ancestors = attributes.intersect?(%i[parent_id parent])
    WorkPackages::UpdateAncestors::Loader
      .new(initiator_work_package, include_former_ancestors)
      .select do |ancestor, loader|
        derive_attributes(ancestor, loader, attributes)

        ancestor.changed?
      end
  end

  def save_updated_work_packages(updated_work_packages)
    updated_initiators, updated_ancestors = updated_work_packages.partition { initiator?(it) }

    # Send notifications for initiator updates
    success = updated_initiators.all? { |wp| wp.save(validate: false) }
    # Do not send notifications for parent updates
    success &&= Journal::NotificationConfiguration.with(false) do
      updated_ancestors.all? { |wp| wp.save(validate: false) }
    end
    success
  end

  def derive_attributes(work_package, loader, attributes)
    return unless modified_attributes_justify_derivation?(attributes)

    {
      # Derived estimated hours and Derived remaining hours need to be
      # calculated before the Derived done ratio below since the
      # aggregation depends on both derived fields.
      # Changes in any of these, also warrant a recalculation of
      # the Derived done ratio.
      #
      # Changes to estimated hours also warrant a recalculation of
      # derived done ratios in the work package's ancestry as the
      # derived estimated hours would affect the derived done ratio
      # or the derived remaining hours, depending on the % Complete mode
      # currently active.
      #
      %i[estimated_hours remaining_hours status status_id] => :derive_total_estimated_and_remaining_hours,
      %i[estimated_hours remaining_hours done_ratio status status_id] => :derive_done_ratio,
      %i[] => :switch_to_automatic_mode,
      %i[ignore_non_working_days] => :derive_ignore_non_working_days
    }.each do |derivative_attributes, method|
      if attributes.intersect?(derivative_attributes + %i[parent parent_id])
        send(method, work_package, loader)
      end
    end
  end

  def set_journal_note(work_packages)
    work_packages.each do |wp|
      wp.journal_notes = I18n.t("work_package.updated_automatically_by_child_changes", child: "##{initiator_work_package.id}")
    end
  end

  def derive_done_ratio(ancestor, loader)
    ancestor.derived_done_ratio = compute_derived_done_ratio(ancestor, loader)
  end

  def compute_derived_done_ratio(work_package, loader)
    return if no_children?(work_package, loader)

    if WorkPackage.work_weighted_average_mode?
      calculate_work_weighted_average_percent_complete(work_package)
    elsif WorkPackage.simple_average_mode?
      calculate_simple_average_percent_complete(work_package, loader)
    end
  end

  def calculate_work_weighted_average_percent_complete(work_package)
    return if work_package.derived_estimated_hours.nil? || work_package.derived_remaining_hours.nil?
    return if work_package.derived_estimated_hours.zero?

    work_done = (work_package.derived_estimated_hours - work_package.derived_remaining_hours)
    progress = (work_done.to_f / work_package.derived_estimated_hours) * 100
    progress.round
  end

  def calculate_simple_average_percent_complete(work_package, loader)
    all_done_ratios = children_done_ratio_values(work_package, loader)

    if work_package.done_ratio.present? && !work_package.status.excluded_from_totals
      all_done_ratios << work_package.done_ratio
    end

    return if all_done_ratios.empty?

    progress = all_done_ratios.sum.to_f / all_done_ratios.count
    progress.round
  end

  def children_done_ratio_values(work_package, loader)
    loader
      .children_of(work_package)
      .filter(&:included_in_totals_calculation?)
      .map { |child| child.derived_done_ratio || child.done_ratio || 0 }
  end

  # Switches the direct parent of the initiator to automatic scheduling mode if
  # it is manually scheduled and has no direct or indirect predecessors and no
  # other children.
  #
  # This method is called only when parent or parent_id attribute of the
  # initiator work package has been changed
  def switch_to_automatic_mode(work_package, loader)
    # it only applies to the initiator's direct parent
    return if initiator_work_package.parent_id.nil?
    return if initiator_work_package.parent_id != work_package.id

    # it only applies if there is no bulk duplicate in progress: if it's a copy, the copy must stay exact
    return if state.bulk_duplicate_in_progress

    # it only applies if the parent is manually scheduled
    return if work_package.schedule_automatically?

    # it only applies if the parent has no other children
    return if loader.children_of(work_package).count != 1

    # it only applies if the parent has no direct or indirect predecessors
    return if Relation.used_for_scheduling_of(work_package).any?

    # it can switch to automatic scheduling mode
    work_package.schedule_manually = false
  end

  # Sets the ignore_non_working_days to true if any descendant has its value set to true.
  # If there is no value returned from the descendants, that means that the work package in
  # question no longer has a descendant. But since we are in the service going up the ancestor chain,
  # such a work package is the former parent. The property of such a work package is reset to `false`.
  def derive_ignore_non_working_days(ancestor, loader)
    return if initiator?(ancestor)
    return if ancestor.schedule_manually

    descendant_value = ignore_non_working_days_of_descendants(ancestor, loader)

    if descendant_value.nil?
      descendant_value = ancestor.ignore_non_working_days
    end

    ancestor.ignore_non_working_days = descendant_value
  end

  def derive_total_estimated_and_remaining_hours(work_package, loader)
    work_package.derived_estimated_hours = derive_total(work_package, :estimated_hours, loader)
    work_package.derived_remaining_hours = derive_total(work_package, :remaining_hours, loader)
  end

  def derive_total(work_package, attribute, loader)
    return if no_children?(work_package, loader)

    work_packages = [work_package] + loader.descendants_of(work_package)
    values = work_packages
      .filter(&:included_in_totals_calculation?)
      .filter_map(&attribute)
    return if values.empty?

    values.sum.to_f
  end

  def no_children?(work_package, loader)
    loader.descendants_of(work_package).none?
  end

  def modified_attributes_justify_derivation?(attributes)
    attributes.intersect?(%i[
                            done_ratio
                            estimated_hours
                            ignore_non_working_days
                            parent parent_id
                            remaining_hours
                            status status_id
                          ])
  end

  def ignore_non_working_days_of_descendants(ancestor, loader)
    children = loader.children_of(ancestor)

    if children.any?
      children.any?(&:ignore_non_working_days)
    end
  end
end