2025-04-14 11:40:34 -04:00
|
|
|
class EntrySearch
|
2024-12-20 11:37:26 -05:00
|
|
|
include ActiveModel::Model
|
|
|
|
include ActiveModel::Attributes
|
|
|
|
|
|
|
|
attribute :search, :string
|
|
|
|
attribute :amount, :string
|
|
|
|
attribute :amount_operator, :string
|
|
|
|
attribute :types, :string
|
2025-01-07 09:41:24 -05:00
|
|
|
attribute :accounts, array: true
|
|
|
|
attribute :account_ids, array: true
|
2024-12-20 11:37:26 -05:00
|
|
|
attribute :start_date, :string
|
|
|
|
attribute :end_date, :string
|
|
|
|
|
|
|
|
class << self
|
2025-02-21 11:57:59 -05:00
|
|
|
def apply_search_filter(scope, search)
|
|
|
|
return scope if search.blank?
|
|
|
|
|
|
|
|
query = scope
|
2025-04-18 11:39:58 -04:00
|
|
|
query = query.where("entries.name ILIKE :search",
|
2025-02-21 11:57:59 -05:00
|
|
|
search: "%#{ActiveRecord::Base.sanitize_sql_like(search)}%"
|
|
|
|
)
|
|
|
|
query
|
2024-12-20 11:37:26 -05:00
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def apply_date_filters(scope, start_date, end_date)
|
|
|
|
return scope if start_date.blank? && end_date.blank?
|
2024-12-20 11:37:26 -05:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
query = scope
|
2025-04-14 11:40:34 -04:00
|
|
|
query = query.where("entries.date >= ?", start_date) if start_date.present?
|
|
|
|
query = query.where("entries.date <= ?", end_date) if end_date.present?
|
2025-02-21 11:57:59 -05:00
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_amount_filter(scope, amount, amount_operator)
|
|
|
|
return scope if amount.blank? || amount_operator.blank?
|
|
|
|
|
|
|
|
query = scope
|
|
|
|
|
2024-12-20 11:37:26 -05:00
|
|
|
case amount_operator
|
|
|
|
when "equal"
|
2025-04-14 11:40:34 -04:00
|
|
|
query = query.where("ABS(ABS(entries.amount) - ?) <= 0.01", amount.to_f.abs)
|
2024-12-20 11:37:26 -05:00
|
|
|
when "less"
|
2025-04-14 11:40:34 -04:00
|
|
|
query = query.where("ABS(entries.amount) < ?", amount.to_f.abs)
|
2024-12-20 11:37:26 -05:00
|
|
|
when "greater"
|
2025-04-14 11:40:34 -04:00
|
|
|
query = query.where("ABS(entries.amount) > ?", amount.to_f.abs)
|
2024-12-20 11:37:26 -05:00
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
query
|
2024-12-20 11:37:26 -05:00
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def apply_accounts_filter(scope, accounts, account_ids)
|
|
|
|
return scope if accounts.blank? && account_ids.blank?
|
2024-12-20 11:37:26 -05:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
query = scope
|
|
|
|
query = query.where(accounts: { name: accounts }) if accounts.present?
|
|
|
|
query = query.where(accounts: { id: account_ids }) if account_ids.present?
|
|
|
|
query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_query(scope)
|
|
|
|
query = scope.joins(:account)
|
|
|
|
query = self.class.apply_search_filter(query, search)
|
|
|
|
query = self.class.apply_date_filters(query, start_date, end_date)
|
|
|
|
query = self.class.apply_amount_filter(query, amount, amount_operator)
|
|
|
|
query = self.class.apply_accounts_filter(query, accounts, account_ids)
|
2024-12-20 11:37:26 -05:00
|
|
|
query
|
|
|
|
end
|
|
|
|
end
|