<?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['tuggakan']['periode2'];
$sqlt=$_SESSION['tuggakan']['kode']['sqlt'];
?>
<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 TUNGGAKAN </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: 14%"><b>Nama</b></td>
<td style="text-align: center; background: #ddd;width: 17%"><b>Alamat</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: 3%"><b>Ujroh</b></td>
<td style="text-align: center; background: #ddd;width: 3%"><b>LM</b></td>
<td style="text-align: center; background: #ddd;width: 7%"><b>Tung.Pokok</b></td>
<td style="text-align: center; background: #ddd;width: 7%"><b>Tung.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";
 /*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;
 
$sql = mysqli_query($koneksi, $sqlt); // 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;
    $tgl  = $tgl;
    $tpokok2=0;
    $tmargin2=0;
    $tot2=0;
    
      $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;
      $tbakidebet2+=$bakidebet;
      $tplafond2+=$tplafond;
      $date2 = date('Y-m-d',strtotime($periode));
    /*$timeStart = strtotime($tgl);*/
    $timeStart2 = strtotime("$tgl");
    $timeEnd2 = strtotime("$date2");
    // Menambah bulan ini + semua bulan pada tahun sebelumnya
    $jmlBulan2 = 1 + (date("Y",$timeEnd2)-date("Y",$timeStart2))*12;
    // menghitung selisih bulan
    $jmlBulan2 += date("m",$timeEnd2)-date("m",$timeStart2); 
     /*Flat*/
     $flat_t_margin=$angsuran_margin*$jmlBulan2-$tmargin;
     $flat_t_pokok=$angsuran_pokok*$jmlBulan2-$tpokok;
     $tot_tunggakan= $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 $nama?> </td>
        <td class ="pertama" style="text-align: left;"> <?php echo $alamat?> </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 $margin?> </td>
        <td class ="pertama" style="text-align: left;"> <?php echo $jangkawaktu?> </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="right"> <?php if($tot_tunggakan>0){
    if($jmlBulan2<=1) echo "0"; elseif($jmlBulan2<=2)echo "1";else echo "2";}else{echo "0";};?> </td>  
    </tr>
      <?php    
    }
    }?>
     <tr>
          <b><td class="warna" colspan="5" style="text-align: center;">Jumlah</td></b>
          <b><td class="warna" style="text-align: center;"><?php echo number_format($tplafond2)?></td></b>
          <b><td class="warna" align="right" width="7%"><?php echo number_format($tbakidebet2,2)?></td></b>               
      <b><td></td></b>
      <b><td></td></b>
          <b><td class="warna" align="right" width="7%"><?php echo number_format($tpokok2,2);?></td></b>
          <b><td class="warna" align="right" width="7%"><?php echo number_format($tmargin2,2);?></td></b>
          <b><td class="warna" align="right" width="7%"><?php  echo number_format($tot2,2);?></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 Tunggakan.pdf', 'D');
?>


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