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
[65783] Add budgets info to projects list columns
Christophe Bliard96fed646ed2e
diff --git a/modules/budgets/app/models/budget.rb b/modules/budgets/app/models/budget.rbindex 2479b0614a4..4bde0cdc4e6 100644--- a/modules/budgets/app/models/budget.rb+++ b/modules/budgets/app/models/budget.rb@@ -164,6 +164,10 @@ class Budget < ApplicationRecordendend+ def available+ budget - spent+ end+def new_material_budget_item_attributes=(material_budget_item_attributes)material_budget_item_attributes.each do |_index, attributes|correct_material_attributes!(attributes)diff --git a/modules/budgets/app/models/queries/projects/selects/budget_available.rb b/modules/budgets/app/models/queries/projects/selects/budget_available.rbnew file mode 100644index 00000000000..109bb3af308--- /dev/null+++ b/modules/budgets/app/models/queries/projects/selects/budget_available.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::BudgetAvailable < Queries::Selects::Base+ def self.key+ :budget_available+ end++ def caption+ I18n.t(:label_budget_available)+ end+enddiff --git a/modules/budgets/app/models/queries/projects/selects/budget_planned.rb b/modules/budgets/app/models/queries/projects/selects/budget_planned.rbnew file mode 100644index 00000000000..6b2245580c4--- /dev/null+++ b/modules/budgets/app/models/queries/projects/selects/budget_planned.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::BudgetPlanned < Queries::Selects::Base+ def self.key+ :budget_planned+ end++ def caption+ I18n.t(:label_budget_planned)+ end+enddiff --git a/modules/budgets/app/models/queries/projects/selects/budget_spent.rb b/modules/budgets/app/models/queries/projects/selects/budget_spent.rbnew file mode 100644index 00000000000..5833837e4a4--- /dev/null+++ b/modules/budgets/app/models/queries/projects/selects/budget_spent.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::BudgetSpent < Queries::Selects::Base+ def self.key+ :budget_spent+ end++ def caption+ I18n.t(:label_budget_spent)+ end+enddiff --git a/modules/budgets/app/models/queries/projects/selects/budget_spent_ratio.rb b/modules/budgets/app/models/queries/projects/selects/budget_spent_ratio.rbnew file mode 100644index 00000000000..ce9f7be93f7--- /dev/null+++ b/modules/budgets/app/models/queries/projects/selects/budget_spent_ratio.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::BudgetSpentRatio < Queries::Selects::Base+ def self.key+ :budget_spent_ratio+ end++ def caption+ I18n.t(:label_budget_spent_ratio)+ end+enddiff --git a/modules/budgets/app/views/budgets/_list.html.erb b/modules/budgets/app/views/budgets/_list.html.erbindex 6b68a2e720d..cb3b8db4a50 100644--- a/modules/budgets/app/views/budgets/_list.html.erb+++ b/modules/budgets/app/views/budgets/_list.html.erb@@ -93,7 +93,7 @@ See COPYRIGHT and LICENSE files for more details.<%= content_tag(:td, link_to(h(budget.subject), budget_path(budget.id)), class: "subject") %><%= 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.budget - budget.spent, precision: 0), class: "currency") %>+ <%= content_tag(:td, number_to_currency(budget.available, precision: 0), class: "currency") %><%= content_tag(:td, extended_progress_bar(budget.budget_ratio, legend: budget.budget_ratio.to_s)) %><%-total_budget += budget.budgetdiff --git a/modules/budgets/config/locales/en.yml b/modules/budgets/config/locales/en.ymlindex feac827fbca..e4c62c7369e 100644--- a/modules/budgets/config/locales/en.yml+++ b/modules/budgets/config/locales/en.yml@@ -77,9 +77,13 @@ en:help_override_rate: "Enter a value here to override the default rate."label_budget: "Budget"+ label_budget_available: "Budget available"+ label_budget_id: "Budget #%{id}"label_budget_new: "New budget"+ label_budget_planned: "Budget planned"label_budget_plural: "Budgets"- label_budget_id: "Budget #%{id}"+ label_budget_spent: "Budget spent"+ label_budget_spent_ratio: "Budget spent ratio"label_deliverable: "Budget"label_example_placeholder: 'e.g., %{decimal}'label_view_all_budgets: "View all budgets"diff --git a/modules/budgets/lib/budgets/engine.rb b/modules/budgets/lib/budgets/engine.rbindex 00572f3ee9c..ce9d56a9149 100644--- a/modules/budgets/lib/budgets/engine.rb+++ b/modules/budgets/lib/budgets/engine.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+module Budgetsclass Engine < ::Rails::Engineinclude OpenProject::Plugins::ActsAsOpEngine@@ -23,6 +25,8 @@ module Budgetsicon: "op-budget"end+ patch_with_namespace :Projects, :RowComponent+add_api_path :budget do |id|"#{root}/budgets/#{id}"end@@ -60,6 +64,13 @@ module Budgets::Queries::Register.register(::Query) dofilter Queries::WorkPackages::Filter::BudgetFilterend++ ::Queries::Register.register(::ProjectQuery) do+ select Queries::Projects::Selects::BudgetPlanned+ select Queries::Projects::Selects::BudgetSpent+ select Queries::Projects::Selects::BudgetSpentRatio+ select Queries::Projects::Selects::BudgetAvailable+ endendendenddiff --git a/modules/budgets/lib/budgets/patches/projects/row_component_patch.rb b/modules/budgets/lib/budgets/patches/projects/row_component_patch.rbnew file mode 100644index 00000000000..9ea970302f4--- /dev/null+++ b/modules/budgets/lib/budgets/patches/projects/row_component_patch.rb@@ -0,0 +1,99 @@+# 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.+#++++module Budgets::Patches::Projects::RowComponentPatch+ def self.included(base) # :nodoc:+ base.prepend InstanceMethods+ end++ class ProjectBudgets+ attr_reader :project++ def initialize(project)+ @project = project+ end++ delegate :any?, to: :budgets++ def total_planned+ @total_planned ||= budgets.sum(&:budget)+ end++ def total_spent+ @total_spent ||= budgets.sum(&:spent)+ end++ def total_available+ @total_available ||= budgets.sum(&:available)+ end++ def total_ratio+ @total_ratio ||= total_planned.zero? ? 0 : ((total_spent / total_planned) * 100).round+ end++ def budgets+ @budgets ||= project.budgets.to_a+ end+ end++ module InstanceMethods+ def budget_planned+ with_project_budgets do |project_budgets|+ number_to_currency(project_budgets.total_planned, precision: 0)+ end+ end++ def budget_spent+ with_project_budgets do |project_budgets|+ number_to_currency(project_budgets.total_spent, precision: 0)+ end+ end++ def budget_spent_ratio+ with_project_budgets do |project_budgets|+ helpers.extended_progress_bar(project_budgets.total_ratio,+ legend: project_budgets.total_ratio.to_s)+ end+ end++ def budget_available+ with_project_budgets do |project_budgets|+ number_to_currency(project_budgets.total_available, precision: 0)+ end+ end++ def with_project_budgets+ @project_budgets ||= ProjectBudgets.new(project)+ return unless @project_budgets.any?++ yield @project_budgets+ end+ end+enddiff --git a/spec/models/queries/projects/project_query_spec.rb b/spec/models/queries/projects/project_query_spec.rbindex dedd7ede0af..5bc9ae43991 100644--- a/spec/models/queries/projects/project_query_spec.rb+++ b/spec/models/queries/projects/project_query_spec.rb@@ -145,6 +145,10 @@ RSpec.describe ProjectQuery dostatus_explanationcf_23cf_42+ budget_available+ budget_planned+ budget_spent+ budget_spent_ratio])endend@@ -169,6 +173,10 @@ RSpec.describe ProjectQuery dorequired_disk_spacecf_23cf_42+ budget_available+ budget_planned+ budget_spent+ budget_spent_ratio])endend