<?php
$page_title = 'Jurnal Umum';
require_once('includes/load.php');
page_require_level(6);

/**
 * Mengambil semua data jurnal dari database, menggabungkannya dengan nama akun,
 * dan mengelompokkannya berdasarkan tanggal dan deskripsi.
 */
$jurnal_entries = find_by_sql("SELECT j.*, c.akun AS coa_akun FROM jurnal AS j JOIN coa AS c ON j.coa_kode = c.kode ORDER BY j.tanggal ASC, j.deskripsi ASC, j.id ASC");

$grouped_jurnal = [];
$total_debit = 0;
$total_kredit = 0;

foreach ($jurnal_entries as $entry) {
    $key = $entry['tanggal'] . '::' . $entry['deskripsi'];
    $grouped_jurnal[$key][] = $entry;

    // Menghitung total debit dan kredit
    $total_debit += $entry['debit'];
    $total_kredit += $entry['kredit'];
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-list-alt"></span>
                    <span>Daftar Jurnal Umum</span>
                </strong>
                <a href="tambah_jurnal.php" class="btn btn-primary pull-right">
                    <span class="glyphicon glyphicon-plus"></span> Tambah Jurnal
                </a>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th class="text-center" style="width: 10%;">Tanggal</th>
                            <th class="text-center">Keterangan</th>
                            <th class="text-center" style="width: 10%;">Kode COA</th>
                            <th class="text-center" style="width: 20%;">Nama Akun</th> <th class="text-center" style="width: 15%;">Debit</th>
                            <th class="text-center" style="width: 15%;">Kredit</th>
                            <th class="text-center" style="width: 10%;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($grouped_jurnal)) : ?>
                            <?php foreach ($grouped_jurnal as $key => $entries) : ?>
                                <?php
                                $row_count = count($entries);
                                $is_first_row = true;
                                ?>
                                <?php foreach ($entries as $index => $entry) : ?>
                                    <tr>
                                        <?php if ($is_first_row) : ?>
                                            <td class="text-center" rowspan="<?php echo $row_count; ?>" style="vertical-align: middle;">
                                                <?php echo date('d-m-Y', strtotime($entry['tanggal'])); ?>
                                            </td>
                                            <td rowspan="<?php echo $row_count; ?>" style="vertical-align: middle;">
                                                <?php echo remove_junk($entry['deskripsi']); ?>
                                            </td>
                                            <?php $is_first_row = false; ?>
                                        <?php endif; ?>
                                        <td class="text-center"><?php echo remove_junk($entry['coa_kode']); ?></td>
                                        <td><?php echo remove_junk($entry['coa_akun']); ?></td> <td class="text-right"><?php echo number_format($entry['debit'], 2, ',', '.'); ?></td>
                                        <td class="text-right"><?php echo number_format($entry['kredit'], 2, ',', '.'); ?></td>
                                        <?php if ($index === 0) : ?>
                                            <td class="text-center" rowspan="<?php echo $row_count; ?>" style="vertical-align: middle;">
                                                <div class="btn-group-vertical">
                                                    <a href="edit_jurnal.php?id=<?php echo (int)$entry['id']; ?>" class="btn btn-warning btn-xs" title="Edit" data-toggle="tooltip">
                                                        <span class="glyphicon glyphicon-edit"></span>
                                                    </a>
                                                    <a href="hapus_jurnal.php?id=<?php echo (int)$entry['id']; ?>" class="btn btn-danger btn-xs" title="Hapus" data-toggle="tooltip" onclick="return confirm('Hapus entri jurnal?');">
                                                        <span class="glyphicon glyphicon-trash"></span>
                                                    </a>
                                                </div>
                                            </td>
                                        <?php endif; ?>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endforeach; ?>
                            <tr class="info">
                                <td colspan="4" class="text-right"><strong>Total</strong></td>
                                <td class="text-right"><strong><?php echo number_format($total_debit, 2, ',', '.'); ?></strong></td>
                                <td class="text-right"><strong><?php echo number_format($total_kredit, 2, ',', '.'); ?></strong></td>
                                <td></td>
                            </tr>
                        <?php else : ?>
                            <tr>
                                <td colspan="7" class="text-center">Tidak ada entri jurnal ditemukan.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>