<?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, "Penyedia", 0, 0, "L");
                $this->cell(5, 5, ":", 0, 0, "L");
                $this->MultiCell(100, 5, $this->penyedia->nama, "FJ");

                $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*10/100, 
                    $this->paper_h*15/100, 
                    $this->paper_h*25/100, 
                    $this->paper_h*12/100, 
                    $this->paper_h*12/100, 
                    $this->paper_h*12/100
                ));
                $this->SetAligns(array("C", "C", "C", "C", "C", "C", "C"));
                $this->setFont('Arial','B',10);
                $this->Row(array(
                    "No", 
                    "Tanggal", 
                    "No. Bukti", 
                    "Keterangan", 
                    "Pembelian", 
                    "Pembayaran", 
                    "Saldo"
                ));

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

        $total = 0;
        $total_pembelian = 0;
        $total_pembayaran = 0;
        $saldo = 0;
        $no = 1;
        if(sizeof($this->saldo_awal) > 0){
            $saldo_awal = $this->saldo_awal;
            $saldo_awal_keterangan = $saldo_awal->keterangan;
            $saldo = $saldo_awal->nominal_netto-$saldo_awal->total_bayar;
        }else{
            $saldo_awal_keterangan = 'SALDO AWAL';
            $saldo = 0;
        }

        $this->Row(array($no, "", "", $saldo_awal_keterangan, "", "", rupiah2($saldo, 2)));
        $no++;

        $sub_saldo = $saldo;
        if(count($this->data) > 0){
            foreach($this->data as $d){
                if($d->no_bukti=='0' || $d->no_bukti==null){
                    continue;
                }
                $sub_saldo += $d->pembelian-$d->pembayaran;
                // $saldo = $sub_saldo;
                $this->Row_bawahputus(array(
                    $no, 
                    tgl_full($d->tanggal_bukti, ''), 
                    $d->no_bukti, 
                    $d->keterangan_bukti, 
                    rupiah2($d->pembelian, 2), 
                    rupiah2($d->pembayaran, 2), 
                    rupiah2($sub_saldo, 2)
                ));

                $no++;
                $total_pembelian += $d->pembelian;
                $total_pembayaran += $d->pembayaran;
                // $total += $saldo;
            }
        }
        $saldo_hutang = $sub_saldo;

        $this->setFont('Arial','B',10);
        $this->SetWidths(array(
            $this->paper_h*55/100, 
            $this->paper_h*12/100,
            $this->paper_h*12/100,
            $this->paper_h*12/100
        ));

        $this->SetAligns(array("R", "R", "R", "R"));
        $this->Row(array(
            "Jumlah",
            rupiah2($total_pembelian, 2), 
            rupiah2($total_pembayaran, 2), 
            rupiah2($saldo_hutang, 2)
        ));

        $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']);

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

        $this->setX($this->paper_h*60/100);
        $this->cell(50, 15, "", "BLR", 0, "C");
        $this->cell(50, 15, "", "BLR", 1, "C");   */
        // end : tanda tangan

        
	}
	
	function Footer()
	{
        $this->SetY(-15);
        $this->SetFont('Arial','',10);
        $this->Cell(0,10, $this->PageNo(),0,0,'R');
		
	}
}
$Pdf = new Pdf();
$Pdf->judul=$data['judul'];
$Pdf->periode=$data['periode'];
$Pdf->saldo_awal=$data['saldo_awal'];
$Pdf->data=$data['data'];
$Pdf->penyedia=$data['penyedia'];
$count = (count($data['data']) > 0) ? $data['data'][0]->nama_penyedia.' ('.$data['data'][0]->pic.')':"";
$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");

?>