2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

fix: batch quantity validations

This commit is contained in:
18alantom 2023-02-27 19:16:04 +05:30
parent 7029fa79ae
commit 9a8e423b72
2 changed files with 30 additions and 53 deletions

View File

@ -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,

View File

@ -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(' ')
);
}
}