2
0
mirror of https://github.com/frappe/books.git synced 2025-01-24 07:38:25 +00:00
books/models/inventory/helpers.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2023-02-27 18:39:18 +05:30
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 13:34:08 +05:30
2023-02-28 11:31:04 +05:30
export async function validateBatch(
2023-02-27 18:39:18 +05:30
doc: StockMovement | StockTransfer | Invoice
) {
for (const row of doc.items ?? []) {
2023-02-28 11:31:04 +05:30
await validateItemRowBatch(row);
2023-02-27 18:39:18 +05:30
}
}
2023-02-28 11:31:04 +05:30
async function validateItemRowBatch(
2023-02-27 18:39:18 +05:30
doc: StockMovementItem | StockTransferItem | InvoiceItem
) {
2023-02-27 19:16:04 +05:30
const idx = doc.idx ?? 0 + 1;
2023-02-27 18:39:18 +05:30
const item = doc.item;
2023-02-28 11:31:04 +05:30
const batch = doc.batch;
2023-02-27 19:16:04 +05:30
if (!item) {
2023-02-27 18:39:18 +05:30
return;
}
2023-02-28 11:31:04 +05:30
const hasBatch = await doc.fyo.getValue(
2023-02-27 18:39:18 +05:30
ModelNameEnum.Item,
item,
2023-02-28 11:31:04 +05:30
'hasBatch'
2023-02-27 18:39:18 +05:30
);
2023-02-28 11:31:04 +05:30
if (!hasBatch && batch) {
2023-02-27 19:16:04 +05:30
throw new ValidationError(
[
2023-02-28 11:31:04 +05:30
doc.fyo.t`Batch set for row ${idx}.`,
2023-02-27 19:16:04 +05:30
doc.fyo.t`Item ${item} is not a batched item`,
].join(' ')
);
2023-02-27 18:39:18 +05:30
}
2023-02-28 11:31:04 +05:30
if (hasBatch && !batch) {
2023-02-27 19:16:04 +05:30
throw new ValidationError(
[
2023-02-28 11:31:04 +05:30
doc.fyo.t`Batch not set for row ${idx}.`,
2023-02-27 19:16:04 +05:30
doc.fyo.t`Item ${item} is a batched item`,
].join(' ')
);
}
2023-02-27 18:39:18 +05:30
}