<?php
$page_title = 'Tambah Jurnal';
require_once('includes/load.php');
page_require_level(6); // Asumsikan level 6 untuk akses akuntansi

// Ambil semua data COA untuk dropdown
$coas = find_by_sql("SELECT kode, akun FROM coa ORDER BY kode ASC");

if (isset($_POST['add_jurnal'])) {
    $req_fields = array('tanggal', 'deskripsi', 'coa_kode_debit', 'debit', 'coa_kode_kredit', 'kredit');
    validate_fields($req_fields);

    // Pastikan nilai debit dan kredit sama
    if ($_POST['debit'] !== $_POST['kredit']) {
        $session->msg('d', 'Jumlah Debit dan Kredit harus sama.');
        redirect('tambah_jurnal.php', false);
    }
    
    // Pastikan COA debit dan kredit berbeda
    if ($_POST['coa_kode_debit'] === $_POST['coa_kode_kredit']) {
        $session->msg('d', 'Akun Debit dan Kredit tidak boleh sama.');
        redirect('tambah_jurnal.php', false);
    }

    if (empty($errors)) {
        $tanggal = remove_junk($db->escape($_POST['tanggal']));
        $deskripsi = remove_junk($db->escape($_POST['deskripsi']));
        $coa_kode_debit = remove_junk($db->escape($_POST['coa_kode_debit']));
        $debit_value = (float)remove_junk($db->escape($_POST['debit']));
        $coa_kode_kredit = remove_junk($db->escape($_POST['coa_kode_kredit']));
        $kredit_value = (float)remove_junk($db->escape($_POST['kredit']));

        $db->begin_transaction();
        try {
            // Jurnal untuk sisi Debit
            $sql_debit = "INSERT INTO jurnal (tanggal, deskripsi, coa_kode, debit, kredit) VALUES ('{$tanggal}', '{$deskripsi}', '{$coa_kode_debit}', '{$debit_value}', 0)";
            $db->query($sql_debit);

            // Jurnal untuk sisi Kredit
            $sql_kredit = "INSERT INTO jurnal (tanggal, deskripsi, coa_kode, debit, kredit) VALUES ('{$tanggal}', '{$deskripsi}', '{$coa_kode_kredit}', 0, '{$kredit_value}')";
            $db->query($sql_kredit);

            $db->commit();
            $session->msg('s', "Entri jurnal berhasil ditambahkan.");
            redirect('jurnal.php', false);

        } catch (Exception $e) {
            $db->rollback();
            $session->msg('d', 'Gagal menambahkan entri jurnal: ' . $e->getMessage());
            redirect('tambah_jurnal.php', false);
        }
    } else {
        $session->msg('d', $errors);
        redirect('tambah_jurnal.php', false);
    }
}
?>

<?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-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-plus"></span>
                    <span>Tambah Entri Jurnal</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="tambah_jurnal.php">
                    <div class="form-group">
                        <label for="tanggal">Tanggal</label>
                        <input type="date" class="form-control" name="tanggal" required>
                    </div>
                    <div class="form-group">
                        <label for="deskripsi">Deskripsi</label>
                        <textarea class="form-control" name="deskripsi" rows="3" required></textarea>
                    </div>
                    <div class="form-group">
                        <label for="coa_kode_debit">Akun Debit</label>
                        <select class="form-control" name="coa_kode_debit" required>
                            <option value="">Pilih Akun Debit</option>
                            <?php foreach ($coas as $coa) : ?>
                                <option value="<?php echo remove_junk($coa['kode']); ?>">
                                    <?php echo remove_junk($coa['kode'] . ' - ' . $coa['akun']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="debit">Jumlah Debit</label>
                        <input type="number" step="0.01" class="form-control" name="debit" required>
                    </div>
                    <div class="form-group">
                        <label for="coa_kode_kredit">Akun Kredit</label>
                        <select class="form-control" name="coa_kode_kredit" required>
                            <option value="">Pilih Akun Kredit</option>
                            <?php foreach ($coas as $coa) : ?>
                                <option value="<?php echo remove_junk($coa['kode']); ?>">
                                    <?php echo remove_junk($coa['kode'] . ' - ' . $coa['akun']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="kredit">Jumlah Kredit</label>
                        <input type="number" step="0.01" class="form-control" name="kredit" required>
                    </div>
                    <div class="form-group clearfix">
                        <button type="submit" name="add_jurnal" class="btn btn-primary">Simpan Jurnal</button>
                        <a href="jurnal.php" class="btn btn-default pull-right">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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