Skip to content

Commit

Permalink
incr: validate has return invoices beforeCancel
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayitzme committed Jun 27, 2023
1 parent 410da26 commit a14a5c3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ export abstract class Invoice extends Transactional {
async beforeCancel(): Promise<void> {
await super.beforeCancel();
await this._validateStockTransferCancelled();
await this._validateHasLinkedReturnInvoices();
}

async beforeDelete(): Promise<void> {
Expand Down Expand Up @@ -889,6 +890,31 @@ export abstract class Invoice extends Transactional {
);
}

async _validateHasLinkedReturnInvoices() {
if (!this.name || this.isReturn) {
return;
}

const returnInvoices = await this.fyo.db.getAll(this.schemaName, {
filters: {
returnAgainst: this.name,
},
});

if (!returnInvoices.length) {
return;
}

const names = returnInvoices.map(({ name }) => name).join(', ');
const label =
this.fyo.schemaMap[this.schemaName]?.label ?? this.schema.name;

throw new ValidationError(
this.fyo
.t`Cannot cancel ${this.schema.label} ${this.name} because of the following ${label}: ${names}`
);
}

async _getLinkedStockTransferNames(cancelled: boolean) {
const name = this.name;
if (!name) {
Expand Down

0 comments on commit a14a5c3

Please sign in to comment.