mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 05:25:24 +02:00
Fix transaction filters when transfers are present (#1986)
* Proper filtering of transfers in search * Fix transaction search
This commit is contained in:
parent
ed55ef624b
commit
dd75cadebc
4 changed files with 96 additions and 45 deletions
|
@ -31,20 +31,6 @@ class Account::EntrySearch
|
|||
query
|
||||
end
|
||||
|
||||
def apply_type_filter(scope, types)
|
||||
return scope if types.blank?
|
||||
|
||||
query = scope
|
||||
|
||||
if types.include?("income") && !types.include?("expense")
|
||||
query = query.where("account_entries.amount < 0")
|
||||
elsif types.include?("expense") && !types.include?("income")
|
||||
query = query.where("account_entries.amount >= 0")
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
|
||||
def apply_amount_filter(scope, amount, amount_operator)
|
||||
return scope if amount.blank? || amount_operator.blank?
|
||||
|
||||
|
@ -76,7 +62,6 @@ class Account::EntrySearch
|
|||
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_type_filter(query, types)
|
||||
query = self.class.apply_amount_filter(query, amount, amount_operator)
|
||||
query = self.class.apply_accounts_filter(query, accounts, account_ids)
|
||||
query
|
||||
|
|
|
@ -14,39 +14,88 @@ class Account::TransactionSearch
|
|||
attribute :merchants, array: true
|
||||
attribute :tags, array: true
|
||||
|
||||
# Returns array of Account::Entry objects to stay consistent with partials, which only deal with Account::Entry
|
||||
def build_query(scope)
|
||||
query = scope.joins(entry: :account)
|
||||
.joins(transfer_join)
|
||||
|
||||
if types.present? && types.exclude?("transfer")
|
||||
query = query.joins("LEFT JOIN transfers ON transfers.inflow_transaction_id = account_entries.id OR transfers.outflow_transaction_id = account_entries.id")
|
||||
.where("transfers.id IS NULL")
|
||||
end
|
||||
|
||||
if categories.present?
|
||||
if categories.exclude?("Uncategorized")
|
||||
query = query
|
||||
.joins(:category)
|
||||
.where(categories: { name: categories })
|
||||
else
|
||||
query = query
|
||||
.left_joins(:category)
|
||||
.where(categories: { name: categories })
|
||||
.or(query.where(category_id: nil))
|
||||
end
|
||||
end
|
||||
|
||||
query = query.joins(:merchant).where(merchants: { name: merchants }) if merchants.present?
|
||||
|
||||
query = query.joins(:tags).where(tags: { name: tags }) if tags.present?
|
||||
|
||||
# Apply common entry search filters
|
||||
query = apply_category_filter(query, categories)
|
||||
query = apply_type_filter(query, types)
|
||||
query = apply_merchant_filter(query, merchants)
|
||||
query = apply_tag_filter(query, tags)
|
||||
query = Account::EntrySearch.apply_search_filter(query, search)
|
||||
query = Account::EntrySearch.apply_date_filters(query, start_date, end_date)
|
||||
query = Account::EntrySearch.apply_type_filter(query, types)
|
||||
query = Account::EntrySearch.apply_amount_filter(query, amount, amount_operator)
|
||||
query = Account::EntrySearch.apply_accounts_filter(query, accounts, account_ids)
|
||||
|
||||
query
|
||||
end
|
||||
|
||||
private
|
||||
def transfer_join
|
||||
<<~SQL
|
||||
LEFT JOIN (
|
||||
SELECT t.*, t.id as transfer_id, a.accountable_type
|
||||
FROM transfers t
|
||||
JOIN account_entries ae ON ae.entryable_id = t.inflow_transaction_id
|
||||
AND ae.entryable_type = 'Account::Transaction'
|
||||
JOIN accounts a ON a.id = ae.account_id
|
||||
) transfer_info ON (
|
||||
transfer_info.inflow_transaction_id = account_transactions.id OR
|
||||
transfer_info.outflow_transaction_id = account_transactions.id
|
||||
)
|
||||
SQL
|
||||
end
|
||||
|
||||
def apply_category_filter(query, categories)
|
||||
return query unless categories.present?
|
||||
|
||||
query = query.left_joins(:category).where(
|
||||
"categories.name IN (?) OR (
|
||||
categories.id IS NULL AND (transfer_info.transfer_id IS NULL OR transfer_info.accountable_type = 'Loan')
|
||||
)",
|
||||
categories
|
||||
)
|
||||
|
||||
if categories.exclude?("Uncategorized")
|
||||
query = query.where.not(category_id: nil)
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
|
||||
def apply_type_filter(query, types)
|
||||
return query unless types.present?
|
||||
return query if types.sort == [ "expense", "income", "transfer" ]
|
||||
|
||||
transfer_condition = "transfer_info.transfer_id IS NOT NULL"
|
||||
expense_condition = "account_entries.amount >= 0"
|
||||
income_condition = "account_entries.amount <= 0"
|
||||
|
||||
condition = case types.sort
|
||||
when [ "transfer" ]
|
||||
transfer_condition
|
||||
when [ "expense" ]
|
||||
Arel.sql("#{expense_condition} AND NOT (#{transfer_condition})")
|
||||
when [ "income" ]
|
||||
Arel.sql("#{income_condition} AND NOT (#{transfer_condition})")
|
||||
when [ "expense", "transfer" ]
|
||||
Arel.sql("#{expense_condition} OR #{transfer_condition}")
|
||||
when [ "income", "transfer" ]
|
||||
Arel.sql("#{income_condition} OR #{transfer_condition}")
|
||||
when [ "expense", "income" ]
|
||||
Arel.sql("NOT (#{transfer_condition})")
|
||||
end
|
||||
|
||||
query.where(condition)
|
||||
end
|
||||
|
||||
def apply_merchant_filter(query, merchants)
|
||||
return query unless merchants.present?
|
||||
query.joins(:merchant).where(merchants: { name: merchants })
|
||||
end
|
||||
|
||||
def apply_tag_filter(query, tags)
|
||||
return query unless tags.present?
|
||||
query.joins(:tags).where(tags: { name: tags })
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue