<?php
$classTr = new transaksi();

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

if (isset($_POST['tanggal1'])) {
    $tgl_1 = $_POST['tanggal1'];
} else {
    $tgl_1 = $tglsekarang;
};
if (isset($_POST['tanggal2'])) {
    $tgl_2 = $_POST['tanggal2'];
} else {
    $tgl_2 = $tglsekarang;
};
if (isset($_POST['penerima']) && $_POST['penerima'] != "Semua") {
    $penerima_lap = $_POST['penerima'];
    $val_penerima = $classTr->namaPenerimaByID($penerima_lap);
} else {
    $penerima_lap = "Semua";
    $val_penerima = "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";
};
if (isset($_POST['kategori'])) {
    $kategori_lap = $_POST['kategori'];
} else {
    $kategori_lap = "Semua";
};
if (isset($_POST['metode'])) {
    $metode_lap = $_POST['metode'];
} else {
    $metode_lap = "Semua";
};
if (isset($_POST['jenis'])) {
    $jenis_lap = $_POST['jenis'];
} else {
    $jenis_lap = "Semua";
};


$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 = "Semua";
};

//Indo to Egg
function ubahformatTgl($tanggal)
{
    $pisah = explode('-', $tanggal);
    $urutan = array($pisah[2], $pisah[1], $pisah[0]);
    $satukan = implode('-', $urutan);
    return $satukan;
}
// Cara penggunaan function ubahTgl
$tanggal1 = ubahformatTgl($tgl_1);
$tanggal2 = ubahformatTgl($tgl_2);



$cekID = new cekID();
$jmldata = $cekID->cek($tb_setor, 'id_setor', $id_page);

$user_admin = new crudData();
$editUser = $user_admin->editData($tb_setor, 'id_setor', $id_page);
$data = $editUser->fetch(PDO::FETCH_OBJ);



