feat: apply credit note to invoice module

This commit is contained in:
Ahmed Bouhuolia
2025-05-04 01:32:08 +02:00
parent 1d53063e09
commit 4f6ad2b293
28 changed files with 633 additions and 707 deletions

View File

@@ -0,0 +1,27 @@
import { forwardRef, Module } from '@nestjs/common';
import { DeleteCustomerLinkedCreditNoteService } from './commands/DeleteCustomerLinkedCreditNote.service';
import { DeleteCreditNoteApplyToInvoices } from './commands/DeleteCreditNoteApplyToInvoices.service';
import { CreditNoteApplyToInvoices } from './commands/CreditNoteApplyToInvoices.service';
import { CreditNoteApplySyncInvoicesCreditedAmount } from './commands/CreditNoteApplySyncInvoices.service';
import { CreditNoteApplySyncCredit } from './commands/CreditNoteApplySyncCredit.service';
import { PaymentsReceivedModule } from '../PaymentReceived/PaymentsReceived.module';
import { CreditNotesModule } from '../CreditNotes/CreditNotes.module';
import { GetCreditNoteAssociatedAppliedInvoices } from './queries/GetCreditNoteAssociatedAppliedInvoices.service';
import { GetCreditNoteAssociatedInvoicesToApply } from './queries/GetCreditNoteAssociatedInvoicesToApply.service';
@Module({
providers: [
DeleteCustomerLinkedCreditNoteService,
DeleteCreditNoteApplyToInvoices,
CreditNoteApplyToInvoices,
CreditNoteApplySyncInvoicesCreditedAmount,
CreditNoteApplySyncCredit,
// GetCreditNoteAssociatedAppliedInvoices,
// GetCreditNoteAssociatedInvoicesToApply
],
exports: [
DeleteCustomerLinkedCreditNoteService,
],
imports: [PaymentsReceivedModule, forwardRef(() => CreditNotesModule)],
})
export class CreditNotesApplyInvoiceModule {}

View File

