<?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");*/

        $this->setup_corak = [25, 26, 27, 28, 29, 30, 31, 32, 33];
    }
	function Header(){
        
		if($this->PageNo() != 0){	

            $spacing = 5;
            $kolom_1 = 40;
            $kolom_2 = 5;
            $kolom_3 = 40;
            $ln = 3;

            $indent_1 = $kolom_1+$kolom_2+$kolom_3+20;

                $this->setFont('Arial','B',14);
                $this->setFillColor(255,255,255);
                $this->setTextColor(0,0,0);
                
                $this->cell(0, 5, "Laporan Hasil - ".$this->detail->nama_proses, 0, 0, "C");
                $this->Ln(10);

                $this->setFont('Arial','',10);
                $y = $this->getY();


                $this->setX($indent_1);
                $this->cell($kolom_1, $spacing, "Pemroses", 0, 0, "L");
                $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                $pemroses = '';
                if($this->detail->nama_pegawai != ''){
                    $pemroses = $this->detail->nama_pegawai;
                }else if($this->detail->nama_rekanan != ''){
                    $pemroses = $this->detail->nama_rekanan;
                }else{
                    $pemroses = $this->detail->nama_mesin;
                }
                $this->MultiCell($kolom_3, $spacing, $pemroses, "FJ");

                $this->Ln($ln);
                $this->SetY($y);
                $this->cell($kolom_1, $spacing, "Tanggal Pengambilan", 0, 0, "L");
                $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                $this->MultiCell($kolom_3, $spacing, tgl_full($this->detail->tanggal_ambil, '0'), "FJ");

                $this->Ln($ln);
                $y = $this->getY();
                $this->cell($kolom_1, $spacing, "Tanggal Perkiraan", 0, 0, "L");
                $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                $this->MultiCell($kolom_3, $spacing, tgl_full($this->detail->tanggal_perkiraan, '0'), "FJ");

                if($this->detail->id_rekanan != 0){
                    $this->setXY($indent_1, $y);
                    $this->cell($kolom_1, $spacing, "Rekanan", 0, 0, "L");
                    $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                    $this->MultiCell($kolom_3, $spacing, $this->detail->nama_kelompok, "FJ");
                }

                $this->Ln($ln);
                $this->cell($kolom_1, $spacing, "Tanggal Selesai", 0, 0, "L");
                $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                $this->MultiCell($kolom_3, $spacing, tgl_full($this->detail->tanggal_selesai, '0'), "FJ");

                $this->Ln($ln);
                $this->cell($kolom_1, $spacing, "Nomor Order", 0, 0, "L");
                $this->cell($kolom_2, $spacing, ":", 0, 0, "C");
                $this->MultiCell($kolom_3, $spacing, ($this->detail->no_workorder_barang != '') ? $this->detail->no_workorder_barang:' - ', "FJ");     
                $this->Ln(5);


                $spacing = 5;
                $this->setFont('Arial','B',10);
                $this->cell(0, $spacing, "Barang Diambil", 0, 0, "L");
                $this->Ln(10);

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

                if(in_array($this->detail->id_proses, setup_corak())){
                    $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, 50, 20, 20, 18, 47, 10));
                    $this->SetAligns(array('C', 'C', 'C', 'C', 'C','C','C','C'));
                    $this->Row(array("No", "Tanggal", "Nama Barang", "Gudang", "Jumlah", "Satuan", "Keterangan", ""));
                }
            }
				
	}
	
	function Content()
	{   

        $spacing = 5;
            $kolom_1 = 40;
            $kolom_2 = 5;
            $kolom_3 = 40;
            $ln = 3;

            $indent_1 = $kolom_1+$kolom_2+$kolom_3+20;

            $this->setFont('Arial','',10);
            
            // ------------------------ Detail Barang Diambil ---------------------------

            if(in_array($this->detail->id_proses, setup_corak())){
                $this->SetAligns(array('C', "C", "L", "L", "L", "L", "R", "L", "L","C"));

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

                    if($this->detail->id_proses == '24'){
                        $this->keluar = $this->keluar->orderby('nama_motif');
                    }else{
                        $this->keluar = $this->keluar->orderby('nama_corak');
                    }

                    foreach ($this->keluar->get() 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("", "", "", "", "", "", "", "", ""));
                }

                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{

                $this->SetAligns(array('C', "C", "L", "L", "R", "L", "L","C"));
                $no = 1;
                $jumlah = array();
                if($this->keluar->count() > 0){
                    foreach ($this->keluar->get() 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);

                            $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("", "", "", "", "","","",""));
                    }

                    foreach($jumlah as $j){
                        $this->SetWidths(array(100, 20, 18, 47, 10));
                        $this->SetAligns(array('R', 'R', 'L', 'L', 'L'));
                        $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], '', ''));    
                    }
            }

            // ------------------------ Detail Barang Diambil ---------------------------

                $this->Ln(5);
                $this->setFont('Arial','B',10);
                $this->cell(0, $spacing, "Barang Disetor", 0, 0, "L");
                
                
                $this->Ln(10);
                $this->setFont('Arial','B',10);
                
                // ----------------- Header Barang Setor ------------------------------------

                if(in_array($this->detail->id_proses, setup_corak())){
                    $nm_corak = '';
                    if($this->detail->id_proses == '24'){
                        $nm_corak = "Ket";
                    }else{
                        $nm_corak = "Ket";
                    }
                    $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", $nm_corak, "Warna", "Gudang", "Jumlah", "Satuan", "Keterangan", ""));

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

                // ----------------- Header Barang Setor ------------------------------------

                $this->setFont('Arial','',10);
                
                // ----------------- Barang Setor ------------------------------------

                if(in_array($this->detail->id_proses, setup_corak())){
                    $this->SetAligns(array('C', "C", "L", "L", "L", "L", "R", "L", "L","C"));

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

                        

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

                        foreach ($this->setor->get() as $k) {

                            $nm_corak = '';
                            if($this->detail->id_proses == '24'){
                                $nm_corak = get_empty($k->nama_motif);
                            }else{
                                $nm_corak = get_empty($k->nama_corak);
                            }
                            $nama_barang = get_empty($k->inisial_komposisi).get_empty($k->nama_kualitas_proses).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_setor, '3'), get_empty($k->nama_barang), $nm_corak, $nama_barang, $k->nama_gudang, format_angka($k->jumlah), $k->nama_satuan, $k->keterangan, ""));

                            $no++;

                            if(isset($jumlah_masuk[$k->id_satuan])){
                                $jumlah_masuk[$k->id_satuan] = ['nama_satuan' => $k->nama_satuan, 'jumlah' => $jumlah_masuk[$k->id_satuan]['jumlah']+$k->jumlah];
                            }else{
                                $jumlah_masuk[$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_masuk = array();
                    if(count($this->setor) > 0){
                        foreach ($this->setor->get() 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_setor, '3'), $nama_barang, $k->nama_gudang, format_angka($k->jumlah), $k->nama_satuan, $k->keterangan,""));
                            $no++;

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

                // ----------------- Barang Setor ------------------------------------


                if(in_array($this->detail->id_proses, setup_corak())){
                    foreach($jumlah_masuk 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_masuk as $j){
                        $this->SetWidths(array(100, 20, 18, 47, 10));
                        $this->SetAligns(array('R', 'R', 'L', 'L', 'C'));
                        $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], '', ''));    
                    }
                }

                if(count($this->sisa) > 0){

                    $this->Ln(5);
                    $this->setFont('Arial','B',10);
                    $this->cell(0, $spacing, "Barang Belum Disetor", 0, 0, "L");
                    
                    
                    $this->Ln(10);
                    $this->setFont('Arial','B',10);

                    if(in_array($this->detail->id_proses, setup_corak())){
                        $nm_corak = '';
                        if($this->detail->id_proses == '24'){
                            $nm_corak = "Ket";
                        }else{
                            $nm_corak = "Ket";
                        }
                        $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", $nm_corak, "Warna", "Gudang", "Jumlah", "Satuan", "Keterangan", ""));

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

                    $this->setFont('Arial','',10);

                    if(in_array($this->detail->id_proses, setup_corak())){
                        $this->SetAligns(array('C', "C", "L", "L", "L", "L", "R", "L", "L","C"));

                        $no = 1;
                        $jumlah_sisa = array();
                        if(count($this->sisa) > 0){

                            foreach ($this->sisa as $k) {

                                $nm_corak = '';
                                if($this->detail->id_proses == '24'){
                                    $nm_corak = get_empty($k->nama_motif);
                                }else{
                                    $nm_corak = get_empty($k->nama_corak);
                                }
                                $nama_barang = get_empty($k->inisial_komposisi).get_empty($k->nama_kualitas_proses).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, "", get_empty($k->nama_barang), $nm_corak, $nama_barang, $k->nama_gudang, format_angka($k->stok), $k->nama_satuan, $k->keterangan, ""));

                                $no++;

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

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

                        $no = 1;
                        $jumlah_sisa = array();
                        if(count($this->sisa) > 0){
                            foreach ($this->sisa as $k) {
                                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, "", $nama_barang, "", format_angka($k->stok), $k->nama_satuan, $k->keterangan,""));
                                $no++;

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

                    if(in_array($this->detail->id_proses, setup_corak())){
                        foreach($jumlah_sisa 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_sisa as $j){
                            $this->SetWidths(array(100, 20, 18, 47, 10));
                            $this->SetAligns(array('R', 'R', 'L', 'L', 'C'));
                            $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], '', ''));    
                        }
                    }
                }

                $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');
	}
}
$Pdf = new Pdf();
$Pdf->detail = $data['masuk'];
$Pdf->keluar = $data['barang'];
$Pdf->setor = $data['setor'];
$Pdf->sisa = $data['sisa'];
$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");

?>