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) {