<?php 
session_start();
ob_start();

 ?>
<html>
<head>
  <title>Cetak PDF</title>
    
   <style>
   table {border-collapse:collapse; table-layout:fixed;width: 630px; }
  /* table td {word-wrap:break-word;width: 20%;}*/
    table td {margin: 0 20px 20px 0;}
</style>
<style>
   /*https://www.dicoding.com/blog/cara-membuat-table-di-html/*/
        table {
            border-collapse: collapse;
        }    
        th, td {
            padding: 4px;
            font-size: 12px;
        }
        p{ margin-top: 0px;            
        }
        h5{ margin-top: 0px;
        }
        h4{ margin-bottom: 1px;
        }
        .subtotal {
            background-color: #f0f0f0;
            font-weight: bold;
        }
        .grandtotal {
            background-color: #d9edf7;
            font-weight: bold;
        }
    </style>
   
</head>

<body>
    <page_footer align="right">
    Page[[page_cu]]
  </page_footer> 
  <page_header>
  
  </page_header>

 <!-- <table border="0">
  <tr>
   
<th ><img src="./../gambar/logommu.png" style="width:745px;height:100px"align="center" /></th>

</tr>

   </table>  --> 
      
<?php     
$periode=$_SESSION['tabungan']['periode'];
$sqlpj=$_SESSION['pecahanUang_tabungan']['kode']['sqlp'];
?>


<p align="center" style="margin:0px;font-family : Arial; ">MMU A-39 REJOSARI</p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;"> Ranting Pondok Pesantren Sidogiri </p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;">LAPORAN MUTASI TABUNGAN </p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;"><?php echo $periode; ?> </p>      

<table border="1" width="100%" style="padding-top: 19px;padding-bottom: 16px">
<tr>
<td style="text-align: center; background: #ddd;width: 4%"><b>No.</b></td>

<td style="text-align: center; background: #ddd;width: 13%"><b>Rekening</b></td>
<td style="text-align: center; background: #ddd;width: 35%"><b>Nama</b></td>
<td style="text-align: center; background: #ddd;width: 4%"><b>Kls</b></td>
<td style="text-align: center; background: #ddd;width: 8%"><b>Saldo</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>100.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>50.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>20.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>10.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>5.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>2.000</b></td>
<td style="text-align: center; background: #ddd;width: 5%"><b>1.000</b></td>


</tr>

<?php
// Load file koneksi.php
include "./../koneksi.php";
/*include ('../koneksi.php');*/
error_reporting(0); //hide php errors
/*error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);*/
$no=0;

// Modifikasi SQL query untuk menambahkan ORDER BY kls
// Pastikan string SQL berisi ORDER BY untuk mengelompokkan berdasarkan kelas
if(strpos($sqlpj, 'ORDER BY') === false) {
    // Jika belum ada ORDER BY, tambahkan di akhir query
    $sqlpj .= " ORDER BY kls, tgl";
}

$query = mysqli_query($koneksi, $sqlpj); // Eksekusi/Jalankan query 
$row = mysqli_num_rows($query); // Ambil jumlah data dari hasil eksekusi $sql

