
<?php 
    include 'header.php'; 
?>            
<div class="container">

  <div class="my-4 border-bottom">
    <h4 class="fw-semibold fs-4">Laporan Buku Per Unit
    </h4>  
  </div>
  <section class="content">
    <div class="row">
      <section class="col-lg-14">
        <div class="box box-info">
          <div class="box-header">
          <div class="box-body">
            <form method="get" action="">
              <div class="row">
                <div class="col-md-2">
                  <div class="form-group">
                    <label>Dari</label>
                    <input type="date" class="form-control" name="tanggal_d" required="required" placeholder="Tanggal dari ..">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <label>Sampai</label>
                    <input type="date" class="form-control" name="tanggal_s" required="required" placeholder="Sampai tanggal ..">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <br/>
                    <input type="submit" value="TAMPILKAN LAPORAN" class="btn btn-sm btn-primary">
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
        
        <div class="box box-info">
          <div class="box-header">
            <h6 class="box-title"></h6>
          </div>
          <div class="box-body">
            <?php 
            if(isset($_GET['tanggal_s']) && isset($_GET['tanggal_d'])){
              $tgl_d = $_GET['tanggal_d'];
              $tgl_s = $_GET['tanggal_s'];
              
              $tgl_pertama    =strtotime("$tgl_d");
              $tgl_kemarin    =date('Y-m-d', strtotime("-1 day", $tgl_pertama));       
            } else { ;
                $tgl_d    = '2024-01-01';
                $tgl_s    = $_SESSION['tgl_sd'];
            }
            $nosub='1101';
            $unit=$_GET['xunit'] ;
                $sql1 = mysqli_query($koneksi, "SELECT sum(awald) as tn_awal FROM nawal WHERE left(noperk,4)='$nosub' and kdu='$unit' ");
                    list($tn_awal) = mysqli_fetch_array($sql1);
                    
                $sql2 = mysqli_query($koneksi, "SELECT sum(mutasid-mutasik) as tj_awal FROM jor WHERE left(noperk,4)='$nosub' and tgljornal<='$tgl_d' and unit='$unit' ");
                    list($tj_awal) = mysqli_fetch_array($sql2);                    
                
                $tsaldo_awal=$tn_awal+$tj_awal;
                
                $sql3 = mysqli_query($koneksi, "SELECT unit AS namaunit FROM a_kd_unit WHERE kd='$unit' ");
                    list($namaunit) = mysqli_fetch_array($sql3);
                
                
              ?>

              <div class="row">
                <div class="col-lg-6">
                  <table class="table table-bordered">
                    <tr>
                      <th width="40%">KAS UNIT</th>
                      <th width="1%">:</th>
                      <td><?php echo $unit ; ?> - <?php echo $namaunit ; ?></td>
                    </tr>
                    <tr>
                      <th width="40%">DARI TANGGAL</th>
                      <th width="1%">:</th>
                      <td><?php echo date('d-m-Y', strtotime($tgl_d)); ?></td>
                    </tr>
                    <tr>
                      <th>SAMPAI TANGGAL</th>
                      <th>:</th>
                      <td><?php echo date('d-m-Y', strtotime($tgl_s)); ?></td>
                    </tr>
                    <tr>
                      <th>Saldo Per <?php echo date('d-m-Y', strtotime($tgl_d)); ?></th>
                      <th>:</th>
                      <td><?php echo "Rp.  " . number_format("$tsaldo_awal", 0, ",", "."); ?></td>
                    </tr>                    
                  </table>
                </div>
              </div>

      <button type="button" class="btn btn-warning btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="lap_a_kas_tgl_sd_tgl_lihat.php?" class="btn btn-warning btn-sm"><i class="bi bi-eyeglasses"></i> &nbsp SEMUA</a> 
      </button>
      <button type="button" class="btn btn-success btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="lap_a_kas_tgl_sd_tgl_lihat_unit.php?xunit=01" class="btn btn-success btn-sm"><i class="bi bi-eyeglasses"></i> &nbsp KAS USP</a> 
      </button>
      <button type="button" class="btn btn-primary btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="lap_a_kas_tgl_sd_tgl_lihat_unit.php?xunit=02" class="btn btn-primary btn-sm"><i class="bi bi-eyeglasses"></i> &nbsp KAS INDUK</a> 
      </button>      
      <button type="button" class="btn btn-info btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="index.php" class="btn btn-info btn-sm pull-right"><i class="bi bi-reply"></i> &nbsp; Kembali</a> 
      </button>
              <div class="table-responsive">
                <table class="table table-bordered table-striped" id="table-datatable">
          <thead>
            <tr>
              <th width="1%" class="text-center" style="text-align:left; font-size: 9pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 9pt; font-weight:normal;">Notrans</th>
              <th width="4%" class="text-center">Tanggal</th>
              <th width="46%" class="text-center">Uraian</th>
              <th width="10%" class="text-center">Tgl Input</th>
              <th width="1%" class="text-center">Masuk</th>
              <th width="1%" class="text-center">Keluar</th>
              <th width="1%" class="text-center">Saldo</th>
              <th width="1%" class="text-center">OPSI</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $no   = 1;
              $awal = $tsaldo_awal ;
              $debet  = 0;
              $kredit  = 0;
              
            $data = mysqli_query($koneksi,"SELECT * FROM jor WHERE left(noperk,4)='$nosub' and unit='$unit' and tgljornal>='$tgl_d' and tgljornal<='$tgl_s' ORDER BY tgljornal ASC ");
            while($d = mysqli_fetch_array($data)){
                $notrans  =$d['notrans'] ;
                $sql3 = mysqli_query($koneksi, "SELECT uraiankas,datetime FROM kas WHERE notrans='$notrans' ");
                    list($uraiankas,$datetime) = mysqli_fetch_array($sql3);   
              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d['notrans']; ?></td>
                <td style="text-align:left; font-size: 9pt; font-weight:normal;"><?php echo date('d/m/Y', strtotime($d['tgljornal'])); ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $uraiankas; ?></td>
                <td style="text-align:left; font-size: 9pt; font-weight:normal;"><?php echo $datetime; ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasid]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasik]", 0, ",", "."); ?></td>
                <td align='right' style="font-weight: bold;">
                        <?php 
                        $debet=$d['mutasid'];
                        $kredit=$d['mutasik'];
                            // SALDO                      
                        if($kredit-$debet>0){
                          $awal = $awal - ($kredit-$debet);
                        }else if($debet-$kredit>0){
                          $awal = $awal + ($debet-$kredit);
                        } 
                        echo "" . number_format("$awal", 0, ",", ".")."";
                        ?>	
                                    <?php   
                                    // DEBIT  
                                    if($kredit-$debet>0){
                                        $deb = $deb +($kredit-$debet);
                                    }   
                                    ?>

                                    <?php   
                                    // KREDIT  
                                    if($debet-$kredit>0){
                                        $kre = $kre + ($debet-$kredit);
                                    }
                                    ?>
                </td>                           
                <td>    
                <a data-toggle='tooltip' data-placement='top' title='Buka Jornal NoTrans <?php echo $d['notrans'] ?>' class="btn btn-primary btn-sm" href="!!_kas_jor_view.php?id=<?php echo $d['notrans'] ?>"><i class="bi bi-layer-backward"></i></a>                                
                </td>
              </tr>
              <?php 
            }
            ?>
          </tbody>
        
        </table>
        
        
              </div>

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

</div>
<?php include 'footer.php'; ?>