<?php
header("Content-type: application/vnd-ms-excel");
if (isset($_GET['bln']) && isset($_GET['thn'])) {
  $bln = $_GET['bln'];
  $thn = $_GET['thn'];
  $periode = "bulan $bln tahun  $thn";
  //tanggal akhir
  $kalender= CAL_GREGORIAN;
  $hari = cal_days_in_month($kalender,$bln,$thn);
  $tgl2="$hari-$bln-$thn";
  header("Content-Disposition: attachment; filename=Rekap buku besar $thn-$bln.xls");
} else {
  $tgl1 = $_GET['tgl1'];
  $tgl2 = $_GET['tgl2'];
  $periode = "$tgl1 sampai $tgl2";
  header("Content-Disposition: attachment; filename=Rekap buku besar $tgl1 sampai $tgl2.xls");
}


?>
<?php
include '../../config/conn.php';

?>
<!-- page content -->
<h2 align="center">REKAP BUKU BESAR/KAS <?php echo $periode; ?><small></small></h2>

<table border="1">
  <thead>
    <tr>
      <!--<th>Tanggal</th>-->
      <!--<th>id transaksi</th> -->
      <th width="15px">NO.</th>
      <th width="15px">Kode</th>
      <th>keterangan</th>
      <th>Debit</th>
      <th>Kredit</th>
      <th width="110">TOTAL<br>(debit-kredit)</th>
    </tr>
  </thead>
  <tbody>
    <?php
    $no = 0;
    $count = 1;
    $total_rekap = 0;
    $total_debit = 0;
    $total_kredit = 0;
    $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis NOT IN (1) order by id_jenis ASC ");


    $ro = 0;



    while ($row = mysqli_fetch_array($query)) {


      if (isset($_GET['bln']) && isset($_GET['thn'])) {
        $query_rekap = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_jenis ='$row[id_jenis]'
                               AND MONTH(tanggal)='$bln' AND YEAR(tanggal)='$thn'");
      } else {
        $query_rekap = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_jenis ='$row[id_jenis]'
                               AND tanggal BETWEEN '$tgl1' AND '$tgl2'");
      }
      $rekap = mysqli_fetch_array($query_rekap);
      $no++;
    ?>

      <tr style="background: <?php if ($no % 2 == 1) { ?>
                          #defff1;
                          <?php } else { ?>
                            white;
                            <?php } ?>">

        <!--<td><?php //echo $row['id_transaksi'];
                ?></td>-->
        <td><?php echo $no; ?></td>
        <td><?php echo $row['id_jenis']; ?></td>
        <td><?php echo $row['nama_jenis']; ?></td>

        <td><?php echo $rekap['jumlah_debit']; ?></td>
        <td><?php echo $rekap['jumlah_kredit']; ?></td>
        <td>
          <?php
          $total = $rekap['jumlah_debit'] - $rekap['jumlah_kredit'];
          echo $total;
          $total_debit = $total_debit + $rekap['jumlah_debit'];
          $total_kredit = $total_kredit + $rekap['jumlah_kredit'];
          $total_rekap = $total_rekap + $total;
          ?>
        </td>

        <?php
        $count++
        ?>

      </tr>

    <?php } ?>

    <tr bgcolor="#f6eee0">
      <td colspan="3" align="center"><b>TOTAL</b></td>
      <td><?php echo $total_debit; ?></td>
      <td><?php echo $total_kredit; ?></td>
      <td><?php echo $total_rekap; ?></td>
    </tr>



  </tbody>
</table>



<!-- <table>
<tr>
<th>NO</th>
<th>Jenis Kas</th>
<th>Jumlah</th>
</tr>

<tr>
<?php
// $n=1;
//                   $namakas = mysqli_query($conn, "SELECT * FROM jenis_kas ORDER BY id");
//                   while ($jenkas = mysqli_fetch_array($namakas)) {
//                     $perkas = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(debit) as debit, SUM(kredit) as kredit 
//                       FROM transaksi WHERE tanggal BETWEEN '01-01-2015' AND '01-01-2022' AND jenis_kas='$jenkas[id]' "));
//                     $kas = $perkas['debit'] - $perkas['kredit'];
//                   ?>
<th><?php // echo $n++; ?></th>
<th><?php //echo $jenkas['nama']; ?></th>
<th><?php //echo $kas; ?></th>
</tr>
<?php  // } ?>
</table> -->