<?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
    public $def_width = 210, $def_height = 330;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->def_width , $this->def_height));
		/*$this->AddFont("tahoma","","tahoma.php");
		$this->AddFont("tahoma","B","tahomabd.php");*/
        $this->AddFont('Arial Regular','','ArialRegular.php');
        $this->AddFont('Arial Regular Bold','B','ArialRegularBold.php');
    }
	function Header(){
        
		if($this->PageNo() == 1){		
                //$this->setFont('Arial Regular Bold','B',11);
                $this->setFont('Courier','B',11);
                $x_kanan = $this->def_width*53/100;
                //$x_kanan = $this->def_width*60/100;
                $h_set = 6;
                $h_yth = 7;
                $this->setXY($x_kanan+5, 22);
                $this->MultiCell($this->def_width*32/100, $h_yth, $this->retur[0]->nama_pelanggan, 0, "FJ");
                
                $y = $this->getY();

                $this->setX($x_kanan+5);
                $this->MultiCell($this->def_width*32/100, $h_yth, $this->retur[0]->alamat_pelanggan, 0, "FJ");

                $this->setX($x_kanan+5);
                $this->MultiCell($this->def_width*32/100, $h_yth, $this->retur[0]->kabupaten, 0, "FJ");

                // $this->setFont('Arial Regular','',16);
                $this->setFont('Courier','',16);
                $this->SetY(30);
                $this->cell(20, $h_set, "", 0, 0, 'L');

                $this->setFillColor(255,255,255);
                $this->setTextColor(0,0,0);
                $this->setX($this->def_width*20/100);
                // $this->setFont('Arial Regular Bold','B',10);
                $this->setFont('Courier','B',10);
                $this->cell(8, $h_set, "", 0, 0, 'L', 1);

                // Nomor
                $this->setY(30);
                $x_no = 40;
                $this->setX($x_no);
                $this->setFillColor(255,255,255);
                $this->setTextColor(0,0,0);
                $this->cell(40, $h_set, nomor_retur_penjualan($this->retur[0]), 0, 0, 'L');

                $this->Ln(8);
                //$this->setFillColor(255,255,255);
                //$this->setTextColor(0,0,0);
                // $this->setFont('Arial Regular Bold','B',11);
                $this->setFont('Courier','B',11);
                $this->cell(11, $h_set, "", 0, 0, 'L', 1);
                
                $x_tgl = 25;
                $this->setX($x_tgl+1);
                //$this->setFillColor(255,255,255);
                //$this->setTextColor(0,0,0);
                $this->cell(57, $h_set, date_db($this->retur[0]->tanggal_retur, true, 2), 0, 1, 'L'); 

                $y_table = $this->getY();
                // $this->Ln(10);
                // $this->setFillColor(0,0,0);
                // $this->setTextColor(255,255,255);
                // $this->setFont('Arial','B',10);
                // $this->cell(40, $h_set, "Banyaknya", 1, 0, 'C', 1);
                // $this->cell(80, $h_set, "NAMA BARANG", 1, 0, 'C', 1);
                // $this->cell(37, $h_set, "Harga @ Rp", 1, 0, 'C', 1);
                // $this->cell(37, $h_set, "Jumlah Rp", 1, 0, 'C', 1);
                $this->setY($y_table+2);
                $this->SetWidths([$this->def_width*14/100, $this->def_width*44/100, $this->def_width*12/100, $this->def_width*14/100]);
                $this->SetAligns(['C', 'C', 'C', 'C']);
                // $this->Row_noborder(["Banyaknya", "Nama Barang", "Harga @Rp", "Jumlah"], $h_set);
                $this->Row_noborder(["", "", "", ""], $h_set);

                // $this->Ln(6);
            }
				
	}
	
	function Content()
	{   
        $h_set = 6;
        $x_table = $this->getX();
        // $this->setFont('Arial Regular Bold','B',11);
        $this->setFont('Courier','B',11);
        //$this->setFillColor(255,255,255);
        //$this->setTextColor(0,0,0);
        $this->SetWidths([$this->def_width*10/100, $this->def_width*40/100, $this->def_width*16/100, $this->def_width*14/100]);
        $this->SetAligns(array('C', 'L', 'R', 'R'));
        $potongan = 0;
        // dd($this->detail);
        if(count($this->retur) > 0){
            $no = 1;
            $total = 0;
            $left = 13-(sizeof($this->retur));
            for ($i=0; $i < sizeof($this->retur)+$left ; $i++) { 
                # code...
                $this->setX($x_table-4);
                if(isset($this->retur[$i])){
                    $total += $this->retur[$i]->jml_retur*$this->retur[$i]->harga_satuan;
                    $potongan += $this->retur[$i]->jml_retur*$this->retur[$i]->potongan;

                    $this->Row_noborder(array($this->retur[$i]->jml_retur.' '.$this->retur[$i]->satuan, strtoupper($this->retur[$i]->nama_barang), $this->tampil_harga=='true' ? rupiah2($this->retur[$i]->harga_satuan, 0) : 0, $this->tampil_harga=='true' ? rupiah2(($this->retur[$i]->jml_retur*$this->retur[$i]->harga_satuan), 0) : 0), $h_set);

                }else if($i == sizeof($this->retur)){
                    if ($potongan != 0) {
                        $this->Row_noborder(array('', "Diskon Penjualan", "", $this->tampil_harga=='true' ? rupiah2($potongan, 0) : 0), $h_set);   
                    } else {
                        $this->Row_noborder(["", "", "", ""], $h_set);
                    }
                }else{
                    $this->Row_noborder(["", "", "", ""], $h_set);
                }
            }

        }else{
            $this->Row_noborder(array('', '', '', ''), $h_set);
        }
                

        $y = $this->getY();
        // $this->setFont('Arial Regular','B',10);
        if ($this->tampil_harga=='true') {
            $this->setXY(148, $y+1);
            //$this->setFillColor(255,255,255);
            //$this->setTextColor(0,0,0);
            $this->cell($this->def_width/100, 10, "", 0, 0, 'R');
            $this->MultiCell($this->def_width*15/100, 10, $this->tampil_harga=='true' ? rupiah2($total-$potongan, 0) : 0, 0, 'R');
        }

        // $this->SetY($y+5);
        // $this->cell(40, 5, "Tanda Terima", 0, 0, 'C');

        // $x = $this->getX();
        // $this->setFillColor(0,0,0);
        // $this->setTextColor(255,255,255);
        // $this->setX($x+20);
        // $this->cell(50, 5, "Perhatian", 1, 0, 'C', 1);

        // $this->Ln();
        // $this->setX(70);
        // $this->setFillColor(255,255,255);
        // $this->setTextColor(0,0,0);
        // $this->MultiCell(50, 5, "Barang-barang yang sudah dibeli tidak dapat ditukar / dikembalikan kecuali ada perjanjian", "LBR", "FJ");
	}
	
	function Footer()
	{
		
	}
}
$Pdf = new Pdf();
$Pdf->retur = $data['retur_penjualan'];
$Pdf->tampil_harga = $data['tampil_harga'];
$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(18,18,18);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial Regular Bold','B',12);
$Pdf->Content();
$Pdf->SetTitle($data['judul']);
$Pdf->AutoPrint(false);
$Pdf->Output($data['judul'], "I");
// $Pdf->Output(public_path('files/penjualan/Invoice.pdf'), "F");

?>