From fe4041917103c9b4d21726d2960ced0526d29afb Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 23 Mar 2026 16:59:34 -0500 Subject: [PATCH] all --- components/purchase-order-create-form.tsx | 153 ++++++++++++++++++++++ components/purchase-order-form.tsx | 124 ++++++++++++++++++ components/sales-order-create-form.tsx | 153 ++++++++++++++++++++++ components/sales-order-form.tsx | 124 ++++++++++++++++++ 4 files changed, 554 insertions(+) create mode 100644 components/purchase-order-create-form.tsx create mode 100644 components/purchase-order-form.tsx create mode 100644 components/sales-order-create-form.tsx create mode 100644 components/sales-order-form.tsx diff --git a/components/purchase-order-create-form.tsx b/components/purchase-order-create-form.tsx new file mode 100644 index 0000000..721fe69 --- /dev/null +++ b/components/purchase-order-create-form.tsx @@ -0,0 +1,153 @@ +"use client"; + +import { useState } from "react"; +import { createPurchaseOrder } from "@/lib/actions"; +import type { ContactRow, OrderItemOption } from "@/lib/types"; + +type PurchaseOrderCreateFormProps = { + vendors: ContactRow[]; + items: OrderItemOption[]; +}; + +type DraftLine = { + rowId: number; + partId: number | ""; + quantity: number; + amount: number; +}; + +function createEmptyLine(rowId: number): DraftLine { + return { + rowId, + partId: "", + quantity: 1, + amount: 0 + }; +} + +export function PurchaseOrderCreateForm({ vendors, items }: PurchaseOrderCreateFormProps) { + const [lines, setLines] = useState([createEmptyLine(1)]); + + function updateLine(rowId: number, patch: Partial) { + setLines((current) => + current.map((line) => { + if (line.rowId !== rowId) { + return line; + } + + const nextLine = { ...line, ...patch }; + if (patch.partId && typeof patch.partId === "number") { + const item = items.find((candidate) => candidate.id === patch.partId); + if (item) { + nextLine.amount = item.unitCost; + } + } + return nextLine; + }) + ); + } + + function addLine() { + setLines((current) => [...current, createEmptyLine(Date.now())]); + } + + function removeLine(rowId: number) { + setLines((current) => (current.length === 1 ? current : current.filter((line) => line.rowId !== rowId))); + } + + const payload = JSON.stringify( + lines + .filter((line) => typeof line.partId === "number") + .map((line) => ({ + partId: line.partId, + quantity: line.quantity, + amount: line.amount + })) + ); + + return ( +
+
+ + +
+
+ +
+ {lines.map((line, index) => ( +
+
+
+ + +
+
+ + updateLine(line.rowId, { quantity: Number(event.target.value) || 0 })} + /> +
+
+ + updateLine(line.rowId, { amount: Number(event.target.value) || 0 })} + /> +
+ +
+
+ ))} +
+
+
+ +
+ +
+ +