Skip to content

Commit

Permalink
Merge pull request #187 from Divyesh000/actualorders
Browse files Browse the repository at this point in the history
Show actual orders on profile page
  • Loading branch information
creme332 authored May 21, 2024
2 parents accbdcc + 73e986b commit cec9a2a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
16 changes: 5 additions & 11 deletions src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Steamy\Model\Client;
use Steamy\Model\District;
use Steamy\Model\Location;
use Steamy\Model\Order;

class Profile
{
Expand Down Expand Up @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Steamy\Model;

use PDO;
use DateTime;
use Exception;
use PDOException;
Expand Down Expand Up @@ -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
{
Expand Down
35 changes: 19 additions & 16 deletions src/views/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

?>

Expand Down Expand Up @@ -85,30 +86,33 @@
<figure>
<table>
<tr>
<th>Date</th>
<th>Order ID</th>
<th>Total cost</th>
<th>Store ID</th>
<th>Date</th>
<th>Status</th>
<th>Total Price</th>
<th>Actions</th>
</tr>

<?php
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);
$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
<tr>
<td>$date</td>
<td>$id</td>
<td>$cost</td>
<td>$storeid</td>
<td>$date</td>
<td>$status</td>
<td>\$$totalPrice</td>
<td class="grid">
<button>cancel</button>
</td>
</tr>
EOL;
EOL;
}

?>
Expand Down Expand Up @@ -201,9 +205,8 @@ function openTab(evt, tabName) {
},
);
});


</script>

<?php
if ($show_account_deletion_confirmation) : ?>
<dialog open>
Expand All @@ -219,4 +222,4 @@ function openTab(evt, tabName) {
</article>
</dialog>
<?php
endif; ?>
endif; ?>

0 comments on commit cec9a2a

Please sign in to comment.