 <?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) '.$this->penerima->nama_proses,0,0,'l',0);
                $this->setFont('Courier','B',18);

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

                $this->setY($this->GetY()-2);
                
                $this->setFont('Courier','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('Courier','B',9);
                if($this->penerima->id_pegawai==0){
                    $this->cell(25,5,'Kepada Yth. '.$this->penerima->nama_rekanan,0,0,'L',1);
                }else{
                    $this->cell(25,5,'Kepada Yth. '.$this->penerima->nama_pegawai,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->penerima->nomor,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->penerima->tanggal_bayar, ''),0,0,'L',1);

                $this->Ln();
                $this->Ln();                  

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

        $this->SetWidths(array(20, 30, 40, 21, 40, 40));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C',));
        $this->Row(array("No Urut", "Tanggal", "Keterangan","QTY","Satuan Upah","Jumlah" ));

        $this->setFont('Courier','B',9);
        $this->SetAligns(array('C', 'C', 'C', 'C', 'R', 'R'));
        $nomor = 1;
        $total1 = 0;
        $total2=0;
        foreach($this->upah as $key => $row){
            $upahh=$row->upah_satuan;
            $set_total=$row->jumlah*$upahh;
            $total1 += $set_total;
            $this->Row(array($nomor, tgl_full($this->penerima->tanggal_bayar, ''), $this->format_keterangan($row), $row->jumlah, rupiah2($upahh,2),  rupiah2($set_total,2) ));
            $nomor++;
        }
        $this->setX(131);
        $this->cell(35, 5, 'Sub Jumlah', 0, 0, 0);        
        $this->cell(35, 5, rupiah2($total1,2), 0, 0, 0);
        $this->setFont('Courier','B',9);
        $this->ln();
        if($this->lain2!=''){
            foreach ($this->lain2 as $key => $value) {
                if ($value->jenis_transaksi == "TAMBAHAN") {
                    $total2 += $value->jumlah;
                    $this->Row(array($nomor, tgl_full($this->penerima->tanggal_bayar, ''), $value->jenis, '', '',rupiah2($value->jumlah,2)));
                } else {
                    $total2 -= $value->jumlah;
                    $this->Row(array($nomor, tgl_full($this->penerima->tanggal_bayar, ''), $value->jenis, '', '','-'.rupiah2($value->jumlah,2)));
                }
                
                $nomor++;
                
            }
            $this->setX(131);
            $this->cell(35, 5, 'Sub Jumlah', 0, 0, 0);        
            $this->cell(35, 5, rupiah2($total2,2), 0, 0, 0);
            $this->setFont('Courier','B',9);
        }

        $this->Ln();
        $this->Line(10,$this->getY(),201,$this->getY());
        $this->setY($this->getY()+1);
        $this->setX(131);
        $this->cell(35, 5, 'Total Jumlah', 0, 0, 0);        
        $this->cell(35, 5, rupiah2($total1+$total2 ,2), 0, 0, 0);
        $this->setFont('Courier','B',9);

        $this->setX(10);
        $this->cell(30,5,'Mengetahui', 1, 0, 'C');        
        $this->cell(30,5,'Kasir', 1, 0, 'C');
        $this->cell(30,5,'Penerima', 1, 1, 'C');

        $this->cell(30,15,'', 1, 0, 0);
        $this->cell(30,15,'', 1, 0, 0);
        $this->cell(30,15,'', 1, 0, 0);

        $y = $this->getY();

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

        $this->Ln();
        $this->SetXY(150, $y);
       
	}
	
	function Footer()
	{
		$this->Ln();		
		$this->SetY(-15);
		//buat garis horizontal
		
		//Arial italic 9
		$this->SetFont('Courier','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');
	}

    public function format_keterangan($data)
    {
        $keterangan = $data->nama_barang;
        if($data->id_corak!=0 && $data->id_corak!=''){
            $keterangan = $data->nama_corak;
        }else if($data->id_motif!=0 && $data->id_motif!=''){
            $keterangan = $data->nama_motif;
        }else if($data->id_komposisi!=0 && $data->id_komposisi!=''){
            $keterangan = $data->nama_komposisi;
        }

        return $keterangan;
    }

	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['data_penerima'];
$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['data_penerima']->nomor);
$Pdf->AutoPrint(false);
$Pdf->Output("Upah Borongan ".$data['data_penerima']->nomor.".pdf", "I");


?>