 <?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)." ".$this->bukti, 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($index, $perPage)
	{          
        $x = $this->GetX();
        $y = $this->GetY();

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

        $value = $this->data->transaksiSumberdana->toArray();
        $row_data = $index*$perPage;
        for ($i=$row_data; $i < ($row_data+$perPage); $i++) {
            if(!isset($value[$i])) break;
        // foreach ($this->data->transaksiSumberdana as $key => $value) {

            $str_nama = strlen($value[$i]['keterangan']);
            $h_str = ceil($str_nama/$str_max);

            if($str_nama>$str_max){
                $h_uraian = $h_uraian*$h_str;
            }

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

            if($value[$i]['tipe_id_akun'] == $this->cek_tipe_jenis_transaksi($this->data->jenis_transaksi)){
                $set_total = "( Rp".rupiah2($value[$i]['nominal_transaksi'], 2)." )";
            }else{
                $set_total = "Rp".rupiah2($value[$i]['nominal_transaksi'], 2);
            }

            $this->setFont('calibri','',11);
            // $this->MultiCell(135,$h_uraian/$h_str,$value[$i]['keterangan'],1,'L');
            // $this->SetXY($x, $y+$y_jumlah);
            $this->SetXY($x, $y);
            $this->SetWidths(array(135));
            $this->SetAligns(array("L"));
            $this->Row(array($value[$i]['keterangan']));

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

            if($value[$i]['tipe_id_akun']==$this->cek_tipe_jenis_transaksi($this->data->jenis_transaksi)){
                $total -= $value[$i]['nominal_transaksi'];
            }else{
                $total += $value[$i]['nominal_transaksi'];
            }

            $y_jumlah += $h_jumlah;

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

        }

        $this->MultiCell(135, 5, "Total", 1, "R");

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

        $y_terbilang = $this->GetY();

        $this->SetXY($x, $y_terbilang);
        $this->MultiCell(20, 10, "Terbilang", 1, "C");

        $this->setXY($x+20, $y_terbilang);
        $this->MultiCell(180, 10, terbilang(abs($total)), 1, "L");

        $y_rekening = $this->getY();

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

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

        $this->SetXY($x+150, $y_rekening);
        $this->MultiCell(50, 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 = 0;
        $y_body_signature = 0;

        $h = 5;
        $l_max = 200;

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

        $value = $this->data->transaksiSumberdana->toArray();
        $row_data = $index*$perPage;
        for ($i=$row_data; $i < ($row_data+$perPage); $i++) {
            if(!isset($value[$i])) break;
        // foreach ($this->data->transaksiSumberdana as $key => $value) {

            $akun = App\Perkiraan::find($value[$i]['ke_akun']);

            $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 <= 59) {
                $h_akun = 5;
            } else if ($l_nama > 59) {
                $h_akun = 10;
            }

            // $this->SetXY($x+20, $y+$height_rek);
            $this->SetXY($x+20, $y);
            // $this->MultiCell(100,$h/$h_nama,format_akun_perkiraan($akun).' '.$height_rek,1,'L');
            $this->SetWidths(array(100));
            $this->SetAligns(array("L"));
            $this->Row(array(format_akun_perkiraan($akun)));

            // $this->SetXY($x+120, $y+$height_rek);
            $this->SetXY($x+120, $y);
            $this->MultiCell(30, $h_akun, $arr_tipe[$value[$i]['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+20, $y);
        // $this->MultiCell(100,$h/$h_nama,format_akun_perkiraan($this->data->perkiraanAkun),1,'L');
        $this->SetWidths(array(100));
        $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 <= 59) {
            $h_akun_k = 5;
        } else if ($l_nama_k > 59) {
            $h_akun_k = 10;
        }

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

        $y_catatan = $this->getY();
        
        $this->SetXY($x, $y_rekening);
        $this->setFont('calibri','',11);
        $this->MultiCell(20, $height_posting+10, "POSTING", 1, "C");

        $y_tgl = $y_rekening+5;
        $this->SetXY($x+150, $y_tanggal);
        $this->setFont('calibri','',11);
        // $this->MultiCell(50, $height_tgl+5, $this->tanggal_cetak, 1, "L");
        $this->drawTextBox2('Tgl : '.$this->tanggal_cetak, 50, $height_tgl+5, 'L', 'T');


        $this->SetXY($x, $y_catatan);
        $this->setFont('calibri','',11);
        // $this->Cell(65, 30, "Catatan :", 1, 0, "T");
        $this->drawTextBox2('Catatan :', 65, 30, 'L', 'T');

        $y_head_signature = $y_catatan;

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

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

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

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

        $y_body_signature = $this->getY();

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

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

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

        $this->SetXY($x+170, $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
	}

    function cek_tipe_jenis_transaksi($jenis_transaksi)
    {
        try {
            switch ($jenis_transaksi) {
                case 2:
                    $tipe_detail = 'D';
                    break;
                
                default:
                    $tipe_detail = 'K';
                    break;
            }
        } catch (Exception $e) {
            $tipe_detail = 'K';
        }

        return $tipe_detail;
    }
}

$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->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->SetFont('Calibri','',11);

$rows_count = $data['detail_data']->transaksiSumberdana->count();
$perPage = 20;
$max_count = ceil($rows_count/$perPage);
for ($index=0; $index < $max_count ; $index++) { 
    $Pdf->AddPage();
    $Pdf->Content($index, $perPage);
}
$Pdf->SetTitle("Cetak Transaksi Pengeluaran - ".$data['detail_data']->nomor_transaksi);
$Pdf->Output("Cetak Transaksi Pengeluaran - ".$data['detail_data']->nomor_transaksi.".pdf", "I");

?>