if($row > 0){ // Jika jumlah data lebih dari 0 (Berarti jika data ada)
    $current_kls = '';
    $kls_subtotal_kredit = 0;
    $t_100 = 0;
    $t_50 = 0;
    $t_20 = 0;
    $t_10 = 0;
    $t_5 = 0;
    $t_2 = 0;
    $t_1 = 0;
    
    while($datatb = mysqli_fetch_array($query)){ 
        $kls = $datatb['kls'];
        $rekening = $datatb['rekening']; 
        $nama = $datatb['nama'];
        $saldotab = $datatb['saldotab'];
        $dataa = $saldotab % 100000;
        $a100 = ($saldotab - $dataa) / 100000;

        $datab = $dataa % 50000;
        $a50 = ($dataa - $datab) / 50000;

        $datac = $datab % 20000;
        $a20 = ($datab - $datac) / 20000;

        $datad = $datac % 10000;
        $a10 = ($datac - $datad) / 10000;

        $datae = $datad % 5000;
        $a5 = ($datad- $datae) / 5000;

        $dataf = $datae % 2000;
        $a2 = ($datae - $dataf) / 2000;
        
        $datag = $dataf % 1000;
        $a1 = ($dataf - $datag) / 1000;

        
        // Jika kelas berubah, tampilkan subtotal untuk kelas sebelumnya
        if($current_kls != '' && $current_kls != $kls) {
            ?>
            <tr class="subtotal">
                <td colspan="4" style="text-align: center;">Subtotal Kelas <?php echo $current_kls; ?></td>
              <td align="right"><?php echo number_format($kls_subtotal_kredit, 0); ?></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit1, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit2, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit3, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit4, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit5, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit6, 0); ?></b></td>
                <td align="right"><b><?php echo number_format($kls_subtotal_kredit7, 0); ?></b></td>
              
            </tr>
            <?php
            // Reset subtotal untuk kelas baru
            $kls_subtotal_kredit = 0;
            $kls_subtotal_kredit1 = 0;
            $kls_subtotal_kredit2 = 0;
            $kls_subtotal_kredit3 = 0;
            $kls_subtotal_kredit4 = 0;
            $kls_subtotal_kredit5 = 0;
            $kls_subtotal_kredit6 = 0;
            $kls_subtotal_kredit7 = 0;
        }
        
        // Update current class
        $current_kls = $kls;
        
        // Menambahkan ke subtotal
        
                // Menambahkan ke subtotal
                $kls_subtotal_kredit += $saldotab;
                $kls_subtotal_kredit1 += $a100;
                $kls_subtotal_kredit2 += $a50;
                $kls_subtotal_kredit3 += $a20;
                $kls_subtotal_kredit4 += $a10;
                $kls_subtotal_kredit5 += $a5;
                $kls_subtotal_kredit6 += $a2;
                $kls_subtotal_kredit7 += $a1;

                
                // Menambahkan ke total keseluruhan
                $t_saldotab += $saldotab;
                $t_saldotab1 += $a100;
                $t_saldotab2 += $a50;
                $t_saldotab3 += $a20;
                $t_saldotab4 += $a10;
                $t_saldotab5 += $a5;
                $t_saldotab6 += $a2;
                $t_saldotab7 += $a1;

        
        $no++;     
    ?>   

    <tr>                      
        <td class="pertama" style="text-align: center;"> <?php echo $no ?> </td>
        
        <td class="pertama" style="text-align: left;"> <?php echo $rekening?> </td>
        <td class="pertama" style="text-align: left;"> <?php echo substr($nama,0,20); ?> </td>
        <td class="pertama" style="text-align: left;"> <?php echo $kls?> </td>       
        <td class="pertama" align="right"> <?php echo number_format($saldotab, 0);?> </td>
         <td class="pertama" align="right"> <?php echo number_format($a100, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a50, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a200, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a10, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a5, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a2, 0);?> </td>
        <td class="pertama" align="right"> <?php echo number_format($a1, 0);?> </td>

        
    </tr>
    <?php    
    }
    
    // Tampilkan subtotal untuk kelas terakhir
    if($current_kls != '') {
    ?>
        <tr class="subtotal">
        <td colspan="4" style="text-align: center;">Subtotal Kelas <?php echo $current_kls; ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit1, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit2, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit3, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit4, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit5, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit6, 0); ?></td>
        <td align="right"><?php echo number_format($kls_subtotal_kredit7, 0); ?></td>            
         
        </tr>
    <?php
    }
}
?>
<!-- Grand Total -->
<tr class="grandtotal">
    <td colspan="4" style="text-align: center;"><b>TOTAL KESELURUHAN</b></td>
    <td align="right"><b><?php echo number_format($t_saldotab, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab1, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab2, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab3, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab4, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab5, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab6, 0);?></b></td>
    <td align="right"><b><?php echo number_format($t_saldotab7, 0);?></b></td>

    
   
</tr>
</table>
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
        
require '../html2pdf/autoload.php';
$pdf = new Spipu\Html2Pdf\Html2Pdf('L','A4','en');
$pdf->WriteHTML($html);
$pdf->Output('Laporan mutasi tabungan.pdf', 'D');
?>