mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
fix: subcategories are not properly handled for budget allocations (#1844)
* fix: `allocated_spending` logic * fix: subcategories exceeding parent limit * refactor: budget allocations and max allocation logic * feat: add stream for budget category form validation * feat: update uncategorized value via stream, refactor confirm button with stream * fix: ensure live updates for parent & sibling budgets in Turbo Stream * fix: lint issues
This commit is contained in:
parent
077694bbde
commit
fb6c6fa6bb
7 changed files with 60 additions and 13 deletions
|
@ -133,10 +133,10 @@ class Budget < ApplicationRecord
|
|||
end
|
||||
|
||||
# =============================================================================
|
||||
# Budget allocations: How much user has budgeted for all categories combined
|
||||
# Budget allocations: How much user has budgeted for all parent categories combined
|
||||
# =============================================================================
|
||||
def allocated_spending
|
||||
budget_categories.sum(:budgeted_spending)
|
||||
budget_categories.reject { |bc| bc.subcategory? }.sum(&:budgeted_spending)
|
||||
end
|
||||
|
||||
def allocated_percent
|
||||
|
|
|
@ -79,4 +79,29 @@ class BudgetCategory < ApplicationRecord
|
|||
|
||||
segments
|
||||
end
|
||||
|
||||
def siblings
|
||||
budget.budget_categories.select { |bc| bc.category.parent_id == category.parent_id && bc.id != id }
|
||||
end
|
||||
|
||||
def max_allocation
|
||||
return nil unless subcategory?
|
||||
|
||||
parent_budget = budget.budget_categories.find { |bc| bc.category.id == category.parent_id }&.budgeted_spending
|
||||
siblings_budget = siblings.sum(&:budgeted_spending)
|
||||
|
||||
[ parent_budget - siblings_budget, 0 ].max
|
||||
end
|
||||
|
||||
def subcategories
|
||||
return BudgetCategory.none unless category.parent_id.nil?
|
||||
|
||||
budget.budget_categories
|
||||
.joins(:category)
|
||||
.where(categories: { parent_id: category.id })
|
||||
end
|
||||
|
||||
def subcategory?
|
||||
category.parent_id.present?
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue