budget/app/models/expense.rb

26 lines
488 B
Ruby
Raw Permalink Normal View History

2022-11-20 15:43:33 -05:00
class Expense < ApplicationRecord
PERIOD_OCCURENCES = {
monthly: 12,
annually: 1,
weekly: 52,
quarterly: 4
2022-11-20 15:43:33 -05:00
}.freeze
enum :period, PERIOD_OCCURENCES.keys
def monthly
payment * PERIOD_OCCURENCES[period.to_sym] / 12
end
2022-11-20 16:21:28 -05:00
def self.total
Expense.sum(&:payment)
end
2023-02-24 19:08:12 -05:00
def self.monthly_total
Expense.all.map(&:monthly).sum.round(2)
end
2023-03-03 18:12:59 -05:00
def self.credit_card_monthly_total
Expense.where(credit_card: true).map(&:monthly).sum.round(2)
2023-02-24 19:08:12 -05:00
end
2022-11-20 15:43:33 -05:00
end