<?php

$time = new time;
$format = new format;
$classTr = new transaksi;
$tglsekarang = $time->getDateInput();

if (isset($_POST['tanggal1'])) {
    $tgl_1Eng = $format->dateToEng($_POST['tanggal1']);
    $tgl_1 = $_POST['tanggal1'];
} else {
    $tgl_1Eng = $time->getDate();
    $tgl_1 = $tglsekarang;
};
if (isset($_POST['tanggal2'])) {
    $tgl_2Eng =  $format->dateToEng($_POST['tanggal2']);
    $tgl_2 = $_POST['tanggal2'];
} else {
    $tgl_2Eng = $time->getDate();
    $tgl_2 = $tglsekarang;
};

if ($level == "admin") {

    if (isset($_POST['penerima']) && $_POST['penerima'] != "Semua") {
        $penerima_lap = $_POST['penerima'];
        $val_penerima = $crud->getValue('user', " where id_user='$_POST[penerima]' || id='$_POST[penerima]'", 'nama');
        $filterPenerima = " and penerima='$penerima_lap'";
    } else {
        $penerima_lap = "Semua";
        $val_penerima = "Semua";
        $filterPenerima = "";
    };
} else {
    $penerima_lap = $_POST['penerima'];
    $val_penerima = $crud->getValue('user', " where id_user='$_POST[penerima]' || id='$_POST[penerima]'", 'nama');
    $filterPenerima = " and penerima='$penerima_lap'";;
}


//if (isset($_POST['penerima']) && $_POST['penerima'] != "Semua") {
//    $penerima_lap = $_POST['penerima'];
//    $val_penerima = $classTr->namaPenerimaByID($penerima_lap);
//    $filterPenerima = " and penerima='$penerima_lap'";
//} else {
//    $penerima_lap = "Semua";
//    $val_penerima = "Semua";
//    $filterPenerima = "";
//};

if (isset($_POST['jenjang']) && $_POST['jenjang'] != "Semua") {
    $jenjang_lap = $_POST['jenjang'];
    $filterJenjang = " and jenjang='$jenjang_lap'";
} else {
    $jenjang_lap = "Semua";
    $filterJenjang = "";
};
if (isset($_POST['paralel']) && $_POST['paralel'] != "Semua") {
    $paralel_lap = $_POST['paralel'];
    $filterParalel = " and paralel='$paralel_lap'";
} else {
    $paralel_lap = "Semua";
    $filterParalel = "";
};
if (isset($_POST['metode']) && $_POST['metode'] != "Semua") {
    $metode_lap = $_POST['metode'];
    $filterMetode = " and metode='$metode_lap'";
} else {
    $metode_lap = "Semua";
    $filterMetode = "";
};


