<?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*24/100, 
                $this->paper_h*24/100, 
                $this->paper_h*12/100,
                $this->paper_h*12/100,
                $this->paper_h*12/100
            ]);
            $this->SetAligns(["C", "C", "C", "C", "C", "C"]);
            $this->Row(["Tanggal", "Akun", "Keterangan", "Masuk", "Keluar", "Sisa"], 7);
        }
        $this->SetAligns(["L", "L", "L", "R", "R", "R"]);
	}
	
	function Content()
	{
        $x = $this->GetX();
        $y = $this->GetY();

        if(count($this->data) > 0) {
            $sisa = 0;
            foreach($this->data AS $key=>$value){
                $sub_total = 0;
                if (count($value) > 0) {
                    foreach ($value AS $key2=>$value2) {
                        if (count($value2['transaksi_sumberdana']) > 0) {
                            foreach ($value2['transaksi_sumberdana'] AS $key3=>$value3) {
                                $nominal = ["D"=>0, "K"=>0];
                                if($value3['tipe_id_akun']=="D"){
                                    $nominal["K"] += $value3['nominal_transaksi'];
                                }else{
                                    $nominal["D"] += $value3['nominal_transaksi'];
                                }

                                $selisih = $value2['tipe_id_akun']=="D" ? $nominal['D']-$nominal['K'] : $nominal['K']-$nominal['D']; 

                                $sisa += $selisih;
                                $sub_total += $selisih;
                                $akun = \App\Perkiraan::find($value3['ke_akun']);

                                $this->setFont('Arial','',10);
                                $this->SetWidths([
                                    $this->paper_h*10/100, 
                                    $this->paper_h*24/100, 
                                    $this->paper_h*24/100, 
                                    $this->paper_h*12/100,
                                    $this->paper_h*12/100,
                                    $this->paper_h*12/100
                                ]);
                                $this->SetAligns(["L", "L", "L", "R", "R", "R"]);
                                $this->Row([
                                    date_db($value2['tanggal_transaksi'], true), 
                                    format_akun_perkiraan($akun), 
                                    $value3['keterangan'],
                                    rupiah2($nominal['D']),
                                    rupiah2($nominal['K']),
                                    rupiah2($sisa)
                                ]);
                            }
                        }
                    }
                }
            }
        }
	}
	
	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->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");

?>