diff --git a/src/models/Order.php b/src/models/Order.php index 8af5b10..cd0ca34 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -208,52 +208,61 @@ public function addOrderProduct(OrderProduct $newOrderProduct): bool public function calculateTotalPrice(): float { - // TODO: Use a single query to calculate total price - return 0; + $query = "SELECT SUM(unit_price * quantity) AS total_price + FROM order_product WHERE order_id = :order_id"; + + $result = self::get_row($query, ['order_id' => $this->order_id]); + + if ($result) { + return (float) $result->total_price; + } + + return 0.0; } public function toHTML(): string { - // TODO: get order products and names of each product using a single query - - $html = << - - - Product - Quantity - Price per Unit - Total Price - - - - HTML; - - // Iterate through each product in the order - foreach ($this->products as $product) { - // Get the product details - $productName = $product['product']->getName(); - $quantity = $product['quantity']; - $pricePerUnit = $product['product']->getPrice(); - $totalPrice = $quantity * $pricePerUnit; - - // Add a row for the product in the HTML table - $html .= << - $productName - Qty $quantity - \$$pricePerUnit - \$$totalPrice - - HTML; - } + $html = << + + + Product + Quantity + Price per Unit + Total Price + + + + HTML; + + $query = "SELECT op.product_id, op.quantity, op.unit_price, p.name + FROM order_product op + JOIN product p ON op.product_id = p.product_id + WHERE op.order_id = :order_id"; + + $orderProducts = self::query($query, ['order_id' => $this->order_id]); + + foreach ($orderProducts as $orderProduct) { + $productName = $orderProduct->name; + $quantity = $orderProduct->quantity; + $pricePerUnit = $orderProduct->unit_price; + $totalPrice = $pricePerUnit * $quantity; - // Close the HTML table $html .= << - + + $productName + Qty $quantity + \$$pricePerUnit + \$$totalPrice + HTML; + } + + $html .= << + + HTML; - return $html; + return $html; } }