diff --git a/src/components/book-out/BookOutForm.tsx b/src/components/book-out/BookOutForm.tsx index df77e96..776089f 100644 --- a/src/components/book-out/BookOutForm.tsx +++ b/src/components/book-out/BookOutForm.tsx @@ -128,7 +128,7 @@ export function BookOutForm({ setOpen, refreshData }: BookOutFormProps) { const currentItem = newItems[index] as BookOutItemSchemaType; if (field === "quantity") { - currentItem[field] = Number(value) || 0; // Convert to number here + currentItem[field] = Number(value) || 0; } else { currentItem[field] = value; } @@ -150,15 +150,17 @@ export function BookOutForm({ setOpen, refreshData }: BookOutFormProps) { async function onSubmit(values: z.infer) { try { - const bookOutData = { - bookOutDate: values.bookOutDate.toISOString(), - customer: values.customer, - items: values.items.map(item => item.stockItemId), - quantity: values.items.map(item => item.quantity), - date_booked_out: values.bookOutDate.toISOString(), - }; - - await pb.collection('book_out_records').create(bookOutData); + // Create one book out record per item with its quantity + for (const item of values.items) { + const bookOutData = { + bookOutDate: values.bookOutDate.toISOString(), + customer: values.customer, + items: [item.stockItemId], // single item ID in array + quantity: item.quantity, + date_booked_out: values.bookOutDate.toISOString(), + }; + await pb.collection('book_out_records').create(bookOutData); + } showSuccess("Stock booked out successfully!"); form.reset(); setOpen(false);