From 9a8e423b72f2ad08aa2d28e02ccb2eeaa920ede3 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 27 Feb 2023 19:16:04 +0530 Subject: [PATCH] fix: batch quantity validations --- models/inventory/StockManager.ts | 60 +++++++------------------------- models/inventory/helpers.ts | 23 ++++++++---- 2 files changed, 30 insertions(+), 53 deletions(-) diff --git a/models/inventory/StockManager.ts b/models/inventory/StockManager.ts index f5d0adc5..8a914400 100644 --- a/models/inventory/StockManager.ts +++ b/models/inventory/StockManager.ts @@ -1,6 +1,5 @@ import { Fyo, t } from 'fyo'; import { ValidationError } from 'fyo/utils/errors'; -import { DateTime } from 'luxon'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { StockLedgerEntry } from './StockLedgerEntry'; @@ -133,43 +132,7 @@ export class StockManager { const date = details.date.toISOString(); const formattedDate = this.fyo.format(details.date, 'Datetime'); - const isItemHasBatchNumber = await this.fyo.getValue( - 'Item', - details.item, - 'hasBatchNumber' - ); - - if (isItemHasBatchNumber && !this.isCancelled) { - if (!details.batchNumber) { - throw new ValidationError( - t`Please enter Batch Number for ${details.item}` - ); - } - - const itemsInBatch = - (await this.fyo.db.getStockQuantity( - details.item, - details.fromLocation, - undefined, - date, - details.batchNumber - )) ?? 0; - - if (details.quantity <= itemsInBatch) return; - - throw new ValidationError( - [ - t`Insufficient Quantity`, - t`Additional quantity (${ - details.quantity - itemsInBatch - }) is required in batch ${ - details.batchNumber - } to make the outward transfer of item ${details.item} from ${ - details.fromLocation - } on ${formattedDate}`, - ].join('\n') - ); - } + const batchNumber = details.batchNumber || undefined; let quantityBefore = (await this.fyo.db.getStockQuantity( @@ -177,22 +140,24 @@ export class StockManager { details.fromLocation, undefined, date, - undefined + batchNumber )) ?? 0; if (this.isCancelled) { quantityBefore += details.quantity; } + const batchNumberMessage = !!batchNumber ? t` in Batch ${batchNumber}` : ''; + if (quantityBefore < details.quantity) { throw new ValidationError( [ t`Insufficient Quantity.`, t`Additional quantity (${ details.quantity - quantityBefore - }) required to make outward transfer of item ${details.item} from ${ - details.fromLocation - } on ${formattedDate}`, + }) required${batchNumberMessage} to make outward transfer of item ${ + details.item + } from ${details.fromLocation} on ${formattedDate}`, ].join('\n') ); } @@ -202,8 +167,9 @@ export class StockManager { details.fromLocation, details.date.toISOString(), undefined, - undefined + batchNumber ); + if (quantityAfter === null) { // No future transactions return; @@ -217,9 +183,9 @@ export class StockManager { t`Transfer will cause future entries to have negative stock.`, t`Additional quantity (${ quantityAfter - quantityRemaining - }) required to make outward transfer of item ${details.item} from ${ - details.fromLocation - } on ${formattedDate}`, + }) required${batchNumberMessage} to make outward transfer of item ${ + details.item + } from ${details.fromLocation} on ${formattedDate}`, ].join('\n') ); } @@ -312,7 +278,7 @@ class StockManagerItem { date: this.date, item: this.item, rate: this.rate, - batchNumber: this.batchNumber, + batchNumber: this.batchNumber || null, quantity, location, referenceName: this.referenceName, diff --git a/models/inventory/helpers.ts b/models/inventory/helpers.ts index 8974f113..e1b6bdba 100644 --- a/models/inventory/helpers.ts +++ b/models/inventory/helpers.ts @@ -18,9 +18,10 @@ export async function validateBatchNumber( async function validateItemRowBatchNumber( doc: StockMovementItem | StockTransferItem | InvoiceItem ) { + const idx = doc.idx ?? 0 + 1; const item = doc.item; const batchNumber = doc.batchNumber; - if (!item || batchNumber) { + if (!item) { return; } @@ -30,11 +31,21 @@ async function validateItemRowBatchNumber( 'hasBatchNumber' ); - if (hasBatchNumber && batchNumber) { - return; + if (!hasBatchNumber && batchNumber) { + throw new ValidationError( + [ + doc.fyo.t`Batch Number set for row ${idx}.`, + doc.fyo.t`Item ${item} is not a batched item`, + ].join(' ') + ); } - throw new ValidationError( - [`Batch Number not set.`, `Item ${item} is a batched item`].join(' ') - ); + if (hasBatchNumber && !batchNumber) { + throw new ValidationError( + [ + doc.fyo.t`Batch Number not set for row ${idx}.`, + doc.fyo.t`Item ${item} is a batched item`, + ].join(' ') + ); + } }