<?php
include 'includes/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $invoice_no = isset($_POST['invoice_no']) ? $_POST['invoice_no'] : '';
    $customer_id = isset($_POST['user_id']) ? $_POST['user_id'] : ''; 
    $cheque_number = isset($_POST['cheque_number']) ? $_POST['cheque_number'] : '';
    $bank_name = isset($_POST['bank_name']) ? $_POST['bank_name'] : ''; 
    $bank_id = isset($_POST['credit_account']) ? $_POST['credit_account'] : ''; 
    $amount = isset($_POST['amount']) ? $_POST['amount'] : '';
    $issued_date = isset($_POST['issued_date']) ? $_POST['issued_date'] : '';
    $hold_until = isset($_POST['hold_until']) ? $_POST['hold_until'] : '';
    $status = isset($_POST['status']) ? $_POST['status'] : '';
    $payment_status = isset($_POST['payment_status']) ? $_POST['payment_status'] : '';
    $sale_date = isset($_POST['sale_date']) ? $_POST['sale_date'] : '';

    $payment_status = $payment_status . '/cheque payment'; 

    try {
        $sql = "INSERT INTO cheque_payments (invoice_no, customer_id, cheque_number, bank_name, credit_bank_id, amount, issued_date, hold_until, status)
                VALUES (:invoice_no, :customer_id, :cheque_number, :bank_name, :credit_bank_id, :amount, :issued_date, :hold_until, :status)";

        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':invoice_no', $invoice_no);
        $stmt->bindParam(':customer_id', $customer_id); 
        $stmt->bindParam(':cheque_number', $cheque_number);
        $stmt->bindParam(':bank_name', $bank_name); 
        $stmt->bindParam(':credit_bank_id', $bank_id); 
        $stmt->bindParam(':amount', $amount);
        $stmt->bindParam(':issued_date', $issued_date);
        $stmt->bindParam(':hold_until', $hold_until);
        $stmt->bindParam(':status', $status);

        $stmt->execute();

        $cheque_id = $conn->lastInsertId();

        if ($status === 'cleared') {
            $sqlCashDeposit = "INSERT INTO cash_deposit (cheque_id, invoice, bank_id, user_id, amount, status, payment_status, date_deposit)
                                VALUES (:cheque_id, :invoice_no, :bank_id, :user_id, :amount, :status, :payment_status, :sale_date)";

            $stmtCashDeposit = $conn->prepare($sqlCashDeposit);

            $stmtCashDeposit->bindParam(':cheque_id', $cheque_id);
            $stmtCashDeposit->bindParam(':invoice_no', $invoice_no);
            $stmtCashDeposit->bindParam(':bank_id', $bank_id); 
            $stmtCashDeposit->bindParam(':user_id', $customer_id); 
            $stmtCashDeposit->bindParam(':amount', $amount);
            $stmtCashDeposit->bindParam(':status', $status);
            $stmtCashDeposit->bindParam(':payment_status', $payment_status); 
            $stmtCashDeposit->bindParam(':sale_date', $sale_date); 

            $stmtCashDeposit->execute();
        }

        echo "<script>
                alert('Cheque payment details inserted successfully!');
                window.location.href = 'list_pos.php';
              </script>";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>
