mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
fix: matching bank transactions should create associate payment transactions for bills and invoicese. (#606)
This commit is contained in:
@@ -67,7 +67,7 @@ export interface IPaymentReceivedEntry {
|
|||||||
export interface IPaymentReceivedEntryDTO {
|
export interface IPaymentReceivedEntryDTO {
|
||||||
id?: number;
|
id?: number;
|
||||||
index: number;
|
index: number;
|
||||||
paymentReceiveId: number;
|
paymentReceiveId?: number;
|
||||||
invoiceId: number;
|
invoiceId: number;
|
||||||
paymentAmount: number;
|
paymentAmount: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,25 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { initialize } from 'objection';
|
import { initialize } from 'objection';
|
||||||
|
import { Knex } from 'knex';
|
||||||
|
import { first } from 'lodash';
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||||
import { GetMatchedTransactionBillsTransformer } from './GetMatchedTransactionBillsTransformer';
|
import { GetMatchedTransactionBillsTransformer } from './GetMatchedTransactionBillsTransformer';
|
||||||
import { GetMatchedTransactionsFilter, MatchedTransactionPOJO } from './types';
|
import {
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
GetMatchedTransactionsFilter,
|
||||||
|
IMatchTransactionDTO,
|
||||||
|
MatchedTransactionPOJO,
|
||||||
|
} from './types';
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||||
|
import { CreateBillPayment } from '@/services/Purchases/BillPayments/CreateBillPayment';
|
||||||
|
import { IBillPaymentDTO } from '@/interfaces';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class GetMatchedTransactionsByBills extends GetMatchedTransactionsByType {
|
export class GetMatchedTransactionsByBills extends GetMatchedTransactionsByType {
|
||||||
@Inject()
|
@Inject()
|
||||||
private tenancy: HasTenancyService;
|
private transformer: TransformerInjectable;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
private transformer: TransformerInjectable;
|
private createPaymentMadeService: CreateBillPayment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the matched transactions.
|
* Retrieves the matched transactions.
|
||||||
@@ -71,4 +78,62 @@ export class GetMatchedTransactionsByBills extends GetMatchedTransactionsByType
|
|||||||
new GetMatchedTransactionBillsTransformer()
|
new GetMatchedTransactionBillsTransformer()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the common matched transaction.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {Array<number>} uncategorizedTransactionIds
|
||||||
|
* @param {IMatchTransactionDTO} matchTransactionDTO
|
||||||
|
* @param {Knex.Transaction} trx
|
||||||
|
*/
|
||||||
|
public async createMatchedTransaction(
|
||||||
|
tenantId: number,
|
||||||
|
uncategorizedTransactionIds: Array<number>,
|
||||||
|
matchTransactionDTO: IMatchTransactionDTO,
|
||||||
|
trx?: Knex.Transaction
|
||||||
|
): Promise<void> {
|
||||||
|
await super.createMatchedTransaction(
|
||||||
|
tenantId,
|
||||||
|
uncategorizedTransactionIds,
|
||||||
|
matchTransactionDTO,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
const { Bill, UncategorizedCashflowTransaction, MatchedBankTransaction } =
|
||||||
|
this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
const uncategorizedTransactionId = first(uncategorizedTransactionIds);
|
||||||
|
const uncategorizedTransaction =
|
||||||
|
await UncategorizedCashflowTransaction.query(trx)
|
||||||
|
.findById(uncategorizedTransactionId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const bill = await Bill.query(trx)
|
||||||
|
.findById(matchTransactionDTO.referenceId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const createPaymentMadeDTO: IBillPaymentDTO = {
|
||||||
|
vendorId: bill.vendorId,
|
||||||
|
paymentAccountId: uncategorizedTransaction.accountId,
|
||||||
|
paymentDate: uncategorizedTransaction.date,
|
||||||
|
exchangeRate: 1,
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
paymentAmount: bill.dueAmount,
|
||||||
|
billId: bill.id,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
branchId: bill.branchId,
|
||||||
|
};
|
||||||
|
// Create a new bill payment associated to the matched bill.
|
||||||
|
const billPayment = await this.createPaymentMadeService.createBillPayment(
|
||||||
|
tenantId,
|
||||||
|
createPaymentMadeDTO,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
// Link the create bill payment with matched transaction.
|
||||||
|
await super.createMatchedTransaction(tenantId, uncategorizedTransactionIds, {
|
||||||
|
referenceType: 'BillPayment',
|
||||||
|
referenceId: billPayment.id,
|
||||||
|
}, trx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,26 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { initialize } from 'objection';
|
import { initialize } from 'objection';
|
||||||
|
import { Knex } from 'knex';
|
||||||
|
import { first } from 'lodash';
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||||
import { GetMatchedTransactionInvoicesTransformer } from './GetMatchedTransactionInvoicesTransformer';
|
import { GetMatchedTransactionInvoicesTransformer } from './GetMatchedTransactionInvoicesTransformer';
|
||||||
import {
|
import {
|
||||||
GetMatchedTransactionsFilter,
|
GetMatchedTransactionsFilter,
|
||||||
|
IMatchTransactionDTO,
|
||||||
MatchedTransactionPOJO,
|
MatchedTransactionPOJO,
|
||||||
MatchedTransactionsPOJO,
|
MatchedTransactionsPOJO,
|
||||||
} from './types';
|
} from './types';
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||||
|
import { CreatePaymentReceived } from '@/services/Sales/PaymentReceived/CreatePaymentReceived';
|
||||||
|
import { IPaymentReceivedCreateDTO } from '@/interfaces';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class GetMatchedTransactionsByInvoices extends GetMatchedTransactionsByType {
|
export class GetMatchedTransactionsByInvoices extends GetMatchedTransactionsByType {
|
||||||
@Inject()
|
@Inject()
|
||||||
protected tenancy: HasTenancyService;
|
protected transformer: TransformerInjectable;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
protected transformer: TransformerInjectable;
|
protected createPaymentReceivedService: CreatePaymentReceived;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the matched transactions.
|
* Retrieves the matched transactions.
|
||||||
@@ -78,4 +82,64 @@ export class GetMatchedTransactionsByInvoices extends GetMatchedTransactionsByTy
|
|||||||
new GetMatchedTransactionInvoicesTransformer()
|
new GetMatchedTransactionInvoicesTransformer()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the common matched transaction.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {Array<number>} uncategorizedTransactionIds
|
||||||
|
* @param {IMatchTransactionDTO} matchTransactionDTO
|
||||||
|
* @param {Knex.Transaction} trx
|
||||||
|
*/
|
||||||
|
public async createMatchedTransaction(
|
||||||
|
tenantId: number,
|
||||||
|
uncategorizedTransactionIds: Array<number>,
|
||||||
|
matchTransactionDTO: IMatchTransactionDTO,
|
||||||
|
trx?: Knex.Transaction
|
||||||
|
) {
|
||||||
|
await super.createMatchedTransaction(
|
||||||
|
tenantId,
|
||||||
|
uncategorizedTransactionIds,
|
||||||
|
matchTransactionDTO,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
const { SaleInvoice, UncategorizedCashflowTransaction, MatchedBankTransaction } =
|
||||||
|
this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
const uncategorizedTransactionId = first(uncategorizedTransactionIds);
|
||||||
|
const uncategorizedTransaction =
|
||||||
|
await UncategorizedCashflowTransaction.query(trx)
|
||||||
|
.findById(uncategorizedTransactionId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const invoice = await SaleInvoice.query(trx)
|
||||||
|
.findById(matchTransactionDTO.referenceId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const createPaymentReceivedDTO: IPaymentReceivedCreateDTO = {
|
||||||
|
customerId: invoice.customerId,
|
||||||
|
paymentDate: uncategorizedTransaction.date,
|
||||||
|
amount: invoice.dueAmount,
|
||||||
|
depositAccountId: uncategorizedTransaction.accountId,
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
index: 1,
|
||||||
|
invoiceId: invoice.id,
|
||||||
|
paymentAmount: invoice.dueAmount,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
branchId: invoice.branchId,
|
||||||
|
};
|
||||||
|
// Create a payment received associated to the matched invoice.
|
||||||
|
const paymentReceived = await this.createPaymentReceivedService.createPaymentReceived(
|
||||||
|
tenantId,
|
||||||
|
createPaymentReceivedDTO,
|
||||||
|
{},
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
// Link the create payment received with matched invoice transaction.
|
||||||
|
await super.createMatchedTransaction(tenantId, uncategorizedTransactionIds, {
|
||||||
|
referenceType: 'PaymentReceive',
|
||||||
|
referenceId: paymentReceived.id,
|
||||||
|
}, trx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user