

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

session_start();
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(19,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";
}
            //////////////////////////////////////////////////////////////////////////// 
            $kdc   =$_SESSION['kdc'];
            $oleh   =$_SESSION['id_kar'];
            ////////////////////////////////////////////////////////////////////////////     
            $tgl_d = $_GET['xtgl_d'];
            $tgl_s = $_GET['xtgl_s'];
            $kdj   = $_GET['xid'];
            ////////////////////////////////////////////////////////////////////////////        
$nawal = mysqli_query($koneksi,"select * from nawal where noperk='$kdj' ");
$na = mysqli_fetch_assoc($nawal);

$sql1 = mysqli_query($koneksi, "SELECT sum(awald) as tn_awal,perk FROM nawal WHERE noperk='$kdj' ");
        list($tn_awal,$perk) = mysqli_fetch_array($sql1);
                    
$sql2 = mysqli_query($koneksi, "SELECT sum(debet-kredit) as tj_awal FROM jor WHERE noperk='$kdj' and tanggal<='$tgl_d' ");
        list($tj_awal) = mysqli_fetch_array($sql2);                    
        $tsaldo_awal=$tn_awal+$tj_awal;
                
$kdkanca = mysqli_query($koneksi,"select * from kdkanca where kd='$kdc'");
$kc = mysqli_fetch_assoc($kdkanca);
$logo_kop=$kc['gambar'] ;


$gambar='../gambar/'.$logo_kop.'';

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

$pdf->SetFont('Arial','B',12);
$pdf->Cell(10,10,' ',0,1);

$pdf->Cell(20,15,'','0',1,'C');
$pdf->Cell(200,8,'Laporan Rekap '.$na['perk'].'',0,1,'C');

$pdf->SetFont('Arial','I',10);
$pdf->Cell(200,5,'Tanggal '.date('d-m-Y', strtotime($tgl_d)).' dan s/d tanggal '.date('d-m-Y', strtotime($tgl_s)).'',0,1,'C');

$pdf->SetFont('Arial','',9);
$pdf->Cell(60,4,'',0,0,'C');
$pdf->Cell(10,4,'',0,1);  

// Atur warna latar belakang sel
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

// Atur warna teks
$pdf->SetTextColor(0, 0, 0); // Warna hitam

$pdf->SetFont('Arial','B',11);
$pdf->Cell(10,9,'No.',1,0,'C', true);
$pdf->Cell(100,9,'Keterangan',1,0,'C', true);
$pdf->Cell(35,9,' Rp. ',1,0,'C', true);
$pdf->Cell(35,9,' Rp. ',1,0,'C', true);
$pdf->Cell(5,9,'',0,1);
$pdf->SetFont('Arial','',8);
$pdf->SetTextColor(0, 0, 0); // Warna hitam

$pdf->SetFont('Arial','',12);
$pdf->Cell(10,9,'I.',1,0,'C');
$pdf->Cell(100,9,'Saldo Awal Kas',1,0,'L');
$pdf->Cell(35,9,'',1,0,'C');
$pdf->Cell(35,9,'' . number_format($tsaldo_awal).'',1,0,'R');
$pdf->Cell(5,9,'',0,1);

$pdf->SetFillColor(220, 220, 220); // Warna abu-abu
$pdf->SetFont('Arial','',10);
$pdf->Cell(10,9,'A.',1,0,'C', true);
$pdf->Cell(100,9,'PENERMAAN',1,0,'L', true);
$pdf->Cell(35,9,'',1,0,'C', true);
$pdf->Cell(35,9,'',1,0,'C', true);
$pdf->Cell(5,9,'',0,1);

