feat: initial foundation for collaborative canteen ordering system in vanilla PHP
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Controller;
|
||||
use App\Core\Http\Request;
|
||||
use App\Core\Http\Response;
|
||||
use App\Models\Order;
|
||||
use App\Models\Product;
|
||||
use App\Models\Customer;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
public function index(Request $request, Response $response): string
|
||||
{
|
||||
$status = $request->get('status', 'all');
|
||||
$search = $request->get('search');
|
||||
|
||||
$orders = Order::all($status, $search);
|
||||
|
||||
return $this->render('orders/index', [
|
||||
'title' => 'Order Management',
|
||||
'orders' => $orders,
|
||||
'currentStatus' => $status,
|
||||
'search' => $search,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Request $request, Response $response): string
|
||||
{
|
||||
$customers = Customer::all();
|
||||
$products = Product::all();
|
||||
|
||||
return $this->render('orders/create', [
|
||||
'title' => 'Create New Order',
|
||||
'customers' => $customers,
|
||||
'products' => $products,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request, Response $response): Response
|
||||
{
|
||||
$customerId = (int)$request->post('customer_id');
|
||||
$status = $request->post('status', 'Pending');
|
||||
$tax = (float)$request->post('tax', 0.00);
|
||||
$discount = (float)$request->post('discount', 0.00);
|
||||
$notes = trim((string)$request->post('notes', ''));
|
||||
$rawItems = $request->post('items', []);
|
||||
|
||||
if ($customerId <= 0) {
|
||||
return $this->redirect('/orders/create', null, 'Please select a customer.');
|
||||
}
|
||||
|
||||
if (empty($rawItems) || !is_array($rawItems)) {
|
||||
return $this->redirect('/orders/create', null, 'Please add at least one product item.');
|
||||
}
|
||||
|
||||
$subtotal = 0.0;
|
||||
$itemsToSave = [];
|
||||
|
||||
foreach ($rawItems as $item) {
|
||||
$productId = (int)($item['product_id'] ?? 0);
|
||||
$qty = (int)($item['quantity'] ?? 0);
|
||||
|
||||
if ($productId <= 0 || $qty <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product = Product::find($productId);
|
||||
if (!$product) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$unitPrice = (float)$product['price'];
|
||||
$lineTotal = $unitPrice * $qty;
|
||||
$subtotal += $lineTotal;
|
||||
|
||||
$itemsToSave[] = [
|
||||
'product_id' => $productId,
|
||||
'product_name' => $product['name'],
|
||||
'quantity' => $qty,
|
||||
'unit_price' => $unitPrice,
|
||||
'total_price' => $lineTotal,
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($itemsToSave)) {
|
||||
return $this->redirect('/orders/create', null, 'Please select valid products with quantities greater than 0.');
|
||||
}
|
||||
|
||||
$total = max(0, $subtotal + $tax - $discount);
|
||||
|
||||
try {
|
||||
$orderId = Order::createOrderWithItems([
|
||||
'customer_id' => $customerId,
|
||||
'status' => $status,
|
||||
'subtotal' => $subtotal,
|
||||
'tax' => $tax,
|
||||
'discount' => $discount,
|
||||
'total' => $total,
|
||||
'notes' => $notes,
|
||||
], $itemsToSave);
|
||||
|
||||
return $this->redirect("/orders/{$orderId}", 'Order created successfully!');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->redirect('/orders/create', null, 'Failed to create order: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function show(Request $request, Response $response, array $params): string|Response
|
||||
{
|
||||
$id = (int)($params['id'] ?? 0);
|
||||
$order = Order::find($id);
|
||||
|
||||
if (!$order) {
|
||||
return $this->redirect('/orders', null, 'Order not found.');
|
||||
}
|
||||
|
||||
return $this->render('orders/show', [
|
||||
'title' => "Order #{$order['order_number']}",
|
||||
'order' => $order,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateStatus(Request $request, Response $response, array $params): Response
|
||||
{
|
||||
$id = (int)($params['id'] ?? 0);
|
||||
$status = $request->post('status');
|
||||
|
||||
$validStatuses = ['Pending', 'Processing', 'Completed', 'Cancelled'];
|
||||
if (in_array($status, $validStatuses, true)) {
|
||||
Order::updateStatus($id, $status);
|
||||
return $this->redirect("/orders/{$id}", "Order status updated to '{$status}'.");
|
||||
}
|
||||
|
||||
return $this->redirect("/orders/{$id}", null, 'Invalid order status.');
|
||||
}
|
||||
|
||||
public function delete(Request $request, Response $response, array $params): Response
|
||||
{
|
||||
$id = (int)($params['id'] ?? 0);
|
||||
Order::delete($id);
|
||||
return $this->redirect('/orders', 'Order deleted successfully.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user