refactor: credit notes and vendor credits to Nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-29 22:55:42 +02:00
parent caf235e2b5
commit 77bbf6828d
93 changed files with 2249 additions and 1634 deletions

View File

@@ -1,53 +0,0 @@
import { Transformer } from '../../Transformer/Transformer';
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
export class CreditNoteAppliedInvoiceTransformer extends Transformer {
/**
* Includeded attributes.
* @returns {string[]}
*/
public includeAttributes = (): string[] => {
return [
'formttedAmount',
'creditNoteNumber',
'creditNoteDate',
'invoiceNumber',
'invoiceReferenceNo',
'formattedCreditNoteDate',
];
};
/**
* Exclude attributes.
* @returns {string[]}
*/
public excludeAttributes = (): string[] => {
return ['saleInvoice', 'creditNote'];
};
public formttedAmount = (item: CreditNoteAppliedInvoice) => {
return this.formatNumber(item.amount, {
currencyCode: item.creditNote.currencyCode,
});
};
public creditNoteNumber = (item: CreditNoteAppliedInvoice) => {
return item.creditNote.creditNoteNumber;
};
public creditNoteDate = (item: CreditNoteAppliedInvoice) => {
return item.creditNote.creditNoteDate;
};
public invoiceNumber = (item: CreditNoteAppliedInvoice) => {
return item.saleInvoice.invoiceNo;
};
public invoiceReferenceNo = (item: CreditNoteAppliedInvoice) => {
return item.saleInvoice.referenceNo;
};
public formattedCreditNoteDate = (item: CreditNoteAppliedInvoice) => {
return this.formatDate(item.creditNote.creditNoteDate);
};
}

View File

@@ -1,42 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { CreditNoteAppliedInvoiceTransformer } from './CreditNoteAppliedInvoiceTransformer';
import { CreditNote } from '../models/CreditNote';
import { TransformerInjectable } from '../../Transformer/TransformerInjectable.service';
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
@Injectable()
export class GetCreditNoteAssociatedAppliedInvoices {
constructor(
private readonly transformer: TransformerInjectable,
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
@Inject(CreditNote.name)
private readonly creditNoteModel: typeof CreditNote
) {
}
/**
* Retrieve credit note associated invoices to apply.
* @param {number} creditNoteId
* @returns {Promise<CreditNoteAppliedInvoice[]>}
*/
public async getCreditAssociatedAppliedInvoices(
creditNoteId: number
): Promise<CreditNoteAppliedInvoice[]> {
// Retrieve credit note or throw not found service error.
const creditNote = await this.creditNoteModel.query()
.findById(creditNoteId)
.throwIfNotFound();
const appliedToInvoices = await this.creditNoteAppliedInvoiceModel.query()
.where('credit_note_id', creditNoteId)
.withGraphFetched('saleInvoice')
.withGraphFetched('creditNote');
// Transforms credit note applied to invoices.
return this.transformer.transform(
appliedToInvoices,
new CreditNoteAppliedInvoiceTransformer()
);
}
}

View File

@@ -1,41 +0,0 @@
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';
@Injectable()
export class GetCreditNoteAssociatedInvoicesToApply {
constructor(
private transformer: TransformerInjectable,
private getCreditNote: GetCreditNote,
@Inject(SaleInvoice.name)
private saleInvoiceModel: typeof SaleInvoice,
) {}
/**
* Retrieve credit note associated invoices to apply.
* @param {number} creditNoteId
* @returns {Promise<ISaleInvoice[]>}
*/
public async getCreditAssociatedInvoicesToApply(
creditNoteId: number,
): Promise<SaleInvoice[]> {
// Retrieve credit note or throw not found service error.
const creditNote = await this.getCreditNote.getCreditNote(creditNoteId);
// Retrieves the published due invoices that associated to the given customer.
const saleInvoices = await this.saleInvoiceModel
.query()
.where('customerId', creditNote.customerId)
.modify('dueInvoices')
.modify('published');
// Transforms the sale invoices models to POJO.
return this.transformer.transform(
saleInvoices,
new CreditNoteWithInvoicesToApplyTransformer(),
);
}
}

View File

@@ -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,

View File

@@ -1,37 +0,0 @@
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';
@Injectable()
export class ListCreditNoteRefunds {
constructor(
private readonly transformer: TransformerInjectable,
@Inject(RefundCreditNote.name)
private readonly refundCreditNoteModel: typeof RefundCreditNote,
) {}
/**
* Retrieve the credit note graph.
* @param {number} creditNoteId
* @returns {Promise<IRefundCreditNotePOJO[]>}
*/
public async getCreditNoteRefunds(
creditNoteId: number,
): Promise<IRefundCreditNotePOJO[]> {
// Retrieve refund credit notes associated to the given credit note.
const refundCreditTransactions = await this.refundCreditNoteModel
.query()
.where('creditNoteId', creditNoteId)
.withGraphFetched('creditNote')
.withGraphFetched('fromAccount');
// Transforms refund credit note models to POJO objects.
return this.transformer.transform(
refundCreditTransactions,
new RefundCreditNoteTransformer(),
);
}
}

View File

@@ -1,37 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { IRefundCreditNote } from '../types/CreditNotes.types';
import { RefundCreditNote } from '../models/RefundCreditNote';
import { RefundCreditNoteTransformer } from './RefundCreditNoteTransformer';
@Injectable()
export class GetRefundCreditNoteTransaction {
/**
* @param {RefundCreditNoteTransformer} transformer
* @param {typeof RefundCreditNote} refundCreditNoteModel
*/
constructor(
private readonly transformer: RefundCreditNoteTransformer,
@Inject(RefundCreditNote.name)
private readonly refundCreditNoteModel: typeof RefundCreditNote,
) {
}
/**
* Retrieve credit note associated invoices to apply.
* @param {number} refundCreditId
* @returns {Promise<IRefundCreditNote>}
*/
public async getRefundCreditTransaction(
refundCreditId: number
): Promise<IRefundCreditNote> {
const refundCreditNote = await this.refundCreditNoteModel
.query()
.findById(refundCreditId)
.withGraphFetched('fromAccount')
.withGraphFetched('creditNote')
.throwIfNotFound();
return this.transformer.transform(refundCreditNote);
}
}