<?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-2">
      <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 ">Laporan Neraca</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'];

         }else{
          $par1="";
          $par2="";
        }
        ?>
        <form action="home.php?home=<?php echo md5("neraca");?>" method="post">
          <div class="form-group row">
            <label  for="nama" class="col-1 m-2">Tgl</label>

            <input type="date" name="par1" class="form-control col-2" value="<?=@$par1?>" required="">
            <span class="col-0 m-2">S/d</span>
            <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="Preview">      
            </div class="col-1">
            <!-- <div><a href="laporan/lap_neraca_pdf.php" target="_blank" style="float: right;" class="btn btn-success" class="btn btn-success"><i class="fa fa-print"></i>PDF</a></div> -->
          </div>
        </form>

        <hr>

        <?php if(isset($_POST['par1'])){ ?>
          <h5 style="font-weight: bold;">AKTIVA</h5>
          <table class="table table-sm">
          <!-- <table class="table table-bordered table-hover"> -->
           
          <tr>
            <th >Kode</th>
            <th >Akun</th>
            <th style="text-align: center;"><?php echo date('d-M-Y', strtotime('-1 days', strtotime($_POST['par1'])));;?></th>
            <th style="text-align: center;">Mutasi</th>
            <th style="text-align: center;"><?php echo date('d-M-Y',strtotime($_POST['par2']));?></th>
          </tr>
          <tr>
            <th colspan="4">Aktiva Lancar</th>
          </tr>
          <?php
          $debetall=0;
          $queryz1      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun AS nama_akun,saldo.saldoAwal AS saldoAwal,SUM(debet-kredit)-saldo.saldoAwal AS mutasi,SUM(debet - kredit) AS saldoAkhir FROM bukubesar LEFT JOIN (SELECT kode_akun AS kode2,SUM(debet - kredit) AS saldoAwal FROM bukubesar WHERE tanggal <'$par1' AND kode_akun <='1.135.10' GROUP BY kode_akun) AS saldo ON bukubesar.kode_akun = saldo.kode2 LEFT JOIN (SELECT n_akun,k_akun FROM kode_akun)AS kode_akun ON bukubesar.kode_akun=kode_akun.k_akun WHERE bukubesar.tanggal <='$par2' AND bukubesar.kode_akun <='1.135.10' GROUP BY kode_akun having saldoAkhir<>0 ";
          $_SESSION['neraca']['sql1']=$queryz1;

          $resultz     = $mysqli->query($queryz1);
          $num_resultz = $resultz->num_rows;
          if ($num_resultz >0) {

            while ($dataz = mysqli_fetch_assoc($resultz)) {
              extract($dataz);
              $tsaldoAwal+=$dataz['saldoAwal'];
              $tsaldoAkhir+=$dataz['saldoAkhir'];
              $tmutasi+=$dataz['mutasi'];
              ?>
              <tr>
               <td width="10%" ><?php echo $dataz['kode_akun']; ?></td>
               <td ><?php echo $dataz['nama_akun']; ?></td>
               <td width="15%"align="right"><?php echo number_format($dataz['saldoAwal'],0); ?></td>
               <td width="15%"align="right"><?php echo number_format($dataz['mutasi'],0); ?></td>
               <td width="15%"align="right"><?php echo number_format($dataz['saldoAkhir'],0); ?></td>
             </tr>
           <?php }} ?>
           <tr>
            <th colspan="2">Jumlah</th>
            <th style="text-align: right;"><?=number_format(($tsaldoAwal),0)?></th>
            <th style="text-align: right;"><?=number_format(($tmutasi),0)?></th>
            <th style="text-align: right;"><?=number_format(($tsaldoAkhir),0)?></th>
          </tr>
          <tr>
            <th colspan="4">Aktiva Tetap</th>
          </tr>
          <?php
          
          $queryz2      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun AS nama_akun,saldo.saldoAwal AS saldoAwal,SUM(debet-kredit)-saldo.saldoAwal AS mutasi,SUM(debet - kredit) AS saldoAkhir FROM bukubesar LEFT JOIN (SELECT kode_akun AS kode2,SUM(debet - kredit) AS saldoAwal FROM bukubesar WHERE tanggal <'$par1'  AND kode_akun >'1.135.10' AND kode_akun <'2.220.10' GROUP BY kode_akun) AS saldo ON bukubesar.kode_akun = saldo.kode2 LEFT JOIN (SELECT n_akun,k_akun FROM kode_akun)AS kode_akun ON bukubesar.kode_akun=kode_akun.k_akun WHERE bukubesar.tanggal <='$par2' AND bukubesar.kode_akun >'1.135.10' AND bukubesar.kode_akun <'2.220.10' GROUP BY kode_akun having saldoAkhir>0";
          $_SESSION['neraca']['sql2']=$queryz2;

          $resultz     = $mysqli->query($queryz2);
          $num_resultz = $resultz->num_rows;
          if ($num_resultz > 0) {

            while ($dataz2 = mysqli_fetch_assoc($resultz)) {
              $tsaldoAwal2+=$dataz2['saldoAwal'];
              $tmutasi2+=$dataz2['mutasi'];
              $tsaldoAkhir2+=$dataz2['saldoAkhir'];
              

              ?>
              <tr>
               <td width="10%"><?php echo $dataz2['kode_akun']; ?></td>
               <td ><?php echo $dataz2['nama_akun']; ?></td>
               <td width="15%" align="right"><?php echo number_format($dataz2['saldoAwal'],0); ?></td>
               <td width="15%" align="right"><?php echo number_format($dataz2['mutasi'],0); ?></td>
               <td width="15%" align="right"><?php echo number_format($dataz2['saldoAkhir'],0); ?></td>
             </tr>

           <?php }} ?>
           
           <tr>
             <th colspan="2">Jumlah</th>
             <th style="text-align: right;"><?=number_format($tsaldoAwal2,0)?></th>
             <th style="text-align: right;"><?=number_format($tmutasi2,0)?></th>
             <th style="text-align: right;"><?=number_format($tsaldoAkhir2,0)?></th>
           </tr>

           

            <?php
              $taktiva1=$tsaldoAwal+$tsaldoAwal2;
              $taktivam=$tmutasi+$tmutasi2;
              $taktiva2=$tsaldoAkhir+$tsaldoAkhir2;
            ?>
            <tr>
             <th colspan="2">Total Aktiva</th>
             <th style="text-align: right;"><?=number_format(($taktiva1),0)?></th>
             <th style="text-align: right;"><?=number_format(($taktivam),0)?></th>
             <th style="text-align: right;"><?=number_format(($taktiva2),0)?></th>
           </tr>
         
         </tbody>
       </table>
     </br>
       <h5 style="font-weight: bold;">PASIVA</h5>

       <table class="table table-sm">
        <tr>
            <th >Kode</th>
            <th >Akun</th>
            <th style="text-align: center;"><?php echo date('d-M-Y', strtotime('-1 days', strtotime($_POST['par1'])));?></th>
            <th style="text-align: center;">Mutasi</th>
            <th style="text-align: center;"><?php echo date('d-M-Y',strtotime($_POST['par2']));?></th>
          </tr>
        <?php
        $tsaldoAwal3=0;
        $tmutasi3=0;
        $tsaldoAkhir3=0;
        $queryz3      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun AS nama_akun,saldo.saldoAwal AS saldoAwal,SUM(kredit-debet)-saldo.saldoAwal AS mutasi,SUM(kredit - debet) AS saldoAkhir FROM bukubesar LEFT JOIN (SELECT kode_akun AS kode2,SUM(kredit - debet) AS saldoAwal FROM bukubesar WHERE tanggal <'$par1'  AND kode_akun like '%2.%' GROUP BY kode_akun) AS saldo ON bukubesar.kode_akun = saldo.kode2 LEFT JOIN (SELECT n_akun,k_akun FROM kode_akun)AS kode_akun ON bukubesar.kode_akun=kode_akun.k_akun WHERE bukubesar.tanggal <='$par2'  AND bukubesar.kode_akun like '%2.%' GROUP BY kode_akun having saldoAkhir<>0";
        $_SESSION['neraca']['sql3']=$queryz3;

        $resultz     = $mysqli->query($queryz3);
        $num_resultz = $resultz->num_rows;
        if ($num_resultz > 0) {

          while ($dataz3 = mysqli_fetch_assoc($resultz)) {
              $tsaldoAwal3+=$dataz3['saldoAwal'];
              $tmutasi3+=$dataz3['mutasi'];
              $tsaldoAkhir3+=$dataz3['saldoAkhir'];
            ?>
            <tr>
             <td width="10%"><?php echo $dataz3['kode_akun']; ?></td>
             <td ><?php echo $dataz3['nama_akun']; ?></td>
             <td width="15%" align="right"><?php echo number_format($dataz3['saldoAwal'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($dataz3['mutasi'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($dataz3['saldoAkhir'],0); ?></td>
           </tr>
         <?php }} ?>

         <?php
        $tsaldoAwals=0;
        $tmutasis=0;
        $tsaldoAkhirs=0;
        $queryzs      = "SELECT tanggal AS tanggal,'3.320.60' AS kode_akun,'LABA BERJALAN' AS nama_akun,saldo.saldoAwal AS saldoAwal,SUM(kredit-debet)-saldo.saldoAwal AS mutasi,SUM(kredit - debet) AS saldoAkhir FROM bukubesar LEFT JOIN (SELECT kode_akun AS kode2,SUM(kredit - debet) AS saldoAwal FROM bukubesar WHERE tanggal <'$par1'  AND kode_akun >='4.400') AS saldo ON bukubesar.kode_akun = saldo.kode2 LEFT JOIN (SELECT n_akun,k_akun FROM kode_akun)AS kode_akun ON bukubesar.kode_akun=kode_akun.k_akun WHERE bukubesar.tanggal <='$par2'  AND bukubesar.kode_akun >='4.400' " ;
        $_SESSION['neraca']['sqls']=$queryzs;

        $resultz     = $mysqli->query($queryzs);
        $num_resultz = $resultz->num_rows;
        if ($num_resultz > 0) {

          while ($datazs = mysqli_fetch_assoc($resultz)) {
              $tsaldoAwals+=$datazs['saldoAwal'];
              $tmutasis+=$datazs['mutasi'];
              $tsaldoAkhirs+=$datazs['saldoAkhir'];
            ?>
            <tr>
             <td width="10%"><?php echo $datazs['kode_akun']; ?></td>
             <td ><?php echo $datazs['nama_akun']; ?></td>
             <td width="15%" align="right"><?php echo number_format($datazs['saldoAwal'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($datazs['mutasi'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($datazs['saldoAkhir'],0); ?></td>
           </tr>
         <?php }} ?>



         <tr>
           <th colspan="2">Total Pasiva</th>
           <th style="text-align: right;"><?php echo number_format(($tsaldoAwals+$tsaldoAwal3),0)?></th>
           <th style="text-align: right;"><?php echo number_format(($tmutasis+$tmutasi3),0)?></th>
           <th style="text-align: right;"><?php echo number_format($tsaldoAkhirs+$tsaldoAkhir3,0)?></th>
         </tr>
       </tbody>
     </table>
   <?php } ?>

   <?php if(isset($_POST['par1'])){
          
    $_SESSION['neraca']['periode1'] =tgl_indo($_POST['par1']);
    $_SESSION['neraca']['periode2'] =tgl_indo($_POST['par2']);
    
    ?>
    <a href="pdf/lap_neraca_pdf.php" target="_blank" style="float: right; margin-top: 10px;" class="btn btn-success" class="btn btn-success"><i class="fa fa-print"></i>PDF</a>
    <!-- <a href="laporan/lap_neraca_pdf.php" target="_blank" style="float: right;margin-top: 10px;" class="btn btn-success"><i class="fa fa-print"></i> Cetak PDF</a> -->

  <?php } ?>

</div>
<div class="card-footer bg-info">
                SAK-ETAP
              </div>
              
            <!-- /.card -->
          </div>
        </div>
      </div>
    </section>
    <!-- /.content -->
  <!-- </div> -->
  <!-- /.content-wrapper -->

