<?php
include '../config/conn.php';
$blnini = date('n');
$thnini = date('Y');

header("Content-type: application/vnd-ms-excel");


if ($_GET['periode'] == 'y') {
    $tgl1 = $_GET['tanggal1'];
    $tgl2 = $_GET['tanggal2'];

    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE MONTH(tanggal)='$blnini' AND DAY(tanggal) BETWEEN '$tgl1' AND '$tgl2'");
    header("Content-Disposition: attachment; filename=data transaksi bulan $blnini $thnini dari tgl $tgl1 sampai tgl $tgl2.xls");
} else {

    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE MONTH(tanggal)='$blnini'");
    header("Content-Disposition: attachment; filename=data transaksi $blnini $thnini keseluruhan .xls");
}




$row_saldo = mysqli_fetch_array($query_saldo);
$saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];

$kredit = 0;
$debit = 0;
$saldo = 0;
$no = 0;

?>

<!-- page content -->
<div class="col" role="main">
    <div class="">
        <div class="clearfix"></div>

        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title" style="text-transform: capitalize; margin:0;">
                        <h3>Data Transaksi Bulan <?php echo $blnini;
                                                    if ($_GET['periode'] == 'y') {
                                                        echo " dari $tgl1 sampai $tgl2";
                                                    } 
                                                    echo "-".$thnini = date('Y');?></h3>

                        <div class="clearfix"></div>





                        <div class="col-md-3 col-sm-12 col-xs-12" style="margin-left: 0px;">

                            <h4>
                                <table>
                                    <tr style="color:green">
                                        <td><small><b>TOTAL DEBIT </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo $row_saldo['jumlah_debit']; ?></td>
                                    </tr>
                                    <tr style="color:red">
                                        <td id="hit"><small><b>TOTAL KREDIT </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo $row_saldo['jumlah_kredit']; ?></td>
                                    </tr>

                                    <tr>
                                        <td id="hit"><small><b>TOTAL </b></small></td>
                                        <td align="right">&nbsp; Rp. <?php echo $saldo_keseluruhan; ?></td>
                                    </tr>
                                </table>
                            </h4>

                        </div>
                        <div class="x_content">

                            <table border="1" id="datatable" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Kode transaksi</th>
                                        <th>Tanggal</th>
                                        <!-- JEnis kas -->
                                        <th>
                                            Jenis Transaksi
                                        </th>
                                        <th>Jenis Kas</th>
                                        <th>Keterangan</th>
                                        <th>Debit</th>
                                        <th>Kredit</th>
                                        <th width="110">Saldo</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    if ($_GET['periode'] == 'y') {
                                        $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE MONTH(tanggal)='$blnini' AND DAY(tanggal) BETWEEN '$tgl1' AND '$tgl2' order by tanggal ASC");
                                    } else {
                                        $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE MONTH(tanggal)='$blnini' order by tanggal ASC");
                                    }

                                    $count = 2;



                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>

                                        <tr style="background: <?php if ($row['kredit'] == 0) { ?>
                          #defff1;
                          <?php } else { ?>
                            #feeeea;
                            <?php } ?>">
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo "'" . $row['id_transaksi']; ?></td>
                                            <td><?php echo $row['tanggal']; ?></td>

                                            <!-- Jenis kas -->
                                            <td>
                                                <?php
                                                $jenistran = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$row[id_jenis]'");
                                                $jetran = mysqli_fetch_array($jenistran);
                                                echo $jetran['nama_jenis'];
                                                ?>
                                            </td>
                                            <?php
                                            $jeniskas = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$row[jenis_kas]'");
                                            $jenkas = mysqli_fetch_array($jeniskas);

                                            echo "<td>$jenkas[nama]</td>";
                                            ?>

                                            <td><?php
                                                echo $row['keterangan']; ?>
                                            <!-- SPP, DAM DAN KOPERASI -->
                                                  <?php if ($row['id_jenis'] == '5' or $row['id_jenis'] == '6' or $row['id_jenis'] == '14') { ?>
                                                    <?php $y  = mysqli_query($conn, "SELECT * FROM santri where id_santri='$row[id_santri]' order by id_santri asc");
                                                        while ($x = mysqli_fetch_array($y)) {
                                                          echo "#$x[id_santri]-$x[nama]";
                                                        ?>
                                                <?php }
                                                      } ?>
                                                </td>

                                            <?php if ($count == 1) { ?>

                                                <td><?php echo "Rp." . $row['debit']; ?></td>
                                                <td><?php echo "Rp." . $row['kredit']; ?></td>
                                                <td>
                                                    <?php
                                                    $debit = $row['debit'];
                                                    $saldo = $row['debit'];
                                                    echo "Rp." . $saldo;
                                                    ?>
                                                </td>

                                                <?php } else {
                                                if ($row['debit'] != 0) {
                                                ?>
                                                    <td><?php echo "Rp." . $row['debit']; ?></td>
                                                    <td><?php echo "Rp." . $row['kredit']; ?></td>
                                                    <td>
                                                        <?php

                                                        $debit = $debit + $row['debit'];
                                                        $saldo = $saldo + $row['debit'];
                                                        echo "Rp." . $saldo;
                                                        ?>


                                                    <?php } else { ?>
                                                    <td><?php echo "Rp." . $row['debit']; ?></td>
                                                    <td><?php echo "Rp." . $row['kredit']; ?></td>
                                                    <td>
                                                <?php

                                                    $kredit = $kredit + $row['kredit'];
                                                    $saldo = $saldo - $row['kredit'];
                                                    echo "Rp." . $saldo;
                                                }
                                            }
                                            $count++


                                                ?>



                                            <?php

                                        } ?>




                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>