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

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { CreateCreditNoteService } from './commands/CreateCreditNote.service';
import { CommandCreditNoteDTOTransform } from './commands/CommandCreditNoteDTOTransform.service';
import { EditCreditNoteService } from './commands/EditCreditNote.service';
@@ -26,6 +26,13 @@ import { GetCreditNotesService } from './queries/GetCreditNotes.service';
import { DynamicListModule } from '../DynamicListing/DynamicList.module';
import { CreditNotesExportable } from './commands/CreditNotesExportable';
import { CreditNotesImportable } from './commands/CreditNotesImportable';
import { CreditNoteInventoryTransactionsSubscriber } from './subscribers/CreditNoteInventoryTransactionsSubscriber';
import { RefundSyncCreditNoteBalanceSubscriber } from './subscribers/RefundSyncCreditNoteBalanceSubscriber';
import { DeleteCustomerLinkedCreditSubscriber } from './subscribers/DeleteCustomerLinkedCreditSubscriber';
import { CreditNoteInventoryTransactions } from './commands/CreditNotesInventoryTransactions';
import { InventoryCostModule } from '../InventoryCost/InventoryCost.module';
import { CreditNoteRefundsModule } from '../CreditNoteRefunds/CreditNoteRefunds.module';
import { CreditNotesApplyInvoiceModule } from '../CreditNotesApplyInvoice/CreditNotesApplyInvoice.module';
@Module({
imports: [
@@ -39,6 +46,9 @@ import { CreditNotesImportable } from './commands/CreditNotesImportable';
LedgerModule,
AccountsModule,
DynamicListModule,
InventoryCostModule,
forwardRef(() => CreditNoteRefundsModule),
forwardRef(() => CreditNotesApplyInvoiceModule)
],
providers: [
CreateCreditNoteService,
@@ -57,6 +67,10 @@ import { CreditNotesImportable } from './commands/CreditNotesImportable';
CreditNoteGLEntriesSubscriber,
CreditNotesExportable,
CreditNotesImportable,
CreditNoteInventoryTransactions,
CreditNoteInventoryTransactionsSubscriber,
RefundSyncCreditNoteBalanceSubscriber,
DeleteCustomerLinkedCreditSubscriber,
],
exports: [
CreateCreditNoteService,

View File

@@ -22,7 +22,7 @@ export class CreditNoteBrandingTemplate {
// Retrieves the organization branding attributes.
const commonOrgBrandingAttrs =
await this.getOrgBrandingAttributes.getOrganizationBrandingAttributes();
await this.getOrgBrandingAttributes.execute();
// Merges the default branding attributes with common organization branding attrs.
const organizationBrandingAttrs = {

View File

@@ -1,30 +1,21 @@
// import { Service, Inject } from 'typedi';
// import events from '@/subscribers/events';
// import BaseCreditNotes from '../commands/CommandCreditNoteDTOTransform.service';
// import { ICreditNoteCreatedPayload } from '@/interfaces';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { CreditNoteAutoIncrementService } from '../commands/CreditNoteAutoIncrement.service';
import { ICreditNoteCreatedPayload } from '../types/CreditNotes.types';
import { events } from '@/common/events/events';
// @Service()
// export default class CreditNoteAutoSerialSubscriber {
// @Inject()
// creditNotesService: BaseCreditNotes;
@Injectable()
export default class CreditNoteAutoSerialSubscriber {
constructor(
private readonly creditNoteIncrementService: CreditNoteAutoIncrementService,
) {}
// /**
// * Attaches events with handlers.
// */
// public attach(bus) {
// bus.subscribe(
// events.creditNote.onCreated,
// this.autoSerialIncrementOnceCreated
// );
// }
// /**
// * Auto serial increment once credit note created.
// * @param {ICreditNoteCreatedPayload} payload -
// */
// private autoSerialIncrementOnceCreated = async ({
// tenantId,
// }: ICreditNoteCreatedPayload) => {
// await this.creditNotesService.incrementSerialNumber(tenantId);
// };
// }
/**
* Auto serial increment once credit note created.
* @param {ICreditNoteCreatedPayload} payload -
*/
@OnEvent(events.creditNote.onCreated)
async autoSerialIncrementOnceCreated({}: ICreditNoteCreatedPayload) {
await this.creditNoteIncrementService.incrementSerialNumber();
}
}

View File

@@ -1,48 +1,34 @@
// import { Inject, Service } from 'typedi';
// import { ServiceError } from '@/exceptions';
// import TenancyService from '@/services/Tenancy/TenancyService';
// import events from '@/subscribers/events';
// import { ICustomerDeletingPayload } from '@/interfaces';
// import DeleteCustomerLinkedCreidtNote from '../commands/DeleteCustomerLinkedCreditNote.service';
import { OnEvent } from '@nestjs/event-emitter';
import { Injectable } from '@nestjs/common';
import { DeleteCustomerLinkedCreditNoteService } from '@/modules/CreditNotesApplyInvoice/commands/DeleteCustomerLinkedCreditNote.service';
import { events } from '@/common/events/events';
import { ICustomerDeletingPayload } from '@/modules/Customers/types/Customers.types';
import { ServiceError } from '@/modules/Items/ServiceError';
// const ERRORS = {
// CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS',
// };
const ERRORS = {
CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS',
};
// @Service()
// export default class DeleteCustomerLinkedCreditSubscriber {
// @Inject()
// tenancy: TenancyService;
@Injectable()
export class DeleteCustomerLinkedCreditSubscriber {
constructor(
private readonly deleteCustomerLinkedCredit: DeleteCustomerLinkedCreditNoteService,
) {}
// @Inject()
// deleteCustomerLinkedCredit: DeleteCustomerLinkedCreidtNote;
// /**
// * Attaches events with handlers.
// * @param bus
// */
// public attach = (bus) => {
// bus.subscribe(
// events.customers.onDeleting,
// this.validateCustomerHasNoLinkedCreditsOnDeleting
// );
// };
// /**
// * Validate vendor has no associated credit transaction once the vendor deleting.
// * @param {IVendorEventDeletingPayload} payload -
// */
// public validateCustomerHasNoLinkedCreditsOnDeleting = async ({
// tenantId,
// customerId,
// }: ICustomerDeletingPayload) => {
// try {
// await this.deleteCustomerLinkedCredit.validateCustomerHasNoCreditTransaction(
// tenantId,
// customerId
// );
// } catch (error) {
// throw new ServiceError(ERRORS.CUSTOMER_HAS_TRANSACTIONS);
// }
// };
// }
/**
* Validate vendor has no associated credit transaction once the vendor deleting.
* @param {IVendorEventDeletingPayload} payload -
*/
@OnEvent(events.customers.onDeleting)
public async validateCustomerHasNoLinkedCreditsOnDeleting({
customerId,
}: ICustomerDeletingPayload) {
try {
await this.deleteCustomerLinkedCredit.validateCustomerHasNoCreditTransaction(
customerId,
);
} catch (error) {
throw new ServiceError(ERRORS.CUSTOMER_HAS_TRANSACTIONS);
}
}
}

View File

@@ -1,61 +1,47 @@
// import { Service, Inject } from 'typedi';
// import events from '@/subscribers/events';
// import RefundCreditNoteGLEntries from '../commands/RefundCreditNoteGLEntries';
// import {
// IRefundCreditNoteCreatedPayload,
// IRefundCreditNoteDeletedPayload,
// } from '@/interfaces';
import { events } from '@/common/events/events';
import { RefundCreditNoteGLEntries } from '@/modules/CreditNoteRefunds/commands/RefundCreditNoteGLEntries';
import {
IRefundCreditNoteCreatedPayload,
IRefundCreditNoteDeletedPayload,
} from '@/modules/CreditNoteRefunds/types/CreditNoteRefunds.types';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
// @Service()
// export default class RefundCreditNoteGLEntriesSubscriber {
// @Inject()
// refundCreditGLEntries: RefundCreditNoteGLEntries;
@Injectable()
export class RefundCreditNoteGLEntriesSubscriber {
constructor(
private readonly refundCreditGLEntries: RefundCreditNoteGLEntries,
) {}
// /**
// * Attaches events with handlers.
// */
// public attach = (bus) => {
// bus.subscribe(
// events.creditNote.onRefundCreated,
// this.writeRefundCreditGLEntriesOnceCreated
// );
// bus.subscribe(
// events.creditNote.onRefundDeleted,
// this.revertRefundCreditGLEntriesOnceDeleted
// );
// };
/**
* Writes refund credit note GL entries once the transaction created.
* @param {IRefundCreditNoteCreatedPayload} payload -
*/
@OnEvent(events.creditNote.onRefundCreated)
async writeRefundCreditGLEntriesOnceCreated({
trx,
refundCreditNote,
creditNote,
}: IRefundCreditNoteCreatedPayload) {
await this.refundCreditGLEntries.createRefundCreditGLEntries(
refundCreditNote.id,
trx,
);
}
// /**
// * Writes refund credit note GL entries once the transaction created.
// * @param {IRefundCreditNoteCreatedPayload} payload -
// */
// private writeRefundCreditGLEntriesOnceCreated = async ({
// trx,
// refundCreditNote,
// creditNote,
// tenantId,
// }: IRefundCreditNoteCreatedPayload) => {
// await this.refundCreditGLEntries.createRefundCreditGLEntries(
// tenantId,
// refundCreditNote.id,
// trx
// );
// };
// /**
// * Reverts refund credit note GL entries once the transaction deleted.
// * @param {IRefundCreditNoteDeletedPayload} payload -
// */
// private revertRefundCreditGLEntriesOnceDeleted = async ({
// trx,
// refundCreditId,
// oldRefundCredit,
// tenantId,
// }: IRefundCreditNoteDeletedPayload) => {
// await this.refundCreditGLEntries.revertRefundCreditGLEntries(
// tenantId,
// refundCreditId,
// trx
// );
// };
// }
/**
* Reverts refund credit note GL entries once the transaction deleted.
* @param {IRefundCreditNoteDeletedPayload} payload -
*/
@OnEvent(events.creditNote.onRefundDeleted)
async revertRefundCreditGLEntriesOnceDeleted({
trx,
refundCreditId,
oldRefundCredit,
}: IRefundCreditNoteDeletedPayload) {
await this.refundCreditGLEntries.revertRefundCreditGLEntries(
refundCreditId,
trx,
);
}
}

View File

@@ -1,62 +1,47 @@
// import { Inject, Service } from 'typedi';
// import {
// IRefundCreditNoteCreatedPayload,
// IRefundCreditNoteDeletedPayload,
// } from '@/interfaces';
// import events from '@/subscribers/events';
// import RefundSyncCreditNoteBalance from '../commands/RefundSyncCreditNoteBalance';
import { events } from '@/common/events/events';
import { RefundSyncCreditNoteBalanceService } from '@/modules/CreditNoteRefunds/commands/RefundSyncCreditNoteBalance';
import {
IRefundCreditNoteCreatedPayload,
IRefundCreditNoteDeletedPayload,
} from '@/modules/CreditNoteRefunds/types/CreditNoteRefunds.types';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
// @Service()
// export default class RefundSyncCreditNoteBalanceSubscriber {
// @Inject()
// refundSyncCreditBalance: RefundSyncCreditNoteBalance;
@Injectable()
export class RefundSyncCreditNoteBalanceSubscriber {
constructor(
private readonly refundSyncCreditBalance: RefundSyncCreditNoteBalanceService,
) {}
// /**
// * Attaches events with handlers.
// */
// attach(bus) {
// bus.subscribe(
// events.creditNote.onRefundCreated,
// this.incrementRefundedAmountOnceRefundCreated
// );
// bus.subscribe(
// events.creditNote.onRefundDeleted,
// this.decrementRefundedAmountOnceRefundDeleted
// );
// return bus;
// }
/**
* Increment credit note refunded amount once associated refund transaction created.
* @param {IRefundCreditNoteCreatedPayload} payload -
*/
@OnEvent(events.creditNote.onRefundCreated)
async incrementRefundedAmountOnceRefundCreated({
trx,
refundCreditNote,
}: IRefundCreditNoteCreatedPayload) {
await this.refundSyncCreditBalance.incrementCreditNoteRefundAmount(
refundCreditNote.creditNoteId,
refundCreditNote.amount,
trx,
);
}
// /**
// * Increment credit note refunded amount once associated refund transaction created.
// * @param {IRefundCreditNoteCreatedPayload} payload -
// */
// private incrementRefundedAmountOnceRefundCreated = async ({
// trx,
// refundCreditNote,
// tenantId,
// }: IRefundCreditNoteCreatedPayload) => {
// await this.refundSyncCreditBalance.incrementCreditNoteRefundAmount(
// tenantId,
// refundCreditNote.creditNoteId,
// refundCreditNote.amount,
// trx
// );
// };
// /**
// * Decrement credit note refunded amount once associated refuned transaction deleted.
// * @param {IRefundCreditNoteDeletedPayload} payload -
// */
// private decrementRefundedAmountOnceRefundDeleted = async ({
// trx,
// oldRefundCredit,
// tenantId,
// }: IRefundCreditNoteDeletedPayload) => {
// await this.refundSyncCreditBalance.decrementCreditNoteRefundAmount(
// tenantId,
// oldRefundCredit.creditNoteId,
// oldRefundCredit.amount,
// trx
// );
// };
// }
/**
* Decrement credit note refunded amount once associated refuned transaction deleted.
* @param {IRefundCreditNoteDeletedPayload} payload -
*/
@OnEvent(events.creditNote.onRefundDeleted)
async decrementRefundedAmountOnceRefundDeleted({
trx,
oldRefundCredit,
}: IRefundCreditNoteDeletedPayload) {
await this.refundSyncCreditBalance.decrementCreditNoteRefundAmount(
oldRefundCredit.creditNoteId,
oldRefundCredit.amount,
trx,
);
}
}