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
Budget Relations & Fixed Budgets (#19543)
Klaus Zanders75ebb91cab1f
diff --git a/modules/budgets/app/contracts/budgets/base_contract.rb b/modules/budgets/app/contracts/budgets/base_contract.rbindex b323bbf985b..fc7468ae6f3 100644--- a/modules/budgets/app/contracts/budgets/base_contract.rb+++ b/modules/budgets/app/contracts/budgets/base_contract.rb@@ -38,6 +38,8 @@ module Budgetsattribute :descriptionattribute :fixed_dateattribute :project+ attribute :state+ attribute :supplementary_amountattribute :new_material_budget_item_attributes,readable: falsediff --git a/modules/budgets/app/controllers/budget_relations_controller.rb b/modules/budgets/app/controllers/budget_relations_controller.rbnew file mode 100644index 00000000000..c73b8532f11--- /dev/null+++ b/modules/budgets/app/controllers/budget_relations_controller.rb@@ -0,0 +1,65 @@+# 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 BudgetRelationsController < ApplicationController+ include AttachableServiceCall++ before_action :find_budget, only: %i[index edit update new create destroy]+ before_action :authorize++ menu_item :budgets++ def index+ @parent_relations = @budget.parent_budget_relations.includes(parent_budget: :project)+ @child_relations = @budget.child_budget_relations.includes(child_budget: :project)+ end++ def new+ end++ def edit+ end++ def create+ end++ def update+ end++ def destroy+ end++ private++ def find_budget+ @budget = Budget.includes(:project, :author).find_by(id: params[:budget_id])+ @project = @budget.project if @budget+ end+enddiff --git a/modules/budgets/app/models/budget.rb b/modules/budgets/app/models/budget.rbindex 4bde0cdc4e6..099cf93d5d2 100644--- a/modules/budgets/app/models/budget.rb+++ b/modules/budgets/app/models/budget.rb@@ -46,6 +46,16 @@ class Budget < ApplicationRecordhas_many :cost_entries, through: :work_packageshas_many :time_entries, through: :work_packages+ has_many :parent_budget_relations, class_name: "BudgetRelation",+ foreign_key: "child_budget_id",+ dependent: :destroy,+ inverse_of: :child_budget++ has_many :child_budget_relations, class_name: "BudgetRelation",+ foreign_key: "parent_budget_id",+ dependent: :destroy,+ inverse_of: :parent_budget+include ActiveModel::ForbiddenAttributesProtectionacts_as_attachable@@ -55,9 +65,17 @@ class Budget < ApplicationRecordtitle: Proc.new { |o| "#{I18n.t(:label_budget)} ##{o.id}: #{o.subject}" },url: Proc.new { |o| { controller: "budgets", action: "show", id: o.id } }- validates_presence_of :subject, :project, :author, :fixed_date- validates_length_of :subject, maximum: 255- validates_length_of :subject, minimum: 1+ validates :subject, :project, :author, :fixed_date, presence: true+ validates :subject, length: { maximum: 255 }+ validates :subject, length: { minimum: 1 }++ enum :state, {+ planned: "planned",+ draft: "draft",+ submitted: "submitted",+ approved: "approved",+ rejected: "rejected"+ }, validate: { allow_nil: true }class << selfdef visible(user)@@ -79,7 +97,8 @@ class Budget < ApplicationRecordprotecteddef copy_attributes(source)- source.attributes.slice("project_id", "subject", "description", "fixed_date").merge("author" => User.current)+ source.attributes.slice("project_id", "subject", "description", "fixed_date", "state",+ "supplementary_amount").merge("author" => User.current)enddef copy_budget_items(source, sink, items:)@@ -98,7 +117,26 @@ class Budget < ApplicationRecordenddef budget- material_budget + labor_budget+ supplementary_amount + material_budget + labor_budget + budget_added_by_children+ end++ def budget_added_by_children+ # TODO: Efficient with query+ @budget_added_by_children ||= child_budget_relations.add.includes(:child_budget).sum do |rel|+ rel.child_budget.budget+ end+ end++ def allocated_to_children+ # TODO: Efficient with query+ @allocated_to_children ||= child_budget_relations.includes(:child_budget).sum { |rel| rel.child_budget.budget }+ end++ def spent_with_children+ # TODO: Efficient with query+ @spent_with_children ||= spent + child_budget_relations.includes(:child_budget).sum do |rel|+ rel.child_budget.spent_with_children+ endenddef type_label@@ -113,7 +151,9 @@ class Budget < ApplicationRecorddef budget_ratioreturn 0.0 if budget.nil? || budget == 0.0- ((spent / budget) * 100).round+ gone = spent + allocated_to_children++ ((gone / budget) * 100).roundenddef css_classes@@ -165,7 +205,7 @@ class Budget < ApplicationRecordenddef available- budget - spent+ budget - spent - allocated_to_childrenenddef new_material_budget_item_attributes=(material_budget_item_attributes)diff --git a/modules/budgets/app/models/budget_relation.rb b/modules/budgets/app/models/budget_relation.rbnew file mode 100644index 00000000000..2d2a3e49556--- /dev/null+++ b/modules/budgets/app/models/budget_relation.rb@@ -0,0 +1,38 @@+# 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 BudgetRelation < ApplicationRecord+ belongs_to :parent_budget, class_name: "Budget"+ belongs_to :child_budget, class_name: "Budget"++ enum :relation_type, { add: "add", subtract: "subtract" }++ validates :child_budget, uniqueness: true+enddiff --git a/modules/budgets/app/models/queries/projects/selects/budget_allocated.rb b/modules/budgets/app/models/queries/projects/selects/budget_allocated.rbnew file mode 100644index 00000000000..2f5998741ee--- /dev/null+++ b/modules/budgets/app/models/queries/projects/selects/budget_allocated.rb@@ -0,0 +1,39 @@+# 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 Queries::Projects::Selects::BudgetAllocated < Queries::Selects::Base+ def self.key+ :budget_allocated+ end++ def caption+ I18n.t(:label_budget_allocated)+ end+enddiff --git a/modules/budgets/app/views/budget_relations/index.html.erb b/modules/budgets/app/views/budget_relations/index.html.erbnew file mode 100644index 00000000000..d55ed1eeac5--- /dev/null+++ b/modules/budgets/app/views/budget_relations/index.html.erb@@ -0,0 +1,5 @@+<h1>Hallo Welt</h1>++<%= @child_relations.inspect %>++<%= @parent_relations.inspect %>diff --git a/modules/budgets/app/views/budgets/_show.html.erb b/modules/budgets/app/views/budgets/_budget_items.html.erbsimilarity index 100%rename from modules/budgets/app/views/budgets/_show.html.erbrename to modules/budgets/app/views/budgets/_budget_items.html.erbdiff --git a/modules/budgets/app/views/budgets/_form.html.erb b/modules/budgets/app/views/budgets/_form.html.erbindex d0d5e86f65a..b9d39f5d825 100644--- a/modules/budgets/app/views/budgets/_form.html.erb+++ b/modules/budgets/app/views/budgets/_form.html.erb@@ -42,6 +42,22 @@ See COPYRIGHT and LICENSE files for more details.<div class="form--field"><%= f.date_picker :fixed_date %></div>+<div class="form--field">+ <%= f.text_field :supplementary_amount,+ size: 12,+ inputmode: :decimal,+ container_class: "-wide",+ value: unitless_currency_number(@budget.supplementary_amount) %>+ <span class="form-label">+ <%= Setting.costs_currency %>+ </span>+</div>++<div class="form--field">+ <%= f.select :state,+ Budget.states.keys.map { |state| [I18n.t(state, scope: %i[activerecord attributes budget states]), state] },+ { include_blank: true } %>+</div><%= render partial: "budgets/subform/material_budget_subform" %><%= render partial: "budgets/subform/labor_budget_subform" %>diff --git a/modules/budgets/app/views/budgets/_list.html.erb b/modules/budgets/app/views/budgets/_list.html.erbindex cb3b8db4a50..05dd8186a11 100644--- a/modules/budgets/app/views/budgets/_list.html.erb+++ b/modules/budgets/app/views/budgets/_list.html.erb@@ -39,11 +39,13 @@ See COPYRIGHT and LICENSE files for more details.<col><col><col>+ <col></colgroup><thead><tr><%= sort_header_tag("id", caption: "#", default_order: "desc") %><%= sort_header_tag("subject", caption: Budget.human_attribute_name(:subject)) %>+ <%= sort_header_tag("state", caption: Budget.human_attribute_name(:state)) %><th class="currency"><div class="generic-table--sort-header-outer"><div class="generic-table--sort-header">@@ -91,6 +93,11 @@ See COPYRIGHT and LICENSE files for more details.<tr id="budget-<%= budget.id %>" class="<%= budget.css_classes %>"><td><%= link_to budget.id, budget_path(budget.id) %></td><%= content_tag(:td, link_to(h(budget.subject), budget_path(budget.id)), class: "subject") %>+ <td>+ <% if budget.state.present? %>+ <%= I18n.t(budget.state, scope: %i[activerecord attributes budget states]) %>+ <% else %> - <% end %>+ </td><%= content_tag(:td, number_to_currency(budget.budget, precision: 0), class: "currency") %><%= content_tag(:td, number_to_currency(budget.spent, precision: 0), class: "currency") %><%= content_tag(:td, number_to_currency(budget.available, precision: 0), class: "currency") %>@@ -105,6 +112,7 @@ See COPYRIGHT and LICENSE files for more details.<% end %><% if budgets.length > 0 %><tr>+ <td /><td /><td /><td class="currency"><strong><%= number_to_currency(total_budget, precision: 0) %></strong></td>diff --git a/modules/budgets/app/views/budgets/show.html.erb b/modules/budgets/app/views/budgets/show.html.erbindex 7c520e817f2..25484402d2b 100644--- a/modules/budgets/app/views/budgets/show.html.erb+++ b/modules/budgets/app/views/budgets/show.html.erb@@ -34,9 +34,11 @@ See COPYRIGHT and LICENSE files for more details.<h3><%= h @budget.subject %></h3><p class="author"><%= authoring @budget.created_at, @budget.author %>.- <%= t(:label_updated_time, value: distance_of_time_in_words(Time.now, @budget.updated_at)) + "." if @budget.created_at != @budget.updated_at %>+ <%= "#{t(:label_updated_time, value: distance_of_time_in_words(Time.zone.now, @budget.updated_at))}." if @budget.created_at != @budget.updated_at %></p>+ <div class="grid-block">+ <div class="grid-content medium-6"><%= render(AttributeGroups::AttributeGroupComponent.new) do |component|component.with_attribute(key: Budget.human_attribute_name(:type), value: @budget.type_label)component.with_attribute(key: Budget.human_attribute_name(:fixed_date), value: format_date(@budget.fixed_date))@@ -44,6 +46,10 @@ See COPYRIGHT and LICENSE files for more details.key: Budget.human_attribute_name(:budget_ratio),value: extended_progress_bar(@budget.budget_ratio, width: "80px", legend: @budget.budget_ratio))+ component.with_attribute(+ key: Budget.human_attribute_name(:state),+ value: @budget.state.present? ? I18n.t(@budget.state, scope: %i[activerecord attributes budget states]) : "-"+ )component.with_attribute(key: Budget.human_attribute_name(:description),value: content_tag(:div, class: "op-uc-container") do@@ -51,11 +57,67 @@ See COPYRIGHT and LICENSE files for more details.end)end %>+ </div>+ <div class="grid-content medium-6">++ <%= render(AttributeGroups::AttributeGroupComponent.new) do |component|+ component.with_header(title: "Totals")+ component.with_attribute(+ key: Budget.human_attribute_name(:supplementary_amount),+ value: number_to_currency(@budget.supplementary_amount)+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:material_budget),+ value: number_to_currency(@budget.material_budget)+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:labor_budget),+ value: number_to_currency(@budget.labor_budget)+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:budget),+ value: content_tag(:strong, number_to_currency(@budget.budget))+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:spent),+ value: number_to_currency(@budget.spent * -1)+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:allocated_to_children),+ value: number_to_currency(@budget.allocated_to_children * -1)+ )+ component.with_attribute(+ key: Budget.human_attribute_name(:available),+ value: content_tag(:strong, number_to_currency(@budget.available))+ )+ end %>+ </div>+ </div><% resource = budget_attachment_representer(@budget) %><%= list_attachments(resource) %>- <%= render partial: "show" %>+ <%= render partial: "budget_items" %>++<h2>Child Budgets</h2>+ <table>+ <tr>+ <th>Budget</th>+ <th>Projekt</th>+ <th>Art</th>+ <th>Betrag</th>+ </tr>++<%- @budget.child_budget_relations.includes(child_budget: :project).find_each do |rel| -%>+<tr>+ <td> <%= link_to rel.child_budget.subject, budget_path(rel.child_budget) %> </td>+ <td> <%= link_to rel.child_budget.project.name, project_path(rel.child_budget.project) %> </td>+ <td> <%= rel.relation_type %> </td>+ <td> <%= number_to_currency(rel.child_budget.budget) %></td>+ </tr>+<%- end %>+ </table>+</div><div style="clear: both;"></div>diff --git a/modules/budgets/config/locales/en.yml b/modules/budgets/config/locales/en.ymlindex e4c62c7369e..06afef217ea 100644--- a/modules/budgets/config/locales/en.yml+++ b/modules/budgets/config/locales/en.yml@@ -29,13 +29,14 @@---en:plugin_budgets_engine:- name: 'Budgets'+ name: "Budgets"activerecord:attributes:budget:author: "Author"available: "Available"+ allocated_to_children: "Allocated"budget: "Planned"budget_ratio: "Spent (ratio)"description: "Description"@@ -45,6 +46,14 @@ en:type: "Cost type"labor_budget: "Planned labor costs"material_budget: "Planned unit costs"+ supplementary_amount: "Supplementary amount"+ state: "State"+ states:+ planned: "Planned"+ draft: "Draft"+ submitted: "Submitted"+ approved: "Approved"+ rejected: "Rejected"work_package:budget_subject: "Budget title"models:@@ -84,8 +93,9 @@ en:label_budget_plural: "Budgets"label_budget_spent: "Budget spent"label_budget_spent_ratio: "Budget spent ratio"+ label_budget_allocated: "Budget allocated to children"label_deliverable: "Budget"- label_example_placeholder: 'e.g., %{decimal}'+ label_example_placeholder: "e.g., %{decimal}"label_view_all_budgets: "View all budgets"label_yes: "Yes"diff --git a/modules/budgets/config/routes.rb b/modules/budgets/config/routes.rbindex 509536fe39a..f0dd5f2e5ba 100644--- a/modules/budgets/config/routes.rb+++ b/modules/budgets/config/routes.rb@@ -35,6 +35,8 @@ Rails.application.routes.draw doendresources :budgets, only: %i[show update destroy edit] do+ resources :budget_relations, path: :relations, except: %i[show]+get :copy, on: :memberget :destroy_info, on: :memberenddiff --git a/modules/budgets/db/migrate/20250714113219_create_budget_relation.rb b/modules/budgets/db/migrate/20250714113219_create_budget_relation.rbnew file mode 100644index 00000000000..14cfa430dfd--- /dev/null+++ b/modules/budgets/db/migrate/20250714113219_create_budget_relation.rb@@ -0,0 +1,43 @@+# 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 CreateBudgetRelation < ActiveRecord::Migration[8.0]+ def change+ create_table :budget_relations do |t|+ t.references :parent_budget, null: false, foreign_key: { to_table: :budgets }+ t.references :child_budget, null: false, foreign_key: { to_table: :budgets }+ t.references :cost_type, null: true, foreign_key: { to_table: :cost_types }++ t.string :relation_type, null: false, default: "add"++ t.timestamps+ end+ end+enddiff --git a/modules/budgets/db/migrate/20250714123836_add_budget_state.rb b/modules/budgets/db/migrate/20250714123836_add_budget_state.rbnew file mode 100644index 00000000000..552eeab8952--- /dev/null+++ b/modules/budgets/db/migrate/20250714123836_add_budget_state.rb@@ -0,0 +1,36 @@+# 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 AddBudgetState < ActiveRecord::Migration[8.0]+ def change+ add_column :budgets, :state, :string, null: true+ add_column :budget_journals, :state, :string, null: true+ end+enddiff --git a/modules/budgets/db/migrate/20250714123904_add_budget_state_index.rb b/modules/budgets/db/migrate/20250714123904_add_budget_state_index.rbnew file mode 100644index 00000000000..5ddb0942421--- /dev/null+++ b/modules/budgets/db/migrate/20250714123904_add_budget_state_index.rb@@ -0,0 +1,37 @@+# 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 AddBudgetStateIndex < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ def change+ add_index :budgets, :state, algorithm: :concurrently+ end+enddiff --git a/modules/budgets/db/migrate/20250714142919_add_supplementary_amount_to_budget.rb b/modules/budgets/db/migrate/20250714142919_add_supplementary_amount_to_budget.rbnew file mode 100644index 00000000000..1ed08c657eb--- /dev/null+++ b/modules/budgets/db/migrate/20250714142919_add_supplementary_amount_to_budget.rb@@ -0,0 +1,36 @@+# 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 AddSupplementaryAmountToBudget < ActiveRecord::Migration[8.0]+ def change+ add_column :budgets, :supplementary_amount, :decimal, precision: 20, scale: 2, null: false, default: "0.0"+ add_column :budget_journals, :supplementary_amount, :decimal, precision: 20, scale: 2, null: false, default: "0.0"+ end+enddiff --git a/modules/budgets/db/migrate/20250716121743_unique_budget_relation_child.rb b/modules/budgets/db/migrate/20250716121743_unique_budget_relation_child.rbnew file mode 100644index 00000000000..10561f4e224--- /dev/null+++ b/modules/budgets/db/migrate/20250716121743_unique_budget_relation_child.rb@@ -0,0 +1,10 @@+# frozen_string_literal: true++class UniqueBudgetRelationChild < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ def change+ remove_index :budget_relations, :child_budget_id, if_exists: true, algorithm: :concurrently+ add_index :budget_relations, :child_budget_id, unique: true, algorithm: :concurrently+ end+enddiff --git a/modules/budgets/db/migrate/20250716122318_remove_cost_type_from_budget_relation.rb b/modules/budgets/db/migrate/20250716122318_remove_cost_type_from_budget_relation.rbnew file mode 100644index 00000000000..68be7bfee56--- /dev/null+++ b/modules/budgets/db/migrate/20250716122318_remove_cost_type_from_budget_relation.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class RemoveCostTypeFromBudgetRelation < ActiveRecord::Migration[8.0]+ def change+ remove_reference(:budget_relations, :cost_type, foreign_key: true)+ end+enddiff --git a/modules/budgets/lib/budgets/engine.rb b/modules/budgets/lib/budgets/engine.rbindex ce9d56a9149..df6516acbc6 100644--- a/modules/budgets/lib/budgets/engine.rb+++ b/modules/budgets/lib/budgets/engine.rb@@ -9,10 +9,16 @@ module Budgetsbundled: true doproject_module :budgets dopermission :view_budgets,- { budgets: %i[index show] },+ {+ budgets: %i[index show],+ budget_relations: %i[index]+ },permissible_on: :projectpermission :edit_budgets,- { budgets: %i[index show edit update destroy destroy_info new create copy] },+ {+ budgets: %i[index show edit update destroy destroy_info new create copy],+ budget_relations: %i[index edit update new create destroy]+ },permissible_on: :projectend@@ -70,6 +76,7 @@ module Budgetsselect Queries::Projects::Selects::BudgetSpentselect Queries::Projects::Selects::BudgetSpentRatioselect Queries::Projects::Selects::BudgetAvailable+ select Queries::Projects::Selects::BudgetAllocatedendendenddiff --git a/modules/budgets/lib/budgets/patches/projects/row_component_patch.rb b/modules/budgets/lib/budgets/patches/projects/row_component_patch.rbindex 9ea970302f4..a6a24060032 100644--- a/modules/budgets/lib/budgets/patches/projects/row_component_patch.rb+++ b/modules/budgets/lib/budgets/patches/projects/row_component_patch.rb@@ -55,7 +55,12 @@ module Budgets::Patches::Projects::RowComponentPatchenddef total_ratio- @total_ratio ||= total_planned.zero? ? 0 : ((total_spent / total_planned) * 100).round+ gone = total_spent + total_allocated+ @total_ratio ||= total_planned.zero? ? 0 : ((gone / total_planned) * 100).round+ end++ def total_allocated+ @total_allocated ||= budgets.sum(&:allocated_to_children)enddef budgets@@ -83,6 +88,12 @@ module Budgets::Patches::Projects::RowComponentPatchendend+ def budget_allocated+ with_project_budgets do |project_budgets|+ number_to_currency(project_budgets.total_allocated, precision: 0)+ end+ end+def budget_availablewith_project_budgets do |project_budgets|number_to_currency(project_budgets.total_available, precision: 0)diff --git a/modules/budgets/spec/factories/budget_factory.rb b/modules/budgets/spec/factories/budget_factory.rbindex 7bec92102f7..347a9e0b8df 100644--- a/modules/budgets/spec/factories/budget_factory.rb+++ b/modules/budgets/spec/factories/budget_factory.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -31,9 +33,15 @@ FactoryBot.define dosequence(:subject) { |n| "Budget No. #{n}" }sequence(:description) { |n| "I am Budget No. #{n}" }project- association :author, factory: :user- fixed_date { Date.today }+ author factory: :user+ fixed_date { Date.current }created_at { 3.days.ago }updated_at { 3.days.ago }++ traits_for_enum(:state)++ trait :with_supplementary_amount do+ supplementary_amount { BigDecimal(250000000) }+ endendenddiff --git a/modules/budgets/spec/factories/budget_relation_factory.rb b/modules/budgets/spec/factories/budget_relation_factory.rbnew file mode 100644index 00000000000..3671d0261db--- /dev/null+++ b/modules/budgets/spec/factories/budget_relation_factory.rb@@ -0,0 +1,38 @@+# 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.+#++++FactoryBot.define do+ factory :budget_relation do+ parent_budget factory: :budget+ child_budget factory: :budget++ traits_for_enum(:relation_type)+ end+enddiff --git a/modules/budgets/spec/models/budget_relation_spec.rb b/modules/budgets/spec/models/budget_relation_spec.rbnew file mode 100644index 00000000000..175ebfc9bbd--- /dev/null+++ b/modules/budgets/spec/models/budget_relation_spec.rb@@ -0,0 +1,407 @@+# 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.+#++++require_relative "../spec_helper"++RSpec.describe BudgetRelation do+ shared_let(:admin) { create(:admin) }+ before do+ allow(User).to receive(:current).and_return(admin)+ end++ describe "calculation logic" do+ describe "bottom->up" do+ let(:portfolio) { create(:project, project_type: :portfolio, name: "Portfolio") }+ let(:portfolio_budget) do+ create(:budget, project: portfolio, supplementary_amount: 0, subject: "Portfolio Budget")+ end++ let(:program1) { create(:project, project_type: :program, parent: portfolio, name: "Program 1") }+ let(:program1_budget) { create(:budget, project: program1, supplementary_amount: 0, subject: "Program 1 Budget") }++ let(:project1) { create(:project, project_type: :project, parent: program1, name: "Project 1") }+ let(:project1_budget) { create(:budget, project: project1, supplementary_amount: 5_000, subject: "Project 1 Budget") }++ let(:project2) { create(:project, project_type: :project, parent: program1, name: "Project 2") }+ let(:project2_budget) { create(:budget, project: project2, supplementary_amount: 2_500, subject: "Project 2 Budget") }++ context "without any relations" do+ it "calculates the correct values" do+ expect(portfolio_budget).to have_attributes(budget: 0,+ allocated_to_children: 0,+ spent: 0,+ available: 0)++ expect(program1_budget).to have_attributes(budget: 0,+ allocated_to_children: 0,+ spent: 0,+ available: 0)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 0,+ available: 5_000)++ expect(project2_budget).to have_attributes(budget: 2_500,+ allocated_to_children: 0,+ spent: 0,+ available: 2_500)+ end+ end++ context "when setting up project1 to add itself to the program1 budget" do+ before do+ described_class.create!(parent_budget: program1_budget,+ child_budget: project1_budget,+ relation_type: :add)+ end++ it "allocates the project's budget to the program's budget" do+ expect(portfolio_budget).to have_attributes(budget: 0,+ allocated_to_children: 0,+ spent: 0,+ available: 0)++ expect(program1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 5_000,+ spent: 0,+ available: 0)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 0,+ available: 5_000)++ expect(project2_budget).to have_attributes(budget: 2_500,+ allocated_to_children: 0,+ spent: 0,+ available: 2_500)+ end++ context "when also setting up project2 to add itself to the program1 budget" do+ before do+ described_class.create!(parent_budget: program1_budget,+ child_budget: project2_budget,+ relation_type: :add)+ end++ it "allocates both project's budget to the program's budget" do+ expect(portfolio_budget).to have_attributes(budget: 0,+ allocated_to_children: 0,+ spent: 0,+ available: 0)++ expect(program1_budget).to have_attributes(budget: 7_500,+ allocated_to_children: 7_500,+ spent: 0,+ available: 0)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 0,+ available: 5_000)++ expect(project2_budget).to have_attributes(budget: 2_500,+ allocated_to_children: 0,+ spent: 0,+ available: 2_500)+ end++ context "when setting program1 to add itself to the portfolio budget" do+ before do+ described_class.create!(parent_budget: portfolio_budget,+ child_budget: program1_budget,+ relation_type: :add)+ end++ it "allocates the program's budget to the portfolio's budget" do+ expect(portfolio_budget).to have_attributes(budget: 7_500,+ allocated_to_children: 7_500,+ spent: 0,+ available: 0)++ expect(program1_budget).to have_attributes(budget: 7_500,+ allocated_to_children: 7_500,+ spent: 0,+ available: 0)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 0,+ available: 5_000)++ expect(project2_budget).to have_attributes(budget: 2_500,+ allocated_to_children: 0,+ spent: 0,+ available: 2_500)+ end++ context "when adding some costs to the projects" do+ let(:work_package1) { create(:work_package, project: project1, budget: project1_budget) }+ let!(:cost_entry1) do+ create(:cost_entry, project: project1, entity: work_package1, overridden_costs: 500)+ end++ let(:work_package2) { create(:work_package, project: project2, budget: project2_budget) }+ let!(:cost_entry2) do+ create(:cost_entry, project: project2, entity: work_package2, overridden_costs: 750)+ end++ it "subtracts the costs from the budgets" do+ expect(portfolio_budget).to have_attributes(spent: 0,+ spent_with_children: 1_250)++ expect(program1_budget).to have_attributes(spent: 0,+ spent_with_children: 1_250)++ expect(project1_budget).to have_attributes(budget: 5_000,+ spent: 500,+ available: 4_500)++ expect(project2_budget).to have_attributes(budget: 2_500,+ spent: 750,+ available: 1_750)+ end+ end+ end+ end+ end+ end++ describe "top->down" do+ let(:portfolio) { create(:project, project_type: :portfolio, name: "Portfolio") }+ let(:portfolio_budget) { create(:budget, project: portfolio, supplementary_amount: 10_000, subject: "Portfolio Budget") }++ let(:program1) { create(:project, project_type: :program, parent: portfolio, name: "Program 1") }+ let(:program1_budget) { create(:budget, project: program1, supplementary_amount: 7_000, subject: "Program 1 Budget") }++ let(:project1) { create(:project, project_type: :project, parent: program1, name: "Project 1") }+ let(:project1_budget) { create(:budget, project: project1, supplementary_amount: 5_000, subject: "Project 1 Budget") }++ let(:program2) { create(:project, project_type: :program, parent: portfolio, name: "Program 2") }+ let(:program2_budget) { create(:budget, project: program2, supplementary_amount: 3_000, subject: "Program 2 Budget") }++ let(:project2) { create(:project, project_type: :project, parent: program2, name: "Project 2") }+ let(:project2_budget) { create(:budget, project: project2, supplementary_amount: 2_000, subject: "Project 2 Budget") }++ context "without any relations" do+ it "calculates the correct values" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 0,+ spent: 0,+ available: 10_000)+ end+ end++ context "with the portfolio -> program1 relation" do+ before do+ described_class.create!(parent_budget: portfolio_budget,+ child_budget: program1_budget,+ relation_type: :subtract)+ end++ it "allocates the program's budget out of the portfolio budget" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 7_000,+ spent: 0,+ available: 3_000)++ expect(program1_budget).to have_attributes(budget: 7_000,+ allocated_to_children: 0,+ spent: 0,+ available: 7_000)+ end++ context "with the program1 -> project1 relation" do+ before do+ described_class.create!(parent_budget: program1_budget,+ child_budget: project1_budget,+ relation_type: :subtract)+ end++ it "allocates the project's budget out of the program's budget" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 7_000,+ spent: 0,+ available: 3_000)++ expect(program1_budget).to have_attributes(budget: 7_000,+ allocated_to_children: 5_000,+ spent: 0,+ available: 2_000)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 0,+ available: 5_000)+ end+ end++ context "with the portfolio -> program1, program2 relation" do+ before do+ described_class.create!(parent_budget: portfolio_budget,+ child_budget: program2_budget,+ relation_type: :subtract)+ end++ it "allocates the program's budget out of the portfolio's budget" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 10_000,+ spent: 0,+ available: 0)++ expect(program1_budget).to have_attributes(budget: 7_000,+ allocated_to_children: 0,+ spent: 0,+ available: 7_000)++ expect(program2_budget).to have_attributes(budget: 3_000,+ allocated_to_children: 0,+ spent: 0,+ available: 3_000)+ end++ context "with the program2 -> project2 relation" do+ before do+ described_class.create!(parent_budget: program2_budget,+ child_budget: project2_budget,+ relation_type: :subtract)+ end++ it "allocates the project's budget out of the program's budget" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 10_000,+ spent: 0,+ available: 0)++ expect(program2_budget).to have_attributes(budget: 3_000,+ allocated_to_children: 2_000,+ spent: 0,+ available: 1_000)++ expect(project2_budget).to have_attributes(budget: 2_000,+ allocated_to_children: 0,+ spent: 0,+ available: 2_000)+ end++ context "with logged costs on project1" do+ let(:work_package) { create(:work_package, project: project2, budget: project2_budget) }+ let!(:cost_entry) do+ create(:cost_entry, project: project2, entity: work_package, overridden_costs: 500)+ end++ it "subtracts the cost from project2's budget" do+ expect(portfolio_budget.spent).to eq(0)+ expect(portfolio_budget.spent_with_children).to eq(500)++ expect(program2_budget.spent).to eq(0)+ expect(program2_budget.spent_with_children).to eq(500)++ expect(project2_budget).to have_attributes(budget: 2_000,+ allocated_to_children: 0,+ spent: 500,+ available: 1_500)+ end+ end+ end+ end+ end+ end++ describe "bottom->up and top->down mixed" do+ # - The portfolio has a budget of 10.000€.+ # - The program does not have an assigned budget but is the sum of the project's budgets.+ # - The program budget is taken out of the portfolio budget.+ # - The projects have budgets of 5.000€ and 2.500€.+ # - The projects have half of their budgets already spent.++ let!(:portfolio) { create(:project, project_type: :portfolio, name: "Portfolio") }+ let!(:portfolio_budget) { create(:budget, project: portfolio, supplementary_amount: 10_000, subject: "Portfolio Budget") }++ let!(:program1) { create(:project, project_type: :program, parent: portfolio, name: "Program 1") }+ let!(:program1_budget) { create(:budget, project: program1, supplementary_amount: 0, subject: "Program 1 Budget") }++ let!(:portfolio_program_relation) do+ create(:budget_relation, parent_budget: portfolio_budget, child_budget: program1_budget, relation_type: :subtract)+ end++ let!(:project1) { create(:project, project_type: :project, parent: program1, name: "Project 1") }+ let!(:project1_budget) { create(:budget, project: project1, supplementary_amount: 5_000, subject: "Project 1 Budget") }++ let!(:project1_program_relation) do+ create(:budget_relation, parent_budget: program1_budget, child_budget: project1_budget, relation_type: :add)+ end++ let!(:work_package1) { create(:work_package, project: project1, budget: project1_budget) }+ let!(:cost_entry1) do+ create(:cost_entry, project: project1, entity: work_package1, overridden_costs: 2_500)+ end++ let!(:project2) { create(:project, project_type: :project, parent: program1, name: "Project 2") }+ let!(:project2_budget) { create(:budget, project: project2, supplementary_amount: 2_500, subject: "Project 2 Budget") }++ let!(:project2_program_relation) do+ create(:budget_relation, parent_budget: program1_budget, child_budget: project2_budget, relation_type: :add)+ end++ let!(:work_package2) { create(:work_package, project: project2, budget: project2_budget) }+ let!(:cost_entry2) do+ create(:cost_entry, project: project2, entity: work_package2, overridden_costs: 1_250)+ end++ it "calculates the correct values" do+ expect(portfolio_budget).to have_attributes(budget: 10_000,+ allocated_to_children: 7_500,+ spent: 0,+ spent_with_children: 3_750,+ available: 2_500)++ expect(program1_budget).to have_attributes(budget: 7_500,+ allocated_to_children: 7_500,+ spent: 0,+ spent_with_children: 3_750,+ available: 0)++ expect(project1_budget).to have_attributes(budget: 5_000,+ allocated_to_children: 0,+ spent: 2_500,+ spent_with_children: 2_500,+ available: 2_500)++ expect(project2_budget).to have_attributes(budget: 2_500,+ allocated_to_children: 0,+ spent: 1_250,+ spent_with_children: 1_250,+ available: 1_250)+ end+ end+ end+enddiff --git a/modules/budgets/spec/models/budget_spec.rb b/modules/budgets/spec/models/budget_spec.rbindex 7857635df56..918e59e0fc1 100644--- a/modules/budgets/spec/models/budget_spec.rb+++ b/modules/budgets/spec/models/budget_spec.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbHdiff --git a/modules/budgets/spec/models/permitted_params_spec.rb b/modules/budgets/spec/models/permitted_params_spec.rbindex bd1a589bb12..ef0b06dc607 100644--- a/modules/budgets/spec/models/permitted_params_spec.rb+++ b/modules/budgets/spec/models/permitted_params_spec.rb@@ -78,6 +78,18 @@ RSpec.describe PermittedParams doit_behaves_like "allows params"end+ context "supplementary_amount" do+ let(:hash) { { "supplementary_amount" => "250000.00" } }++ it_behaves_like "allows params"+ end++ context "state" do+ let(:hash) { { "state" => "approved" } }++ it_behaves_like "allows params"+ end+context "project_id" dolet(:hash) { { "project_id" => 42 } }diff --git a/modules/costs/lib/costs/patches/permitted_params_patch.rb b/modules/costs/lib/costs/patches/permitted_params_patch.rbindex 1783584042d..b71f6fc63ec 100644--- a/modules/costs/lib/costs/patches/permitted_params_patch.rb+++ b/modules/costs/lib/costs/patches/permitted_params_patch.rb@@ -44,7 +44,7 @@ module Costs::Patches::PermittedParamsPatchdef budgetparams.require(:budget).permit(:subject,:description,- :fixed_date,+ :fixed_date, :supplementary_amount, :state,{ new_material_budget_item_attributes: %i[units cost_type_id comments amount] },{ new_labor_budget_item_attributes: %i[hours user_id comments amount] },{ existing_material_budget_item_attributes: %i[units cost_type_id comments amount] },diff --git a/spec/models/queries/projects/project_query_spec.rb b/spec/models/queries/projects/project_query_spec.rbindex 5bc9ae43991..8953303b06c 100644--- a/spec/models/queries/projects/project_query_spec.rb+++ b/spec/models/queries/projects/project_query_spec.rb@@ -149,6 +149,7 @@ RSpec.describe ProjectQuery dobudget_plannedbudget_spentbudget_spent_ratio+ budget_allocated])endend@@ -176,6 +177,7 @@ RSpec.describe ProjectQuery dobudget_availablebudget_plannedbudget_spent+ budget_allocatedbudget_spent_ratio])end