<?php
class Pdf extends PDF_MC_Table{
	//Page header
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(210 ,330 ));
    }

	function Header(){
        
		$this->setFont('Courier','B',12);
        $this->cell(100,5,'SLIP UPAH BORONGAN (VIA KAS) Tenun (MAKELAR)',0,0,'L',0);

        $this->setFont('Courier','B',10);
        $this->setX($this->GetX()+46);
        $this->cell(17,5,'Tanggal',0,0,'L',0);
        $this->cell(3,5,':',0,0,'C',0);
        $this->cell(25,5,tgl_full($this->data->tanggal_upah, ''),0,0,'L',0);
        $this->Ln();
        $this->setFont('Courier','B',10);
        $this->cell(100,5,'NAMA : '.strtoupper($this->data->makelar),0,0,'L',0);
        $this->setX($this->GetX()+46);
        $this->cell(17,5,'Nomor',0,0,'L',0);
        $this->cell(3,5,':',0,0,'C',0);
        $this->cell(25,5,$this->data->nomor,0,0,'L',0);

        $this->Ln();

        $this->setFont('Courier','B',12);
        $this->setX(90);
        $this->Line(10,$this->getY(),201,$this->getY());
        $this->Ln();
        
        $this->setFont('Courier','B',10);

        $this->cell(15, 5, "NO", 1, 0,"C");
        $this->cell(85, 5, "KETERANGAN", 1, 0,"C");
        $this->cell(15, 5, "KWT", 1, 0,"C");
        $this->cell(30, 5, "H. SATUAN", 1, 0,"C");
        $this->cell(15, 5, "QTY", 1, 0,"C");
        $this->cell(31, 5, "Jumlah", 1, 1,"C");
				
	}
	
	function Content()
	{         
		$this->setFont('Courier','B',10);
        $sub_jumlah = 0;
        $jum_qty = 0;
        $no = 1;
        if (sizeof($this->data_detail) > 0) {
        	foreach ($this->data_detail as $key => $detail) {
        		$jumlah = $detail->upah_makelar_satuan*$detail->qty;
        		$this->cell(15, 5, $no, 1, 0,"C");
		        $this->cell(85, 5, $detail->keterangan, 1, 0,"L");
		        $this->cell(15, 5, $detail->kualitas, 1, 0,"C");
		        $this->cell(30, 5, rupiah2($detail->upah_makelar_satuan), 1, 0,"R");
		        $this->cell(15, 5, $detail->qty, 1, 0,"C");
		        $this->cell(31, 5, rupiah2($jumlah), 1, 1,"R");
                $sub_jumlah += $jumlah;
                $jum_qty += $detail->qty;
                $no++;
        	}
        }

        if (sizeof($this->data_lain) > 0) {
            foreach ($this->data_lain as $key => $lain) {
                if ($lain->tipe == "K") {
                    $sub_jumlah -= $lain->jumlah;
                } else {
                    $sub_jumlah += $lain->jumlah;
                }

                $this->cell(15, 5, $no, 1, 0,"C");
                $this->cell(85, 5, $lain->keterangan, 1, 0,"L");
                $this->cell(15, 5, "", 1, 0,"C");
                $this->cell(30, 5, "", 1, 0,"R");
                $this->cell(15, 5, "", 1, 0,"C");
                $this->cell(31, 5, ($lain->tipe == "K") ? '-'.rupiah2($lain->jumlah) : rupiah2($lain->jumlah), 1, 1,"R");
                
                $no++;
            }
        }
        
        $this->cell(145, 5, 'JUMLAH', 1, 0, "C");        
        $this->cell(15, 5, $jum_qty, 1, 0, "C");
        $this->cell(31, 5, rupiah2($sub_jumlah), 1, 0, "R");
        $this->ln();
	}
	
	function Footer()
	{
		$this->Ln();
        
		$this->SetY(-15);
		//buat garis horizontal
		
		//Courier italic 9
		$this->SetFont('Courier','B',9);
        $this->Cell(0,10,'',0,0,'L');
		//nomor halaman
	}

	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->SetAutoPageBreak(true ,15);
$Pdf->data=$upah_tenun;
$Pdf->data_detail=$upah_tenun_dt;
$Pdf->data_lain=$upah_tenun_lain;
$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Slip Upah Tenun Makelar");
$Pdf->SetCreator("CV. KETJUBUNG");
$Pdf->AutoPrint(false);
$Pdf->Output("Slip Upah Tenun Makelar.pdf", "I");
//$Pdf->Output();

?>