114 lines
3.8 KiB
PHP
114 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use App\Core\Http\Request;
|
|
use App\Core\Http\Response;
|
|
use App\Core\Auth;
|
|
use App\Models\EmployeeOrder;
|
|
use App\Models\DailySession;
|
|
|
|
class EmployeeOrderController extends Controller
|
|
{
|
|
public function store(Request $request, Response $response): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$sessionId = (int)$request->post('session_id');
|
|
$userId = (int)Auth::id();
|
|
$generalNotes = trim((string)$request->post('notes', ''));
|
|
$rawItems = $request->post('items', []);
|
|
|
|
$session = DailySession::find($sessionId);
|
|
if (!$session) {
|
|
return $this->redirect('/', null, 'Session not found.');
|
|
}
|
|
|
|
if ($session['status'] === 'closed' || $session['status'] === 'completed') {
|
|
return $this->redirect("/sessions/{$sessionId}", null, 'Order placement is currently closed for this session.');
|
|
}
|
|
|
|
if (empty($rawItems) || !is_array($rawItems)) {
|
|
return $this->redirect("/sessions/{$sessionId}", null, 'Please select at least one menu item.');
|
|
}
|
|
|
|
// If user already had an order in this session, remove old order first to update
|
|
$existingOrder = EmployeeOrder::findUserOrderBySession($sessionId, $userId);
|
|
if ($existingOrder) {
|
|
EmployeeOrder::delete((int)$existingOrder['id']);
|
|
}
|
|
|
|
$itemsToSave = [];
|
|
foreach ($rawItems as $item) {
|
|
$name = trim((string)($item['item_name'] ?? ''));
|
|
$qty = max(1, (int)($item['quantity'] ?? 1));
|
|
$notes = trim((string)($item['notes'] ?? ''));
|
|
$menuId = !empty($item['session_menu_id']) ? (int)$item['session_menu_id'] : null;
|
|
|
|
if (empty($name)) {
|
|
continue;
|
|
}
|
|
|
|
$unitPrice = 0.0;
|
|
if ($menuId && !empty($session['menus'])) {
|
|
foreach ($session['menus'] as $m) {
|
|
if ((int)$m['id'] === $menuId) {
|
|
$unitPrice = (float)$m['final_price'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
$itemsToSave[] = [
|
|
'session_menu_id' => $menuId,
|
|
'item_name' => $name,
|
|
'quantity' => $qty,
|
|
'unit_price' => $unitPrice,
|
|
'notes' => $notes,
|
|
];
|
|
}
|
|
|
|
if (empty($itemsToSave)) {
|
|
return $this->redirect("/sessions/{$sessionId}", null, 'No valid menu items selected.');
|
|
}
|
|
|
|
try {
|
|
EmployeeOrder::createOrder($sessionId, $userId, $generalNotes, $itemsToSave);
|
|
return $this->redirect("/sessions/{$sessionId}", 'Your lunch order has been submitted successfully!');
|
|
} catch (\Throwable $e) {
|
|
return $this->redirect("/sessions/{$sessionId}", null, 'Failed to submit order: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function togglePaid(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$orderId = (int)($params['id'] ?? 0);
|
|
$sessionId = (int)$request->post('session_id');
|
|
|
|
EmployeeOrder::togglePaid($orderId);
|
|
return $this->redirect("/sessions/{$sessionId}", 'Payment status updated.');
|
|
}
|
|
|
|
public function delete(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$orderId = (int)($params['id'] ?? 0);
|
|
$sessionId = (int)$request->post('session_id');
|
|
|
|
EmployeeOrder::delete($orderId);
|
|
return $this->redirect("/sessions/{$sessionId}", 'Order cancelled.');
|
|
}
|
|
}
|