feat: wip advanced payment

This commit is contained in:
Ahmed Bouhuolia
2024-07-23 15:02:39 +02:00
parent 1141991e44
commit 5c3a371e8a
3 changed files with 12 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
exports.up = function (knex) { exports.up = function (knex) {
return knex.schema.table('payment_receives', (table) => { return knex.schema.table('payment_receives', (table) => {
table.decimal('unapplied_amount', 13, 3).defaultTo(0); table.decimal('applied_amount', 13, 3).defaultTo(0);
table.decimal('used_amount', 13, 3).defaultTo(0);
table table
.integer('unearned_revenue_account_id') .integer('unearned_revenue_account_id')
.unsigned() .unsigned()
@@ -12,8 +11,7 @@ exports.up = function (knex) {
exports.down = function (knex) { exports.down = function (knex) {
return knex.schema.table('payment_receives', (table) => { return knex.schema.table('payment_receives', (table) => {
table.dropColumn('unapplied_amount'); table.dropColumn('applied_amount');
table.dropColumn('used_amount');
table.dropColumn('unearned_revenue_account_id'); table.dropColumn('unearned_revenue_account_id');
}); });
}; };

View File

@@ -31,26 +31,27 @@ export class AutoApplyUnearnedRevenue {
): Promise<void> { ): Promise<void> {
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId); const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
const unappliedPayments = await PaymentReceive.query(trx).where(
'unappliedAmount',
'>',
0
);
const invoice = await SaleInvoice.query(trx) const invoice = await SaleInvoice.query(trx)
.findById(saleInvoiceId) .findById(saleInvoiceId)
.throwIfNotFound(); .throwIfNotFound();
const unappliedPayments = await PaymentReceive.query(trx)
.where('customerId', invoice.customerId)
.whereRaw('amount - applied_amount > 0')
.whereNotNull('unearnedRevenueAccountId');
let unappliedAmount = invoice.total; let unappliedAmount = invoice.total;
let appliedTotalAmount = 0; let appliedTotalAmount = 0; // Total applied amount after applying.
const processHandler: ProcessHandler< const processHandler: ProcessHandler<
IPaymentReceive, IPaymentReceive,
Promise<void> Promise<void>
> = async (unappliedPayment: IPaymentReceive, index: number, pool) => { > = async (unappliedPayment: IPaymentReceive, index: number, pool) => {
const appliedAmount = Math.min(unappliedAmount, unappliedAmount); const appliedAmount = Math.min(unappliedAmount, unappliedPayment.amount);
unappliedAmount = unappliedAmount - appliedAmount; unappliedAmount = unappliedAmount - appliedAmount;
appliedTotalAmount += appliedAmount; appliedTotalAmount += appliedAmount;
// Stop applying once the unapplied amount reache zero or less.
if (appliedAmount <= 0) { if (appliedAmount <= 0) {
pool.stop(); pool.stop();
return; return;
@@ -108,7 +109,7 @@ export class AutoApplyUnearnedRevenue {
invoiceId, invoiceId,
paymentAmount: appliedAmount, paymentAmount: appliedAmount,
}); });
await PaymentReceive.query(trx).increment('usedAmount', appliedAmount); await PaymentReceive.query(trx).increment('appliedAmount', appliedAmount);
// Triggers the event `onPaymentReceivedUnearnedRevenue`. // Triggers the event `onPaymentReceivedUnearnedRevenue`.
await this.eventPublisher.emitAsync( await this.eventPublisher.emitAsync(

View File

@@ -37,7 +37,6 @@ export class PaymentReceiveDTOTransformer {
oldPaymentReceive?: IPaymentReceive oldPaymentReceive?: IPaymentReceive
): Promise<IPaymentReceive> { ): Promise<IPaymentReceive> {
const appliedAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount'); const appliedAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount');
const unappliedAmount = paymentReceiveDTO.amount - appliedAmount;
// Retreive the next invoice number. // Retreive the next invoice number.
const autoNextNumber = const autoNextNumber =
@@ -55,7 +54,7 @@ export class PaymentReceiveDTOTransformer {
...formatDateFields(omit(paymentReceiveDTO, ['entries', 'attachments']), [ ...formatDateFields(omit(paymentReceiveDTO, ['entries', 'attachments']), [
'paymentDate', 'paymentDate',
]), ]),
unappliedAmount, appliedAmount,
currencyCode: customer.currencyCode, currencyCode: customer.currencyCode,
...(paymentReceiveNo ? { paymentReceiveNo } : {}), ...(paymentReceiveNo ? { paymentReceiveNo } : {}),
exchangeRate: paymentReceiveDTO.exchangeRate || 1, exchangeRate: paymentReceiveDTO.exchangeRate || 1,