mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: auto fill categorize form from recognized transaction
This commit is contained in:
22
packages/server/src/services/Banking/Matching/_utils.ts
Normal file
22
packages/server/src/services/Banking/Matching/_utils.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
||||
import { MatchedTransactionPOJO } from './types';
|
||||
|
||||
export const sortClosestMatchTransactions = (
|
||||
uncategorizedTransaction: UncategorizedCashflowTransaction,
|
||||
matches: MatchedTransactionPOJO[]
|
||||
) => {
|
||||
return R.sortWith([
|
||||
// Sort by amount difference (closest to uncategorized transaction amount first)
|
||||
R.ascend((match: MatchedTransactionPOJO) =>
|
||||
Math.abs(match.amount - uncategorizedTransaction.amount)
|
||||
),
|
||||
// Sort by date difference (closest to uncategorized transaction date first)
|
||||
R.ascend((match: MatchedTransactionPOJO) =>
|
||||
Math.abs(
|
||||
moment(match.date).diff(moment(uncategorizedTransaction.date), 'days')
|
||||
)
|
||||
),
|
||||
])(matches);
|
||||
};
|
||||
@@ -20,6 +20,7 @@ import NewCashflowTransactionService from './NewCashflowTransactionService';
|
||||
import GetCashflowAccountsService from './GetCashflowAccountsService';
|
||||
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
|
||||
import { GetRecognizedTransactionsService } from './GetRecongizedTransactions';
|
||||
import { GetRecognizedTransactionService } from './GetRecognizedTransaction';
|
||||
|
||||
@Service()
|
||||
export class CashflowApplication {
|
||||
@@ -56,6 +57,9 @@ export class CashflowApplication {
|
||||
@Inject()
|
||||
private getRecognizedTranasctionsService: GetRecognizedTransactionsService;
|
||||
|
||||
@Inject()
|
||||
private getRecognizedTransactionService: GetRecognizedTransactionService;
|
||||
|
||||
/**
|
||||
* Creates a new cashflow transaction.
|
||||
* @param {number} tenantId
|
||||
@@ -234,4 +238,20 @@ export class CashflowApplication {
|
||||
filter
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the recognized transaction of the given uncategorized transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} uncategorizedTransactionId
|
||||
* @returns
|
||||
*/
|
||||
public getRecognizedTransaction(
|
||||
tenantId: number,
|
||||
uncategorizedTransactionId: number
|
||||
) {
|
||||
return this.getRecognizedTransactionService.getRecognizedTransaction(
|
||||
tenantId,
|
||||
uncategorizedTransactionId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
|
||||
@Service()
|
||||
export class GetRecognizedTransactionService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private transformer: TransformerInjectable;
|
||||
|
||||
/**
|
||||
* Retrieves the recognized transaction of the given uncategorized transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} uncategorizedTransactionId
|
||||
*/
|
||||
public async getRecognizedTransaction(
|
||||
tenantId: number,
|
||||
uncategorizedTransactionId: number
|
||||
) {
|
||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const uncategorizedTransaction =
|
||||
await UncategorizedCashflowTransaction.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.withGraphFetched('matchedBankTransactions')
|
||||
.withGraphFetched('recognizedTransaction.assignAccount')
|
||||
.withGraphFetched('recognizedTransaction.bankRule')
|
||||
.withGraphFetched('account')
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
tenantId,
|
||||
uncategorizedTransaction,
|
||||
new GetRecognizedTransactionTransformer()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user