 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(210 ,330 ));
        $this->AddFont("calibri","","calibri.php");
        $this->AddFont("calibri","B","calibrib.php");
    }
	function Header(){
        
		if($this->PageNo() == 1){		
                $this->setFont('Calibri','B',18);
                $this->setFillColor(255,255,255);
                $this->cell(190, 7, 'PEMBELIAN', 0, 0, 'C', 1);
                $this->setFont('Calibri','',18);

                $this->setX($this->GetX());
                // $this->Ln();
                // $this->line(10,$this->GetY(),205,$this->GetY());
                $this->Ln(10);
                $this->setFont('Calibri','',11);
                $this->setFillColor(255,255,255);

                $this->setY($this->GetY()-2);
                
                $this->setFont('Calibri','B',15);
                $cv = $this->data->cv;
                $set_label = 'CV. KETJUBUNG';
                if($cv==0){
                    $set_label = '';
                }
                $this->cell(0,5,$set_label,0,0,'L',1);
                $this->Ln();

                $this->setFont('Calibri','',11);
                $this->cell(25,5,'Kepada Yth. '.$this->data->nama_penyedia,0,0,'L',1);
                $this->setX($this->GetX()+112);
                $this->cell(23,5, strtoupper('Nomor'),0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                $this->cell(2,5,$this->data->no_faktur.$this->data->sufix_no_faktur,0,0,'L',1);
                $this->Ln();
                
                $this->Cell(137,5,'',0,0,'L',1);
                
                $this->Cell(23,5, strtoupper('Tanggal'),0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                $this->cell(2,5, tgl_full($this->data->tanggal, ''),0,1,'L',1);

                $this->Ln();
                
                $this->Cell(10,5,'Mohon dapat dipenuhi kebutuhan kami atas barang dengan jumlah, kondisi serta spesifikasi sebagai berikut :',0,0,'L',1);
                $this->Ln(7);

                                             

            }
				
	}
	
	function Content()
	{          
        $this->setFont('Calibri','',11);

        /*$this->SetWidths(array(12, 30, 20, 25, 18, 22, 32, 32));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'));
        $this->Row(array("NO\nURUT", "NAMA BARANG", "KODE\nBARANG", "SPESIPIKASI", "SATUAN", "KWANTITAS", "HARGA\nSATUAN", "JUMLAH\n(RUPIAH)" ), 4);*/
        $this->SetWidths(array(12, 55, 20, 18, 22, 32, 32));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'));
        $this->Row(array("NO\nURUT", "NAMA BARANG", "KODE\nBARANG", "SATUAN", "KWANTITAS", "HARGA\nSATUAN", "JUMLAH\n(RUPIAH)" ), 4);

        $this->setFont('Calibri','',11);
        /*$this->SetAligns(array('C', 'L', 'C', 'C', 'C', 'R', 'R', 'R'));*/
        $this->SetAligns(array('C', 'L', 'C', 'C', 'R', 'R', 'R'));
        $nomor = 1;
        $total = 0;
        foreach($this->detail as $key => $row){
            // $total += $row->total;
            $row_total = $row->jml_barang*$row->harga_satuan;
            $set_harga_satuan = $this->tampil_harga=='true' ? rupiah2($row->harga_satuan, 2) : "";
            $set_total = $this->tampil_harga=='true' ? rupiah2($row_total, 2) : "";
            /*$this->Row(array($nomor, $row->nama_barang, $row->kode_barang, $row->nama_barang, $row->nama_satuan, $row->jml_barang, $set_harga_satuan, $set_total ));*/
            $this->Row(array($nomor, $row->nama_barang, $row->kode_barang, $row->nama_satuan, $row->jml_barang, $set_harga_satuan, $set_total ));

            $nomor++;
        }

        $set_sub_total = "";
        $set_potongan = "";
        $set_dpp = "";
        $set_ppn = "";
        $set_ongkir = "";
        $set_terbilang = "";
        $set_sum_total = "";
        $potongan = 0;
        $ppn = 0;
        if($this->tampil_harga=='true'){
            $total = $this->data->nominal_total;
            $set_sub_total = rupiah2($total, 2);
            $potongan = $this->data->nominal_potongan;
            $set_potongan = rupiah2($potongan, 2);
            $set_dpp = rupiah2($total-$potongan, 2);
            $ppn = $this->data->nominal_pajak;
            $set_ppn = rupiah2($ppn, 2);
            $ongkir = $this->data->nominal_ongkir;
            $set_ongkir = rupiah2($ongkir, 2);

            $sum_total = $total-$potongan+$ppn+$ongkir;
            $set_sum_total = rupiah2($sum_total, 2);
            $set_terbilang = terbilang($sum_total);

        }
        $y = $y2 = $this->getY();
        $this->setXY(137, $y);
        $this->cell(32, 5, 'Sub Total', 0, 0, 0);
        $this->cell(32, 5, $set_sub_total, 1, 1, 0);

        if($potongan!=0){
            $y = $this->getY();
            $this->setXY(137, $y);
            $this->cell(32, 5, 'Potongan', 0, 0, 0);
            $this->cell(32, 5, $set_potongan, 1, 1, 0);
        }

        $y = $this->getY();
        $this->setXY(137, $y);
        $this->cell(32, 5, 'DPP', 0, 0, 0);
        $this->cell(32, 5, $set_dpp, 1, 1, 0);

        $y = $this->getY();
        $this->setXY(137, $y);
        $this->cell(32, 5, 'PPN', 0, 0, 0);
        $this->cell(32, 5, $set_ppn, 1, 0, 0);

        $y = $this->getY();
        $this->setXY(137, $y+5);
        $this->cell(32, 5, 'Ongkos kirim', 0, 0, 0);
        $this->cell(32, 5, $set_ongkir, 1, 0, 0);

        $y = $this->getY();
        $this->setXY(137, $y+5);
        $this->cell(32, 5, 'Jumlah', 0, 0, 0);
        $this->cell(32, 5, $set_sum_total, 1, 0, 0);

        $y_sk = $this->getY();

        $this->setY($y2+5);
        $this->cell(40, 5, 'Terbilang', 0, 0, 'R');
        $this->cell(5, 5, ':', 0, 'C');
        $this->setFont('Calibri','U',11);
        $this->MultiCell(82, 5, $set_terbilang, 0, "FJ");
        $this->setFont('Calibri','',11);

        $x = $this->getX();
        // $y = $this->getY();

        $this->setY($y_sk+5);
        $this->setFont('Calibri','B',11);
        $this->setFillColor(255,255,255);
        $this->cell(25, 5,"SYARAT DAN KETENTUAN", 0, 'L');
        $this->Ln();

        $y_ttd = $this->getY();

        $this->setFont('Calibri','',11);
        $this->setFillColor(255,255,255);
        $this->cell(40, 5,"Syarat Pembayaran", 0, 0, 'L');
        // $this->setFont('Calibri','',11);
        $this->cell(5, 5,":", 0, 0, 'C');
        $this->MultiCell(75, 5, $this->data->syarat_bayar, 0, 'FJ');

        $y = $this->getY();

        // $this->setFont('Calibri','B',11);
        $this->setFillColor(255,255,255);
        $this->cell(40, 5,"Tempat Penyerahan", 0, 0, 'L');
        // $this->setFont('Calibri','',11);
        $this->cell(5, 5,":", 0, 0, 'C');
        $this->MultiCell(85, 5, $this->data->tempat_penyerahan." - ".$this->data->nama_lokasi."\n".$this->data->alamat, 0, 'FJ');

        $y_ttd2 = $this->getY();

        // $this->setFont('Calibri','B',11);
        $this->setFillColor(255,255,255);
        $this->cell(40, 5,"Tanggal Penyerahan", 0, 0, 'L');
        // $this->setFont('Calibri','',11);
        $this->cell(5, 5,":", 0, 0, 'C');
        $this->MultiCell(75, 5, tgl_full($this->data->tanggal_serah, ''), 0, 'FJ');

        $this->setFont('Calibri','B',11);
        $this->setFillColor(255,255,255);
        $this->cell(40, 5,"Penting", 0, 0, 'L');
        $this->setFont('Calibri','',11);
        $this->cell(5, 5,":", 0, 0, 'C');

        $this->Ln();
        $this->MultiCell(80, 5, "Penerimaan barang ditutup Jam 15.00 WIB kecuali dengan janji khusus", 'FJ');

        $this->SetXY(150, $y_ttd);
        $this->MultiCell(55, 5, $this->data->nama_lokasi.", ".$this->tanggal_cetak, '', 'C');

        $this->SetXY(150, $y_ttd2);
        $this->setFont('Calibri','BU',11);
        $this->MultiCell(55, 5, $this->data->penerima, '', 'C');
	}
	
	function Footer()
	{
		$this->Ln();
        /*$jumlah=0;
			foreach($this->kas_keluar as $key => $kas){
				$jumlah += $kas['jumlah'];
			}*/

	/*	$this->setXY($this->GetX()+14,$this->GetY()+10);
		$this->cell(27.5,6,"SYARAT DAN KETENTUAN",0,0,'C',1);
        $this->Ln();
        $this->setFont('Calibri','B',9);
		$this->setX($this->GetX()+4);
		$this->cell(40,5,"Syarat Pembayaran",0,0,'L',1);
		$this->cell(3,5,":",0,0,'C',1);
        $this->cell(40,5,"Mundur 7 bulan dari tanggal nota",0,1,'L',false);
        $this->setX($this->GetX()+4);
        $this->cell(40,5,"Syarat Penyerahan",0,0,'L',1);
        $this->cell(3,5,":",0,0,'C',1);
        $this->cell(120,5,"di kirim ke gresik",0,0,'L',false);
        $this->cell(3,5,"Gresik, ".date_format(date_create($this->data->tanggal),'d-m-Y'),0,1,'C',1);
        $this->setX($this->GetX()+4);
        $this->cell(40,5,"Syarat Penyerahan",0,0,'L',1);
        $this->cell(3,5,":",0,0,'C',1);

        $this->cell(120,5,date_format(date_create($this->data->tanggal+1),'d-m-Y'),0,1,'L',false);
        $this->setX($this->GetX()+4);
        $this->cell(40,5,"Tag",0,0,'L',1);
        $this->cell(3,5,":",0,0,'C',1);
        $this->cell(120,5,"",0,1,'L',false);
        $this->setX($this->GetX()+4);
        $this->cell(40,5,"Penerimaan barang ditutup jam 15.00 WIB",0,1,'L',1);
        $this->setX($this->GetX()+4);
        $this->cell(155,5,"dengan janji khusus",0,0,'L',1);
        $this->cell(20,5,"MUNIF",0,0,'C',1);*/
		
		
		
		$this->SetY(-15);
		//buat garis horizontal
		
		//Arial italic 9
		$this->SetFont('Calibri','B',9);
        $this->Cell(0,10,'',0,0,'L');
		//nomor halaman
		//$this->SetFont('tahoma','',9);
		//$this->Cell(0,10,'Halaman '.$this->PageNo().' dari {nb}',0,0,'R');
	}
	function MultiCells($w, $h, $txt, $border=0, $ln=0, $align='J', $fill=false)
    {
        // Custom Tomaz Ahlin
        if($ln == 0) {
            $current_y = $this->GetY();
            $current_x = $this->GetX();
        }

        // Output text with automatic or explicit line breaks
        $cw = &$this->CurrentFont['cw'];
        if($w==0)
            $w = $this->w-$this->rMargin-$this->x;
        $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
        $s = str_replace("\r",'',$txt);
        $nb = strlen($s);
        if($nb>0 && $s[$nb-1]=="\n")
            $nb--;
        $b = 0;
        if($border)
        {
            if($border==1)
            {
                $border = 'LTRB';
                $b = 'LRT';
                $b2 = 'LR';
            }
            else
            {
                $b2 = '';
                if(strpos($border,'L')!==false)
                    $b2 .= 'L';
                if(strpos($border,'R')!==false)
                    $b2 .= 'R';
                $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
            }
        }
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $ns = 0;
        $nl = 1;
        while($i<$nb)
        {
            // Get next character
            $c = $s[$i];
            if($c=="\n")
            {
                // Explicit line break
                if($this->ws>0)
                {
                    $this->ws = 0;
                    $this->_out('0 Tw');
                }
                $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $ns = 0;
                $nl++;
                if($border && $nl==2)
                    $b = $b2;
                continue;
            }
            if($c==' ')
            {
                $sep = $i;
                $ls = $l;
                $ns++;
            }
            $l += $cw[$c];
            if($l>$wmax)
            {
                // Automatic line break
                if($sep==-1)
                {
                    if($i==$j)
                        $i++;
                    if($this->ws>0)
                    {
                        $this->ws = 0;
                        $this->_out('0 Tw');
                    }
                    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
                }
                else
                {
                    if($align=='J')
                    {
                        $this->ws = ($ns>1) ?     ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
                        $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
                    }
                    $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
                    $i = $sep+1;
                }
                $sep = -1;
                $j = $i;
                $l = 0;
                $ns = 0;
                $nl++;
                if($border && $nl==2)
                    $b = $b2;
            }
            else
                $i++;
        }
        // Last chunk
        if($this->ws>0)
        {
            $this->ws = 0;
            $this->_out('0 Tw');
        }
        if($border && strpos($border,'B')!==false)
            $b .= 'B';
        $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
        $this->x = $this->lMargin;

        // Custom Tomaz Ahlin
        if($ln == 0) {
            $this->SetXY($current_x + $w, $current_y);
        }
    }
}
$Pdf = new Pdf();
$Pdf->detail=$data['detail'];
$Pdf->data=$data['data'];
$Pdf->tampil_harga=$data['tampil_harga'];
$Pdf->tanggal_cetak=$data['tanggal_cetak'];
$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Calibri','',12);
$Pdf->Content();
$Pdf->SetTitle("Order Pembelian - ".$data['data']->no_faktur." ".$data['data']->nama_penyedia);
$Pdf->Output("Order Pembelian - ".$data['data']->no_faktur." ".$data['data']->nama_penyedia.".pdf", "I");


?>