 <?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("tahoma","","tahoma.php");
        $this->AddFont("tahoma","B","tahomabd.php");*/
    }
    function Header(){
        
        if($this->PageNo() == 1){       
                $this->setFont('Arial','B',18);
                $this->setFillColor(255,255,255);
                $this->cell(0,7,'PEMBELIAN',0,0,'C',1);
                $this->setFont('Arial','',18);

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

                $this->setY($this->GetY()-2);
                
                $this->setFont('Arial','B',12);
                $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('Arial','',9);
                $this->cell(25,5,'Kepada Yth. '.$this->data->dataPenyedia->nama,0,0,'L',1);
                $this->setX($this->GetX()+112);
                $this->cell(23,5,'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,'Tanggal',0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                $this->cell(2,5, tgl_full($this->data->tanggal, ''),0,0,'L',1);

                $this->Ln();
                $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('arial','B',9);

        $this->SetWidths(array(10, 30, 20, 25, 18, 18, 35, 40));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'));
        $this->Row(array("No Urut", "Nama Barang", "Kode Barang", "Spesifikasi", "Satuan", "Kwantitas", "Harga Satuan", "Jumlah (Rupiah)" ));

        $this->setFont('arial','',9);
        $this->SetAligns(array('C', 'L', 'C', 'C', 'C', 'R', 'R', 'R'));
        $nomor = 1;
        $total = 0;
        foreach($this->detail as $key => $row){
            $total += $row->total;
            $this->Row(array($nomor, $row->nama_barang, $row->kode_barang, $row->nama_barang, $row->nama_satuan, $row->jml_barang, rupiah($row->harga_satuan), rupiah($row->total) ));

            $nomor++;
        }
        $y = $this->getY();
        $this->setXY(131, $y);
        $this->cell(35, 5, 'DPP', 0, 1, 0);
        $this->setXY(166, $y);
        $this->cell(40, 5, rupiah($total), 1, 1, 0);

        $y = $this->getY();
        $this->setXY(131, $y);
        $this->cell(35, 5, 'PPN', 0, 0, 0);
        $this->cell(40, 5, '', 1, 0, 0);

        $x = $this->getX();
        $y = $this->getY();
        
        $this->Ln();
        $this->setX(131);
        $this->cell(35, 5, 'Jumlah', 0, 0, 0);
        $this->cell(40, 5, rupiah($total), 1, 0, 0);

        

        $this->setY($y);
        $this->cell(40, 5, 'Terbilang', 0, 0, 'R');
        $this->cell(5, 5, ':', 0, 'C');
        $this->setFont('arial','U',9);
        $this->MultiCell(75, 5, terbilang($total), 0, "FJ");
        $this->setFont('arial','',9);
        

        $this->Ln(10);
        $y = $this->getY();

        // $this->setFont('arial','B',10);
        // $this->setFillColor(255,255,255);
        // $this->cell(25, 5,"SYARAT DAN KETENTUAN", 0, 'L');
        // $this->Ln();

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


        // $this->setFont('arial','B',10);
        // $this->setFillColor(255,255,255);
        // $this->cell(40, 5,"Tempat Penyerahan", 0, 0, 'L');
        // $this->setFont('arial','',10);
        // $this->cell(5, 5,":", 0, 0, 'C');
        // $this->MultiCell(75, 5, $this->data->tempat_serah, 0, 'FJ');

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

        // $this->setFont('arial','B',10);
        // $this->setFillColor(255,255,255);
        // $this->cell(40, 5,"Penting", 0, 0, 'L');
        // $this->setFont('arial','',10);
        // $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);
        $this->MultiCell(55, 5, "Gresik, ".tgl_full(date('d-m-Y'), 0), '', 'C');

        $this->SetXY(150, $y+20);
        $this->setFont('arial','BU',10);
        $this->MultiCell(55, 5, "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('arial','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('Arial','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->data=$data['data'];
$Pdf->detail=$data['detail'];
$Pdf->SetAutoPageBreak(true ,15);

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


?>