Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: SPD-125 - Progress of create order page to database" #50

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions src/finish-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

?>
if($_SERVER["REQUEST_METHOD"] === "POST") {
// SQL to insert to transaction and items purchased table
}
94 changes: 29 additions & 65 deletions src/javascript/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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(
'<tr><td colspan="4" class="text-center text-muted table-striped">No items in cart</td></tr>'
);
$("#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(
'<tr><td colspan="4" class="text-center text-muted table-striped">No items in cart</td></tr>'
);
$("#cancelOrder").on("click", clearCart);
$("#proceedOrder").prop("disabled", true);
} else {
alert(data.message); // Display error message
}
},
error: function () {
alert("An error occurred. Please try again.");
},
});
});

Expand Down
Loading