HEX
Server: LiteSpeed
System: Linux server214.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: bigisxfd (746)
PHP: 8.4.15
Disabled: NONE
Upload Files
File: /home/bigisxfd/public_html/cowork/insert_partial.php
<?php 

// Include necessary files
include_once("functions.php");
session_start();


// Get user ID from the session
$userId = $_SESSION['userid'];

// Get form data
$orderId     = $_POST['orderid'];
$memberId    = $_POST['memberId'];
$paymentMode = $_POST['paymentMode'];
$amount      = $_POST['paymentAmount'];   // The user-specified (partial or full) payment amount

// 1) Insert this payment into the Payments table
$insertPaymentQuery = "
    INSERT INTO Payments (userid, memberid, orderid, amount, mode, date)
    VALUES (?, ?, ?, ?, ?, NOW())
";
$insertPaymentStmt = $conn->prepare($insertPaymentQuery);

if (!$insertPaymentStmt) {
    die("Error during payment query preparation: " . $conn->error);
}

$insertPaymentStmt->bind_param("iiids", 
    $userId,    // integer
    $memberId,  // integer
    $orderId,   // integer
    $amount,    // double/decimal
    $paymentMode // string
);

if (!$insertPaymentStmt->execute()) {
    die("Error during payment insertion: " . $insertPaymentStmt->error);
}

// Get this newly inserted payment ID for reference
$newPaymentId = $insertPaymentStmt->insert_id;
$insertPaymentStmt->close();

// 2) Fetch the current balance from the Orders table
$getBalanceQuery = "
    SELECT balance
    FROM Orders
    WHERE orderid = ?
    LIMIT 1
";
$getBalanceStmt = $conn->prepare($getBalanceQuery);
$getBalanceStmt->bind_param("i", $orderId);

if (!$getBalanceStmt->execute()) {
    die("Error fetching order balance: " . $getBalanceStmt->error);
}

$result = $getBalanceStmt->get_result();
if ($result->num_rows === 0) {
    // No order found for this ID
    $getBalanceStmt->close();
    header("Location: advanced_booking.php?error=OrderNotFound");
    exit();
}

$orderRow      = $result->fetch_assoc();
$currentBalance = $orderRow['balance'];
$getBalanceStmt->close();

// 3) Calculate the new balance
$newBalance = $currentBalance - $amount;

// 4) Determine payment status
//    If new balance <= 0, it's fully paid => payment_status=1
//    Otherwise partial => payment_status=0
if ($newBalance <= 0) {
    $newBalance     = 0;
    $payment_status = 1;  // Fully paid
} else {
    $payment_status = 0;  // Still partially unpaid
}

// 5) Update the Orders table
$updateOrderQuery = "
    UPDATE Orders
    SET balance = ?, 
        paymentid = ?,
        payment_status = ?
    WHERE orderid = ?
";
$updateOrderStmt = $conn->prepare($updateOrderQuery);

if (!$updateOrderStmt) {
    die("Error preparing Orders update: " . $conn->error);
}

$updateOrderStmt->bind_param("diis", 
    $newBalance,      // decimal
    $newPaymentId,    // int (the newly inserted payment record)
    $payment_status,  // int (0 or 1)
    $orderId          // int
);

if (!$updateOrderStmt->execute()) {
    die("Error updating the Orders table: " . $updateOrderStmt->error);
}

$updateOrderStmt->close();

// 6) Optionally, update Bookings table if you want to track payment on that booking
//    This depends on your logic. For example, if there's only one order per booking, 
//    you could do something like marking the booking as fully paid if $payment_status=1.
//    Otherwise partial. For now, let's keep it simple and skip updating Bookings 
//    or only do so if fully paid:

if ($payment_status === 1) {
    // If you want to mark the booking as fully paid, do something like:
    $updateBookingQuery = "
        UPDATE Bookings
        SET paymentid = ?, 
            paymentstatus = 1
        WHERE bookingid = (
            SELECT bookingid 
            FROM Orders 
            WHERE orderid = ?
        )
        LIMIT 1
    ";
    $updateBookingStmt = $conn->prepare($updateBookingQuery);
    if ($updateBookingStmt) {
        $updateBookingStmt->bind_param("ii", $newPaymentId, $orderId);
        $updateBookingStmt->execute();
        $updateBookingStmt->close();
    }
}

// 7) Redirect to success page (or anywhere you want)
header("Location: advanced_booking.php?success=true");
exit();
?>