 <?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, "", 1, "C");

            $akun=App\Perkiraan::where([['id', '=', $this->data->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->no_transaksi_bayar, 1, "L");

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

            $this->SetXY($x+87, $y+8);
            $this->MultiCell(45, 8, "Tanggal : ".date_db($this->data->tanggal_bayar, 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->transaksi as $key => $value) {
            $uraian = "Pembayaran Tagihan ".$this->sewa->nama." untuk faktur tgl ".date_db($value->tanggal_faktur, true)." - ".$value->no_faktur;
            $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 <= 60) {
                $h_jumlah = 5;
            } else if ($str_nama > 60) {
                $h_jumlah = 10;
            }

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

            $this->setFont('calibri','',9);

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

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

            
            $total += $value->jumlah_bayar;

            $y_jumlah += $h_jumlah;
        }

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

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

        $str_terbilang = strlen(terbilang($total));

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

        $this->MultiCell(15, $h_terbilang, "Terbilang", 1, "C");

        $y_terbilang = $y_jumlah+5;

        $this->SetXY($x+15, $y+$y_terbilang);

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

        $y_rekening = $y_terbilang+10;

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

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

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

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

            $this->SetWidths(array(52));
            $this->SetAligns(array("L"));
            $this->Row(array(format_akun_perkiraan($value)));

            $this->SetXY($x+67, $y+$height_rek);
            $this->MultiCell(25, $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) {
            $this->SetXY($x+15, $y+$height_rek);
            $this->setFont('calibri','',9);

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

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

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

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

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

        $y_tgl = $y_rekening+5;
        $this->SetXY($x+92, $y+$y_tgl);
        $this->drawTextBox2('Tgl : '.$this->tanggal_cetak, 40, $height_tgl, 'L', 'T');

        $y_catatan = $y_tgl+$height_posting;

        $this->SetXY($x, $y+$y_catatan);
        $this->drawTextBox2('Catatan :', 40, 24, 'L', 'T');

        $y_head_signature = $y_tgl+$height_tgl;

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

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

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

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

        $y_body_signature = $y_head_signature+8;

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

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

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

        $this->SetXY($x+109, $y+$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->transaksi     = $data['transaksi'];
$Pdf->sewa  		= $data['sewa'];
$Pdf->posting       = $data['posting'];
$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']->no_transaksi_bayar);
$Pdf->Output("Cetak Bukti Bayar - ".$data['detail_data']->no_transaksi_bayar.".pdf", "I");

?>