<?php
include "koneksi.php";
if (!isset($_SESSION["UserID"])) {
	header("location:login.php?p=3");
}

// memanggil library FPDF
require('fpdf/fpdf.php');
// intance object dan memberikan pengaturan halaman PDF
$pdf = new FPDF('p','mm','A4');
// membuat halaman baru
$pdf->AddPage();
// setting jenis font yang akan digunakan
$pdf->SetFont('Arial','B',18);
// mencetak string 


$pdf->Image('images/mhb.jpeg',15,15	,13);

$pdf->Image('profile/iso1.jpg',165,15,11);
$pdf->Image('profile/iso2.jpg',178,15,11);
$pdf->Image('profile/iso3.jpg',190,15,11);

//$pdf->Image('profile/iso1.jpg',205,10,14);
//$pdf->Image('profile/iso2.jpg',225,10,15);
//$pdf->Image('profile/iso3.jpg',245,10,15);



$pdf->Cell(170,20,'PT.MULTI HIDRACHROME BALIKPAPAN',0,1,'C');
$pdf->SetFont('Arial','B',10);

$pdf->Cell(170,5,'DETAIL PERMOHONAN DANA',0,1,'C');
$pdf->SetFont('Arial','',10);


$timezone = time() + (60 * 60 * 1);
date_default_timezone_set('Asia/Jakarta');
$pdf->Cell(170,6,'Balikpapan,  '.date('d / M / yy / H:i:s a',$timezone),0,1,'C');
$pdf->SetFont('Arial','B',10);



//$idcuti = mysqli_real_escape_string($conn, $_GET["id"]);
$q = "SELECT * FROM data_purchasing_requisition WHERE advance='1'";
$res = mysqli_query($conn, $q);
$row = mysqli_fetch_array($res);

$id_suplier = $row['id_suplier'];
$s = "SELECT * FROM data_suplier WHERE id=" .$row['id_suplier'];
$resi = mysqli_query($conn,$s);
//$rowi = mysqli_fetch_array($resi);

$id_user = $_SESSION['UserID'];
$a = "SELECT * FROM user WHERE id=" .$id_user;
$resa = mysqli_query($conn,$a);
$rowa = mysqli_fetch_array($resa);

$pdf->Cell(20 , 5, '',0,1,'c');
$pdf->Cell(20 , 5, '',0,1,'c');
$sql ="select * from advance";
$resl = mysqli_query($conn, $sql);
while($datal = mysqli_fetch_array($resl))
{
	$id_advance = $datal['id_advance'];
	$ql = "select * from data_purchasing_requisition where id=".$id_advance;
	$res = mysqli_query($conn, $ql);
	while($data = mysqli_fetch_array($res))
	{
		
	
		$harga_belum_diskon[] =$data['harga_total_pcs'];
	}
}
$total_harga_belum_diskon    =array_sum($harga_belum_diskon);

$pdf->SetFont('Arial','',8);
$pdf->Cell(35 , 5, 'Kepada Yth,',0,1,'c');
$pdf->Cell(35 , 5, 'Bagian Accounting/Finance/tax',0,1,'c');
$pdf->Cell(35 , 5, 'di-',0,1,'c');
$pdf->Cell(35 , 10, 'Tempat',0,1,'c');

$pdf->Cell(35 , 10, 'Mohon dianggarkan dana sebesar Rp.'.number_format($total_harga_belum_diskon, 0, ".", ".").' untuk keperluan tersebut dibawah ini :',0,1,'c');







$pdf->SetFont('Arial','',11);//mulai isi
$pdf->Cell(20 , 5, '',0,1,'c');


//$id_user = $_SESSION['UserID'];
//$a = "SELECT * FROM user WHERE id=" .$id_user;
//$resa = mysqli_query($conn,$a);
//$rowa = mysqli_fetch_array($resa);
//$user = $rowa['nama'];