$no=1;
            $data = mysqli_query($koneksi,"SELECT * FROM jor WHERE noperk='$kdj' and tanggal>='$tgl_d' and tanggal<='$tgl_s' and akunt<'50000' GROUP BY akunt ");
            while($x = mysqli_fetch_array($data)){
                $akunt  =$x['akunt'] ;
                $sql3 = mysqli_query($koneksi, "SELECT noperk,perk FROM nawal WHERE noperk='$akunt' ");
                    list($noperk,$perk) = mysqli_fetch_array($sql3);  
                    
                $jor2 = mysqli_query($koneksi,"select count(noperk) AS jtakun,sum(debet) AS jdebet,sum(kredit) AS jkredit FROM jor WHERE noperk='$kdj' and akunt='$akunt' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                    $j2 = mysqli_fetch_assoc($jor2);    
    
  $pdf->Cell(10,7, $no++,1,0,'C');
  $pdf->Cell(100,7,$noperk.' - '.$perk,1,0,'L');
  $pdf->Cell(35,7,'' . number_format($j2['jdebet']).'',1,0,'R'); 
  $pdf->Cell(35,7,'' . number_format($j2['jkredit']).'',1,0,'R');
  $pdf->Cell(5,7,'',0,1);  
}

                $jor3 = mysqli_query($koneksi,"select count(noperk) AS jtakun,sum(debet) AS jdebet,sum(kredit) AS jkredit FROM jor WHERE noperk='$kdj' and akunt<'50000' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                    $j3 = mysqli_fetch_assoc($jor3);  
                    $j3total=$j3['jdebet']-$j3['jkredit'] ;
    
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(10,9,'',1,0,'C');
$pdf->Cell(100,7,'TOTAL PENERIMAAN ',1,0,'L');
$pdf->Cell(35,7,' ',1,0,'R');
$pdf->Cell(35,7,'' . number_format($j3total).'',1,0,'R');
$pdf->Cell(5,7,'',0,1);  
  

$pdf->SetFillColor(220, 220, 220); // Warna abu-abu
$pdf->SetFont('Arial','',10);
$pdf->Cell(10,9,'B.',1,0,'C', true);
$pdf->Cell(100,9,'PENGELUARAN',1,0,'L', true);
$pdf->Cell(35,9,'',1,0,'C', true);
$pdf->Cell(35,9,'',1,0,'C', true);
$pdf->Cell(5,9,'',0,1);

$no=1;
            $data = mysqli_query($koneksi,"SELECT * FROM jor WHERE noperk='$kdj' and tanggal>='$tgl_d' and tanggal<='$tgl_s' and akunt>'50000' GROUP BY akunt ");
            while($x = mysqli_fetch_array($data)){
                $akunt  =$x['akunt'] ;
                $sql3 = mysqli_query($koneksi, "SELECT noperk,perk FROM nawal WHERE noperk='$akunt' ");
                    list($noperk,$perk) = mysqli_fetch_array($sql3);  
                    
                $jor2 = mysqli_query($koneksi,"select count(noperk) AS jtakun,sum(debet) AS jdebet,sum(kredit) AS jkredit FROM jor WHERE noperk='$kdj' and akunt='$akunt' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                    $j2 = mysqli_fetch_assoc($jor2);    
    
  $pdf->Cell(10,7, $no++,1,0,'C');
  $pdf->Cell(100,7,$noperk.' - '.$perk,1,0,'L');
  $pdf->Cell(35,7,'' . number_format($j2['jkredit']).'',1,0,'R');
  $pdf->Cell(35,7,'' . number_format($j2['jdebet']).'',1,0,'R'); 
  $pdf->Cell(5,7,'',0,1);  
}

                $jor3 = mysqli_query($koneksi,"select count(noperk) AS jtakun,sum(debet) AS jdebet,sum(kredit) AS jkredit FROM jor WHERE noperk='$kdj' and akunt>'50000' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                    $j3 = mysqli_fetch_assoc($jor3);  
                    $j3total=$j3['jdebet']-$j3['jkredit'] ;
    
$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$pdf->SetFont('Arial','B',10);
$pdf->Cell(10,9,'',1,0,'C');
$pdf->Cell(100,9,'TOTAL PENGELUARAN ',1,0,'L');
$pdf->Cell(35,9,' ',1,0,'R');
$pdf->Cell(35,9,'' . number_format($j3total).'',1,0,'R');
$pdf->Cell(5,9,'',0,1);  

$pdf->SetFillColor(220, 220, 220); // Warna abu-abu

$sql1 = mysqli_query($koneksi, "SELECT sum(awald) as tn_awal,perk FROM nawal WHERE noperk='$kdj' ");
        list($tn_awal,$perk) = mysqli_fetch_array($sql1);
                    
$sql2 = mysqli_query($koneksi, "SELECT sum(debet-kredit) as tj_awal FROM jor WHERE noperk='$kdj' and tanggal<='$tgl_s' ");
        list($tj_awal) = mysqli_fetch_array($sql2);                    
        $tsaldo_akhir=$tn_awal+$tj_awal;
        
$pdf->SetFont('Arial','',12);
$pdf->Cell(10,9,'II.',1,0,'C', true);
$pdf->Cell(100,9,'Saldo Akhir '.$na['perk'].'',1,0,'L', true);
$pdf->Cell(35,9,'',1,0,'C', true);
$pdf->Cell(35,9,'' . number_format($tsaldo_akhir).'',1,0,'R', true);
$pdf->Cell(5,9,'',0,1);



date_default_timezone_set('Asia/Jakarta');

$bulan = array(
  1 => 'Januari',
  'Februari',
  'Maret',
  'April',
  'Mei',
  'Juni',
  'Juli',
  'Agustus',
  'September',
  'Oktober',
  'November',
  'Desember'
);

$tgl = date('d');
$bln = date('n');
$thn = date('Y');
//echo $tgl . ' ' . $bulan[$bln] . ' ' . $thn;

$pdf->SetFont('Arial','',9);
$pdf->Cell(60,3,'',0,0,'C');
$pdf->Cell(10,3,'',0,1);  

$pdf->Cell(70,5,'',0,0,'C');
$pdf->Cell(80,5,''.$kc['kota'].',  '.$tgl . ' ' . $bulan[$bln] . ' ' . $thn.'',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,$kc['ttd_1'],0,0,'C');
$pdf->Cell(40,5,$kc['ttd_2'],0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');

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

$pdf->Cell(90,5,'',0,0,'C');
$pdf->Cell(40,5,$kc['nama_ttd_1'],0,0,'C');
$pdf->Cell(40,5,$kc['nama_ttd_2'],0,0,'C');
$pdf->Cell(27,5,'',0,1,'C');

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="dokumen.pdf"');
$pdf->Output();
?>