 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
    public $paper_w = 210, $paper_h = 330;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->paper_w , $this->paper_h));
		/*$this->AddFont("tahoma","","tahoma.php");
		$this->AddFont("tahoma","B","tahomabd.php");*/

        
    }
	function Header(){
        
		if($this->PageNo() != 0){		
                $this->setFont('Arial','B',14);
                $this->setFillColor(255,255,255);
                $this->cell(70,7,'',0,0,'L',1);
                $this->cell(90,7,'BON BAHAN - '.strtoupper($this->data->getProses->nama_proses),0,0,'L',1);
                $this->Ln(10);
                $this->setFont('Arial','',14);

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

                $this->setY($this->GetY()-2);
                
                $this->setFont('Arial','',11);
                $this->Cell(50,5,'Tanggal Pengambilan',0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                $this->cell(2,5, tgl_full($this->data->tanggal, '0'),0,0,'L',1);

                $this->setX($this->GetX()+80);
                $this->cell(23,5,'Pemroses',0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                if($this->data->id_pegawai != '0'){
                    $this->cell(2,5,$this->data->getPegawai->nama_pegawai,0,0,'L',1);
                }else if($this->data->id_rekanan != '0'){
                    $this->cell(2,5,$this->data->getRekanan->nama,0,0,'L',1);
                }else if($this->data->id_mesin != '0'){
                    $this->cell(2,5,$this->data->getMesin->nama,0,0,'L',1);
                }
                $this->ln();

                $this->Cell(50,5,'Tanggal Perkiraan Selesai',0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                $this->cell(2,5, tgl_full($this->data->tanggal_perkiraan, '0'),0,0,'L',1);
                
                if($this->data->id_rekanan != '0'){
                    $this->setX($this->GetX()+80);
                    $this->cell(23,5,'Rekanan',0,0,'L',1);
                    $this->cell(2,5,':',0,0,'L',1);
                    $this->cell(2,5, $this->kelompok->dataKelompok->nama,0,0,'L',1);
                }

                $this->ln();
                $this->Cell(50,5,'No. Order',0,0,'L',1);
                $this->cell(2,5,':',0,0,'L',1);
                 if($this->data->id_workorder_barang != '0'){
                    $this->cell(2,5, $this->data->getOrder->no_workorder,0,0,'L',1);
                }else{
                    $this->cell(2,5, '-',0,0,'L',1);
                }
                
                $this->ln();        
                $this->Ln();                                             

                $this->setFont('Arial','B',13);
                $this->Cell(10,5,'Detail Barang',0,0,'L',1);
                $this->Ln(10);

                $this->setFont('arial','B',10);
                

               

                if(in_array($this->data->id_proses, setup_corakbon())){
                    $this->SetWidths(array(10, 20, 16, 26, 20, 30, 16, 18, 40, 7));
                    $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C','C','C','C'));
                    $this->Row(array("No", "Tanggal", "Benang", "Ket", "Warna", "Gudang", "Jumlah", "Satuan", "Keterangan", ""));

                    $this->SetAligns(array('C', "C", "L", "L", "L", "L", "R", "L", "L", "C"));
                }else{
                    $this->SetWidths(array(10, 20, 40, 30, 18, 18, 40, 20, 7));
                    $this->SetAligns(array('C', 'C', 'C', 'C', 'C','C','C','C', 'C'));
                    $this->Row(array("No", "Tanggal", "Nama Barang", "Gudang", "Jumlah", "Satuan", "Keterangan", "Kualitas", ""));

                    $this->SetAligns(array('C', "C", "L", "L", "R", "L", "L", "L", "C"));
                }
            }
				
	}
	
	function Content()
	{   
        
        $this->setFont('Arial','',10);
        
        if(in_array($this->data->id_proses, setup_corakbon())){
            $this->SetAligns(array('C', "C", "L", "L", "L", "L", "R", "L", "L","C"));

            $no = 1;
            $jumlah = array();
            if(count($this->detail) > 0){

                foreach ($this->detail as $k) {
                    $nama_barang = get_empty($k->inisial_komposisi); //.get_empty($k->nama_kualitas_proses).get_empty($k->nama_motif).get_empty($k->nama_kualitas).get_empty($k->kualitas_dudul).get_empty($k->nama_kualitas_periksa).get_empty($k->nama_label).get_empty($k->nama_cap).get_empty($k->nama_kemasan).get_empty($k->nama_kemasansatuan).get_empty($k->nama_kemasansepuluh).get_empty($k->nama_kemasanseratus);

                    $this->Row(array($no, tgl_full($k->tanggal_proses, '3'), get_empty($k->nama_barang), get_empty($k->nama_corak), $nama_barang, $k->nama_gudang, format_angka($k->jumlah), $k->nama_satuan, $k->keterangan, ""));

                    $no++;

                    if(isset($jumlah[$k->id_satuan])){
                        $jumlah[$k->id_satuan] = ['nama_satuan' => $k->nama_satuan, 'jumlah' => $jumlah[$k->id_satuan]['jumlah']+$k->jumlah];
                    }else{
                        $jumlah[$k->id_satuan] = ['nama_satuan' => $k->nama_satuan, 'jumlah' => $k->jumlah];
                    }
                }
            }else{
                $this->Row(array("", "", "", "", "", "", "", "", ""));
            }

        }else{
            $this->SetAligns(array('C', "C", "L", "L", "R", "L", "L","C"));

                $no = 1;
                $jumlah = array();
                if(count($this->detail) > 0){
                    foreach ($this->detail as $k) {
                    //    $nama_barang = get_empty($k->nama_barang).get_empty($k->inisial_komposisi).get_empty($k->nama_kualitas_proses).get_empty($k->nama_corak).get_empty($k->nama_motif).get_empty($k->nama_kualitas).get_empty($k->kualitas_dudul).get_empty($k->nama_kualitas_periksa).get_empty($k->nama_label).get_empty($k->nama_cap).get_empty($k->nama_kemasan).get_empty($k->nama_kemasansatuan).get_empty($k->nama_kemasansepuluh).get_empty($k->nama_kemasanseratus);

                        if(in_array($k->id_proses, setup_benang())){
                            $nama_barang = get_empty($k->nama_barang).get_empty($k->inisial_komposisi);
                        }else if(in_array($k->id_proses, setup_corakbon())){
                            $nama_barang = '';
                            if($k->id_proses == 24){
                                $nama_barang .= get_empty($k->nama_motif);
                            }else{
                                $nama_barang .= get_empty($k->nama_corak);
                            }
                            $nama_barang .= get_empty($k->inisial_komposisi); 
                        } else{  
                            $nama_barang = get_empty($k->nama_barang).get_empty($k->nama_motif);
                        }


                        $this->Row(array($no, tgl_full($k->tanggal_proses, '3'), $nama_barang, $k->nama_gudang, format_angka($k->jumlah), $k->nama_satuan, $k->keterangan, "", ""));

                        $no++;

                        if(isset($jumlah[$k->id_satuan])){
                            $jumlah[$k->id_satuan] = ['nama_satuan' => $k->nama_satuan, 'jumlah' => $jumlah[$k->id_satuan]['jumlah']+$k->jumlah];
                        }else{
                            $jumlah[$k->id_satuan] = ['nama_satuan' => $k->nama_satuan, 'jumlah' => $k->jumlah];
                        }
                    }
                }else{
                    $this->Row(array("", "", "", "", "", "", "", "", ""));
                }
        }

        if(in_array($this->data->id_proses, setup_corakbon())){
            foreach($jumlah as $j){
                $this->SetWidths(array(122, 16, 18, 40, 7));
                $this->SetAligns(array('R', 'R', 'L', 'L', 'C'));
                $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], '', ''));    
            }
        }else{
            foreach($jumlah as $j){
                $this->SetWidths(array(100, 18, 18, 60, 7));
                $this->SetAligns(array('R', 'R', 'L', 'L', 'C'));
                $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], '', ''));    
            }
        }

        // ----------------------------------------------------------------------------------------------------------------------------------------
        // Tanda tangan

        $this->Ln();
        $this->setX($this->paper_h*30/100);
        $this->cell(51, 5, "Mengetahui", "TLR", 0, "C");
        $this->cell(50, 5, "Penerima", "TLR", 1, "C");

        $this->setX($this->paper_h*30/100);
        $this->cell(51, 15, "", "BLR", 0, "C");
        $this->cell(50, 15, "", "BLR", 1, "C");
	}
	
	function Footer()
	{
        $this->Ln();
        $this->SetY(-15);
        $this->SetFont('Arial','',9);
        $this->Cell(0,10, $this->PageNo(),0,0,'C');
	}

	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->kelompok=$data['kelompok'];
$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Bon Bahan - ".$data['data']['noAuto'].$data['data']['noAuto_ext']);
$Pdf->Output("Bon Bahan - ".$data['data']['noAuto'].$data['data']['noAuto_ext'].".pdf", "I");


?>