<?php
// debug_order_http.php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$host = "localhost";
$user = "mhsg3183_loundry";
$pass = "lana212.";
$db   = "mhsg3183_loundry";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get latest order
$query = "SELECT * FROM orders ORDER BY id DESC LIMIT 1";
$result = $conn->query($query);
$order = $result->fetch_assoc();

if (!$order) {
    die("No orders found.");
}

echo "Latest Order (ID: {$order['id']})\n";
echo "Total (Raw): '{$order['total']}' (Type: " . gettype($order['total']) . ")\n";
echo "Paid Amount (Raw): '{$order['paid_amount']}' (Type: " . gettype($order['paid_amount']) . ")\n";

// Get payments for this order
$query = "SELECT * FROM payments WHERE order_id = " . (int)$order['id'];
$p_result = $conn->query($query);

echo "Payments:\n";
$sum_payments = 0;
while ($row = $p_result->fetch_assoc()) {
    echo "Payment ID: {$row['id']}, Amount: '{$row['amount']}'\n";
    $sum_payments += (float)$row['amount'];
}

echo "Analysis:\n";
echo "Sum of Payments: $sum_payments\n";
echo "Order Paid Amount: " . (float)$order['paid_amount'] . "\n";
echo "Remaining Calculation: max(0, " . (float)$order['total'] . " - " . (float)$order['paid_amount'] . ") = " . max(0, (float)$order['total'] - (float)$order['paid_amount']) . "\n";

$conn->close();
?>