@@ -11,7 +11,7 @@ import { ERRORS } from '../../CreditNotes/constants';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export default class DeleteCreditNoteApplyToInvoices {
export class DeleteCreditNoteApplyToInvoices {
/**
* @param {UnitOfWork} uow - Unit of work.
* @param {EventEmitter2} eventPublisher - Event emitter.

View File

@@ -1,67 +1,52 @@
// import { Service, Inject } from 'typedi';
// import { sumBy } from 'lodash';
// import events from '@/subscribers/events';
// import {
// IApplyCreditToInvoicesCreatedPayload,
// IApplyCreditToInvoicesDeletedPayload,
// } from '@/interfaces';
// import CreditNoteApplySyncCredit from '../commands/CreditNoteApplySyncCredit.service';
import { sumBy } from 'lodash';
import { Injectable } from '@nestjs/common';
import {
IApplyCreditToInvoicesCreatedPayload,
IApplyCreditToInvoicesDeletedPayload,
} from '../types/CreditNoteApplyInvoice.types';
import { CreditNoteApplySyncCredit } from '../commands/CreditNoteApplySyncCredit.service';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
// @Service()
// export default class CreditNoteApplySyncCreditSubscriber {
// @Inject()
// syncInvoicedAmountWithCredit: CreditNoteApplySyncCredit;
@Injectable()
export class CreditNoteApplySyncCreditSubscriber {
constructor(
private readonly syncInvoicedAmountWithCredit: CreditNoteApplySyncCredit,
) {}
// /**
// *
// * @param bus
// */
// attach(bus) {
// bus.subscribe(
// events.creditNote.onApplyToInvoicesCreated,
// this.incrementCreditedAmountOnceApplyToInvoicesCreated
// );
// bus.subscribe(
// events.creditNote.onApplyToInvoicesDeleted,
// this.decrementCreditedAmountOnceApplyToInvoicesDeleted
// );
// }
/**
* Increment credited amount of credit note transaction once the transaction created.
* @param {IApplyCreditToInvoicesCreatedPayload} payload -
*/
@OnEvent(events.creditNote.onApplyToInvoicesCreated)
async incrementCreditedAmountOnceApplyToInvoicesCreated({
trx,
creditNote,
creditNoteAppliedInvoices,
}: IApplyCreditToInvoicesCreatedPayload) {
const totalCredited = sumBy(creditNoteAppliedInvoices, 'amount');
// /**
// * Increment credited amount of credit note transaction once the transaction created.
// * @param {IApplyCreditToInvoicesCreatedPayload} payload -
// */
// private incrementCreditedAmountOnceApplyToInvoicesCreated = async ({
// trx,
// creditNote,
// tenantId,
// creditNoteAppliedInvoices,
// }: IApplyCreditToInvoicesCreatedPayload) => {
// const totalCredited = sumBy(creditNoteAppliedInvoices, 'amount');
await this.syncInvoicedAmountWithCredit.incrementCreditNoteInvoicedAmount(
creditNote.id,
totalCredited,
trx,
);
}
// await this.syncInvoicedAmountWithCredit.incrementCreditNoteInvoicedAmount(
// tenantId,
// creditNote.id,
// totalCredited,
// trx
// );
// };
// /**
// * Decrement credited amount of credit note transaction once the transaction deleted.
// * @param {IApplyCreditToInvoicesDeletedPayload} payload -
// */
// private decrementCreditedAmountOnceApplyToInvoicesDeleted = async ({
// tenantId,
// creditNote,
// creditNoteAppliedToInvoice,
// trx,
// }: IApplyCreditToInvoicesDeletedPayload) => {
// await this.syncInvoicedAmountWithCredit.decrementCreditNoteInvoicedAmount(
// tenantId,
// creditNote.id,
// creditNoteAppliedToInvoice.amount,
// trx
// );
// };
// }
/**
* Decrement credited amount of credit note transaction once the transaction deleted.
* @param {IApplyCreditToInvoicesDeletedPayload} payload -
*/
@OnEvent(events.creditNote.onApplyToInvoicesDeleted)
async decrementCreditedAmountOnceApplyToInvoicesDeleted({
creditNote,
creditNoteAppliedToInvoice,
trx,
}: IApplyCreditToInvoicesDeletedPayload) {
await this.syncInvoicedAmountWithCredit.decrementCreditNoteInvoicedAmount(
creditNote.id,
creditNoteAppliedToInvoice.amount,
trx,
);
}
}

View File

@@ -1,61 +1,47 @@
// import { Service, Inject } from 'typedi';
// import events from '@/subscribers/events';
// import {
// IApplyCreditToInvoicesCreatedPayload,
// IApplyCreditToInvoicesDeletedPayload,
// } from '@/interfaces';
// import CreditNoteApplySyncInvoicesCreditedAmount from '../commands/CreditNoteApplySyncInvoices.service';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import {
IApplyCreditToInvoicesCreatedPayload,
IApplyCreditToInvoicesDeletedPayload,
} from '../types/CreditNoteApplyInvoice.types';
import { CreditNoteApplySyncInvoicesCreditedAmount } from '../commands/CreditNoteApplySyncInvoices.service';
import { events } from '@/common/events/events';
// @Service()
// export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber {
// @Inject()
// private syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount;
@Injectable()
export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber {
constructor(
private readonly syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount,
) {}
// /**
// * Attaches events with handlers.
// */
// public attach(bus) {
// bus.subscribe(
// events.creditNote.onApplyToInvoicesCreated,
// this.incrementAppliedInvoicesOnceCreditCreated
// );
// bus.subscribe(
// events.creditNote.onApplyToInvoicesDeleted,
// this.decrementAppliedInvoicesOnceCreditDeleted
// );
// }
/**
* Increment invoices credited amount once the credit note apply to invoices transaction
* @param {IApplyCreditToInvoicesCreatedPayload} payload -
*/
@OnEvent(events.creditNote.onApplyToInvoicesCreated)
async incrementAppliedInvoicesOnceCreditCreated({
trx,
creditNoteAppliedInvoices,
}: IApplyCreditToInvoicesCreatedPayload) {
await this.syncInvoicesWithCreditNote.incrementInvoicesCreditedAmount(
creditNoteAppliedInvoices,
trx,
);
}
// /**
// * Increment invoices credited amount once the credit note apply to invoices transaction
// * @param {IApplyCreditToInvoicesCreatedPayload} payload -
// */
// private incrementAppliedInvoicesOnceCreditCreated = async ({
// trx,
// tenantId,
// creditNoteAppliedInvoices,
// }: IApplyCreditToInvoicesCreatedPayload) => {
// await this.syncInvoicesWithCreditNote.incrementInvoicesCreditedAmount(
// tenantId,
// creditNoteAppliedInvoices,
// trx
// );
// };
// /**
// *
// * @param {IApplyCreditToInvoicesDeletedPayload} payload -
// */
// private decrementAppliedInvoicesOnceCreditDeleted = async ({
// trx,
// creditNoteAppliedToInvoice,
// tenantId,
// }: IApplyCreditToInvoicesDeletedPayload) => {
// // Decrement invoice credited amount.
// await this.syncInvoicesWithCreditNote.decrementInvoiceCreditedAmount(
// tenantId,
// creditNoteAppliedToInvoice.invoiceId,
// creditNoteAppliedToInvoice.amount,
// trx
// );
// };
// }
/**
*
* @param {IApplyCreditToInvoicesDeletedPayload} payload -
*/
@OnEvent(events.creditNote.onApplyToInvoicesDeleted)
async decrementAppliedInvoicesOnceCreditDeleted({
trx,
creditNoteAppliedToInvoice,
}: IApplyCreditToInvoicesDeletedPayload) {
// Decrement invoice credited amount.
await this.syncInvoicesWithCreditNote.decrementInvoiceCreditedAmount(
creditNoteAppliedToInvoice.invoiceId,
creditNoteAppliedToInvoice.amount,
trx,
);
}
}

View File

@@ -1,5 +1,6 @@
import { CreditNote } from '@/modules/CreditNotes/models/CreditNote';
import { Knex } from 'knex';
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
export interface ICreditNoteApplyInvoiceDTO {
entries: { invoiceId: number; amount: number }[];
@@ -17,12 +18,12 @@ export interface IApplyCreditToInvoicesDTO {
export interface IApplyCreditToInvoicesCreatedPayload {
trx: Knex.Transaction;
creditNote: CreditNote;
creditNoteAppliedInvoices: ICreditNoteAppliedToInvoice[];
creditNoteAppliedInvoices: CreditNoteAppliedInvoice[];
}
export interface IApplyCreditToInvoicesDeletedPayload {
trx: Knex.Transaction;
creditNote: CreditNote;
creditNoteAppliedToInvoice: ICreditNoteAppliedToInvoice;
creditNoteAppliedToInvoice: CreditNoteAppliedInvoice;
}
export interface ICreditNoteAppliedToInvoice {