

<?php 
    session_start();
    include 'header.php'; 
    $ket=$_SESSION['keter'];
?>

<div class="container">

  <div class="my-2 border-bottom">
    <h5 class="fw-semibold fs-5">Laporan Posisi Keuangan </h5>
  </div>

  <section class="content">
    <div class="row">
      <section class="col-lg-12">
         
        <div class="box box-info">
          <div class="box-header">
            <h6 class="box-title"></h6>
          </div>
          <div class="box-body">
            <?php 
            //////////////////////////////////////////////////////////////////////////// 
            $ket    =$_SESSION['keter'];
            $oleh   =$_SESSION['id_kar'];
            $xx     = $oleh ;
            $tabel_tujuan   = "nawal_".$xx; 
            $tabel_asal     = "nawal";
            ////////////////////////////////////////////////////////////////////////////     
                $sql0 = mysqli_query($koneksi, "SELECT tgl_htg,tgl_sd FROM $tabel_tujuan WHERE mutasid>=1 limit 1 ");
                list($tgl_htg,$tgl_sd) = mysqli_fetch_array($sql0);            
              $tgl_d    = $tgl_htg;
              $tgl_s    = $tgl_sd;
              
              $tgl_ke_1 =date('2025-01-01');        
              $tgl_p    =strtotime("$tgl_d");
              $tgl_lalu =date('Y-m-d', strtotime("-1 day", $tgl_p));
                if ($ket=='tgl_s/d_tgl') { ;
                    $jelase='Dari Tanggal s/d Tanggal';
                } else {
                    $jelase='Khusus Bulan';
                }
                $nol=0;
                $sql9 = mysqli_query($koneksi, "SELECT sum(awalk-awald) AS tshu_a_01,sum(mutasik-mutasid) AS tshu_mut_01,sum(akhirk-akhird) AS tshu_akhir_01 FROM $tabel_tujuan WHERE klp>'40' and kdu='01' ");
                list($tshu_a_01,$tshu_mut_01,$tshu_akhir_01) = mysqli_fetch_array($sql9);             
                ///////////// 
     
            //////////////////////////////////////////////////////////////////////////// 
            $oleh   = $_SESSION['id_kar'];
            $tabel_tujuan_padat   = "nawal_padat_".$oleh; 
            
            $query_hapus = "DROP TABLE $tabel_tujuan_padat ";
            mysqli_query($koneksi, $query_hapus);

            $querycc = "CREATE TABLE $tabel_tujuan_padat LIKE $tabel_tujuan";
            mysqli_query($koneksi, $querycc);
            ////////////////////////////////////////////////////////////////////////////   
              $sqlx = mysqli_query($koneksi, "TRUNCATE TABLE $tabel_tujuan_padat; ");
                list($no) = mysqli_fetch_array($sqlx);
                
//            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan GROUP BY NOSUB ");
//            while($a = mysqli_fetch_array($data)){
                ///////////// NGISI NAWAL_ISI
