From 1e3b0e8d0f1c914279c5dc2dc121b5782b642d51 Mon Sep 17 00:00:00 2001 From: Carl Tabuso Date: Sun, 23 Jun 2024 18:46:45 +0800 Subject: [PATCH] Revert "feat: SPD-125 - Progress of create order page to database" --- src/finish-order.php | 42 ++--------------- src/javascript/billing.js | 94 ++++++++++++--------------------------- 2 files changed, 33 insertions(+), 103 deletions(-) diff --git a/src/finish-order.php b/src/finish-order.php index c189768..52f18a2 100644 --- a/src/finish-order.php +++ b/src/finish-order.php @@ -4,42 +4,8 @@ require_once FileUtils::normalizeFilePath('includes/db-connector.php'); include_once FileUtils::normalizeFilePath('includes/default-timezone.php'); -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $conn = new mysqli($db_host, $db_user, $db_pass, $db_name); +header('Content-Type: application/json'); - if ($conn->connect_error) { - die(json_encode(['success' => false, 'message' => 'Connection failed: ' . $conn->connect_error])); - } - - $subtotal = (float) $_POST['subtotal']; - $discount = (float) $_POST['discount']; - $grand_total = (float) $_POST['grand_total']; - $change = (float) $_POST['change']; - $items = json_decode($_POST['items'], true); - $user_id = 54; - - // Insert transaction into `transaction` table - $stmt = $conn->prepare("INSERT INTO `transaction` (user_id, timestamp, total_amount, receipt) VALUES (?, NOW(), ?, ?)"); - $stmt->bind_param('idd', $user_id, $grand_total); - - if ($stmt->execute()) { - $transaction_id = $stmt->insert_id; - - // Insert items into `items_purchased` table - $stmt = $conn->prepare("INSERT INTO `items_purchased` (transaction_id, item_id, quantity, price_per_unit) VALUES (?, ?, ?, ?)"); - foreach ($items as $item) { - $stmt->bind_param('iiid', $transaction_id, $item['id'], $item['quantity'], $item['price']); - $stmt->execute(); - } - - $stmt->close(); - $conn->close(); - echo json_encode(['success' => true]); - } else { - echo json_encode(['success' => false, 'message' => 'Failed to insert transaction']); - } -} else { - echo json_encode(['success' => false, 'message' => 'Invalid request method']); -} - -?> \ No newline at end of file +if($_SERVER["REQUEST_METHOD"] === "POST") { + // SQL to insert to transaction and items purchased table +} \ No newline at end of file diff --git a/src/javascript/billing.js b/src/javascript/billing.js index c5ae54d..3a49e5d 100644 --- a/src/javascript/billing.js +++ b/src/javascript/billing.js @@ -103,16 +103,6 @@ $(document).ready(function () { const tenderedAmount = parseFloat($("#tenderedAmount").val()); const change = tenderedAmount - parseFloat($("#grandTotalValue").text()); - const items = []; - $("#orderCart tr").each(function () { - const itemData = $(this).find("td"); - items.push({ - id: itemData.data("item-id"), - quantity: parseInt(itemData.eq(0).find("p").text().trim().slice(1)), - price: parseFloat(itemData.eq(2).find(".text-carbon-grey").text().trim().replace(/[^\d.]/g, "")) - }); - }); - $("#orderConfirmationModal").modal("hide"); $("#receiptModal").modal("show"); @@ -122,65 +112,39 @@ $(document).ready(function () { $("#tenderedAmount").val(""); $("#changeDisplay").html(""); + // $.ajax({ + // url: "finish-order.php", + // type: "POST", + // dataType: "json", + // success: function (response) { + // // code here + // }, + // }); printOrderConfirmation(tenderedAmount); - checkCart(); - - $.ajax({ - type: "POST", - url: "clear_cart.php", - success: function (response) { - let data = JSON.parse(response); - if (data.status === "success") { - $("#subtotalValue").text("0.00"); - $("#orderCart") - .empty() - .append( - 'No items in cart' - ); - $("#cancelOrder").on("click", clearCart); - $("#proceedOrder").prop("disabled", true); - } else { - alert(data.message); - } - }, - error: function () { - alert("An error occurred. Please try again."); - }, - }); - - const subtotal = items.reduce((acc, item) => acc + (item.quantity * item.price), 0); - - console.log("Sending data to server:", { - subtotal: subtotal, - discount: parseFloat($("#discountInput").val()) || 0, - grand_total: parseFloat($("#grandTotalValue").text()), - tendered_amount: tenderedAmount, - change: change, - items: JSON.stringify(items) - }); - $.ajax({ - url: "finish-order.php", - type: "POST", - dataType: "json", - data: { - subtotal: subtotal, - discount: parseFloat($("#discountInput").val()) || 0, - grand_total: parseFloat($("#grandTotalValue").text()), - tendered_amount: tenderedAmount, - change: change, - items: JSON.stringify(items) - }, - success: function (response) { - if (response.success) { - console.log("Order processed successfully"); - } else { - alert("Error: " + response.message); - } - }, - error: function (xhr, status, error) { + type: "POST", + url: "clear_cart.php", + success: function (response) { + let data = JSON.parse(response); + if (data.status === "success") { + // Clear the subtotal + $("#subtotalValue").text("0.00"); + // Clear the order cart display and maintain table striping + $("#orderCart") + .empty() + .append( + 'No items in cart' + ); + $("#cancelOrder").on("click", clearCart); + $("#proceedOrder").prop("disabled", true); + } else { + alert(data.message); // Display error message } + }, + error: function () { + alert("An error occurred. Please try again."); + }, }); });