<?php
// memanggil library FPDF
require('../library/fpdf185/fpdf.php');

include '../koneksi.php';

// intance object dan memberikan pengaturan halaman PDF
// asline // $pdf=new FPDF('L','mm', array(200,104));
// $pdf=new FPDF('P','mm', 'A6');

$pdf=new FPDF('P','mm','A4');
 

// asline //$pdf->SetMargins(7,8,0,0);
$pdf->SetMargins(9,10,0,0);
$pdf->AddPage();

function penyebut($nilai) {
  $nilai = abs($nilai);
  $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
  $temp = "";
  if ($nilai < 12) {
    $temp = "". $huruf[$nilai];
  } else if ($nilai <20) {
    $temp = penyebut($nilai - 10). " belas";
  } else if ($nilai < 100) {
    $temp = penyebut($nilai/10)." puluh ". penyebut($nilai % 10);
  } else if ($nilai < 200) {
    $temp = " seratus " . penyebut($nilai - 100);
  } else if ($nilai < 1000) {
    $temp = penyebut($nilai/100) . " ratus " . penyebut($nilai % 100);
  } else if ($nilai < 2000) {
    $temp = " seribu " . penyebut($nilai - 1000);
  } else if ($nilai < 1000000) {
    $temp = penyebut($nilai/1000) . " ribu " . penyebut($nilai % 1000);
  } else if ($nilai < 1000000000) {
    $temp = penyebut($nilai/1000000) . " juta " . penyebut($nilai % 1000000);
  } else if ($nilai < 1000000000000) {
    $temp = penyebut($nilai/1000000000) . " milyar " . penyebut(fmod($nilai,1000000000));
  } else if ($nilai < 1000000000000000) {
    $temp = penyebut($nilai/1000000000000) . " trilyun " . penyebut(fmod($nilai,1000000000000));
  }     
  return $temp;
}

function terbilang($nilai) {
  if($nilai<0) {
    $hasil = "minus ". trim(penyebut($nilai));
  } else {
    $hasil = trim(penyebut($nilai));
  }         
  return $hasil . " rupiah";
}

              $xkdr  = $_GET['id'] ;    
              $tg_d = $_GET['tgl_d'];
              $tg_a = $tgl_dari-1;
              $tg_s = $_GET['tgl_s'];

$pinjaman = mysqli_query($koneksi,"select * from pinjaman where pinj_id='$id'");
$s = mysqli_fetch_assoc($pinjaman);
$totale=($s['pinj_pokok']+$s['pinj_jasa']);
$angs=($s['pinj_pokok']+$s['pinj_jasa'])/$s['pinj_jkw'] ;

$user = mysqli_query($koneksi,"select * from user where user_kdr='$xkdr'");
$u = mysqli_fetch_assoc($user);

$rayon = mysqli_query($koneksi,"select * from rayon where rayon_kdr='$xkdr'");
$r = mysqli_fetch_assoc($rayon);

$karyawan = mysqli_query($koneksi,"select * from karyawan where id='$u[id_kar]'");
$k = mysqli_fetch_assoc($karyawan);

$pinjamanx = mysqli_query($koneksi,"select count(pinj_angg_no) AS tkali from pinjaman where pinj_angg_no='$noang'");
$x = mysqli_fetch_assoc($pinjamanx);

$pinjamanx = mysqli_query($koneksi,"select sum(simp_d) AS tsimp_d,sum(simp_k) AS tsimp_k from simpanan where angg_no='$noang'");
$y = mysqli_fetch_assoc($pinjamanx);

$total=$y['tsimp_d']-$y['tsimp_k'] ;
$kini=$s['simp'] ;
$lalu=$total-$kini ;

$kdkanca = mysqli_query($koneksi,"select * from kdkanca where id='1'");
$kc = mysqli_fetch_assoc($kdkanca);

$gambar='../gambar/kop_surat_stm.png';

$pdf->Image(‘foto/’ . $gambar,10,10,70);

$pdf->SetFont('Arial','B',13);


$pdf->Cell(20,15,'','0',1,'C');
$pdf->Cell(200,8,'DAFTAR ANGSURAN',0,1,'C');

$pdf->SetFont('Arial','I',10);
$pdf->Cell(30,5,'Resort',0,0);
$pdf->Cell(2,5,':',0,0,'C');
$pdf->Cell(10,5,$r['rayon_nama'],0,1);

$pdf->Cell(30,5,'Dari Tanggal',0,0);
$pdf->Cell(2,5,':',0,0,'C');
$pdf->Cell(10,5,date('d-m-Y', strtotime($tg_d)),0,1);
$pdf->Cell(30,5,'Sampai Tanggal',0,0);
$pdf->Cell(2,5,':',0,0,'C');
$pdf->Cell(10,5,date('d-m-Y', strtotime($tg_s)),0,1);

