<?php
class Pdf extends PDF_MC_Table{
	//Page header
	public $paper_w = 210, $paper_h = 297;
    function __construct() {
        //set Page 
        parent::__construct('L','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',11);

            $this->SetWidths([
                $this->paper_h*10/100, 
                $this->paper_h*40/100, 
                $this->paper_h*14/100,
                $this->paper_h*14/100,
                $this->paper_h*15/100
            ]);
            $this->SetAligns(["C", "C", "C", "C", "C"]);
            $this->Row(["Tanggal", "Keterangan", "Masuk", "Keluar", "Sisa"], 7);
        }
        $this->SetAligns(["L", "L", "R", "R", "R"]);
	}
	
	function Content()
	{
        $x = $this->GetX();
        $y = $this->GetY();

        $sisa = 0;
        $total_masuk = 0;
        $total_keluar = 0;

        $this->setFont('Arial','',10);
        $this->SetWidths([
            $this->paper_h*10/100, 
            $this->paper_h*40/100, 
            $this->paper_h*14/100,
            $this->paper_h*14/100,
            $this->paper_h*15/100
        ]);
        $this->SetAligns(["L", "L", "R", "R", "R"]);
        $this->Row([
            "",
            "SALDO AWAL",
            "",
            "",
            setNominal($this->saldo_awal, 2)
        ]);

        if(count($this->data) > 0) {
            foreach($this->data AS $key=>$value){
                $nominal = ["D"=>0, "K"=>0];
                if($value->tipe_id_akun=="D"){
                    $nominal["D"] += $value->nominal_transaksi;
                    $total_masuk += $value->nominal_transaksi;
                    $this->saldo_awal += $value->nominal_transaksi;
                }else{
                    $nominal["K"] += $value->nominal_transaksi;
                    $total_keluar += $value->nominal_transaksi;
                    $this->saldo_awal -= $value->nominal_transaksi;
                }

                $selisih = $nominal['D']-$nominal['K']; 

                $sisa += $selisih;

                $this->setFont('Arial','',10);
                $this->SetWidths([
                    $this->paper_h*10/100, 
                    $this->paper_h*40/100, 
                    $this->paper_h*14/100,
                    $this->paper_h*14/100,
                    $this->paper_h*15/100
                ]);
                $this->SetAligns(["L", "L", "R", "R", "R"]);
                $this->Row([
                    date_db($value->tanggal_transaksi, true),
                    $value->keterangan,
                    rupiah2($nominal['D'], 2),
                    rupiah2($nominal['K'], 2),
                    setNominal($this->saldo_awal, 2)
                ]);
            }
        }

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

        $this->SetWidths([
            $this->paper_h*50/100,
            $this->paper_h*14/100,
            $this->paper_h*14/100,
            $this->paper_h*15/100
        ]);
        $this->SetAligns(["R", "R", "R", "R"]);
        $this->Row(["Total", setNominal($total_masuk, 2), setNominal($total_keluar, 2), setNominal($this->saldo_awal, 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");

?>