?>
<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-person-badge"></span> Data Pembayaran </b>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body my-4 mx-4">
                            <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">
                                            <option><?php echo $jenjang_lap; ?></option>
                                            <option>Semua</option>
                                            <?php
                                            $sekolah = new sekolah;

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

                                            $opt = new form();
                                            $opval = $opt->optSelect('tb_tingkat', "where {$jenjang}<>'' order by tingkat 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>
                                            <option>Semua</option>
                                            <?php
                                            $opval = $opt->optSelect('tb_paralel', ' order by paralel 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">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("user where status='Aktif' && (level='admin' || level='teller') order by nama asc");
                                                echo "<option value='$penerima_lap'>$val_penerima</option>";
                                                echo "<option value='Semua'>Semua</option>";
                                            } elseif ($level = "teller") {
                                                $opval = $crud->showTable("user where id='$user_id' || id_user='$penerimaID' order by nama asc");
                                                echo "<option value='$user_id'>$val_penerima</option>";
                                            } else {
                                                $opval = $crud->showTable($tb_transaksi . " where penerima='$user_id' || penerima='$penerimaID'  group by penerima order by penerima asc");
                                                echo "<option value=''>--#Ref--</option>";
                                            };

                                            while ($datapnr = $opval->fetch(PDO::FETCH_OBJ)) {

                                                $countUser = $crud->countData('user', " where (id='$datapnr->id' || id_user='$datapnr->id_user') && status='Aktif' && (level='admin' || level='teller')");
                                                if ($countUser > 0) {
                                                    $namaP = $crud->getValue('user', " where (id='$datapnr->id' || id_user='$datapnr->id_user') && status='Aktif' && (level='admin' || level='teller')", 'nama');
                                                } else {
                                                    $namaP = "#REF";
                                                };

                                                echo "<option value='$datapnr->id'>$namaP</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 value="Semua">Semua</option>
                                            <option value="Tunai">Tunai</option>
                                            <option value="Bank">Bank/Transfer</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row mt-4">
                                    <label class="col-sm-3 col-form-label">Jumlah Record Transaksi</label>
                                    <div class="col-md-9"><b>
                                            <?php //$classLaporan = new laporan();
                                            // $db = database::getInstance();
                                            //$show = $db->runQuery("SELECT SUM(jumlah) FROM {$tb_transaksi}");
                                            // $value = $show->count();
                                            $count = $crud->countData($tb_transaksi, '');
                                            //$value = $data[0];
                                            echo $format->ribuan($count);
                                            ?> Record</b>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="col-sm-3 col-form-label">Total Transaksi</label>
                                    <div class="col-md-9">
                                        <b>Rp.
                                            <?php //$classLaporan = new laporan();
                                            $db = database::getInstance();
                                            $show = $db->runQuery("SELECT SUM(jumlah) FROM {$tb_transaksi}");
                                            $data = $show->fetch();
                                            $value = $data[0];
                                            echo $format->ribuan($value);
                                            ?></b>
                                    </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-search"></i> <b>SEARCH</b></button>
                                    </div>
                                </div>

                            </form>
                            <a <?php echo $link->page('transaksi/kuitansi_read'); ?> class="btn btn-primary btn-xs mb-2 btn-rounded small px-2"> <i class="bi bi-printer"></i> CETAK ULANG KUITANSI</a>

                            <form action="" method="post">
                                <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>Info</th>
                                            <th>Tanggal</th>
                                            <th>Nama</th>
                                            <th>NIS</th>
                                            <th>Kelas</th>
                                            <th>Jenis </th>
                                            <th>Jumlah</th>

                                            <?php

                                            if ($level == "admin") {
                                                echo " <th>Edit</th>";
                                            } else {
                                                echo "";
                                            }; ?>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        <?php

                                        $crud = new crudData();
                                        $clstime = new time;
                                        $now = $clstime->getdate();

                                        $showList = $crud->showTable($tb_transaksi . " where tanggal>='$tgl_1Eng' && tanggal<='$tgl_2Eng' $filterPenerima $filterJenjang $filterParalel $filterMetode order by tanggal desc");
                                        while ($data = $showList->fetch(PDO::FETCH_OBJ)) {
                                            $format = new format();
                                            if ($data->setor == "Ya") {
                                                $setor = "Disetor";
                                            } else {
                                                $setor = "--";
                                            };
                                            $nis = $data->nis;
                                            $tahap = $data->tahap;

                                            //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);

                                            $countCodeBayar = $crud->countData('tb_nama_bayar', "where kode='$data->jenis' and tp='{$tahun}' and kelas='{$data->jenjang}'");

                                            if ($countCodeBayar > 0) {
                                                $field_tang = 'tang_' . $datajenis->kode;
                                                $setbulanan = $datajenis->bulanan; //Cek Apakah Bulanan
                                            } else {
                                                $field_tang = "";
                                                $setbulanan = "";
                                            }

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

                                            //Proses Setor
                                            $clsSetor = new setor;
                                            $statusSetor = $clsSetor->statusSetor($data->setor);

                                            $link = new linkpage;
                                            $penerima = $classTr->namaPenerimaByID($data->penerima);
                                            $checkbox = "<input type='checkbox' class='d-inline small chk_boxes1' name='transaksi_delete[]' value='" . $data->id . "'>";
                                            if ($data->setor != "") {
                                                $check = "";
                                            } else {
                                                $check = $checkbox;
                                            };

                                            if ($level == "admin") {
                                                $edit = "<td width='75px' align='center'>
                                            <a " . $link->pageID($data->id, 'transaksi/transaksi_edit') . " class='badge badge-warning  px-1 py-1 d-inline''> <i class='bi bi-pencil-square'></i> EDIT</a>
                                            </td>";
                                            } else {
                                                $edit = "";
                                            };

                                            //cek data siswa
                                            $count_siswa = $crud->countData($tb_siswa, " where nis='$nis'");
                                            if ($count_siswa > 0) {
                                                $bg_tr = "";
                                            } else {
                                                $bg_tr = "bg-danger text-light text-bold";
                                            };

                                            echo "
                                                    <tr class='$bg_tr'>
                                                        <td width='5px'>$check</td>
                                                        <td width='50px' align='center'>
                                                        " . $modal->modalTabunganTr('./view/transaksi/transaksi_view', 'viewTr', $data->id . $tahun, 'xl', 'default', 'DETAIL TRANSAKSI', "<strong class='bi bi-search text-secondary'> DETAIL TRANSAKSI</strong>", 'text-light', '<b class=text-light>DETAIL</b>', 'primary', 'DETAIL') . "
                                                         </td>
                                                        <td align=center>" . $format->dateToIndo($data->tanggal) . "</td>
                                                        <td align=left>$data->nama</td>
                                                        <td align=center>$nis</td>    
                                                        <td align=left>$data->jenjang - $data->paralel</td>                                                           
                                                        <td align=left>$data->jenis</td>                    
                                                        <td align=right>" . $format->ribuan($data->jumlah) . "</td>                                                    
                                                     
                                                        $edit
                                                    </tr>";
                                        }

                                        ?>

                                    </tbody>
                                </table>

                                <div class="div d-inline">
                                    <div class="col-lg-3 float-right mt-4">
                                        <?php echo $link->exportXlsx('transaksi/transaksi_export', $tb_transaksi); ?>
                                    </div>
                                    <input type="text" name="alasan" value="" class="form-control form-control-sm fontInput col-lg-6" placeholder="Isi Alasan Hapus Transaksi" required>
                                    <label for="exampleEmail" class="col-lg-6 col-md-6 col-sm-12 col-form-label">Alasan Hapus <small><i class='text-danger'>Isi alasan jika ingin menghapus Transaksi</i></small> </label>

                                </div>
                                <button type="submit" name="submit" class="btn btn-danger btn-xs float-left mt-1"> <i class="bi bi-trash"></i> HAPUS TRANSAKSI</button>

                        </div>
                    </div>

                    </form>

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

<?php

$direct = new toast();
$clsTime = new time;
$clsIP = new ip;
$clsCrud = new crudData;

$waktu = $clsTime->getTime();
$ip = $clsIP->getIP();
$browser = $clsIP->getBrowser();
$user = $clsCrud->getValue('user', " where username='$username'", 'id');

if (isset($_POST['submit']) && isset($_POST['transaksi_delete'])) {
    foreach ($_POST['transaksi_delete'] as $id) {
        //Insert ke DB Transaksi

        $tanggal = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'tanggal');
        $nis = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'nis');
        $nama = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'nama');
        $kelamin = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'kelamin');
        $jenjang = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'jenjang');
        $paralel = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'paralel');
        $jenis = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'jenis');
        $tahap = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'tahap');
        $jumlah = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'jumlah');
        $random = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'random');
        $penerima = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'penerima');
        $setor = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'setor');
        $id_setor = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'id_setor');
        $metode = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'metode');
        $catatan = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'catatan');
        $time = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'time');
        $ip = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'ip');
        $browser = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'browser');
        $id_tr = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'id_tr');
        $id = $clsCrud->getValue($tb_transaksi, "where id='$id'", 'id');

        $alasan = $_POST['alasan'];

        $dataarray = [
            'tanggal' => $tanggal,
            'nis' => $nis,
            'nama' => $nama,
            'kelamin' => $kelamin,
            'jenjang' => $jenjang,
            'paralel' => $paralel,
            'jenis' => $jenis,
            'tahap' => $tahap,
            'jumlah' => $jumlah,
            'random' => $random,
            'penerima' => $penerima,
            'setor' => $setor,
            'id_setor' => $id_setor,
            'metode' => $metode,
            'catatan' => $catatan,
            'time' => $time,
            'ip' => $ip,
            'browser' => $browser,
            'id_tr' => $id_tr,
            'id' => $id,
            'deleted_at' => $waktu,
            'deleted_by' => $user,
            'alasan' => $alasan
        ];
        $clsCrud->insertData('deleted_transaksi', $dataarray);
        $delete = $clsCrud->deleteData($tb_transaksi,  'id',  $id);
    }
    echo  $direct->redirect('toast', 'delete', 'transaksi/transaksi_read');
} else {
    echo "";
};
