false, 'error' => 'No data provided']); exit; } try { $pdo = db(); $pdo->beginTransaction(); $stmt = $pdo->prepare("INSERT INTO orders (outlet_id, table_number, total_amount, status) VALUES (?, ?, ?, 'pending')"); $stmt->execute([1, $data['table_number'] ?? '1', $data['total_amount']]); $order_id = $pdo->lastInsertId(); $item_stmt = $pdo->prepare("INSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES (?, ?, ?, ?)"); foreach ($data['items'] as $item) { $item_stmt->execute([$order_id, $item['id'], $item['quantity'], $item['price']]); } $pdo->commit(); echo json_encode(['success' => true, 'order_id' => $order_id]); } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } echo json_encode(['success' => false, 'error' => $e->getMessage()]); }