Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconcile match transactionss #522

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: cashflow transaction matching
  • Loading branch information
abouolia committed Jul 4, 2024
commit 87f60f746186e47c9e6dbecbb81576074c135533
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Transformer } from '@/lib/Transformer/Transformer';

export class GetMatchedTransactionCashflowTransformer extends Transformer {
/**
* Include these attributes to sale credit note object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'referenceNo',
'amount',
'amountFormatted',
'transactionNo',
'date',
'dateFormatted',
'transactionId',
'transactionNo',
'transactionType',
'transsactionTypeFormatted',
'referenceId',
'referenceType',
];
};

/**
* Exclude all attributes.
* @returns {Array<string>}
*/
public excludeAttributes = (): string[] => {
return ['*'];
};

/**
* Retrieve the invoice reference number.
* @returns {string}
*/
protected referenceNo(invoice) {
return invoice.referenceNo;
}

/**
* Retrieve the transaction amount.
* @param transaction
* @returns {number}
*/
protected amount(transaction) {
return transaction.amount;
}

/**
* Retrieve the transaction formatted amount.
* @param transaction
* @returns {string}
*/
protected amountFormatted(transaction) {
return this.formatNumber(transaction.amount, {
currencyCode: transaction.currencyCode,
money: true,
});
}

/**
* Retrieve the date of the invoice.
* @param invoice
* @returns {Date}
*/
protected date(transaction) {
return transaction.date;
}

/**
* Format the date of the invoice.
* @param invoice
* @returns {string}
*/
protected dateFormatted(transaction) {
return this.formatDate(transaction.date);
}

/**
* Retrieve the transaction ID of the invoice.
* @param invoice
* @returns {number}
*/
protected transactionId(transaction) {
return transaction.id;
}

/**
* Retrieve the invoice transaction number.
* @param invoice
* @returns {string}
*/
protected transactionNo(transaction) {
return transaction.transactionNumber;
}

/**
* Retrieve the invoice transaction type.
* @param invoice
* @returns {String}
*/
protected transactionType(transaction) {
return transaction.transactionType;
}

/**
* Retrieve the invoice formatted transaction type.
* @param invoice
* @returns {string}
*/
protected transsactionTypeFormatted(transaction) {
return transaction.transactionTypeFormatted;
}

protected referenceId(transaction) {
return transaction.id;
}

protected referenceType() {
return 'CashflowTransaction';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class GetMatchedTransactionInvoicesTransformer extends Transformer {
* @param invoice
* @returns {string}
*/
protected formatAmount(invoice) {
protected amountFormatted(invoice) {
return this.formatNumber(invoice.dueAmount, {
currencyCode: invoice.currencyCode,
money: true,
Expand Down Expand Up @@ -79,7 +79,7 @@ export class GetMatchedTransactionInvoicesTransformer extends Transformer {
* @param invoice
* @returns {number}
*/
protected getTransactionId(invoice) {
protected transactionId(invoice) {
return invoice.id;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { sortClosestMatchTransactions } from './_utils';
import { GetMatchedTransactionsByCashflow } from './GetMatchedTransactionsByCashflow';

@Service()
export class GetMatchedTransactions {
Expand All @@ -26,6 +27,9 @@ export class GetMatchedTransactions {
@Inject()
private getMatchedExpensesService: GetMatchedTransactionsByExpenses;

@Inject()
private getMatchedCashflowService: GetMatchedTransactionsByCashflow;

/**
* Registered matched transactions types.
*/
Expand All @@ -35,6 +39,7 @@ export class GetMatchedTransactions {
{ type: 'Bill', service: this.getMatchedBillsService },
{ type: 'Expense', service: this.getMatchedExpensesService },
{ type: 'ManualJournal', service: this.getMatchedManualJournalService },
{ type: 'Cashflow', service: this.getMatchedCashflowService },
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Inject, Service } from 'typedi';
import { initialize } from 'objection';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
import { GetMatchedTransactionCashflowTransformer } from './GetMatchedTransactionCashflowTransformer';
import { GetMatchedTransactionsFilter } from './types';

@Service()
export class GetMatchedTransactionsByCashflow extends GetMatchedTransactionsByType {
@Inject()
private transformer: TransformerInjectable;

/**
* Retrieve the matched transactions of cash flow.
* @param {number} tenantId
* @param {GetMatchedTransactionsFilter} filter
* @returns
*/
async getMatchedTransactions(
tenantId: number,
filter: Omit<GetMatchedTransactionsFilter, 'transactionType'>
) {
const { CashflowTransaction, MatchedBankTransaction } =
this.tenancy.models(tenantId);
const knex = this.tenancy.knex(tenantId);

// Initialize the ORM models metadata.
await initialize(knex, [CashflowTransaction, MatchedBankTransaction]);

const transactions = await CashflowTransaction.query()
.withGraphJoined('matchedBankTransaction')
.whereNull('matchedBankTransaction.id');

return this.transformer.transform(
tenantId,
transactions,
new GetMatchedTransactionCashflowTransformer()
);
}

/**
* Retrieves the matched transaction of cash flow.
* @param {number} tenantId
* @param {number} transactionId
* @returns
*/
async getMatchedTransaction(tenantId: number, transactionId: number) {
const { CashflowTransaction, MatchedBankTransaction } =
this.tenancy.models(tenantId);
const knex = this.tenancy.knex(tenantId);

// Initialize the ORM models metadata.
await initialize(knex, [CashflowTransaction, MatchedBankTransaction]);

const transactions = await CashflowTransaction.query()
.findById(transactionId)
.withGraphJoined('matchedBankTransaction')
.whereNull('matchedBankTransaction.id')
.throwIfNotFound();

return this.transformer.transform(
tenantId,
transactions,
new GetMatchedTransactionCashflowTransformer()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
import { MatchTransactionsTypesRegistry } from './MatchTransactionsTypesRegistry';
import { GetMatchedTransactionsByInvoices } from './GetMatchedTransactionsByInvoices';
import { GetMatchedTransactionCashflowTransformer } from './GetMatchedTransactionCashflowTransformer';
import { GetMatchedTransactionsByCashflow } from './GetMatchedTransactionsByCashflow';

@Service()
export class MatchTransactionsTypes {
Expand All @@ -25,6 +27,10 @@ export class MatchTransactionsTypes {
type: 'ManualJournal',
service: GetMatchedTransactionsByManualJournals,
},
{
type: 'CashflowTransaction',
service: GetMatchedTransactionsByCashflow,
},
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ import {
import { useCreateCashflowTransaction } from '@/hooks/query';
import { useAccountTransactionsContext } from '../../AccountTransactions/AccountTransactionsProvider';
import { MatchingReconcileFormSchema } from './MatchingReconcileTransactionForm.schema';
import { initialValues } from './_utils';
import { initialValues, transformToReq } from './_utils';

interface MatchingReconcileTransactionFormProps {
onSubmitSuccess?: (values: any) => void;
}

function MatchingReconcileTransactionFormRoot({
closeReconcileMatchingTransaction,
}) {

// #props¿
onSubmitSuccess,
}: MatchingReconcileTransactionFormProps) {
// Mutation create cashflow transaction.
const { mutateAsync: createCashflowTransactionMutate } =
useCreateCashflowTransaction();
Expand All @@ -47,14 +54,16 @@ function MatchingReconcileTransactionFormRoot({
const _values = transformToReq(values, accountId);

createCashflowTransactionMutate(_values)
.then(() => {
.then((res) => {
setSubmitting(false);

AppToaster.show({
message: 'The transaction has been created.',
intent: Intent.SUCCESS,
});
closeReconcileMatchingTransaction();
onSubmitSuccess &&
onSubmitSuccess({ id: res.data.id, type: 'CashflowTransaction' });
})
.catch((error) => {
setSubmitting(false);
Expand Down Expand Up @@ -97,25 +106,6 @@ export const MatchingReconcileTransactionForm = R.compose(withBankingActions)(
MatchingReconcileTransactionFormRoot,
);

export function MatchingReconcileTransactionFooter() {
const { isSubmitting } = useFormikContext();

return (
<Box className={styles.footer}>
<Group>
<Button
fill
type={'submit'}
intent={Intent.PRIMARY}
loading={isSubmitting}
>
Submit
</Button>
</Group>
</Box>
);
}

function ReconcileMatchingType() {
const { setFieldValue, values } =
useFormikContext<MatchingReconcileFormValues>();
Expand All @@ -135,7 +125,7 @@ function ReconcileMatchingType() {
);
}

export function CreateReconcileTransactionContent() {
function CreateReconcileTransactionContent() {
const { accounts, branches } = useMatchingReconcileTransactionBoot();

return (
Expand Down Expand Up @@ -197,3 +187,22 @@ export function CreateReconcileTransactionContent() {
</Box>
);
}

function MatchingReconcileTransactionFooter() {
const { isSubmitting } = useFormikContext();

return (
<Box className={styles.footer}>
<Group>
<Button
fill
type={'submit'}
intent={Intent.PRIMARY}
loading={isSubmitting}
>
Submit
</Button>
</Group>
</Box>
);
}
Loading