<?php

error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);
?>

<!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-1">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Laporan Neraca []</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

<!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Buku Besar</h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>

              <!-- /.card-header -->
        <div class="card-body">
          <?php
          
          
          $kode=$_SESSION['kode'];
          if(isset($_POST['par1'])){


            $par1=$_POST['par1'];
            $par2=$_POST['par2'];
            $id_akun=$_POST['k_akun'];

            if($_POST['k_akun']=='Pilih')
              $where="where a.k_akun='0'";
            else
              $where="where a.kode_akun='$id_akun'  AND a.tanggal>='$par1' AND a.tanggal<='$par2' GROUP BY a.id_jurnal ORDER BY a.tanggal,a.id_jurnal ASC";

          }else{
            $par1="";
            $par2="";
            $id_akun="";

            $where="where a.kode='$kode' and a.kode_akun='$id_akun'";
           
          }
          ?>
          <form role="form" id="quickForm" action="home.php?home=<?php echo md5("buku_besar");?>" method="POST">
            <div class="form-group row">
              <label for="nama" class="col-1 m-2">Akun</label>
              <select id="k_akun" name="k_akun" class="form-control select2 col-sm-2" onchange="this.form.submit();">
                     <?php
          include 'koneksi.php';
          // ambil data dari database
            $query1 = "SELECT * FROM kode_akun order by k_akun";    
            /*https://kodingbagus.blogspot.com/2019/08/cara-membuat-pencarian-di-selectoption.html*/
            $hasil = mysqli_query($koneksi, $query1);
            while ($row = mysqli_fetch_array($hasil)) {
              $ket="";
              if(isset($_POST['k_akun'])){
                $desa=trim($_POST['k_akun']);
                if($desa==$row['k_akun'])
                {
                  $ket="selected";
                }
              }
                ?>
              
                <option  <?php echo $ket;?> value="<?php echo $row['k_akun']; ?>"><?php echo $row['k_akun']; ?>-<?php echo $row['n_akun']; ?></option>
                <?php
            }
            ?>
              <!-- https://kelasprogrammer.com/menampilkan-data-berdasarkan-pilihan-combo-box-di-php/ -->
        </select>
        <?php

        include 'koneksi.php';
        if(isset($_POST['par2'])){
        $k_akun = $_POST['k_akun'];
        $klap =mysqli_fetch_assoc(mysqli_query($koneksi,"SELECT klap as klap1 FROM kode_akun WHERE k_akun='$k_akun' "));
          extract($klap);
          $_SESSION['bukubesar']['klap']['sql']= $_POST['klap'];
        ?>
        <input type="hidden" name="klap" id="klap" class="form-control col-2" value="<?php echo "$klap1"; ?>" >
        <?php 
      }
        ?>

              <label  for="nama" class="col-2 m-2">Periode Tanggal</label>
              <input type="date" name="par1" class="form-control col-2" value="<?=@$par1?>"  required="">
              <input type="date" name="par2" class="form-control col-2" value="<?=@$par2?>" required="">
              <div class="col-1">
                <input type="submit" name="proses" class="btn btn-info" style="float: right" value="Proses">
              </div>
            </div>
          
          </form>
          <hr>

          <?php if(isset($_POST['proses'])){?>  
          <table id="sortTable" class="table table-bordered table-hover">
            <thead>
              <tr>
                <th>No Transaksi</th>
                <th>Tanggal</th>
                <th>Keterangan Transaksi</th>
                <th>Debet</th>
                <th>Kredit</th>
                <th>Saldo</th>
              </tr>
            </thead>
            <tbody>
              <?php

              $saldodb =mysqli_fetch_assoc(mysqli_query($koneksi,"SELECT IFNULL(SUM(debet-kredit),0) AS saldodb1 FROM bukubesar WHERE tanggal<='$par1' AND kode_akun='$id_akun' "));
              extract($saldodb);          
              $_SESSION['bukubesar']['saldodb1']['sql']=$saldodb1;
              $saldokd =mysqli_fetch_assoc(mysqli_query($koneksi,"SELECT IFNULL(SUM(kredit-debet),0) AS saldokd2 FROM bukubesar WHERE tanggal<='$par1' "));
              extract($saldokd);
              $_SESSION['bukubesar']['saldokd2']['sql']=$saldokd2;

              $queryb = "SELECT a.no_bukti AS no_bukti,a.tanggal AS tanggal,a.kode_akun AS kode_akun,a.keterangan AS keterangan,a.debet AS debet,a.kredit AS kredit from bukubesar a left join user b ON a.kode=b.kode $where  ";
              $querya       =mysqli_query($koneksi,$queryb);
               $_SESSION['bukubesar']['kode']['sql']= $queryb;             
                if(mysqli_num_rows($querya)){
                while ($data = mysqli_fetch_assoc($querya)) {
                  extract($data);
                   $saldo1+=$saldodb1+$debet-$kredit;
                   $saldo2+=$saldokd2+$kredit-$debet;
                   $saldobk = array("a" =>$saldo1,"p"=>$saldo2);
                   
                  ?>
                  <tr>
                    <td style="text-align: right; width: 7%;"><?php echo $no_bukti; ?></td>
                    <td style="text-align: center; width: 5%;"><?php echo date('d-m-Y',strtotime($tanggal)); ?></td>
                    <td style="text-align:left ; width: 35%;"><?php echo substr($keterangan,0,38); ?></td>
                    <td style="text-align: right; width: 11%;"><?php echo number_format($debet,0); ?></td>
                    <td style="text-align: right; width: 11%;"><?php echo number_format($kredit,0); ?></td>
                    
                    <th style="text-align: right; width: 11%;"><?php echo number_format($saldobk[$klap1],0);  ?></th>
                  </tr>
                <?php }}?>
              </tbody>
              </table>

              <script type="text/javascript" charset="utf8" src="./assets/jquery.dataTables.js"></script>
              <!-- <script type="text/javascript" charset="utf8" src="./assets/jquery-1.11.0.min"></script> -->
              <link rel="stylesheet" type="text/css" href="./assets/jquery.dataTables.css">
                  <script>
              $('#sortTable').DataTable(); //https://www.sahretech.com/2020/04/cara-membuat-pencarian-sorting-dan.html?m=1/ cara buat sorter table
              </script>

              <?php if(isset($_POST['par1'])){
                $_SESSION['bukubesar']['periode'] =date('Y-m-d',strtotime($_POST['par1'])). " s/d ". date('Y-m-d',strtotime($_POST['par2']));
                
                $_SESSION['bukubesar']['akun']=$_POST['k_akun'];
                
                if($_POST['k_akun']=='Pilih')
                  $_SESSION['bukubesar']['k_akun']='Pilih';
                else
                  $_SESSION['bukubesar']['k_akun']="select n_akun from kode_akun where k_akun='".$_POST['k_akun']."'";

                ?>
                <a href="pdf/lap_buku_besar_pdf.php" target="_blank" style="float: right;margin-top: 10px;" class="btn btn-success"><i class="fa fa-print"></i>PDF</a>

              <?php }} ?>
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>

      <!-- /.row -->
    </section>
    <!-- /.content -->