$pdf->SetFont('Arial','B',10);
$pdf->Cell(10,7,'No.',1,0,'C');
$pdf->Cell(20,7,'Tanggal.',1,0,'C');
$pdf->Cell(17,7,'Rekg',1,0,'C');
$pdf->Cell(40,7,'Nama' ,1,0,'C');
$pdf->Cell(50,7,'Alamat',1,0,'C');
$pdf->Cell(20,7,'Bayar',1,0,'C');
$pdf->Cell(20,7,'Saldo',1,0,'C');
$pdf->Cell(7,7,'Gol',1,0,'C');
$pdf->Cell(10,7,'',0,1);
$pdf->SetFont('Arial','',8);

$no=1;
$data = mysqli_query($koneksi,"SELECT * FROM pinj_angs WHERE angs_kdr=$xkdr and angs_tgl>='$tg_d' and angs_tgl<='$tg_s' ORDER BY stat ASC");
while($d = mysqli_fetch_array($data)){


    
    $anggota = mysqli_query($koneksi,"select * from anggota where angg_no='$d[angg_no]'");
    $a = mysqli_fetch_assoc($anggota);    
    $namane    =$a['angg_nama'];
    $tampil_nama    =substr($namane, 0, 20);
    
    $alamate    =$a['angg_alamat'];
    $tampil_alamat    =substr($alamate, 0, 30);    
/////////////// MENCARI SALDO AKHIR SETELAH ANGSURAN ////////////////////////
    $pinjaman = mysqli_query($koneksi,"select * from pinjaman where pinj_no='$d[pinj_no]'");
    $p = mysqli_fetch_assoc($pinjaman); 
    $pokok=$p['pinj_pokok'];
    $jasa=$p['pinj_jasa'];
    $total=$pokok+$jasa ;

    $pinj_angs = mysqli_query($koneksi,"select sum(angs_byrjml) AS tbayar from pinj_angs where pinj_no='$d[pinj_no]' and angs_tgl<='$d[angs_tgl]' ");
    $b = mysqli_fetch_assoc($pinj_angs);   
    
    $sisa=$total-$b['tbayar'];
    
  $pdf->Cell(10,5, $no++,1,0,'C');
  $pdf->Cell(20,5, date('d-m-Y', strtotime($d['angs_tgl'])) ,1,0,'C');
  $pdf->Cell(17,5, $d['pinj_no'],1,0,'C');
  $pdf->Cell(40,5, $tampil_nama,1,0);
  $pdf->Cell(50,5, $tampil_alamat,1,0);  
  $pdf->Cell(20,5,'' . number_format($d['angs_byrjml']).'',1,0,'R');
  $pdf->Cell(20,5,'' . number_format($sisa).'',1,0,'R');
  $pdf->Cell(7,5, $d['stat'],1,1);  
}
    $pinj_angs = mysqli_query($koneksi,"select sum(angs_byrjml) AS tbayar from pinj_angs WHERE angs_kdr=$xkdr and angs_tgl>='$tg_d' and angs_tgl<='$tg_s' ");
    $t = mysqli_fetch_assoc($pinj_angs);   
    
$pdf->SetFont('Arial','B',10);
$pdf->Cell(137,7,'TOTAL',1,0,'C');
$pdf->SetFont('Arial','',8);
$pdf->Cell(20,7,'' . number_format($t['tbayar']).'',1,0,'R');
$pdf->Cell(27,7,'',1,0,'C');
$pdf->Cell(10,7,'',0,1);  
  
  $tgl_skr=date('Y-m-d');
  
$pdf->SetFont('Arial','',9);
$pdf->Cell(60,3,'',0,0,'C');
$pdf->Cell(10,3,'',0,1);  

$pdf->Cell(60,5,'',0,0,'C');
$pdf->Cell(67,5,$kc['kota'],0,0,'R');
$pdf->Cell(20,5,date('d-m-Y', strtotime($tgl_skr)),0,0,'R');
$pdf->Cell(27,5,'',0,0,'C');
$pdf->Cell(10,5,'',0,1);  

$pdf->Cell(90,5,'',0,0,'C');
$pdf->Cell(40,5,Pengawas,0,0,'C');
$pdf->Cell(40,5,Petugas,0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');

$pdf->Cell(60,12,'',0,0,'C');
$pdf->Cell(10,12,'',0,1);  

$pdf->Cell(90,5,'',0,0,'C');
$pdf->Cell(40,5,$kc['pengawas'],0,0,'C');
$pdf->Cell(40,5,$k['nama'],0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');
$pdf->Output();

?>