From 129248ef4a86354bd18ddce0af176eb8585673c1 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Fri, 17 May 2024 20:10:48 +0400 Subject: [PATCH 1/7] add Order model method to fetch orders by client ID and update Profile controller to use it --- src/controllers/Profile.php | 16 +++++----------- src/models/Order.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index f7eca130..24151cac 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 { @@ -197,17 +198,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 5a88b5d9..c4b61456 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -9,6 +9,7 @@ use PDOException; use Steamy\Core\Model; use Steamy\Core\Utility; +use Steamy\Core\Database; class Order { @@ -264,6 +265,23 @@ private static function getOrderProducts(int $order_id): array return $order_products_arr; } + public static function getOrdersByClientId(int $client_id, int $limit = 5): array { + // Perform database query to fetch orders by client ID + $query = "SELECT * FROM `order` WHERE `client_id` = :client_id ORDER BY `created_date` DESC LIMIT :limit"; + $params = [':client_id' => $client_id, ':limit' => $limit]; + // Execute the query + $orders = self::query($query, $params); + + // Check if orders were found + if ($orders === false) { + // No orders found, return an empty array + return []; + } + + // Orders found, return them + return $orders; + } + public function getOrderID(): int { From 925386d9fc41b7ba3707709b34a55516896f78f0 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 18 May 2024 17:30:58 +0400 Subject: [PATCH 2/7] resolve the problem with sql but when orders are ordered the tab 'orders' and 'setting' got disable --- src/models/Order.php | 31 ++++++++++++++++--------------- src/views/Profile.php | 6 +----- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index c4b61456..fbeddf2a 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; @@ -265,21 +266,21 @@ private static function getOrderProducts(int $order_id): array return $order_products_arr; } - public static function getOrdersByClientId(int $client_id, int $limit = 5): array { - // Perform database query to fetch orders by client ID - $query = "SELECT * FROM `order` WHERE `client_id` = :client_id ORDER BY `created_date` DESC LIMIT :limit"; - $params = [':client_id' => $client_id, ':limit' => $limit]; - // Execute the query - $orders = self::query($query, $params); - - // Check if orders were found - if ($orders === false) { - // No orders found, return an empty array - return []; - } - - // Orders found, return them - return $orders; + public static function getOrdersByClientId(int $client_id, int $limit = 5): array + { + $db = self::connect(); + $stmt = $db->prepare(' + SELECT order_id, created_date, status + FROM `order` + WHERE client_id = :client_id + ORDER BY created_date DESC + LIMIT :limit + '); + $stmt->bindParam(':client_id', $client_id, PDO::PARAM_INT); + $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_OBJ); } diff --git a/src/views/Profile.php b/src/views/Profile.php index 10d5ad12..d30a50b4 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -86,7 +86,6 @@ Date Order ID - Total cost Status Actions @@ -95,13 +94,11 @@ foreach ($orders as $order) { $date = htmlspecialchars($order->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); echo <<< EOL $date $id - $cost $status @@ -201,5 +198,4 @@ function openTab(evt, tabName) { }); - - + \ No newline at end of file From d5ee3f167f71be069e1736bb96df40422d11bb41 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 18 May 2024 21:38:34 +0400 Subject: [PATCH 3/7] update variable names in Profile.php: $order->date to $order->created_date and $order->id to $order->order_id --- src/views/Profile.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/views/Profile.php b/src/views/Profile.php index d30a50b4..625c1954 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -92,8 +92,8 @@ date); - $id = filter_var($order->id, FILTER_SANITIZE_NUMBER_INT); + $date = htmlspecialchars($order->created_date); + $id = filter_var($order->order_id, FILTER_SANITIZE_NUMBER_INT); $status = htmlspecialchars($order->status); echo <<< EOL From 085f1854fa70cc1dec73ee7ad39d9f7470ef3a56 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sun, 19 May 2024 17:46:13 +0400 Subject: [PATCH 4/7] update Order model to include total price in getOrdersByClientId method and modified Profile view to display additional order details. --- src/models/Order.php | 29 ++++++++++++++++++++--------- src/views/Profile.php | 11 +++++++++-- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index fbeddf2a..79d9ebc5 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -266,15 +266,25 @@ private static function getOrderProducts(int $order_id): array return $order_products_arr; } - public static function getOrdersByClientId(int $client_id, int $limit = 5): array - { - $db = self::connect(); - $stmt = $db->prepare(' - SELECT order_id, created_date, status - FROM `order` - WHERE client_id = :client_id - ORDER BY created_date DESC - LIMIT :limit + /** + * Retrieves a list of orders for a specific client, including the total price of products in each order. + * + * @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 array An array of stdClass objects, each containing order details and the total price. + * @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, SUM(op.unit_price * op.quantity) AS total_price + FROM `order` o + JOIN order_product op ON o.order_id = op.order_id + WHERE o.client_id = :client_id + GROUP BY o.order_id, o.created_date, o.status, o.store_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); @@ -284,6 +294,7 @@ public static function getOrdersByClientId(int $client_id, int $limit = 5): arra } + public function getOrderID(): int { return $this->order_id; diff --git a/src/views/Profile.php b/src/views/Profile.php index 625c1954..ede78919 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -10,6 +10,7 @@ */ use Steamy\Model\Client; +use Steamy\Model\Order; ?> @@ -84,9 +85,11 @@
- + + + @@ -94,12 +97,16 @@ foreach ($orders as $order) { $date = htmlspecialchars($order->created_date); $id = filter_var($order->order_id, FILTER_SANITIZE_NUMBER_INT); + $storeid = filter_var($order->store_id, FILTER_SANITIZE_NUMBER_INT); $status = htmlspecialchars($order->status); + $totalPrice = $order->total_price; echo <<< EOL - + + + From b7ee6657874468964ac000294cbc80c7b729f458 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Mon, 20 May 2024 19:26:40 +0400 Subject: [PATCH 5/7] refactore Order model to return array of Order objects instead of stdClass objects, and update Profile view to accommodate changes --- src/models/Order.php | 30 +++++++++++++++++++++++------- src/views/Profile.php | 17 +++++++---------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index 79d9ebc5..e991f048 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -10,7 +10,6 @@ use PDOException; use Steamy\Core\Model; use Steamy\Core\Utility; -use Steamy\Core\Database; class Order { @@ -267,22 +266,20 @@ private static function getOrderProducts(int $order_id): array } /** - * Retrieves a list of orders for a specific client, including the total price of products in each order. + * 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 array An array of stdClass objects, each containing order details and the total price. + * @return Order[] An array of Order objects. * @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, SUM(op.unit_price * op.quantity) AS total_price + SELECT o.order_id, o.created_date, o.status, o.store_id, o.pickup_date, o.client_id FROM `order` o - JOIN order_product op ON o.order_id = op.order_id WHERE o.client_id = :client_id - GROUP BY o.order_id, o.created_date, o.status, o.store_id ORDER BY o.created_date DESC LIMIT :limit; '); @@ -290,7 +287,26 @@ public static function getOrdersByClientId(int $client_id, int $limit = 5): arra $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_OBJ); + $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), + ); + } + + return $orders; } diff --git a/src/views/Profile.php b/src/views/Profile.php index ede78919..a58016b8 100644 --- a/src/views/Profile.php +++ b/src/views/Profile.php @@ -6,12 +6,9 @@ * The following attributes are defined in controllers/Profile.php * * @var $client Client signed in client - * @var $orders array array of orders + * @var $orders Order[] array of orders */ -use Steamy\Model\Client; -use Steamy\Model\Order; - ?>
Date Order IDStore IDDate StatusTotal Price Actions
$date $id$storeid$date $status$totalPrice