2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +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 Normal View History

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-28 06:01:04 +00:00
export async function validateBatch(
2023-02-27 13:09:18 +00:00
doc: StockMovement | StockTransfer | Invoice
) {
for (const row of doc.items ?? []) {
2023-02-28 06:01:04 +00:00
await validateItemRowBatch(row);
2023-02-27 13:09:18 +00:00
}
}
2023-02-28 06:01:04 +00:00
async function validateItemRowBatch(
2023-02-27 13:09:18 +00:00
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;
2023-02-28 06:01:04 +00:00
const batch = doc.batch;
2023-02-27 13:46:04 +00:00
if (!item) {
2023-02-27 13:09:18 +00:00
return;
}
2023-02-28 06:01:04 +00:00
const hasBatch = await doc.fyo.getValue(
2023-02-27 13:09:18 +00:00
ModelNameEnum.Item,
item,
2023-02-28 06:01:04 +00:00
'hasBatch'
2023-02-27 13:09:18 +00:00
);
2023-02-28 06:01:04 +00:00
if (!hasBatch && batch) {
2023-02-27 13:46:04 +00:00
throw new ValidationError(
[
2023-02-28 06:01:04 +00:00
doc.fyo.t`Batch set for row ${idx}.`,
2023-02-27 13:46:04 +00:00
doc.fyo.t`Item ${item} is not a batched item`,
].join(' ')
);
2023-02-27 13:09:18 +00:00
}
2023-02-28 06:01:04 +00:00
if (hasBatch && !batch) {
2023-02-27 13:46:04 +00:00
throw new ValidationError(
[
2023-02-28 06:01:04 +00:00
doc.fyo.t`Batch not set for row ${idx}.`,
2023-02-27 13:46:04 +00:00
doc.fyo.t`Item ${item} is a batched item`,
].join(' ')
);
}
2023-02-27 13:09:18 +00:00
}