feat: initial foundation for collaborative canteen ordering system in vanilla PHP
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database\Database;
|
||||
use PDO;
|
||||
|
||||
class EmployeeOrder
|
||||
{
|
||||
public static function getBySessionId(int $sessionId): array
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->prepare("
|
||||
SELECT eo.*, u.name as user_name, u.username as mattermost_username, u.email as user_email
|
||||
FROM employee_orders eo
|
||||
JOIN users u ON eo.user_id = u.id
|
||||
WHERE eo.session_id = ?
|
||||
ORDER BY eo.id ASC
|
||||
");
|
||||
$stmt->execute([$sessionId]);
|
||||
$orders = $stmt->fetchAll();
|
||||
|
||||
foreach ($orders as &$order) {
|
||||
$order['items'] = self::getItemsByOrderId((int)$order['id']);
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
public static function getItemsByOrderId(int $orderId): array
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->prepare("SELECT * FROM employee_order_items WHERE employee_order_id = ? ORDER BY id ASC");
|
||||
$stmt->execute([$orderId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public static function findUserOrderBySession(int $sessionId, int $userId): ?array
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->prepare("SELECT * FROM employee_orders WHERE session_id = ? AND user_id = ?");
|
||||
$stmt->execute([$sessionId, $userId]);
|
||||
$order = $stmt->fetch();
|
||||
|
||||
if (!$order) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$order['items'] = self::getItemsByOrderId((int)$order['id']);
|
||||
return $order;
|
||||
}
|
||||
|
||||
public static function createOrder(int $sessionId, int $userId, ?string $notes, array $items): int
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$db->beginTransaction();
|
||||
|
||||
try {
|
||||
$stmtOrder = $db->prepare("
|
||||
INSERT INTO employee_orders (session_id, user_id, status, notes, total_bill)
|
||||
VALUES (?, ?, 'unpaid', ?, 0.00)
|
||||
");
|
||||
$stmtOrder->execute([$sessionId, $userId, $notes]);
|
||||
$orderId = (int)$db->lastInsertId();
|
||||
|
||||
$stmtItem = $db->prepare("
|
||||
INSERT INTO employee_order_items (employee_order_id, session_menu_id, item_name, quantity, unit_price, total_price, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
|
||||
$totalBill = 0.0;
|
||||
|
||||
foreach ($items as $item) {
|
||||
$menuId = !empty($item['session_menu_id']) ? (int)$item['session_menu_id'] : null;
|
||||
$name = trim((string)($item['item_name'] ?? ''));
|
||||
$qty = max(1, (int)($item['quantity'] ?? 1));
|
||||
$unitPrice = (float)($item['unit_price'] ?? 0.0);
|
||||
$lineTotal = $qty * $unitPrice;
|
||||
$itemNotes = trim((string)($item['notes'] ?? ''));
|
||||
|
||||
if (empty($name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$stmtItem->execute([
|
||||
$orderId,
|
||||
$menuId,
|
||||
$name,
|
||||
$qty,
|
||||
$unitPrice,
|
||||
$lineTotal,
|
||||
$itemNotes
|
||||
]);
|
||||
|
||||
$totalBill += $lineTotal;
|
||||
}
|
||||
|
||||
// Update total bill
|
||||
$stmtUpdateTotal = $db->prepare("UPDATE employee_orders SET total_bill = ? WHERE id = ?");
|
||||
$stmtUpdateTotal->execute([$totalBill, $orderId]);
|
||||
|
||||
$db->commit();
|
||||
return $orderId;
|
||||
} catch (\Throwable $e) {
|
||||
$db->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public static function togglePaid(int $orderId): bool
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->prepare("
|
||||
UPDATE employee_orders
|
||||
SET status = CASE WHEN status = 'paid' THEN 'unpaid' ELSE 'paid' END,
|
||||
updated_at = datetime('now', 'localtime')
|
||||
WHERE id = ?
|
||||
");
|
||||
return $stmt->execute([$orderId]);
|
||||
}
|
||||
|
||||
public static function delete(int $orderId): bool
|
||||
{
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->prepare("DELETE FROM employee_orders WHERE id = ?");
|
||||
return $stmt->execute([$orderId]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user