33 lines
1.0 KiB
PHP
33 lines
1.0 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
echo json_encode(['success' => 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()]);
|
|
}
|