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

View File

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

View File

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