From eeff6a3e1eb3376d5ea6964c6560f61739801acf Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Tue, 7 May 2024 15:33:27 +0400 Subject: [PATCH 1/3] fetch order products in calculateTotalPrice and toHTML --- src/models/Order.php | 64 ++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index cf9ea45..ae77a1c 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -208,13 +208,22 @@ public function addOrderProduct(OrderProduct $newOrderProduct): bool public function calculateTotalPrice(): float { - // TODO: Use a single query to calculate total price - return 0; + $totalPrice = 0; + + // Fetch all products associated with this order + $orderProducts = self::getOrderProducts($this->order_id); + + // Calculate total price based on unit prices and quantities + foreach ($orderProducts as $orderProduct) { + $totalPrice += $orderProduct->unit_price * $orderProduct->quantity; + } + return $totalPrice; } public function toHTML(): string { - // TODO: get order products and names of each product using a single query + // Fetch all products associated with this order + $orderProducts = self::getOrderProducts($this->order_id); $html = << @@ -229,31 +238,40 @@ public function toHTML(): string 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; + // Iterate through each product in the order + foreach ($orderProducts as $orderProduct) { - // Add a row for the product in the HTML table - $html .= << - $productName - Qty $quantity - \$$pricePerUnit - \$$totalPrice - - HTML; - } + $productid=$orderProduct->product_id; - // Close the HTML table + $query = "SELECT name + FROM product + WHERE product_id = :product_id"; + + $data = self::query($query, ['product_id' => $productid]); + + // Get the product details + $productName = !empty($data) ? $data[0]->name : "N/A"; + $quantity = $orderProduct->quantity; + $pricePerUnit = $orderProduct->unit_price; + $totalPrice = self::calculateTotalPrice(); + + // Add a row for the product in the HTML table $html .= << + $productName + Qty $quantity + \$$pricePerUnit + \$$totalPrice + + HTML; + } + + // Close the HTML table + $html .= << - HTML; + HTML; - return $html; + return $html; } } From 68edfe694ad9ff748f75a1f072db438f1c9c675c Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Tue, 7 May 2024 16:32:20 +0400 Subject: [PATCH 2/3] refactor calculateTotalPrice and toHTML methods in Order model using a single query --- src/models/Order.php | 81 ++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index ae77a1c..659c828 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -208,68 +208,53 @@ public function addOrderProduct(OrderProduct $newOrderProduct): bool public function calculateTotalPrice(): float { - $totalPrice = 0; - - // Fetch all products associated with this order - $orderProducts = self::getOrderProducts($this->order_id); - - // Calculate total price based on unit prices and quantities - foreach ($orderProducts as $orderProduct) { - $totalPrice += $orderProduct->unit_price * $orderProduct->quantity; - } - return $totalPrice; + $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 { - // Fetch all products associated with this order - $orderProducts = self::getOrderProducts($this->order_id); - - $html = << - - - Product - Quantity - Price per Unit - Total Price - - - - HTML; - - // Iterate through each product in the order - foreach ($orderProducts as $orderProduct) { + $html = << + + + Product + Total Price + + + + HTML; - $productid=$orderProduct->product_id; + $query = "SELECT op.product_id, p.name + FROM order_product op + JOIN product p ON op.product_id = p.product_id + WHERE op.order_id = :order_id"; - $query = "SELECT name - FROM product - WHERE product_id = :product_id"; - - $data = self::query($query, ['product_id' => $productid]); + $orderProducts = self::query($query, ['order_id' => $this->order_id]); - // Get the product details - $productName = !empty($data) ? $data[0]->name : "N/A"; - $quantity = $orderProduct->quantity; - $pricePerUnit = $orderProduct->unit_price; + foreach ($orderProducts as $orderProduct) { + $productName = $orderProduct->name; $totalPrice = self::calculateTotalPrice(); - // Add a row for the product in the HTML table $html .= << - $productName - Qty $quantity - \$$pricePerUnit - \$$totalPrice - + + $productName + \$$totalPrice + HTML; } - // Close the HTML table $html .= << - + + HTML; return $html; From f6b9bf30de317c51aa77563d7246e709468f5161 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Wed, 8 May 2024 18:50:34 +0400 Subject: [PATCH 3/3] add quantity and price per unit columns to order product table display in order view & quantity x price per unit to get the total price of the product --- src/models/Order.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index 659c828..2c319ca 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -227,13 +227,15 @@ public function toHTML(): string Product + Quantity + Price per Unit Total Price HTML; - $query = "SELECT op.product_id, p.name + $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"; @@ -242,11 +244,15 @@ public function toHTML(): string foreach ($orderProducts as $orderProduct) { $productName = $orderProduct->name; - $totalPrice = self::calculateTotalPrice(); + $quantity = $orderProduct->quantity; + $pricePerUnit = $orderProduct->unit_price; + $totalPrice = $pricePerUnit * $quantity; $html .= << $productName + Qty $quantity + \$$pricePerUnit \$$totalPrice HTML;