diff --git a/class/banktransaction.class.php b/class/banktransaction.class.php index a5814ac..96305a4 100755 --- a/class/banktransaction.class.php +++ b/class/banktransaction.class.php @@ -852,8 +852,22 @@ class BankImportTransaction extends CommonObject } } - // Sort by score descending - usort($matches, function($a, $b) { + // Sort by: 1) Amount difference (closer to transaction amount is better), 2) Score + $absAmount = abs($this->amount); + usort($matches, function($a, $b) use ($absAmount) { + $diffA = abs($a['amount'] - $absAmount); + $diffB = abs($b['amount'] - $absAmount); + + // If one match is much closer in amount (within 10% of transaction), prefer it + $threshold = $absAmount * 0.10; + if ($diffA < $threshold && $diffB >= $threshold) { + return -1; // A is better (closer amount) + } + if ($diffB < $threshold && $diffA >= $threshold) { + return 1; // B is better (closer amount) + } + + // Otherwise sort by score return $b['match_score'] - $a['match_score']; });