<?php
date_default_timezone_set('Asia/Jakarta');
$tanggal = mktime(date("m"), date("d"), date("Y"));
$tglsekarang = date("d-m-Y", $tanggal);

$sekolah = new sekolah;
if (isset($_POST['jenis'])) {
    $jenis_lap = $_POST['jenis'];
} else {
    $jenis_lap = "Semua";
};

if (isset($_POST['jenjang'])) {
    $jenjang_lap = $_POST['jenjang'];
    $jenjang_post = $_POST['jenjang'];
    $aktifjenis = "";
} else {
    $jenjang_lap = "--Pilih--";
    $jenjang_post = "Semua";
    $aktifjenis = "disabled";
};
if (isset($_POST['paralel'])) {
    $paralel_lap = $_POST['paralel'];
} else {
    $paralel_lap = "Semua";
};

$optVal = new lembaga();

if (isset($_POST['bulan1'])) {
    $bulan1 = $_POST['bulan1'];
    $v_bulan1 = $optVal->ValPeriode($bulan1, $tahun1, $tahun2);
} else {
    $bulan1 = 1;
    $v_bulan1 = "-- Pilih Bulan Awal --";
};

if (isset($_POST['bulan2'])) {
    $bulan2 = $_POST['bulan2'];
    $v_bulan2 = $optVal->ValPeriode($bulan2, $tahun1, $tahun2);
} else {
    $bulan2 = 1;
    $v_bulan2 = "-- Pilih Bulan Akhir --";
};
$jml_bulan = @($bulan2 - $bulan1) + 1;


if ($jml_bulan == 1) {
    $periode = $v_bulan1;
    $jml_bulan = 1;
} elseif ($jml_bulan < 1) {
    $periode = "-##-";
    $jml_bulan = 1;
} else {
    $periode = $v_bulan1 . " - " . $v_bulan2;
    $jml_bulan = $jml_bulan;
};

//$jumlah_tanggungan = @(($jml_tang_ok / 12) * $jml_bulan);

$dt_jenis = new namaBayar();
$showJenis = $dt_jenis->shownamaBayar($tahun, $jenis_lap, $jenjang_lap);
$count_nb = $showJenis->rowCount();
$datajenis = $showJenis->fetch(PDO::FETCH_OBJ);
if ($count_nb > 0) {
    $namaBayar = $datajenis->nama;
} else {
    $namaBayar = "--";
};

//Query Tanggungan
$classTanggungan = new listTanggungan();
$val_sumtang = $classTanggungan->sumTanggunganOnPeriode($tahun, $tb_siswa, $jenjang_post, $paralel_lap,  $jenis_lap, $jml_bulan);

if ($val_sumtang == "" || $val_sumtang  == 0 || isset($_POST['jenjang']) == NULL) {
    $sumtanggungan = number_format(0, 0, ",", ".");
    $v_sumtanggungan = 0;
} else {
    $sumtanggungan = number_format($val_sumtang, 0, ",", ".");
    $v_sumtanggungan = $val_sumtang;
};

//Terbayar & Kurang
if (isset($_POST['jenjang']) == NULL) {
    $terbayar = @(number_format(0, 0, ".", "."));
    $kurang = @(number_format(0, 0, ".", "."));;
} else {
    $classTrans = new transaksi();
    $Terbayar = $classTrans->infoterbayarOnTahapAllJenisOnSUM($tahun, $jenjang_post, $paralel_lap, $tb_transaksi, $jenis_lap, $bulan1, $bulan2);
    $terbayar = @(number_format($Terbayar, 0, ".", "."));
    $kurang = $v_sumtanggungan - $Terbayar;
    $kurang = @(number_format(($kurang), 0, ".", "."));
};


?>

