[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; const currentItem = newItems[index] as BookOutItemSchemaType;
if (field === "quantity") { if (field === "quantity") {
currentItem[field] = Number(value) || 0; // Convert to number here currentItem[field] = Number(value) || 0;
} else { } else {
currentItem[field] = value; currentItem[field] = value;
} }
@ -150,15 +150,17 @@ export function BookOutForm({ setOpen, refreshData }: BookOutFormProps) {
async function onSubmit(values: z.infer<typeof bookOutFormSchema>) { async function onSubmit(values: z.infer<typeof bookOutFormSchema>) {
try { try {
const bookOutData = { // Create one book out record per item with its quantity
bookOutDate: values.bookOutDate.toISOString(), for (const item of values.items) {
customer: values.customer, const bookOutData = {
items: values.items.map(item => item.stockItemId), bookOutDate: values.bookOutDate.toISOString(),
quantity: values.items.map(item => item.quantity), customer: values.customer,
date_booked_out: values.bookOutDate.toISOString(), 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); };
await pb.collection('book_out_records').create(bookOutData);
}
showSuccess("Stock booked out successfully!"); showSuccess("Stock booked out successfully!");
form.reset(); form.reset();
setOpen(false); setOpen(false);