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

  <div class="my-4 border-bottom">
    <h4 class="fw-semibold fs-4">Rekap Kas Per Jenis
    </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">

          </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['tgl_dr']) && isset($_GET['tgl_sd']) && isset($_GET['tkdj'])) {
              $tgl_d = $_GET['tgl_dr'];
              $tgl_s = $_GET['tgl_sd'];
              $kdj   = $_GET['id'];
            //}
            
                $sql1 = mysqli_query($koneksi, "SELECT sum(awald) as tn_awal,perk FROM nawal WHERE noperk='$kdj' ");
                    list($tn_awal,$perk) = mysqli_fetch_array($sql1);
                    
                $sql2 = mysqli_query($koneksi, "SELECT sum(debet-kredit) as tj_awal FROM jor WHERE noperk='$kdj' and tanggal<='$tgl_d' ");
                    list($tj_awal) = mysqli_fetch_array($sql2);                    
                $tsaldo_awal=$tn_awal+$tj_awal;
                
              ?>

              <div class="row">
                <div class="col-lg-6">
                  <table class="table table-bordered">
                    <tr>
                      <th width="30%">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>Jenis</th>
                      <th>:</th>
                      <td><?php echo $kdj; ?> - <?php echo $perk; ?></td>
                    </tr>      
                    <tr>
                      <th>Saldo Awal</th>
                      <th>:</th>
                      <td><?php echo "" . number_format("$tsaldo_awal", 0, ",", "."); ?></td>
                    </tr>                          
                    <tr>
                      <th>Lanjutan </th>
                      <th>:</th>
                      <td>
                          <a href="lap_a_kas_tgl_sd_tgl_per_kas_rekap_pdf.php?xid=<?php echo $kdj ?>&xtgl_d=<?php echo date('Y-m-d', strtotime($tgl_d)); ?>&xtgl_s=<?php echo date('Y-m-d', strtotime($tgl_s)); ?>" target="_blank" class="btn btn-warning btn-sm"><i class="bi bi-printer-fill"></i> &nbsp CETAK PDF</a>
                          <a href="javascript:history.back()" class="btn btn-info btn-sm pull-right"><i class="bi bi-reply"></i> &nbsp; Kembali ke sebelume</a>
                       </td>
                    </tr>                    
                  </table>
                </div>
              </div>
              <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: 10pt; font-weight:normal;">No</th>
              <th width="4%" class="text-center">NoPerk</th>
              <th width="26%" class="text-center">Perk</th>
              <th width="1%" class="text-center">x</th>
              <th width="10%" class="text-center">Penerimaan</th>
              <th width="10%" class="text-center">Pengeluaran</th>
              <th width="10%" 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 noperk='$kdj' and tanggal>='$tgl_d' and tanggal<='$tgl_s' GROUP BY akunt ");
            while($x = mysqli_fetch_array($data)){
                $akunt  =$x['akunt'] ;
                $sql3 = mysqli_query($koneksi, "SELECT noperk,perk FROM nawal WHERE noperk='$akunt' ");
                    list($noperk,$perk) = mysqli_fetch_array($sql3);  
                    
                $jor2 = mysqli_query($koneksi,"select count(noperk) AS jtakun,sum(debet) AS jdebet,sum(kredit) AS jkredit FROM jor WHERE noperk='$kdj' and akunt='$akunt' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                    $j2 = mysqli_fetch_assoc($jor2);                     
              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td style="text-align:center; font-size: 14pt; font-weight:normal;"><?php echo $noperk; ?></td>
                <td style="text-align:left; font-size: 14pt; font-weight:normal;"><?php echo $perk; ?></td>
                <td style="text-align:left; font-size: 14pt; font-weight:normal;"><?php echo "" . number_format("$j2[jtakun]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 14pt; font-weight:normal;"><?php echo "" . number_format("$j2[jdebet]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 14pt; font-weight:normal;"><?php echo "" . number_format("$j2[jkredit]", 0, ",", "."); ?></td>
                <td align='right' style="font-weight: bold;">
                        <?php 
                        $debet=$j2['jdebet'] ;
                        $kredit=$j2['jkredit'] ;
                            // 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='Lihat rincian NoPerk <?php echo $noperk ?>' class="btn btn-primary btn-sm" href="lap_a_kas_tgl_sd_tgl_per_kas_rekap_view.php?id=<?php echo $noperk ?>&tgl_dr=<?php echo date('Y-m-d', strtotime($tgl_d)); ?>&tgl_sd=<?php echo date('Y-m-d', strtotime($tgl_s)); ?>"><i class="bi bi-eye"></i></a>                                
                </td>
              </tr>
              <?php 
            }
            ?>
          </tbody>
        </table>
        
              </div>
          </div>
        </div>
      </section>
    </div>
  </section>

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