diff --git a/packages/server/src/services/Banking/Matching/GetMatchedTransactions.ts b/packages/server/src/services/Banking/Matching/GetMatchedTransactions.ts index 1f73081bf..bbe41a5aa 100644 --- a/packages/server/src/services/Banking/Matching/GetMatchedTransactions.ts +++ b/packages/server/src/services/Banking/Matching/GetMatchedTransactions.ts @@ -7,6 +7,7 @@ import { GetMatchedTransactionsByExpenses } from './GetMatchedTransactionsByExpe import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills'; import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals'; import HasTenancyService from '@/services/Tenancy/TenancyService'; +import { sortClosestMatchTransactions } from './_utils'; @Service() export class GetMatchedTransactions { @@ -88,14 +89,19 @@ export class GetMatchedTransactions { ): MatchedTransactionsPOJO { const results = R.compose(R.flatten)(matchedTransactions?.results); + // Sort the results based on amount, date, and transaction type + const closestResullts = sortClosestMatchTransactions( + uncategorizedTransaction, + results + ); const perfectMatches = R.filter( (match) => match.amount === uncategorizedTransaction.amount && moment(match.date).isSame(uncategorizedTransaction.date, 'day'), - results + closestResullts ); - const possibleMatches = R.difference(results, perfectMatches); + const possibleMatches = R.difference(closestResullts, perfectMatches); return { perfectMatches, possibleMatches }; } diff --git a/packages/server/src/services/Banking/Matching/types.ts b/packages/server/src/services/Banking/Matching/types.ts index b39ce3ed2..5e60f88f9 100644 --- a/packages/server/src/services/Banking/Matching/types.ts +++ b/packages/server/src/services/Banking/Matching/types.ts @@ -47,11 +47,12 @@ export interface MatchedTransactionPOJO { referenceNo: string; transactionNo: string; transactionId: number; + transactionType: string; } export type MatchedTransactionsPOJO = { - perfectMatches: Array; - possibleMatches: Array; + perfectMatches: Array; + possibleMatches: Array; }; export const ERRORS = {