<?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;
        }
    </style>
   
</head>

<body>
    <page_footer align="right">
    Page[[page_cu]]/ 
    [[page_nb]]
  </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['kolek']['periode2'];
$sqlkl=$_SESSION['kolek']['kode']['sqlkl'];
?>
<p align="center" style="margin:0px;font-family : Arial; ">PT. MENARA SEMBILAN SEJAHTERA</p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;"> KANTOR <?php echo $_SESSION['kantor'];?> </p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;">LAPORAN KOLEKTIBILITAS </p>
<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;"><?php echo $periode; ?> </p>      

<table border="1" style="width:100%;padding-top: 19px;padding-bottom: 16px">
<tr>
<td style="text-align: center; background: #ddd;width: 3%"><b>No.</b></td>
<td style="text-align: center; background: #ddd; width: 5%"><b>Tgl</b></td> 
<td style="text-align: center; background: #ddd;width: 9%"><b>Rekening</b></td>
<td style="text-align: center; background: #ddd;width: 11%"><b>Nama</b></td>
<td style="text-align: center; background: #ddd;width: 9%"><b>Plafond</b></td>
<td style="text-align: center; background: #ddd;width: 9%"><b>Bakidebet</b></td>
<td style="text-align: center; background: #ddd;width: 9%"><b>Barang</b></td>
<!-- <td style="text-align: center; background: #ddd;width: 9%"><b>Jaminan</b></td> -->

<td style="text-align: center; background: #ddd;width: 7%"><b>Tg.Pokok</b></td>
<td style="text-align: center; background: #ddd;width: 7%"><b>Tg.Margin</b></td>
<td style="text-align: center; background: #ddd;width: 7%"><b>Jumlah</b></td>
<td style="text-align: center; background: #ddd;width: 3%"><b>Kol</b></td>
</tr>

<?php
// Load file koneksi.php
include "./../koneksi.php";
 error_reporting(0); //hide php errors
 $no=0;
function kol($kolbln){
                    if($kolbln<="15"){
                     return 0;
                    }elseif($kolbln<="30"){
                    return 1;
                    }elseif($kolbln<="45"){
                        return 2;
                    }else{return 3;
                    } 
                    }
 
$sql = mysqli_query($koneksi, $sqlkl); // Eksekusi/Jalankan query dari variabel $query
$row = mysqli_num_rows($sql); // Ambil jumlah data dari hasil eksekusi $sql
if($row > 0){ // Jika jumlah data lebih dari 0 (Berarti jika data ada)
    while($data = mysqli_fetch_array($sql)){ // Ambil semua data dari hasil eksekusi $sql
     extract($data);
     $jumlah_pinjaman = $plafond;
      $lama_pinjaman   = $jangkawaktu;
      $margin_perbulan  = $margin;
      $angsuran_margin=$jumlah_pinjaman*$margin_perbulan/100;
      $angsuran_pokok = $jumlah_pinjaman/$lama_pinjaman;
      $jumlahangsuran = $angsuran_pokok+$angsuran_margin;
      $tpokok2=0;
      $tmargin2=0;
      $tot2=0;
      $tpokok2+=$angsuran_pokok;
      $tmargin2+=$angsuran_margin;
      $tot2+=$jumlahangsuran;
      $date2 = $periode;
    
      $timeStart2 = new DateTime($tgl);
      $timeEnd2 = new DateTime($date2);
      $hitung = $timeStart2->diff($timeEnd2);
      $hasil= $hitung->format('%a');
      /*https://www.belajarwebpedia.com/2019/11/cara-menghitung-selisih-tanggal-dengan-php.html#:~:text=%3C%3Fphp%20%24tanggal1%20%3D%20new%20DateTime%20%28%272019-11-11%27%29%3B%20%24tanggal2%20%3D,berapa%20hari%20dari%20kedua%20tanggal%20tersebut.%20%2A%2F%20%3F%3E
      https://rachmat.id/articles/cara-menghitung-selisih-hari-dengan-php
      */       
       
       $flat_t_margin=$angsuran_margin*kol($hasil)-$tmargin;
       $flat_t_pokok=$angsuran_pokok*kol($hasil)-$tpokok;
       $tot_tunggakan= $flat_t_margin+$flat_t_pokok; 

     $gab_plafond += $plafond;
     $gab_bakidebet += $bakidebet;
     $gab_margin +=$angsuran_margin*kol($hasil)-$tmargin;
     $gab_pokok += $angsuran_pokok*kol($hasil)-$tpokok;
     $gab_total += $flat_t_margin+$flat_t_pokok;
 
       $no++;     
      ?>   

      <tr>                      
        <td class ="pertama" style="text-align: center;"> <?php echo $no ?> </td>
        <td class ="pertama" style="text-align: center;"> <?php echo date('d-m-Y',strtotime($tgl))?> </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 substr($alamat,0,20)?> </td>  -->                        
        <td class ="pertama" align="right"> <?php echo number_format($plafond); ?> </td>
        <td class ="pertama" align="right"> <?php echo number_format($bakidebet,2); ?> </td>
        <td class ="pertama" style="text-align: left;"> <?php echo substr($barang,0,33)?> </td>
        <!-- <td class ="pertama" style="text-align: left;"> <?php echo substr($jaminan,0,20);?> </td> -->
        <td class ="pertama" align="right"> <?php echo number_format($flat_t_pokok);?> </td>
        <td class ="pertama" align="right"> <?php echo number_format($flat_t_margin);?> </td>  
        <td class ="pertama" align="right"> <?php echo number_format($tot_tunggakan);?> </td>  
        <td class ="pertama" align="center"><?php echo $kol;?></td>  
                        
    </tr>

    
      <?php    
    }
    }?>
     <tr>
          <b><td class="warna" colspan="4" style="text-align: center;">Jumlah</td></b>

          <b><td class="warna" style="text-align: center;"><?php echo number_format($gab_plafond,0)?></td></b>
          <b><td class="warna" align="right" width="7%"><?php echo number_format($gab_bakidebet,0)?></td></b>  
          <b><td></td></b>
                      
          <b><td class="warna" align="right" width="7%"><?php echo number_format($gab_pokok,0);?></td></b>
          <b><td class="warna" align="right" width="7%"><?php echo number_format($gab_margin,0);?></td></b>
          <b><td class="warna" align="right" width="7%"><?php  echo number_format($gab_total,0);?></td></b> 
          <b><td></td></b>
          </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 Kolektibilitas.pdf', 'D');
?>


<!-- https://www.mynotescode.com/cara-mudah-membuat-laporan-pdf-dengan-php-dan-mysql/ -->