2024-06-24 11:58:39 -04:00
|
|
|
class Account::Transaction < ApplicationRecord
|
2024-07-01 10:49:43 -04:00
|
|
|
include Account::Entryable
|
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
|
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
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
scope :active, -> { where(excluded: false) }
|
2024-03-11 14:51:16 +02:00
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
class << self
|
2024-07-01 10:49:43 -04:00
|
|
|
def search(params)
|
|
|
|
query = all
|
|
|
|
query = query.joins("LEFT JOIN categories ON categories.id = account_transactions.category_id").where(categories: { name: params[:categories] }) if params[:categories].present?
|
|
|
|
query = query.joins("LEFT JOIN merchants ON merchants.id = account_transactions.merchant_id").where(merchants: { name: params[:merchants] }) if params[:merchants].present?
|
|
|
|
query
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def requires_search?(params)
|
|
|
|
searchable_keys.any? { |key| params.key?(key) }
|
2024-05-30 20:55:18 -04:00
|
|
|
end
|
2024-03-28 13:23:54 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
private
|
2024-05-30 20:55:18 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def searchable_keys
|
2024-08-23 10:06:24 -04:00
|
|
|
%i[categories merchants]
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
2024-05-30 20:55:18 -04:00
|
|
|
end
|
2024-03-28 13:23:54 -04:00
|
|
|
|
2024-05-30 20:55:18 -04:00
|
|
|
private
|
|
|
|
def previous_transaction_date
|
|
|
|
self.account
|
|
|
|
.transactions
|
|
|
|
.where("date < ?", date)
|
|
|
|
.order(date: :desc)
|
|
|
|
.first&.date
|
2024-03-28 13:23:54 -04:00
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|