mirror of
https://github.com/frappe/books.git
synced 2025-02-02 12:08:27 +00:00
fix: batch quantity validations
This commit is contained in:
parent
7029fa79ae
commit
9a8e423b72
@ -1,6 +1,5 @@
|
|||||||
import { Fyo, t } from 'fyo';
|
import { Fyo, t } from 'fyo';
|
||||||
import { ValidationError } from 'fyo/utils/errors';
|
import { ValidationError } from 'fyo/utils/errors';
|
||||||
import { DateTime } from 'luxon';
|
|
||||||
import { ModelNameEnum } from 'models/types';
|
import { ModelNameEnum } from 'models/types';
|
||||||
import { Money } from 'pesa';
|
import { Money } from 'pesa';
|
||||||
import { StockLedgerEntry } from './StockLedgerEntry';
|
import { StockLedgerEntry } from './StockLedgerEntry';
|
||||||
@ -133,43 +132,7 @@ export class StockManager {
|
|||||||
|
|
||||||
const date = details.date.toISOString();
|
const date = details.date.toISOString();
|
||||||
const formattedDate = this.fyo.format(details.date, 'Datetime');
|
const formattedDate = this.fyo.format(details.date, 'Datetime');
|
||||||
const isItemHasBatchNumber = await this.fyo.getValue(
|
const batchNumber = details.batchNumber || undefined;
|
||||||
'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')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let quantityBefore =
|
let quantityBefore =
|
||||||
(await this.fyo.db.getStockQuantity(
|
(await this.fyo.db.getStockQuantity(
|
||||||
@ -177,22 +140,24 @@ export class StockManager {
|
|||||||
details.fromLocation,
|
details.fromLocation,
|
||||||
undefined,
|
undefined,
|
||||||
date,
|
date,
|
||||||
undefined
|
batchNumber
|
||||||
)) ?? 0;
|
)) ?? 0;
|
||||||
|
|
||||||
if (this.isCancelled) {
|
if (this.isCancelled) {
|
||||||
quantityBefore += details.quantity;
|
quantityBefore += details.quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const batchNumberMessage = !!batchNumber ? t` in Batch ${batchNumber}` : '';
|
||||||
|
|
||||||
if (quantityBefore < details.quantity) {
|
if (quantityBefore < details.quantity) {
|
||||||
throw new ValidationError(
|
throw new ValidationError(
|
||||||
[
|
[
|
||||||
t`Insufficient Quantity.`,
|
t`Insufficient Quantity.`,
|
||||||
t`Additional quantity (${
|
t`Additional quantity (${
|
||||||
details.quantity - quantityBefore
|
details.quantity - quantityBefore
|
||||||
}) required to make outward transfer of item ${details.item} from ${
|
}) required${batchNumberMessage} to make outward transfer of item ${
|
||||||
details.fromLocation
|
details.item
|
||||||
} on ${formattedDate}`,
|
} from ${details.fromLocation} on ${formattedDate}`,
|
||||||
].join('\n')
|
].join('\n')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -202,8 +167,9 @@ export class StockManager {
|
|||||||
details.fromLocation,
|
details.fromLocation,
|
||||||
details.date.toISOString(),
|
details.date.toISOString(),
|
||||||
undefined,
|
undefined,
|
||||||
undefined
|
batchNumber
|
||||||
);
|
);
|
||||||
|
|
||||||
if (quantityAfter === null) {
|
if (quantityAfter === null) {
|
||||||
// No future transactions
|
// No future transactions
|
||||||
return;
|
return;
|
||||||
@ -217,9 +183,9 @@ export class StockManager {
|
|||||||
t`Transfer will cause future entries to have negative stock.`,
|
t`Transfer will cause future entries to have negative stock.`,
|
||||||
t`Additional quantity (${
|
t`Additional quantity (${
|
||||||
quantityAfter - quantityRemaining
|
quantityAfter - quantityRemaining
|
||||||
}) required to make outward transfer of item ${details.item} from ${
|
}) required${batchNumberMessage} to make outward transfer of item ${
|
||||||
details.fromLocation
|
details.item
|
||||||
} on ${formattedDate}`,
|
} from ${details.fromLocation} on ${formattedDate}`,
|
||||||
].join('\n')
|
].join('\n')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -312,7 +278,7 @@ class StockManagerItem {
|
|||||||
date: this.date,
|
date: this.date,
|
||||||
item: this.item,
|
item: this.item,
|
||||||
rate: this.rate,
|
rate: this.rate,
|
||||||
batchNumber: this.batchNumber,
|
batchNumber: this.batchNumber || null,
|
||||||
quantity,
|
quantity,
|
||||||
location,
|
location,
|
||||||
referenceName: this.referenceName,
|
referenceName: this.referenceName,
|
||||||
|
@ -18,9 +18,10 @@ export async function validateBatchNumber(
|
|||||||
async function validateItemRowBatchNumber(
|
async function validateItemRowBatchNumber(
|
||||||
doc: StockMovementItem | StockTransferItem | InvoiceItem
|
doc: StockMovementItem | StockTransferItem | InvoiceItem
|
||||||
) {
|
) {
|
||||||
|
const idx = doc.idx ?? 0 + 1;
|
||||||
const item = doc.item;
|
const item = doc.item;
|
||||||
const batchNumber = doc.batchNumber;
|
const batchNumber = doc.batchNumber;
|
||||||
if (!item || batchNumber) {
|
if (!item) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,11 +31,21 @@ async function validateItemRowBatchNumber(
|
|||||||
'hasBatchNumber'
|
'hasBatchNumber'
|
||||||
);
|
);
|
||||||
|
|
||||||
if (hasBatchNumber && batchNumber) {
|
if (!hasBatchNumber && batchNumber) {
|
||||||
return;
|
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(
|
if (hasBatchNumber && !batchNumber) {
|
||||||
[`Batch Number not set.`, `Item ${item} is a batched item`].join(' ')
|
throw new ValidationError(
|
||||||
);
|
[
|
||||||
|
doc.fyo.t`Batch Number not set for row ${idx}.`,
|
||||||
|
doc.fyo.t`Item ${item} is a batched item`,
|
||||||
|
].join(' ')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user