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

$query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE DAY(tanggal)='$hariini' AND MONTH(tanggal)='$blnini'");


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


?>

<!-- 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;">
                        <h2>Data Transaksi Tanggal <?php echo $hariini . " " . namabulan(date('n')) . " " . date('Y');
                                                    ?></h2>

                        <div class="clearfix"></div>
                        <div class="well" style="overflow: auto">
                            <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 rupiah($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 rupiah($row_saldo['jumlah_kredit']); ?></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" height="2px" bgcolor="black"></td>
                                        </tr>
                                        <tr>
                                            <td id="hit"><small><b>TOTAL </b></small></td>
                                            <td align="right">&nbsp; Rp. <?php echo rupiah($saldo_keseluruhan); ?></td>
                                        </tr>
                                    </table>
                                </h4>

                            </div>
                            <div class="col-md-12 col-sm-12 col-xs-12" style="margin-left: 0px;">
                                <!-- CETAK -->
                                <a class="btn btn-primary btn-sm" href="component/tampil_data_hari_export.php"><i class="fa fa-print"></i>cetak</a>
                                <a href="./" class="btn btn-default btn-sm">kembali</a>

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

                                <table id="datatable" class="table table-striped table-bordered">
                                    <thead>
                                        <tr>
                                            <th width="20">Tipe</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;


                                        $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE DAY(tanggal)='$hariini' AND 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 if ($row['kredit'] == 0) { ?><a class="btn btn-success btn-xs"><i class="glyphicon glyphicon-save-file"></i></a> <?php } else { ?> <a class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-open-file"></i></a><?php } ?></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." . rupiah($row['debit']); ?></td>
                                                    <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                                                    <td>
                                                        <?php
                                                        $debit = $row['debit'];
                                                        $saldo = $row['debit'];
                                                        echo "Rp." . rupiah($saldo);
                                                        ?>
                                                    </td>

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


                                                        <?php } else { ?>
                                                        <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                                                        <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                                                        <td>
                                                    <?php
                                                        $kredit = $kredit + $row['kredit'];
                                                        $saldo = $saldo - $row['kredit'];
                                                        echo "Rp." . rupiah($saldo);
                                                    }
                                                }
                                                $count++


                                                    ?>



                                                <?php

                                            } ?>




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

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