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

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

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

                $this->setFont('Calibri','',8);
                $this->cell($this->def_width*62/100,3,'Kepada Yth. '.$this->data->nama_penyedia,0,0,'L',1);
                // $this->setX($this->GetX()+112);
                $this->cell($this->def_width*10/100,3, strtoupper('Nomor'),0,0,'L',1);
                $this->cell($this->def_width*3/100,3,':',0,0,'L',1);
                $this->cell($this->def_width*17/100,3,$this->data->no_faktur.$this->data->sufix_no_faktur,0,0,'L',1);
                $this->Ln();
                
                $this->Cell($this->def_width*62/100,3,'',0,0,'L',1);
                
                $this->Cell($this->def_width*10/100,3,strtoupper('Tanggal'),0,0,'L',1);
                $this->cell($this->def_width*3/100,3,':',0,0,'L',1);
                $this->cell($this->def_width*17/100,3, tgl_full($this->data->tanggal, ''),0,0,'L',1);

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

                                             

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

        /*$this->SetWidths(array($this->def_width*7/100, $this->def_width*17/100, $this->def_width*9.5/100, $this->def_width*14/100, $this->def_width*9.5/100, $this->def_width*6/100, $this->def_width*15/100, $this->def_width*15/100));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'));
        $this->Row(array("NO\nURUT", "NAMA\nBARANG", "KODE\nBARANG", "SPESIPIKASI", "SATUAN", "QTY", "HARGA\nSATUAN", "JUMLAH\n(RUPIAH)" ), 3);*/
        $this->SetWidths(array($this->def_width*7/100, $this->def_width*31/100, $this->def_width*9.5/100, $this->def_width*9.5/100, $this->def_width*6/100, $this->def_width*15/100, $this->def_width*15/100));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C'));
        $this->Row(array("NO\nURUT", "NAMA\nBARANG", "KODE\nBARANG", "SATUAN", "QTY", "HARGA\nSATUAN", "JUMLAH\n(RUPIAH)" ), 3);

        $this->setFont('Calibri', '', 9);
        /*$this->SetAligns(array('C', 'L', 'C', 'L', '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 ), 4);*/
            $this->Row(array($nomor, $row->nama_barang, $row->kode_barang, $row->nama_satuan, $row->jml_barang, $set_harga_satuan, $set_total ), 4);

            $nomor++;
        }
        
        $y = $this->getY();

        $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($this->def_width*66.5/100, $y);
        $this->cell($this->def_width*15/100, 4, 'Sub Total', 0, 1, 0);

        $x = $this->getX();
        $this->setXY($this->def_width*81.5/100, $y);
        $this->cell($this->def_width*15/100, 4, $set_sub_total, 1, 1, 0);

        $y_terbilang = $this->getY();

        if($potongan!=0){
            $y = $this->getY();
            $this->setXY($this->def_width*66.5/100, $y);
            $this->cell($this->def_width*15/100, 4, 'Potongan', 0, 1, 0);
            $this->setXY($this->def_width*81.5/100, $y);
            $this->cell($this->def_width*15/100, 4, $set_potongan, 1, 1, 0);
        }

        $y = $this->getY();
        $this->setXY($this->def_width*66.5/100, $y);
        $this->cell($this->def_width*15/100, 4, 'DPP', 0, 1, 0);
        $this->setXY($this->def_width*81.5/100, $y);
        $this->cell($this->def_width*15/100, 4, $set_dpp, 1, 1, 0);

        $y = $this->getY();
        $this->setXY($this->def_width*66.5/100, $y);
        $this->cell($this->def_width*15/100, 4, 'PPN', 0, 0, 0);
        $this->cell($this->def_width*15/100, 4, $set_ppn, 1, 0, 0);

        $y = $this->getY();
        $this->setXY($this->def_width*66.5/100, $y+4);
        $this->cell($this->def_width*15/100, 4, 'Ongkos Kirim', 0, 0, 0);
        $this->cell($this->def_width*15/100, 4, $set_ongkir, 1, 0, 0);

        $y = $this->getY();
        $this->setXY($this->def_width*66.5/100, $y+4);
        $this->cell($this->def_width*15/100, 4, 'Jumlah', 0, 0, 0);
        $this->cell($this->def_width*15/100, 4, $set_sum_total, 1, 0, 0);

        $y_sk = $this->getY();

        $this->setY($y_terbilang);
        $this->cell($this->def_width*10/100, 4, 'Terbilang', 0, 0, 'R');
        $this->cell($this->def_width*2/100, 4, ':', 0, 'C');
        $this->setFont('Calibri','U',9);
        $this->MultiCell($this->def_width*50/100, 4, $set_terbilang, 0, "FJ");
        $this->setFont('Calibri','',9);

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

        $h_sk = 3.5;
        $f_sk = 8;
        $this->setY($y_sk+5);
        $this->setFont('Calibri','B', $f_sk);
        $this->setFillColor(255,255,255);
        $this->cell($this->def_width*93/100, $h_sk,"SYARAT DAN KETENTUAN", 0, 1, 'L');

        $y_ttd = $this->getY();

        $this->setFont('Calibri','', $f_sk);
        $this->setFillColor(255,255,255);
        $this->cell($this->def_width*21/100, $h_sk,"Syarat Pembayaran", 0, 0, 'L');
        // $this->setFont('Calibri','', $f_sk);
        $this->cell($this->def_width*2/100, $h_sk,":", 0, 0, 'C');
        $this->MultiCell($this->def_width*40/100, $h_sk, $this->data->syarat_bayar, 0, 'FJ');

        $y = $this->getY();

        // $this->setFont('Calibri','B', $f_sk);
        $this->setFillColor(255,255,255);
        $this->cell($this->def_width*21/100, $h_sk,"Tempat Penyerahan", 0, 0, 'L');
        // $this->setFont('Calibri','', $f_sk);
        $this->cell($this->def_width*2/100, $h_sk,":", 0, 0, 'C');
        $this->MultiCell($this->def_width*40/100, $h_sk, $this->data->tempat_penyerahan." - ".$this->data->nama_lokasi."\n".$this->data->alamat, 0, 'FJ');

        $y_ttd2 = $this->getY();

        // $this->setFont('Calibri','B', $f_sk);
        $this->setFillColor(255,255,255);
        $this->cell($this->def_width*21/100, $h_sk,"Tanggal Penyerahan", 0, 0, 'L');
        // $this->setFont('Calibri','', $f_sk);
        $this->cell($this->def_width*2/100, $h_sk,":", 0, 0, 'C');
        $this->MultiCell($this->def_width*40/100, $h_sk, tgl_full($this->data->tanggal_serah, ''), 0, 'FJ');

        $this->setFont('Calibri','B', $f_sk);
        $this->setFillColor(255,255,255);
        $this->cell($this->def_width*21/100, $h_sk,"Penting", 0, 0, 'L');
        $this->setFont('Calibri','', $f_sk);
        $this->cell($this->def_width*2/100, $h_sk,":", 0, 1, 'C');
        $this->MultiCell($this->def_width*58/100, $h_sk, "Penerimaan barang ditutup Jam 15.00 WIB kecuali dengan janji khusus", 'FJ');

        $this->SetXY($this->def_width*63/100, $y_ttd);
        $this->MultiCell($this->def_width*34/100, 5, $this->data->nama_lokasi.", ".$this->tanggal_cetak, '', 'C');

        $this->SetXY($this->def_width*63/100, $y_ttd2);
        $this->setFont('Calibri','BU', $f_sk);
        $this->MultiCell($this->def_width*34/100, 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(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Calibri','',8);
$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");


?>