<?php
//error_reporting(0);
session_start();

ob_start();
$html = ob_get_clean();
// include autoloader
require_once '../setting/crud.php';
require_once '../setting/koneksi.php';
require_once '../setting/tanggal.php';
require_once '../setting/fungsi.php';

require_once '../dompdf/autoload.inc.php';
require_once  '../dompdf/vendor/autoload.php' ;
/*require_once '../dompdf/style.php';*/

use Dompdf\Dompdf;


$periode1=$_SESSION['tuggakan']['periode2'];
$sqlt=$_SESSION['tuggakan']['kode']['sqlt'];


$header='<p align="center" style="margin:0px;font-family : Arial; ">'."PT. MENARA SEMBILAN SEJAHTERA".'</p>';
$header=$header.'<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;"> KANTOR '.$_SESSION['kantor'].' </p>';
$header=$header.'<p style="font-size:13px;text-align:center;margin:0px; font-family : Arial;">'."LAPORAN TUNGGAKAN".' </p>';

/*$periode='<p style="font-size:5px;text-align:center;margin-top:0px"></p>';*/


$isi=$isi='<table border="1" class="table table-bordered table-hover" align="center"
<style>
   table {border-collapse:collapse; table-layout:fixed;width: 100%; }
    table td {margin: 0 20px 20px 0; font-family : Arial;}
        table {
            border-collapse: collapse;
        }
        
        
        th, td {
            padding-right: 15px;
            font-size: 13px;
            height: 20px;
            /*text-align: right;*/
            margin-bottom: 1px;
            
            /*font-weight: bold; fon tebal*/
            border-bottom: bold;
          }

          body { font-family: Arial; }
          p { font-family: Courier, monospace; }
          div { font-family: "Duru Sans", Verdana, sans-serif; }
          
                 
        
    </style>
>

	    <thead>
	      <tr>
	        <th style="text-align: center; background: #ddd;width: 5%">No</th>
	        <th style="text-align: center; background: #ddd;width: 12%">Tgl</th>
            <th style="text-align: center; background: #ddd;width: 14%">Rekening</th>
            <th style="text-align: center; background: #ddd;width: 15%">Nama</th>
            <th style="text-align: center; background: #ddd;width: 15%">Alamat</th>
            <th style="text-align: center; background: #ddd;width: 12%">Plafond</th>
            <th style="text-align: center; background: #ddd;width: 7%">Ujroh</th>
            <th style="text-align: center; background: #ddd;width: 7%">Lama</th>
            <th style="text-align: center; background: #ddd;width: 12%">Bakidebet</th>
            <th style="text-align: center; background: #ddd;width: 12%">T.Pokok</th>
            <th style="text-align: center; background: #ddd;width: 12%">T.Margin</th>
            <th style="text-align: center; background: #ddd;width: 12%">Jumlah</th>
            <th style="text-align: center; background: #ddd;width: 5%">Kol</th>
            
	      </tr>
	    </thead>
    <tbody>';
			$no=0;
	  	
      
	  $query      = $sqlt;
	  $result     = $mysqli->query($query);
	  $num_result = $result->num_rows;
	  if ($num_result > 0) {
	  	
	    while ($data = mysqli_fetch_assoc($result)) {
	      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;
                  $date2 = date('Y-m-d',strtotime($periode1));
                /*$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++;

	      	$isi=$isi.'<tr>';
	       	$isi=$isi.'<td style="text-align: center; padding-left: 6px;">'.$no.'</td>';
	        $isi=$isi.'<td style="text-align: center; padding-left: 6px;">'.date('d-m-Y',strtotime($tgl)).'</td>';
	        $isi=$isi.'<td style="text-align: center; padding-left: 6px;">'.$rekening.'</td>';
	        $isi=$isi.'<td style="text-align: left; padding-left: 6px;">'.$nama.'</td>';
			$isi=$isi.'<td style="text-align: left; padding-left: 6px;">'.$alamat.'</td>';	        
	        $isi=$isi.'<td style="text-align: right; padding-right: 6px;">'.number_format($plafond,0).'</td>';
	        $isi=$isi.'<td style="text-align: center; padding-right: 6px;">'.$margin.'</td>';
	        $isi=$isi.'<td style="text-align: center; padding-right: 6px;">'.$jangkawaktu.'</td>';
	        $isi=$isi.'<td style="text-align: right; padding-right: 6px;">'.number_format($bakidebet,0).'</td>';
	        $isi=$isi.'<td style="text-align: right; padding-right: 6px;">'.number_format($flat_t_pokok,0).'</td>';
	        $isi=$isi.'<td style="text-align: right; padding-right: 6px;">'.number_format($flat_t_margin,0).'</td>';
	        $isi=$isi.'<td style="text-align: right; padding-left: 6px;">'.number_format($tot_tunggakan,0).'</td>';
            function kol($tunggakan,$Bulan2){
                    if($tunggakan<=0)
                    { echo "0";
                    }else{
                    if($Bulan2<=1){ echo "0";
                    }elseif($Bulan2<=2){
                    echo "1";
                    }else{echo "2";
                    } 
                    }
                    }  
            $isi=$isi.'<td style="text-align: right; padding-left: 6px;">'?><?php echo kol($tot_tunggakan,$jmlBulan2).'</td>';
            /*$isi=$isi.'<td style="text-align: right; padding-left: 6px;">'?><?php if($tot_tunggakan<=0){"0"; }else{
                    "2";'</td>';}*/
	        
	    }
	}

$isi=$isi.'</tbody></table>';

//echo $style.$header.$periode.$isi;

// instantiate and use the dompdf class
//echo $style.$header.$periode.$isi;
$html = ob_get_contents();
ob_end_clean();
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($header.$periode.$isi);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$filename = "Laporan_Tunggakan_".date("Y-m-d_H-i-s");
$dompdf->stream($filename);


?>