Increase FX rate deviation tolerance to 10% when matching transfers (#877)

* Increase exchange rate deviation tolerance to 10% when matching transfers

* Fix test
This commit is contained in:
Jakub Kottnauer
2026-02-03 14:20:34 +01:00
committed by GitHub
parent 4be1c39e1f
commit 011957b4cb
2 changed files with 5 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
module Family::AutoTransferMatchable
def transfer_match_candidates(date_window: 4)
def transfer_match_candidates(date_window: 4, exchange_rate_tolerance: 0.1)
Entry.select([
"inflow_candidates.entryable_id as inflow_transaction_id",
"outflow_candidates.entryable_id as outflow_transaction_id",
@@ -39,7 +39,7 @@ module Family::AutoTransferMatchable
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
ABS(inflow_candidates.amount / NULLIF(outflow_candidates.amount * exchange_rates.rate, 0)) BETWEEN #{1 - exchange_rate_tolerance} AND #{1 + exchange_rate_tolerance}
)
")
.where(existing_transfers: { id: nil })

View File

@@ -27,7 +27,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
@family.auto_match_transfers!
end
# test match within lower 5% bound
# test match within lower 10% bound
create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 1000)
create_transaction(date: Date.current, account: @credit_card, amount: -1330, currency: "CAD")
@@ -35,7 +35,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
@family.auto_match_transfers!
end
# test match within upper 5% bound
# test match within upper 10% bound
create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 1500)
create_transaction(date: Date.current, account: @credit_card, amount: -2189, currency: "CAD")
@@ -45,7 +45,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
# test no match outside of slippage tolerance
create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 1000)
create_transaction(date: Date.current, account: @credit_card, amount: -1320, currency: "CAD")
create_transaction(date: Date.current, account: @credit_card, amount: -1250, currency: "CAD")
assert_difference -> { Transfer.count } => 0 do
@family.auto_match_transfers!