[dyad] Fixed quantity saving by creating separate book out records per item - wrote 1 file(s)

This commit is contained in:
[dyad] 2025-09-12 16:36:26 +02:00
parent 91106aef39
commit 195ed1582e

View file

@ -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<typeof bookOutFormSchema>) {
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);