From 312f78ff73fc64ba0a942666216b90daf668bac9 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Tue, 1 Aug 2023 11:15:06 +0530 Subject: [PATCH] fix: validation: cant cancel doc if return is made --- .../AccountingSettings/AccountingSettings.ts | 1 - models/inventory/StockTransfer.ts | 33 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/models/baseModels/AccountingSettings/AccountingSettings.ts b/models/baseModels/AccountingSettings/AccountingSettings.ts index c6ac8886..e1cef9da 100644 --- a/models/baseModels/AccountingSettings/AccountingSettings.ts +++ b/models/baseModels/AccountingSettings/AccountingSettings.ts @@ -51,7 +51,6 @@ export class AccountingSettings extends Doc { override hidden: HiddenMap = { discountAccount: () => !this.enableDiscounting, gstin: () => this.fyo.singles.SystemSettings?.countryCode !== 'in', - enableStockReturns: () => !this.enableInventory, }; async change(ch: ChangeArg) { diff --git a/models/inventory/StockTransfer.ts b/models/inventory/StockTransfer.ts index 1e187338..9588f6f2 100644 --- a/models/inventory/StockTransfer.ts +++ b/models/inventory/StockTransfer.ts @@ -206,6 +206,11 @@ export abstract class StockTransfer extends Transfer { await this._updateItemsReturned(); } + async beforeCancel(): Promise { + await super.beforeCancel(); + await this._validateHasReturnDocs(); + } + async afterCancel(): Promise { await super.afterCancel(); await updateSerialNumbers(this, true, this.isReturn); @@ -268,8 +273,8 @@ export abstract class StockTransfer extends Transfer { } async _updateItemsReturned() { - if (!this.isSubmitted || !this.returnAgainst) { - return null; + if (this.isSyncing || !this.returnAgainst) { + return; } const linkedReference = await this.loadAndGetLink('returnAgainst'); @@ -281,11 +286,33 @@ export abstract class StockTransfer extends Transfer { this.schemaName, linkedReference.name ); - const isReturned = !!referenceDoc; + const isReturned = this.isSubmitted; await referenceDoc.setAndSync({ isReturned }); } + async _validateHasReturnDocs() { + if (!this.name) { + return; + } + + const returnDocs = await this.fyo.db.getAll(this.schemaName, { + filters: { returnAgainst: this.name }, + }); + + const hasReturnDocs = !!returnDocs.length; + if (!hasReturnDocs) { + return; + } + + const returnDocNames = returnDocs.map((doc) => doc.name).join(', '); + const label = this.fyo.schemaMap[this.schemaName]?.label ?? this.schemaName; + + throw new ValidationError( + t`Cannot cancel ${this.schema.label} ${this.name} because of the following ${label}: ${returnDocNames}` + ); + } + _getTransferMap() { return (this.items ?? []).reduce((acc, item) => { if (!item.item) {