?>
<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-secondary  py-1 pl-1">
                            <b class="text-light judul"><span class="bi bi-box-arrow-in-up-right"></span> Tambah Transaksi Setor</b>
                        </div>

                        <div class="card-body mb-0">
                            <div class="row mx-3 mt-2">
                                <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">Periode</label>
                                                <div class="col-md-4 d-inline">
                                                    <input type="text" class="form-control form-control-sm fontInput" placeholder="" id="tanggal1" pattern="\d{1,2}-\d{1,2}-\d{4}" value="<?php echo $tgl_1;
                                                                                                                                                                                            ?>" name="tanggal1" style="text-align:center" required>
                                                </div>
                                                <div class="col-md-1">
                                                    <label class="col-form-label ml-3">s.d</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="text" class="form-control form-control-sm fontInput" placeholder="" id="tanggal2" pattern="\d{1,2}-\d{1,2}-\d{4}" value="<?php echo $tgl_2;
                                                                                                                                                                                            ?>" name="tanggal2" style="text-align:center" required>
                                                </div>
                                            </div>
                                            <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>
                                                        <?php

                                                        $sekolah = new sekolah;

                                                        $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>
                                                        <option><?php echo $paralel_lap; ?></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">Kategori Siswa</label>
                                                <div class="col-md-9">
                                                    <select name="kategori" class="form-control form-control-sm fontInput small" required>
                                                        <option><?php echo $kategori_lap; ?></option>
                                                        <option>Semua</option>
                                                        <?php
                                                        $opval = $opt->optSelect('tb_siswa_kategori', ' order by kategori asc');
                                                        while ($datapar = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            echo "<option>$datapar->kategori</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; ?>>
                                                        <option value="<?php echo $jenis_lap; ?>"><?php echo $jenis_lap . " : " . $namaBayar; ?></option>
                                                        <option>Semua</option>
                                                        <?php
                                                        $dt_jenis = new namaBayar();
                                                        $showJenis = $dt_jenis->NamaTransaksi('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">Penerima</label>
                                                <div class="col-md-9">
                                                    <select name="penerima" class="form-control form-control-sm fontInput small" required>

                                                        <?php
                                                        $classLaporan = new laporan();
                                                        $crud = new crudData();
                                                        if ($level == "admin") {
                                                            $opval = $crud->showTable($tb_transaksi . " group by penerima order by penerima asc");
                                                            echo "<option value='$penerima_lap'>$val_penerima</option>";
                                                            echo "<option value=Semua'>Semua</option>";
                                                        } else {
                                                            $opval = $crud->showTable($tb_transaksi . " where penerima='$user_id' group by penerima order by penerima asc");
                                                        };

                                                        while ($datapnr = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            $namaPenerima = $classTr->namaPenerimaByID($datapnr->penerima);
                                                            echo "<option value='$datapnr->penerima'>$namaPenerima</option>";
                                                        };
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Metode Bayar</label>
                                                <div class="col-md-9">
                                                    <select name="metode" class="form-control form-control-sm fontInput small" required>
                                                        <option><?php echo $metode_lap; ?></option>
                                                        <option>Semua</option>
                                                        <option>Tunai</option>
                                                        <option>Bank</option>
                                                    </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->exportXlsxLapTransaksi('laporan/transaksi/print_excel', $tahun, $tgl_1, $tgl_2, $metode_lap, $jenjang_lap, $paralel_lap, $penerima_lap); 
                                        ?>


                                    </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-calculator"></i></span>
                                            <div class="info-box-content">
                                                <div class="col-sm-12 invoice-col">
                                                    <dl class="row">
                                                        <dt class="col-sm-3 pt-3">PERIODE</dt>
                                                        <dd class="col-sm-9  pt-3"><b>: <?php echo $tgl_1 . " s.d " . $tgl_2; ?></b></dd>
                                                        <dt class="col-sm-3">JUMLAH</dt>
                                                        <dd class="col-sm-9"><b>: <?php $classLaporan = new laporan();
                                                                                    echo $classLaporan->transaksiTotalSetor($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $jenis_lap, $jenjang_post,  $paralel_lap, $kategori_lap)
                                                                                    ?></b></dd>
                                                        <dt class="col-sm-3">RECORD</dt>
                                                        <dd class="col-sm-9"><b>: <?php echo $classLaporan->CountRecordAll($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $kategori_lap); ?> Data</b></dd>
                                                        <dt class="col-sm-3">ID SETOR</dt>
                                                        <dd class="col-sm-9"><b>: <?php echo strtoupper($data->id_setor) ?></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 TRANSAKSI</b></h6>
                    </label>
                    <table id="" class="table table-bordered table-sm">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Kode : Jenis Pembayaran</th>
                                <th>Jenjang</th>
                                <th>Bulanan</th>
                                <th>Record</th>
                                <th>Jumlah</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $classForm = new form();
                            $tingkat = $classForm->optSelect('tb_tingkat', "where {$jenjang}<>'' order by no asc");
                            while ($datatingkat = $tingkat->fetch(PDO::FETCH_OBJ)) {
                                $jenjang_opt = $datatingkat->{$jenjang};
                                echo "
                                        <tr>
                                            <td width='5px'>#</td>
                                            <td align=left colspan='4'><b>Jenjang Kelas $jenjang_opt</b></td>
                                        </tr>";
                                $classLaporan = new laporan();
                                $jenisBayar = $classLaporan->ShowjenisBayar($tahun, $jenjang_opt);
                                while ($datajenis = $jenisBayar->fetch(PDO::FETCH_OBJ)) {
                                    $jenis = $datajenis->kode;
                                    echo "
                                            <tr>
                                                <td width='5px'></td>
                                                <td align=left>$datajenis->kode : $datajenis->nama</td>
                                                <td align=left>$datajenis->kelas</td>
                                                <td align=left>$datajenis->bulanan</td>
                                                <td align=left>" . $classLaporan->CountRecordJenis($tb_transaksi, $jenis, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $jenjang_opt, $paralel_lap, $kategori_lap,) . "</td>
                                                <td align=right>" . $classLaporan->TransaksisubTotalJenis($tb_transaksi, $jenis, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $jenjang_opt, $kategori_lap,) . "</td>
                                            </tr>";
                                };
                                echo "
                                        <tr background='grey'>
                                        <td width='5px'></td>
                                        <td align=left colspan='3'><i>Jumlah Kelas $jenjang_opt </i></td>
                                        <td align=left>" . $classLaporan->CountRecordJenjang($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $jenjang_opt, $kategori_lap,) . "</td>
                                        <td align=right><i>" . $classLaporan->TransaksisubTotalJenjang($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $jenjang_opt, $kategori_lap,) . "</i></td>
                                         </tr>";
                            };
                            echo "
                                        <tr>
                                            <td width='5px'></td>
                                            <td align=left colspan='3'><b>TOTAL</b></td>
                                            <td align=left>" . $classLaporan->CountRecordAll($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $kategori_lap,) . "</td>
                                            <td align=right><b>" . $classLaporan->TransaksiTotal($tb_transaksi, $tgl_1, $tgl_2, $penerima_lap, $metode_lap, $kategori_lap,) . "</b></td>
                                        </tr>";
                            ?>

                        </tbody>
                    </table>
                </div>
                        -->
                <div class="card-body mx-4">
                    <label>
                        <h6><b class="text-primary">RINCIAN TRANSAKSI</b></h6>
                    </label>
                    <form action="<?php echo $link->pageAction('setor/setor_addTr_proses'); ?>" method="post">
                        <input name="id_setor" value="<?php echo $data->id_setor; ?>" hidden />
                        <input name="tb_transaksi" value="<?php echo $tb_transaksi; ?>" hidden />
                        <table id="example3" class="table table-bordered table-striped table-hover table-sm mb-4">
                            <thead>
                                <tr>
                                    <th><input type="checkbox" class="small check_all"></th>
                                    <th>Tanggal</th>
                                    <th>Nama </th>
                                    <th>NIS</th>
                                    <th>Kelas</th>
                                    <th>Kode : Jenis Pembayaran</th>
                                    <th>Periode</th>
                                    <th>Jumlah</th>
                                    <th>Setor</th>
                                    <th>Metode</th>
                                    <th>Penerima</th>
                                    <th>Catatan</th>

                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                $class = new setor();
                                $showList = $class->TransaksitoAdd($tb_transaksi, $tgl_1, $tgl_2, $jenis_lap, $penerima_lap, $metode_lap, $jenjang_lap, $paralel_lap, $kategori_lap);
                                while ($data = $showList->fetch(PDO::FETCH_OBJ)) {

                                    if ($data->jumlah == "" || $data->jumlah == 0) {
                                        $jumlah = number_format(0, 0, ",", ".");
                                    } else {
                                        $jumlah = number_format($data->jumlah, 0, ",", ".");
                                    };
                                    if ($data->setor == "Ya") {
                                        $setor = "Disetor";
                                    } else {
                                        $setor = "--";
                                    };
                                    $nis = $data->nis;
                                    $tahap = $data->tahap;

                                    echo "<input name='id_tr' value='$data->id_tr' hidden/>";
                                    //nama Bayar
                                    $classNB = new namaBayar();
                                    $ceknamaBayar = $classNB->ceknamaBayar($tahun, $data->jenis, $data->jenjang);

                                    //startAt
                                    $classTr = new transaksi();
                                    $startAt = $classTr->startAt($tb_siswa, $nis);
                                    $cekbulan = $classTr->cekPeriode($startAt, $tahap, $tahun1, $tahun2);

                                    //nama Bayar
                                    $classNB = new namaBayar();
                                    $ceknamaBayar = $classNB->ceknamaBayar($tahun, $data->jenis, $data->jenjang);

                                    $showJenis = $classNB->shownamaBayar($tahun, $data->jenis, $data->jenjang);
                                    $datajenis = $showJenis->fetch(PDO::FETCH_OBJ);
                                    $field_tang = 'tang_' . $datajenis->kode;
                                    $setbulanan = $datajenis->bulanan; //Cek Apakah Bulanan

                                    if ($setbulanan == "Tidak") {
                                        $periode = "Tahap " . $tahap;
                                    } else {
                                        $periode = $cekbulan;
                                    };

                                    $link = new linkpage;
                                    echo "
                                    <tr>
                                        <td width='5px'>
                                        <input type='checkbox' class='d-inline small chk_boxes1' name='id_tr[]' value='" . $data->no . "'> 
                                        </td>
                                        <td align=left>$data->time</td>
                                        <td align=left>$data->nama</td>
                                        <td align=left>$nis</td>
                                        <td align=left>$data->jenjang - $data->paralel</td>
                                        <td align=left>$data->jenis : $ceknamaBayar</td>
                                        <td align=left>$periode</td>
                                        <td align=left>" . $jumlah . "</td>
                                        <td align=left>$setor</td>
                                        <td align=left>$data->metode</td>
                                        <td align=left>" . $crud->getValue('user', "where id='$data->penerima'", 'nama') . "</td>
                                        <td align=left><i style='font-size:8px'>$data->catatan</i></td>
                                       
                                    </tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                        <div class="div d-inline">
                            <button type="submit" name="submitProseAdd" class="btn btn-primary btn-xs float-left px-2 mx-1 d-inline"><i class="bi bi-file-earmark-plus-fill"></i><b> PROSES TAMBAHKAN</b></button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- /.content-wrapper -->
<script type="text/javascript">
    $(function() {
        $('.check_all').click(function() {
            $('.chk_boxes1').prop('checked', this.checked);
        });
    });
</script>