2025-04-14 11:40:34 -04:00
|
|
|
class Transaction < ApplicationRecord
|
2025-04-18 11:39:58 -04:00
|
|
|
include Entryable, Transferable, Ruleable
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-03-07 19:15:50 +01:00
|
|
|
belongs_to :category, optional: true
|
2024-04-29 21:17:28 +02:00
|
|
|
belongs_to :merchant, optional: true
|
2025-02-21 11:57:59 -05:00
|
|
|
|
2024-05-23 08:09:33 -04:00
|
|
|
has_many :taggings, as: :taggable, dependent: :destroy
|
|
|
|
has_many :tags, through: :taggings
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
accepts_nested_attributes_for :taggings, allow_destroy: true
|
2024-03-15 12:21:59 -07:00
|
|
|
|
2025-06-20 13:31:58 -04:00
|
|
|
enum :kind, {
|
|
|
|
standard: "standard", # A regular transaction, included in budget analytics
|
|
|
|
funds_movement: "funds_movement", # Movement of funds between accounts, excluded from budget analytics
|
|
|
|
cc_payment: "cc_payment", # A CC payment, excluded from budget analytics (CC payments offset the sum of expense transactions)
|
|
|
|
loan_payment: "loan_payment", # A payment to a Loan account, treated as an expense in budgets
|
|
|
|
one_time: "one_time" # A one-time expense/income, excluded from budget analytics
|
|
|
|
}
|
|
|
|
|
|
|
|
# Overarching grouping method for all transfer-type transactions
|
|
|
|
def transfer?
|
|
|
|
funds_movement? || cc_payment? || loan_payment?
|
2024-05-30 20:55:18 -04:00
|
|
|
end
|
2025-04-18 11:39:58 -04:00
|
|
|
|
|
|
|
def set_category!(category)
|
|
|
|
if category.is_a?(String)
|
|
|
|
category = entry.account.family.categories.find_or_create_by!(
|
|
|
|
name: category
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
update!(category: category)
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|