2023-02-27 13:09:18 +00:00
|
|
|
import { ValidationError } from 'fyo/utils/errors';
|
|
|
|
import { Invoice } from 'models/baseModels/Invoice/Invoice';
|
|
|
|
import { InvoiceItem } from 'models/baseModels/InvoiceItem/InvoiceItem';
|
|
|
|
import { ModelNameEnum } from 'models/types';
|
|
|
|
import { StockMovement } from './StockMovement';
|
|
|
|
import { StockMovementItem } from './StockMovementItem';
|
|
|
|
import { StockTransfer } from './StockTransfer';
|
|
|
|
import { StockTransferItem } from './StockTransferItem';
|
2022-10-28 08:04:08 +00:00
|
|
|
|
2023-02-27 13:09:18 +00:00
|
|
|
export async function validateBatchNumber(
|
|
|
|
doc: StockMovement | StockTransfer | Invoice
|
|
|
|
) {
|
|
|
|
for (const row of doc.items ?? []) {
|
|
|
|
await validateItemRowBatchNumber(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateItemRowBatchNumber(
|
|
|
|
doc: StockMovementItem | StockTransferItem | InvoiceItem
|
|
|
|
) {
|
2023-02-27 13:46:04 +00:00
|
|
|
const idx = doc.idx ?? 0 + 1;
|
2023-02-27 13:09:18 +00:00
|
|
|
const item = doc.item;
|
|
|
|
const batchNumber = doc.batchNumber;
|
2023-02-27 13:46:04 +00:00
|
|
|
if (!item) {
|
2023-02-27 13:09:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasBatchNumber = await doc.fyo.getValue(
|
|
|
|
ModelNameEnum.Item,
|
|
|
|
item,
|
|
|
|
'hasBatchNumber'
|
|
|
|
);
|
|
|
|
|
2023-02-27 13:46:04 +00:00
|
|
|
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(' ')
|
|
|
|
);
|
2023-02-27 13:09:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 13:46:04 +00:00
|
|
|
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(' ')
|
|
|
|
);
|
|
|
|
}
|
2023-02-27 13:09:18 +00:00
|
|
|
}
|