From 77bbf6828dfe33f332340f3e609f1c82a0dbf9d8 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sun, 29 Dec 2024 22:55:42 +0200 Subject: [PATCH] refactor: credit notes and vendor credits to Nestjs --- packages/server-nest/package.json | 1 + .../server-nest/src/common/types/Discount.ts | 4 + .../server-nest/src/modules/App/App.module.ts | 12 +- .../BillPayments/BillPayments.module.ts | 3 +- .../CreditNoteRefunds.controller.ts | 42 ++++ .../CreditNoteRefunds.module.ts | 21 ++ .../CreditNotesRefundsApplication.service.ts | 43 ++++ .../CreateRefundCreditNote.service.ts | 16 +- .../DeleteRefundCreditNote.service.ts | 18 +- .../commands/RefundCreditNote.service.ts | 2 +- .../commands/RefundCreditNoteGLEntries.ts | 0 .../commands/RefundSyncCreditNoteBalance.ts | 4 +- .../models/RefundCreditNote.ts | 0 .../queries/GetCreditNoteRefunds.service.ts | 4 +- .../GetRefundCreditNoteTransaction.service.ts | 5 +- .../types/CreditNoteRefunds.types.ts | 47 ++++ .../CreditNoteApplication.service.ts | 39 ++-- .../CreditNotes/CreditNotes.controller.ts | 12 - .../modules/CreditNotes/CreditNotes.module.ts | 45 +++- ...reditNoteAssociatedInvoicesToApply copy.ts | 42 ---- .../GetCreditNoteAssociatedInvoicesToApply.ts | 1 - .../CommandCreditNoteDTOTransform.service.ts | 7 +- .../commands/CreateCreditNote.service.ts | 18 +- .../commands/DeleteCreditNote.service.ts | 4 +- .../commands/OpenCreditNote.service.ts | 10 +- .../modules/CreditNotes/models/CreditNote.ts | 157 +++++++++++-- .../queries/GetCreditNotePdf.serivce.ts | 9 + .../CreditNoteApplySyncCreditSubscriber.ts | 67 ------ .../CreditNoteApplySyncInvoicesSubscriber.ts | 61 ----- .../CreditNoteAutoSerialSubscriber.ts | 54 ++--- .../CreditNoteGLEntriesSubscriber.ts | 208 +++++++++--------- ...editNoteInventoryTransactionsSubscriber.ts | 180 +++++++-------- .../DeleteCustomerLinkedCreditSubscriber.ts | 86 ++++---- .../RefundCreditNoteGLEntriesSubscriber.ts | 114 +++++----- .../RefundSyncCreditNoteBalanceSubscriber.ts | 116 +++++----- .../CreditNotes/types/CreditNotes.types.ts | 175 +++------------ .../src/modules/CreditNotes/utils.ts | 1 + .../CreditNoteApplySyncCredit.service.ts | 2 +- .../CreditNoteApplySyncInvoices.service.ts | 8 +- .../CreditNoteApplyToInvoices.service.ts | 27 ++- ...DeleteCreditNoteApplyToInvoices.service.ts | 12 +- .../DeleteCustomerLinkedCreditNote.service.ts | 7 +- .../models/CreditNoteAppliedInvoice.ts | 2 +- .../CreditNoteAppliedInvoiceTransformer.ts | 30 +++ ...reditNoteWithInvoicesToApplyTransformer.ts | 0 ...itNoteAssociatedAppliedInvoices.service.ts | 2 +- ...itNoteAssociatedInvoicesToApply.service.ts | 9 +- .../CreditNoteApplySyncCreditSubscriber.ts | 67 ++++++ .../CreditNoteApplySyncInvoicesSubscriber.ts | 61 +++++ .../types/CreditNoteApplyInvoice.types.ts | 35 +++ .../commands/ActivateCustomer.service.ts | 4 +- .../src/modules/Items/ItemsEntries.service.ts | 2 +- .../SaleEstimates/models/SaleEstimate.ts | 126 +++++------ .../commands/DeleteSaleInvoice.service.ts | 2 +- .../Tenancy/TenancyModels/Tenancy.module.ts | 12 +- .../VendorCredit/VendorCredits.module.ts | 38 +++- .../VendorCreditsApplication.service.ts | 8 +- .../commands/CreateVendorCredit.service.ts | 13 +- .../commands/DeleteVendorCredit.service.ts | 12 +- .../commands/EditVendorCredit.service.ts | 15 +- .../commands/OpenVendorCredit.service.ts | 2 +- .../VendorCreditDTOTransform.service.ts | 14 +- .../VendorCredit/models/VendorCredit.ts | 100 ++++++++- .../queries/GetVendorCredit.service.ts | 22 +- ...Credits.ts => GetVendorCredits.service.ts} | 0 .../queries/VendorCreditTransformer.ts | 45 ++-- .../DeleteVendorAssociatedVendorCredit.ts | 86 ++++---- ...RefundSyncVendorCreditBalanceSubscriber.ts | 116 +++++----- .../RefundVendorCreditGLEntriesSubscriber.ts | 110 ++++----- .../VendorCreditAutoSerialSubscriber.ts | 48 ++-- .../VendorCreditGLEntriesSubscriber.ts | 202 ++++++++--------- ...orCreditInventoryTransactionsSusbcriber.ts | 172 +++++++-------- .../VendorCredit/types/VendorCredit.types.ts | 144 ++---------- .../VendorCreditApplyBills.controller.ts | 48 ++++ .../VendorCreditApplyBills.module.ts | 38 +++- ...ndorCreditApplyBillsApplication.service.ts | 67 ++++++ .../ApplyVendorCreditSyncInvoiced.service.ts | 3 + .../ApplyVendorCreditToBills.service.ts | 8 +- .../DeleteApplyVendorCreditToBill.service.ts | 2 + .../GetAppliedBillsToVendorCredit.service.ts | 2 + .../ApplyVendorCreditSyncBillsSubscriber.ts | 114 +++++----- ...ApplyVendorCreditSyncInvoicedSubscriber.ts | 128 +++++------ .../VendorCreditsRefund.application.ts | 44 ++++ .../VendorCreditsRefund.controller.ts | 44 ++++ .../VendorCreditsRefund.module.ts | 20 ++ .../CreateRefundVendorCredit.service.ts | 75 +++++-- .../DeleteRefundVendorCredit.service.ts | 25 ++- .../RefundSyncVendorCreditBalance.service.ts | 12 +- .../commands/RefundVendorCredit.service.ts | 4 +- .../models/RefundVendorCredit.ts | 2 +- .../queries/GetRefundVendorCredit.service.ts | 12 +- .../queries/GetRefundVendorCredits.service.ts | 8 +- .../types/VendorCreditRefund.types.ts | 44 ++++ 93 files changed, 2249 insertions(+), 1634 deletions(-) create mode 100644 packages/server-nest/src/common/types/Discount.ts create mode 100644 packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.controller.ts create mode 100644 packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.module.ts create mode 100644 packages/server-nest/src/modules/CreditNoteRefunds/CreditNotesRefundsApplication.service.ts rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/commands/CreateRefundCreditNote.service.ts (88%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/commands/DeleteRefundCreditNote.service.ts (82%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/commands/RefundCreditNote.service.ts (96%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/commands/RefundCreditNoteGLEntries.ts (100%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/commands/RefundSyncCreditNoteBalance.ts (91%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/models/RefundCreditNote.ts (100%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/queries/GetCreditNoteRefunds.service.ts (86%) rename packages/server-nest/src/modules/{CreditNotes => CreditNoteRefunds}/queries/GetRefundCreditNoteTransaction.service.ts (84%) create mode 100644 packages/server-nest/src/modules/CreditNoteRefunds/types/CreditNoteRefunds.types.ts delete mode 100644 packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply copy.ts delete mode 100644 packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply.ts delete mode 100644 packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncCreditSubscriber.ts delete mode 100644 packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/commands/CreditNoteApplySyncCredit.service.ts (94%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/commands/CreditNoteApplySyncInvoices.service.ts (84%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/commands/CreditNoteApplyToInvoices.service.ts (85%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/commands/DeleteCreditNoteApplyToInvoices.service.ts (82%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/commands/DeleteCustomerLinkedCreditNote.service.ts (78%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/models/CreditNoteAppliedInvoice.ts (95%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/queries/CreditNoteAppliedInvoiceTransformer.ts (69%) rename packages/server-nest/src/modules/{CreditNotes/commands => CreditNotesApplyInvoice/queries}/CreditNoteWithInvoicesToApplyTransformer.ts (100%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts (95%) rename packages/server-nest/src/modules/{CreditNotes => CreditNotesApplyInvoice}/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts (76%) create mode 100644 packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncCreditSubscriber.ts create mode 100644 packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts create mode 100644 packages/server-nest/src/modules/CreditNotesApplyInvoice/types/CreditNoteApplyInvoice.types.ts rename packages/server-nest/src/modules/VendorCredit/queries/{ListVendorCredits.ts => GetVendorCredits.service.ts} (100%) create mode 100644 packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.controller.ts create mode 100644 packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBillsApplication.service.ts create mode 100644 packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.application.ts create mode 100644 packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.controller.ts create mode 100644 packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.module.ts rename packages/server-nest/src/modules/{VendorCredit => VendorCreditsRefund}/models/RefundVendorCredit.ts (95%) create mode 100644 packages/server-nest/src/modules/VendorCreditsRefund/types/VendorCreditRefund.types.ts diff --git a/packages/server-nest/package.json b/packages/server-nest/package.json index f62650e03..438b0cc01 100644 --- a/packages/server-nest/package.json +++ b/packages/server-nest/package.json @@ -38,6 +38,7 @@ "@types/passport-local": "^1.0.38", "@types/ramda": "^0.30.2", "accounting": "^0.4.1", + "bluebird": "^3.7.2", "async": "^3.2.0", "axios": "^1.6.0", "form-data": "^4.0.0", diff --git a/packages/server-nest/src/common/types/Discount.ts b/packages/server-nest/src/common/types/Discount.ts new file mode 100644 index 000000000..6b2fd41c3 --- /dev/null +++ b/packages/server-nest/src/common/types/Discount.ts @@ -0,0 +1,4 @@ +export enum DiscountType { + Percentage = 'percentage', + Amount = 'amount', +} \ No newline at end of file diff --git a/packages/server-nest/src/modules/App/App.module.ts b/packages/server-nest/src/modules/App/App.module.ts index a273b6417..c41ed8a33 100644 --- a/packages/server-nest/src/modules/App/App.module.ts +++ b/packages/server-nest/src/modules/App/App.module.ts @@ -46,10 +46,12 @@ import { BillsModule } from '../Bills/Bills.module'; import { SaleInvoicesModule } from '../SaleInvoices/SaleInvoices.module'; import { SaleReceiptsModule } from '../SaleReceipts/SaleReceipts.module'; import { ManualJournalsModule } from '../ManualJournals/ManualJournals.module'; -import { VendorCreditsModule } from '../VendorCredit/VendorCredits.module'; import { CreditNotesModule } from '../CreditNotes/CreditNotes.module'; +import { VendorCreditsModule } from '../VendorCredit/VendorCredits.module'; import { VendorCreditApplyBillsModule } from '../VendorCreditsApplyBills/VendorCreditApplyBills.module'; -// import { BillPaymentsModule } from '../BillPayments/BillPayments.module'; +import { VendorCreditsRefundModule } from '../VendorCreditsRefund/VendorCreditsRefund.module'; +import { CreditNoteRefundsModule } from '../CreditNoteRefunds/CreditNoteRefunds.module'; +import { BillPaymentsModule } from '../BillPayments/BillPayments.module'; @Module({ imports: [ @@ -120,10 +122,12 @@ import { VendorCreditApplyBillsModule } from '../VendorCreditsApplyBills/VendorC SaleReceiptsModule, BillsModule, ManualJournalsModule, + CreditNotesModule, VendorCreditsModule, VendorCreditApplyBillsModule, - CreditNotesModule, - // BillPaymentsModule, + VendorCreditsRefundModule, + CreditNoteRefundsModule, + BillPaymentsModule, ], controllers: [AppController], providers: [ diff --git a/packages/server-nest/src/modules/BillPayments/BillPayments.module.ts b/packages/server-nest/src/modules/BillPayments/BillPayments.module.ts index dfd502e7b..45fd0800e 100644 --- a/packages/server-nest/src/modules/BillPayments/BillPayments.module.ts +++ b/packages/server-nest/src/modules/BillPayments/BillPayments.module.ts @@ -25,8 +25,9 @@ import { BranchesSettingsService } from '../Branches/BranchesSettings'; CommandBillPaymentDTOTransformer, BranchTransactionDTOTransformer, BranchesSettingsService, - TenancyContext + TenancyContext, ], + exports: [BillPaymentValidators], controllers: [], }) export class BillPaymentsModule {} diff --git a/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.controller.ts b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.controller.ts new file mode 100644 index 000000000..f677475e3 --- /dev/null +++ b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.controller.ts @@ -0,0 +1,42 @@ +import { Body, Controller, Delete, Param, Post } from '@nestjs/common'; +import { ICreditNoteRefundDTO } from '../CreditNotes/types/CreditNotes.types'; +import { CreditNotesRefundsApplication } from './CreditNotesRefundsApplication.service'; +import { RefundCreditNote } from './models/RefundCreditNote'; + +@Controller('credit-notes') +export class CreditNoteRefundsController { + constructor( + private readonly creditNotesRefundsApplication: CreditNotesRefundsApplication, + ) {} + + /** + * Create a refund credit note. + * @param {number} creditNoteId - The credit note ID. + * @param {ICreditNoteRefundDTO} creditNoteDTO - The credit note DTO. + * @returns {Promise} + */ + @Post(':creditNoteId/refunds') + createRefundCreditNote( + @Param('creditNoteId') creditNoteId: number, + @Body() creditNoteDTO: ICreditNoteRefundDTO, + ): Promise { + return this.creditNotesRefundsApplication.createRefundCreditNote( + creditNoteId, + creditNoteDTO, + ); + } + + /** + * Delete a refund credit note. + * @param {number} refundCreditId - The refund credit ID. + * @returns {Promise} + */ + @Delete('refunds/:refundCreditId') + deleteRefundCreditNote( + @Param('refundCreditId') refundCreditId: number, + ): Promise { + return this.creditNotesRefundsApplication.deleteRefundCreditNote( + refundCreditId, + ); + } +} diff --git a/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.module.ts b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.module.ts new file mode 100644 index 000000000..eb96ec5dd --- /dev/null +++ b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNoteRefunds.module.ts @@ -0,0 +1,21 @@ +import { Module } from '@nestjs/common'; +import { CreateRefundCreditNoteService } from './commands/CreateRefundCreditNote.service'; +import { DeleteRefundCreditNoteService } from './commands/DeleteRefundCreditNote.service'; +import { RefundCreditNoteService } from './commands/RefundCreditNote.service'; +import { RefundSyncCreditNoteBalanceService } from './commands/RefundSyncCreditNoteBalance'; +import { CreditNotesRefundsApplication } from './CreditNotesRefundsApplication.service'; +import { CreditNoteRefundsController } from './CreditNoteRefunds.controller'; +import { CreditNotesModule } from '../CreditNotes/CreditNotes.module'; + +@Module({ + imports: [CreditNotesModule], + providers: [ + CreateRefundCreditNoteService, + DeleteRefundCreditNoteService, + RefundCreditNoteService, + RefundSyncCreditNoteBalanceService, + CreditNotesRefundsApplication, + ], + controllers: [CreditNoteRefundsController], +}) +export class CreditNoteRefundsModule {} diff --git a/packages/server-nest/src/modules/CreditNoteRefunds/CreditNotesRefundsApplication.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNotesRefundsApplication.service.ts new file mode 100644 index 000000000..04db315dc --- /dev/null +++ b/packages/server-nest/src/modules/CreditNoteRefunds/CreditNotesRefundsApplication.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@nestjs/common'; +import { ICreditNoteRefundDTO } from '../CreditNotes/types/CreditNotes.types'; +import { CreateRefundCreditNoteService } from './commands/CreateRefundCreditNote.service'; +import { DeleteRefundCreditNoteService } from './commands/DeleteRefundCreditNote.service'; +import { RefundCreditNoteService } from './commands/RefundCreditNote.service'; +import { RefundSyncCreditNoteBalanceService } from './commands/RefundSyncCreditNoteBalance'; + +@Injectable() +export class CreditNotesRefundsApplication { + constructor( + private readonly createRefundCreditNoteService: CreateRefundCreditNoteService, + private readonly deleteRefundCreditNoteService: DeleteRefundCreditNoteService, + private readonly refundCreditNoteService: RefundCreditNoteService, + private readonly refundSyncCreditNoteBalanceService: RefundSyncCreditNoteBalanceService, + ) {} + + /** + * Create a refund credit note. + * @param {number} creditNoteId - The credit note ID. + * @param {ICreditNoteRefundDTO} creditNoteDTO - The credit note DTO. + * @returns {Promise} + */ + public createRefundCreditNote( + creditNoteId: number, + creditNoteDTO: ICreditNoteRefundDTO, + ) { + return this.createRefundCreditNoteService.createCreditNoteRefund( + creditNoteId, + creditNoteDTO, + ); + } + + /** + * Delete a refund credit note. + * @param {number} refundCreditId - The refund credit ID. + * @returns {Promise} + */ + public deleteRefundCreditNote(refundCreditId: number) { + return this.deleteRefundCreditNoteService.deleteCreditNoteRefund( + refundCreditId, + ); + } +} diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreateRefundCreditNote.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/commands/CreateRefundCreditNote.service.ts similarity index 88% rename from packages/server-nest/src/modules/CreditNotes/commands/CreateRefundCreditNote.service.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/commands/CreateRefundCreditNote.service.ts index 4533bc091..a04eab248 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CreateRefundCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/commands/CreateRefundCreditNote.service.ts @@ -4,14 +4,14 @@ import { ICreditNoteRefundDTO, IRefundCreditNoteCreatedPayload, IRefundCreditNoteCreatingPayload, -} from '../types/CreditNotes.types'; +} from '../types/CreditNoteRefunds.types'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Account } from '@/modules/Accounts/models/Account.model'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; -import { RefundCreditNote } from '../models/RefundCreditNote'; +import { RefundCreditNote } from '@/modules/CreditNoteRefunds/models/RefundCreditNote'; +import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service'; +import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; import { events } from '@/common/events/events'; -import { CommandCreditNoteDTOTransform } from './CommandCreditNoteDTOTransform.service'; -import { CreditNote } from '../models/CreditNote'; @Injectable() export class CreateRefundCreditNoteService { @@ -63,9 +63,9 @@ export class CreateRefundCreditNoteService { newCreditNoteDTO.amount, ); // Validate the refund withdrawal account type. - this.commandCreditNoteDTOTransform.validateRefundWithdrawwalAccountType( - fromAccount, - ); + // this.commandCreditNoteDTOTransform.validateRefundWithdrawwalAccountType( + // fromAccount, + // ); // Creates a refund credit note transaction. return this.uow.withTransaction(async (trx: Knex.Transaction) => { // Triggers `onCreditNoteRefundCreating` event. @@ -102,7 +102,7 @@ export class CreateRefundCreditNoteService { private transformDTOToModel = ( creditNote: CreditNote, creditNoteDTO: ICreditNoteRefundDTO, - ): RefundCreditNote => { + ): Partial => { return { creditNoteId: creditNote.id, currencyCode: creditNote.currencyCode, diff --git a/packages/server-nest/src/modules/CreditNotes/commands/DeleteRefundCreditNote.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/commands/DeleteRefundCreditNote.service.ts similarity index 82% rename from packages/server-nest/src/modules/CreditNotes/commands/DeleteRefundCreditNote.service.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/commands/DeleteRefundCreditNote.service.ts index 2e9c684bb..57cd146d0 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/DeleteRefundCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/commands/DeleteRefundCreditNote.service.ts @@ -4,18 +4,17 @@ import { Knex } from 'knex'; import { IRefundCreditNoteDeletedPayload, IRefundCreditNoteDeletingPayload, - IRefundVendorCreditDeletedPayload, -} from '../types/CreditNotes.types'; +} from '../types/CreditNoteRefunds.types'; import { RefundCreditNote } from '../models/RefundCreditNote'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { events } from '@/common/events/events'; @Injectable() -export default class DeleteRefundCreditNote { +export class DeleteRefundCreditNoteService { /** - * @param {UnitOfWork} uow - * @param {EventEmitter2} eventPublisher - * @param {typeof RefundCreditNoteModel} refundCreditNoteModel + * @param {UnitOfWork} uow - Unit of work. + * @param {EventEmitter2} eventPublisher - Event emitter. + * @param {typeof RefundCreditNote} refundCreditNoteModel - Refund credit note model. */ constructor( private readonly uow: UnitOfWork, @@ -27,8 +26,8 @@ export default class DeleteRefundCreditNote { /** * Retrieve the credit note graph. - * @param {number} refundCreditId - * @returns + * @param {number} refundCreditId - Refund credit note ID. + * @returns {Promise} */ public deleteCreditNoteRefund = async (refundCreditId: number) => { // Retrieve the old credit note or throw not found service error. @@ -56,7 +55,6 @@ export default class DeleteRefundCreditNote { events.creditNote.onRefundDeleting, eventPayload, ); - // Deletes the refund credit note graph from the storage. await this.refundCreditNoteModel .query(trx) @@ -66,7 +64,7 @@ export default class DeleteRefundCreditNote { // Triggers `onCreditNoteRefundDeleted` event. await this.eventPublisher.emitAsync( events.creditNote.onRefundDeleted, - eventPayload as IRefundVendorCreditDeletedPayload, + eventPayload as IRefundCreditNoteDeletedPayload, ); }); }; diff --git a/packages/server-nest/src/modules/CreditNotes/commands/RefundCreditNote.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundCreditNote.service.ts similarity index 96% rename from packages/server-nest/src/modules/CreditNotes/commands/RefundCreditNote.service.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundCreditNote.service.ts index 0661efd0b..013903895 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/RefundCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundCreditNote.service.ts @@ -1,5 +1,5 @@ import { Inject, Injectable } from '@nestjs/common'; -import { ERRORS } from '../constants'; +import { ERRORS } from '../../CreditNotes/constants'; import { RefundCreditNote } from '../models/RefundCreditNote'; import { ServiceError } from '@/modules/Items/ServiceError'; import { Account } from '@/modules/Accounts/models/Account.model'; diff --git a/packages/server-nest/src/modules/CreditNotes/commands/RefundCreditNoteGLEntries.ts b/packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundCreditNoteGLEntries.ts similarity index 100% rename from packages/server-nest/src/modules/CreditNotes/commands/RefundCreditNoteGLEntries.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundCreditNoteGLEntries.ts diff --git a/packages/server-nest/src/modules/CreditNotes/commands/RefundSyncCreditNoteBalance.ts b/packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundSyncCreditNoteBalance.ts similarity index 91% rename from packages/server-nest/src/modules/CreditNotes/commands/RefundSyncCreditNoteBalance.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundSyncCreditNoteBalance.ts index 1a1a5433d..0af481a3d 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/RefundSyncCreditNoteBalance.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/commands/RefundSyncCreditNoteBalance.ts @@ -1,9 +1,9 @@ import { Knex } from 'knex'; import { Inject, Injectable } from '@nestjs/common'; -import { CreditNote } from '../models/CreditNote'; +import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; @Injectable() -export class RefundSyncCreditNoteBalance { +export class RefundSyncCreditNoteBalanceService { /** * @param {typeof CreditNote} creditNoteModel - The credit note model. */ diff --git a/packages/server-nest/src/modules/CreditNotes/models/RefundCreditNote.ts b/packages/server-nest/src/modules/CreditNoteRefunds/models/RefundCreditNote.ts similarity index 100% rename from packages/server-nest/src/modules/CreditNotes/models/RefundCreditNote.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/models/RefundCreditNote.ts diff --git a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteRefunds.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/queries/GetCreditNoteRefunds.service.ts similarity index 86% rename from packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteRefunds.service.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/queries/GetCreditNoteRefunds.service.ts index 36e585014..e8b22704e 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteRefunds.service.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/queries/GetCreditNoteRefunds.service.ts @@ -1,8 +1,8 @@ import { Inject, Injectable } from '@nestjs/common'; -import RefundCreditNoteTransformer from './RefundCreditNoteTransformer'; import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service'; import { RefundCreditNote } from '../models/RefundCreditNote'; -import { IRefundCreditNotePOJO } from '../types/CreditNotes.types'; +import { RefundCreditNoteTransformer } from '@/modules/CreditNotes/queries/RefundCreditNoteTransformer'; +import { IRefundCreditNotePOJO } from '../types/CreditNoteRefunds.types'; @Injectable() export class ListCreditNoteRefunds { diff --git a/packages/server-nest/src/modules/CreditNotes/queries/GetRefundCreditNoteTransaction.service.ts b/packages/server-nest/src/modules/CreditNoteRefunds/queries/GetRefundCreditNoteTransaction.service.ts similarity index 84% rename from packages/server-nest/src/modules/CreditNotes/queries/GetRefundCreditNoteTransaction.service.ts rename to packages/server-nest/src/modules/CreditNoteRefunds/queries/GetRefundCreditNoteTransaction.service.ts index de9b320b0..aaf2201a6 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/GetRefundCreditNoteTransaction.service.ts +++ b/packages/server-nest/src/modules/CreditNoteRefunds/queries/GetRefundCreditNoteTransaction.service.ts @@ -1,7 +1,6 @@ import { Inject, Injectable } from '@nestjs/common'; -import { IRefundCreditNote } from '../types/CreditNotes.types'; import { RefundCreditNote } from '../models/RefundCreditNote'; -import { RefundCreditNoteTransformer } from './RefundCreditNoteTransformer'; +import { RefundCreditNoteTransformer } from '../../CreditNotes/queries/RefundCreditNoteTransformer'; @Injectable() export class GetRefundCreditNoteTransaction { @@ -24,7 +23,7 @@ export class GetRefundCreditNoteTransaction { */ public async getRefundCreditTransaction( refundCreditId: number - ): Promise { + ): Promise { const refundCreditNote = await this.refundCreditNoteModel .query() .findById(refundCreditId) diff --git a/packages/server-nest/src/modules/CreditNoteRefunds/types/CreditNoteRefunds.types.ts b/packages/server-nest/src/modules/CreditNoteRefunds/types/CreditNoteRefunds.types.ts new file mode 100644 index 000000000..7b5df296a --- /dev/null +++ b/packages/server-nest/src/modules/CreditNoteRefunds/types/CreditNoteRefunds.types.ts @@ -0,0 +1,47 @@ +import { Knex } from 'knex'; +import { RefundCreditNote } from '../models/RefundCreditNote'; +import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; + +export interface ICreditNoteRefundDTO { + fromAccountId: number; + amount: number; + exchangeRate?: number; + referenceNo: string; + description: string; + date: Date; + branchId?: number; +} + +export interface IRefundCreditNotePOJO { + formattedAmount: string; +} + +export interface IRefundCreditNoteDeletedPayload { + trx: Knex.Transaction; + refundCreditId: number; + oldRefundCredit: RefundCreditNote; +} + +export interface IRefundCreditNoteDeletingPayload { + trx: Knex.Transaction; + refundCreditId: number; + oldRefundCredit: RefundCreditNote; +} + +export interface IRefundCreditNoteCreatingPayload { + trx: Knex.Transaction; + creditNote: CreditNote; + newCreditNoteDTO: ICreditNoteRefundDTO; +} + +export interface IRefundCreditNoteCreatedPayload { + trx: Knex.Transaction; + refundCreditNote: RefundCreditNote; + creditNote: CreditNote; +} + +export interface IRefundCreditNoteOpenedPayload { + creditNoteId: number; + oldCreditNote: CreditNote; + trx: Knex.Transaction; +} diff --git a/packages/server-nest/src/modules/CreditNotes/CreditNoteApplication.service.ts b/packages/server-nest/src/modules/CreditNotes/CreditNoteApplication.service.ts index 2256599c5..b0d4e5731 100644 --- a/packages/server-nest/src/modules/CreditNotes/CreditNoteApplication.service.ts +++ b/packages/server-nest/src/modules/CreditNotes/CreditNoteApplication.service.ts @@ -1,6 +1,5 @@ import { Injectable } from '@nestjs/common'; import { CreateCreditNoteService } from './commands/CreateCreditNote.service'; -import { CreateRefundCreditNoteService } from './commands/CreateRefundCreditNote.service'; import { DeleteCreditNoteService } from './commands/DeleteCreditNote.service'; import { EditCreditNoteService } from './commands/EditCreditNote.service'; import { OpenCreditNoteService } from './commands/OpenCreditNote.service'; @@ -8,24 +7,33 @@ import { GetCreditNotePdf } from './queries/GetCreditNotePdf.serivce'; import { ICreditNoteEditDTO, ICreditNoteNewDTO, - ICreditNoteRefundDTO, } from './types/CreditNotes.types'; @Injectable() export class CreditNoteApplication { constructor( private createCreditNoteService: CreateCreditNoteService, - private createRefundCreditNoteService: CreateRefundCreditNoteService, private editCreditNoteService: EditCreditNoteService, private openCreditNoteService: OpenCreditNoteService, private deleteCreditNoteService: DeleteCreditNoteService, private getCreditNotePdfService: GetCreditNotePdf, ) {} + /** + * Creates a new credit note. + * @param {ICreditNoteNewDTO} creditNoteDTO + * @returns {Promise} + */ createCreditNote(creditNoteDTO: ICreditNoteNewDTO) { return this.createCreditNoteService.creditCreditNote(creditNoteDTO); } + /** + * Edits a credit note. + * @param {number} creditNoteId + * @param {ICreditNoteEditDTO} creditNoteDTO + * @returns {Promise} + */ editCreditNote(creditNoteId: number, creditNoteDTO: ICreditNoteEditDTO) { return this.editCreditNoteService.editCreditNote( creditNoteId, @@ -33,24 +41,29 @@ export class CreditNoteApplication { ); } + /** + * Opens a credit note. + * @param {number} creditNoteId + * @returns {Promise} + */ openCreditNote(creditNoteId: number) { return this.openCreditNoteService.openCreditNote(creditNoteId); } + /** + * Deletes a credit note. + * @param {number} creditNoteId + * @returns {Promise} + */ deleteCreditNote(creditNoteId: number) { return this.deleteCreditNoteService.deleteCreditNote(creditNoteId); } - createRefundCreditNote( - creditNoteId: number, - creditNoteDTO: ICreditNoteRefundDTO, - ) { - return this.createRefundCreditNoteService.createCreditNoteRefund( - creditNoteId, - creditNoteDTO, - ); - } - + /** + * Retrieves the PDF for a credit note. + * @param {number} creditNoteId + * @returns {Promise} + */ getCreditNotePdf(creditNoteId: number) { return this.getCreditNotePdfService.getCreditNotePdf(creditNoteId); } diff --git a/packages/server-nest/src/modules/CreditNotes/CreditNotes.controller.ts b/packages/server-nest/src/modules/CreditNotes/CreditNotes.controller.ts index 4d7f75a4f..dbbba8190 100644 --- a/packages/server-nest/src/modules/CreditNotes/CreditNotes.controller.ts +++ b/packages/server-nest/src/modules/CreditNotes/CreditNotes.controller.ts @@ -3,7 +3,6 @@ import { CreditNoteApplication } from './CreditNoteApplication.service'; import { ICreditNoteEditDTO, ICreditNoteNewDTO, - ICreditNoteRefundDTO, } from './types/CreditNotes.types'; @Controller('credit-notes') @@ -38,15 +37,4 @@ export class CreditNotesController { deleteCreditNote(@Param('id') creditNoteId: number) { return this.creditNoteApplication.deleteCreditNote(creditNoteId); } - - @Post(':id/refund') - createRefundCreditNote( - @Param('id') creditNoteId: number, - @Body() creditNoteDTO: ICreditNoteRefundDTO, - ) { - return this.creditNoteApplication.createRefundCreditNote( - creditNoteId, - creditNoteDTO, - ); - } } diff --git a/packages/server-nest/src/modules/CreditNotes/CreditNotes.module.ts b/packages/server-nest/src/modules/CreditNotes/CreditNotes.module.ts index a83408403..5b1850e88 100644 --- a/packages/server-nest/src/modules/CreditNotes/CreditNotes.module.ts +++ b/packages/server-nest/src/modules/CreditNotes/CreditNotes.module.ts @@ -1,28 +1,59 @@ import { Module } from '@nestjs/common'; import { CreateCreditNoteService } from './commands/CreateCreditNote.service'; import { CommandCreditNoteDTOTransform } from './commands/CommandCreditNoteDTOTransform.service'; -import { CreateRefundCreditNoteService } from './commands/CreateRefundCreditNote.service'; import { EditCreditNoteService } from './commands/EditCreditNote.service'; import { OpenCreditNoteService } from './commands/OpenCreditNote.service'; import { DeleteCreditNoteService } from './commands/DeleteCreditNote.service'; -import { CreditNoteApplySyncCredit } from './commands/CreditNoteApplySyncCredit.service'; -import { DeleteCustomerLinkedCreditNoteService } from './commands/DeleteCustomerLinkedCreditNote.service'; import { CreditNoteAutoIncrementService } from './commands/CreditNoteAutoIncrement.service'; import { CreditNoteApplication } from './CreditNoteApplication.service'; import { CreditNotesController } from './CreditNotes.controller'; +import { GetCreditNoteState } from './queries/GetCreditNoteState.service'; +import { GetCreditNotePdf } from './queries/GetCreditNotePdf.serivce'; +import { ItemsModule } from '../Items/items.module'; +import { BranchesModule } from '../Branches/Branches.module'; +import { WarehousesModule } from '../Warehouses/Warehouses.module'; +import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module'; +import { ChromiumlyTenancyModule } from '../ChromiumlyTenancy/ChromiumlyTenancy.module'; +import { TemplateInjectableModule } from '../TemplateInjectable/TemplateInjectable.module'; +import { GetCreditNote } from './queries/GetCreditNote.service'; +import { CreditNoteBrandingTemplate } from './queries/CreditNoteBrandingTemplate.service'; +import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module'; @Module({ + imports: [ + ItemsModule, + BranchesModule, + WarehousesModule, + PdfTemplatesModule, + ChromiumlyTenancyModule, + TemplateInjectableModule, + AutoIncrementOrdersModule + ], providers: [ CreateCreditNoteService, + GetCreditNote, CommandCreditNoteDTOTransform, - CreateRefundCreditNoteService, EditCreditNoteService, OpenCreditNoteService, DeleteCreditNoteService, - CreditNoteApplySyncCredit, - DeleteCustomerLinkedCreditNoteService, + GetCreditNotePdf, CreditNoteAutoIncrementService, - CreditNoteApplication + GetCreditNoteState, + CreditNoteApplication, + CreditNoteBrandingTemplate + ], + exports: [ + CreateCreditNoteService, + GetCreditNote, + CommandCreditNoteDTOTransform, + EditCreditNoteService, + OpenCreditNoteService, + DeleteCreditNoteService, + GetCreditNotePdf, + CreditNoteAutoIncrementService, + GetCreditNoteState, + CreditNoteApplication, + CreditNoteBrandingTemplate ], controllers: [CreditNotesController], }) diff --git a/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply copy.ts b/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply copy.ts deleted file mode 100644 index ef44818d8..000000000 --- a/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply copy.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Service, Inject } from 'typedi'; -import BaseCreditNotes from '../CreditNotes'; -import { ISaleInvoice } from '@/interfaces'; -import { CreditNoteWithInvoicesToApplyTransformer } from './commands/CreditNoteWithInvoicesToApplyTransformer'; -import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable'; - -@Service() -export default class GetCreditNoteAssociatedInvoicesToApply extends BaseCreditNotes { - @Inject() - private transformer: TransformerInjectable; - - /** - * Retrieve credit note associated invoices to apply. - * @param {number} tenantId - * @param {number} creditNoteId - * @returns {Promise} - */ - public getCreditAssociatedInvoicesToApply = async ( - tenantId: number, - creditNoteId: number - ): Promise => { - const { SaleInvoice } = this.tenancy.models(tenantId); - - // Retireve credit note or throw not found service error. - const creditNote = await this.getCreditNoteOrThrowError( - tenantId, - creditNoteId - ); - // Retrieves the published due invoices that associated to the given customer. - const saleInvoices = await SaleInvoice.query() - .where('customerId', creditNote.customerId) - .modify('dueInvoices') - .modify('published'); - - // Transformes the sale invoices models to POJO. - return this.transformer.transform( - tenantId, - saleInvoices, - new CreditNoteWithInvoicesToApplyTransformer() - ); - }; -} diff --git a/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply.ts b/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply.ts deleted file mode 100644 index 0519ecba6..000000000 --- a/packages/server-nest/src/modules/CreditNotes/GetCreditNoteAssociatedInvoicesToApply.ts +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service.ts b/packages/server-nest/src/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service.ts index c900e693d..270061a5c 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service.ts +++ b/packages/server-nest/src/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service.ts @@ -5,7 +5,6 @@ import * as R from 'ramda'; import composeAsync from 'async/compose'; import { ERRORS } from '../constants'; import { - ICreditNote, ICreditNoteEditDTO, ICreditNoteEntryNewDTO, ICreditNoteNewDTO, @@ -92,9 +91,9 @@ export class CommandCreditNoteDTOTransform { )(initialDTO); return R.compose( - this.branchDTOTransform.transformDTO, - this.warehouseDTOTransform.transformDTO, - )(initialAsyncDTO); + this.branchDTOTransform.transformDTO, + this.warehouseDTOTransform.transformDTO, + )(initialAsyncDTO) as CreditNote; }; /** diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreateCreditNote.service.ts b/packages/server-nest/src/modules/CreditNotes/commands/CreateCreditNote.service.ts index c83bd1f58..4d4fd7205 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CreateCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNotes/commands/CreateCreditNote.service.ts @@ -1,5 +1,5 @@ import { Knex } from 'knex'; -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { ICreditNoteCreatedPayload, ICreditNoteCreatingPayload, @@ -15,13 +15,25 @@ import { events } from '@/common/events/events'; @Injectable() export class CreateCreditNoteService { + /** + * @param {UnitOfWork} uow - Unit of work. + * @param {ItemsEntriesService} itemsEntriesService - Items entries service. + * @param {EventEmitter2} eventPublisher - Event emitter. + * @param {typeof CreditNote} creditNoteModel - Credit note model. + * @param {typeof Contact} contactModel - Contact model. + * @param {CommandCreditNoteDTOTransform} commandCreditNoteDTOTransform - Command credit note DTO transform service. + */ constructor( private readonly uow: UnitOfWork, private readonly itemsEntriesService: ItemsEntriesService, private readonly eventPublisher: EventEmitter2, - private readonly creditNoteModel: typeof CreditNote, - private readonly contactModel: typeof Contact, private readonly commandCreditNoteDTOTransform: CommandCreditNoteDTOTransform, + + @Inject(CreditNote.name) + private readonly creditNoteModel: typeof CreditNote, + + @Inject(Contact.name) + private readonly contactModel: typeof Contact, ) {} /** diff --git a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNote.service.ts b/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNote.service.ts index 6c74a6a27..993902590 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNote.service.ts @@ -6,10 +6,10 @@ import { } from '../types/CreditNotes.types'; import { ERRORS } from '../constants'; import { CreditNote } from '../models/CreditNote'; -import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; +import { CreditNoteAppliedInvoice } from '../../CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice'; import { RefundCreditNote as RefundCreditNoteModel, -} from '../models/RefundCreditNote'; +} from '../../CreditNoteRefunds/models/RefundCreditNote'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { ItemEntry } from '@/modules/Items/models/ItemEntry'; diff --git a/packages/server-nest/src/modules/CreditNotes/commands/OpenCreditNote.service.ts b/packages/server-nest/src/modules/CreditNotes/commands/OpenCreditNote.service.ts index ed69d4d8a..3b5345c20 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/OpenCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNotes/commands/OpenCreditNote.service.ts @@ -49,7 +49,6 @@ export class OpenCreditNoteService { // Sales the credit note transactions with associated entries. return this.uow.withTransaction(async (trx: Knex.Transaction) => { const eventPayload = { - creditNoteId, oldCreditNote, trx, } as ICreditNoteOpeningPayload; @@ -62,8 +61,7 @@ export class OpenCreditNoteService { // Saves the credit note graph to the storage. const creditNote = await this.creditNoteModel .query(trx) - .findById(creditNoteId) - .update({ + .updateAndFetchById(creditNoteId, { openedAt: new Date(), }); // Triggers `onCreditNoteOpened` event. @@ -77,10 +75,10 @@ export class OpenCreditNoteService { }; /** - * - * @param creditNote + * Throws an error if the given credit note is already open. + * @param {CreditNote} creditNote - */ - public throwErrorIfAlreadyOpen = (creditNote) => { + public throwErrorIfAlreadyOpen = (creditNote: CreditNote) => { if (creditNote.openedAt) { throw new ServiceError(ERRORS.CREDIT_NOTE_ALREADY_OPENED); } diff --git a/packages/server-nest/src/modules/CreditNotes/models/CreditNote.ts b/packages/server-nest/src/modules/CreditNotes/models/CreditNote.ts index 29bc5f97e..2a04e8605 100644 --- a/packages/server-nest/src/modules/CreditNotes/models/CreditNote.ts +++ b/packages/server-nest/src/modules/CreditNotes/models/CreditNote.ts @@ -1,3 +1,9 @@ +import { DiscountType } from '@/common/types/Discount'; +import { BaseModel } from '@/models/Model'; +import { Branch } from '@/modules/Branches/models/Branch.model'; +import { Customer } from '@/modules/Customers/models/Customer'; +import { ItemEntry } from '@/modules/Items/models/ItemEntry'; +import { Warehouse } from '@/modules/Warehouses/models/Warehouse.model'; import { mixin, Model, raw } from 'objection'; // import TenantModel from 'models/TenantModel'; // import ModelSetting from './ModelSetting'; @@ -5,22 +11,32 @@ import { mixin, Model, raw } from 'objection'; // import { DEFAULT_VIEWS } from '@/services/CreditNotes/constants'; // import ModelSearchable from './ModelSearchable'; // import CreditNoteMeta from './CreditNote.Meta'; -import { BaseModel } from '@/models/Model'; +// import { DiscountType } from '@/interfaces'; export class CreditNote extends BaseModel { - customerId: number; - creditNoteDate: Date; - creditNoteNumber: string; - referenceNo: string; - amount: number; - exchangeRate: number; - refundedAmount: number; - invoicesAmount: number; - currencyCode: string; - note: string; - termsConditions: string; - openedAt: Date; - userId: number; + public amount: number; + public exchangeRate: number; + public openedAt: Date; + public discount: number; + public discountType: DiscountType; + public adjustment: number; + public refundedAmount: number; + public invoicesAmount: number; + public creditNoteDate: Date; + public creditNoteNumber: string; + public currencyCode: string; + public customerId: number; + + public branchId: number; + public warehouseId: number; + + public customer!: Customer; + public entries!: ItemEntry[]; + + public branch!: Branch; + public warehouse!: Warehouse; + + /** * Table name @@ -46,8 +62,21 @@ export class CreditNote extends BaseModel { 'isPublished', 'isOpen', 'isClosed', + 'creditsRemaining', 'creditsUsed', + + 'subtotal', + 'subtotalLocal', + + 'discountAmount', + 'discountAmountLocal', + 'discountPercentage', + + 'total', + 'totalLocal', + + 'adjustmentLocal', ]; } @@ -59,6 +88,72 @@ export class CreditNote extends BaseModel { return this.amount * this.exchangeRate; } + /** + * Credit note subtotal. + * @returns {number} + */ + get subtotal() { + return this.amount; + } + + /** + * Credit note subtotal in local currency. + * @returns {number} + */ + get subtotalLocal() { + return this.subtotal * this.exchangeRate; + } + + /** + * Discount amount. + * @returns {number} + */ + get discountAmount() { + return this.discountType === DiscountType.Amount + ? this.discount + : this.subtotal * (this.discount / 100); + } + + /** + * Discount amount in local currency. + * @returns {number} + */ + get discountAmountLocal() { + return this.discountAmount ? this.discountAmount * this.exchangeRate : null; + } + + /** + * Discount percentage. + * @returns {number | null} + */ + get discountPercentage(): number | null { + return this.discountType === DiscountType.Percentage ? this.discount : null; + } + + /** + * Adjustment amount in local currency. + * @returns {number} + */ + get adjustmentLocal() { + return this.adjustment ? this.adjustment * this.exchangeRate : null; + } + + /** + * Credit note total. + * @returns {number} + */ + get total() { + return this.subtotal - this.discountAmount + this.adjustment; + } + + /** + * Credit note total in local currency. + * @returns {number} + */ + get totalLocal() { + return this.total * this.exchangeRate; + } + /** * Detarmines whether the credit note is draft. * @returns {boolean} @@ -98,6 +193,9 @@ export class CreditNote extends BaseModel { return Math.max(this.amount - this.refundedAmount - this.invoicesAmount, 0); } + /** + * Retrieve the credits used. + */ get creditsUsed() { return this.refundedAmount + this.invoicesAmount; } @@ -128,7 +226,7 @@ export class CreditNote extends BaseModel { query .where( raw(`COALESCE(REFUNDED_AMOUNT) + COALESCE(INVOICES_AMOUNT) < - COALESCE(AMOUNT)`) + COALESCE(AMOUNT)`), ) .modify('published'); }, @@ -140,7 +238,7 @@ export class CreditNote extends BaseModel { query .where( raw(`COALESCE(REFUNDED_AMOUNT) + COALESCE(INVOICES_AMOUNT) = - COALESCE(AMOUNT)`) + COALESCE(AMOUNT)`), ) .modify('published'); }, @@ -171,7 +269,7 @@ export class CreditNote extends BaseModel { */ sortByStatus(query, order) { query.orderByRaw( - `COALESCE(REFUNDED_AMOUNT) + COALESCE(INVOICES_AMOUNT) = COALESCE(AMOUNT) ${order}` + `COALESCE(REFUNDED_AMOUNT) + COALESCE(INVOICES_AMOUNT) = COALESCE(AMOUNT) ${order}`, ); }, }; @@ -181,12 +279,15 @@ export class CreditNote extends BaseModel { * Relationship mapping. */ static get relationMappings() { - const { AccountTransaction } = require('../../Accounts/models/AccountTransaction.model'); - const { ItemEntry } = require('../../TransactionItemEntry/models/ItemEntry'); + const { + AccountTransaction, + } = require('../../Accounts/models/AccountTransaction.model'); + const { ItemEntry } = require('../../Items/models/ItemEntry'); const { Customer } = require('../../Customers/models/Customer'); const { Branch } = require('../../Branches/models/Branch.model'); const { Document } = require('../../ChromiumlyTenancy/models/Document'); const { Warehouse } = require('../../Warehouses/models/Warehouse.model'); + const { PdfTemplate } = require('../../PdfTemplate/models/PdfTemplate'); return { /** @@ -277,12 +378,24 @@ export class CreditNote extends BaseModel { query.where('model_ref', 'CreditNote'); }, }, + + /** + * Credit note may belongs to pdf branding template. + */ + pdfTemplate: { + relation: Model.BelongsToOneRelation, + modelClass: PdfTemplate, + join: { + from: 'credit_notes.pdfTemplateId', + to: 'pdf_templates.id', + }, + }, }; } - /** - * Sale invoice meta. - */ + // /** + // * Sale invoice meta. + // */ // static get meta() { // return CreditNoteMeta; // } diff --git a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNotePdf.serivce.ts b/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNotePdf.serivce.ts index c88452024..abdacff49 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNotePdf.serivce.ts +++ b/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNotePdf.serivce.ts @@ -12,6 +12,15 @@ import { events } from '@/common/events/events'; @Injectable() export class GetCreditNotePdf { + /** + * @param {ChromiumlyTenancy} chromiumlyTenancy - Chromiumly tenancy service. + * @param {TemplateInjectable} templateInjectable - Template injectable service. + * @param {GetCreditNote} getCreditNoteService - Get credit note service. + * @param {CreditNoteBrandingTemplate} creditNoteBrandingTemplate - Credit note branding template service. + * @param {EventEmitter2} eventPublisher - Event publisher service. + * @param {typeof CreditNote} creditNoteModel - Credit note model. + * @param {typeof PdfTemplateModel} pdfTemplateModel - Pdf template model. + */ constructor( private readonly chromiumlyTenancy: ChromiumlyTenancy, private readonly templateInjectable: TemplateInjectable, diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncCreditSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncCreditSubscriber.ts deleted file mode 100644 index 39ab18462..000000000 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncCreditSubscriber.ts +++ /dev/null @@ -1,67 +0,0 @@ -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'; - -@Service() -export default class CreditNoteApplySyncCreditSubscriber { - @Inject() - 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 - - */ - private incrementCreditedAmountOnceApplyToInvoicesCreated = async ({ - trx, - creditNote, - tenantId, - creditNoteAppliedInvoices, - }: IApplyCreditToInvoicesCreatedPayload) => { - const totalCredited = sumBy(creditNoteAppliedInvoices, 'amount'); - - 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 - ); - }; -} diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts deleted file mode 100644 index 8d7f9d2b7..000000000 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import { - IApplyCreditToInvoicesCreatedPayload, - IApplyCreditToInvoicesDeletedPayload, -} from '@/interfaces'; -import CreditNoteApplySyncInvoicesCreditedAmount from '../commands/CreditNoteApplySyncInvoices.service'; - -@Service() -export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber { - @Inject() - private 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 - - */ - 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 - ); - }; -} diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteAutoSerialSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteAutoSerialSubscriber.ts index 8880789e9..0984b969b 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteAutoSerialSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteAutoSerialSubscriber.ts @@ -1,30 +1,30 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import BaseCreditNotes from '../commands/CommandCreditNoteDTOTransform.service'; -import { ICreditNoteCreatedPayload } from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import BaseCreditNotes from '../commands/CommandCreditNoteDTOTransform.service'; +// import { ICreditNoteCreatedPayload } from '@/interfaces'; -@Service() -export default class CreditNoteAutoSerialSubscriber { - @Inject() - creditNotesService: BaseCreditNotes; +// @Service() +// export default class CreditNoteAutoSerialSubscriber { +// @Inject() +// creditNotesService: BaseCreditNotes; - /** - * Attaches events with handlers. - */ - public attach(bus) { - bus.subscribe( - events.creditNote.onCreated, - this.autoSerialIncrementOnceCreated - ); - } +// /** +// * 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 - +// */ +// private autoSerialIncrementOnceCreated = async ({ +// tenantId, +// }: ICreditNoteCreatedPayload) => { +// await this.creditNotesService.incrementSerialNumber(tenantId); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteGLEntriesSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteGLEntriesSubscriber.ts index b29ab57db..649e47d82 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteGLEntriesSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteGLEntriesSubscriber.ts @@ -1,113 +1,113 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import { - ICreditNoteCreatedPayload, - ICreditNoteDeletedPayload, - ICreditNoteEditedPayload, - ICreditNoteOpenedPayload, -} from '@/interfaces'; -import CreditNoteGLEntries from '../commands/CreditNoteGLEntries'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import { +// ICreditNoteCreatedPayload, +// ICreditNoteDeletedPayload, +// ICreditNoteEditedPayload, +// ICreditNoteOpenedPayload, +// } from '@/interfaces'; +// import CreditNoteGLEntries from '../commands/CreditNoteGLEntries'; -@Service() -export default class CreditNoteGLEntriesSubscriber { - @Inject() - private creditNoteGLEntries: CreditNoteGLEntries; +// @Service() +// export default class CreditNoteGLEntriesSubscriber { +// @Inject() +// private creditNoteGLEntries: CreditNoteGLEntries; - /** - * Attaches events with handlers. - * @param bus - */ - public attach(bus) { - bus.subscribe( - events.creditNote.onCreated, - this.writeGlEntriesOnceCreditNoteCreated - ); - bus.subscribe( - events.creditNote.onOpened, - this.writeGLEntriesOnceCreditNoteOpened - ); - bus.subscribe( - events.creditNote.onEdited, - this.editVendorCreditGLEntriesOnceEdited - ); - bus.subscribe( - events.creditNote.onDeleted, - this.revertGLEntriesOnceCreditNoteDeleted - ); - } +// /** +// * Attaches events with handlers. +// * @param bus +// */ +// public attach(bus) { +// bus.subscribe( +// events.creditNote.onCreated, +// this.writeGlEntriesOnceCreditNoteCreated +// ); +// bus.subscribe( +// events.creditNote.onOpened, +// this.writeGLEntriesOnceCreditNoteOpened +// ); +// bus.subscribe( +// events.creditNote.onEdited, +// this.editVendorCreditGLEntriesOnceEdited +// ); +// bus.subscribe( +// events.creditNote.onDeleted, +// this.revertGLEntriesOnceCreditNoteDeleted +// ); +// } - /** - * Writes the GL entries once the credit note transaction created or open. - * @private - * @param {ICreditNoteCreatedPayload|ICreditNoteOpenedPayload} payload - - */ - private writeGlEntriesOnceCreditNoteCreated = async ({ - tenantId, - creditNote, - creditNoteId, - trx, - }: ICreditNoteCreatedPayload | ICreditNoteOpenedPayload) => { - // Can't continue if the credit note is not published yet. - if (!creditNote.isPublished) return; +// /** +// * Writes the GL entries once the credit note transaction created or open. +// * @private +// * @param {ICreditNoteCreatedPayload|ICreditNoteOpenedPayload} payload - +// */ +// private writeGlEntriesOnceCreditNoteCreated = async ({ +// tenantId, +// creditNote, +// creditNoteId, +// trx, +// }: ICreditNoteCreatedPayload | ICreditNoteOpenedPayload) => { +// // Can't continue if the credit note is not published yet. +// if (!creditNote.isPublished) return; - await this.creditNoteGLEntries.createVendorCreditGLEntries( - tenantId, - creditNoteId, - trx - ); - }; +// await this.creditNoteGLEntries.createVendorCreditGLEntries( +// tenantId, +// creditNoteId, +// trx +// ); +// }; - /** - * Writes the GL entries once the vendor credit transaction opened. - * @param {ICreditNoteOpenedPayload} payload - */ - private writeGLEntriesOnceCreditNoteOpened = async ({ - tenantId, - creditNoteId, - trx, - }: ICreditNoteOpenedPayload) => { - await this.creditNoteGLEntries.createVendorCreditGLEntries( - tenantId, - creditNoteId, - trx - ); - }; +// /** +// * Writes the GL entries once the vendor credit transaction opened. +// * @param {ICreditNoteOpenedPayload} payload +// */ +// private writeGLEntriesOnceCreditNoteOpened = async ({ +// tenantId, +// creditNoteId, +// trx, +// }: ICreditNoteOpenedPayload) => { +// await this.creditNoteGLEntries.createVendorCreditGLEntries( +// tenantId, +// creditNoteId, +// trx +// ); +// }; - /** - * Reverts GL entries once credit note deleted. - */ - private revertGLEntriesOnceCreditNoteDeleted = async ({ - tenantId, - oldCreditNote, - creditNoteId, - trx, - }: ICreditNoteDeletedPayload) => { - // Can't continue if the credit note is not published yet. - if (!oldCreditNote.isPublished) return; +// /** +// * Reverts GL entries once credit note deleted. +// */ +// private revertGLEntriesOnceCreditNoteDeleted = async ({ +// tenantId, +// oldCreditNote, +// creditNoteId, +// trx, +// }: ICreditNoteDeletedPayload) => { +// // Can't continue if the credit note is not published yet. +// if (!oldCreditNote.isPublished) return; - await this.creditNoteGLEntries.revertVendorCreditGLEntries( - tenantId, - creditNoteId - ); - }; +// await this.creditNoteGLEntries.revertVendorCreditGLEntries( +// tenantId, +// creditNoteId +// ); +// }; - /** - * Edits vendor credit associated GL entries once the transaction edited. - * @param {ICreditNoteEditedPayload} payload - - */ - private editVendorCreditGLEntriesOnceEdited = async ({ - tenantId, - creditNote, - creditNoteId, - trx, - }: ICreditNoteEditedPayload) => { - // Can't continue if the credit note is not published yet. - if (!creditNote.isPublished) return; +// /** +// * Edits vendor credit associated GL entries once the transaction edited. +// * @param {ICreditNoteEditedPayload} payload - +// */ +// private editVendorCreditGLEntriesOnceEdited = async ({ +// tenantId, +// creditNote, +// creditNoteId, +// trx, +// }: ICreditNoteEditedPayload) => { +// // Can't continue if the credit note is not published yet. +// if (!creditNote.isPublished) return; - await this.creditNoteGLEntries.editVendorCreditGLEntries( - tenantId, - creditNoteId, - trx - ); - }; -} +// await this.creditNoteGLEntries.editVendorCreditGLEntries( +// tenantId, +// creditNoteId, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteInventoryTransactionsSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteInventoryTransactionsSubscriber.ts index 2b594bb4e..7abba2f5a 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteInventoryTransactionsSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/CreditNoteInventoryTransactionsSubscriber.ts @@ -1,98 +1,98 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import CreditNoteInventoryTransactions from '../commands/CreditNotesInventoryTransactions'; -import { - ICreditNoteCreatedPayload, - ICreditNoteDeletedPayload, - ICreditNoteEditedPayload, -} from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import CreditNoteInventoryTransactions from '../commands/CreditNotesInventoryTransactions'; +// import { +// ICreditNoteCreatedPayload, +// ICreditNoteDeletedPayload, +// ICreditNoteEditedPayload, +// } from '@/interfaces'; -@Service() -export default class CreditNoteInventoryTransactionsSubscriber { - @Inject() - inventoryTransactions: CreditNoteInventoryTransactions; +// @Service() +// export default class CreditNoteInventoryTransactionsSubscriber { +// @Inject() +// inventoryTransactions: CreditNoteInventoryTransactions; - /** - * Attaches events with publisher. - */ - public attach(bus) { - bus.subscribe( - events.creditNote.onCreated, - this.writeInventoryTranscationsOnceCreated - ); - bus.subscribe( - events.creditNote.onEdited, - this.rewriteInventoryTransactionsOnceEdited - ); - bus.subscribe( - events.creditNote.onDeleted, - this.revertInventoryTransactionsOnceDeleted - ); - bus.subscribe( - events.creditNote.onOpened, - this.writeInventoryTranscationsOnceCreated - ); - } +// /** +// * Attaches events with publisher. +// */ +// public attach(bus) { +// bus.subscribe( +// events.creditNote.onCreated, +// this.writeInventoryTranscationsOnceCreated +// ); +// bus.subscribe( +// events.creditNote.onEdited, +// this.rewriteInventoryTransactionsOnceEdited +// ); +// bus.subscribe( +// events.creditNote.onDeleted, +// this.revertInventoryTransactionsOnceDeleted +// ); +// bus.subscribe( +// events.creditNote.onOpened, +// this.writeInventoryTranscationsOnceCreated +// ); +// } - /** - * Writes inventory transactions once credit note created. - * @param {ICreditNoteCreatedPayload} payload - - * @returns {Promise} - */ - public writeInventoryTranscationsOnceCreated = async ({ - tenantId, - creditNote, - trx, - }: ICreditNoteCreatedPayload) => { - // Can't continue if the credit note is open yet. - if (!creditNote.isOpen) return; +// /** +// * Writes inventory transactions once credit note created. +// * @param {ICreditNoteCreatedPayload} payload - +// * @returns {Promise} +// */ +// public writeInventoryTranscationsOnceCreated = async ({ +// tenantId, +// creditNote, +// trx, +// }: ICreditNoteCreatedPayload) => { +// // Can't continue if the credit note is open yet. +// if (!creditNote.isOpen) return; - await this.inventoryTransactions.createInventoryTransactions( - tenantId, - creditNote, - trx - ); - }; +// await this.inventoryTransactions.createInventoryTransactions( +// tenantId, +// creditNote, +// trx +// ); +// }; - /** - * Rewrites inventory transactions once credit note edited. - * @param {ICreditNoteEditedPayload} payload - - * @returns {Promise} - */ - public rewriteInventoryTransactionsOnceEdited = async ({ - tenantId, - creditNoteId, - creditNote, - trx, - }: ICreditNoteEditedPayload) => { - // Can't continue if the credit note is open yet. - if (!creditNote.isOpen) return; +// /** +// * Rewrites inventory transactions once credit note edited. +// * @param {ICreditNoteEditedPayload} payload - +// * @returns {Promise} +// */ +// public rewriteInventoryTransactionsOnceEdited = async ({ +// tenantId, +// creditNoteId, +// creditNote, +// trx, +// }: ICreditNoteEditedPayload) => { +// // Can't continue if the credit note is open yet. +// if (!creditNote.isOpen) return; - await this.inventoryTransactions.editInventoryTransactions( - tenantId, - creditNoteId, - creditNote, - trx - ); - }; +// await this.inventoryTransactions.editInventoryTransactions( +// tenantId, +// creditNoteId, +// creditNote, +// trx +// ); +// }; - /** - * Reverts inventory transactions once credit note deleted. - * @param {ICreditNoteDeletedPayload} payload - - */ - public revertInventoryTransactionsOnceDeleted = async ({ - tenantId, - creditNoteId, - oldCreditNote, - trx, - }: ICreditNoteDeletedPayload) => { - // Can't continue if the credit note is open yet. - if (!oldCreditNote.isOpen) return; +// /** +// * Reverts inventory transactions once credit note deleted. +// * @param {ICreditNoteDeletedPayload} payload - +// */ +// public revertInventoryTransactionsOnceDeleted = async ({ +// tenantId, +// creditNoteId, +// oldCreditNote, +// trx, +// }: ICreditNoteDeletedPayload) => { +// // Can't continue if the credit note is open yet. +// if (!oldCreditNote.isOpen) return; - await this.inventoryTransactions.deleteInventoryTransactions( - tenantId, - creditNoteId, - trx - ); - }; -} +// await this.inventoryTransactions.deleteInventoryTransactions( +// tenantId, +// creditNoteId, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/DeleteCustomerLinkedCreditSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/DeleteCustomerLinkedCreditSubscriber.ts index 826c94e9d..fee140d62 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/DeleteCustomerLinkedCreditSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/DeleteCustomerLinkedCreditSubscriber.ts @@ -1,48 +1,48 @@ -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 { 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'; -const ERRORS = { - CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS', -}; +// const ERRORS = { +// CUSTOMER_HAS_TRANSACTIONS: 'CUSTOMER_HAS_TRANSACTIONS', +// }; -@Service() -export default class DeleteCustomerLinkedCreditSubscriber { - @Inject() - tenancy: TenancyService; +// @Service() +// export default class DeleteCustomerLinkedCreditSubscriber { +// @Inject() +// tenancy: TenancyService; - @Inject() - deleteCustomerLinkedCredit: DeleteCustomerLinkedCreidtNote; +// @Inject() +// deleteCustomerLinkedCredit: DeleteCustomerLinkedCreidtNote; - /** - * Attaches events with handlers. - * @param bus - */ - public attach = (bus) => { - bus.subscribe( - events.customers.onDeleting, - this.validateCustomerHasNoLinkedCreditsOnDeleting - ); - }; +// /** +// * 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 - +// */ +// public validateCustomerHasNoLinkedCreditsOnDeleting = async ({ +// tenantId, +// customerId, +// }: ICustomerDeletingPayload) => { +// try { +// await this.deleteCustomerLinkedCredit.validateCustomerHasNoCreditTransaction( +// tenantId, +// customerId +// ); +// } catch (error) { +// throw new ServiceError(ERRORS.CUSTOMER_HAS_TRANSACTIONS); +// } +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/RefundCreditNoteGLEntriesSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/RefundCreditNoteGLEntriesSubscriber.ts index a4d0365a0..8f5a4851c 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/RefundCreditNoteGLEntriesSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/RefundCreditNoteGLEntriesSubscriber.ts @@ -1,61 +1,61 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import RefundCreditNoteGLEntries from '../commands/RefundCreditNoteGLEntries'; -import { - IRefundCreditNoteCreatedPayload, - IRefundCreditNoteDeletedPayload, -} from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import RefundCreditNoteGLEntries from '../commands/RefundCreditNoteGLEntries'; +// import { +// IRefundCreditNoteCreatedPayload, +// IRefundCreditNoteDeletedPayload, +// } from '@/interfaces'; -@Service() -export default class RefundCreditNoteGLEntriesSubscriber { - @Inject() - refundCreditGLEntries: RefundCreditNoteGLEntries; +// @Service() +// export default class RefundCreditNoteGLEntriesSubscriber { +// @Inject() +// refundCreditGLEntries: RefundCreditNoteGLEntries; - /** - * Attaches events with handlers. - */ - public attach = (bus) => { - bus.subscribe( - events.creditNote.onRefundCreated, - this.writeRefundCreditGLEntriesOnceCreated - ); - bus.subscribe( - events.creditNote.onRefundDeleted, - this.revertRefundCreditGLEntriesOnceDeleted - ); - }; +// /** +// * 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 - - */ - private writeRefundCreditGLEntriesOnceCreated = async ({ - trx, - refundCreditNote, - creditNote, - tenantId, - }: IRefundCreditNoteCreatedPayload) => { - await this.refundCreditGLEntries.createRefundCreditGLEntries( - tenantId, - 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 - +// */ +// private revertRefundCreditGLEntriesOnceDeleted = async ({ +// trx, +// refundCreditId, +// oldRefundCredit, +// tenantId, +// }: IRefundCreditNoteDeletedPayload) => { +// await this.refundCreditGLEntries.revertRefundCreditGLEntries( +// tenantId, +// refundCreditId, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/subscribers/RefundSyncCreditNoteBalanceSubscriber.ts b/packages/server-nest/src/modules/CreditNotes/subscribers/RefundSyncCreditNoteBalanceSubscriber.ts index a4ac93396..4f0cae69c 100644 --- a/packages/server-nest/src/modules/CreditNotes/subscribers/RefundSyncCreditNoteBalanceSubscriber.ts +++ b/packages/server-nest/src/modules/CreditNotes/subscribers/RefundSyncCreditNoteBalanceSubscriber.ts @@ -1,62 +1,62 @@ -import { Inject, Service } from 'typedi'; -import { - IRefundCreditNoteCreatedPayload, - IRefundCreditNoteDeletedPayload, -} from '@/interfaces'; -import events from '@/subscribers/events'; -import RefundSyncCreditNoteBalance from '../commands/RefundSyncCreditNoteBalance'; +// import { Inject, Service } from 'typedi'; +// import { +// IRefundCreditNoteCreatedPayload, +// IRefundCreditNoteDeletedPayload, +// } from '@/interfaces'; +// import events from '@/subscribers/events'; +// import RefundSyncCreditNoteBalance from '../commands/RefundSyncCreditNoteBalance'; -@Service() -export default class RefundSyncCreditNoteBalanceSubscriber { - @Inject() - refundSyncCreditBalance: RefundSyncCreditNoteBalance; +// @Service() +// export default class RefundSyncCreditNoteBalanceSubscriber { +// @Inject() +// refundSyncCreditBalance: RefundSyncCreditNoteBalance; - /** - * Attaches events with handlers. - */ - attach(bus) { - bus.subscribe( - events.creditNote.onRefundCreated, - this.incrementRefundedAmountOnceRefundCreated - ); - bus.subscribe( - events.creditNote.onRefundDeleted, - this.decrementRefundedAmountOnceRefundDeleted - ); - return bus; - } +// /** +// * 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 - - */ - private incrementRefundedAmountOnceRefundCreated = async ({ - trx, - refundCreditNote, - tenantId, - }: IRefundCreditNoteCreatedPayload) => { - await this.refundSyncCreditBalance.incrementCreditNoteRefundAmount( - tenantId, - 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 - +// */ +// private decrementRefundedAmountOnceRefundDeleted = async ({ +// trx, +// oldRefundCredit, +// tenantId, +// }: IRefundCreditNoteDeletedPayload) => { +// await this.refundSyncCreditBalance.decrementCreditNoteRefundAmount( +// tenantId, +// oldRefundCredit.creditNoteId, +// oldRefundCredit.amount, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotes/types/CreditNotes.types.ts b/packages/server-nest/src/modules/CreditNotes/types/CreditNotes.types.ts index 3a56f73e0..c8a7488fa 100644 --- a/packages/server-nest/src/modules/CreditNotes/types/CreditNotes.types.ts +++ b/packages/server-nest/src/modules/CreditNotes/types/CreditNotes.types.ts @@ -1,19 +1,11 @@ import { Knex } from 'knex'; -import { DiscountType, IDynamicListFilter, IItemEntry } from '@/interfaces'; -import { ILedgerEntry } from './Ledger'; -import { AttachmentLinkDTO } from './Attachments'; import { CreditNote } from '../models/CreditNote'; -import { RefundCreditNote } from '../models/RefundCreditNote'; +import { RefundCreditNote } from '../../CreditNoteRefunds/models/RefundCreditNote'; +import { AttachmentLinkDTO } from '@/modules/Attachments/Attachments.types'; +import { IItemEntryDTO } from '@/modules/TransactionItemEntry/ItemEntry.types'; + +export interface ICreditNoteEntryNewDTO extends IItemEntryDTO {} -export interface ICreditNoteEntryNewDTO { - index?: number; - itemId: number; - rate: number; - quantity: number; - discount: number; - description: string; - warehouseId?: number; -} export interface ICreditNoteNewDTO { customerId: number; exchangeRate?: number; @@ -26,7 +18,7 @@ export interface ICreditNoteNewDTO { warehouseId?: number; attachments?: AttachmentLinkDTO[]; discount?: number; - discountType?: DiscountType; + // discountType?: DiscountType; adjustment?: number; } @@ -43,34 +35,6 @@ export interface ICreditNoteEditDTO { attachments?: AttachmentLinkDTO[]; } -export interface ICreditNoteEntry extends IItemEntry {} - -export interface ICreditNote { - id?: number; - customerId: number; - amount: number; - refundedAmount: number; - currencyCode: string; - exchangeRate: number; - creditNoteDate: Date; - creditNoteNumber: string; - referenceNo?: string; - // note?: string; - openedAt: Date | null; - entries: ICreditNoteEntry[]; - isOpen: boolean; - isClosed: boolean; - isDraft: boolean; - isPublished: boolean; - creditsRemaining: number; - localAmount?: number; - branchId?: number; - warehouseId: number; - createdAt?: Date; - termsConditions: string; - note: string; -} - export enum CreditNoteAction { Create = 'Create', Edit = 'Edit', @@ -102,37 +66,28 @@ export interface ICreditNoteEditingPayload { export interface ICreditNoteEditedPayload { trx: Knex.Transaction; oldCreditNote: CreditNote; - // creditNoteId: number; creditNote: CreditNote; creditNoteEditDTO: ICreditNoteEditDTO; - // tenantId: number; } export interface ICreditNoteCreatedPayload { - // tenantId: number; creditNoteDTO: ICreditNoteNewDTO; creditNote: CreditNote; - // creditNoteId: number; trx: Knex.Transaction; } export interface ICreditNoteCreatingPayload { - // tenantId: number; creditNoteDTO: ICreditNoteNewDTO; trx: Knex.Transaction; } export interface ICreditNoteOpeningPayload { - tenantId: number; - creditNoteId: number; oldCreditNote: CreditNote; trx: Knex.Transaction; } export interface ICreditNoteOpenedPayload { - // tenantId: number; creditNote: CreditNote; - // creditNoteId: number; oldCreditNote: CreditNote; trx: Knex.Transaction; } @@ -141,11 +96,11 @@ export interface ICreditNotesQueryDTO { filterQuery?: (query: any) => void; } -export interface ICreditNotesQueryDTO extends IDynamicListFilter { - page: number; - pageSize: number; - searchKeyword?: string; -} +// export interface ICreditNotesQueryDTO extends IDynamicListFilter { +// page: number; +// pageSize: number; +// searchKeyword?: string; +// } export interface ICreditNoteRefundDTO { fromAccountId: number; @@ -157,96 +112,22 @@ export interface ICreditNoteRefundDTO { branchId?: number; } -export interface ICreditNoteApplyInvoiceDTO { - entries: { invoiceId: number; amount: number }[]; -} -export interface IRefundCreditNotePOJO { - formattedAmount: string; -} - -export interface IRefundCreditNoteDeletedPayload { - trx: Knex.Transaction; - refundCreditId: number; - oldRefundCredit: RefundCreditNote; - tenantId: number; -} - -export interface IRefundCreditNoteDeletingPayload { - trx: Knex.Transaction; - refundCreditId: number; - oldRefundCredit: RefundCreditNote; - // tenantId: number; -} - -export interface IRefundCreditNoteCreatingPayload { - trx: Knex.Transaction; - creditNote: CreditNote; - newCreditNoteDTO: ICreditNoteRefundDTO; - // tenantId: number; -} - -export interface IRefundCreditNoteCreatedPayload { - trx: Knex.Transaction; - refundCreditNote: RefundCreditNote; - creditNote: CreditNote; - // tenantId: number; -} - -export interface IRefundCreditNoteOpenedPayload { - // tenantId: number; - creditNoteId: number; - oldCreditNote: CreditNote; - trx: Knex.Transaction; -} - -export interface IApplyCreditToInvoiceEntryDTO { - amount: number; - invoiceId: number; -} - -export interface IApplyCreditToInvoicesDTO { - entries: IApplyCreditToInvoiceEntryDTO[]; -} - -export interface IApplyCreditToInvoicesCreatedPayload { - trx: Knex.Transaction; - creditNote: ICreditNote; - creditNoteAppliedInvoices: ICreditNoteAppliedToInvoice[]; - // tenantId: number; -} -export interface IApplyCreditToInvoicesDeletedPayload { - trx: Knex.Transaction; - creditNote: ICreditNote; - creditNoteAppliedToInvoice: ICreditNoteAppliedToInvoice; - // tenantId: number; -} - -export interface ICreditNoteAppliedToInvoice { - invoiceId: number; - amount: number; - creditNoteId: number; -} -export interface ICreditNoteAppliedToInvoiceModel { - amount: number; - entries: ICreditNoteAppliedToInvoice[]; -} - -export type ICreditNoteGLCommonEntry = Pick< - ILedgerEntry, - | 'date' - | 'userId' - | 'currencyCode' - | 'exchangeRate' - | 'transactionType' - | 'transactionId' - | 'transactionNumber' - | 'referenceNumber' - | 'createdAt' - | 'indexGroup' - | 'credit' - | 'debit' - | 'branchId' ->; +// export type ICreditNoteGLCommonEntry = Pick< +// ILedgerEntry, +// | 'date' +// | 'userId' +// | 'currencyCode' +// | 'exchangeRate' +// | 'transactionType' +// | 'transactionId' +// | 'transactionNumber' +// | 'referenceNumber' +// | 'createdAt' +// | 'indexGroup' +// | 'credit' +// | 'debit' +// | 'branchId' +// >; export interface CreditNotePdfTemplateAttributes { // # Primary color @@ -304,4 +185,4 @@ export interface CreditNotePdfTemplateAttributes { export interface ICreditNoteState { defaultTemplateId: number; -} \ No newline at end of file +} diff --git a/packages/server-nest/src/modules/CreditNotes/utils.ts b/packages/server-nest/src/modules/CreditNotes/utils.ts index 8936f922c..d06550c98 100644 --- a/packages/server-nest/src/modules/CreditNotes/utils.ts +++ b/packages/server-nest/src/modules/CreditNotes/utils.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import { CreditNotePdfTemplateAttributes, ICreditNote } from '@/interfaces'; import { contactAddressTextFormat } from '@/utils/address-text-format'; diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncCredit.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncCredit.service.ts similarity index 94% rename from packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncCredit.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncCredit.service.ts index d32abc4a4..8b7a58964 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncCredit.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncCredit.service.ts @@ -1,6 +1,6 @@ import { Knex } from 'knex'; import { Inject, Injectable } from '@nestjs/common'; -import { CreditNote } from '../models/CreditNote'; +import { CreditNote } from '../../CreditNotes/models/CreditNote'; @Injectable() export class CreditNoteApplySyncCredit { diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncInvoices.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncInvoices.service.ts similarity index 84% rename from packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncInvoices.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncInvoices.service.ts index ade0f54a6..06ac32ed1 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplySyncInvoices.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplySyncInvoices.service.ts @@ -1,11 +1,15 @@ import { Knex } from 'knex'; import { Injectable, Inject } from '@nestjs/common'; import Bluebird from 'bluebird'; -import { ICreditNoteAppliedToInvoice } from '../types/CreditNotes.types'; +import { ICreditNoteAppliedToInvoice } from '../types/CreditNoteApplyInvoice.types'; import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; +import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; @Injectable() export class CreditNoteApplySyncInvoicesCreditedAmount { + /** + * @param {typeof SaleInvoice} saleInvoiceModel - Sale invoice model. + */ constructor( @Inject(SaleInvoice.name) private readonly saleInvoiceModel: typeof SaleInvoice, @@ -22,7 +26,7 @@ export class CreditNoteApplySyncInvoicesCreditedAmount { ) => { await Bluebird.each( creditNoteAppliedInvoices, - (creditNoteAppliedInvoice: ICreditNoteAppliedToInvoice) => { + (creditNoteAppliedInvoice: CreditNoteAppliedInvoice) => { return this.saleInvoiceModel.query(trx) .where('id', creditNoteAppliedInvoice.invoiceId) .increment('creditedAmount', creditNoteAppliedInvoice.amount); diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplyToInvoices.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplyToInvoices.service.ts similarity index 85% rename from packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplyToInvoices.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplyToInvoices.service.ts index 5f194b0de..ab462f48d 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteApplyToInvoices.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/CreditNoteApplyToInvoices.service.ts @@ -2,20 +2,20 @@ import { Inject, Injectable } from '@nestjs/common'; import { Knex } from 'knex'; import { sumBy } from 'lodash'; import { - ICreditNoteAppliedToInvoice, ICreditNoteAppliedToInvoiceModel, IApplyCreditToInvoicesDTO, IApplyCreditToInvoicesCreatedPayload, - ICreditNote, -} from '../types/CreditNotes.types'; -import { ERRORS } from '../constants'; +} from '../types/CreditNoteApplyInvoice.types'; +import { ERRORS } from '../../CreditNotes/constants'; import { PaymentReceivedValidators } from '@/modules/PaymentReceived/commands/PaymentReceivedValidators.service'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { EventEmitter2 } from '@nestjs/event-emitter'; -import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; import { events } from '@/common/events/events'; import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; import { ServiceError } from '@/modules/Items/ServiceError'; +import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; +import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; +import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service'; @Injectable() export class CreditNoteApplyToInvoices { @@ -29,9 +29,13 @@ export class CreditNoteApplyToInvoices { private readonly paymentReceiveValidators: PaymentReceivedValidators, private readonly uow: UnitOfWork, private readonly eventPublisher: EventEmitter2, + private readonly creditNoteDTOTransform: CommandCreditNoteDTOTransform, @Inject(CreditNoteAppliedInvoice.name) private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice, + + @Inject(CreditNote.name) + private readonly creditNoteModel: typeof CreditNote, ) {} /** @@ -42,9 +46,12 @@ export class CreditNoteApplyToInvoices { public async applyCreditNoteToInvoices( creditNoteId: number, applyCreditToInvoicesDTO: IApplyCreditToInvoicesDTO, - ): Promise { + ): Promise { // Saves the credit note or throw not found service error. - const creditNote = await this.getCreditNoteOrThrowError(creditNoteId); + const creditNote = await this.creditNoteModel + .query() + .findById(creditNoteId) + .throwIfNotFound(); // Retrieve the applied invoices that associated to the credit note customer. const appliedInvoicesEntries = @@ -58,18 +65,16 @@ export class CreditNoteApplyToInvoices { applyCreditToInvoicesDTO, creditNote, ); - // Validate invoices has remaining amount to apply. this.validateInvoicesRemainingAmount( appliedInvoicesEntries, creditNoteAppliedModel.amount, ); // Validate the credit note remaining amount. - this.validateCreditRemainingAmount( + this.creditNoteDTOTransform.validateCreditRemainingAmount( creditNote, creditNoteAppliedModel.amount, ); - // Creates credit note apply to invoice transaction. return this.uow.withTransaction(async (trx: Knex.Transaction) => { // Saves the credit note apply to invoice graph to the storage layer. @@ -99,7 +104,7 @@ export class CreditNoteApplyToInvoices { */ private transformApplyDTOToModel = ( applyDTO: IApplyCreditToInvoicesDTO, - creditNote: ICreditNote, + creditNote: CreditNote, ): ICreditNoteAppliedToInvoiceModel => { const entries = applyDTO.entries.map((entry) => ({ invoiceId: entry.invoiceId, diff --git a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNoteApplyToInvoices.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCreditNoteApplyToInvoices.service.ts similarity index 82% rename from packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNoteApplyToInvoices.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCreditNoteApplyToInvoices.service.ts index 623a532bf..1b90ce211 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCreditNoteApplyToInvoices.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCreditNoteApplyToInvoices.service.ts @@ -1,16 +1,22 @@ import { Inject, Injectable } from '@nestjs/common'; import { Knex } from 'knex'; import { EventEmitter2 } from '@nestjs/event-emitter'; -import { IApplyCreditToInvoicesDeletedPayload } from '../types/CreditNotes.types'; +import { IApplyCreditToInvoicesDeletedPayload } from '../types/CreditNoteApplyInvoice.types'; import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { events } from '@/common/events/events'; import { ServiceError } from '@/modules/Items/ServiceError'; -import { CreditNote } from '../models/CreditNote'; -import { ERRORS } from '../constants'; +import { CreditNote } from '../../CreditNotes/models/CreditNote'; +import { ERRORS } from '../../CreditNotes/constants'; @Injectable() export default class DeleteCreditNoteApplyToInvoices { + /** + * @param {UnitOfWork} uow - Unit of work. + * @param {EventEmitter2} eventPublisher - Event emitter. + * @param {typeof CreditNoteAppliedInvoice} creditNoteAppliedInvoiceModel - Credit note applied invoice model. + * @param {typeof CreditNote} creditNoteModel - Credit note model. + */ constructor( private readonly uow: UnitOfWork, private readonly eventPublisher: EventEmitter2, diff --git a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCustomerLinkedCreditNote.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCustomerLinkedCreditNote.service.ts similarity index 78% rename from packages/server-nest/src/modules/CreditNotes/commands/DeleteCustomerLinkedCreditNote.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCustomerLinkedCreditNote.service.ts index 149047e68..914eb58e6 100644 --- a/packages/server-nest/src/modules/CreditNotes/commands/DeleteCustomerLinkedCreditNote.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/commands/DeleteCustomerLinkedCreditNote.service.ts @@ -1,10 +1,13 @@ import { Inject, Injectable } from '@nestjs/common'; -import { CreditNote } from '../models/CreditNote'; -import { ERRORS } from '../constants'; +import { CreditNote } from '../../CreditNotes/models/CreditNote'; +import { ERRORS } from '../../CreditNotes/constants'; import { ServiceError } from '@/modules/Items/ServiceError'; @Injectable() export class DeleteCustomerLinkedCreditNoteService { + /** + * @param {typeof CreditNote} creditNoteModel - Credit note model. + */ constructor( @Inject(CreditNote.name) private readonly creditNoteModel: typeof CreditNote, diff --git a/packages/server-nest/src/modules/CreditNotes/models/CreditNoteAppliedInvoice.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice.ts similarity index 95% rename from packages/server-nest/src/modules/CreditNotes/models/CreditNoteAppliedInvoice.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice.ts index 2c3b23cb7..def5940dd 100644 --- a/packages/server-nest/src/modules/CreditNotes/models/CreditNoteAppliedInvoice.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice.ts @@ -5,7 +5,7 @@ import { mixin, Model } from 'objection'; // import ModelSearchable from './ModelSearchable'; import { BaseModel } from '@/models/Model'; import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; -import { CreditNote } from './CreditNote'; +import { CreditNote } from '../../CreditNotes/models/CreditNote'; export class CreditNoteAppliedInvoice extends BaseModel { public amount: number; diff --git a/packages/server-nest/src/modules/CreditNotes/queries/CreditNoteAppliedInvoiceTransformer.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/CreditNoteAppliedInvoiceTransformer.ts similarity index 69% rename from packages/server-nest/src/modules/CreditNotes/queries/CreditNoteAppliedInvoiceTransformer.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/CreditNoteAppliedInvoiceTransformer.ts index b5105c931..5f6575f6f 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/CreditNoteAppliedInvoiceTransformer.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/CreditNoteAppliedInvoiceTransformer.ts @@ -25,28 +25,58 @@ export class CreditNoteAppliedInvoiceTransformer extends Transformer { return ['saleInvoice', 'creditNote']; }; + /** + * Formatted amount. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public formttedAmount = (item: CreditNoteAppliedInvoice) => { return this.formatNumber(item.amount, { currencyCode: item.creditNote.currencyCode, }); }; + /** + * Credit note number. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public creditNoteNumber = (item: CreditNoteAppliedInvoice) => { return item.creditNote.creditNoteNumber; }; + /** + * Credit note date. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public creditNoteDate = (item: CreditNoteAppliedInvoice) => { return item.creditNote.creditNoteDate; }; + /** + * Invoice number. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public invoiceNumber = (item: CreditNoteAppliedInvoice) => { return item.saleInvoice.invoiceNo; }; + /** + * Invoice reference no. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public invoiceReferenceNo = (item: CreditNoteAppliedInvoice) => { return item.saleInvoice.referenceNo; }; + /** + * Formatted credit note date. + * @param {CreditNoteAppliedInvoice} item - + * @returns {string} + */ public formattedCreditNoteDate = (item: CreditNoteAppliedInvoice) => { return this.formatDate(item.creditNote.creditNoteDate); }; diff --git a/packages/server-nest/src/modules/CreditNotes/commands/CreditNoteWithInvoicesToApplyTransformer.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/CreditNoteWithInvoicesToApplyTransformer.ts similarity index 100% rename from packages/server-nest/src/modules/CreditNotes/commands/CreditNoteWithInvoicesToApplyTransformer.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/CreditNoteWithInvoicesToApplyTransformer.ts diff --git a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts similarity index 95% rename from packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts index 94d2b12d3..c7fa4eccb 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedAppliedInvoices.service.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from '@nestjs/common'; import { CreditNoteAppliedInvoiceTransformer } from './CreditNoteAppliedInvoiceTransformer'; -import { CreditNote } from '../models/CreditNote'; +import { CreditNote } from '../../CreditNotes/models/CreditNote'; import { TransformerInjectable } from '../../Transformer/TransformerInjectable.service'; import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice'; diff --git a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts similarity index 76% rename from packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts rename to packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts index e2f275284..6feb4c006 100644 --- a/packages/server-nest/src/modules/CreditNotes/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/queries/GetCreditNoteAssociatedInvoicesToApply.service.ts @@ -1,11 +1,16 @@ import { Inject, Injectable } from '@nestjs/common'; -import { CreditNoteWithInvoicesToApplyTransformer } from '../commands/CreditNoteWithInvoicesToApplyTransformer'; import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service'; import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; -import { GetCreditNote } from './GetCreditNote.service'; +import { GetCreditNote } from '../../CreditNotes/queries/GetCreditNote.service'; +import { CreditNoteWithInvoicesToApplyTransformer } from './CreditNoteWithInvoicesToApplyTransformer'; @Injectable() export class GetCreditNoteAssociatedInvoicesToApply { + /** + * @param {TransformerInjectable} transformer - Transformer service. + * @param {GetCreditNote} getCreditNote - Get credit note service. + * @param {typeof SaleInvoice} saleInvoiceModel - Sale invoice model. + */ constructor( private transformer: TransformerInjectable, private getCreditNote: GetCreditNote, diff --git a/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncCreditSubscriber.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncCreditSubscriber.ts new file mode 100644 index 000000000..2b49ecb78 --- /dev/null +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncCreditSubscriber.ts @@ -0,0 +1,67 @@ +// 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'; + +// @Service() +// export default class CreditNoteApplySyncCreditSubscriber { +// @Inject() +// 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 - +// */ +// private incrementCreditedAmountOnceApplyToInvoicesCreated = async ({ +// trx, +// creditNote, +// tenantId, +// creditNoteAppliedInvoices, +// }: IApplyCreditToInvoicesCreatedPayload) => { +// const totalCredited = sumBy(creditNoteAppliedInvoices, 'amount'); + +// 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 +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts new file mode 100644 index 000000000..bcf0580d7 --- /dev/null +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/subscribers/CreditNoteApplySyncInvoicesSubscriber.ts @@ -0,0 +1,61 @@ +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import { +// IApplyCreditToInvoicesCreatedPayload, +// IApplyCreditToInvoicesDeletedPayload, +// } from '@/interfaces'; +// import CreditNoteApplySyncInvoicesCreditedAmount from '../commands/CreditNoteApplySyncInvoices.service'; + +// @Service() +// export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber { +// @Inject() +// private 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 - +// */ +// 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 +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/CreditNotesApplyInvoice/types/CreditNoteApplyInvoice.types.ts b/packages/server-nest/src/modules/CreditNotesApplyInvoice/types/CreditNoteApplyInvoice.types.ts new file mode 100644 index 000000000..2d3bc633e --- /dev/null +++ b/packages/server-nest/src/modules/CreditNotesApplyInvoice/types/CreditNoteApplyInvoice.types.ts @@ -0,0 +1,35 @@ +import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; +import { Knex } from 'knex'; + +export interface ICreditNoteApplyInvoiceDTO { + entries: { invoiceId: number; amount: number }[]; +} + +export interface IApplyCreditToInvoiceEntryDTO { + amount: number; + invoiceId: number; +} + +export interface IApplyCreditToInvoicesDTO { + entries: IApplyCreditToInvoiceEntryDTO[]; +} + +export interface IApplyCreditToInvoicesCreatedPayload { + trx: Knex.Transaction; + creditNote: CreditNote; + creditNoteAppliedInvoices: ICreditNoteAppliedToInvoice[]; +} +export interface IApplyCreditToInvoicesDeletedPayload { + trx: Knex.Transaction; + creditNote: CreditNote; + creditNoteAppliedToInvoice: ICreditNoteAppliedToInvoice; +} + +export interface ICreditNoteAppliedToInvoice { + amount: number; + creditNoteId: number; +} +export interface ICreditNoteAppliedToInvoiceModel { + amount: number; + entries: ICreditNoteAppliedToInvoice[]; +} diff --git a/packages/server-nest/src/modules/Customers/commands/ActivateCustomer.service.ts b/packages/server-nest/src/modules/Customers/commands/ActivateCustomer.service.ts index 86aa27de1..adc6dbbf1 100644 --- a/packages/server-nest/src/modules/Customers/commands/ActivateCustomer.service.ts +++ b/packages/server-nest/src/modules/Customers/commands/ActivateCustomer.service.ts @@ -1,14 +1,14 @@ +import { Knex } from 'knex'; import { Inject, Injectable } from '@nestjs/common'; +import { EventEmitter2 } from '@nestjs/event-emitter'; import { CustomerValidators } from './CustomerValidators.service'; import { ICustomerActivatedPayload, ICustomerActivatingPayload, } from '../types/Customers.types'; import { Customer } from '@/modules/Customers/models/Customer'; -import { EventEmitter2 } from '@nestjs/event-emitter'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { events } from '@/common/events/events'; -import { Knex } from 'knex'; @Injectable() export class ActivateCustomer { diff --git a/packages/server-nest/src/modules/Items/ItemsEntries.service.ts b/packages/server-nest/src/modules/Items/ItemsEntries.service.ts index df53cca8b..badbea497 100644 --- a/packages/server-nest/src/modules/Items/ItemsEntries.service.ts +++ b/packages/server-nest/src/modules/Items/ItemsEntries.service.ts @@ -235,7 +235,7 @@ export class ItemsEntriesService { * @param {ItemEntry[]} entries - Items entries. * @returns {number} */ - public getTotalItemsEntries(entries: ItemEntry[]): number { + public getTotalItemsEntries(entries: IItemEntryDTO[]): number { return sumBy(entries, (e) => ItemEntry.calcAmount(e)); } diff --git a/packages/server-nest/src/modules/SaleEstimates/models/SaleEstimate.ts b/packages/server-nest/src/modules/SaleEstimates/models/SaleEstimate.ts index c5fd413bd..1e7d9fe0b 100644 --- a/packages/server-nest/src/modules/SaleEstimates/models/SaleEstimate.ts +++ b/packages/server-nest/src/modules/SaleEstimates/models/SaleEstimate.ts @@ -10,16 +10,6 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class SaleEstimate extends BaseModel { - constructor( - private readonly itemEntryModel: typeof ItemEntry, - private readonly customerModel: typeof Customer, - private readonly branchModel: typeof Branch, - private readonly warehouseModel: typeof Warehouse, - private readonly documentModel: typeof Document, - ) { - super(); - } - exchangeRate!: number; amount!: number; @@ -216,64 +206,64 @@ export class SaleEstimate extends BaseModel { /** * Relationship mapping. */ - static get relationMappings() { - return { - customer: { - relation: Model.BelongsToOneRelation, - modelClass: this.customerModel, - join: { - from: 'sales_estimates.customerId', - to: 'contacts.id', - }, - filter(query) { - query.where('contact_service', 'customer'); - }, - }, - entries: { - relation: Model.HasManyRelation, - modelClass: this.itemEntryModel, - join: { - from: 'sales_estimates.id', - to: 'items_entries.referenceId', - }, - filter(builder) { - builder.where('reference_type', 'SaleEstimate'); - builder.orderBy('index', 'ASC'); - }, - }, - branch: { - relation: Model.BelongsToOneRelation, - modelClass: this.branchModel, - join: { - from: 'sales_estimates.branchId', - to: 'branches.id', - }, - }, - warehouse: { - relation: Model.BelongsToOneRelation, - modelClass: this.warehouseModel, - join: { - from: 'sales_estimates.warehouseId', - to: 'warehouses.id', - }, - }, - attachments: { - relation: Model.ManyToManyRelation, - modelClass: this.documentModel, - join: { - from: 'sales_estimates.id', - through: { - from: 'document_links.modelId', - to: 'document_links.documentId', - }, - to: 'documents.id', - }, - filter(query) { - query.where('model_ref', 'SaleEstimate'); - }, - }, - }; - } + // static get relationMappings() { + // return { + // customer: { + // relation: Model.BelongsToOneRelation, + // modelClass: this.customerModel, + // join: { + // from: 'sales_estimates.customerId', + // to: 'contacts.id', + // }, + // filter(query) { + // query.where('contact_service', 'customer'); + // }, + // }, + // entries: { + // relation: Model.HasManyRelation, + // modelClass: this.itemEntryModel, + // join: { + // from: 'sales_estimates.id', + // to: 'items_entries.referenceId', + // }, + // filter(builder) { + // builder.where('reference_type', 'SaleEstimate'); + // builder.orderBy('index', 'ASC'); + // }, + // }, + // branch: { + // relation: Model.BelongsToOneRelation, + // modelClass: this.branchModel, + // join: { + // from: 'sales_estimates.branchId', + // to: 'branches.id', + // }, + // }, + // warehouse: { + // relation: Model.BelongsToOneRelation, + // modelClass: this.warehouseModel, + // join: { + // from: 'sales_estimates.warehouseId', + // to: 'warehouses.id', + // }, + // }, + // attachments: { + // relation: Model.ManyToManyRelation, + // modelClass: this.documentModel, + // join: { + // from: 'sales_estimates.id', + // through: { + // from: 'document_links.modelId', + // to: 'document_links.documentId', + // }, + // to: 'documents.id', + // }, + // filter(query) { + // query.where('model_ref', 'SaleEstimate'); + // }, + // }, + // }; + // } /** * Model settings. diff --git a/packages/server-nest/src/modules/SaleInvoices/commands/DeleteSaleInvoice.service.ts b/packages/server-nest/src/modules/SaleInvoices/commands/DeleteSaleInvoice.service.ts index e69467c16..4314ed073 100644 --- a/packages/server-nest/src/modules/SaleInvoices/commands/DeleteSaleInvoice.service.ts +++ b/packages/server-nest/src/modules/SaleInvoices/commands/DeleteSaleInvoice.service.ts @@ -14,7 +14,7 @@ import { ServiceError } from '@/modules/Items/ServiceError'; import { ERRORS } from '../constants'; import { events } from '@/common/events/events'; import { PaymentReceivedEntry } from '@/modules/PaymentReceived/models/PaymentReceivedEntry'; -import { CreditNoteAppliedInvoice } from '@/modules/CreditNotes/models/CreditNoteAppliedInvoice'; +import { CreditNoteAppliedInvoice } from '@/modules/CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice'; @Injectable() export class DeleteSaleInvoice { diff --git a/packages/server-nest/src/modules/Tenancy/TenancyModels/Tenancy.module.ts b/packages/server-nest/src/modules/Tenancy/TenancyModels/Tenancy.module.ts index cb1efe1db..9e750f1e0 100644 --- a/packages/server-nest/src/modules/Tenancy/TenancyModels/Tenancy.module.ts +++ b/packages/server-nest/src/modules/Tenancy/TenancyModels/Tenancy.module.ts @@ -28,12 +28,15 @@ import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost' import { VendorCreditAppliedBill } from '@/modules/VendorCreditsApplyBills/models/VendorCreditAppliedBill'; import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice'; import { PaymentReceivedEntry } from '@/modules/PaymentReceived/models/PaymentReceivedEntry'; -import { CreditNoteAppliedInvoice } from '@/modules/CreditNotes/models/CreditNoteAppliedInvoice'; +import { CreditNoteAppliedInvoice } from '@/modules/CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice'; import { CreditNote } from '@/modules/CreditNotes/models/CreditNote'; import { PaymentLink } from '@/modules/PaymentLinks/models/PaymentLink'; import { SaleReceipt } from '@/modules/SaleReceipts/models/SaleReceipt'; import { ManualJournal } from '@/modules/ManualJournals/models/ManualJournal'; import { ManualJournalEntry } from '@/modules/ManualJournals/models/ManualJournalEntry'; +import { RefundCreditNote } from '@/modules/CreditNoteRefunds/models/RefundCreditNote'; +import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; +import { RefundVendorCredit } from '@/modules/VendorCreditsRefund/models/RefundVendorCredit'; const models = [ Item, @@ -64,10 +67,15 @@ const models = [ PaymentReceivedEntry, CreditNoteAppliedInvoice, CreditNote, + RefundCreditNote, PaymentLink, SaleReceipt, ManualJournal, - ManualJournalEntry + ManualJournalEntry, + VendorCredit, + VendorCreditAppliedBill, + RefundVendorCredit, + ]; const modelProviders = models.map((model) => { diff --git a/packages/server-nest/src/modules/VendorCredit/VendorCredits.module.ts b/packages/server-nest/src/modules/VendorCredit/VendorCredits.module.ts index b18a1fd00..a80915388 100644 --- a/packages/server-nest/src/modules/VendorCredit/VendorCredits.module.ts +++ b/packages/server-nest/src/modules/VendorCredit/VendorCredits.module.ts @@ -5,9 +5,27 @@ import { EditVendorCreditService } from './commands/EditVendorCredit.service'; import { VendorCreditDTOTransformService } from './commands/VendorCreditDTOTransform.service'; import { VendorCreditAutoIncrementService } from './commands/VendorCreditAutoIncrement.service'; import { GetRefundVendorCreditService } from '../VendorCreditsRefund/queries/GetRefundVendorCredit.service'; -import GetVendorCreditService from './queries/GetVendorCredit.service'; - +import { GetVendorCreditService } from './queries/GetVendorCredit.service'; +import { VendorCreditsController } from './VendorCredits.controller'; +import { ItemsModule } from '../Items/items.module'; +import { TemplateInjectableModule } from '../TemplateInjectable/TemplateInjectable.module'; +import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module'; +import { ChromiumlyTenancyModule } from '../ChromiumlyTenancy/ChromiumlyTenancy.module'; +import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module'; +import { BranchesModule } from '../Branches/Branches.module'; +import { WarehousesModule } from '../Warehouses/Warehouses.module'; +import { VendorCreditsApplicationService } from './VendorCreditsApplication.service'; + @Module({ + imports: [ + ItemsModule, + PdfTemplatesModule, + ChromiumlyTenancyModule, + TemplateInjectableModule, + AutoIncrementOrdersModule, + BranchesModule, + WarehousesModule, + ], providers: [ CreateVendorCreditService, DeleteVendorCreditService, @@ -15,9 +33,19 @@ import GetVendorCreditService from './queries/GetVendorCredit.service'; VendorCreditDTOTransformService, VendorCreditAutoIncrementService, GetRefundVendorCreditService, - VendorCreditDTOTransformService, - GetVendorCreditService + GetVendorCreditService, + VendorCreditsApplicationService, ], - controllers: [], + exports: [ + CreateVendorCreditService, + DeleteVendorCreditService, + EditVendorCreditService, + VendorCreditDTOTransformService, + VendorCreditAutoIncrementService, + GetRefundVendorCreditService, + GetVendorCreditService, + VendorCreditsApplicationService, + ], + controllers: [VendorCreditsController], }) export class VendorCreditsModule {} diff --git a/packages/server-nest/src/modules/VendorCredit/VendorCreditsApplication.service.ts b/packages/server-nest/src/modules/VendorCredit/VendorCreditsApplication.service.ts index 99fa82e18..ce674abe3 100644 --- a/packages/server-nest/src/modules/VendorCredit/VendorCreditsApplication.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/VendorCreditsApplication.service.ts @@ -2,13 +2,19 @@ import { Knex } from 'knex'; import { CreateVendorCreditService } from './commands/CreateVendorCredit.service'; import { DeleteVendorCreditService } from './commands/DeleteVendorCredit.service'; import { EditVendorCreditService } from './commands/EditVendorCredit.service'; -import GetVendorCreditService from './queries/GetVendorCredit.service'; +import { GetVendorCreditService } from './queries/GetVendorCredit.service'; import { IVendorCreditEditDTO } from './types/VendorCredit.types'; import { IVendorCreditCreateDTO } from './types/VendorCredit.types'; import { Injectable } from '@nestjs/common'; @Injectable() export class VendorCreditsApplicationService { + /** + * @param {CreateVendorCreditService} createVendorCreditService - Create vendor credit service. + * @param {EditVendorCreditService} editVendorCreditService - Edit vendor credit service. + * @param {DeleteVendorCreditService} deleteVendorCreditService - Delete vendor credit service. + * @param {GetVendorCreditService} getVendorCreditService - Get vendor credit service. + */ constructor( private readonly createVendorCreditService: CreateVendorCreditService, private readonly editVendorCreditService: EditVendorCreditService, diff --git a/packages/server-nest/src/modules/VendorCredit/commands/CreateVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCredit/commands/CreateVendorCredit.service.ts index dc6c85e16..fa0ce26fb 100644 --- a/packages/server-nest/src/modules/VendorCredit/commands/CreateVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/commands/CreateVendorCredit.service.ts @@ -11,6 +11,7 @@ import { events } from '@/common/events/events'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; +import { VendorCreditDTOTransformService } from './VendorCreditDTOTransform.service'; @Injectable() export class CreateVendorCreditService { @@ -25,6 +26,7 @@ export class CreateVendorCreditService { private readonly uow: UnitOfWork, private readonly itemsEntriesService: ItemsEntriesService, private readonly eventPublisher: EventEmitter2, + private readonly vendorCreditDTOTransformService: VendorCreditDTOTransformService, @Inject(VendorCredit.name) private readonly vendorCreditModel: typeof VendorCredit, @@ -45,7 +47,6 @@ export class CreateVendorCreditService { await this.eventPublisher.emitAsync(events.vendorCredit.onCreate, { vendorCreditCreateDTO, }); - // Retrieve the given vendor or throw not found service error. const vendor = await this.vendorModel .query() @@ -56,12 +57,12 @@ export class CreateVendorCreditService { await this.itemsEntriesService.validateNonSellableEntriesItems( vendorCreditCreateDTO.entries, ); - // Transforms the credit DTO to storage layer. - const vendorCreditModel = this.transformCreateEditDTOToModel( - vendorCreditCreateDTO, - vendor.currencyCode, - ); + const vendorCreditModel = + this.vendorCreditDTOTransformService.transformCreateEditDTOToModel( + vendorCreditCreateDTO, + vendor.currencyCode, + ); // Saves the vendor credit transactions under UOW environment. return this.uow.withTransaction(async (trx: Knex.Transaction) => { // Triggers `onVendorCreditCreating` event. diff --git a/packages/server-nest/src/modules/VendorCredit/commands/DeleteVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCredit/commands/DeleteVendorCredit.service.ts index d1763c9dd..d0ea79268 100644 --- a/packages/server-nest/src/modules/VendorCredit/commands/DeleteVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/commands/DeleteVendorCredit.service.ts @@ -11,7 +11,7 @@ import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry'; import { VendorCreditAppliedBill } from '../../VendorCreditsApplyBills/models/VendorCreditAppliedBill'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { ServiceError } from '@/modules/Items/ServiceError'; -import { RefundVendorCredit } from '../models/RefundVendorCredit'; +import { RefundVendorCredit } from '../../VendorCreditsRefund/models/RefundVendorCredit'; import { events } from '@/common/events/events'; @Injectable() @@ -25,11 +25,15 @@ export class DeleteVendorCreditService { * @param {typeof VendorCreditAppliedBill} vendorCreditAppliedBillModel - The vendor credit applied bill model. */ constructor( - private vendorCreditModel: typeof VendorCredit, - private itemEntryModel: typeof ItemEntry, private eventPublisher: EventEmitter2, private uow: UnitOfWork, + @Inject(ItemEntry.name) + private itemEntryModel: typeof ItemEntry, + + @Inject(VendorCredit.name) + private vendorCreditModel: typeof VendorCredit, + @Inject(RefundVendorCredit.name) private refundVendorCreditModel: typeof RefundVendorCredit, @@ -46,7 +50,7 @@ export class DeleteVendorCreditService { trx?: Knex.Transaction, ) => { // Retrieve the old vendor credit. - const oldVendorCredit = await this.refundVendorCreditModel + const oldVendorCredit = await this.vendorCreditModel .query() .findById(vendorCreditId) .throwIfNotFound(); diff --git a/packages/server-nest/src/modules/VendorCredit/commands/EditVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCredit/commands/EditVendorCredit.service.ts index 89cdf5316..4d8c3a6aa 100644 --- a/packages/server-nest/src/modules/VendorCredit/commands/EditVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/commands/EditVendorCredit.service.ts @@ -10,6 +10,8 @@ import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service'; import { VendorCredit } from '../models/VendorCredit'; import { Contact } from '@/modules/Contacts/models/Contact'; import { events } from '@/common/events/events'; +import { Knex } from 'knex'; +import { VendorCreditDTOTransformService } from './VendorCreditDTOTransform.service'; @Injectable() export class EditVendorCreditService { @@ -24,6 +26,7 @@ export class EditVendorCreditService { private readonly eventPublisher: EventEmitter2, private readonly uow: UnitOfWork, private readonly itemsEntriesService: ItemsEntriesService, + private readonly vendorCreditDTOTransform: VendorCreditDTOTransformService, @Inject(VendorCredit.name) private readonly vendorCreditModel: typeof VendorCredit, @@ -69,11 +72,12 @@ export class EditVendorCreditService { vendorCreditDTO.entries, ); // Transformes edit DTO to model storage layer. - const vendorCreditModel = this.transformCreateEditDTOToModel( - vendorCreditDTO, - vendor.currencyCode, - oldVendorCredit, - ); + const vendorCreditModel = + this.vendorCreditDTOTransform.transformCreateEditDTOToModel( + vendorCreditDTO, + vendor.currencyCode, + oldVendorCredit, + ); // Edits the vendor credit graph under unit-of-work envirement. return this.uow.withTransaction(async (trx) => { // Triggers `onVendorCreditEditing` event. @@ -94,7 +98,6 @@ export class EditVendorCreditService { await this.eventPublisher.emitAsync(events.vendorCredit.onEdited, { oldVendorCredit, vendorCredit, - vendorCreditId, vendorCreditDTO, trx, } as IVendorCreditEditedPayload); diff --git a/packages/server-nest/src/modules/VendorCredit/commands/OpenVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCredit/commands/OpenVendorCredit.service.ts index d564f0a9b..4e576f729 100644 --- a/packages/server-nest/src/modules/VendorCredit/commands/OpenVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/commands/OpenVendorCredit.service.ts @@ -65,7 +65,7 @@ export class OpenVendorCreditService { const vendorCredit = await this.vendorCreditModel .query(trx) .findById(vendorCreditId) - .update({ + .updateAndFetchById(vendorCreditId, { openedAt: new Date(), }); // Triggers `onVendorCreditOpened` event. diff --git a/packages/server-nest/src/modules/VendorCredit/commands/VendorCreditDTOTransform.service.ts b/packages/server-nest/src/modules/VendorCredit/commands/VendorCreditDTOTransform.service.ts index f09c17370..c77012ec3 100644 --- a/packages/server-nest/src/modules/VendorCredit/commands/VendorCreditDTOTransform.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/commands/VendorCreditDTOTransform.service.ts @@ -16,7 +16,6 @@ import { VendorCreditAutoIncrementService } from './VendorCreditAutoIncrement.se import { ServiceError } from '@/modules/Items/ServiceError'; import { Injectable } from '@nestjs/common'; - @Injectable() export class VendorCreditDTOTransformService { /** @@ -29,7 +28,7 @@ export class VendorCreditDTOTransformService { private itemsEntriesService: ItemsEntriesService, private branchDTOTransform: BranchTransactionDTOTransformer, private warehouseDTOTransform: WarehouseTransactionDTOTransform, - private vendorCreditAutoIncrement: VendorCreditAutoIncrementService + private vendorCreditAutoIncrement: VendorCreditAutoIncrementService, ) {} /** @@ -42,11 +41,11 @@ export class VendorCreditDTOTransformService { public transformCreateEditDTOToModel = ( vendorCreditDTO: IVendorCreditCreateDTO | IVendorCreditEditDTO, vendorCurrencyCode: string, - oldVendorCredit?: VendorCredit + oldVendorCredit?: VendorCredit, ): VendorCredit => { // Calculates the total amount of items entries. const amount = this.itemsEntriesService.getTotalItemsEntries( - vendorCreditDTO.entries + vendorCreditDTO.entries, ); const entries = R.compose( // Associate the default index to each item entry. @@ -56,7 +55,7 @@ export class VendorCreditDTOTransformService { R.map((entry: IVendorCreditEntryDTO) => ({ referenceType: 'VendorCredit', ...entry, - })) + })), )(vendorCreditDTO.entries); // Retreive the next vendor credit number. @@ -81,8 +80,9 @@ export class VendorCreditDTOTransformService { }), }; return R.compose( + VendorCredit.fromJson, this.branchDTOTransform.transformDTO, - this.warehouseDTOTransform.transformDTO( + this.warehouseDTOTransform.transformDTO, )(initialDTO); }; @@ -93,7 +93,7 @@ export class VendorCreditDTOTransformService { */ public validateCreditRemainingAmount = ( vendorCredit: VendorCredit, - amount: number + amount: number, ) => { if (vendorCredit.creditsRemaining < amount) { throw new ServiceError(ERRORS.VENDOR_CREDIT_HAS_NO_REMAINING_AMOUNT); diff --git a/packages/server-nest/src/modules/VendorCredit/models/VendorCredit.ts b/packages/server-nest/src/modules/VendorCredit/models/VendorCredit.ts index 6ab11b68e..c15bd11e2 100644 --- a/packages/server-nest/src/modules/VendorCredit/models/VendorCredit.ts +++ b/packages/server-nest/src/modules/VendorCredit/models/VendorCredit.ts @@ -6,32 +6,52 @@ import { Model, raw, mixin } from 'objection'; // import { DEFAULT_VIEWS } from '@/services/Purchases/VendorCredits/constants'; // import ModelSearchable from './ModelSearchable'; // import VendorCreditMeta from './VendorCredit.Meta'; +// import { DiscountType } from '@/interfaces'; +import { Vendor } from '@/modules/Vendors/models/Vendor'; +import { Warehouse } from '@/modules/Warehouses/models/Warehouse.model'; +import { Branch } from '@/modules/Branches/models/Branch.model'; +import { ItemEntry } from '@/modules/Items/models/ItemEntry'; import { BaseModel } from '@/models/Model'; +import { DiscountType } from '@/common/types/Discount'; export class VendorCredit extends BaseModel { vendorId: number; amount: number; currencyCode: string; + vendorCreditDate: Date; vendorCreditNumber: string; + referenceNo: string; refundedAmount: number; invoicedAmount: number; + adjustment: number; exchangeRate: number; note: string; openedAt: Date; userId: number; + discount: number; + discountType: DiscountType; + branchId: number; warehouseId: number; + vendor?: Vendor; + warehouse?: Warehouse; + branch?: Branch; + entries?: ItemEntry[]; + attachments?: Document[]; + + createdAt: Date; + updatedAt: Date; + /** * Table name */ static get tableName() { return 'vendor_credits'; } - /** * Vendor credit amount in local currency. * @returns {number} @@ -40,6 +60,72 @@ export class VendorCredit extends BaseModel { return this.amount * this.exchangeRate; } + /** + * Vendor credit subtotal. + * @returns {number} + */ + get subtotal() { + return this.amount; + } + + /** + * Vendor credit subtotal in local currency. + * @returns {number} + */ + get subtotalLocal() { + return this.subtotal * this.exchangeRate; + } + + /** + * Discount amount. + * @returns {number} + */ + get discountAmount() { + return this.discountType === DiscountType.Amount + ? this.discount + : this.subtotal * (this.discount / 100); + } + + /** + * Discount amount in local currency. + * @returns {number | null} + */ + get discountAmountLocal() { + return this.discountAmount ? this.discountAmount * this.exchangeRate : null; + } + + /** + * Discount percentage. + * @returns {number | null} + */ + get discountPercentage(): number | null { + return this.discountType === DiscountType.Percentage ? this.discount : null; + } + + /** + * Adjustment amount in local currency. + * @returns {number | null} + */ + get adjustmentLocal() { + return this.adjustment ? this.adjustment * this.exchangeRate : null; + } + + /** + * Vendor credit total. + * @returns {number} + */ + get total() { + return this.subtotal - this.discountAmount + this.adjustment; + } + + /** + * Vendor credit total in local currency. + * @returns {number} + */ + get totalLocal() { + return this.total * this.exchangeRate; + } + /** * Model modifiers. */ @@ -127,12 +213,22 @@ export class VendorCredit extends BaseModel { */ static get virtualAttributes() { return [ - 'localAmount', 'isDraft', 'isPublished', 'isOpen', 'isClosed', + 'creditsRemaining', + 'localAmount', + + 'discountAmount', + 'discountAmountLocal', + 'discountPercentage', + + 'adjustmentLocal', + + 'total', + 'totalLocal', ]; } diff --git a/packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredit.service.ts index 24467c34c..dffb2be3f 100644 --- a/packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredit.service.ts @@ -1,13 +1,17 @@ +import { Knex } from 'knex'; import { Inject, Injectable } from '@nestjs/common'; import { VendorCreditTransformer } from './VendorCreditTransformer'; import { ERRORS } from '../constants'; import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service'; import { VendorCredit } from '../models/VendorCredit'; import { ServiceError } from '@/modules/Items/ServiceError'; -import { Knex } from 'knex'; @Injectable() -export default class GetVendorCreditService { +export class GetVendorCreditService { + /** + * @param {typeof VendorCredit} vendorCreditModel - Vendor credit model. + * @param {TransformerInjectable} transformer - Transformer service. + */ constructor( @Inject(VendorCredit.name) private readonly vendorCreditModel: typeof VendorCredit, @@ -15,20 +19,18 @@ export default class GetVendorCreditService { ) {} /** - * Retrieve the given vendor credit. + * Retrieves the given vendor credit. * @param {number} vendorCreditId - Vendor credit id. + * @param {Knex.Transaction} trx - Knex transaction. */ - public async getVendorCredit( - vendorCreditId: number, - trx?: Knex.Transaction, - ) { + public async getVendorCredit(vendorCreditId: number, trx?: Knex.Transaction) { // Retrieve the vendor credit model graph. const vendorCredit = await this.vendorCreditModel - .query() + .query(trx) .findById(vendorCreditId) .withGraphFetched('entries.item') .withGraphFetched('vendor') - .withGraphFetched('branch') + .withGraphFetched('branch') .withGraphFetched('attachments'); if (!vendorCredit) { @@ -36,7 +38,7 @@ export default class GetVendorCreditService { } return this.transformer.transform( vendorCredit, - new VendorCreditTransformer() + new VendorCreditTransformer(), ); } } diff --git a/packages/server-nest/src/modules/VendorCredit/queries/ListVendorCredits.ts b/packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredits.service.ts similarity index 100% rename from packages/server-nest/src/modules/VendorCredit/queries/ListVendorCredits.ts rename to packages/server-nest/src/modules/VendorCredit/queries/GetVendorCredits.service.ts diff --git a/packages/server-nest/src/modules/VendorCredit/queries/VendorCreditTransformer.ts b/packages/server-nest/src/modules/VendorCredit/queries/VendorCreditTransformer.ts index 6b307cdb9..39809923d 100644 --- a/packages/server-nest/src/modules/VendorCredit/queries/VendorCreditTransformer.ts +++ b/packages/server-nest/src/modules/VendorCredit/queries/VendorCreditTransformer.ts @@ -1,6 +1,7 @@ import { AttachmentTransformer } from "@/modules/Attachments/Attachment.transformer"; import { ItemEntryTransformer } from "@/modules/TransactionItemEntry/ItemEntry.transformer"; import { Transformer } from "@/modules/Transformer/Transformer"; +import { VendorCredit } from "../models/VendorCredit"; export class VendorCreditTransformer extends Transformer { /** @@ -31,28 +32,28 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieve formatted vendor credit date. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {String} */ - protected formattedVendorCreditDate = (vendorCredit): string => { + protected formattedVendorCreditDate = (vendorCredit: VendorCredit): string => { return this.formatDate(vendorCredit.vendorCreditDate); }; /** * Retireve formatted created at date. - * @param vendorCredit + * @param {VendorCredit} vendorCredit * @returns {string} */ - protected formattedCreatedAt = (vendorCredit): string => { + protected formattedCreatedAt = (vendorCredit: VendorCredit): string => { return this.formatDate(vendorCredit.createdAt); }; /** * Retrieve formatted vendor credit amount. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ - protected formattedAmount = (vendorCredit): string => { + protected formattedAmount = (vendorCredit: VendorCredit): string => { return this.formatNumber(vendorCredit.amount, { currencyCode: vendorCredit.currencyCode, }); @@ -60,19 +61,19 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the vendor credit formatted subtotal. - * @param {IVendorCredit} vendorCredit + * @param {VendorCredit} vendorCredit * @returns {string} */ - protected formattedSubtotal = (vendorCredit): string => { + protected formattedSubtotal = (vendorCredit: VendorCredit): string => { return this.formatNumber(vendorCredit.amount, { money: false }); }; /** * Retrieve formatted credits remaining. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ - protected formattedCreditsRemaining = (credit) => { + protected formattedCreditsRemaining = (credit: VendorCredit): string => { return this.formatNumber(credit.creditsRemaining, { currencyCode: credit.currencyCode, }); @@ -92,7 +93,7 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the formatted discount amount in local currency. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ protected discountAmountLocalFormatted = (credit): string => { @@ -104,7 +105,7 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the formatted discount percentage. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ protected discountPercentageFormatted = (credit): string => { @@ -113,7 +114,7 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the formatted adjustment amount. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ protected adjustmentFormatted = (credit): string => { @@ -125,10 +126,10 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the formatted adjustment amount in local currency. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ - protected adjustmentLocalFormatted = (credit): string => { + protected adjustmentLocalFormatted = (credit: VendorCredit): string => { return this.formatNumber(credit.adjustmentLocal, { currencyCode: this.context.organization.baseCurrency, excerptZero: true, @@ -140,7 +141,7 @@ export class VendorCreditTransformer extends Transformer { * @param credit * @returns {string} */ - protected formattedInvoicedAmount = (credit) => { + protected formattedInvoicedAmount = (credit: VendorCredit): string => { return this.formatNumber(credit.invoicedAmount, { currencyCode: credit.currencyCode, }); @@ -148,10 +149,10 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the formatted total. - * @param {IVendorCredit} credit + * @param {VendorCredit} credit * @returns {string} */ - protected totalFormatted = (credit) => { + protected totalFormatted = (credit: VendorCredit): string => { return this.formatNumber(credit.total, { currencyCode: credit.currencyCode, }); @@ -159,10 +160,10 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the entries of the bill. - * @param {IVendorCredit} vendorCredit + * @param {VendorCredit} vendorCredit * @returns {} */ - protected entries = (vendorCredit) => { + protected entries = (vendorCredit: VendorCredit) => { return this.item(vendorCredit.entries, new ItemEntryTransformer(), { currencyCode: vendorCredit.currencyCode, }); @@ -170,10 +171,10 @@ export class VendorCreditTransformer extends Transformer { /** * Retrieves the vendor credit attachments. - * @param {IVendorCredit} invoice + * @param {VendorCredit} invoice * @returns */ - protected attachments = (vendorCredit) => { + protected attachments = (vendorCredit: VendorCredit) => { return this.item(vendorCredit.attachments, new AttachmentTransformer()); }; } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/DeleteVendorAssociatedVendorCredit.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/DeleteVendorAssociatedVendorCredit.ts index 33f5c0377..cbda2b2b4 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/DeleteVendorAssociatedVendorCredit.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/DeleteVendorAssociatedVendorCredit.ts @@ -1,48 +1,48 @@ -import { Inject, Injectable } from '@nestjs/common'; -import { OnEvent } from '@nestjs/event-emitter'; -import { VendorCredit } from '../models/VendorCredit'; -import { IVendorEventDeletingPayload } from '@/modules/Vendors/types/Vendors.types'; -import { events } from '@/common/events/events'; -import { ServiceError } from '@/modules/Items/ServiceError'; +// import { Inject, Injectable } from '@nestjs/common'; +// import { OnEvent } from '@nestjs/event-emitter'; +// import { VendorCredit } from '../models/VendorCredit'; +// import { IVendorEventDeletingPayload } from '@/modules/Vendors/types/Vendors.types'; +// import { events } from '@/common/events/events'; +// import { ServiceError } from '@/modules/Items/ServiceError'; -const ERRORS = { - VENDOR_HAS_TRANSACTIONS: 'VENDOR_HAS_TRANSACTIONS', -}; +// const ERRORS = { +// VENDOR_HAS_TRANSACTIONS: 'VENDOR_HAS_TRANSACTIONS', +// }; -@Injectable() -export class DeleteVendorAssociatedVendorCredit { - /** - * @param {typeof VendorCredit} vendorCreditModel - Vendor credit model. - */ - constructor( - @Inject(VendorCredit.name) - private readonly vendorCreditModel: typeof VendorCredit, - ) {} +// @Injectable() +// export class DeleteVendorAssociatedVendorCredit { +// /** +// * @param {typeof VendorCredit} vendorCreditModel - Vendor credit model. +// */ +// constructor( +// @Inject(VendorCredit.name) +// private readonly vendorCreditModel: typeof VendorCredit, +// ) {} - /** - * Validate vendor has no associated credit transaction once the vendor deleting. - * @param {IVendorEventDeletingPayload} payload - - */ - @OnEvent(events.vendors.onDeleting) - public async validateVendorHasNoCreditsTransactionsOnceDeleting({ - vendorId, - }: IVendorEventDeletingPayload) { - await this.validateVendorHasNoCreditsTransactions(vendorId); - } +// /** +// * Validate vendor has no associated credit transaction once the vendor deleting. +// * @param {IVendorEventDeletingPayload} payload - +// */ +// @OnEvent(events.vendors.onDeleting) +// public async validateVendorHasNoCreditsTransactionsOnceDeleting({ +// vendorId, +// }: IVendorEventDeletingPayload) { +// await this.validateVendorHasNoCreditsTransactions(vendorId); +// } - /** - * Validate the given vendor has no associated vendor credit transactions. - * @param {number} vendorId - */ - public async validateVendorHasNoCreditsTransactions( - vendorId: number, - ): Promise { - const associatedVendors = await this.vendorCreditModel - .query() - .where('vendorId', vendorId); +// /** +// * Validate the given vendor has no associated vendor credit transactions. +// * @param {number} vendorId +// */ +// public async validateVendorHasNoCreditsTransactions( +// vendorId: number, +// ): Promise { +// const associatedVendors = await this.vendorCreditModel +// .query() +// .where('vendorId', vendorId); - if (associatedVendors.length > 0) { - throw new ServiceError(ERRORS.VENDOR_HAS_TRANSACTIONS); - } - } -} +// if (associatedVendors.length > 0) { +// throw new ServiceError(ERRORS.VENDOR_HAS_TRANSACTIONS); +// } +// } +// } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/RefundSyncVendorCreditBalanceSubscriber.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/RefundSyncVendorCreditBalanceSubscriber.ts index dbce1c3dc..c385f1819 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/RefundSyncVendorCreditBalanceSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/RefundSyncVendorCreditBalanceSubscriber.ts @@ -1,62 +1,62 @@ -import { Service, Inject } from 'typedi'; -import { - IRefundVendorCreditCreatedPayload, - IRefundVendorCreditDeletedPayload, -} from '@/interfaces'; -import events from '@/subscribers/events'; -import RefundSyncCreditRefundedAmount from './RefundSyncCreditRefundedAmount'; +// import { Service, Inject } from 'typedi'; +// import { +// IRefundVendorCreditCreatedPayload, +// IRefundVendorCreditDeletedPayload, +// } from '@/interfaces'; +// import events from '@/subscribers/events'; +// import RefundSyncCreditRefundedAmount from './RefundSyncCreditRefundedAmount'; -@Service() -export default class RefundSyncVendorCreditBalanceSubscriber { - @Inject() - refundSyncCreditRefunded: RefundSyncCreditRefundedAmount; +// @Service() +// export default class RefundSyncVendorCreditBalanceSubscriber { +// @Inject() +// refundSyncCreditRefunded: RefundSyncCreditRefundedAmount; - /** - * Attaches events with handlers. - */ - public attach = (bus) => { - bus.subscribe( - events.vendorCredit.onRefundCreated, - this.incrementRefundedAmountOnceRefundCreated - ); - bus.subscribe( - events.vendorCredit.onRefundDeleted, - this.decrementRefundedAmountOnceRefundDeleted - ); - }; +// /** +// * Attaches events with handlers. +// */ +// public attach = (bus) => { +// bus.subscribe( +// events.vendorCredit.onRefundCreated, +// this.incrementRefundedAmountOnceRefundCreated +// ); +// bus.subscribe( +// events.vendorCredit.onRefundDeleted, +// this.decrementRefundedAmountOnceRefundDeleted +// ); +// }; - /** - * Increment refunded vendor credit amount once refund transaction created. - * @param {IRefundVendorCreditCreatedPayload} payload - - */ - private incrementRefundedAmountOnceRefundCreated = async ({ - refundVendorCredit, - vendorCredit, - tenantId, - trx, - }: IRefundVendorCreditCreatedPayload) => { - await this.refundSyncCreditRefunded.incrementCreditRefundedAmount( - tenantId, - refundVendorCredit.vendorCreditId, - refundVendorCredit.amount, - trx - ); - }; +// /** +// * Increment refunded vendor credit amount once refund transaction created. +// * @param {IRefundVendorCreditCreatedPayload} payload - +// */ +// private incrementRefundedAmountOnceRefundCreated = async ({ +// refundVendorCredit, +// vendorCredit, +// tenantId, +// trx, +// }: IRefundVendorCreditCreatedPayload) => { +// await this.refundSyncCreditRefunded.incrementCreditRefundedAmount( +// tenantId, +// refundVendorCredit.vendorCreditId, +// refundVendorCredit.amount, +// trx +// ); +// }; - /** - * Decrement refunded vendor credit amount once refund transaction deleted. - * @param {IRefundVendorCreditDeletedPayload} payload - - */ - private decrementRefundedAmountOnceRefundDeleted = async ({ - trx, - oldRefundCredit, - tenantId, - }: IRefundVendorCreditDeletedPayload) => { - await this.refundSyncCreditRefunded.decrementCreditNoteRefundAmount( - tenantId, - oldRefundCredit.vendorCreditId, - oldRefundCredit.amount, - trx - ); - }; -} +// /** +// * Decrement refunded vendor credit amount once refund transaction deleted. +// * @param {IRefundVendorCreditDeletedPayload} payload - +// */ +// private decrementRefundedAmountOnceRefundDeleted = async ({ +// trx, +// oldRefundCredit, +// tenantId, +// }: IRefundVendorCreditDeletedPayload) => { +// await this.refundSyncCreditRefunded.decrementCreditNoteRefundAmount( +// tenantId, +// oldRefundCredit.vendorCreditId, +// oldRefundCredit.amount, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/RefundVendorCreditGLEntriesSubscriber.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/RefundVendorCreditGLEntriesSubscriber.ts index 6839c2af1..4e3371f05 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/RefundVendorCreditGLEntriesSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/RefundVendorCreditGLEntriesSubscriber.ts @@ -1,59 +1,59 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import RefundVendorCreditGLEntries from '../RefundVendorCredits/commands/RefundVendorCreditGLEntries'; -import { - IRefundCreditNoteDeletedPayload, - IRefundVendorCreditCreatedPayload, -} from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import RefundVendorCreditGLEntries from '../RefundVendorCredits/commands/RefundVendorCreditGLEntries'; +// import { +// IRefundCreditNoteDeletedPayload, +// IRefundVendorCreditCreatedPayload, +// } from '@/interfaces'; -@Service() -export default class RefundVendorCreditGLEntriesSubscriber { - @Inject() - refundVendorGLEntries: RefundVendorCreditGLEntries; +// @Service() +// export default class RefundVendorCreditGLEntriesSubscriber { +// @Inject() +// refundVendorGLEntries: RefundVendorCreditGLEntries; - /** - * Attaches events with handlers. - */ - attach(bus) { - bus.subscribe( - events.vendorCredit.onRefundCreated, - this.writeRefundVendorCreditGLEntriesOnceCreated - ); - bus.subscribe( - events.vendorCredit.onRefundDeleted, - this.revertRefundVendorCreditOnceDeleted - ); - } +// /** +// * Attaches events with handlers. +// */ +// attach(bus) { +// bus.subscribe( +// events.vendorCredit.onRefundCreated, +// this.writeRefundVendorCreditGLEntriesOnceCreated +// ); +// bus.subscribe( +// events.vendorCredit.onRefundDeleted, +// this.revertRefundVendorCreditOnceDeleted +// ); +// } - /** - * Writes refund vendor credit GL entries once the transaction created. - * @param {IRefundCreditNoteCreatedPayload} payload - - */ - private writeRefundVendorCreditGLEntriesOnceCreated = async ({ - tenantId, - trx, - refundVendorCredit, - }: IRefundVendorCreditCreatedPayload) => { - await this.refundVendorGLEntries.saveRefundCreditGLEntries( - tenantId, - refundVendorCredit.id, - trx - ); - }; +// /** +// * Writes refund vendor credit GL entries once the transaction created. +// * @param {IRefundCreditNoteCreatedPayload} payload - +// */ +// private writeRefundVendorCreditGLEntriesOnceCreated = async ({ +// tenantId, +// trx, +// refundVendorCredit, +// }: IRefundVendorCreditCreatedPayload) => { +// await this.refundVendorGLEntries.saveRefundCreditGLEntries( +// tenantId, +// refundVendorCredit.id, +// trx +// ); +// }; - /** - * Reverts refund vendor credit GL entries once the transaction deleted. - * @param {IRefundCreditNoteDeletedPayload} payload - - */ - private revertRefundVendorCreditOnceDeleted = async ({ - tenantId, - trx, - refundCreditId, - }: IRefundCreditNoteDeletedPayload) => { - await this.refundVendorGLEntries.revertRefundCreditGLEntries( - tenantId, - refundCreditId, - trx - ); - }; -} +// /** +// * Reverts refund vendor credit GL entries once the transaction deleted. +// * @param {IRefundCreditNoteDeletedPayload} payload - +// */ +// private revertRefundVendorCreditOnceDeleted = async ({ +// tenantId, +// trx, +// refundCreditId, +// }: IRefundCreditNoteDeletedPayload) => { +// await this.refundVendorGLEntries.revertRefundCreditGLEntries( +// tenantId, +// refundCreditId, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditAutoSerialSubscriber.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditAutoSerialSubscriber.ts index 9e2070cff..b40a33412 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditAutoSerialSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditAutoSerialSubscriber.ts @@ -1,27 +1,27 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import BaseVendorCredit from '../commands/VendorCreditDTOTransform.service'; -import { IVendorCreditCreatedPayload } from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import BaseVendorCredit from '../commands/VendorCreditDTOTransform.service'; +// import { IVendorCreditCreatedPayload } from '@/interfaces'; -@Service() -export default class VendorCreditAutoSerialSubscriber { - @Inject() - vendorCreditService: BaseVendorCredit; +// @Service() +// export default class VendorCreditAutoSerialSubscriber { +// @Inject() +// vendorCreditService: BaseVendorCredit; - /** - * Attaches events with handlers. - */ - public attach(bus) { - bus.subscribe(events.vendorCredit.onCreated, this.autoIncrementOnceCreated); - } +// /** +// * Attaches events with handlers. +// */ +// public attach(bus) { +// bus.subscribe(events.vendorCredit.onCreated, this.autoIncrementOnceCreated); +// } - /** - * Auto serial increment once the vendor credit created. - * @param {IVendorCreditCreatedPayload} payload - */ - private autoIncrementOnceCreated = ({ - tenantId, - }: IVendorCreditCreatedPayload) => { - this.vendorCreditService.incrementSerialNumber(tenantId); - }; -} +// /** +// * Auto serial increment once the vendor credit created. +// * @param {IVendorCreditCreatedPayload} payload +// */ +// private autoIncrementOnceCreated = ({ +// tenantId, +// }: IVendorCreditCreatedPayload) => { +// this.vendorCreditService.incrementSerialNumber(tenantId); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditGLEntriesSubscriber.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditGLEntriesSubscriber.ts index 250d27a53..141e8604f 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditGLEntriesSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditGLEntriesSubscriber.ts @@ -1,110 +1,110 @@ -import { Service, Inject } from 'typedi'; -import events from '@/subscribers/events'; -import { - IVendorCreditCreatedPayload, - IVendorCreditDeletedPayload, - IVendorCreditEditedPayload, - IVendorCreditOpenedPayload, -} from '@/interfaces'; -import VendorCreditGLEntries from '../commands/VendorCreditGLEntries'; +// import { Service, Inject } from 'typedi'; +// import events from '@/subscribers/events'; +// import { +// IVendorCreditCreatedPayload, +// IVendorCreditDeletedPayload, +// IVendorCreditEditedPayload, +// IVendorCreditOpenedPayload, +// } from '@/interfaces'; +// import VendorCreditGLEntries from '../commands/VendorCreditGLEntries'; -@Service() -export default class VendorCreditGlEntriesSubscriber { - @Inject() - private vendorCreditGLEntries: VendorCreditGLEntries; +// @Service() +// export default class VendorCreditGlEntriesSubscriber { +// @Inject() +// private vendorCreditGLEntries: VendorCreditGLEntries; - /*** - * Attaches events with handlers. - */ - public attach(bus) { - bus.subscribe( - events.vendorCredit.onCreated, - this.writeGLEntriesOnceVendorCreditCreated - ); - bus.subscribe( - events.vendorCredit.onOpened, - this.writeGLEntgriesOnceVendorCreditOpened - ); - bus.subscribe( - events.vendorCredit.onEdited, - this.editGLEntriesOnceVendorCreditEdited - ); - bus.subscribe( - events.vendorCredit.onDeleted, - this.revertGLEntriesOnceDeleted - ); - } +// /*** +// * Attaches events with handlers. +// */ +// public attach(bus) { +// bus.subscribe( +// events.vendorCredit.onCreated, +// this.writeGLEntriesOnceVendorCreditCreated +// ); +// bus.subscribe( +// events.vendorCredit.onOpened, +// this.writeGLEntgriesOnceVendorCreditOpened +// ); +// bus.subscribe( +// events.vendorCredit.onEdited, +// this.editGLEntriesOnceVendorCreditEdited +// ); +// bus.subscribe( +// events.vendorCredit.onDeleted, +// this.revertGLEntriesOnceDeleted +// ); +// } - /** - * Writes GL entries of vendor credit once the transaction created. - * @param {IVendorCreditCreatedPayload} payload - - */ - private writeGLEntriesOnceVendorCreditCreated = async ({ - tenantId, - vendorCredit, - trx, - }: IVendorCreditCreatedPayload): Promise => { - // Can't continue if the vendor credit is not open yet. - if (!vendorCredit.isPublished) return; +// /** +// * Writes GL entries of vendor credit once the transaction created. +// * @param {IVendorCreditCreatedPayload} payload - +// */ +// private writeGLEntriesOnceVendorCreditCreated = async ({ +// tenantId, +// vendorCredit, +// trx, +// }: IVendorCreditCreatedPayload): Promise => { +// // Can't continue if the vendor credit is not open yet. +// if (!vendorCredit.isPublished) return; - await this.vendorCreditGLEntries.writeVendorCreditGLEntries( - tenantId, - vendorCredit.id, - trx - ); - }; +// await this.vendorCreditGLEntries.writeVendorCreditGLEntries( +// tenantId, +// vendorCredit.id, +// trx +// ); +// }; - /** - * Writes Gl entries of vendor credit once the transaction opened. - * @param {IVendorCreditOpenedPayload} payload - - */ - private writeGLEntgriesOnceVendorCreditOpened = async ({ - tenantId, - vendorCreditId, - trx, - }: IVendorCreditOpenedPayload) => { - await this.vendorCreditGLEntries.writeVendorCreditGLEntries( - tenantId, - vendorCreditId, - trx - ); - }; +// /** +// * Writes Gl entries of vendor credit once the transaction opened. +// * @param {IVendorCreditOpenedPayload} payload - +// */ +// private writeGLEntgriesOnceVendorCreditOpened = async ({ +// tenantId, +// vendorCreditId, +// trx, +// }: IVendorCreditOpenedPayload) => { +// await this.vendorCreditGLEntries.writeVendorCreditGLEntries( +// tenantId, +// vendorCreditId, +// trx +// ); +// }; - /** - * Edits associated GL entries once vendor credit edited. - * @param {IVendorCreditEditedPayload} payload - */ - private editGLEntriesOnceVendorCreditEdited = async ({ - tenantId, - vendorCreditId, - vendorCredit, - trx, - }: IVendorCreditEditedPayload) => { - // Can't continue if the vendor credit is not open yet. - if (!vendorCredit.isPublished) return; +// /** +// * Edits associated GL entries once vendor credit edited. +// * @param {IVendorCreditEditedPayload} payload +// */ +// private editGLEntriesOnceVendorCreditEdited = async ({ +// tenantId, +// vendorCreditId, +// vendorCredit, +// trx, +// }: IVendorCreditEditedPayload) => { +// // Can't continue if the vendor credit is not open yet. +// if (!vendorCredit.isPublished) return; - await this.vendorCreditGLEntries.rewriteVendorCreditGLEntries( - tenantId, - vendorCreditId, - trx - ); - }; +// await this.vendorCreditGLEntries.rewriteVendorCreditGLEntries( +// tenantId, +// vendorCreditId, +// trx +// ); +// }; - /** - * Reverts the GL entries once vendor credit deleted. - * @param {IVendorCreditDeletedPayload} payload - - */ - private revertGLEntriesOnceDeleted = async ({ - vendorCreditId, - tenantId, - oldVendorCredit, - }: IVendorCreditDeletedPayload): Promise => { - // Can't continue of the vendor credit is not open yet. - if (!oldVendorCredit.isPublished) return; +// /** +// * Reverts the GL entries once vendor credit deleted. +// * @param {IVendorCreditDeletedPayload} payload - +// */ +// private revertGLEntriesOnceDeleted = async ({ +// vendorCreditId, +// tenantId, +// oldVendorCredit, +// }: IVendorCreditDeletedPayload): Promise => { +// // Can't continue of the vendor credit is not open yet. +// if (!oldVendorCredit.isPublished) return; - await this.vendorCreditGLEntries.revertVendorCreditGLEntries( - tenantId, - vendorCreditId - ); - }; -} +// await this.vendorCreditGLEntries.revertVendorCreditGLEntries( +// tenantId, +// vendorCreditId +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditInventoryTransactionsSusbcriber.ts b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditInventoryTransactionsSusbcriber.ts index 112868723..d1a797377 100644 --- a/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditInventoryTransactionsSusbcriber.ts +++ b/packages/server-nest/src/modules/VendorCredit/subscribers/VendorCreditInventoryTransactionsSusbcriber.ts @@ -1,93 +1,93 @@ -import { Inject, Service } from 'typedi'; -import events from '@/subscribers/events'; -import { - IVendorCreditCreatedPayload, - IVendorCreditDeletedPayload, - IVendorCreditEditedPayload, -} from '@/interfaces'; -import VendorCreditInventoryTransactions from '../commands/VendorCreditInventoryTransactions'; +// import { Inject, Service } from 'typedi'; +// import events from '@/subscribers/events'; +// import { +// IVendorCreditCreatedPayload, +// IVendorCreditDeletedPayload, +// IVendorCreditEditedPayload, +// } from '@/interfaces'; +// import VendorCreditInventoryTransactions from '../commands/VendorCreditInventoryTransactions'; -@Service() -export default class VendorCreditInventoryTransactionsSubscriber { - @Inject() - private inventoryTransactions: VendorCreditInventoryTransactions; +// @Service() +// export default class VendorCreditInventoryTransactionsSubscriber { +// @Inject() +// private inventoryTransactions: VendorCreditInventoryTransactions; - /** - * Attaches events with handlers. - * @param bus - */ - attach(bus) { - bus.subscribe( - events.vendorCredit.onCreated, - this.writeInventoryTransactionsOnceCreated - ); - bus.subscribe( - events.vendorCredit.onOpened, - this.writeInventoryTransactionsOnceCreated - ); - bus.subscribe( - events.vendorCredit.onEdited, - this.rewriteInventroyTransactionsOnceEdited - ); - bus.subscribe( - events.vendorCredit.onDeleted, - this.revertInventoryTransactionsOnceDeleted - ); - } +// /** +// * Attaches events with handlers. +// * @param bus +// */ +// attach(bus) { +// bus.subscribe( +// events.vendorCredit.onCreated, +// this.writeInventoryTransactionsOnceCreated +// ); +// bus.subscribe( +// events.vendorCredit.onOpened, +// this.writeInventoryTransactionsOnceCreated +// ); +// bus.subscribe( +// events.vendorCredit.onEdited, +// this.rewriteInventroyTransactionsOnceEdited +// ); +// bus.subscribe( +// events.vendorCredit.onDeleted, +// this.revertInventoryTransactionsOnceDeleted +// ); +// } - /** - * Writes inventory transactions once vendor created created. - * @param {IVendorCreditCreatedPayload} payload - - */ - private writeInventoryTransactionsOnceCreated = async ({ - tenantId, - vendorCredit, - trx, - }: IVendorCreditCreatedPayload) => { - // Can't continue if vendor credit is not opened. - if (!vendorCredit.openedAt) return null; +// /** +// * Writes inventory transactions once vendor created created. +// * @param {IVendorCreditCreatedPayload} payload - +// */ +// private writeInventoryTransactionsOnceCreated = async ({ +// tenantId, +// vendorCredit, +// trx, +// }: IVendorCreditCreatedPayload) => { +// // Can't continue if vendor credit is not opened. +// if (!vendorCredit.openedAt) return null; - await this.inventoryTransactions.createInventoryTransactions( - tenantId, - vendorCredit, - trx - ); - }; +// await this.inventoryTransactions.createInventoryTransactions( +// tenantId, +// vendorCredit, +// trx +// ); +// }; - /** - * Rewrites inventory transactions once vendor credit edited. - * @param {IVendorCreditEditedPayload} payload - - */ - private rewriteInventroyTransactionsOnceEdited = async ({ - tenantId, - vendorCreditId, - vendorCredit, - trx, - }: IVendorCreditEditedPayload) => { - // Can't continue if vendor credit is not opened. - if (!vendorCredit.openedAt) return null; +// /** +// * Rewrites inventory transactions once vendor credit edited. +// * @param {IVendorCreditEditedPayload} payload - +// */ +// private rewriteInventroyTransactionsOnceEdited = async ({ +// tenantId, +// vendorCreditId, +// vendorCredit, +// trx, +// }: IVendorCreditEditedPayload) => { +// // Can't continue if vendor credit is not opened. +// if (!vendorCredit.openedAt) return null; - await this.inventoryTransactions.editInventoryTransactions( - tenantId, - vendorCreditId, - vendorCredit, - trx - ); - }; +// await this.inventoryTransactions.editInventoryTransactions( +// tenantId, +// vendorCreditId, +// vendorCredit, +// trx +// ); +// }; - /** - * Reverts inventory transactions once vendor credit deleted. - * @param {IVendorCreditDeletedPayload} payload - - */ - private revertInventoryTransactionsOnceDeleted = async ({ - tenantId, - vendorCreditId, - trx, - }: IVendorCreditDeletedPayload) => { - await this.inventoryTransactions.deleteInventoryTransactions( - tenantId, - vendorCreditId, - trx - ); - }; -} +// /** +// * Reverts inventory transactions once vendor credit deleted. +// * @param {IVendorCreditDeletedPayload} payload - +// */ +// private revertInventoryTransactionsOnceDeleted = async ({ +// tenantId, +// vendorCreditId, +// trx, +// }: IVendorCreditDeletedPayload) => { +// await this.inventoryTransactions.deleteInventoryTransactions( +// tenantId, +// vendorCreditId, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCredit/types/VendorCredit.types.ts b/packages/server-nest/src/modules/VendorCredit/types/VendorCredit.types.ts index 97805ec1f..2ebfac076 100644 --- a/packages/server-nest/src/modules/VendorCredit/types/VendorCredit.types.ts +++ b/packages/server-nest/src/modules/VendorCredit/types/VendorCredit.types.ts @@ -1,6 +1,9 @@ -import { DiscountType, IDynamicListFilter, IItemEntry, IItemEntryDTO } from '@/interfaces'; import { Knex } from 'knex'; import { VendorCredit } from '../models/VendorCredit'; +import { AttachmentLinkDTO } from '@/modules/Attachments/Attachments.types'; +import { IRefundVendorCreditDTO } from '@/modules/VendorCreditsRefund/types/VendorCreditRefund.types'; +import { IItemEntryDTO } from '@/modules/TransactionItemEntry/ItemEntry.types'; +import { DiscountType } from '@/common/types/Discount'; export enum VendorCreditAction { Create = 'Create', @@ -10,25 +13,14 @@ export enum VendorCreditAction { Refund = 'Refund', } - export interface IVendorCreditEntryDTO extends IItemEntryDTO {} -export interface IRefundVendorCredit { - id?: number | null; - date: Date; - referenceNo: string; - amount: number; - currencyCode: string; - exchangeRate: number; - depositAccountId: number; - description: string; - vendorCreditId: number; - createdAt: Date | null; - userId: number; - branchId?: number; - - vendorCredit?: VendorCredit -} +// export interface IVendorCreditsQueryDTO extends IDynamicListFilter { +// page: number; +// pageSize: number; +// searchKeyword?: string; +// filterQuery?: (q: any) => void; +// } export interface IVendorCreditDTO { vendorId: number; @@ -53,13 +45,12 @@ export interface IVendorCreditDTO { export interface IVendorCreditCreateDTO extends IVendorCreditDTO {} export interface IVendorCreditEditDTO extends IVendorCreditDTO {} export interface IVendorCreditCreatePayload { - // tenantId: number; refundVendorCreditDTO: IRefundVendorCreditDTO; - vendorCreditId: number; } +// Create Vendor Credit Events +// ------------------------ export interface IVendorCreditCreatingPayload { - // tenantId: number; vendorCredit: VendorCredit; vendorCreditId: number; vendorCreditCreateDTO: IVendorCreditCreateDTO; @@ -74,26 +65,22 @@ export interface IVendorCreditCreatedPayload { } export interface IVendorCreditCreatedPayload {} + +// Delete Vendor Credit Events +// ------------------------ export interface IVendorCreditDeletedPayload { trx: Knex.Transaction; - // tenantId: number; vendorCreditId: number; oldVendorCredit: VendorCredit; } export interface IVendorCreditDeletingPayload { trx: Knex.Transaction; - // tenantId: number; oldVendorCredit: VendorCredit; } -export interface IVendorCreditsQueryDTO extends IDynamicListFilter { - page: number; - pageSize: number; - searchKeyword?: string; - filterQuery?: (q: any) => void; -} - +// Edit Vendor Credit Events +// ------------------------ export interface IVendorCreditEditingPayload { // tenantId: number; oldVendorCredit: VendorCredit; @@ -105,64 +92,18 @@ export interface IVendorCreditEditedPayload { // tenantId: number; oldVendorCredit: VendorCredit; vendorCredit: VendorCredit; - vendorCreditId: number; + // vendorCreditId: number; vendorCreditDTO: IVendorCreditEditDTO; trx: Knex.Transaction; } -export interface IRefundVendorCreditDTO { - amount: number; - exchangeRate?: number; - depositAccountId: number; - description: string; - date: Date; - branchId?: number; -} - -export interface IRefundVendorCreditDeletedPayload { - trx: Knex.Transaction; - refundCreditId: number; - oldRefundCredit: IRefundVendorCredit; - // tenantId: number; -} - -export interface IRefundVendorCreditDeletePayload { - trx: Knex.Transaction; - refundCreditId: number; - oldRefundCredit: IRefundVendorCredit; - // tenantId: number; -} -export interface IRefundVendorCreditDeletingPayload { - trx: Knex.Transaction; - refundCreditId: number; - oldRefundCredit: IRefundVendorCredit; - // tenantId: number; -} - -export interface IRefundVendorCreditCreatingPayload { - trx: Knex.Transaction; - vendorCredit: VendorCredit; - refundVendorCreditDTO: IRefundVendorCreditDTO; - // tenantId: number; -} - -export interface IRefundVendorCreditCreatedPayload { - refundVendorCredit: IRefundVendorCredit; - vendorCredit: VendorCredit; - trx: Knex.Transaction; - // tenantId: number; -} -export interface IRefundVendorCreditPOJO {} - export interface IApplyCreditToBillEntryDTO { amount: number; billId: number; } -export interface IApplyCreditToBillsDTO { - entries: IApplyCreditToBillEntryDTO[]; -} - +// Open Vendor Credit Events +// ------------------------ export interface IVendorCreditOpenedPayload { // tenantId: number; vendorCreditId: number; @@ -182,48 +123,3 @@ export interface IVendorCreditOpeningPayload { oldVendorCredit: VendorCredit; trx: Knex.Transaction; } - -export interface IVendorCreditApplyToBillsCreatedPayload { - // tenantId: number; - vendorCredit: VendorCredit; - vendorCreditAppliedBills: IVendorCreditAppliedBill[]; - trx: Knex.Transaction; -} -export interface IVendorCreditApplyToBillsCreatingPayload { - trx: Knex.Transaction; -} -export interface IVendorCreditApplyToBillsCreatePayload { - trx: Knex.Transaction; -} -export interface IVendorCreditApplyToBillDeletedPayload { - // tenantId: number; - vendorCredit: VendorCredit; - oldCreditAppliedToBill: IVendorCreditAppliedBill; - trx: Knex.Transaction; -} - -export interface IVendorCreditApplyToInvoiceDTO { - amount: number; - billId: number; -} - -export interface IVendorCreditApplyToInvoicesDTO { - entries: IVendorCreditApplyToInvoiceDTO[]; -} - -export interface IVendorCreditApplyToInvoiceModel { - billId: number; - amount: number; - vendorCreditId: number; -} - -export interface IVendorCreditApplyToInvoicesModel { - entries: IVendorCreditApplyToInvoiceModel[]; - amount: number; -} - -export interface IVendorCreditAppliedBill { - billId: number; - amount: number; - vendorCreditId: number; -} diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.controller.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.controller.ts new file mode 100644 index 000000000..56501e13d --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.controller.ts @@ -0,0 +1,48 @@ +import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'; +import { VendorCreditApplyBillsApplicationService } from './VendorCreditApplyBillsApplication.service'; +import { IVendorCreditApplyToInvoicesDTO } from './types/VendorCreditApplyBills.types'; + +@Controller('vendor-credits') +export class VendorCreditApplyBillsController { + constructor( + private readonly vendorCreditApplyBillsApplication: VendorCreditApplyBillsApplicationService, + ) {} + + @Get(':vendorCreditId/bills-to-apply') + async getVendorCreditToApplyBills( + @Param('vendorCreditId') vendorCreditId: number, + ) { + return this.vendorCreditApplyBillsApplication.getVendorCreditToApplyBills( + vendorCreditId, + ); + } + + @Post(':vendorCreditId/apply-to-bills') + async applyVendorCreditToBills( + @Param('vendorCreditId') vendorCreditId: number, + @Body() applyCreditToBillsDTO: IVendorCreditApplyToInvoicesDTO, + ) { + return this.vendorCreditApplyBillsApplication.applyVendorCreditToBills( + vendorCreditId, + applyCreditToBillsDTO, + ); + } + + @Delete('applied-bills/:vendorCreditAppliedBillId') + async deleteAppliedBillToVendorCredit( + @Param('vendorCreditAppliedBillId') vendorCreditAppliedBillId: number, + ) { + return this.vendorCreditApplyBillsApplication.deleteAppliedBillToVendorCredit( + vendorCreditAppliedBillId, + ); + } + + @Get(':vendorCreditId/applied-bills') + async getAppliedBillsToVendorCredit( + @Param('vendorCreditId') vendorCreditId: number, + ) { + return this.vendorCreditApplyBillsApplication.getAppliedBillsToVendorCredit( + vendorCreditId, + ); + } +} diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.module.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.module.ts index a573817c5..2f8a2b7b6 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.module.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBills.module.ts @@ -1,20 +1,38 @@ - - -import { Module } from "@nestjs/common"; -import { ApplyVendorCreditSyncBillsService } from "./command/ApplyVendorCreditSyncBills.service"; -import { ApplyVendorCreditSyncInvoicedService } from "./command/ApplyVendorCreditSyncInvoiced.service"; -import { DeleteApplyVendorCreditToBillService } from "./command/DeleteApplyVendorCreditToBill.service"; -import { ApplyVendorCreditToBillsService } from "./command/ApplyVendorCreditToBills.service"; -import { GetAppliedBillsToVendorCreditService } from "./queries/GetAppliedBillsToVendorCredit.service"; +import { Module } from '@nestjs/common'; +import { ApplyVendorCreditSyncBillsService } from './command/ApplyVendorCreditSyncBills.service'; +import { ApplyVendorCreditSyncInvoicedService } from './command/ApplyVendorCreditSyncInvoiced.service'; +import { DeleteApplyVendorCreditToBillService } from './command/DeleteApplyVendorCreditToBill.service'; +import { ApplyVendorCreditToBillsService } from './command/ApplyVendorCreditToBills.service'; +import { GetAppliedBillsToVendorCreditService } from './queries/GetAppliedBillsToVendorCredit.service'; +import { GetVendorCreditToApplyBills } from './queries/GetVendorCreditToApplyBills.service'; +import { VendorCreditApplyBillsApplicationService } from './VendorCreditApplyBillsApplication.service'; +import { VendorCreditApplyBillsController } from './VendorCreditApplyBills.controller'; +import { BillsModule } from '../Bills/Bills.module'; +import { BillPaymentsModule } from '../BillPayments/BillPayments.module'; +import { VendorCreditDTOTransformService } from '../VendorCredit/commands/VendorCreditDTOTransform.service'; +import { ItemsModule } from '../Items/items.module'; +import { BranchesModule } from '../Branches/Branches.module'; +import { WarehousesModule } from '../Warehouses/Warehouses.module'; +import { VendorCreditsModule } from '../VendorCredit/VendorCredits.module'; @Module({ imports: [ + BillsModule, + BillPaymentsModule, + ItemsModule, + BranchesModule, + WarehousesModule, + VendorCreditsModule + ], + providers: [ ApplyVendorCreditSyncBillsService, ApplyVendorCreditSyncInvoicedService, ApplyVendorCreditToBillsService, DeleteApplyVendorCreditToBillService, - GetAppliedBillsToVendorCreditService + GetAppliedBillsToVendorCreditService, + GetVendorCreditToApplyBills, + VendorCreditApplyBillsApplicationService, ], - controllers: [], + controllers: [VendorCreditApplyBillsController], }) export class VendorCreditApplyBillsModule {} diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBillsApplication.service.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBillsApplication.service.ts new file mode 100644 index 000000000..de3a65f96 --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/VendorCreditApplyBillsApplication.service.ts @@ -0,0 +1,67 @@ +import { ApplyVendorCreditToBillsService } from './command/ApplyVendorCreditToBills.service'; +import { DeleteApplyVendorCreditToBillService } from './command/DeleteApplyVendorCreditToBill.service'; +import { GetAppliedBillsToVendorCreditService } from './queries/GetAppliedBillsToVendorCredit.service'; +import { GetVendorCreditToApplyBills } from './queries/GetVendorCreditToApplyBills.service'; +import { IVendorCreditApplyToInvoicesDTO } from './types/VendorCreditApplyBills.types'; + +export class VendorCreditApplyBillsApplicationService { + /** + * @param {ApplyVendorCreditToBillsService} applyVendorCreditToBillsService + * @param {DeleteApplyVendorCreditToBillService} deleteApplyVendorCreditToBillService + * @param {GetVendorCreditToApplyBills} getVendorCreditToApplyBillsService + * @param {GetAppliedBillsToVendorCreditService} getAppliedBillsToVendorCreditService + */ + constructor( + private readonly applyVendorCreditToBillsService: ApplyVendorCreditToBillsService, + private readonly deleteApplyVendorCreditToBillService: DeleteApplyVendorCreditToBillService, + private readonly getVendorCreditToApplyBillsService: GetVendorCreditToApplyBills, + private readonly getAppliedBillsToVendorCreditService: GetAppliedBillsToVendorCreditService, + ) {} + + /** + * Retrieve bills that valid apply to the given vendor credit. + * @param {number} vendorCreditId + * @returns {Promise} + */ + async getVendorCreditToApplyBills(vendorCreditId: number) { + return this.getVendorCreditToApplyBillsService.getCreditToApplyBills( + vendorCreditId, + ); + } + + /** + * Apply credit note to the given bills. + * @param {number} vendorCreditId + * @param {IVendorCreditApplyToInvoicesDTO} applyCreditToBillsDTO + */ + async applyVendorCreditToBills( + vendorCreditId: number, + applyCreditToBillsDTO: IVendorCreditApplyToInvoicesDTO, + ) { + return this.applyVendorCreditToBillsService.applyVendorCreditToBills( + vendorCreditId, + applyCreditToBillsDTO, + ); + } + + /** + * Delete applied bill to the given vendor credit. + * @param {number} vendorCreditAppliedBillId + */ + async deleteAppliedBillToVendorCredit(vendorCreditAppliedBillId: number) { + return this.deleteApplyVendorCreditToBillService.deleteApplyVendorCreditToBills( + vendorCreditAppliedBillId, + ); + } + + /** + * Retrieve applied bills to the given vendor credit. + * @param {number} vendorCreditId + * @returns {Promise} + */ + async getAppliedBillsToVendorCredit(vendorCreditId: number) { + return this.getAppliedBillsToVendorCreditService.getAppliedBills( + vendorCreditId, + ); + } +} diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditSyncInvoiced.service.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditSyncInvoiced.service.ts index 8cab15c49..51426795a 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditSyncInvoiced.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditSyncInvoiced.service.ts @@ -4,6 +4,9 @@ import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; @Injectable() export class ApplyVendorCreditSyncInvoicedService { + /** + * @param {typeof VendorCredit} vendorCreditModel - The vendor credit model. + */ constructor( @Inject(VendorCredit.name) private readonly vendorCreditModel: typeof VendorCredit, diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditToBills.service.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditToBills.service.ts index 959a1f25f..7660b8fb7 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditToBills.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/ApplyVendorCreditToBills.service.ts @@ -14,6 +14,8 @@ import { EventEmitter2 } from '@nestjs/event-emitter'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { Bill } from '@/modules/Bills/models/Bill'; import { ServiceError } from '@/modules/Items/ServiceError'; +import { events } from '@/common/events/events'; +import { VendorCreditDTOTransformService } from '@/modules/VendorCredit/commands/VendorCreditDTOTransform.service'; @Injectable() export class ApplyVendorCreditToBillsService { @@ -28,6 +30,9 @@ export class ApplyVendorCreditToBillsService { private readonly uow: UnitOfWork, private readonly eventPublisher: EventEmitter2, private readonly billPaymentValidators: BillPaymentValidators, + private readonly vendorCreditDTOTransform: VendorCreditDTOTransformService, + + @Inject(VendorCreditAppliedBill.name) private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill, @Inject(VendorCredit.name) @@ -68,11 +73,10 @@ export class ApplyVendorCreditToBillsService { vendorCreditAppliedModel.amount, ); // Validate vendor credit remaining credit amount. - this.validateCreditRemainingAmount( + this.vendorCreditDTOTransform.validateCreditRemainingAmount( vendorCredit, vendorCreditAppliedModel.amount, ); - // Saves vendor credit applied to bills under unit-of-work envirement. return this.uow.withTransaction(async (trx: Knex.Transaction) => { // Inserts vendor credit applied to bills graph to the storage layer. diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/DeleteApplyVendorCreditToBill.service.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/DeleteApplyVendorCreditToBill.service.ts index 728030aff..85038b31a 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/command/DeleteApplyVendorCreditToBill.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/command/DeleteApplyVendorCreditToBill.service.ts @@ -19,6 +19,8 @@ export class DeleteApplyVendorCreditToBillService { constructor( private readonly uow: UnitOfWork, private readonly eventPublisher: EventEmitter2, + + @Inject(VendorCreditAppliedBill.name) private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill, @Inject(VendorCredit.name) diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/queries/GetAppliedBillsToVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/queries/GetAppliedBillsToVendorCredit.service.ts index 9083d15d1..f5ec53880 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/queries/GetAppliedBillsToVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/queries/GetAppliedBillsToVendorCredit.service.ts @@ -8,6 +8,8 @@ import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; export class GetAppliedBillsToVendorCreditService { constructor( private readonly transformer: TransformerInjectable, + + @Inject(VendorCreditAppliedBill.name) private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill, @Inject(VendorCredit.name) diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncBillsSubscriber.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncBillsSubscriber.ts index e13a588ba..cb8e6afb5 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncBillsSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncBillsSubscriber.ts @@ -1,61 +1,61 @@ -import { Injectable } from '@nestjs/common'; -import { InjectModel } from '@nestjs/objection'; -import events from '@/subscribers/events'; -import { - IVendorCreditApplyToBillDeletedPayload, - IVendorCreditApplyToBillsCreatedPayload, -} from '@/interfaces'; -import { ApplyVendorCreditSyncBillsService } from '../command/ApplyVendorCreditSyncBills.service'; -import { VendorCreditApplyToBill } from '../models/VendorCreditApplyToBill'; +// import { Injectable } from '@nestjs/common'; +// import { InjectModel } from '@nestjs/objection'; +// import events from '@/subscribers/events'; +// import { +// IVendorCreditApplyToBillDeletedPayload, +// IVendorCreditApplyToBillsCreatedPayload, +// } from '@/interfaces'; +// import { ApplyVendorCreditSyncBillsService } from '../command/ApplyVendorCreditSyncBills.service'; +// import { VendorCreditApplyToBill } from '../models/VendorCreditApplyToBill'; -@Injectable() -export default class ApplyVendorCreditSyncBillsSubscriber { - constructor( - private readonly syncBillsWithVendorCredit: ApplyVendorCreditSyncBillsService, - @InjectModel(VendorCreditApplyToBill) - private readonly vendorCreditApplyToBillModel: typeof VendorCreditApplyToBill, - ) {} +// @Injectable() +// export default class ApplyVendorCreditSyncBillsSubscriber { +// constructor( +// private readonly syncBillsWithVendorCredit: ApplyVendorCreditSyncBillsService, +// @InjectModel(VendorCreditApplyToBill) +// private readonly vendorCreditApplyToBillModel: typeof VendorCreditApplyToBill, +// ) {} - /** - * Attaches events with handlers. - */ - attach(bus) { - bus.subscribe( - events.vendorCredit.onApplyToInvoicesCreated, - this.incrementAppliedBillsOnceCreditCreated - ); - bus.subscribe( - events.vendorCredit.onApplyToInvoicesDeleted, - this.decrementAppliedBillsOnceCreditDeleted - ); - } +// /** +// * Attaches events with handlers. +// */ +// attach(bus) { +// bus.subscribe( +// events.vendorCredit.onApplyToInvoicesCreated, +// this.incrementAppliedBillsOnceCreditCreated +// ); +// bus.subscribe( +// events.vendorCredit.onApplyToInvoicesDeleted, +// this.decrementAppliedBillsOnceCreditDeleted +// ); +// } - /** - * Increment credited amount of applied bills once the vendor credit transaction created. - * @param {IVendorCreditApplyToBillsCreatedPayload} paylaod - - */ - private incrementAppliedBillsOnceCreditCreated = async ({ - vendorCreditAppliedBills, - trx, - }: IVendorCreditApplyToBillsCreatedPayload) => { - await this.syncBillsWithVendorCredit.incrementBillsCreditedAmount( - vendorCreditAppliedBills, - trx - ); - }; +// /** +// * Increment credited amount of applied bills once the vendor credit transaction created. +// * @param {IVendorCreditApplyToBillsCreatedPayload} paylaod - +// */ +// private incrementAppliedBillsOnceCreditCreated = async ({ +// vendorCreditAppliedBills, +// trx, +// }: IVendorCreditApplyToBillsCreatedPayload) => { +// await this.syncBillsWithVendorCredit.incrementBillsCreditedAmount( +// vendorCreditAppliedBills, +// trx +// ); +// }; - /** - * Decrement credited amount of applied bills once the vendor credit - * transaction delted. - * @param {IVendorCreditApplyToBillDeletedPayload} payload - */ - private decrementAppliedBillsOnceCreditDeleted = async ({ - oldCreditAppliedToBill, - trx, - }: IVendorCreditApplyToBillDeletedPayload) => { - await this.syncBillsWithVendorCredit.decrementBillCreditedAmount( - oldCreditAppliedToBill, - trx - ); - }; -} +// /** +// * Decrement credited amount of applied bills once the vendor credit +// * transaction delted. +// * @param {IVendorCreditApplyToBillDeletedPayload} payload +// */ +// private decrementAppliedBillsOnceCreditDeleted = async ({ +// oldCreditAppliedToBill, +// trx, +// }: IVendorCreditApplyToBillDeletedPayload) => { +// await this.syncBillsWithVendorCredit.decrementBillCreditedAmount( +// oldCreditAppliedToBill, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncInvoicedSubscriber.ts b/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncInvoicedSubscriber.ts index 2936f864a..58db5b984 100644 --- a/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncInvoicedSubscriber.ts +++ b/packages/server-nest/src/modules/VendorCreditsApplyBills/subscribers/ApplyVendorCreditSyncInvoicedSubscriber.ts @@ -1,70 +1,70 @@ -import { Service, Inject } from 'typedi'; -import { sumBy } from 'lodash'; -import HasTenancyService from '@/services/Tenancy/TenancyService'; -import ApplyVendorCreditSyncInvoiced from '../command/ApplyVendorCreditSyncInvoiced.service'; -import events from '@/subscribers/events'; -import { - IVendorCreditApplyToBillDeletedPayload, - IVendorCreditApplyToBillsCreatedPayload, -} from '@/interfaces'; +// import { Service, Inject } from 'typedi'; +// import { sumBy } from 'lodash'; +// import HasTenancyService from '@/services/Tenancy/TenancyService'; +// import ApplyVendorCreditSyncInvoiced from '../command/ApplyVendorCreditSyncInvoiced.service'; +// import events from '@/subscribers/events'; +// import { +// IVendorCreditApplyToBillDeletedPayload, +// IVendorCreditApplyToBillsCreatedPayload, +// } from '../types/VendorCreditApplyBills.types'; -@Service() -export default class ApplyVendorCreditSyncInvoicedSubscriber { - @Inject() - tenancy: HasTenancyService; +// @Service() +// export default class ApplyVendorCreditSyncInvoicedSubscriber { +// @Inject() +// tenancy: HasTenancyService; - @Inject() - syncCreditWithInvoiced: ApplyVendorCreditSyncInvoiced; +// @Inject() +// syncCreditWithInvoiced: ApplyVendorCreditSyncInvoiced; - /** - * Attaches events with handlers. - */ - attach(bus) { - bus.subscribe( - events.vendorCredit.onApplyToInvoicesCreated, - this.incrementBillInvoicedOnceCreditApplied - ); - bus.subscribe( - events.vendorCredit.onApplyToInvoicesDeleted, - this.decrementBillInvoicedOnceCreditApplyDeleted - ); - } +// /** +// * Attaches events with handlers. +// */ +// attach(bus) { +// bus.subscribe( +// events.vendorCredit.onApplyToInvoicesCreated, +// this.incrementBillInvoicedOnceCreditApplied +// ); +// bus.subscribe( +// events.vendorCredit.onApplyToInvoicesDeleted, +// this.decrementBillInvoicedOnceCreditApplyDeleted +// ); +// } - /** - * Increment vendor credit invoiced amount once the apply transaction created. - * @param {IVendorCreditApplyToBillsCreatedPayload} payload - - */ - private incrementBillInvoicedOnceCreditApplied = async ({ - vendorCredit, - tenantId, - vendorCreditAppliedBills, - trx, - }: IVendorCreditApplyToBillsCreatedPayload) => { - const amount = sumBy(vendorCreditAppliedBills, 'amount'); +// /** +// * Increment vendor credit invoiced amount once the apply transaction created. +// * @param {IVendorCreditApplyToBillsCreatedPayload} payload - +// */ +// private incrementBillInvoicedOnceCreditApplied = async ({ +// vendorCredit, +// tenantId, +// vendorCreditAppliedBills, +// trx, +// }: IVendorCreditApplyToBillsCreatedPayload) => { +// const amount = sumBy(vendorCreditAppliedBills, 'amount'); - await this.syncCreditWithInvoiced.incrementVendorCreditInvoicedAmount( - tenantId, - vendorCredit.id, - amount, - trx - ); - }; +// await this.syncCreditWithInvoiced.incrementVendorCreditInvoicedAmount( +// tenantId, +// vendorCredit.id, +// amount, +// trx +// ); +// }; - /** - * Decrement vendor credit invoiced amount once the apply transaction deleted. - * @param {IVendorCreditApplyToBillDeletedPayload} payload - - */ - private decrementBillInvoicedOnceCreditApplyDeleted = async ({ - tenantId, - vendorCredit, - oldCreditAppliedToBill, - trx, - }: IVendorCreditApplyToBillDeletedPayload) => { - await this.syncCreditWithInvoiced.decrementVendorCreditInvoicedAmount( - tenantId, - oldCreditAppliedToBill.vendorCreditId, - oldCreditAppliedToBill.amount, - trx - ); - }; -} +// /** +// * Decrement vendor credit invoiced amount once the apply transaction deleted. +// * @param {IVendorCreditApplyToBillDeletedPayload} payload - +// */ +// private decrementBillInvoicedOnceCreditApplyDeleted = async ({ +// tenantId, +// vendorCredit, +// oldCreditAppliedToBill, +// trx, +// }: IVendorCreditApplyToBillDeletedPayload) => { +// await this.syncCreditWithInvoiced.decrementVendorCreditInvoicedAmount( +// tenantId, +// oldCreditAppliedToBill.vendorCreditId, +// oldCreditAppliedToBill.amount, +// trx +// ); +// }; +// } diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.application.ts b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.application.ts new file mode 100644 index 000000000..c86f81beb --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.application.ts @@ -0,0 +1,44 @@ +import { Injectable } from '@nestjs/common'; +import { DeleteRefundVendorCreditService } from './commands/DeleteRefundVendorCredit.service'; +import { RefundVendorCredit } from './models/RefundVendorCredit'; +import { CreateRefundVendorCredit } from './commands/CreateRefundVendorCredit.service'; +import { IRefundVendorCreditDTO } from './types/VendorCreditRefund.types'; + +@Injectable() +export class VendorCreditsRefundApplication { + /** + * @param {CreateRefundVendorCredit} createRefundVendorCreditService + * @param {DeleteRefundVendorCreditService} deleteRefundVendorCreditService + */ + constructor( + private readonly createRefundVendorCreditService: CreateRefundVendorCredit, + private readonly deleteRefundVendorCreditService: DeleteRefundVendorCreditService, + ) {} + + /** + * Creates a refund vendor credit. + * @param {number} vendorCreditId + * @param {IRefundVendorCreditDTO} refundVendorCreditDTO + * @returns {Promise} + */ + public async createRefundVendorCredit( + vendorCreditId: number, + refundVendorCreditDTO: IRefundVendorCreditDTO, + ): Promise { + return this.createRefundVendorCreditService.createRefund( + vendorCreditId, + refundVendorCreditDTO, + ); + } + + /** + * Deletes a refund vendor credit. + * @param {number} refundCreditId + * @returns {Promise} + */ + public async deleteRefundVendorCredit(refundCreditId: number): Promise { + return this.deleteRefundVendorCreditService.deleteRefundVendorCreditRefund( + refundCreditId, + ); + } +} diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.controller.ts b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.controller.ts new file mode 100644 index 000000000..4ec0c99e5 --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.controller.ts @@ -0,0 +1,44 @@ + + +import { Body, Controller, Delete, Param, Post } from '@nestjs/common'; +import { VendorCreditsRefundApplication } from './VendorCreditsRefund.application'; +import { IRefundVendorCreditDTO } from './types/VendorCreditRefund.types'; +import { RefundVendorCredit } from './models/RefundVendorCredit'; + +@Controller('vendor-credits') +export class VendorCreditsRefundController { + constructor( + private readonly vendorCreditsRefundApplication: VendorCreditsRefundApplication, + ) {} + + /** + * Creates a refund vendor credit. + * @param {number} vendorCreditId + * @param {IRefundVendorCreditDTO} refundVendorCreditDTO + * @returns {Promise} + */ + @Post(':vendorCreditId/refunds') + public async createRefundVendorCredit( + @Param('vendorCreditId') vendorCreditId: string, + @Body() refundVendorCreditDTO: IRefundVendorCreditDTO, + ): Promise { + return this.vendorCreditsRefundApplication.createRefundVendorCredit( + Number(vendorCreditId), + refundVendorCreditDTO, + ); + } + + /** + * Deletes a refund vendor credit. + * @param {number} refundCreditId + * @returns {Promise} + */ + @Delete('refunds/:refundCreditId') + public async deleteRefundVendorCredit( + @Param('refundCreditId') refundCreditId: string, + ): Promise { + return this.vendorCreditsRefundApplication.deleteRefundVendorCredit( + Number(refundCreditId), + ); + } +} diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.module.ts b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.module.ts new file mode 100644 index 000000000..b9f614a59 --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsRefund/VendorCreditsRefund.module.ts @@ -0,0 +1,20 @@ +import { Module } from '@nestjs/common'; +import { GetRefundVendorCreditsService } from './queries/GetRefundVendorCredits.service'; +import { DeleteRefundVendorCreditService } from './commands/DeleteRefundVendorCredit.service'; +import { VendorCreditsRefundController } from './VendorCreditsRefund.controller'; +import { VendorCreditsRefundApplication } from './VendorCreditsRefund.application'; +import { CreateRefundVendorCredit } from './commands/CreateRefundVendorCredit.service'; +import { WarehousesModule } from '../Warehouses/Warehouses.module'; +import { BranchesModule } from '../Branches/Branches.module'; + +@Module({ + imports: [WarehousesModule, BranchesModule], + providers: [ + GetRefundVendorCreditsService, + DeleteRefundVendorCreditService, + CreateRefundVendorCredit, + VendorCreditsRefundApplication + ], + controllers: [VendorCreditsRefundController], +}) +export class VendorCreditsRefundModule {} diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/commands/CreateRefundVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/commands/CreateRefundVendorCredit.service.ts index a07a74e1a..55be7501a 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/commands/CreateRefundVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/commands/CreateRefundVendorCredit.service.ts @@ -3,21 +3,22 @@ import { Knex } from 'knex'; import * as R from 'ramda'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { - IRefundVendorCredit, IRefundVendorCreditCreatedPayload, IRefundVendorCreditCreatingPayload, IRefundVendorCreditDTO, - IVendorCreditCreatePayload, -} from '@/modules/VendorCredit/types/VendorCredit.types'; +} from '../types/VendorCreditRefund.types'; import { Account } from '@/modules/Accounts/models/Account.model'; -import { VendorCredit } from '../../models/VendorCredit'; +import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; -import { RefundVendorCredit } from '../../models/RefundVendorCredit'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; import { BranchTransactionDTOTransformer } from '@/modules/Branches/integrations/BranchTransactionDTOTransform'; +import { IVendorCreditCreatePayload } from '@/modules/VendorCredit/types/VendorCredit.types'; import { events } from '@/common/events/events'; +import { ServiceError } from '@/modules/Items/ServiceError'; +import { ERRORS } from '../constants'; @Injectable() -export class CreateRefundVendorCredit { +export class CreateRefundVendorCredit { constructor( private readonly uow: UnitOfWork, private readonly eventPublisher: EventEmitter2, @@ -31,8 +32,7 @@ export class CreateRefundVendorCredit { @Inject(VendorCredit.name) private readonly vendorCreditModel: typeof VendorCredit, - ) { - } + ) {} /** * Creates a refund vendor credit. @@ -42,22 +42,24 @@ export class CreateRefundVendorCredit { */ public createRefund = async ( vendorCreditId: number, - refundVendorCreditDTO: IRefundVendorCreditDTO - ): Promise => { + refundVendorCreditDTO: IRefundVendorCreditDTO, + ): Promise => { // Retrieve the vendor credit or throw not found service error. - const vendorCredit = await this.vendorCreditModel.query() + const vendorCredit = await this.vendorCreditModel + .query() .findById(vendorCreditId) .throwIfNotFound(); // Retrieve the deposit account or throw not found service error. - const depositAccount = await this.accountModel.query() + const depositAccount = await this.accountModel + .query() .findById(refundVendorCreditDTO.depositAccountId) .throwIfNotFound(); // Validate vendor credit has remaining credit. this.validateVendorCreditRemainingCredit( vendorCredit, - refundVendorCreditDTO.amount + refundVendorCreditDTO.amount, ); // Validate refund deposit account type. this.validateRefundDepositAccountType(depositAccount); @@ -70,7 +72,7 @@ export class CreateRefundVendorCredit { const refundCreditObj = this.transformDTOToModel( vendorCredit, - refundVendorCreditDTO + refundVendorCreditDTO, ); // Saves refund vendor credit with associated transactions. return this.uow.withTransaction(async (trx: Knex.Transaction) => { @@ -83,12 +85,13 @@ export class CreateRefundVendorCredit { // Triggers `onVendorCreditRefundCreating` event. await this.eventPublisher.emitAsync( events.vendorCredit.onRefundCreating, - eventPayload as IRefundVendorCreditCreatingPayload + eventPayload as IRefundVendorCreditCreatingPayload, ); // Inserts refund vendor credit to the storage layer. - const refundVendorCredit = - await this.refundVendorCreditModel.query().insertAndFetch(refundCreditObj); - + const refundVendorCredit = await this.refundVendorCreditModel + .query() + .insertAndFetch(refundCreditObj); + // Triggers `onVendorCreditCreated` event. await this.eventPublisher.emitAsync(events.vendorCredit.onRefundCreated, { ...eventPayload, @@ -101,13 +104,13 @@ export class CreateRefundVendorCredit { /** * Transformes the refund DTO to refund vendor credit model. - * @param {IVendorCredit} vendorCredit - - * @param {IRefundVendorCreditDTO} vendorCreditDTO + * @param {IVendorCredit} vendorCredit - + * @param {IRefundVendorCreditDTO} vendorCreditDTO * @returns {IRefundVendorCredit} */ public transformDTOToModel = ( vendorCredit: VendorCredit, - vendorCreditDTO: IRefundVendorCreditDTO + vendorCreditDTO: IRefundVendorCreditDTO, ) => { const initialDTO = { vendorCreditId: vendorCredit.id, @@ -115,8 +118,32 @@ export class CreateRefundVendorCredit { currencyCode: vendorCredit.currencyCode, exchangeRate: vendorCreditDTO.exchangeRate || 1, }; - return R.compose(this.branchDTOTransform.transformDTO())( - initialDTO - ); + return R.compose(this.branchDTOTransform.transformDTO)(initialDTO); }; + + /** + * Validate the deposit refund account type. + * @param {IAccount} account + */ + public validateRefundDepositAccountType(account: Account) { + const supportedTypes = ['bank', 'cash', 'fixed-asset']; + + if (supportedTypes.indexOf(account.accountType) === -1) { + throw new ServiceError(ERRORS.DEPOSIT_ACCOUNT_INVALID_TYPE); + } + } + + /** + * Validate vendor credit has remaining credits. + * @param {IVendorCredit} vendorCredit + * @param {number} amount + */ + public validateVendorCreditRemainingCredit( + vendorCredit: VendorCredit, + amount: number, + ) { + if (vendorCredit.creditsRemaining < amount) { + throw new ServiceError(ERRORS.VENDOR_CREDIT_HAS_NO_CREDITS_REMAINING); + } + } } diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/commands/DeleteRefundVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/commands/DeleteRefundVendorCredit.service.ts index 769825279..67437f450 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/commands/DeleteRefundVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/commands/DeleteRefundVendorCredit.service.ts @@ -1,12 +1,12 @@ import { Knex } from 'knex'; +import { Inject, Injectable } from '@nestjs/common'; import { IRefundVendorCreditDeletedPayload, IRefundVendorCreditDeletePayload, IRefundVendorCreditDeletingPayload, -} from '@/modules/VendorCredit/types/VendorCredit.types'; -import { Inject, Injectable } from '@nestjs/common'; -import { RefundVendorCredit } from '../../models/RefundVendorCredit'; -import { RefundVendorCreditService } from './RefundVendorCredit.service'; +} from '../types/VendorCreditRefund.types'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; +// import { RefundVendorCreditService } from './RefundVendorCredit.service'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { events } from '@/common/events/events'; @@ -24,8 +24,7 @@ export class DeleteRefundVendorCreditService { @Inject(RefundVendorCredit.name) private readonly refundVendorCreditModel: typeof RefundVendorCredit, - ) { - } + ) {} /** * Deletes the refund vendor credit. @@ -33,12 +32,14 @@ export class DeleteRefundVendorCreditService { * @returns {Promise} */ public async deleteRefundVendorCreditRefund( - refundCreditId: number + refundCreditId: number, ): Promise { // Retrieve the old credit note or throw not found service error. - const oldRefundCredit = await this.getRefundVendorCreditOrThrowError( - refundCreditId - ); + const oldRefundCredit = await this.refundVendorCreditModel + .query() + .findById(refundCreditId) + .throwIfNotFound(); + // Triggers `onVendorCreditRefundDelete` event. await this.eventPublisher.emitAsync(events.vendorCredit.onRefundDelete, { refundCreditId, @@ -56,7 +57,7 @@ export class DeleteRefundVendorCreditService { // Triggers `onVendorCreditRefundDeleting` event. await this.eventPublisher.emitAsync( events.vendorCredit.onRefundDeleting, - eventPayload + eventPayload, ); // Deletes the refund vendor credit graph from the storage. await this.refundVendorCreditModel @@ -67,7 +68,7 @@ export class DeleteRefundVendorCreditService { // Triggers `onVendorCreditRefundDeleted` event. await this.eventPublisher.emitAsync( events.vendorCredit.onRefundDeleted, - eventPayload as IRefundVendorCreditDeletedPayload + eventPayload as IRefundVendorCreditDeletedPayload, ); }); } diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundSyncVendorCreditBalance.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundSyncVendorCreditBalance.service.ts index d3a12110f..f138b22ec 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundSyncVendorCreditBalance.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundSyncVendorCreditBalance.service.ts @@ -1,7 +1,7 @@ import { Knex } from 'knex'; import { Inject, Injectable } from '@nestjs/common'; -import { IRefundVendorCredit } from '../../types/VendorCredit.types'; -import { VendorCredit } from '../../models/VendorCredit'; +import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; @Injectable() export class RefundSyncVendorCreditBalance { @@ -12,11 +12,11 @@ export class RefundSyncVendorCreditBalance { /** * Increment vendor credit refunded amount. - * @param {IRefundVendorCredit} refundCreditNote - + * @param {RefundVendorCredit} refundCreditNote - * @param {Knex.Transaction} trx - */ public async incrementVendorCreditRefundAmount( - refundVendorCredit: IRefundVendorCredit, + refundVendorCredit: RefundVendorCredit, trx?: Knex.Transaction ): Promise { await this.vendorCreditModel @@ -26,11 +26,11 @@ export class RefundSyncVendorCreditBalance { /** * Decrement vendor credit refunded amount. - * @param {IRefundVendorCredit} refundCreditNote + * @param {RefundVendorCredit} refundCreditNote * @param {Knex.Transaction} trx */ public async decrementVendorCreditRefundAmount( - refundVendorCredit: IRefundVendorCredit, + refundVendorCredit: RefundVendorCredit, trx?: Knex.Transaction ): Promise { await this.vendorCreditModel diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundVendorCredit.service.ts index 70235fc24..1db412eff 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/commands/RefundVendorCredit.service.ts @@ -1,8 +1,8 @@ import { Inject, Injectable } from '@nestjs/common'; import { ERRORS } from '../constants'; -import { RefundVendorCredit } from '../../models/RefundVendorCredit'; import { Account } from '@/modules/Accounts/models/Account.model'; -import { VendorCredit } from '../../models/VendorCredit'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; +import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; import { ServiceError } from '@/modules/Items/ServiceError'; @Injectable() diff --git a/packages/server-nest/src/modules/VendorCredit/models/RefundVendorCredit.ts b/packages/server-nest/src/modules/VendorCreditsRefund/models/RefundVendorCredit.ts similarity index 95% rename from packages/server-nest/src/modules/VendorCredit/models/RefundVendorCredit.ts rename to packages/server-nest/src/modules/VendorCreditsRefund/models/RefundVendorCredit.ts index ae9067a04..5d7a16760 100644 --- a/packages/server-nest/src/modules/VendorCredit/models/RefundVendorCredit.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/models/RefundVendorCredit.ts @@ -5,7 +5,7 @@ import { Model, mixin } from 'objection'; // import ModelSearchable from './ModelSearchable'; import { BaseModel } from '@/models/Model'; import { Account } from '@/modules/Accounts/models/Account.model'; -import { VendorCredit } from './VendorCredit'; +import { VendorCredit } from '../../VendorCredit/models/VendorCredit'; export class RefundVendorCredit extends BaseModel { public vendorCreditId!: number; diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredit.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredit.service.ts index ed808d1b3..078a64f3f 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredit.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredit.service.ts @@ -1,6 +1,9 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { RefundVendorCreditTransformer } from '../commands/RefundVendorCreditTransformer'; -import { RefundVendorCredit, RefundVendorCredit as RefundVendorCreditModel } from '../../models/RefundVendorCredit'; +import { + RefundVendorCredit, + RefundVendorCredit as RefundVendorCreditModel, +} from '../models/RefundVendorCredit'; import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service'; @Injectable() @@ -11,9 +14,10 @@ export class GetRefundVendorCreditService { */ constructor( private readonly transformer: TransformerInjectable, + + @Inject(RefundVendorCredit.name) private readonly refundVendorCreditModel: typeof RefundVendorCreditModel, - ) { - } + ) {} /** * Retrieve refund vendor credit transaction. diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredits.service.ts b/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredits.service.ts index 77c4751fa..bdf1106d8 100644 --- a/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredits.service.ts +++ b/packages/server-nest/src/modules/VendorCreditsRefund/queries/GetRefundVendorCredits.service.ts @@ -1,8 +1,8 @@ +import { Inject, Injectable } from '@nestjs/common'; import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service'; import { RefundVendorCreditTransformer } from '../commands/RefundVendorCreditTransformer'; -import { Injectable } from '@nestjs/common'; -import { RefundVendorCredit } from '../../models/RefundVendorCredit'; -import { IRefundVendorCreditPOJO } from '../../types/VendorCredit.types'; +import { IRefundVendorCreditPOJO } from '../types/VendorCreditRefund.types'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; @Injectable() export class GetRefundVendorCreditsService { @@ -12,6 +12,8 @@ export class GetRefundVendorCreditsService { */ constructor( private readonly transformer: TransformerInjectable, + + @Inject(RefundVendorCredit.name) private readonly refundVendorCreditModel: typeof RefundVendorCredit, ) {} diff --git a/packages/server-nest/src/modules/VendorCreditsRefund/types/VendorCreditRefund.types.ts b/packages/server-nest/src/modules/VendorCreditsRefund/types/VendorCreditRefund.types.ts new file mode 100644 index 000000000..2b1c740ca --- /dev/null +++ b/packages/server-nest/src/modules/VendorCreditsRefund/types/VendorCreditRefund.types.ts @@ -0,0 +1,44 @@ +import { Knex } from 'knex'; +import { RefundVendorCredit } from '../models/RefundVendorCredit'; +import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit'; + +export interface IRefundVendorCreditDTO { + amount: number; + exchangeRate?: number; + depositAccountId: number; + description: string; + date: Date; + branchId?: number; +} + +export interface IRefundVendorCreditDeletedPayload { + trx: Knex.Transaction; + refundCreditId: number; + oldRefundCredit: RefundVendorCredit; +} + +export interface IRefundVendorCreditDeletePayload { + trx: Knex.Transaction; + refundCreditId: number; + oldRefundCredit: RefundVendorCredit; +} + +export interface IRefundVendorCreditDeletingPayload { + trx: Knex.Transaction; + refundCreditId: number; + oldRefundCredit: RefundVendorCredit; +} + +export interface IRefundVendorCreditCreatingPayload { + trx: Knex.Transaction; + vendorCredit: VendorCredit; + refundVendorCreditDTO: IRefundVendorCreditDTO; +} + +export interface IRefundVendorCreditCreatedPayload { + refundVendorCredit: RefundVendorCredit; + vendorCredit: VendorCredit; + trx: Knex.Transaction; +} + +export interface IRefundVendorCreditPOJO {}