 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(110 ,150 ));
		
    }
	function Header(){
        
		if($this->PageNo() != 0){		
                $this->setFont('Arial','B',9);
                $this->setFillColor(255,255,255);
                
                $this->SetWidths(array(54, 18, 30));
                $this->SetAligns(array("L", "L", "L"));
                $this->Row_noborder(array("Laporan Hasil ".strtoupper($this->masuk->nama_proses), "Tanggal ", ": ".tgl_full($this->masuk->tanggal_selesai, '')));

                
                $peg = '';

                $text_nomor = ''; $isi_nomor = '';

                if($this->masuk->nama_pegawai != ''){
                    $peg = $this->masuk->nama_pegawai;
                }else if($this->masuk->nama_rekanan != ''){
                    $peg = $this->masuk->nama_rekanan;
                }else{
                    $peg = $this->masuk->nama_mesin;
                }

                $this->SetWidths(array(16, 38, 18, 30));
                $this->SetAligns(array("L", "L", "L"));

                if($this->masuk->id_rekanan != 0){
                    $this->Row_noborder(array("Rekanan ", ": ".$this->masuk->nama_kelompok, "Nomor ", ": ".$this->masuk->noAuto.$this->masuk->noAuto_ext));
                }else{
                    $text_nomor = "Nomor ";
                    $isi_nomor = ": ".$this->masuk->noAuto.$this->masuk->noAuto_ext;
                }

                $this->Row_noborder(array("Nama ", ": ".$peg, $text_nomor, $isi_nomor));
                if($this->masuk->no_workorder_barang != ''){
                    $this->Row_noborder(array("No Order", ": ".$this->masuk->no_workorder_barang, " ", " "));
                }
                
                $this->ln();

                if(in_array($this->masuk->id_proses, setup_corak()))                                      {
                    $this->SetWidths(array(10, 14, 23, 13, 16, 16, 6));
                }else{
                    $this->SetWidths(array(10, 50, 16, 16, 6));
                }
            }
				
	}
	
	function Content()
	{    
        $key = array();
        $count = 0;
        foreach($this->setor->get() as $g){
            if($g->id_perubahansatuan != 0){
                $key[] = [$g->id_perubahansatuan];
            }
        }
        $count = count($key);
        $row_header = array("NO", "KETERANGAN", "QTY", "SATUAN", "");
        $align_data = array('C', 'L', 'R', 'L');
        
        if($count > 0){
            $row_header = array("NO", "KETERANGAN", "QTY", "QTY", "");
            $align_data = array('C', 'L', 'R', 'R');
        }

        if(in_array($this->masuk->id_proses, setup_corak())){
            $nm_corak = '';
            if($this->masuk->id_proses == '24'){
                $nm_corak = "KET";
            }else{
                $nm_corak = "KET";
            }

            $row_header = array("NO", "BENANG", $nm_corak, "WARNA", "QTY", "SATUAN", "");
            $align_data = array('C', 'L', 'L', 'L', 'R', 'L');
                
            if($count > 0){
                $row_header = array("NO", "BENANG", $nm_corak, "WARNA", "QTY", "QTY", "");
                $align_data = array('C', 'L', 'L', 'L', 'R', 'R', 'C');
            }
        }


        $this->setFont('arial','',8);

        if(in_array($this->masuk->id_proses, setup_corak())){
            $this->SetWidths(array(10, 14, 23, 13, 16, 16, 6));
            $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C'));
        }else{
            $this->SetWidths(array(10, 50, 16, 16, 6));
            $this->SetAligns(array('C', 'C', 'C', 'C', 'C'));
        }

        
        $this->Row($row_header);  

        $this->SetAligns($align_data);

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

            if(in_array($this->masuk->id_proses, setup_corak())){
                

                if($this->masuk->id_proses == '24'){
                    $this->setor = $this->setor->orderby('nama_motif', 'asc');
                }else{
                    $this->setor = $this->setor->orderby('nama_corak', 'asc');
                }

            }

            foreach ($this->setor->get() as $k) {
                
                if(in_array($this->masuk->id_proses, setup_corak())){
                    $nm_corak = '';
                    if($this->masuk->id_proses == '24'){
                        $nm_corak = get_empty($k->nama_motif);
                    }else{
                        $nm_corak = get_empty($k->nama_corak);
                    }

                    $arr_data = array($no, get_empty($k->nama_barang), $nm_corak, get_empty($k->inisial_komposisi), format_angka($k->jumlah), $k->nama_satuan, '');
                    if($count > 0){
                        $arr_data = array($no, get_empty($k->nama_barang), $nm_corak, get_empty($k->inisial_komposisi), format_angka($k->jumlah)." ".$k->nama_satuan, format_angka($k->jumlah*$k->jumlah_rencana)." ".$k->nama_perubahansatuan, '');
                    }
                }else{
                //    $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);
                        }

                    $arr_data = array($no, $nama_barang, format_angka($k->jumlah), $k->nama_satuan, '');
                    if($count > 0){
                        $arr_data = array($no, $nama_barang, format_angka($k->jumlah)." ".$k->nama_satuan, format_angka($k->jumlah*$k->jumlah_rencana)." ".$k->nama_perubahansatuan, '');
                    }
                }

                

                $this->Row($arr_data);

                $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];
                }

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

        $jum = array_merge($jumlah, array());
        $jum_konv = array_merge($jumlah_konv, array());

        if($count > 0){
            for($i=0; $i<count($jumlah); $i++){
                $this->SetWidths(array(60, 16, 16, 6));
                $this->SetAligns(array('R', 'R', 'R', 'C'));
                $this->Row(array("Total", $jum[$i]['jumlah']." ".$jum[$i]['nama_satuan'], $jum_konv[$i]['jumlah']." ".$jum_konv[$i]['nama_satuan'], ''));
            }
        }else{
            foreach($jumlah as $j){
                $this->SetWidths(array(60, 16, 16, 6));
                $this->SetAligns(array('R', 'R', 'L', 'C'));
                $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], ''));    
            }  
        }


        $this->Ln();

        $this->SetWidths(array(49, 49));
        $this->SetAligns(array('C', 'C'));
        $this->Row(array("Mengetahui", "Penerima"));
        $this->Row(array("\n\n\n", ''));



	}
	
	function Footer()
	{
		$this->Ln();
		
		
		
		$this->SetY(-15);
		//buat garis horizontal
		
		//Arial italic 9
		$this->SetFont('Arial','',9);
		//nomor halaman
		//$this->SetFont('tahoma','',9);
		$this->Cell(0,10, $this->PageNo(),0,0,'C');
	}
	
}
$Pdf = new Pdf();
$Pdf->masuk = $data['masuk'];
$Pdf->setor = $data['setor'];
$Pdf->SetAutoPageBreak(true ,15);

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

?>