<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>

    <div class="container col-12 mb-4 pb-5">
        <div class="row">
            <div class="col-12">
                <div class="col-12">
                    <div class="card  mx-3">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-journal-text"></span> Laporan Bulanan </b>
                        </div>
                        <div class="card-body mb-0">
                            <div class="row mx-3 mt-2" style="background-color: gainsboro;">
                                <div class="col-md-6">
                                    <div class="card-body mt-4 px-1 mx-2">
                                        <form action="" method="post">

                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Kelas [Jenjang-Paralel]</label>
                                                <div class="col-md-2">
                                                    <select name="jenjang" class="form-control form-control-sm fontInput small" onchange="this.form.submit()">

                                                        <option><?php echo $jenjang_lap; ?></option>
                                                        <option>Semua</option>
                                                        <?php

                                                        $jenjang = strtolower($sekolah->dataSekolah('jenjang'));

                                                        $opt = new form();
                                                        $opval = $opt->optSelect('tb_tingkat', "where {$jenjang}<>'' order by no asc");
                                                        while ($dataopt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            $optJenjang = $dataopt->{$jenjang};
                                                            echo "<option>" . $optJenjang . " </option>";
                                                        };
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class="col-md-7">
                                                    <select name="paralel" class="form-control form-control-sm fontInput small" required onchange="this.form.submit()">
                                                        <option><?php echo $paralel_lap; ?></option>
                                                        <option>Semua</option>
                                                        <?php
                                                        $opval = $opt->optSelect('tb_paralel', ' order by no asc');
                                                        while ($datapar = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            echo "<option>$datapar->paralel</option>";
                                                        };
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Jenis Pembayaran</label>
                                                <div class="col-md-9">
                                                    <select name="jenis" class="form-control form-control-sm fontInput small" required <?php echo $aktifjenis; ?> onchange="this.form.submit()">
                                                        <option value="<?php echo $jenis_lap; ?>"><?php echo $jenis_lap . " : " . $namaBayar; ?></option>

                                                        <?php

                                                        $showJenis = $dt_jenis->NamaTransaksibyBulanan('tb_nama_bayar', 'tp', $tahun, $jenjang_lap, 'kode');
                                                        $count = $showJenis->rowCount();
                                                        while ($datajenis = $showJenis->fetch(PDO::FETCH_OBJ)) {
                                                            echo "<option value='$datajenis->kode'>$datajenis->kode : $datajenis->nama</option>";
                                                        };
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Bulan</label>
                                                <div class="col-md-4">
                                                    <select name="bulan1" class="form-control form-control-sm fontInput small" onchange="this.form.submit()">

                                                        <option value="<?php echo $bulan1; ?>"><?php echo $v_bulan1; ?></option>
                                                        <?php
                                                        $oplist = $optVal->listPeriode($tahun1, $tahun2);
                                                        echo $oplist;
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class="col-sm-1 pl-3 pt-2"><b>s.d</b></div>
                                                <div class="col-md-4">
                                                    <select name="bulan2" class="form-control form-control-sm fontInput small" required onchange="this.form.submit()">
                                                        <option value="<?php echo $bulan2; ?>"><?php echo $v_bulan2; ?></option>
                                                        <?php
                                                        $oplist2 = $optVal->listPeriode($tahun1, $tahun2);
                                                        echo $oplist;
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="row mt-3">
                                                <label class="col-sm-3 col-form-label"></label>
                                                <div class="col-md-9">
                                                    <button type="submit" class="btn btn-primary btn-xs float-right px-2 mx-1" name="save"><i class="bi-funnel"></i> <b>FILTER</b></button>
                                        </form>

                                        <?php echo $link->exportXlsxLapBulanan('laporan/bulanan/print_excel', $tahun,  $jenjang_post, $paralel_lap, $jenis_lap, $bulan1, $bulan2); ?>

                                        <form name="cetak" action="<?php echo $link->pageAction('laporan/bulanan/print_preview'); ?>" method="post">
                                            <input name="tahun" value="<?php echo $tahun; ?>" hidden>
                                            <input name="jenis" value="<?php echo $jenis_lap; ?>" hidden>
                                            <input name="jenjang" value="<?php echo $jenjang_post; ?>" hidden>
                                            <input name="paralel" value="<?php echo $paralel_lap; ?>" hidden>
                                            <input name="bulan1" value="<?php echo $bulan1; ?>" hidden>
                                            <input name="bulan2" value="<?php echo $bulan2; ?>" hidden>
                                            <button type="submit" class="btn btn-primary btn-xs float-right px-2 mx-1 d-inline"><i class="bi bi-printer"></i><b> CETAK</b></button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card-body mt-4 px-1 mx-2">
                                <div class="row">
                                    <div class="col-12 col-sm-6 col-md-12">
                                        <div class="info-box mb-3">
                                            <span class="info-box-icon bg-primary elevation-1"><i class="bi bi-journal-text"></i></span>
                                            <div class="info-box-content">

                                                <div class="col-sm-12 invoice-col">
                                                    <dl class="row">
                                                        <dt class="col-sm-4 pt-3">JML TANGGUNGAN</dt>
                                                        <dd class="col-sm-8 pt-3"><b>: <?php echo $sumtanggungan; ?></b></dd>
                                                        <dt class="col-sm-4">JML TERBAYAR</dt>
                                                        <dd class="col-sm-8 "><b>: <?php echo $terbayar; ?> </b></dd>
                                                        <dt class="col-sm-4">JML KURANG</dt>
                                                        <dd class="col-sm-8"><b>: <?php echo $kurang; ?></b>
                                                        </dd>
                                                    </dl>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>


                <div class="card-body mx-4">
                    <label>
                        <h6><b class="text-primary">REKAPITULASI</b></h6>
                    </label>
                    <table id="example51" class="table table-bordered table-striped table-hover table-sm mb-4">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Nama</th>
                                <th>NIS</th>
                                <th>Kelas</th>
                                <th>Periode</th>
                                <th>Jml Bulan</th>
                                <th>Tanggungan</th>
                                <th>Terbayar</th>
                                <th>Kurang</th>
                                <th>Kartu</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php

                            if ($jenis_lap == "Semua") {
                                echo "
                                    <tr>
                                        <td width='5px' colspan='10'><b class='text-danger'>Pilih Jenjang & Jenis Pembayaran untuk menampilkan Laporan</b></td>
                                    
                                    </tr>";
                            } else {

                                $classLaporan = new laporan();
                                $showList = $classLaporan->showRekap($tb_siswa, $jenjang_lap, $paralel_lap, 'jenjang asc, paralel asc, nama asc');
                                $no = 0;
                                while ($data = $showList->fetch(PDO::FETCH_OBJ)) {

                                    $nis = $data->nis;
                                    $id = $data->id;
                                    $nama = $data->nama;
                                    $jenjang = $data->jenjang;
                                    $paralel = $data->paralel;
                                    $kelas = $jenjang . '-' . $paralel;

                                    //Query Tanggungan

                                    $classTanggungan = new listTanggungan();
                                    $val_tang = $classTanggungan->tanggunganByKodeOnPeriode($tahun, $jenjang, $tb_siswa, 'id', $id, $jenis_lap);
                                    if ($val_tang == "" || $val_tang  == 0) {
                                        $tanggungan = number_format(0, 0, ",", ".");
                                        $v_tanggungan = 0;
                                    } else {
                                        $tanggungan = number_format($val_tang, 0, ",", ".");
                                        $v_tanggungan = $val_tang;
                                    };

                                    //tanggungan : jumlah bulan
                                    $tanggungan = number_format(($v_tanggungan / 12) * $jml_bulan, 0, ".", ".");
                                    //Terbayar & Kurang
                                    $classTrans = new transaksi();
                                    $Terbayar = $classTrans->infoterbayarOnTahapAllJenis($tahun, $jenjang, $tb_transaksi, $nis, $jenis_lap, $bulan1, $bulan2);
                                    $terbayar = @(number_format($Terbayar, 0, ".", "."));
                                    $kurang = (($v_tanggungan / 12) * $jml_bulan) - $Terbayar;
                                    $kurang = @(number_format(($kurang), 0, ".", "."));

                                    $no++;

                                    if ($bulan2 >= $bulan1) {
                                        $jmlbulan = $jml_bulan;
                                        $tanggungan = $tanggungan;
                                        $terbayar = $terbayar;
                                        $kurang = $kurang;
                                    } else {
                                        $jmlbulan = "-##-";
                                        $tanggungan = 0;
                                        $terbayar = 0;
                                        $kurang = 0;
                                    };

                                    $kartu = "
                                    <form name='cetak' action=" . $link->pageAction('laporan/bulanan/kartu_preview') . " method='post'>
                                            <input name='id' value='$id' hidden>
                                            <input name='tahun' value='$tahun' hidden>
                                            <input name='jenis' value='$jenis_lap' hidden>
                                            <input name='jenjang' value='$jenjang_post' hidden>
                                            <input name='paralel' value='$paralel_lap' hidden>
                                            <input name='bulan1' value='$bulan1' hidden>
                                            <input name='bulan2' value='$bulan2' hidden>
                                            <button type='submit' class='badge badge-primary px-1 py-1 d-inline'><i class='bi bi-printer'></i><b> KARTU</b></button>
                                        </form>
                                    ";


                                    echo "
                                    <tr>
                                        <td width='5px'>$no</td>
                                        <td align=left>$nama</td>
                                        <td align=left>$nis</td>
                                        <td align=left>$kelas</td>
                                        <td align=left>$periode</td>
                                        <td align=left>$jmlbulan</td>
                                        <td align=left>$tanggungan</td>
                                        <td align=left>$terbayar</td>
                                        <td align=left>$kurang</td>
                                        <td width='75px' align='center'>$kartu</td>
                                    </tr>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>