<?php
class Pdf extends PDF_MC_Table{
	//Page header
	public $paper_w = 210, $paper_h = 297;
    function __construct() {
        //set Page 
        parent::__construct('P','mm',array($this->paper_w ,$this->paper_h));
        /*$this->AddFont("tahoma","","tahoma.php");
        $this->AddFont("tahoma","B","tahomabd.php");*/
    }

	function Header(){
        
		if($this->PageNo() != 0){		
            $this->setFont('Arial','B',12);
            
            $this->cell(100, 5, $this->judul, 0, 0, "L");
            $this->Ln();

            $this->setFont('Arial','',10);
            $this->cell(25, 5, "Akun", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->akun, "FJ");
            // $this->Ln();
            $this->cell(25, 5, "Periode", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->periode, "FJ");

            $this->Ln();

            $this->setFont('Arial','B',10);

            $this->SetWidths([
	            $this->paper_h*7/100, 
	            $this->paper_h*7/100, 
	            $this->paper_h*18/100, 
	            $this->paper_h*11/100,
	            $this->paper_h*11/100,
	            $this->paper_h*11/100
	        ]);
            $this->SetAligns(["C", "C", "C", "C", "C", "C"]);
            $this->Row_atasbawah(["Tanggal", "No. Bukti", "Keterangan", "Debet", "Kredit", "Saldo"], 7);
        }
        $this->SetAligns(["L", "L", "L", "R", "R", "R"]);
        $this->setFont('Arial','',8);
        $this->SetWidths([
            $this->paper_h*7/100, 
            $this->paper_h*7/100, 
            $this->paper_h*18/100, 
            $this->paper_h*11/100,
            $this->paper_h*11/100,
            $this->paper_h*11/100
        ]);
	}
	
	function Content()
	{
        $x = $this->GetX();
        $y = $this->GetY();

        $saldo = $this->saldo_awal;
        $jml_debet = 0;
        $jml_kredit = 0;

        $this->setFont('Arial','B',9);
        $this->SetWidths([
            $this->paper_h*7/100, 
            $this->paper_h*7/100, 
            $this->paper_h*18/100, 
            $this->paper_h*11/100,
            $this->paper_h*11/100,
            $this->paper_h*11/100
        ]);
        $this->SetAligns(["L", "L", "L", "R", "R", "R"]);
        $this->Row_atasbawah([
            "",
            "Saldo Awal",
            "",
            "",
            "",
            setNominal($this->saldo_awal, 2)
        ], 7);

        $sub_saldo = $saldo;
        if(count($this->data) > 0) {
            foreach($this->data AS $key=>$value){
            	if ($value->tipe_transaksi == "D") {
            		$sub_saldo += $value->nominal;
                    $jml_debet += $value->nominal;
            	} else if ($value->tipe_transaksi == "K") {
            		$sub_saldo -= $value->nominal;
                    $jml_kredit += $value->nominal;
            	}

            	$this->setFont('Arial','',9);
            	$this->SetWidths([
            	    $this->paper_h*7/100, 
            	    $this->paper_h*7/100, 
            	    $this->paper_h*18/100, 
            	    $this->paper_h*11/100,
            	    $this->paper_h*11/100,
            	    $this->paper_h*11/100
            	]);
            	$this->SetAligns(["L", "L", "L", "R", "R", "R"]);
            	$this->Row_noborder([
            	    $value->tanggal,
            	    $value->no_bukti,
            	    $value->keterangan,
            	    $value->tipe_transaksi == "D" ? setNominal($value->nominal, 2) : '',
            	    $value->tipe_transaksi == "K" ? setNominal($value->nominal, 2) : '',
            	    setNominal($sub_saldo, 2)
            	]);
            }

            $this->setFont('Arial','B',9);
            $this->SetWidths([
	            $this->paper_h*32/100, 
                $this->paper_h*11/100,
                $this->paper_h*11/100,
	            $this->paper_h*11/100
	        ]);
	        $this->SetAligns(["C","R","R","R"]);
            $this->Row_atasbawah([
	            "Saldo Akhir",
                setNominal($jml_debet, 2),
                setNominal($jml_kredit, 2),
	            setNominal($sub_saldo, 2)
	        ], 7);
        }
	}
	
	function Footer()
	{
		$this->SetY(-15);
        $this->SetFont('Arial','',9);
        $this->Cell(0,10, $this->PageNo(),0,0,'C');
	}
}
$Pdf = new Pdf();
$Pdf->judul=$data['judul'];
$Pdf->periode=$data['periode'];
$Pdf->akun=$data['akun'];
$Pdf->data=$data['data'];
$Pdf->saldo_awal=$data['saldo_awal'];

$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("$data[judul] ".$data['periode']);
$Pdf->AutoPrint(false);
$Pdf->Output("$data[judul] ".$data['periode'].".pdf", "I");

?>