$pdf->SetFont('Arial','B',7);
$pdf->Cell(8,6,'No.',1,0,'C');
$pdf->Cell(80,6,'Description',1,0,'C');
$pdf->Cell(15,6,'Qty',1,0,'C');
//$pdf->Cell(30,6,'User',1,0,'C');
$pdf->Cell(30,6,'Harga Satuan',1,0,'C');
$pdf->Cell(30,6,'Total',1,0,'C');
$pdf->Cell(30,6,'No.PR',1,0,'C');
$pdf->Ln(2);
$no = 0;

//$idcuti = mysqli_real_escape_string($conn, $_GET["id"]);
$sql ="select * from advance";
$resl = mysqli_query($conn, $sql);
while($datal = mysqli_fetch_array($resl))
{
	$id_advance = $datal['id_advance'];
	$ql = "select * from data_purchasing_requisition where id=".$id_advance;
	$res = mysqli_query($conn, $ql);
	while($data = mysqli_fetch_array($res))
	{
		$discount=$data['discount_gabung'];
		$ppn=$data['ppn_gabung'];
		$delivery_po_date=$data['delivery_po_date'];
		$suplier = $data['id_suplier'];
		$tgl_acc = $data['tgl_disetujui_direktur'];
		$user_request = $data['user_request'];
		$harga_total = $data['harga_total_pcs'];
	    $jumlah = $data['quantity'];
		
		$harga_satuan = $harga_total / $jumlah;
		
		
		$no++;
		$pdf->Ln(4);
		$pdf->SetFont('Arial','',7);
		$pdf->Cell(8,4,$no.".",1,0,'C');
		$pdf->Cell(80,4,$data['description'],1,0,'C');
		//$pdf->Cell(30,4,$user,1,0,'C');
		$pdf->Cell(15,4,$data['quantity'].' '.$data['unit'],1,0,'C');
		$pdf->Cell(30,4,'Rp.     '.number_format($harga_satuan, 0, ".", "."),1,0,'C');
		$pdf->Cell(30,4,'Rp.     '.number_format($data['harga_total_pcs'], 0, ".", "."),1,0,'C');
		$pdf->Cell(30,4,$data['pr_no'],1,0,'C');
	
	}
}

	$total_harga_belum_diskon    =array_sum($harga_belum_diskon);
	//penghitungan
	//$total_harga_belum_diskon    =array_sum($harga_belum_diskon);
	//$diskon = $total_harga_belum_diskon * $discount / 100;
	//$total_diskon = $total_harga_belum_diskon - $diskon;
	$totalPpn= 0;



	$pdf->Ln(8);
	//$pdf->Cell(20 , 2, '',0,1,'c');//supaya bisa tulisan masuk ke kolom
	$pdf->SetFont('Arial','',7);
	$pdf->Cell(35 , 5, 'Pembulatan Nilai :'.$data[''],0,1,'c');
	
 
	// FUNGSI TERBILANG OLEH : MALASNGODING.COM
	// WEBSITE : WWW.MALASNGODING.COM
	// AUTHOR : https://www.malasngoding.com/author/admin
 
 
	function penyebut($total_harga_belum_diskon) {
		$total_harga_belum_diskon = abs($total_harga_belum_diskon);
		$huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
		$temp = "";
		if ($total_harga_belum_diskon < 12) {
			$temp = " ". $huruf[$total_harga_belum_diskon];
		} else if ($total_harga_belum_diskon <20) {
			$temp = penyebut($total_harga_belum_diskon - 10). " belas";
		} else if ($total_harga_belum_diskon < 100) {
			$temp = penyebut($total_harga_belum_diskon/10)." puluh". penyebut($total_harga_belum_diskon % 10);
		} else if ($total_harga_belum_diskon < 200) {
			$temp = " seratus" . penyebut($total_harga_belum_diskon - 100);
		} else if ($total_harga_belum_diskon < 1000) {
			$temp = penyebut($total_harga_belum_diskon/100) . " ratus" . penyebut($total_harga_belum_diskon % 100);
		} else if ($total_harga_belum_diskon < 2000) {
			$temp = " seribu" . penyebut($total_harga_belum_diskon - 1000);
		} else if ($total_harga_belum_diskon < 1000000) {
			$temp = penyebut($total_harga_belum_diskon/1000) . " ribu" . penyebut($total_harga_belum_diskon % 1000);
		} else if ($total_harga_belum_diskon < 1000000000) {
			$temp = penyebut($total_harga_belum_diskon/1000000) . " juta" . penyebut($total_harga_belum_diskon % 1000000);
		} else if ($total_harga_belum_diskon < 1000000000000) {
			$temp = penyebut($total_harga_belum_diskon/1000000000) . " milyar" . penyebut(fmod($total_harga_belum_diskon,1000000000));
		} else if ($total_harga_belum_diskon < 1000000000000000) {
			$temp = penyebut($total_harga_belum_diskon/1000000000000) . " trilyun" . penyebut(fmod($total_harga_belum_diskon,1000000000000));
		}     
		return $temp;
	}
 
	function terbilang($total_harga_belum_diskon) {
		if($total_harga_belum_diskon<0) {
			$hasil = "minus ". trim(penyebut($total_harga_belum_diskon));
		} else {
			$hasil = trim(penyebut($total_harga_belum_diskon));
		}     		
		return $hasil;
	}
 
 
	//$angka = 1530093;
	//echo terbilang($angka);
	
	$pdf->SetFont('Arial','B',9);
	$pdf->Cell(35 , 5,'Terbilang : '.terbilang($total_harga_belum_diskon).'                                                                                  
												           	Rp.               '.number_format($total_harga_belum_diskon, 0, ".", "."),0,1,'c');

	$pdf->Cell(20 , 2, '',0,1,'c');//supaya bisa tulisan masuk ke kolom
	$pdf->SetFont('Arial','',8);
	$pdf->Ln(2);


	$pdf->SetFont('Arial','',8);



	$pdf->Ln(20);

	$pdf->SetFont('Arial','B',8);
	$pdf->Cell(77 , 20, '                 Prepared & Request by,',0,0,'c');
	$pdf->Cell(20 , 20, '                                                                   Approved by,',0,1,'c');

	$pdf->SetFont('Arial','',8);
	$pdf->Cell(100 , 20, '                     (Acc By System)',0,0,'c');
	$pdf->Cell(63 , 20, '                                     Lukito Prasetyo',0,0,'c');
	$pdf->Cell(65 , 20, ' Olive S.R',0,0,'c');
	//$pdf->Cell(50 , 5, '                (Acc By System)'.$tgl_acc,0,0,'c');
	$pdf->Cell(20 , 15, '               ',0,1,'c');

	$b = "SELECT * FROM data_karyawan WHERE nik=" .$row['user_request'];
	$resb = mysqli_query($conn,$b);
	/////////////////$rowb = mysqli_fetch_array($resb);

	$pdf->SetFont('Arial','B',8);
	/////////////////$pdf->Cell(89 , 10, '      '.$rowb['nama_karyawan'].', '.$rowb['departement'],0,0,'c');
	////////////////$pdf->Cell(60 , 5, 'SPV'.', '.$rowb['departement'],0,0,'c');
	//$pdf->Cell(20 , 5,$rowi['nama_suplier'],0,1,'c');
	
	$pdf->Cell(85 , 5, '          Hary Prasetya, Adm Purchasing',0,0,'c');
	$pdf->Cell(45 , 5, '    Manager Fungsional',0,0,'c');
	$pdf->Cell(30 , 5, '    Direktur',0,0,'c');
	$pdf->Cell(93 , 5, '    Acc. Dept',0,0,'c');
	$pdf->Cell(20 , 5, '    ',0,1,'c');


	//$rowi['kode_suplier']. ' ( '.$rowi['nama_suplier'].' )'

	$no = 1;
	$pdf->Ln();
	while ($row = mysqli_fetch_array($res)){
		//$pdf->SetFont('Arial','B',10);
		//$pdf->SetFillColor(255,255,255);
		//$pdf->Cell(15,6,$no++,1,0,'C',true);
		//$pdf->Cell(40,6,$row['nik'],1,0,'C');
		//$pdf->Cell(85,6,$row['nama_karyawan'],1,0,'L');
		//$pdf->Cell(47,6,"",1,0,'C');
		//$pdf->Ln();
}

$pdf->Output();


?>