//                    $query = mysqli_query($mysqli, "INSERT INTO $tabel_tujuan_padat (perk,noperk,kdc,klp,nosub,ap,awald,awalk,akhird,akhirk,sub,kt,kelompok,kdu) 
//                                    VALUES('$a[nosub]','$a[sub]','$tkdc','$a[klp]','$a[nosub]','X','$a[awald]','$a[awalk]','$a[awald]','$a[awalk]','$a[sub]','$a[kt]','$a[kelompok]','$a[kdu]' )")
//                                    or die('Ada kesalahan pada query insert : '.mysqli_error($mysqli));
//            }            
                    mysqli_query($koneksi, "INSERT INTO $tabel_tujuan_padat (perk,noperk,kdc,klp,ap,awald,awalk,akhird,akhirk,kt,kelompok,kdu,tgl_htg,tgl_sd)
                        SELECT sub,nosub,kdc,klp,ap,SUM(awald),SUM(awalk),SUM(akhird),SUM(akhirk),kt,kelompok,kdu,tgl_htg,tgl_sd
                        FROM $tabel_tujuan
                        GROUP BY nosub");


              ?>
              <div class="row">
                <div class="col-lg-10">
                  <table class="table table-bordered">
                    <tr>
                      <th width="30%">MUTASI TERAKHIR DIHITUNG </th>
                      <th width="1%">:</th>
                      <td><?php echo $jelase; ?></td>
                    </tr>
                    <tr>
                      <th width="20%">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>Cetak </th>
                      <th>:</th>
                      <td>
                      <a href="lap_akunt_nrc_tgl_tgl_tenan_padat_print.php?toleh=<?php echo $oleh ?>" target="_blank" class="btn btn-warning btn-sm"><i class="bi bi-print"></i> &nbsp Print</a>
<!--                      <a href="lap_akunt_nrc_tgl_tgl_tenan_pdf.php?toleh=<?php echo $oleh ?>" target="_blank" class="btn btn-info btn-sm"><i class="bi bi-file-earmark-pdf"></i> &nbsp Print PDF</a>
                      <a href="lap_akunt_nrc_tgl_tgl_tenan_excell.php?xtgl_d=<?php echo $tgl_d ?>&xtgl_s=<?php echo $tgl_s ?>" class="btn btn-success btn-sm"><i class="bi bi-file-word"></i> &nbsp Excell</a>
                      <a href="lap_akunt_nrc_tgl_tgl_tenan_padat.php?toleh=<?php echo $oleh ?>" class="btn btn-secondary btn-sm"><i class="bi bi-award"></i> &nbsp Padatkan </a>
-->
                      <a href="javascript:history.go(-1)" class="btn btn-primary btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a> 
                      </td>
                    </tr>   
                  </table>
                </div>
              </div>

              <div class="table-responsive">
                <table class="table table-bordered table-striped" id="table-datatable1">
                  <thead>
              <thead>
            <tr>
                <th Style="text-align:center" colspan="3" bgcolor="#D3D3D3">Akunt Buku Besar</th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Per <?php echo date('d-m-Y', strtotime($tgl_s)); ?></th>
                <th Style="text-align:center" colspan="2" bgcolor="#DCDCDC">Per <?php echo date('d-m-Y', strtotime($tgl_lalu)); ?></th>
            </tr>                 
            <tr>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nomor</th>
              <th width="20%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nama</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Debet</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Kredit</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Debet</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Kredit</th>
            </tr>
                  </thead>
          <tbody>
            <?php
                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_kode_1 WHERE kd1='1' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);
                $batas_klp='20';
            ?>
                <tr>
                    <td colspan="3" style="font-weight: bold;text-align: center;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo $nama ?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                </tr>
            <?php 
            $no=1;
            $nol=0;
            //and (awald<>0 and awalk<>0 and mutasid<>0 and mutasik<>0)
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan_padat WHERE klp<'$batas_klp' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($w = mysqli_fetch_array($data)){
                $sqly = mysqli_query($koneksi, "SELECT nama AS xnama FROM n_kode_3 WHERE kd='$w[noperk]' ");
                list($xnama) = mysqli_fetch_array($sqly);                   
              ?>
                          <tr>
                            <td><?php echo $no++; ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $w['noperk'] ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $xnama; ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhird]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhirk]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awald]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awalk]", 0, ",", "."); ?></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_padat WHERE kt<>'X' and klp<'$batas_klp' ");
                list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);

                $laba_rugi_a=($awald-$awalk);
                $laba_rugi_b=($tmutd-$tmutk);
                $laba_rugi_c=($takhird-$takhirk);
                $nol=0 ;
                $total_balance_a=($awalk+$laba_rugi_a);
                $total_balance_b=($tmutk+$laba_rugi_b);
                $total_balance_c=($takhirk+$laba_rugi_c);
            	        ?>						
							<tr>
						    	<td colspan="3" style="font-weight: bold;text-align: center; background-color: #B0C4DE;">Total <?php echo $nama ?> </td>																																									
								<td style="font-weight: bold;text-align: right; background-color: #B0C4DE;"><?php echo "" . number_format("$takhird", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right; background-color: #B0C4DE;"><?php echo "" . number_format("$takhirk", 0, ",", "."); ?></td>
								<td style="font-weight: bold;text-align: right; background-color: #B0C4DE;"><?php echo "" . number_format("$awald", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right; background-color: #B0C4DE;"><?php echo "" . number_format("$awalk", 0, ",", "."); ?></td>								
							</tr>             
 

          <tbody>
            <?php
                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_kode_1 WHERE kd1='2' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);
                $batas_klp_b='30';
            ?>
                <tr>
                    <td colspan="3" style="font-weight: bold;text-align: center;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo $nama ?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                </tr>
            <?php 
            $no=1;
            $nol=0;
            //and (awald<>0 and awalk<>0 and mutasid<>0 and mutasik<>0)
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan_padat WHERE klp>'$batas_klp' and klp<'$batas_klp_b' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($w = mysqli_fetch_array($data)){
                $sqly = mysqli_query($koneksi, "SELECT nama AS xnama FROM n_kode_3 WHERE kd='$w[noperk]' ");
                list($xnama) = mysqli_fetch_array($sqly);                   
              ?>
                          <tr>
                            <td><?php echo $no++; ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $w['noperk'] ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $xnama; ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhird]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhirk]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awald]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awalk]", 0, ",", "."); ?></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_padat WHERE kt<>'X' and klp>'$batas_klp' and klp<'$batas_klp_b' ");
                list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);

                $laba_rugi_a=($awald-$awalk);
                $laba_rugi_b=($tmutd-$tmutk);
                $laba_rugi_c=($takhird-$takhirk);
                $nol=0 ;
                $total_balance_a=($awalk+$laba_rugi_a);
                $total_balance_b=($tmutk+$laba_rugi_b);
                $total_balance_c=($takhirk+$laba_rugi_c);
            	        ?>						
							<tr>
						    	<td colspan="3" style="font-weight: bold;text-align: center;">Total <?php echo $nama ?> </td>																																									
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$takhird", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$takhirk", 0, ",", "."); ?></td>
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$awald", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$awalk", 0, ",", "."); ?></td>								
							</tr>          
		
          <tbody>
            <?php
                $sqlx = mysqli_query($koneksi, "SELECT nama FROM n_kode_1 WHERE kd1='3' limit 1 ");
                list($nama) = mysqli_fetch_array($sqlx);
                $batas_klp_c='40';
            ?>
                <tr>
                    <td colspan="3" style="font-weight: bold;text-align: center;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo $nama ?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo ""?></td>
                </tr>
            <?php 
            $no=1;
            $nol=0;
            //and (awald<>0 and awalk<>0 and mutasid<>0 and mutasik<>0)
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan_padat WHERE klp>'$batas_klp_b' and klp<'$batas_klp_c' and kt<>'X' and (awald+awalk+mutasid+mutasik)<>0 order by noperk ASC ");
            while($w = mysqli_fetch_array($data)){
                $sqly = mysqli_query($koneksi, "SELECT nama AS xnama FROM n_kode_3 WHERE kd='$w[noperk]' ");
                list($xnama) = mysqli_fetch_array($sqly);                   
              ?>
                          <tr>
                            <td><?php echo $no++; ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $w['noperk'] ?></td>
                            <td style="text-align:left; font-size: 11pt; font-weight:normal;"><?php echo $xnama; ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhird]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[akhirk]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awald]", 0, ",", "."); ?></td>
                            <td style="text-align:right; font-size: 11pt; font-weight:normal;"><?php echo "" . number_format("$w[awalk]", 0, ",", "."); ?></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_padat WHERE kt<>'X' and klp>'$batas_klp_b' and klp<'$batas_klp_c' ");
                list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);

                $laba_rugi_a=($awald-$awalk);
                $laba_rugi_b=($tmutd-$tmutk);
                $laba_rugi_c=($takhird-$takhirk);
                $nol=0 ;
                $total_balance_a=($awalk+$laba_rugi_a);
                $total_balance_b=($tmutk+$laba_rugi_b);
                $total_balance_c=($takhirk+$laba_rugi_c);
            	        ?>						
							<tr>
						    	<td colspan="3" style="font-weight: bold;text-align: center;">Total <?php echo $nama ?> </td>																																									
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$takhird", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$takhirk", 0, ",", "."); ?></td>
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$awald", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;"><?php echo "" . number_format("$awalk", 0, ",", "."); ?></td>								
							</tr>          

 
                        <?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_padat WHERE kt<>'X' and klp<'40' ");
                list($awald,$awalk,$tmutd,$tmutk,$takhird,$takhirk) = mysqli_fetch_array($sql2);

                $laba_rugi_a=($awald-$awalk);
                $laba_rugi_b=($tmutd-$tmutk);
                $laba_rugi_c=($takhird-$takhirk);
                $nol=0 ;
                $total_balance_a=($awalk+$laba_rugi_a);
                $total_balance_b=($tmutk+$laba_rugi_b);
                $total_balance_c=($takhirk+$laba_rugi_c);
            	        ?>						
							<tr>
						    	<td colspan="3" style="font-weight: bold;text-align: center;background-color: #B0C4DE;">Total LIABILITAS & EQUITAS</td>																																									
								<td style="font-weight: bold;text-align: right;background-color: #B0C4DE;"><?php echo "" . number_format("$nol", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;background-color: #B0C4DE;"><?php echo "" . number_format("$takhirk", 0, ",", "."); ?></td>
								<td style="font-weight: bold;text-align: right;background-color: #B0C4DE;"><?php echo "" . number_format("$nol", 0, ",", "."); ?></td>    
								<td style="font-weight: bold;text-align: right;background-color: #B0C4DE;"><?php echo "" . number_format("$awalk", 0, ",", "."); ?></td>								
							</tr> 
        </table>
              </div>

              <?php 

            ?>

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

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