mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
refactor: credit notes and vendor credits to Nestjs
This commit is contained in:
@@ -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<ICreditNote>,
|
||||
this.warehouseDTOTransform.transformDTO<ICreditNote>,
|
||||
)(initialAsyncDTO);
|
||||
this.branchDTOTransform.transformDTO<CreditNote>,
|
||||
this.warehouseDTOTransform.transformDTO<CreditNote>,
|
||||
)(initialAsyncDTO) as CreditNote;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICreditNoteRefundDTO,
|
||||
IRefundCreditNoteCreatedPayload,
|
||||
IRefundCreditNoteCreatingPayload,
|
||||
} from '../types/CreditNotes.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 { events } from '@/common/events/events';
|
||||
import { CommandCreditNoteDTOTransform } from './CommandCreditNoteDTOTransform.service';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
|
||||
@Injectable()
|
||||
export class CreateRefundCreditNoteService {
|
||||
/**
|
||||
* @param {UnitOfWork} uow - The unit of work service.
|
||||
* @param {EventEmitter2} eventPublisher - The event emitter service.
|
||||
* @param {CommandCreditNoteDTOTransform} commandCreditNoteDTOTransform - The command credit note DTO transform service.
|
||||
* @param {typeof RefundCreditNote} refundCreditNoteModel - The refund credit note model.
|
||||
* @param {typeof Account} accountModel - The account model.
|
||||
* @param {typeof CreditNote} creditNoteModel - The credit note model.
|
||||
*/
|
||||
constructor(
|
||||
private uow: UnitOfWork,
|
||||
private eventPublisher: EventEmitter2,
|
||||
private commandCreditNoteDTOTransform: CommandCreditNoteDTOTransform,
|
||||
|
||||
@Inject(RefundCreditNote.name)
|
||||
private refundCreditNoteModel: typeof RefundCreditNote,
|
||||
|
||||
@Inject(Account.name) private accountModel: typeof Account,
|
||||
@Inject(CreditNote.name) private creditNoteModel: typeof CreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} creditNoteId
|
||||
* @param {ICreditNoteRefundDTO} newCreditNoteDTO
|
||||
* @returns {Promise<IRefundCreditNote>}
|
||||
*/
|
||||
public async createCreditNoteRefund(
|
||||
creditNoteId: number,
|
||||
newCreditNoteDTO: ICreditNoteRefundDTO,
|
||||
): Promise<RefundCreditNote> {
|
||||
// Retrieve the credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Retrieve the withdrawal account or throw not found service error.
|
||||
const fromAccount = await this.accountModel
|
||||
.query()
|
||||
.findById(newCreditNoteDTO.fromAccountId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate the credit note remaining amount.
|
||||
this.commandCreditNoteDTOTransform?.validateCreditRemainingAmount(
|
||||
creditNote,
|
||||
newCreditNoteDTO.amount,
|
||||
);
|
||||
// Validate the refund withdrawal account type.
|
||||
this.commandCreditNoteDTOTransform.validateRefundWithdrawwalAccountType(
|
||||
fromAccount,
|
||||
);
|
||||
// Creates a refund credit note transaction.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCreditNoteRefundCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundCreating, {
|
||||
trx,
|
||||
creditNote,
|
||||
newCreditNoteDTO,
|
||||
} as IRefundCreditNoteCreatingPayload);
|
||||
|
||||
// Stores the refund credit note graph to the storage layer.
|
||||
const refundCreditNote = await this.refundCreditNoteModel
|
||||
.query(trx)
|
||||
.insertAndFetch({
|
||||
...this.transformDTOToModel(creditNote, newCreditNoteDTO),
|
||||
});
|
||||
|
||||
// Triggers `onCreditNoteRefundCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundCreated, {
|
||||
trx,
|
||||
refundCreditNote,
|
||||
creditNote,
|
||||
} as IRefundCreditNoteCreatedPayload);
|
||||
|
||||
return refundCreditNote;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transformes the refund credit note DTO to model.
|
||||
* @param {number} creditNoteId
|
||||
* @param {ICreditNoteRefundDTO} creditNoteDTO
|
||||
* @returns {ICreditNote}
|
||||
*/
|
||||
private transformDTOToModel = (
|
||||
creditNote: CreditNote,
|
||||
creditNoteDTO: ICreditNoteRefundDTO,
|
||||
): RefundCreditNote => {
|
||||
return {
|
||||
creditNoteId: creditNote.id,
|
||||
currencyCode: creditNote.currencyCode,
|
||||
...creditNoteDTO,
|
||||
exchangeRate: creditNoteDTO.exchangeRate || 1,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplySyncCredit {
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private creditNoteModel: typeof CreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Increment credit note invoiced amount.
|
||||
* @param {number} creditNoteId
|
||||
* @param {number} invoicesAppliedAmount
|
||||
* @param {Knex.Transaction} [trx]
|
||||
*/
|
||||
public async incrementCreditNoteInvoicedAmount(
|
||||
creditNoteId: number,
|
||||
invoicesAppliedAmount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.creditNoteModel
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.increment('invoicesAmount', invoicesAppliedAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement credit note invoiced amount.
|
||||
* @param {number} creditNoteId
|
||||
* @param {number} invoicesAppliedAmount
|
||||
* @param {Knex.Transaction} [trx]
|
||||
*/
|
||||
public async decrementCreditNoteInvoicedAmount(
|
||||
creditNoteId: number,
|
||||
invoicesAppliedAmount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.creditNoteModel
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.decrement('invoicesAmount', invoicesAppliedAmount);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Injectable, Inject } from '@nestjs/common';
|
||||
import Bluebird from 'bluebird';
|
||||
import { ICreditNoteAppliedToInvoice } from '../types/CreditNotes.types';
|
||||
import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplySyncInvoicesCreditedAmount {
|
||||
constructor(
|
||||
@Inject(SaleInvoice.name)
|
||||
private readonly saleInvoiceModel: typeof SaleInvoice,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Increment invoices credited amount.
|
||||
* @param {ICreditNoteAppliedToInvoice[]} creditNoteAppliedInvoices -
|
||||
* @param {Knex.Transaction} trx -
|
||||
*/
|
||||
public incrementInvoicesCreditedAmount = async (
|
||||
creditNoteAppliedInvoices: ICreditNoteAppliedToInvoice[],
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
await Bluebird.each(
|
||||
creditNoteAppliedInvoices,
|
||||
(creditNoteAppliedInvoice: ICreditNoteAppliedToInvoice) => {
|
||||
return this.saleInvoiceModel.query(trx)
|
||||
.where('id', creditNoteAppliedInvoice.invoiceId)
|
||||
.increment('creditedAmount', creditNoteAppliedInvoice.amount);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} invoicesIds
|
||||
* @param {number} amount -
|
||||
* @param {Knex.Transaction} knex -
|
||||
*/
|
||||
public decrementInvoiceCreditedAmount = async (
|
||||
invoiceId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
await this.saleInvoiceModel.query(trx)
|
||||
.findById(invoiceId)
|
||||
.decrement('creditedAmount', amount);
|
||||
};
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
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';
|
||||
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';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplyToInvoices {
|
||||
/**
|
||||
* @param {PaymentReceivedValidators} paymentReceiveValidators - The payment received validators service.
|
||||
* @param {UnitOfWork} uow - The unit of work service.
|
||||
* @param {EventEmitter2} eventPublisher - The event emitter service.
|
||||
* @param {typeof CreditNoteAppliedInvoice} creditNoteAppliedInvoiceModel - The credit note applied invoice model.
|
||||
*/
|
||||
constructor(
|
||||
private readonly paymentReceiveValidators: PaymentReceivedValidators,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(CreditNoteAppliedInvoice.name)
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Apply credit note to the given invoices.
|
||||
* @param {number} creditNoteId
|
||||
* @param {IApplyCreditToInvoicesDTO} applyCreditToInvoicesDTO
|
||||
*/
|
||||
public async applyCreditNoteToInvoices(
|
||||
creditNoteId: number,
|
||||
applyCreditToInvoicesDTO: IApplyCreditToInvoicesDTO,
|
||||
): Promise<ICreditNoteAppliedToInvoice[]> {
|
||||
// Saves the credit note or throw not found service error.
|
||||
const creditNote = await this.getCreditNoteOrThrowError(creditNoteId);
|
||||
|
||||
// Retrieve the applied invoices that associated to the credit note customer.
|
||||
const appliedInvoicesEntries =
|
||||
await this.paymentReceiveValidators.validateInvoicesIDsExistance(
|
||||
creditNote.customerId,
|
||||
applyCreditToInvoicesDTO.entries,
|
||||
);
|
||||
|
||||
// Transformes apply DTO to model.
|
||||
const creditNoteAppliedModel = this.transformApplyDTOToModel(
|
||||
applyCreditToInvoicesDTO,
|
||||
creditNote,
|
||||
);
|
||||
|
||||
// Validate invoices has remaining amount to apply.
|
||||
this.validateInvoicesRemainingAmount(
|
||||
appliedInvoicesEntries,
|
||||
creditNoteAppliedModel.amount,
|
||||
);
|
||||
// Validate the credit note remaining amount.
|
||||
this.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.
|
||||
const creditNoteAppliedInvoices = await this.creditNoteAppliedInvoiceModel
|
||||
.query()
|
||||
.insertGraph(creditNoteAppliedModel.entries);
|
||||
|
||||
// Triggers `onCreditNoteApplyToInvoiceCreated` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onApplyToInvoicesCreated,
|
||||
{
|
||||
creditNote,
|
||||
creditNoteAppliedInvoices,
|
||||
trx,
|
||||
} as IApplyCreditToInvoicesCreatedPayload,
|
||||
);
|
||||
|
||||
return creditNoteAppliedInvoices;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transformes apply DTO to model.
|
||||
* @param {IApplyCreditToInvoicesDTO} applyDTO
|
||||
* @param {ICreditNote} creditNote
|
||||
* @returns
|
||||
*/
|
||||
private transformApplyDTOToModel = (
|
||||
applyDTO: IApplyCreditToInvoicesDTO,
|
||||
creditNote: ICreditNote,
|
||||
): ICreditNoteAppliedToInvoiceModel => {
|
||||
const entries = applyDTO.entries.map((entry) => ({
|
||||
invoiceId: entry.invoiceId,
|
||||
amount: entry.amount,
|
||||
creditNoteId: creditNote.id,
|
||||
}));
|
||||
return {
|
||||
amount: sumBy(entries, 'amount'),
|
||||
entries,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the invoice remaining amount.
|
||||
* @param {ISaleInvoice[]} invoices
|
||||
* @param {number} amount
|
||||
*/
|
||||
private validateInvoicesRemainingAmount = (
|
||||
invoices: SaleInvoice[],
|
||||
amount: number,
|
||||
) => {
|
||||
const invalidInvoices = invoices.filter(
|
||||
(invoice) => invoice.dueAmount < amount,
|
||||
);
|
||||
if (invalidInvoices.length > 0) {
|
||||
throw new ServiceError(ERRORS.INVOICES_HAS_NO_REMAINING_AMOUNT);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import { Transformer } from "@/modules/Transformer/Transformer";
|
||||
|
||||
export class CreditNoteWithInvoicesToApplyTransformer extends Transformer {
|
||||
/**
|
||||
* Include these attributes to sale invoice object.
|
||||
* @returns {Array}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [
|
||||
'formattedInvoiceDate',
|
||||
'formattedDueDate',
|
||||
'formattedAmount',
|
||||
'formattedDueAmount',
|
||||
'formattedPaymentAmount',
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve formatted invoice date.
|
||||
* @param {ISaleInvoice} invoice
|
||||
* @returns {String}
|
||||
*/
|
||||
protected formattedInvoiceDate = (invoice): string => {
|
||||
return this.formatDate(invoice.invoiceDate);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve formatted due date.
|
||||
* @param {ISaleInvoice} invoice
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedDueDate = (invoice): string => {
|
||||
return this.formatDate(invoice.dueDate);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve formatted invoice amount.
|
||||
* @param {ISaleInvoice} invoice
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedAmount = (invoice): string => {
|
||||
return this.formatNumber(invoice.balance, {
|
||||
currencyCode: invoice.currencyCode,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve formatted invoice due amount.
|
||||
* @param {ISaleInvoice} invoice
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedDueAmount = (invoice): string => {
|
||||
return this.formatNumber(invoice.dueAmount, {
|
||||
currencyCode: invoice.currencyCode,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve formatted payment amount.
|
||||
* @param {ISaleInvoice} invoice
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedPaymentAmount = (invoice): string => {
|
||||
return this.formatNumber(invoice.paymentAmount, {
|
||||
currencyCode: invoice.currencyCode,
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { IApplyCreditToInvoicesDeletedPayload } from '../types/CreditNotes.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';
|
||||
|
||||
@Injectable()
|
||||
export default class DeleteCreditNoteApplyToInvoices {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Apply credit note to the given invoices.
|
||||
* @param {number} creditNoteId
|
||||
* @param {IApplyCreditToInvoicesDTO} applyCreditToInvoicesDTO
|
||||
*/
|
||||
public deleteApplyCreditNoteToInvoices = async (
|
||||
applyCreditToInvoicesId: number,
|
||||
): Promise<void> => {
|
||||
const creditNoteAppliedToInvoice = await this.creditNoteAppliedInvoiceModel
|
||||
.query()
|
||||
.findById(applyCreditToInvoicesId);
|
||||
|
||||
if (!creditNoteAppliedToInvoice) {
|
||||
throw new ServiceError(ERRORS.CREDIT_NOTE_APPLY_TO_INVOICES_NOT_FOUND);
|
||||
}
|
||||
// Retrieve the credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel
|
||||
.query()
|
||||
.findById(creditNoteAppliedToInvoice.creditNoteId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Creates credit note apply to invoice transaction.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Delete credit note applied to invoices.
|
||||
await this.creditNoteAppliedInvoiceModel
|
||||
.query(trx)
|
||||
.findById(applyCreditToInvoicesId)
|
||||
.delete();
|
||||
|
||||
// Triggers `onCreditNoteApplyToInvoiceDeleted` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onApplyToInvoicesDeleted,
|
||||
{
|
||||
creditNote,
|
||||
creditNoteAppliedToInvoice,
|
||||
trx,
|
||||
} as IApplyCreditToInvoicesDeletedPayload,
|
||||
);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
import { ERRORS } from '../constants';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteCustomerLinkedCreditNoteService {
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validate the given customer has no linked credit note transactions.
|
||||
* @param {number} customerId - The customer identifier.
|
||||
*/
|
||||
public async validateCustomerHasNoCreditTransaction(customerId: number) {
|
||||
const associatedCredits = await this.creditNoteModel
|
||||
.query()
|
||||
.where('customerId', customerId);
|
||||
|
||||
if (associatedCredits.length > 0) {
|
||||
throw new ServiceError(ERRORS.CUSTOMER_HAS_LINKED_CREDIT_NOTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
IRefundCreditNoteDeletedPayload,
|
||||
IRefundCreditNoteDeletingPayload,
|
||||
IRefundVendorCreditDeletedPayload,
|
||||
} from '../types/CreditNotes.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 {
|
||||
/**
|
||||
* @param {UnitOfWork} uow
|
||||
* @param {EventEmitter2} eventPublisher
|
||||
* @param {typeof RefundCreditNoteModel} refundCreditNoteModel
|
||||
*/
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(RefundCreditNote.name)
|
||||
private readonly refundCreditNoteModel: typeof RefundCreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} refundCreditId
|
||||
* @returns
|
||||
*/
|
||||
public deleteCreditNoteRefund = async (refundCreditId: number) => {
|
||||
// Retrieve the old credit note or throw not found service error.
|
||||
const oldRefundCredit = await this.refundCreditNoteModel
|
||||
.query()
|
||||
.findById(refundCreditId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundDelete, {
|
||||
refundCreditId,
|
||||
oldRefundCredit,
|
||||
} as IRefundCreditNoteDeletedPayload);
|
||||
|
||||
// Deletes refund credit note transactions with associated entries.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
const eventPayload = {
|
||||
trx,
|
||||
refundCreditId,
|
||||
oldRefundCredit,
|
||||
} as IRefundCreditNoteDeletedPayload | IRefundCreditNoteDeletingPayload;
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleting` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onRefundDeleting,
|
||||
eventPayload,
|
||||
);
|
||||
|
||||
// Deletes the refund credit note graph from the storage.
|
||||
await this.refundCreditNoteModel
|
||||
.query(trx)
|
||||
.findById(refundCreditId)
|
||||
.delete();
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleted` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onRefundDeleted,
|
||||
eventPayload as IRefundVendorCreditDeletedPayload,
|
||||
);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ERRORS } from '../constants';
|
||||
import { RefundCreditNote } from '../models/RefundCreditNote';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
|
||||
@Injectable()
|
||||
export class RefundCreditNoteService {
|
||||
/**
|
||||
* @param {typeof RefundCreditNote} refundCreditNoteModel - The refund credit note model.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(RefundCreditNote.name)
|
||||
private readonly refundCreditNoteModel: typeof RefundCreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} refundCreditId
|
||||
* @returns {Promise<RefundCreditNote>}
|
||||
*/
|
||||
public getCreditNoteRefundOrThrowError = async (
|
||||
refundCreditId: number,
|
||||
): Promise<RefundCreditNote> => {
|
||||
const refundCreditNote = await this.refundCreditNoteModel
|
||||
.query()
|
||||
.findById(refundCreditId);
|
||||
if (!refundCreditNote) {
|
||||
throw new ServiceError(ERRORS.REFUND_CREDIT_NOTE_NOT_FOUND);
|
||||
}
|
||||
return refundCreditNote;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the refund account type.
|
||||
* @param {Account} account
|
||||
*/
|
||||
public validateRefundWithdrawwalAccountType = (account: Account): void => {
|
||||
const supportedTypes = ['bank', 'cash', 'fixed-asset'];
|
||||
|
||||
if (supportedTypes.indexOf(account.accountType) === -1) {
|
||||
throw new ServiceError(ERRORS.ACCOUNT_INVALID_TYPE);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { AccountNormal, ILedgerEntry, IRefundCreditNote } from '@/interfaces';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
||||
// import Ledger from '@/services/Accounting/Ledger';
|
||||
|
||||
// @Service()
|
||||
// export default class RefundCreditNoteGLEntries {
|
||||
// @Inject()
|
||||
// ledgerStorage: LedgerStorageService;
|
||||
|
||||
// @Inject()
|
||||
// tenancy: HasTenancyService;
|
||||
|
||||
// /**
|
||||
// * Retrieves the refund credit common GL entry.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @returns
|
||||
// */
|
||||
// private getRefundCreditCommonGLEntry = (
|
||||
// refundCreditNote: IRefundCreditNote
|
||||
// ) => {
|
||||
// return {
|
||||
// currencyCode: refundCreditNote.currencyCode,
|
||||
// exchangeRate: refundCreditNote.exchangeRate,
|
||||
|
||||
// transactionType: 'RefundCreditNote',
|
||||
// transactionId: refundCreditNote.id,
|
||||
// date: refundCreditNote.date,
|
||||
// userId: refundCreditNote.userId,
|
||||
|
||||
// referenceNumber: refundCreditNote.referenceNo,
|
||||
|
||||
// createdAt: refundCreditNote.createdAt,
|
||||
// indexGroup: 10,
|
||||
|
||||
// credit: 0,
|
||||
// debit: 0,
|
||||
|
||||
// note: refundCreditNote.description,
|
||||
// branchId: refundCreditNote.branchId,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the refudn credit receivable GL entry.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {number} ARAccountId
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getRefundCreditGLReceivableEntry = (
|
||||
// refundCreditNote: IRefundCreditNote,
|
||||
// ARAccountId: number
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// debit: refundCreditNote.amount,
|
||||
// accountId: ARAccountId,
|
||||
// contactId: refundCreditNote.creditNote.customerId,
|
||||
// index: 1,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the refund credit withdrawal GL entry.
|
||||
// * @param {number} refundCreditNote
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getRefundCreditGLWithdrawalEntry = (
|
||||
// refundCreditNote: IRefundCreditNote
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// credit: refundCreditNote.amount,
|
||||
// accountId: refundCreditNote.fromAccountId,
|
||||
// index: 2,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieve the refund credit note GL entries.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {number} receivableAccount
|
||||
// * @returns {ILedgerEntry[]}
|
||||
// */
|
||||
// public getRefundCreditGLEntries(
|
||||
// refundCreditNote: IRefundCreditNote,
|
||||
// ARAccountId: number
|
||||
// ): ILedgerEntry[] {
|
||||
// const receivableEntry = this.getRefundCreditGLReceivableEntry(
|
||||
// refundCreditNote,
|
||||
// ARAccountId
|
||||
// );
|
||||
// const withdrawalEntry =
|
||||
// this.getRefundCreditGLWithdrawalEntry(refundCreditNote);
|
||||
|
||||
// return [receivableEntry, withdrawalEntry];
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Creates refund credit GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public createRefundCreditGLEntries = async (
|
||||
// tenantId: number,
|
||||
// refundCreditNoteId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// const { Account, RefundCreditNote } = this.tenancy.models(tenantId);
|
||||
|
||||
// // Retrieve the refund with associated credit note.
|
||||
// const refundCreditNote = await RefundCreditNote.query(trx)
|
||||
// .findById(refundCreditNoteId)
|
||||
// .withGraphFetched('creditNote');
|
||||
|
||||
// // Receivable account A/R.
|
||||
// const receivableAccount = await Account.query().findOne(
|
||||
// 'slug',
|
||||
// 'accounts-receivable'
|
||||
// );
|
||||
// // Retrieve refund credit GL entries.
|
||||
// const refundGLEntries = this.getRefundCreditGLEntries(
|
||||
// refundCreditNote,
|
||||
// receivableAccount.id
|
||||
// );
|
||||
// const ledger = new Ledger(refundGLEntries);
|
||||
|
||||
// // Saves refund ledger entries.
|
||||
// await this.ledgerStorage.commit(tenantId, ledger, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Reverts refund credit note GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} refundCreditNoteId
|
||||
// * @param {number} receivableAccount
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public revertRefundCreditGLEntries = async (
|
||||
// tenantId: number,
|
||||
// refundCreditNoteId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// await this.ledgerStorage.deleteByReference(
|
||||
// tenantId,
|
||||
// refundCreditNoteId,
|
||||
// 'RefundCreditNote',
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
@@ -1,48 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
|
||||
@Injectable()
|
||||
export class RefundSyncCreditNoteBalance {
|
||||
/**
|
||||
* @param {typeof CreditNote} creditNoteModel - The credit note model.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Increments the refund amount of the credit note.
|
||||
* @param {number} creditNoteId - The credit note ID.
|
||||
* @param {number} amount - The amount to increment.
|
||||
* @param {Knex.Transaction} trx - The knex transaction.
|
||||
*/
|
||||
public incrementCreditNoteRefundAmount = async (
|
||||
creditNoteId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> => {
|
||||
await this.creditNoteModel
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.increment('refunded_amount', amount);
|
||||
};
|
||||
|
||||
/**
|
||||
* Decrements the refund amount of the credit note.
|
||||
* @param {number} creditNoteId - The credit note ID.
|
||||
* @param {number} amount - The amount to decrement.
|
||||
* @param {Knex.Transaction} trx - The knex transaction.
|
||||
*/
|
||||
public decrementCreditNoteRefundAmount = async (
|
||||
creditNoteId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> => {
|
||||
await this.creditNoteModel
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.decrement('refunded_amount', amount);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user