66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { z } from "zod";
|
|
import { bomKind } from "./bom.ts";
|
|
import { currency, uuid } from "./commerce.ts";
|
|
import { materialInput } from "./procurement.ts";
|
|
|
|
const cents = z.string().regex(/^\d{1,10}\.\d{2}$/);
|
|
const quantity = z.string().regex(/^\d{1,9}\.\d{3}$/);
|
|
const rateRevision = z.number().int().min(1).max(1000);
|
|
const timestamp = z.string().datetime();
|
|
|
|
export const costRateInput = z.object({
|
|
currency,
|
|
unitCost: z.string().regex(/^(0|[1-9]\d{0,7})\.\d{4}$/, "Use four decimal places")
|
|
.refine((value) => value !== "0.0000", "Unit cost must be positive"),
|
|
}).strict();
|
|
|
|
export const costRate = z.object({
|
|
id: uuid,
|
|
materialId: uuid,
|
|
currency,
|
|
revision: rateRevision,
|
|
unitCost: z.string().regex(/^\d{1,8}(\.\d{1,4})?$/),
|
|
createdAt: timestamp,
|
|
});
|
|
|
|
const costLine = z.object({
|
|
bomLineId: uuid,
|
|
materialId: uuid,
|
|
materialCode: z.string(),
|
|
materialName: z.string(),
|
|
unit: materialInput.shape.unit,
|
|
rateId: uuid,
|
|
rateRevision,
|
|
unitCost: costRateInput.shape.unitCost,
|
|
});
|
|
|
|
export const bomEstimate = z.object({
|
|
bomId: uuid,
|
|
kind: bomKind,
|
|
currency,
|
|
outputQuantity: z.number().int().positive(),
|
|
estimatedAt: timestamp,
|
|
estimatedTotal: cents,
|
|
lines: z.array(costLine.extend({ quantity, estimatedCost: cents })),
|
|
});
|
|
|
|
export const snapshotInput = z.object({ currency, idempotencyKey: uuid }).strict();
|
|
export const batchSnapshot = z.object({
|
|
id: uuid,
|
|
batchId: uuid,
|
|
currency,
|
|
goodQuantity: z.number().int().min(0).max(100000),
|
|
createdAt: timestamp,
|
|
productionTotal: cents,
|
|
packagingTotal: cents,
|
|
total: cents,
|
|
lines: z.array(costLine.extend({
|
|
kind: bomKind,
|
|
usedQuantity: quantity,
|
|
wastedQuantity: quantity,
|
|
lineCost: cents,
|
|
})),
|
|
});
|
|
|
|
export const costCurrencies = currency.options.map((value) => ({ value, label: value }));
|