diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index be9e8e0..d64fd06 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -9,6 +9,7 @@ use Steamy\Model\Client; use Steamy\Model\District; use Steamy\Model\Location; +use Steamy\Model\Order; class Profile { @@ -218,17 +219,10 @@ public function index(): void return; } - // TODO: fetch 5 latest orders - $this->view_data["orders"] = array_fill( - 0, - 5, - (object)[ - 'date' => '16/01/2024', - 'id' => 4343, - 'cost' => 100.00, - 'status' => 'Completed' - ] - ); + // Fetch orders for the signed-in client + $orders = Order::getOrdersByClientId($this->signed_client->getUserID()); + + $this->view_data["orders"] = $orders; // initialize user details for template $this->view_data["name"] = $this->signed_client->getFirstName() . " " . $this->signed_client->getLastName(); diff --git a/src/models/Order.php b/src/models/Order.php index 5a88b5d..e89a57f 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -4,6 +4,7 @@ namespace Steamy\Model; +use PDO; use DateTime; use Exception; use PDOException; @@ -264,6 +265,52 @@ private static function getOrderProducts(int $order_id): array return $order_products_arr; } + /** + * Retrieves a list of orders for a specific client. + * + * @param int $client_id The ID of the client whose orders are to be retrieved. + * @param int $limit The maximum number of orders to retrieve. Defaults to 5. + * @return Order[] An array of Order objects ordered in descending order of created_date + * @throws PDOException If there is an error executing the database query. + */ + public static function getOrdersByClientId(int $client_id, int $limit = 5): array + { + $db = self::connect(); + $stmt = $db->prepare( + ' + SELECT o.order_id, o.created_date, o.status, o.store_id, o.pickup_date, o.client_id + FROM `order` o + WHERE o.client_id = :client_id + ORDER BY o.created_date DESC + LIMIT :limit; + ' + ); + $stmt->bindParam(':client_id', $client_id, PDO::PARAM_INT); + $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); + $stmt->execute(); + + $orderDataArray = $stmt->fetchAll(PDO::FETCH_OBJ); + $orders = []; + + foreach ($orderDataArray as $orderData) { + // Get the line items for this order + $lineItems = self::getOrderProducts((int)$orderData->order_id); + + // Create an Order object with the retrieved data + $orders[] = new Order( + store_id: (int)$orderData->store_id, + client_id: (int)$orderData->client_id, + line_items: $lineItems, + order_id: (int)$orderData->order_id, + pickup_date: $orderData->pickup_date ? Utility::stringToDate($orderData->pickup_date) : null, + status: OrderStatus::from($orderData->status), + created_date: Utility::stringToDate($orderData->created_date), + ); + } + $db = null; + return $orders; + } + public function getOrderID(): int { diff --git a/src/views/Profile.php b/src/views/Profile.php index 54d9425..14f600b 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -3,14 +3,15 @@ declare(strict_types=1); /** - * The following attributes are defined in controllers/Profile.php + * The following attributes are defined in controllers/Profile.php: * - * @var $client Client signed in client - * @var $show_account_deletion_confirmation bool Whether to display a confirmation dialog for account deletion - * @var $orders array array of orders + * @var Client $client signed in client + * @var Order[] $orders array of orders + * @var bool $show_account_deletion_confirmation Whether to display a confirmation dialog for account deletion */ use Steamy\Model\Client; +use Steamy\Model\Order; ?> @@ -85,30 +86,33 @@
- - + + + date); - $id = filter_var($order->id, FILTER_SANITIZE_NUMBER_INT); - $cost = filter_var($order->cost, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); - $status = htmlspecialchars($order->status); + $date = htmlspecialchars($order->getCreatedDate()->format('Y-m-d H:i:s')); + $id = filter_var($order->getOrderID(), FILTER_SANITIZE_NUMBER_INT); + $storeid = filter_var($order->getStoreID(), FILTER_SANITIZE_NUMBER_INT); + $status = htmlspecialchars(ucfirst($order->getStatus()->value)); + $totalPrice = htmlspecialchars(number_format($order->calculateTotalPrice(), 2)); echo <<< EOL - - + + + - EOL; + EOL; } ?> @@ -201,9 +205,8 @@ function openTab(evt, tabName) { }, ); }); - - + @@ -219,4 +222,4 @@ function openTab(evt, tabName) { +endif; ?> \ No newline at end of file
Date Order IDTotal costStore IDDate StatusTotal Price Actions
$date $id$cost$storeid$date $status\$$totalPrice