 <?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->dataAkunPerkiraan->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->dataTransaksiBayar->no_transaksi_bayar, 1, "L");

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

            $this->SetXY($x+135, $y+10);
            $this->setFont('calibri','',11);
            $this->MultiCell(65, 10, "Tanggal : ".date_db($this->data->tgl_bayar, 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_jumlah = 0;
        $h_uraian = 5;
        $str_max = 200;

        foreach ($this->transaksi->detailBayar as $key => $value) {
            $orderservice_detail = App\OrderService::find($value->id_orderservice);
            $uraian = "Pembayaran Tagihan ".$this->orderservice->dataPenyedia->nama." untuk nota tgl ".date_db($orderservice_detail->tanggal_nota, true)." - ".nomor_orderservice($orderservice_detail);
            $str_nama = strlen($uraian);
            $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->tipe_id_akun == 'K'){
            //     $set_total = "( Rp".rupiah2($value->jum_bayar, 2)." )";
            // }else{
            //     $set_total = "Rp".rupiah2($value->jum_bayar, 2);
            // }

            $set_total = "Rp".rupiah2($value->jum_bayar, 2);

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

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

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

            $y_jumlah += $h_jumlah;
        }

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

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

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

        $y_terbilang = $y_jumlah+5;

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

        $y_rekening = $y_terbilang+10;

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

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

        $this->SetXY($x+150, $y+$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;
        
        foreach ($this->posting['row_debet'] as $key => $value) {

            $l_nama = strlen(format_akun_perkiraan($value));
            $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->SetWidths(array(100));
            $this->SetAligns(array("L"));
            $this->Row(array(format_akun_perkiraan($value)));

            $this->SetXY($x+120, $y+$height_rek);
            $this->MultiCell(30, $h_akun, $arr_tipe[$value->tipe_id_akun_hutang], 1, "C");

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

        foreach ($this->posting['row_kredit'] as $key => $value) {

            $l_nama = strlen(format_akun_perkiraan($value));
            $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->SetWidths(array(100));
            $this->SetAligns(array("L"));
            $this->Row(array(format_akun_perkiraan($value)));

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

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

        // $this->SetXY($x+20, $y+$height_rek);
        // $this->SetWidths(array(100));
        // $this->SetAligns(array("L"));
        // $this->Row(array(format_akun_perkiraan($this->data->dataAkunPerkiraan)));

        // $l_nama_k = strlen(format_akun_perkiraan($this->data->dataAkunPerkiraan));
        // $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+$height_rek);
        // $this->MultiCell(30, $h_akun_k, $arr_tipe[$this->data->tipe_id_akun], 1, "C");

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

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

        $y_catatan = $y_tgl+$height_posting;

        $this->SetXY($x, $y+$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_tgl+$height_posting;

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

        $y_body_signature = $y_head_signature+10;

        $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->transaksi     = $data['transaksi'];
$Pdf->orderservice  = $data['orderservice'];
$Pdf->posting       = $data['posting'];
$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 Bukti Bayar - ".$data['detail_data']->dataTransaksiBayar->no_transaksi_bayar);
$Pdf->Output("Cetak Bukti Bayar - ".$data['detail_data']->dataTransaksiBayar->no_transaksi_bayar.".pdf", "I");

?>