<?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',11);
            $this->setFillColor(255,255,255);
            $this->setTextColor(0,0,0);
            
            $this->cell(193, 5, strtoupper($this->judul), 0, 1, "C");

            $this->setFont('Arial','',11);
            $this->cell(193, 5, strtoupper(strtolower("Bulan : ".$this->periode)), 0, 1, "C");
            $this->Ln();

            $this->setFont('Arial','B',9);
            
            $x = $this->GetX();
            $y = $this->GetY();

            $this->setWidths([
                    $this->paper_w*4/100,
                    $this->paper_w*13/100,
                    $this->paper_w*13/100,
                    $this->paper_w*22/100,
                    $this->paper_w*14/100,
                    $this->paper_w*10/100,
                    $this->paper_w*15/100,
                ]);
            $this->setAligns([
                    "C",
                    "C",
                    "C",
                    "C",
                    "C",
                    "C",
                    "C",
                ]);
            $this->Row_withAligment([
                    "NO",
                    "TANGGAL PENGAMBILAN",
                    "NO. FAKTUR",
                    "NAMA PELANGGAN",
                    "JUMLAH",
                    "TGL JATUH TEMPO",
                    "KETERANGAN",
                ], 5, ['valign'=>"M"]);
        }

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

        $total = 0;
        if(count($this->data) > 0){
            $no = 1;
            foreach($this->data as $d){
                $selisih = (float)$d->piutang-(float)$d->total_bayar;
                if($selisih==0){
                    continue;
                }

                $set_selisih = 0;
                if ($d->mata_uang != 1) {
                    $set_selisih = $selisih*$d->kurs_jual;
                    $total += $set_selisih;
                } else {
                    $set_selisih = $selisih;
                    $total += $set_selisih;
                }

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

                $this->Row_withAligment([
                        $no,
                        date_db($d->tanggal_faktur, true),
                        nomor_penjualan($d),
                        $d->nama_pelanggan,
                        rupiah2($set_selisih),
                        date_db($d->tanggal_tempo, true),
                        $d->keterangan
                    ], 7, ['valign'=>'M']);

                $no++;
                
            }
        }
        $this->setFont('Arial','B',10);
        //$this->SetWidths(array(108, 30, 55));
        //$this->SetAligns(array("R", "R", "L"));
        //$this->Row(array("TOTAL", number_format($total, 2, ',', '.'), ""));
        $this->cell($this->paper_w*52/100,7,"Total",1,0,'R',0);
        $this->cell($this->paper_w*14/100,7,number_format($total, 0, ',', '.'),1,0,'R',0);
        $this->cell($this->paper_w*25/100,7,"",1,1,'R',0);
        //$this->cell(55,7,"",1,0,'L',0);
        // $this->Ln();

        $set_y = $this->getY();

        if($set_y > 270){
            $this->AddPage();
            $this->Ln();
            $this->setFont('Arial','',9);
            $this->drawTextBox2('Mengetahui,', 65, 25, 'C', 'T');

            $this->setXY(131, $this->getY()-5);
            $this->setFont('Arial','',9);
            $this->drawTextBox2('Dibuat Oleh :', 65, 25, 'C', 'T');
        } else {
            $this->setY($set_y+5);
            $this->setFont('Arial','',9);
            // $this->cell(70, 5, "Mengetahui,", "LRT", 1, "C");
            // $this->cell(70, 20, "", "LRB", 0, "C");
            $this->drawTextBox2('Mengetahui,', 65, 25, 'C', 'T');

            $this->setXY(131, $set_y+5);
            $this->setFont('Arial','',9);
            // $this->cell(70, 5, "Dibuat Oleh :", "LRT", 1, "C");
            // $this->setX(131);
            // $this->cell(70, 20, "", "LRB", 0, "C");
            $this->drawTextBox2('Dibuat Oleh :', 65, 25, 'C', 'T');
        }


	}
	
	function Footer()
	{
		
	}
}
$Pdf = new Pdf();
$Pdf->judul=$data['judul'];
$Pdf->periode=$data['periode'];
$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("Laporan Jatuh Tempo Penjualan ".$data['periode']);
$Pdf->AutoPrint(false);
$Pdf->Output("Laporan Jatuh Tempo Penjualan periode".$data['periode'].".pdf", "I");

?>