<?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('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->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('Arial','',10);
                $this->cell(20, 5, "Periode", 0, 0, "L");
                $this->cell(5, 5, ":", 0, 0, "L");
                $this->MultiCell(100, 5, $this->periode, "FJ");

                // $this->setXY(115, $y);
                // $this->MultiCell(85, 5, (count($this->data) > 0) ? $this->data[0]->nama.' - '.$this->data[0]->pic:"", 0, "R");

                $this->Ln();

                $this->SetWidths(array(
                    $this->paper_h*5/100, 
                    $this->paper_h*22/100, 
                    $this->paper_h*11/100, 
                    $this->paper_h*11/100, 
                    $this->paper_h*11/100, 
                    $this->paper_h*11/100, 
                    $this->paper_h*11/100,
                    $this->paper_h*11/100
                ));
                $this->SetAligns(array("C", "C", "C", "C", "C", "C", "C", "C"));
                $this->setFont('Arial','B',10);
                $this->Row(array("No", "Pelanggan", "Total", "Belum\nJatuh Tempo", "1-30 Hari", "31-60 Hari", "61-90 Hari", ">90 Hari"));
                // $this->setFont('Arial','',10);
                $this->SetAligns(array("C", "L", "R", "R", "R", "R", "R", "R"));

            }
	}
	
	function Content()
	{
        if(count($this->data) > 0){
            $piutang_total = 0;
            $piutang_0 = 0;
            $piutang_30 = 0;
            $piutang_60 = 0;
            $piutang_90 = 0;
            $piutang_91 = 0;

            $no = 1;
            foreach($this->data as $d){
                if($d->child['piutang_total']==0) continue;

                $this->setFont('Arial','',10);
                $this->SetAligns(array("C", "L", "R", "R", "R", "R", "R", "R"));

                $this->Row_bawahputus([
                        $no,
                        $d->nama,
                        $this->setNominal($d->child['piutang_total']),
                        $this->setNominal($d->child['piutang_0']),
                        $this->setNominal($d->child['piutang_30']),
                        $this->setNominal($d->child['piutang_60']),
                        $this->setNominal($d->child['piutang_90']),
                        $this->setNominal($d->child['piutang_91'])
                    ]);

                $piutang_total += $d->child['piutang_total'];
                $piutang_0 += $d->child['piutang_0'];
                $piutang_30 += $d->child['piutang_30'];
                $piutang_60 += $d->child['piutang_60'];
                $piutang_90 += $d->child['piutang_90'];
                $piutang_91 += $d->child['piutang_91'];

                $no++;
            }

            $this->setFont('Arial','B',10);
            // $this->SetWidths([
            //         $this->paper_h*27/100, 
            //         $this->paper_h*11/100, 
            //         $this->paper_h*11/100, 
            //         $this->paper_h*11/100, 
            //         $this->paper_h*11/100, 
            //         $this->paper_h*11/100,
            //         $this->paper_h*11/100
            //     ]);
            // $this->SetAligns(array("R", "R", "R", "R", "R", "R", "R"));
            // $this->Row([
            //         "Total Hutang",
            //         $this->setNominal($piutang_total),
            //         $this->setNominal($piutang_0),
            //         $this->setNominal($piutang_30),
            //         $this->setNominal($piutang_60),
            //         $this->setNominal($piutang_90),
            //         $this->setNominal($piutang_91),
            //     ]);

            $this->cell($this->paper_h*27/100, 7, "Total Hutang", 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_total), 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_0), 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_30), 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_60), 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_90), 1, 0,"R");
            $this->cell($this->paper_h*11/100, 7, $this->setNominal($piutang_91), 1, 1,"R");
        }

        $this->setFont('Arial','',10);
        $this->setFillColor(255,255,255);
        $this->setTextColor(0,0,0);

        // tanda tangan
        if($this->getY() > 155){
            $this->AddPage();
            $this->ln();   
        }else{
            $this->ln();
        }

        $this->setX(170);
        $this->SetWidths([
                $this->paper_h*18/100,
                $this->paper_h*18/100
            ]);
        $this->SetAligns(['C', 'C']);
        $this->Row_withAligment(['Mengetahui', 'Dilaporkan Oleh :'], 25, ['valign'=>'T']);
        // end : tanda tangan

	}
	
	function Footer()
	{
        $this->SetY(-15);
        $this->SetFont('Arial','',10);
        $this->Cell(0, 10, $this->PageNo(), 0, 0, 'R');
		
	}

    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->data=$data['data'];
$Pdf->rtp=$data['rtp'];
$Pdf->SetAutoPageBreak(true ,15);

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

?>