serial numbers

This commit is contained in:
2026-10-07 14:00:32 +05:30
committed by ImanThiyanga
parent 3abcfa6e6f
commit a788349138
89 changed files with 15320 additions and 216 deletions
+14 -5
View File
@@ -3,7 +3,7 @@
// (over-receipt tolerance, referential existence, concurrency) are never
// re-implemented here; the server's ProblemDetails is the final word (§3.3).
import { z } from "zod"
import { Warranty } from "@/types/master-data"
import { TrackingMode, Warranty } from "@/types/master-data"
export const grnHeaderSchema = z.object({
warehouseId: z.number({ error: "Select a warehouse" }).positive("Select a warehouse"),
@@ -18,6 +18,9 @@ export function validateLine(input: {
discountPct: string
vatPct: string
warranty: Warranty | null
trackingMode: TrackingMode | null
batchNo: string
/** Per-unit numbers: serial numbers for a Serial item, else warranty numbers for a Warranty item. */
warrantyNumbers: string[]
}): Record<string, string> {
const errors: Record<string, string> = {}
@@ -38,14 +41,20 @@ export function validateLine(input: {
if (input.vatPct !== "" && (Number.isNaN(vatPct) || vatPct < 0 || vatPct > 100))
errors.vatPct = "VAT must be 0–100%"
if (input.warranty === "Warranty") {
if (input.trackingMode === "Batch" && !input.batchNo.trim()) errors.batchNo = "Enter the batch number"
const serial = input.trackingMode === "Serial"
if (serial && !Number.isNaN(qty) && !Number.isInteger(qty)) errors.qty = "Serial-tracked items are received in whole units"
if (serial || input.warranty === "Warranty") {
const label = serial ? "serial number" : "warranty number"
const numbers = input.warrantyNumbers.map((s) => s.trim()).filter((s) => s.length > 0)
if (numbers.length === 0) {
errors.warrantyNumbers = "Enter one warranty number per unit"
errors.warrantyNumbers = `Enter one ${label} per unit`
} else if (!Number.isNaN(qty) && numbers.length !== qty) {
errors.warrantyNumbers = `Enter exactly ${qty || 0} warranty number(s) — got ${numbers.length}`
errors.warrantyNumbers = `Enter exactly ${qty || 0} ${label}(s) — got ${numbers.length}`
} else if (new Set(numbers.map((s) => s.toLowerCase())).size !== numbers.length) {
errors.warrantyNumbers = "Warranty numbers must be unique"
errors.warrantyNumbers = `${serial ? "Serial" : "Warranty"} numbers must be unique`
}
}