= $_GET["amount"] || $usertype == "seller") {
$sql_price = "SELECT $m FROM inventory WHERE name='$name'";
$res = mysqli_query($conn, $sql_price);
$price = mysqli_fetch_array($res);
//unit total price
$fruitprice = $price[$m] * $_GET["amount"];
//get user balance
$sql_checkmoney = "SELECT $c FROM $usertype WHERE name = '$username'";
$res = mysqli_query($conn, $sql_checkmoney);
$balance = mysqli_fetch_array($res);
//if buyer have no balance
if ($balance[$c] < $fruitprice && $usertype == "buyer") {
$errorMessage = urlencode("Insufficient Balance");
header("Location: ./interfaces/failed.php?why=$errorMessage");
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();
}
//update user balance
if ($usertype == 'buyer') {
$sql_update_inventory = "UPDATE inventory SET quantities = quantities - {$_GET["amount"]} WHERE name='$name'";
}
else {
$sql_update_inventory = "UPDATE inventory SET quantities = quantities + {$_GET["amount"]} WHERE name='$name'";
}
$res = mysqli_query($conn, $sql_update_inventory);
// Create the details string for the receipt
$details = "$name|{$_GET["amount"]}|$price[$m]|$fruitprice|";
// Get the current date and time in Hong Kong
$date = new DateTime('now', 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', '$fruitprice', '$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 - $fruitprice WHERE name='$username'";
$sql_profit = "UPDATE buyer_receipt SET details = details + $fruitprice WHERE name='sell_out'";
} else {
$sql_money = "UPDATE seller SET profit = profit + $fruitprice WHERE name='$username'";
$sql_profit = "UPDATE seller_receipt SET details = details + $fruitprice 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');
}
}
else {
$errorMessage = urlencode("Insufficient inventory for item '$name'");
header("Location: ./interfaces/failed.php?why=$errorMessage");
exit();
}
}
?>