<?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
    public $paper_w = 210, $paper_h = 297;

	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->paper_w , $this->paper_h));
    }

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

            $y = $this->getY();
            $this->setFont('Tahoma','',9);
            $this->cell(20, 5, "Pegawai", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->pegawai, "FJ");

            $this->cell(20, 5, "Periode", 0, 0, "L");
            $this->cell(5, 5, ":", 0, 0, "L");
            $this->MultiCell(100, 5, $this->periode, "FJ");

            $this->Ln();

            $this->setFont('Tahoma','B',9);

            $this->setWidths([
                    $this->paper_w*4/100,
                    $this->paper_w*9/100,
                    $this->paper_w*39/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                ]);
            $this->setAligns([
                "C",
                "C",
                "C",
                "C",
                "C",
                "C",
            ]);
            $this->Row([
                    "No",
                    "Tanggal",
                    "Keterangan",
                    "Kas Bon",
                    "Pembayaran",
                    "Saldo",
                ], 8);
        }
	}
	
	function Content()
	{           
        $this->setFont('Tahoma','',9);

        $total = 0;
        $total_kasbon = 0;
        $total_pembayaran = 0;
        $saldo = 0;
        $no = 1;
        if($this->saldo_awal != null){
            // $saldo_awal = $this->saldo_awal;
            $saldo_awal_keterangan = "SALDO AWAL";
            // $saldo = ($saldo_awal->total_retur != null) ? ($saldo_awal->nominal_netto-$saldo_awal->total_bayar)-$saldo_awal->total_retur : $saldo_awal->nominal_netto-$saldo_awal->total_bayar;
            $saldo = $this->saldo_awal;
        }else{
            $saldo_awal_keterangan = "SALDO AWAL";
            $saldo = 0;
        }

            $this->setWidths([
                    $this->paper_w*4/100,
                    $this->paper_w*9/100,
                    $this->paper_w*39/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                ]);
            $this->setAligns([
                "C",
                "L",
                "L",
                "R",
                "R",
                "R",
            ]);
            $this->Row_bawahputus([
                    $no,
                    "",
                    $saldo_awal_keterangan,
                    "",
                    "",
                    setNominal($saldo, 0),
                ], 5, ['valign'=>'M']);
            $no++;

        $sub_saldo = $saldo;

        if (count($this->data) > 0) {
            foreach ($this->data as $key => $d) {
                $this->setWidths([
                        $this->paper_w*4/100,
                        $this->paper_w*9/100,
                        $this->paper_w*39/100,
                        $this->paper_w*13/100,
                        $this->paper_w*13/100,
                        $this->paper_w*13/100,
                    ]);
                $this->setAligns(["C", "L", "L", "R", "R", "R"]);

                if ($d->tipe_akun == "D") {
                	$sub_saldo += $d->nominal;
                    $kasbon = $d->nominal;
                    $pembayaran = 0;
                } else {
                    // $kasbon = abs($d->nominal_total) * -1;
                    $kasbon = 0;
                    $pembayaran = $d->nominal;
                    $sub_saldo -= $d->nominal;
                }

                $this->Row_bawahputus([
                        $no, 
                        tgl_full($d->tanggal_transaksi, ''),  
                        $d->keterangan,
                        setNominal($kasbon, 0), 
                        setNominal($pembayaran, 0), 
                        setNominal($sub_saldo, 0),
                    ], 5, ['valign'=>'M']);

                $no++;

                $total_kasbon += $kasbon;
                $total_pembayaran += $pembayaran;
            }
        }
        
        $saldo_piutang = $sub_saldo;

        $this->setFont('Tahoma','B',9);
        $this->setWidths([
                $this->paper_w*52/100,
                $this->paper_w*13/100,
                $this->paper_w*13/100,
                $this->paper_w*13/100,
            ]);
        $this->setAligns([
            "R",
            "R",
            "R",
            "R",
        ]);
        $this->Row([
                "Jumlah",
                $this->setNominal($total_kasbon),
                $this->setNominal($total_pembayaran),
                $this->setNominal($saldo_piutang),
            ], 8);

        $this->setFont('Tahoma','',9);
        $this->setFillColor(255,255,255);
        $this->setTextColor(0,0,0);
        $this->ln();

        $this->setX($this->paper_w*45/100);
        $this->cell(50, 5, "Mengetahui", "TLR", 0, "C");
        $this->cell(50, 5, "Dilaporkan Oleh : ", "TLR", 1, "C");

        $this->setX($this->paper_w*45/100);
        $this->cell(50, 15, "", "BLR", 0, "C");
        $this->cell(50, 15, "", "BLR", 1, "C");
	}
	
	function Footer()
	{
        //
	}

    function setNominal($value=0)
    {
        if($value < 0){
            $set = "( ".rupiah2(abs($value))." )";
        }else{
            $set = rupiah2($value);
        }
        return $set;
    }
}

$Pdf = new Pdf();
$Pdf->judul		= $data['judul'];
$Pdf->periode 	= $data['periode'];
$Pdf->saldo_awal= $data['saldo_awal'];
$Pdf->data      = $data['data'];
$Pdf->pegawai 	= (!is_null($data['pegawai']->nomor_pegawai)) ? $data['pegawai']->nomor_pegawai.' - '.$data['pegawai']->nama_pegawai.' ('.$data['pegawai']->nama_departement.')' : $data['pegawai']->nama_pegawai.' ('.$data['pegawai']->nama_departement.')';

$Pdf->AddFont("Tahoma","","tahoma.php");
$Pdf->AddFont("Tahoma","B","tahomabd.php");
$Pdf->SetAutoPageBreak(true ,15);
$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Tahoma','',12);
$Pdf->Content();
$Pdf->SetTitle($data['judul']);
$Pdf->Output($data['judul'].".pdf", "I");

?>