mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
Fix SimpleFIN liability balance sign when transaction history is incomplete (#642)
* Add sanity check for net balance mismatch in overpayment analysis - Introduced a validation to detect cases where transactions' net balance deviates significantly from the observed balance (using a 10% or $5 tolerance). - Returns `:unknown` with a `net-balance-mismatch` error for incomplete data scenarios. * Clarify liability net-balance comparison logic in overpayment analysis. --------- Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
@@ -180,6 +180,15 @@ class SimplefinAccount::Liabilities::OverpaymentAnalyzer
|
||||
end
|
||||
|
||||
eps = [ epsilon_base, (@observed.abs * BigDecimal("0.005")) ].max
|
||||
net = m[:charges_total] - m[:payments_total]
|
||||
|
||||
# Sanity check: if transaction net is way off from observed balance, data is likely incomplete
|
||||
# (e.g., pending charges not in history yet). Use 10% tolerance or minimum $5.
|
||||
# Note: SimpleFIN always sends negative for liabilities, so we compare magnitudes only.
|
||||
tolerance = [ BigDecimal("5"), @observed.abs * BigDecimal("0.10") ].max
|
||||
if (net.abs - @observed.abs).abs > tolerance
|
||||
return [ :unknown, "net-balance-mismatch" ]
|
||||
end
|
||||
|
||||
# Overpayment (credit): payments exceed charges by at least the observed balance (within eps)
|
||||
if (m[:payments_total] - m[:charges_total]) >= (@observed.abs - eps)
|
||||
|
||||
Reference in New Issue
Block a user