
<?php 
    include 'header.php'; 

    $kdk  = $_GET['kdk'];
    $kab = $_GET['kab'];
    $kdc = $_GET['kdc'];   

    $tgl_ini=date('d-m-Y') ;
    $tgl_ini2=date('Y-m-d') ; 
    $kdc    =$_SESSION['kdc'];

            //////////////////////////////////////////////////////////////////////////// 
            $oleh   =$_SESSION['id_kar'];
            $xx     = $oleh ;
            $tabel_tujuan   = "nawal_".$xx; 
            $tabel_asal     = "nawal";
              
            $query_hapus = "DROP TABLE $tabel_tujuan ";
            mysqli_query($koneksi, $query_hapus);

            $querycc = "CREATE TABLE $tabel_tujuan LIKE $tabel_asal";
            mysqli_query($koneksi, $querycc);
            //////////////////////////////////////////////////////////////////////////// 
            
              $sqlx = mysqli_query($koneksi, "TRUNCATE TABLE $tabel_tujuan; ");
                list($no) = mysqli_fetch_array($sqlx);
                
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_asal ORDER BY id ASC ");
            while($a = mysqli_fetch_array($data)){
                $xnoperk=$a['noperk'];
                $sql1 = mysqli_query($koneksi, "SELECT sum(debet) AS tdeb,sum(kredit) AS tkred FROM jor WHERE noperk='$xnoperk' ");
                list($tdep,$tkred) = mysqli_fetch_array($sql1);
                
                if ($a['klp']<='20') { ;
                    $saldo=($a['awald']+$tdep)-$tkred ;
                    if ($saldo>=1) {
                        $akhir_d=$saldo ;
                        $akhir_k=0 ;
                    } else {
                        $akhir_d=$saldo ;
                        $akhir_k=0 ;               
                    }                
                } else {
                    $saldo=($a['awalk']-$tdep)+$tkred ;
                    if ($saldo>=1) {
                        $akhir_k=$saldo ;
                        $akhir_d=0 ;
                    } else {
                        $akhir_k=$saldo ;
                        $akhir_d=0 ;                  
                    }            
                }
                ///////////// NGISI $tabel_tujuan
            $query = mysqli_query($mysqli, "INSERT INTO $tabel_tujuan (perk,noperk,kdc,klp,nosub,ap,awald,awalk,mutasid,mutasik,akhird,akhirk) 
                    VALUES('$a[perk]','$xnoperk','$tkdc','$a[klp]','$a[nosub]','$a[ap]','$a[awald]','$a[awalk]','$tdep','$tkred','$akhir_d','$akhir_k' )")
                    or die('Ada kesalahan pada query insert : '.mysqli_error($mysqli));
            }  

?>

<div class="container">
     <div class="my-2 border-bottom">
       <h5 class="fw-semibold fs-3">Daftar Akunt Buku Besar NERACA</h5>
     </div>
  <div class="row justify-content-center">
    <div class="col-lg-10">
       <div class="card">
        <div class="card-header d-flex justify-content-between">
          <span class="card-title fw-semibold align-self-start mb">Jumlah terhitung s/d transaksi terakhir </span>
          <button type="button" class="btn btn-primary btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
           <a href="index.php" class="btn btn-primary btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a> 
           <a href="lap_akunt_all_print.php" target="_blank" class="btn btn-warning btn-sm"><i class="bi bi-printer"></i> &nbsp Print</a> 
          </button>
        </div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered table-striped" id="table-datatable">
              <thead>
            <tr>
                <th Style="text-align:center" colspan="3" bgcolor="#D3D3D3">Akunt Buku Besar</th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Saldo Awal</th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Mutasi</th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Saldo Akhir</th>
                <th Style="text-align:center" colspan="1" bgcolor="#DCDCDC">Opsi</th>
            </tr>                
            <tr>
              <th width="1%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Nomor</th>
              <th width="20%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Nama</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Debet</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Kredit</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Debet</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Kredit</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Debet</th>
              <th width="10%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Kredit</th>
              <th width="1%" class="text-center" style="text-align:center; font-size: 10pt; font-weight:normal;">Opsi</th>
            </tr>
          </thead>
          <tbody>
            <?php 
            $no=1;
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan WHERE klp<'40'  ORDER BY noperk ASC");
            while($t = mysqli_fetch_array($data)){
                

              ?>
                          <tr>
                            <td><?php echo $no++; ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $t['noperk'] ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $t['perk']; ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[awald]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[awalk]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[mutasid]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[mutasik]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[akhird]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$t[akhirk]", 0, ",", "."); ?></td>
                            <td>    
                                <a data-toggle='tooltip' data-placement='top' title='Buka Data Trans <?php echo $t['perk'] ?>' class="btn btn-primary btn-sm" href="!!_view_trans_per_akunt.php?id=<?php echo $t['noperk'] ?>"><i class="bi bi-eye"></i></a>
                            </td>           
                          </tr>    
                        </div>
                      </div>
                    </div>
              <?php } ?>
          </tbody>
                        <?php 
                $sql2 = mysqli_query($koneksi, "SELECT sum(awald) AS tawald,sum(awalk) AS tawalk,sum(mutasid) AS tmutd,sum(mutasik) AS tmutk,sum(akhird) AS takhird,sum(akhirk) AS takhirk FROM $tabel_tujuan WHERE klp<'40' ");
                list($awald,$awalk,$tmutd,$tmutk,$akhird,$akhirk) = mysqli_fetch_array($sql2);
                
                $laba_rugi=($tmutd-$tmutk);
                $nol=0 ;
                $total_balance=($akhirk+$laba_rugi);
            	        ?>						
							<tr>
						    	<td colspan="3" style="font-weight: bold;text-align: center;">Total </td>																																									
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$awald", 0, ",", "."); ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$awalk", 0, ",", "."); ?>
								</td>								

								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tmutd", 0, ",", "."); ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$tmutk", 0, ",", "."); ?>
								</td>	
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$akhird", 0, ",", "."); ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$akhirk", 0, ",", "."); ?>
								</td>		
							</tr>             
				
							<tr>
						    	<td colspan="7" style="font-weight: bold;text-align: center;">LABA (RUGI) </td>																																									
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$nol", 0, ",", "."); ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$laba_rugi", 0, ",", "."); ?>
								</td>		
								<td>
								    <a data-toggle='tooltip' data-placement='top' title='Buka Akunt LABA (RUGI)' class="btn btn-danger btn-sm" href="lap_akunt_all_r_l.php?"><i class="bi bi-emoji-sunglasses-fill"></i></a>
								</td>								
							</tr>         
							<tr>
						    	<td colspan="7" style="font-weight: bold;text-align: center;">TOTAL BALANCE </td>																																									
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$akhird", 0, ",", "."); ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <?php echo "" . number_format("$total_balance", 0, ",", "."); ?>
								</td>		
							</tr>							
        </table>
       </div> 
    </div>    
  </div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>