import { NextRequest, NextResponse } from "next/server"; import { getDb } from "@/lib/db"; export async function GET( _request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const sql = getDb(); const { id } = await params; const orderRows = await sql( `SELECT po.*, c.name AS customer_name, c.code AS customer_code, c.email AS customer_email, c.phone AS customer_phone, c.address AS customer_address, c.contact_person AS customer_contact FROM purchase_orders po JOIN customers c ON c.id = po.customer_id WHERE po.id = $1`, [Number(id)] ); if (orderRows.length === 0) { return NextResponse.json({ error: "Order not found" }, { status: 404 }); } const items = await sql( `SELECT poi.*, p.name AS product_name, p.sku AS product_sku, (poi.quantity * poi.unit_price) AS line_total, COALESCE(poi.shipped_quantity, 0) AS shipped_quantity, (poi.quantity - COALESCE(poi.shipped_quantity, 0)) AS remaining_quantity FROM purchase_order_items poi JOIN products p ON p.id = poi.product_id WHERE poi.purchase_order_id = $1 ORDER BY poi.created_at ASC`, [Number(id)] ); return NextResponse.json({ ...orderRows[0], items }); } catch (error) { console.error("[v0] Order GET error:", error); return NextResponse.json( { error: error instanceof Error ? error.message : "Failed to fetch order" }, { status: 500 } ); } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const sql = getDb(); const { id } = await params; const body = await request.json(); const { customer_id, po_number, customer_ref, status, order_date, due_date, notes, items } = body; const orderRows = await sql( `UPDATE purchase_orders SET customer_id = $1, po_number = $2, customer_ref = $3, status = $4, order_date = $5, due_date = $6, notes = $7, updated_at = NOW() WHERE id = $8 RETURNING *`, [customer_id, po_number, customer_ref || null, status, order_date, due_date || null, notes || null, Number(id)] ); if (orderRows.length === 0) { return NextResponse.json({ error: "Order not found" }, { status: 404 }); } // Replace items if (items && Array.isArray(items)) { await sql(`DELETE FROM purchase_order_items WHERE purchase_order_id = $1`, [Number(id)]); for (const item of items) { await sql( `INSERT INTO purchase_order_items (purchase_order_id, product_id, quantity, unit_price, notes) VALUES ($1, $2, $3, $4, $5)`, [Number(id), item.product_id, item.quantity, item.unit_price || 0, item.notes || null] ); } } return NextResponse.json(orderRows[0]); } catch (error) { console.error("[v0] Order PUT error:", error); return NextResponse.json( { error: error instanceof Error ? error.message : "Failed to update order" }, { status: 500 } ); } } export async function DELETE( _request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const sql = getDb(); const { id } = await params; await sql(`DELETE FROM purchase_order_items WHERE purchase_order_id = $1`, [Number(id)]); await sql(`DELETE FROM purchase_orders WHERE id = $1`, [Number(id)]); return NextResponse.json({ success: true }); } catch (error) { console.error("[v0] Order DELETE error:", error); return NextResponse.json( { error: error instanceof Error ? error.message : "Failed to delete order" }, { status: 500 } ); } }