 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	public $def_width = 140, $def_height = 310;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->def_width , $this->def_height));
        $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',9);

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

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

            $this->SetXY($x+30, $y);
            $this->MultiCell(57, 8, "BUKTI ".strtoupper($akun->nama_perkiraan)." ".$this->bukti, 1, "C");

            $this->SetXY($x+87, $y);
            $this->setFont('calibri','',9);
            $this->MultiCell(45, 8, "Nomor : ".$this->data->nomor_transaksi, 1, "L");

            $this->MultiCell(87, 8, "Dibayarkan kepada :"."\n".$this->data->penerima_transaksi, 1, "L");

            $this->SetXY($x+87, $y+8);
            $this->MultiCell(45, 8, "Tanggal : ".date_db($this->data->tanggal_transaksi, true), 1, "L");

            $this->SetXY($x+87, $y+16);
            $this->MultiCell(45, 8, "Lampiran : ", 1, "L");

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

            $this->SetXY($x+87, $y+24);
            $this->setFont('calibri','B',9);
            $this->MultiCell(45, 5, "Jumlah", 1, "C");
        }
				
	}
	
	function Content()
	{          
        $x = $this->GetX();
        $y = $this->GetY();

        $total = 0;
        $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 ($str_nama <= 60) {
                $h_jumlah = 5;
            } else if ($str_nama > 60) {
                $h_jumlah = 10;
            }

            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','',9);

            // $this->MultiCell(87,$h_uraian/$h_str,$value->keterangan,1,'L');
            // $this->SetXY($x, $y+$y_jumlah);
            $this->SetXY($x, $y);
            $this->SetWidths(array(87));
            $this->SetAligns(array("L"));
            $this->Row(array($value->keterangan));

            // $this->SetXY($x+87, $y+$y_jumlah);
            $this->SetXY($x+87, $y);
            $this->MultiCell(45, $h_jumlah, $set_total, 1, "R");

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

            $y_jumlah += $h_jumlah;

            $x = $this->GetX();
            $y = $this->GetY();
        }

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

        // $this->SetXY($x+87, $y+$y_jumlah);
        $this->SetXY($x+87, $y);
        $this->MultiCell(45, 5, "Rp".rupiah2($total, 2), 1, "R");

        $y_terbilang = $this->getY();
        $this->MultiCell(15, 8, "Terbilang", 1, "C");

        $this->SetXY($x+15, $y_terbilang);
        // $this->MultiCell(117, 10, terbilang($total), 1, "L");
        $str_terbilang = strlen(terbilang($total));

        if ($str_terbilang <= 80) {
            $h_terbilang = 8;
        } else if ($str_terbilang > 80) {
            $h_terbilang = 5;
        }

        $this->SetWidths(array(117));
        $this->SetAligns(array("L"));
        $this->Row(array(terbilang($total)),$h_terbilang);

        $y_rekening = $this->getY();

        $this->SetXY($x+15, $y_rekening);
        $this->MultiCell(52, 5, "REKENING", 1, "C");

        $this->SetXY($x+67, $y_rekening);
        $this->MultiCell(25, 5, "DEBIT/KREDIT", 1, "C");

        $this->SetXY($x+92, $y_rekening);
        $this->MultiCell(40, 5, "Dibuat Oleh :", 1, "L");

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

        $h = 5;
        $l_max = 200;

        $x = $this->GetX();
        $y = $this->GetY();
        $y_tanggal = $y;
        
        foreach ($this->data->transaksiSumberdana as $key => $value) {
            $akun = App\Perkiraan::find($value->ke_akun);
            $this->SetXY($x+15, $y);
            $this->setFont('calibri','',9);

            $l_nama = strlen(format_akun_perkiraan($akun));
            $h_nama = ceil($l_nama/$l_max);
            if($l_nama>$l_max){
                $h = $h*$h_nama;
            }

            if ($l_nama <= 36) {
                $h_akun = 5;
            } else if ($l_nama < 60) {
                $h_akun = 8;
            } else if ($l_nama >= 60) {
                $h_akun = 12;
            }

            // $this->MultiCell(52,$h/$h_nama,format_akun_perkiraan($akun),1,'L');
            $this->SetWidths(array(52));
            $this->SetAligns(array("L"));
            $this->Row(array(format_akun_perkiraan($akun)));

            // $this->SetXY($x+67, $y+$height_rek);
            $this->SetXY($x+67, $y);
            $this->MultiCell(25, $h_akun, $arr_tipe[$value->tipe_id_akun], 1, "C");

            $height_rek += $h_akun;
            $height_tgl += $h_akun;
            $height_posting += $h_akun;

            $x = $this->GetX();
            $y = $this->GetY();
        }

        $this->SetXY($x+15, $y);
        $this->SetWidths(array(52));
        $this->SetAligns(array("L"));
        $this->Row(array(format_akun_perkiraan($this->data->perkiraanAkun)));

        $l_nama_k = strlen(format_akun_perkiraan($this->data->perkiraanAkun));
        $h_nama_k = ceil($l_nama_k/$l_max);

        if ($l_nama_k <= 36) {
            $h_akun_k = 5;
        } else if ($l_nama_k < 60) {
            $h_akun_k = 10;
        } else if ($l_nama_k >= 60) {
            $h_akun_k = 15;
        }

        $this->SetXY($x+67, $y);
        $this->MultiCell(25, $h_akun_k, $arr_tipe[$this->data->tipe_id_akun], 1, "C");

        $y_catatan = $this->getY();

        $this->SetXY($x, $y_rekening);
        $this->MultiCell(15, $height_posting+$h_akun_k+5, "POSTING", 1, "C");

        $y_tgl = $y_rekening+5;
        $this->SetXY($x+92, $y_tanggal);
        // $this->MultiCell(40, $height_tgl+$h_akun_k, $this->tanggal_cetak, 1, "L");
        $this->drawTextBox2('Tgl : '.$this->tanggal_cetak, 40, $height_tgl+$h_akun_k, 'L', 'T');


        $this->SetXY($x, $y_catatan);
        // $this->Cell(40, 30, "Catatan :", 1, 0, "T");
        $this->drawTextBox2('Catatan :', 40, 24, 'L', 'T');

        $y_head_signature = $y_catatan;

        $this->SetXY($x+40, $y_head_signature);
        $this->MultiCell(23, 8, "Pembukuan", 1, "C");

        $this->SetXY($x+63, $y_head_signature);
        $this->MultiCell(23, 8, "Mengetahui", 1, "C");

        $this->SetXY($x+86, $y_head_signature);
        $this->MultiCell(23, 8, "Menyetujui", 1, "C");

        $this->SetXY($x+109, $y_head_signature);
        $this->MultiCell(23, 8, "Penerima", 1, "C");

        $y_body_signature = $this->getY();

        $this->SetXY($x+40, $y_body_signature);
        $this->MultiCell(23, 16, "", 1, "C");

        $this->SetXY($x+63, $y_body_signature);
        $this->MultiCell(23, 16, "", 1, "C");

        $this->SetXY($x+86, $y_body_signature);
        $this->MultiCell(23, 16, "", 1, "C");

        $this->SetXY($x+109, $y_body_signature);
        $this->MultiCell(23, 16, "", 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->bukti 		= $data['bukti'];

$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");

?>CetakBuktiKecil.blade.php