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

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

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

            $dataKasbonBayar = $this->data->detailTransaksi()
                    ->groupBy("id_transaksi_bayar")
                    ->with(['dataPerkiraan', 'dataKasbonRekanan'])
                    ->first();

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

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

            $this->SetXY($x+90, $y);
            $this->setFont('calibri','',$this->font_content);
            $this->MultiCell(40, 8, "Nomor : ".$this->data->no_transaksi_bayar, 1, "L");

            $kasbonRekanan = $dataKasbonBayar
                    ->dataKasbonRekanan()
                    ->with(['dataRekanan', 'dataKelompokRekanan'])
                    ->first();

            $set_penerima = "";
            if($kasbonRekanan->dataRekanan()->count() > 0){
                $set_penerima = $kasbonRekanan->dataRekanan->nama;
            }else if($kasbonRekanan->dataKelompokRekanan()->count() > 0){
                $set_penerima = $kasbonRekanan->dataKelompokRekanan->nama;
            }

            $this->setFont('calibri','',$this->font_content);
            $this->MultiCell(90, 8, "Diterima dari :"."\n".$set_penerima, 1, "L");

            $this->SetXY($x+90, $y+8);
            $this->setFont('calibri','',$this->font_content);
            $this->MultiCell(40, 8, "Tanggal : ".date_db($this->data->tanggal_bayar, true), 1, "L");

            $this->SetXY($x+90, $y+16);
            $this->setFont('calibri','',$this->font_content);
            $this->MultiCell(40, 8, "Lampiran : ", 1, "L");

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

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

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

        $detail = $this->data->detailTransaksi()
                ->with(['dataKasbonRekanan'])
                ->get()
                ->sortByDesc('tanggal_transaksi');

        foreach ($detail as $key => $value2) {
            $rekanan = $value2
                    ->dataKasbonRekanan()
                    ->with(['dataRekanan', 'dataKelompokRekanan'])
                    ->first();

            $set_penerima = "";
            if($rekanan->dataRekanan()->count() > 0){
                $set_penerima = $rekanan->dataRekanan->nama;
            }else if($rekanan->dataKelompokRekanan()->count() > 0){
                $set_penerima = $rekanan->dataKelompokRekanan->nama;
            }

            $keterangan = "Kasbon ".$set_penerima." Tgl. ".date_db($value2->dataKasbonRekanan->tanggal_transaksi, true);
            $str_nama = strlen($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($value2->tipe_id_akun == 'K'){
                $set_total = "( Rp".rupiah2($value2->jum_bayar, 2)." )";
            }else{
                $set_total = "Rp".rupiah2($value2->jum_bayar, 2);
            }

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

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

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

            $y_jumlah += $h_jumlah;
        }

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

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

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

        $y_terbilang = $y_jumlah+5;

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

        $y_rekening = $y_terbilang+8;

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

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

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

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

        $h = 5;
        $l_max = 200;

        $detailTransaksi = $this->data->detailTransaksi()->groupBy("id_transaksi_bayar")->first();
        $perkiraan = $detailTransaksi->with(['dataPerkiraan'])->first();
        $this->SetXY($x+20, $y+$height_rek);
        // $this->MultiCell(100,$h/$h_nama,format_akun_perkiraan($this->data->perkiraanAkun),1,'L');
        $this->SetWidths(array(60));
        $this->SetAligns(array("L"));
        $this->Row(array(format_akun_perkiraan($perkiraan->dataPerkiraan)));

        $l_nama_k = strlen(format_akun_perkiraan($perkiraan->dataPerkiraan));
        $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+80, $y+$height_rek);
        $this->MultiCell(20, $h_akun_k, $arr_tipe[$detailTransaksi->tipe_id_akun], 1, "C");
        
        // $this->SetXY($x+20, $y+$height_rek);
        $height_rek = $h_akun_k;

        foreach ($detail as $key => $value2) {
            $rekanan = $value2
                    ->dataKasbonRekanan()
                    ->with(['dataRekanan', 'dataKelompokRekanan'])
                    ->first();

            $id_akun_piutang = 0;
            if($rekanan->dataRekanan()->count() > 0){
                $d = $rekanan->dataRekanan()->with(['dataKelompok'])->first();
                $id_akun_piutang = $d->dataKelompok->id_akun_piutang;

            }else if($rekanan->dataKelompokRekanan()->count() > 0){
                $id_akun_piutang = $rekanan->dataKelompokRekanan->id_akun_piutang;

            }

            $akun = App\Perkiraan::find($id_akun_piutang);

            $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;
            }

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

            $this->SetXY($x+80, $set_y_row);
            $this->MultiCell(20, $h_akun, $arr_tipe[$value2->dataKasbonRekanan->tipe_dari_akun], 1, "C");

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

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

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

        // $y_tgl = 200;
        $y_catatan = $y_tgl+$height_posting+5;

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

        $y_head_signature = $y_tgl+$height_posting;

        $this->SetXY($x+50, $y+$y_head_signature+5);
        $this->setFont('calibri','',$this->font_content);
        $this->MultiCell(20, 6, "Pembukuan", 1, "C");

        $this->SetXY($x+70, $y+$y_head_signature+5);
        $this->setFont('calibri','',$this->font_content);
        $this->MultiCell(20, 6, "Mengetahui", 1, "C");

        $this->SetXY($x+90, $y+$y_head_signature+5);
        $this->setFont('calibri','',$this->font_content);
        $this->MultiCell(20, 6, "Menyetujui", 1, "C");

        $this->SetXY($x+110, $y+$y_head_signature+5);
        $this->setFont('calibri','',$this->font_content);
        $this->MultiCell(20, 6, "Penerima", 1, "C");

        $y_body_signature = $y_head_signature+8;

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

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

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

        $this->SetXY($x+110, $y+$y_body_signature+3);
        $this->setFont('calibri','',11);
        $this->MultiCell(20, 15, "", 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['transaksi_bayar'];
$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($data['judul']);
$Pdf->Output($data['judul'].".pdf", "I");

?>