mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
* Budgeting V1 * Basic UI template * Fully scaffolded budgeting v1 * Basic working budget * Finalize donut chart for budgets * Allow categorization of loan payments for budget * Include loan payments in incomes_and_expenses scope * Add budget allocations progress * Empty states * Clean up budget methods * Category aggregation queries * Handle overage scenarios in form * Finalize budget donut chart controller * Passing tests * Fix allocation naming * Add income category migration * Native support for uncategorized budget category * Formatting * Fix subcategory sort order, padding * Fix calculation for category rollups in budget
82 lines
2.2 KiB
Ruby
82 lines
2.2 KiB
Ruby
class BudgetCategory < ApplicationRecord
|
|
include Monetizable
|
|
|
|
belongs_to :budget
|
|
belongs_to :category
|
|
|
|
validates :budget_id, uniqueness: { scope: :category_id }
|
|
|
|
monetize :budgeted_spending, :actual_spending, :available_to_spend
|
|
|
|
class Group
|
|
attr_reader :budget_category, :budget_subcategories
|
|
|
|
delegate :category, to: :budget_category
|
|
delegate :name, :color, to: :category
|
|
|
|
def self.for(budget_categories)
|
|
top_level_categories = budget_categories.select { |budget_category| budget_category.category.parent_id.nil? }
|
|
top_level_categories.map do |top_level_category|
|
|
subcategories = budget_categories.select { |bc| bc.category.parent_id == top_level_category.category_id && top_level_category.category_id.present? }
|
|
new(top_level_category, subcategories.sort_by { |subcategory| subcategory.category.name })
|
|
end.sort_by { |group| group.category.name }
|
|
end
|
|
|
|
def initialize(budget_category, budget_subcategories = [])
|
|
@budget_category = budget_category
|
|
@budget_subcategories = budget_subcategories
|
|
end
|
|
end
|
|
|
|
class << self
|
|
def uncategorized
|
|
new(
|
|
id: Digest::UUID.uuid_v5(Digest::UUID::URL_NAMESPACE, "uncategorized"),
|
|
category: nil,
|
|
)
|
|
end
|
|
end
|
|
|
|
def initialized?
|
|
budget.initialized?
|
|
end
|
|
|
|
def category
|
|
super || budget.family.categories.uncategorized
|
|
end
|
|
|
|
def subcategory?
|
|
category.parent_id.present?
|
|
end
|
|
|
|
def actual_spending
|
|
category.month_total(date: budget.start_date)
|
|
end
|
|
|
|
def available_to_spend
|
|
(budgeted_spending || 0) - actual_spending
|
|
end
|
|
|
|
def percent_of_budget_spent
|
|
return 0 unless budgeted_spending > 0
|
|
|
|
(actual_spending / budgeted_spending) * 100
|
|
end
|
|
|
|
def to_donut_segments_json
|
|
unused_segment_id = "unused"
|
|
overage_segment_id = "overage"
|
|
|
|
return [ { color: "#F0F0F0", amount: 1, id: unused_segment_id } ] unless actual_spending > 0
|
|
|
|
segments = [ { color: category.color, amount: actual_spending, id: id } ]
|
|
|
|
if available_to_spend.negative?
|
|
segments.push({ color: "#EF4444", amount: available_to_spend.abs, id: overage_segment_id })
|
|
else
|
|
segments.push({ color: "#F0F0F0", amount: available_to_spend, id: unused_segment_id })
|
|
end
|
|
|
|
segments
|
|
end
|
|
end
|