<?php
require_once __DIR__ . '/config/database.php';
$db = getDB();

// Check Dana Pensiun data
echo "=== DANA PENSIUN DATA STATUS ===\n\n";

$stmt = $db->query("SELECT COUNT(*) as total FROM simpanan WHERE jenis='dana_pensiun'");
$result = $stmt->fetch();
echo "Total Dana Pensiun records: " . $result['total'] . "\n";

$stmt = $db->query("SELECT COUNT(*) as total FROM simpanan WHERE jenis='dana_pensiun' AND DATE_FORMAT(tanggal, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m')");
$result = $stmt->fetch();
echo "April 2026 records: " . $result['total'] . "\n";

echo "\nMonths with Dana Pensiun data:\n";
$stmt = $db->query("SELECT DISTINCT DATE_FORMAT(tanggal, '%Y-%m') as month FROM simpanan WHERE jenis='dana_pensiun' ORDER BY tanggal DESC");
foreach($stmt->fetchAll() as $row) {
    echo "  - " . $row['month'] . "\n";
}

echo "\nSample records:\n";
$stmt = $db->query("SELECT s.*, a.nama FROM simpanan s LEFT JOIN anggota a ON s.anggota_id=a.id WHERE s.jenis='dana_pensiun' ORDER BY s.tanggal DESC LIMIT 5");
foreach($stmt->fetchAll() as $row) {
    echo "  " . $row['tanggal'] . " | " . $row['nama'] . " | " . $row['tipe'] . " | " . $row['jumlah'] . "\n";
}

echo "\n=== SUMMARY ===\n";
$stmt = $db->query("
    SELECT
        COALESCE(SUM(CASE WHEN tipe='setoran' THEN jumlah ELSE 0 END),0) as total_setoran,
        COALESCE(SUM(CASE WHEN tipe='penarikan' THEN jumlah ELSE 0 END),0) as total_penarikan
    FROM simpanan WHERE jenis='dana_pensiun'
");
$summary = $stmt->fetch();
echo "Total Setoran: " . $summary['total_setoran'] . "\n";
echo "Total Penarikan: " . $summary['total_penarikan'] . "\n";
echo "Saldo: " . ($summary['total_setoran'] - $summary['total_penarikan']) . "\n";
?>
