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

Add a method on the budget to calculate allocated_unused

Klaus Zandersf4bc0a6652cb
diff --git a/modules/budgets/app/models/budget.rb b/modules/budgets/app/models/budget.rb
index 099cf93d5d2..7a8b2d2bc70 100644
--- a/modules/budgets/app/models/budget.rb
+++ b/modules/budgets/app/models/budget.rb
@@ -132,9 +132,17 @@ class Budget < ApplicationRecord
@allocated_to_children ||= child_budget_relations.includes(:child_budget).sum { |rel| rel.child_budget.budget }
end
+ def allocated_unused
+ allocated_to_children - spent_on_children
+ end
+
def spent_with_children
+ spent + spent_on_children
+ end
+
+ def spent_on_children
# TODO: Efficient with query
- @spent_with_children ||= spent + child_budget_relations.includes(:child_budget).sum do |rel|
+ @spent_on_children ||= child_budget_relations.includes(:child_budget).sum do |rel|
rel.child_budget.spent_with_children
end
end