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

	function Header(){
        
		if($this->PageNo() == 1){		
            $this->setFont('Courier','B',12);
            $this->setFillColor(255,255,255);
            $this->cell(100,7,'SLIP UPAH BORONGAN (VIA KAS)',0,0,'l',0);

            $this->setFont('Courier','B',18);
            $this->setX($this->GetX());
            $this->Ln(10);
            $this->setFont('Courier','B',9);
            $this->setFillColor(255,255,255);
            $this->setY($this->GetY()-2);
            
            $this->setFont('Courier','B',12);
            $set_label = 'CV. KETJUBUNG';
            $this->cell(0,5,$set_label,0,0,'L',1);
            $this->Ln();

            $this->setFont('Courier','B',9);
            $this->cell(25,5,'Kepada Yth. '.$this->penerima,0,0,'L',1);
            //$this->setX($this->GetX()+112);

            $this->Ln();
        }
				
	}
	
	function Content()
	{          
		$this->setFont('Courier','B',12);
        $this->setX(90);
        $this->Line(10,$this->getY(),201,$this->getY());
        $this->Ln();
        
        $this->setFont('Courier','B',9);
        $this->cell(10, 5, "No", 1, 0,"C");
        $this->cell(20, 5, "Tanggal", 1, 0,"C");
        $this->cell(20, 5, "Nomor", 1, 0,"C");
        $this->cell(40, 5, "Keterangan", 1, 0,"C");
        $this->cell(15, 5, "KWT", 1, 0,"C");
        $this->cell(11, 5, "QTY", 1, 0,"C");
        $this->cell(25, 5, "Stn. Upah", 1, 0,"C");
        $this->cell(20, 5, "Stn. Pick", 1, 0,"C");
        $this->cell(30, 5, "Jumlah", 1, 1,"C");

        $this->setFont('Courier','B',9);

        $nomor 	= 1;
        $total1 = 0;
        $total2	= 0;
        $total3	= 0;
        $ppn 	= 0;
        $netto  = 0;

        foreach($this->data as $key => $row){
            foreach ($this->data[$key] as $key_hasil => $val_hasil) {
                $upahh=0;
                $pick=0;
                foreach ($this->upah as $keya => $valuea) {
                    foreach ($this->upah[$keya] as $key_upah => $val_upah) {
                        $id_barang=$val_hasil->id_barang || 0;
                        $id_motif = $val_hasil->id_motif || 0;
                        $id_motif_barang= $val_hasil->id_motif_barang || 0;
                        $id_proses=$val_hasil->id_proses || 0;
                        if($val_upah->id_barang==$id_barang && $val_upah->id_motif==$id_motif && $val_upah->id_motif_barang==$id_motif_barang && $val_upah->id_proses==$id_proses && $val_hasil->id==$val_upah->id_pbb){
                            $upahh=$val_upah->upah_satuan;
                            $pick=$val_upah->upah_pick;
                        }
                    }
                }
                $set_total=($upahh*$val_hasil->jumlah)+($pick*$val_hasil->jumlah);
                $total1 += $set_total;
                $this->cell(10, 5, $nomor, 1, 0,"C");
                $this->cell(20, 5, tgl_full($val_hasil->tanggal, ''), 1, 0,"C");
                $this->cell(20, 5, $val_hasil->nomor, 1, 0,"C");
                $this->cell(40, 5, $val_hasil->nama_barang, 1, 0,"L");
                $this->cell(15, 5, $val_hasil->nama_kualitas, 1, 0,"L");
                $this->cell(11, 5, $val_hasil->jumlah, 1, 0,"C");
                $this->cell(25, 5, rupiah2($upahh,2), 1, 0,"R");
                $this->cell(20, 5, rupiah2($pick,2), 1, 0,"R");
                $this->cell(30, 5, rupiah2($set_total,2), 1, 1,"R");
                $nomor++;
            }
        }

        $this->setX(131);
        $this->cell(35, 5, 'Jumlah', 0, 0, 0);        
        $this->cell(35, 5, rupiah2($total1,2), 0, 0, 0);
        $this->setFont('Courier','B',9);
        $this->ln();

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

        

        $x = $this->getX();
        $y = $this->getY();
        
        $this->Ln();
        $this->SetXY(150, $y);
	}
	
	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->data=$data['hasil'];
$Pdf->penerima=$data['nama_makelar'];
$Pdf->proses=$data['proses'];
$Pdf->lain2=$data['lain2'];
$Pdf->upah = $data['upah_bayar'];
$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(10,10,10);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Upah Borongan ".$data['nama_makelar']);
$Pdf->SetAuthor($data['author']);
$Pdf->SetSubject('SLIP UPAH BORONGAN (VIA KAS)');
$Pdf->SetKeywords('SLIP UPAH BORONGAN (VIA KAS)');
$Pdf->SetCreator("CV. KETJUBUNG");
$Pdf->AutoPrint(false);
$Pdf->Output("Upah Borongan ".$data['nama_makelar'].".pdf", "I");

?>