-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveInvoiceProcesscart.php
More file actions
67 lines (56 loc) · 2.54 KB
/
saveInvoiceProcesscart.php
File metadata and controls
67 lines (56 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
session_start();
include "db/connections.php";
if (isset($_SESSION["u"])) {
$order_id = isset($_POST["o"]) ? $_POST["o"] : '';
$price = isset($_POST["p"]) ? $_POST["p"] : 0.0;
$umail = $_SESSION["u"]["id"];
$address = $_SESSION["u"]["address_line1"] . " " . $_SESSION["u"]["address_line2"];
$userdetail = $_SESSION["u"]["first_name"] . " " . $_SESSION["u"]["last_name"];
if (empty($order_id) || empty($price)) {
echo "Order ID or price is missing";
exit();
}
// Fetch cart data
$cart_rs = Database::search("SELECT * FROM `cart` WHERE `user_id`='" . $umail . "'");
if ($cart_rs->num_rows > 0) {
while ($cart_data = $cart_rs->fetch_assoc()) {
$qty = $cart_data["qty"];
$pid = $cart_data["product_id"];
// Fetch product data
$product_rs = Database::search("SELECT * FROM `products` WHERE `id`='" . $pid . "'");
if ($product_rs->num_rows > 0) {
$product_data = $product_rs->fetch_assoc();
$sellers_username =$product_data["sellers_username"];
$new_qty = $product_data["qty"] - $qty;
$product=$product_data["price"];
$title= $product_data["title"];
// Update product quantity
Database::iud("UPDATE `products` SET `qty`='" . $new_qty . "' WHERE `id`='" . $pid . "'");
// Set timezone and get current date and time
$d = new DateTime();
$tz = new DateTimeZone("Asia/Colombo");
$d->setTimezone($tz);
$date = $d->format("Y-m-d H:i:s");
// Insert invoice data
$invoice_query = "
INSERT INTO `invoice` (`order_id`, `date`, `total`, `qty`, `status`, `products_id`, `users_id`,`sellers_name`,`title`,`price`,`useraddress`,`username`)
VALUES ('$order_id', '$date', '$price', '$qty', '0', '$pid', '$umail','$sellers_username','$title','$product',' $address ','$userdetail')
";
Database::iud($invoice_query);
} else {
echo "Product not found for ID: $pid";
exit();
}
}
// Delete cart data
$delete_query = "DELETE FROM `cart` WHERE `user_id`='" . $umail . "'";
Database::iud($delete_query);
echo "success";
} else {
echo "Cart is empty for user ID: $umail";
}
} else {
echo "User not logged in";
}
?>