 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(210 ,330 ));
        $this->AddFont("calibri","","calibri.php");
        $this->AddFont("calibri","B","calibrib.php");
    }
	function Header(){
        
		if($this->PageNo() == 1){
            $x = $this->GetX();
            $y = $this->GetY();

            $this->setFont('calibri','B',13);

            $this->MultiCell(60, 10, $this->label_cv=="true" ? "CV KETJUBUNG" : "", 1, "C");

            $akun=App\Perkiraan::where([['id', '=', $this->data->perkiraanAkun->parent]])->first();

            $this->SetXY($x+60, $y);
            $this->MultiCell(75, 10, "BUKTI ".strtoupper($akun->nama_perkiraan)." KELUAR", 1, "C");

            $this->SetXY($x+135, $y);
            $this->setFont('calibri','',11);
            $this->MultiCell(65, 10, "Nomor : ".$this->data->nomor_transaksi, 1, "L");

            $this->setFont('calibri','',11);
            $this->MultiCell(135, 10, "Dibayarkan kepada :"."\n".$this->data->penerima_transaksi, 1, "L");

            $this->SetXY($x+135, $y+10);
            $this->setFont('calibri','',11);
            $this->MultiCell(65, 10, "Tanggal : ".date_db($this->data->tanggal_transaksi, true), 1, "L");

            $this->SetXY($x+135, $y+20);
            $this->setFont('calibri','',11);
            $this->MultiCell(65, 10, "Lampiran : ", 1, "L");

            $this->setFont('calibri','B',11);
            $this->MultiCell(135, 5, "Uraian", 1, "C");

            $this->SetXY($x+135, $y+30);
            $this->setFont('calibri','B',11);
            $this->MultiCell(65, 5, "Jumlah", 1, "C");
        }
				
	}
	
	function Content()
	{          
        $x = $this->GetX();
        $y = $this->GetY();

        $total = 0;
        $y_total = 5;
        $y_jumlah = 0;
        $h_uraian = 5;
        $str_max = 200;

        foreach ($this->data->transaksiSumberdana as $key => $value) {
            $str_nama = strlen($value->keterangan);
            $h_str = ceil($str_nama/$str_max);

            if($str_nama>$str_max){
                $h_uraian = $h_uraian*$h_str;
            }
            if($value->tipe_id_akun == 'K'){
                $set_total = "( Rp".rupiah2($value->nominal_transaksi, 2)." )";
            }else{
                $set_total = "Rp".rupiah2($value->nominal_transaksi, 2);
            }

            $this->setFont('calibri','',11);
            // $this->MultiCell(135, 5, $value->keterangan, 1, "L");
            $this->MultiCell(135,$h_uraian/$h_str,$value->keterangan,1,'L');

            if ($str_nama <= 75) {
                $h_jumlah = 5;
            } else if ($str_nama > 75) {
                $h_jumlah = 10;
            }

            $this->SetXY($x+135, $y+$y_jumlah);
            $this->MultiCell(65, $h_jumlah, $set_total, 1, "R");

            if($value->tipe_id_akun=='K'){
                $total -= $value->nominal_transaksi;
            }else{
                $total += $value->nominal_transaksi;
            }

            $y_total += 5;
            $y_jumlah += 5;
        }

        $this->setFont('calibri','',11);
        $this->MultiCell(135, 5, "Total", 1, "R");

        $this->SetXY($x+135, $y+$y_jumlah);
        $this->setFont('calibri','',11);
        $this->MultiCell(65, 5, "Rp".rupiah2($total, 2), 1, "R");

        $this->setFont('calibri','',11);
        $this->MultiCell(20, 10, "Terbilang", 1, "C");

        $y_terbilang = $y_jumlah+5;

        $this->SetXY($x+20, $y+$y_terbilang);
        $this->setFont('calibri','',11);
        $this->MultiCell(180, 10, terbilang($total), 1, "L");

        $y_rekening = $y_terbilang+10;

        $this->SetXY($x+20, $y+$y_rekening);
        $this->setFont('calibri','',11);
        $this->MultiCell(100, 5, "REKENING", 1, "C");

        $this->SetXY($x+120, $y+$y_rekening);
        $this->setFont('calibri','',11);
        $this->MultiCell(30, 5, "DEBIT/KREDIT", 1, "C");

        $this->SetXY($x+150, $y+$y_rekening);
        $this->setFont('calibri','',11);
        $this->MultiCell(50, 5, "Dibuat Oleh :", 1, "L");

        $arr_tipe=['D'=>"DEBIT", 'K'=>"KREDIT"];
        $height_rek = $y_rekening+5;
        $height_tgl = 0;
        $y_posting = 20;
        $height_posting = 0;
        $y_head_signature = 20;
        $y_body_signature = 30;

        $h = 5;
        $l_max = 200;
        

        foreach ($this->data->transaksiSumberdana as $key => $value) {
            $akun = App\Perkiraan::find($value->ke_akun);
            $this->SetXY($x+20, $y+$height_rek);
            $this->setFont('calibri','',11);

            $l_nama = strlen(format_akun_perkiraan($akun));
            $h_nama = ceil($l_nama/$l_max);
            if($l_nama>$l_max){
                $h = $h*$h_nama;
            }
            $this->MultiCell(100,$h/$h_nama,format_akun_perkiraan($akun),1,'L');

            // $this->MultiCell(100, 5, format_akun_perkiraan($akun), 1, "L");

            if ($l_nama <= 50) {
                $h_akun = 5;
            } else if ($l_nama > 50) {
                $h_akun = 10;
            }

            $this->SetXY($x+120, $y+$height_rek);
            $this->setFont('calibri','',11);
            $this->MultiCell(30, $h_akun, $arr_tipe[$value->tipe_id_akun], 1, "L");

            $height_rek += 10;
            $height_tgl += $h_akun;
            $height_posting += $h_akun+5;
            $y_head_signature += $h_akun+5;
            $y_body_signature += $h_akun+5;
        }

        $this->SetXY($x, $y+$y_posting);
        $this->setFont('calibri','',11);
        $this->MultiCell(20, $height_posting, "POSTING", 1, "C");

        $y_tgl = $y_rekening+5;
        $this->SetXY($x+150, $y+$y_tgl);
        $this->setFont('calibri','',11);
        $this->MultiCell(50, $height_tgl, $this->tanggal_cetak, 1, "L");

        $this->setFont('calibri','',11);
        $this->Cell(65, 30, "Catatan :", 1, 0, "T");

        $this->SetXY($x+65, $y+$y_head_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 10, "Pembukuan", 1, "C");

        $this->SetXY($x+100, $y+$y_head_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 10, "Mengetahui", 1, "C");

        $this->SetXY($x+135, $y+$y_head_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 10, "Menyetujui", 1, "C");

        $this->SetXY($x+170, $y+$y_head_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(30, 10, "Penerima", 1, "C");

        $this->SetXY($x+65, $y+$y_body_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 20, "", 1, "C");

        $this->SetXY($x+100, $y+$y_body_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 20, "", 1, "C");

        $this->SetXY($x+135, $y+$y_body_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(35, 20, "", 1, "C");

        $this->SetXY($x+170, $y+$y_body_signature);
        $this->setFont('calibri','',11);
        $this->MultiCell(30, 20, "", 1, "C");
	}
	
	function Footer()
	{
		$this->Ln();
		
		$this->SetY(-15);
		//buat garis horizontal
		
		//Arial italic 9
		$this->SetFont('Calibri','B',9);
        $this->Cell(0,10,'',0,0,'L');
		//nomor halaman
	}
}

$Pdf = new Pdf();

$Pdf->data          = $data['detail_data'];
$Pdf->label_cv      = $data['label_cv'];
$Pdf->tanggal_cetak = $data['tanggal_cetak'];

$Pdf->SetAutoPageBreak(true ,15);
$Pdf->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Calibri','',11);
$Pdf->Content();
$Pdf->SetTitle("Cetak Transaksi Pengeluaran - ".$data['detail_data']->nomor_transaksi);
$Pdf->Output("Cetak Transaksi Pengeluaran - ".$data['detail_data']->nomor_transaksi.".pdf", "I");

?>