2024-02-23 21:34:33 -05:00
|
|
|
class Transaction < ApplicationRecord
|
2024-03-18 11:21:00 -04:00
|
|
|
include Monetizable
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
belongs_to :account
|
2024-03-07 19:15:50 +01:00
|
|
|
belongs_to :category, optional: true
|
2024-02-29 08:32:52 -05:00
|
|
|
|
2024-03-15 12:21:59 -07:00
|
|
|
validates :name, :date, :amount, :account, presence: true
|
|
|
|
|
2024-03-18 11:21:00 -04:00
|
|
|
monetize :amount
|
|
|
|
|
|
|
|
scope :inflows, -> { where("amount > 0") }
|
|
|
|
scope :outflows, -> { where("amount < 0") }
|
|
|
|
scope :active, -> { where(excluded: false) }
|
|
|
|
|
2024-03-11 14:51:16 +02:00
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
|
|
%w[name amount date]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
|
|
%w[category account]
|
|
|
|
end
|
|
|
|
|
2024-03-28 13:23:54 -04:00
|
|
|
def self.build_filter_list(params, family)
|
|
|
|
filters = []
|
|
|
|
|
|
|
|
if params
|
|
|
|
params.each do |key, value|
|
|
|
|
next if value.blank?
|
|
|
|
|
|
|
|
case key
|
|
|
|
when "account_id_in"
|
|
|
|
value.each do |account_id|
|
|
|
|
filters << { type: "account", value: family.accounts.find(account_id), original: { key: key, value: account_id } }
|
|
|
|
end
|
|
|
|
when "category_name_or_account_name_or_name_cont"
|
|
|
|
filters << { type: "search", value: value, original: { key: key, value: nil } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
filters
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|