????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................<?php
include('session.php');
include 'connection.php';
?>

<!DOCTYPE html>
<html>
<head>
    <title>Debug Fee Report</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        table { border-collapse: collapse; width: 100%; margin: 20px 0; }
        table, th, td { border: 1px solid #ddd; }
        th, td { padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        pre { background: #f5f5f5; padding: 10px; border: 1px solid #ccc; }
    </style>
</head>
<body>
    <h2>Debug Fee Report System</h2>

    <?php
    // 1. Check connection
    echo "<h3>1. Database Connection Test</h3>";
    if ($conn) {
        echo "<p class='success'>✓ Database connection successful</p>";
    } else {
        echo "<p class='error'>✗ Database connection failed</p>";
        exit;
    }

    // 2. Check if tables exist
    echo "<h3>2. Table Existence Check</h3>";
    
    $tables_check = "SHOW TABLES";
    $tables_result = $conn->query($tables_check);
    $existing_tables = array();
    
    if ($tables_result) {
        while ($table_row = $tables_result->fetch_array()) {
            $existing_tables[] = $table_row[0];
        }
        
        if (in_array('fees', $existing_tables)) {
            echo "<p class='success'>✓ 'fees' table exists</p>";
        } else {
            echo "<p class='error'>✗ 'fees' table does not exist</p>";
        }
        
        if (in_array('accounts', $existing_tables)) {
            echo "<p class='success'>✓ 'accounts' table exists</p>";
        } else {
            echo "<p class='error'>✗ 'accounts' table does not exist</p>";
        }
        
        if (in_array('admission', $existing_tables)) {
            echo "<p class='success'>✓ 'admission' table exists</p>";
        } else {
            echo "<p class='info'>! 'admission' table does not exist (this is okay)</p>";
        }
    }

    // 3. Check table data counts
    echo "<h3>3. Data Count Check</h3>";
    
    $fees_count_sql = "SELECT COUNT(*) as count FROM fees";
    $fees_count_result = $conn->query($fees_count_sql);
    if ($fees_count_result) {
        $fees_count = $fees_count_result->fetch_assoc()['count'];
        if ($fees_count > 0) {
            echo "<p class='success'>✓ Fees table has $fees_count records</p>";
        } else {
            echo "<p class='error'>✗ Fees table is empty (0 records)</p>";
        }
    }
    
    $accounts_count_sql = "SELECT COUNT(*) as count FROM accounts";
    $accounts_count_result = $conn->query($accounts_count_sql);
    if ($accounts_count_result) {
        $accounts_count = $accounts_count_result->fetch_assoc()['count'];
        if ($accounts_count > 0) {
            echo "<p class='success'>✓ Accounts table has $accounts_count records</p>";
        } else {
            echo "<p class='error'>✗ Accounts table is empty (0 records)</p>";
        }
    }

    // 4. Show sample data from fees table
    echo "<h3>4. Sample Data from Fees Table</h3>";
    $sample_fees_sql = "SELECT * FROM fees ORDER BY id DESC LIMIT 3";
    $sample_fees_result = $conn->query($sample_fees_sql);
    
    if ($sample_fees_result && $sample_fees_result->num_rows > 0) {
        echo "<table>";
        echo "<tr><th>ID</th><th>Admission No</th><th>Student Name</th><th>Course</th><th>Total Fee</th><th>Due Amount</th><th>Received Amount</th><th>Payment Date</th></tr>";
        
        while($row = $sample_fees_result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['admission_no'] . "</td>";
            echo "<td>" . $row['student_name'] . "</td>";
            echo "<td>" . $row['course_name'] . "</td>";
            echo "<td>₹" . number_format($row['total_fee'], 2) . "</td>";
            echo "<td>₹" . number_format($row['due_amount'], 2) . "</td>";
            echo "<td>₹" . number_format($row['received_amount'], 2) . "</td>";
            echo "<td>" . $row['payment_date'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p class='error'>No data found in fees table</p>";
    }

    // 5. Show sample data from accounts table
    echo "<h3>5. Sample Data from Accounts Table</h3>";
    $sample_accounts_sql = "SELECT * FROM accounts ORDER BY id DESC LIMIT 3";
    $sample_accounts_result = $conn->query($sample_accounts_sql);
    
    if ($sample_accounts_result && $sample_accounts_result->num_rows > 0) {
        echo "<table>";
        echo "<tr><th>ID</th><th>Admission No</th><th>Total Fees</th><th>Due Amount</th><th>Paid Amount</th><th>Received Amount</th></tr>";
        
        while($row = $sample_accounts_result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['admission_no'] . "</td>";
            echo "<td>₹" . number_format($row['total_fees'], 2) . "</td>";
            echo "<td>₹" . number_format($row['due_amount'], 2) . "</td>";
            echo "<td>₹" . number_format($row['paid_amount'], 2) . "</td>";
            echo "<td>₹" . number_format($row['received_amount'], 2) . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p class='error'>No data found in accounts table</p>";
    }

    // 6. Test the exact query used in fee-report.php
    echo "<h3>6. Test Fee Report Query</h3>";
    $test_sql = "SELECT f.*, 
                   COALESCE(a.due_amount, f.total_fee) as account_due_amount,
                   COALESCE(a.paid_amount, 0) as account_paid_amount,
                   COALESCE(a.received_amount, 0) as account_received_amount
            FROM fees f
            LEFT JOIN accounts a ON f.admission_no = a.admission_no
            ORDER BY f.id DESC LIMIT 5";
    
    echo "<p><strong>Query:</strong></p>";
    echo "<pre>" . $test_sql . "</pre>";
    
    $test_result = $conn->query($test_sql);
    
    if (!$test_result) {
        echo "<p class='error'>✗ Query failed: " . $conn->error . "</p>";
    } else {
        $num_rows = $test_result->num_rows;
        echo "<p class='success'>✓ Query executed successfully. Rows returned: $num_rows</p>";
        
        if ($num_rows > 0) {
            echo "<table>";
            echo "<tr><th>Admission No</th><th>Student Name</th><th>Course</th><th>Total Fee</th><th>Account Due</th><th>Received</th><th>Date</th></tr>";
            
            while($row = $test_result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row['admission_no'] . "</td>";
                echo "<td>" . $row['student_name'] . "</td>";
                echo "<td>" . $row['course_name'] . "</td>";
                echo "<td>₹" . number_format($row['total_fee'], 2) . "</td>";
                echo "<td>₹" . number_format($row['account_due_amount'], 2) . "</td>";
                echo "<td>₹" . number_format($row['received_amount'], 2) . "</td>";
                echo "<td>" . $row['payment_date'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
    }

    // 7. Test student details API
    if (isset($sample_fees_result) && $sample_fees_result->num_rows > 0) {
        $sample_fees_result = $conn->query($sample_fees_sql); // Re-run query
        $test_row = $sample_fees_result->fetch_assoc();
        $test_admission = $test_row['admission_no'];
        
        echo "<h3>7. Test Student Details API</h3>";
        echo "<p>Testing with admission number: <strong>$test_admission</strong></p>";
        echo "<button onclick=\"testAPI('$test_admission')\">Test Student Details API</button>";
        echo "<div id='api-result'></div>";
    }

    $conn->close();
    ?>

    <hr>
    <h3>Next Steps:</h3>
    <ul>
        <li><strong>If no data in fees table:</strong> <a href="create-fee.php">Create some fee records first</a></li>
        <li><strong>If data exists but fee-report.php shows nothing:</strong> Check PHP errors in browser developer tools</li>
        <li><strong>If API test fails:</strong> There's a server-side issue with get_student_details.php</li>
        <li><strong>If everything looks good:</strong> <a href="fee-report.php">Try fee-report.php again</a></li>
    </ul>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
    function testAPI(admission_no) {
        $('#api-result').html('<p>Testing API...</p>');
        
        $.post("get_student_details.php", {admission_no: admission_no}, function(response) {
            console.log('Raw response:', response);
            console.log('Response type:', typeof response);
            
            var html = '<div style="background: #f0f0f0; padding: 15px; margin: 10px 0; border: 1px solid #ccc;">';
            html += '<h4>API Test Result:</h4>';
            html += '<p><strong>Response Type:</strong> ' + typeof response + '</p>';
            
            var data;
            if (typeof response === 'object') {
                data = response;
                html += '<p style="color: green;"><strong>✓ JSON auto-parsed by jQuery</strong></p>';
            } else {
                try {
                    data = JSON.parse(response);
                    html += '<p style="color: green;"><strong>✓ JSON manually parsed</strong></p>';
                } catch (e) {
                    html += '<p style="color: red;"><strong>✗ JSON Parse Error:</strong> ' + e.message + '</p>';
                    html += '<p><strong>Raw Response:</strong> ' + response + '</p>';
                    html += '</div>';
                    $('#api-result').html(html);
                    return;
                }
            }
            
            html += '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
            
            if (data.success) {
                html += '<p style="color: green;"><strong>✓ API Success!</strong></p>';
            } else {
                html += '<p style="color: red;"><strong>✗ API Error:</strong> ' + data.message + '</p>';
            }
            
            html += '</div>';
            $('#api-result').html(html);
            
        }).fail(function(xhr, status, error) {
            $('#api-result').html('<div style="background: #ffebee; padding: 15px; margin: 10px 0; border: 1px solid #f44336; color: red;"><strong>AJAX Failed:</strong> ' + status + ' - ' + error + '</div>');
        });
    }
    </script>
</body>
</html>