mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-24 16:49:48 +00:00
fix(server): add invoices Map for validateInvoicesRemainingAmount
This commit is contained in:
@@ -18,6 +18,7 @@ import { CreditNote } from '@/modules/CreditNotes/models/CreditNote';
|
|||||||
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
|
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
|
||||||
import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service';
|
import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service';
|
||||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||||
|
import { ApplyCreditNoteToInvoicesDto } from '../dtos/ApplyCreditNoteToInvoices.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CreditNoteApplyToInvoices {
|
export class CreditNoteApplyToInvoices {
|
||||||
@@ -49,7 +50,7 @@ export class CreditNoteApplyToInvoices {
|
|||||||
*/
|
*/
|
||||||
public async applyCreditNoteToInvoices(
|
public async applyCreditNoteToInvoices(
|
||||||
creditNoteId: number,
|
creditNoteId: number,
|
||||||
applyCreditToInvoicesDTO: IApplyCreditToInvoicesDTO,
|
applyCreditToInvoicesDTO: ApplyCreditNoteToInvoicesDto,
|
||||||
): Promise<CreditNoteAppliedInvoice[]> {
|
): Promise<CreditNoteAppliedInvoice[]> {
|
||||||
// Saves the credit note or throw not found service error.
|
// Saves the credit note or throw not found service error.
|
||||||
const creditNote = await this.creditNoteModel()
|
const creditNote = await this.creditNoteModel()
|
||||||
@@ -131,11 +132,12 @@ export class CreditNoteApplyToInvoices {
|
|||||||
invoices: SaleInvoice[],
|
invoices: SaleInvoice[],
|
||||||
entries: ICreditNoteAppliedToInvoice[],
|
entries: ICreditNoteAppliedToInvoice[],
|
||||||
) => {
|
) => {
|
||||||
const invalidInvoices = invoices.filter((invoice) => {
|
const invoiceMap = new Map(invoices.map((inv) => [inv.id, inv]));
|
||||||
const entry = entries.find((e) => e.invoiceId === invoice.id);
|
const invalidEntries = entries.filter((entry) => {
|
||||||
return entry && invoice.dueAmount < entry.amount;
|
const invoice = invoiceMap.get(entry.invoiceId);
|
||||||
|
return invoice != null && invoice.dueAmount < entry.amount;
|
||||||
});
|
});
|
||||||
if (invalidInvoices.length > 0) {
|
if (invalidEntries.length > 0) {
|
||||||
throw new ServiceError(ERRORS.INVOICES_HAS_NO_REMAINING_AMOUNT);
|
throw new ServiceError(ERRORS.INVOICES_HAS_NO_REMAINING_AMOUNT);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export interface IApplyCreditToInvoicesDeletedPayload {
|
|||||||
export interface ICreditNoteAppliedToInvoice {
|
export interface ICreditNoteAppliedToInvoice {
|
||||||
amount: number;
|
amount: number;
|
||||||
creditNoteId: number;
|
creditNoteId: number;
|
||||||
|
invoiceId: number;
|
||||||
}
|
}
|
||||||
export interface ICreditNoteAppliedToInvoiceModel {
|
export interface ICreditNoteAppliedToInvoiceModel {
|
||||||
amount: number;
|
amount: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user