<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];

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

      <div class="row">
        <div class="col-md-6">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">

              <?php if ($_GET['periode'] == 'y') {
             
                $tanggal2 = $_POST['tanggal2'];
               
                $tgl2 = date('Y-m-d', strtotime($tanggal2));
                echo "<h2>Rekap buku besar/Kas periode $tanggal2</h2>";
              } else {
                $tglhariini = date('d-m-Y');
                echo "<h2>Rekap buku besar/Kas $tglhariini</h2>";
              } ?>



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


              <form action="?module=<?php echo $_GET['module']; ?>&periode=y" enctype="multipart/form-data" method="POST">


                <div class="well" style="overflow: auto">

                  <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                    <label for="nama">Pilih Periode Sampai:</label>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">

                    <div class="input-group">
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                    echo $tanggal2;
                                                                                                  } else {
                                                                                                    echo "";
                                                                                                  } ?>" id="tanggal2" name="tanggal2">
                    </div>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">
                    <button type="submit" class="btn btn-primary btn-sm">Pilih</button>
              </form>


            </div>

            <div class="x_content">


              <table id="" class="table table-bordered">
                <thead>
                  <tr>
                    <th width="15px">NO.</th>
                    <th>Jenis</th>
                    <th>SALDO</th>
                 </tr>
                </thead>
         
                
                <tbody>
                    
                     <?php
                     $no=0;
                     $kas_total=0;
                  $namakas = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE jenis='0' ORDER BY nama_jenis ASC");
                  while ($jenkas = mysqli_fetch_array($namakas)) {
                      
                      
                       if ($_GET['periode'] == 'y') {
                           
                      $perkas = mysqli_fetch_array(mysqli_query($conn, 
                      "SELECT 
                          SUM(debit) AS debit, 
                          SUM(kredit) AS kredit,
                          (SELECT SUM(kredit) FROM transaksi 
                          WHERE id_jenis_sumber = '$jenkas[id_jenis]' 
                            AND tanggal <= '$tgl2') AS kredit_sumber
                      FROM transaksi 
                      WHERE id_jenis = '$jenkas[id_jenis]' 
                        AND tanggal <= '$tgl2'"));

                      
                           
                       }else{
                        $perkas = mysqli_fetch_array(mysqli_query($conn, 
                        "SELECT 
                            SUM(debit) AS debit, 
                            SUM(kredit) AS kredit,
                            (SELECT SUM(kredit) FROM transaksi WHERE id_jenis_sumber = '$jenkas[id_jenis]') AS kredit_sumber
                        FROM transaksi 
                        WHERE id_jenis = '$jenkas[id_jenis]'"));
                    
                           
                       }
                      
                    $kas = $perkas['debit'] - $perkas['kredit'] - $perkas['kredit_sumber'];
                    $kas_total=$kas_total + $kas;
                    $no++;
                  ?>

                    
                   <tr bgcolor="#defff1">
                    <td><?php echo $no; ?></td>
                    <td ><b><h2></h2><?php echo $jenkas['nama_jenis']; ?></h2></b></td>
                    <td align="right"><b><h2>Rp. <?php echo rupiah($kas); ?></h2></b></td>
                  </tr>
                 
                  <?php } ?>
                   <tr bgcolor="#defff1">
                   
                    <td colspan="2" align="center"><b><h2></h2>TOTAL</h2></b></td>
                    <td align="right"><b><h2>Rp. <?php echo rupiah($kas_total); ?></h2></b></td>
                  </tr>
                </tbody>
              </table>
              <?php
              if ($_GET['periode'] == 'y') {
                echo "<a href='component/com_transaksi/export.php?tgl1=$tgl1&tgl2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              } else {
                echo "<a href='component/com_transaksi/export.php?bln=$bln&thn=$thn' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              }
              ?>
            </div>



          </div>

        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>



<?php } ?>