<?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, "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->cell($this->paper_w*4/100, 5, "No", 1, 0,"C");
                $this->cell($this->paper_w*24/100, 5, "Pelanggan", 1, 0,"C");
		        $this->cell($this->paper_w*16/100, 5, "Saldo Awal", 1, 0,"C");
		        $this->cell($this->paper_w*16/100, 5,"Penjualan", 1, 0,"C");
		        $this->cell($this->paper_w*16/100, 5, "Pembayaran", 1, 0,"C");
		        $this->cell($this->paper_w*16/100, 5, "Saldo", 1, 1,"C");
            }
	}
	
	function Content()
	{           
        $this->setFont('Tahoma','',9);

        $total = 0;
        $total_saldo_awal = 0;
        $total_penjualan = 0;
        $total_pembayaran = 0;
        $no = 1;

        $sub_saldo = 0;
        if(count($this->data) > 0){
            foreach($this->data as $d){
                $penjualan = ($d->total_retur_tr != "0") ? ($d->penjualan-$d->total_retur_tr) : $d->penjualan;

                $saldo_awal = ($d->saldo_awal-$d->last_retur_sa)+$d->total_retur_tagihan_sa-$d->saldo_awal_peny_piutang;
                $pembayaran = ($d->pembayaran-$d->total_retur_tagihan_tr)+$d->peny_piutang;
                $sub_saldo = $saldo_awal+$penjualan-$pembayaran;

                if ($saldo_awal != 0 || $penjualan != 0 || $pembayaran != 0) {
                    $this->cell($this->paper_w*4/100, 5, $no, 1, 0,"C");
                    $this->cell($this->paper_w*24/100, 5, $d->nama_pelanggan, 1, 0,"L");
                    $this->cell($this->paper_w*16/100, 5, $this->setNominal($saldo_awal), 1, 0,"R");
                    $this->cell($this->paper_w*16/100, 5, $this->setNominal($penjualan), 1, 0,"R");
                    $this->cell($this->paper_w*16/100, 5, $this->setNominal($pembayaran), 1, 0,"R");
                    $this->cell($this->paper_w*16/100, 5, $this->setNominal($sub_saldo), 1, 1,"R");
                    $no++;
                    $total_saldo_awal += $saldo_awal;
                    $total_penjualan += $penjualan;
                    $total_pembayaran += $pembayaran;
                }
            }
        }
        $saldo_hutang = $total_saldo_awal+$total_penjualan-$total_pembayaran;
        $this->setFont('Tahoma','B',9);
        
        $this->cell($this->paper_w*28/100, 5, "Jumlah", 1, 0,"R");
        $this->cell($this->paper_w*16/100, 5, $this->setNominal($total_saldo_awal), 1, 0,"R");
        $this->cell($this->paper_w*16/100, 5, $this->setNominal($total_penjualan), 1, 0,"R");
        $this->cell($this->paper_w*16/100, 5, $this->setNominal($total_pembayaran), 1, 0,"R");
        $this->cell($this->paper_w*16/100, 5, $this->setNominal($saldo_hutang), 1, 1,"R");

        $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->data      = $data['data'];

$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");

?>