0) {
$sql_check_availability = "SELECT $a FROM inventory WHERE name='$col_name[$i]'";
$res = mysqli_query($conn, $sql_check_availability);
$avail = mysqli_fetch_array($res);
if ($avail[$a] != 1) {
// Handle unavailability case (e.g., display an error message)
echo "";
exit();
}
//get inventory
$sql_check_inventory = "SELECT quantities FROM inventory WHERE name='$col_name[$i]'";
$res = mysqli_query($conn, $sql_check_inventory);
$inventory = mysqli_fetch_array($res);
//if sufficient inventory
if ($inventory['quantities'] >= $goods[$i] || $usertype == "seller") {
$sql_price = "SELECT $m FROM inventory WHERE name='$col_name[$i]'";
$res = mysqli_query($conn, $sql_price);
$price = mysqli_fetch_array($res);
//unit price
$fruitprice = $price[$m] * $goods[$i];
//total price
$total += $fruitprice;
}
else {
echo "";
exit();
}
}
}
//if buyer donot have enough balance
$sql_checkmoney = "SELECT $c FROM $usertype WHERE name='$username'";
$res = mysqli_query($conn, $sql_checkmoney);
$balance = mysqli_fetch_array($res);
if($balance[$c] < $total && $usertype == "buyer"){
echo "";
exit();
}
//check cashflow
$sql_sell_out = "SELECT details FROM buyer_receipt WHERE name='sell_out'";
$result = mysqli_query($conn, $sql_sell_out);
$sell_out = mysqli_fetch_array($result);
$sql_buy_in = "SELECT details FROM seller_receipt WHERE name='buy_in'";
$result = mysqli_query($conn, $sql_buy_in);
$buy_in = mysqli_fetch_array($result);
$profit = $sell_out['details'] - $buy_in['details'];
//if insufficient cashflow
if($profit < $total && $usertype == "seller"){
echo "";
exit();
}
else{
for ($i = 3; $i < $col; $i++) {
if ($goods[$i] > 0) {
//get price
$sql_price = "SELECT $m FROM inventory WHERE name='$col_name[$i]'";
$res = mysqli_query($conn, $sql_price);
$price = mysqli_fetch_array($res);
//unit total price
$fruitprice = $price[$m] * $goods[$i];
// Update the user's cart by setting the item quantity to 0
$sql_closecart = "UPDATE $usertype SET `$col_name[$i]` = 0 WHERE name='$username'";
$res = mysqli_query($conn, $sql_closecart);
// Update the inventory based on the user type
if ($usertype == 'buyer') {
$sql_update_inventory = "UPDATE inventory SET quantities = quantities - $goods[$i] WHERE name='$col_name[$i]'";
} else {
$sql_update_inventory = "UPDATE inventory SET quantities = quantities + $goods[$i] WHERE name='$col_name[$i]'";
}
$res = mysqli_query($conn, $sql_update_inventory);
// Create the details string for the receipt
$details .= "$col_name[$i]|$goods[$i]|$price[$m]|$fruitprice|";
}
}
}
// Get the current date and time in HK
$date = new DateTime('now');
$date->setTimezone(new DateTimeZone('Asia/Hong_Kong'));
$formattedDate = $date->format('Y-m-d H:i:s');
// Use the formatted date in your SQL query
$sql_add_receipt = "INSERT INTO `$r` (`name`, `details`, `total`, `date`) VALUES ('$username', '$details', '$total', '$formattedDate')";
$result = mysqli_query($conn, $sql_add_receipt);
// Deduct the total amount from the user's money
if($usertype == "buyer"){
$sql_money = "UPDATE buyer SET money = money - $total WHERE name='$username'";
$sql_profit = "UPDATE buyer_receipt SET details = details + $total WHERE name='sell_out'";
}
else{
$sql_money = "UPDATE seller SET profit = profit + $total WHERE name='$username'";
$sql_profit = "UPDATE seller_receipt SET details = details + $total WHERE name='buy_in'";
}
$result = mysqli_query($conn, $sql_money);
$result_profit = mysqli_query($conn, $sql_profit);
if ($result && $result_profit) {
echo "";
}
else{
header('Location: ./interfaces/failed.php');
}
?>