<?php
class Pdf extends PDF_MC_Table{
	//Page header
	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('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->setY($y);
            $this->setFont('Arial','B',12);
            $this->cell(0, 5, $this->kategori->nama_kelompok." - ".$this->kategori->nama, 0, 1, "R");

            $this->Ln();

            $this->setFont('Arial','B',10);
            $this->setWidths([
                    $this->paper_w*10/100,
                    $this->paper_w*15/100,
                    $this->paper_w*20/100,
                    $this->paper_w*15/100,
                    $this->paper_w*15/100,
                    $this->paper_w*15/100,
                ]);
            $this->setAligns([
                    "C",
                    "C",
                    "C",
                    "C",
                    "C",
                    "C",
                ]);
            $this->Row([
                    "Tanggal",
                    "No Faktur",
                    "Pelanggan",
                    "Penjualan",
                    "Diskon",
                    "Piutang",
                ], 7);
        }

        $this->setAligns([
                "L",
                "L",
                "L",
                "R",
                "R",
                "R",
            ]);
				
	}
	
	function Content()
	{   
        $this->setFont('Arial','',10);

        $x = $this->GetX();
        $y = $this->GetY();

        $total = 0;
        $total_penjualan = 0;
        $total_diskon = 0;
        $total_piutang = 0;
        if(count($this->data) > 0){
            $no = 1;
            foreach($this->data as $d){
                $set_nominal_penjualan  = 0;
                $set_nominal_potongan   = 0;
                $set_nominal_piutang    = 0;

                $nominal_penjualan  = $d->nominal_jumlah+$d->nominal_pajak;
                $nominal_piutang    = $nominal_penjualan-$d->nominal_potongan;
                
                if ($d->mata_uang != 1) {
                    $set_nominal_penjualan  = $nominal_penjualan*$d->kurs_jual;
                    $set_nominal_potongan   = $d->nominal_potongan*$d->kurs_jual;
                    $set_nominal_piutang    = $nominal_piutang*$d->kurs_jual;

                    $total              += $set_nominal_piutang;
                    $total_penjualan    += $set_nominal_penjualan;
                    $total_diskon       += $set_nominal_potongan;
                    $total_piutang      += $set_nominal_piutang;
                } else {
                    $set_nominal_penjualan  = $nominal_penjualan;
                    $set_nominal_potongan   = $d->nominal_potongan;
                    $set_nominal_piutang    = $nominal_piutang;

                    $total              += $set_nominal_piutang;
                    $total_penjualan    += $set_nominal_penjualan;
                    $total_diskon       += $set_nominal_potongan;
                    $total_piutang      += $set_nominal_piutang;
                }

                $this->setAligns([
                    "L",
                    "L",
                    "L",
                    "R",
                    "R",
                    "R",
                ]);

                $this->Row_withAligment([
                        date_db($d->tanggal_faktur, true),
                        nomor_penjualan($d),
                        $d->nama_pelanggan,
                        rupiah2($set_nominal_penjualan, 0),
                        rupiah2($set_nominal_potongan, 0),
                        rupiah2($set_nominal_piutang, 0),
                    ], 5, ['valign'=>"M"]);

            }
        } else {
        	$this->cell(189,5,"Tidak ada transaksi pada periode ini",1,1,'C',0);
        }
                
        $this->setFont('Arial','B',10);
        $this->cell(94,7,"Jumlah Keseluruhan",1,0,'R',0);
        $this->cell(32,7,rupiah2($total_penjualan, 0),1,0,'R',0);
        $this->cell(31,7,rupiah2($total_diskon, 0),1,0,'R',0);
        $this->cell(32,7,rupiah2($total_piutang, 0),1,1,'R',0);
                

        
	}
	
	function Footer()
	{
		$this->SetY(-15);
        $this->SetFont('Arial','I',9);
        $this->Cell(0,10, "Halaman ".$this->PageNo(),0,0,'R');
	}
}
$Pdf = new Pdf();
$Pdf->judul=$data['judul'];
$Pdf->periode=$data['periode'];
$Pdf->kategori=$data['kategori'];
$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");

?>