1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

transfer: Support transfers of different currencies between accounts. (#2243)

Fixes part of #1852.

Co-authored-by: Zach Gollwitzer <zach@maybe.co>
This commit is contained in:
Joseph Ho 2025-06-25 16:34:18 -04:00 committed by GitHub
parent 72a0f87a9c
commit 637d630388
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 106 additions and 2 deletions

View file

@ -9,8 +9,6 @@ module Family::AutoTransferMatchable
JOIN entries outflow_candidates ON (
inflow_candidates.amount < 0 AND
outflow_candidates.amount > 0 AND
inflow_candidates.amount = -outflow_candidates.amount AND
inflow_candidates.currency = outflow_candidates.currency AND
inflow_candidates.account_id <> outflow_candidates.account_id AND
inflow_candidates.date BETWEEN outflow_candidates.date - 4 AND outflow_candidates.date + 4
)
@ -24,12 +22,26 @@ module Family::AutoTransferMatchable
rejected_transfers.inflow_transaction_id = inflow_candidates.entryable_id AND
rejected_transfers.outflow_transaction_id = outflow_candidates.entryable_id
)")
.joins("LEFT JOIN exchange_rates ON (
exchange_rates.date = outflow_candidates.date AND
exchange_rates.from_currency = outflow_candidates.currency AND
exchange_rates.to_currency = inflow_candidates.currency
)")
.joins("JOIN accounts inflow_accounts ON inflow_accounts.id = inflow_candidates.account_id")
.joins("JOIN accounts outflow_accounts ON outflow_accounts.id = outflow_candidates.account_id")
.where("inflow_accounts.family_id = ? AND outflow_accounts.family_id = ?", self.id, self.id)
.where("inflow_accounts.is_active = true")
.where("outflow_accounts.is_active = true")
.where("inflow_candidates.entryable_type = 'Transaction' AND outflow_candidates.entryable_type = 'Transaction'")
.where("
(
inflow_candidates.currency = outflow_candidates.currency AND
inflow_candidates.amount = -outflow_candidates.amount
) OR (
inflow_candidates.currency <> outflow_candidates.currency AND
ABS(inflow_candidates.amount / NULLIF(outflow_candidates.amount * exchange_rates.rate, 0)) BETWEEN 0.95 AND 1.05
)
")
.where(existing_transfers: { id: nil })
.order("date_diff ASC") # Closest matches first
end