2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00

refactor: batch-wise stock availability validation

This commit is contained in:
akshayitzme 2023-02-17 15:30:44 +05:30
parent 1d188151e8
commit ec31a3ba65

View File

@ -132,44 +132,45 @@ export class StockManager {
return;
}
const ifItemHasBatchNumber = await this.fyo.getValue(
const isItemHasBatchNumber = await this.fyo.getValue(
'Item',
details.item,
'hasBatchNumber'
);
if (!isItemHasBatchNumber) return;
const date = details.date.toISOString();
const formattedDate = this.fyo.format(details.date, 'Datetime');
if (ifItemHasBatchNumber) {
if (!details.batchNumber) {
throw new ValidationError(
t`Please enter Batch Number for ${details.item}`
);
}
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;
const itemsInBatch =
(await this.fyo.db.getStockQuantity(
details.item,
details.fromLocation,
undefined,
date,
details.batchNumber
)) ?? 0;
if (details.quantity > itemsInBatch) {
throw new ValidationError(
[
t`Insufficient Quantity in Batch ${details.batchNumber}`,
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')
);
}
if (details.quantity > itemsInBatch) {
throw new ValidationError(
[
t`Insufficient Quantity in Batch ${details.batchNumber}`,
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')
);
}
}