<?php
/**
 * Test script to debug get-bill-delete-details.php
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

// Set a test session
$_SESSION['uid'] = 1;
$_SESSION['usertype'] = 'Admin';

echo "<h2>Testing Bill Delete Details API</h2>";

// Test database connection
include_once('../../includes/config.php');

if (!$con) {
    die("Database connection failed: " . mysqli_connect_error());
}

echo "<p>✓ Database connected successfully</p>";

// Test bill ID
$billId = 2; // Using bill ID from database
echo "<p>Testing with Bill ID: $billId</p>";

// Test bill query
$billQuery = "SELECT s.*, c.CustomerName, c.CustomerCode 
              FROM tblsales s 
              LEFT JOIN tblcustomers c ON s.CustomerID = c.ID 
              WHERE s.ID = $billId";
              
echo "<h3>Bill Query:</h3>";
echo "<pre>$billQuery</pre>";

$billResult = mysqli_query($con, $billQuery);

if (!$billResult) {
    die("<p style='color:red'>Query failed: " . mysqli_error($con) . "</p>");
}

echo "<p>✓ Bill query successful</p>";
echo "<p>Rows found: " . mysqli_num_rows($billResult) . "</p>";

if (mysqli_num_rows($billResult) > 0) {
    $bill = mysqli_fetch_assoc($billResult);
    echo "<h3>Bill Data:</h3>";
    echo "<pre>" . print_r($bill, true) . "</pre>";
}

// Test items query
$itemsQuery = "SELECT * FROM tblsales_details WHERE SaleID = $billId ORDER BY ID";
echo "<h3>Items Query:</h3>";
echo "<pre>$itemsQuery</pre>";

$itemsResult = mysqli_query($con, $itemsQuery);

if (!$itemsResult) {
    echo "<p style='color:red'>Items query failed: " . mysqli_error($con) . "</p>";
} else {
    echo "<p>✓ Items query successful</p>";
    echo "<p>Items found: " . mysqli_num_rows($itemsResult) . "</p>";
}

// Test account transactions
if (isset($bill)) {
    $invoiceNumber = mysqli_real_escape_string($con, $bill['InvoiceNumber']);
    $accountQuery = "SELECT * FROM tblcustomer_accounts 
                     WHERE (InvoiceNumber = '$invoiceNumber' 
                            OR InvoiceNumber LIKE 'PAY-$invoiceNumber%')
                     ORDER BY TransactionDate, ID";
    
    echo "<h3>Account Transactions Query:</h3>";
    echo "<pre>$accountQuery</pre>";
    
    $accountResult = mysqli_query($con, $accountQuery);
    
    if (!$accountResult) {
        echo "<p style='color:red'>Account query failed: " . mysqli_error($con) . "</p>";
    } else {
        echo "<p>✓ Account query successful</p>";
        echo "<p>Transactions found: " . mysqli_num_rows($accountResult) . "</p>";
    }
}

// Test loyalty query
$loyaltyQuery = "SELECT * FROM tblcustomer_loyalty_points 
                 WHERE SaleID = $billId 
                 ORDER BY CreatedDate";

echo "<h3>Loyalty Query:</h3>";
echo "<pre>$loyaltyQuery</pre>";

$loyaltyResult = mysqli_query($con, $loyaltyQuery);

if (!$loyaltyResult) {
    echo "<p style='color:red'>Loyalty query failed: " . mysqli_error($con) . "</p>";
} else {
    echo "<p>✓ Loyalty query successful</p>";
    echo "<p>Transactions found: " . mysqli_num_rows($loyaltyResult) . "</p>";
}

// Test GST details table
echo "<h3>Checking GST Details Table:</h3>";
$tablesQuery = "SHOW TABLES LIKE 'tblsales_gst_details'";
$tablesResult = mysqli_query($con, $tablesQuery);

if ($tablesResult && mysqli_num_rows($tablesResult) > 0) {
    echo "<p>✓ tblsales_gst_details table exists</p>";
    
    $gstQuery = "SELECT COUNT(*) as count FROM tblsales_gst_details WHERE SaleID = $billId";
    $gstResult = mysqli_query($con, $gstQuery);
    
    if ($gstResult) {
        $gstRow = mysqli_fetch_assoc($gstResult);
        echo "<p>GST records found: " . $gstRow['count'] . "</p>";
    }
} else {
    echo "<p style='color:orange'>⚠ tblsales_gst_details table does not exist</p>";
}

echo "<h3>✓ All tests completed successfully!</h3>";

mysqli_close($con);
?>
