65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { z } from "zod";
|
|
import { uuid } from "./commerce.ts";
|
|
import { bomDetail } from "./bom.ts";
|
|
|
|
const quantity = z.string().regex(/^(0|[1-9]\d{0,8})\.\d{3}$/, "Use three decimal places");
|
|
const signedQuantity = z.string().regex(/^-?\d+\.\d{3}$/);
|
|
const packagingLine = z.object({
|
|
bomLineId: uuid,
|
|
usedQuantity: quantity,
|
|
wastedQuantity: quantity,
|
|
wasteReason: z.string().trim().min(1).max(500).optional(),
|
|
}).strict().refine((line) => line.wastedQuantity === "0.000" || Boolean(line.wasteReason), {
|
|
path: ["wasteReason"],
|
|
message: "Enter a reason for wasted material.",
|
|
});
|
|
|
|
export const packagingRunInput = z.object({
|
|
bomId: uuid,
|
|
idempotencyKey: uuid,
|
|
lines: z.array(packagingLine).min(1).max(100)
|
|
.refine((lines) => new Set(lines.map((line) => line.bomLineId)).size === lines.length, "Record each material once."),
|
|
}).strict();
|
|
|
|
const record = z.object({
|
|
id: uuid,
|
|
batchId: uuid,
|
|
createdAt: z.string().datetime(),
|
|
});
|
|
|
|
export const packagingRun = record.extend({
|
|
bomId: uuid,
|
|
lines: z.array(z.object({
|
|
bomLineId: uuid,
|
|
materialId: uuid,
|
|
materialName: z.string(),
|
|
materialCode: z.string(),
|
|
unit: bomDetail.shape.lines.element.shape.unit,
|
|
plannedQuantity: quantity,
|
|
usedQuantity: quantity,
|
|
wastedQuantity: quantity,
|
|
varianceQuantity: signedQuantity,
|
|
wasteReason: z.string().nullable(),
|
|
})),
|
|
});
|
|
|
|
const stockMovement = record.extend({ warehouseId: uuid });
|
|
const debit = z.object({
|
|
id: uuid,
|
|
materialId: uuid,
|
|
stockItemId: uuid,
|
|
delta: signedQuantity.refine((value) => value.startsWith("-") && value !== "-0.000"),
|
|
});
|
|
export const productionDebit = stockMovement.extend({
|
|
entries: z.array(debit.extend({ kind: z.enum(["PRODUCTION_CONSUMED", "PRODUCTION_WASTED"]) })),
|
|
});
|
|
export const packagingDebit = stockMovement.extend({
|
|
entries: z.array(debit.extend({ kind: z.enum(["PACKAGING_USED", "PACKAGING_WASTED"]) })),
|
|
});
|
|
export const finishedGoodsReceipt = stockMovement.extend({
|
|
variantId: uuid,
|
|
stockItemId: uuid,
|
|
ledgerEntryId: uuid,
|
|
quantity: z.number().int().min(1).max(100000),
|
|
});
|