 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(210 , 330 ));
		
    }
	function Header(){
        
		if($this->PageNo() != 0){		
                $this->setFont('Arial','B',14);
                $this->setFillColor(255,255,255);
                /*if(count($this->induk['nama_gudang']) > 0 AND count($this->induk['barang']) > 0){*/
                  $this->cell(0,7,'KARTU PERSEDIAAN GUDANG TENUN',0,0,'C',1);  
                /*}else{
                  $this->cell(0,7,'KARTU '.$this->induk['barang']->nama_barang,0,0,'C',1);  
                }*/
                $this->Ln(10);
                $this->setFont('Arial','',11);
                
                $this->ln();
                $this->SetWidths(array(30, 5, 60, 30, 5, 60));
                $this->SetAligns(array("L", "L", "L"));
                $barang = "Semua Barang";
                
                if(count($this->induk['barang']) > 0){
                    $barang = $this->induk['barang']->nama_barang;
                }                

                $this->Row_noborder(array("Nama Barang", ": ", $barang, "Nama Kategori",": ",($this->induk['kategori'] != '') ? $this->induk['kategori']:" ")); //

                $tgl = '';
                if($this->induk['tanggal_awal'] != '' && $this->induk['tanggal_akhir'] != ''){
                    $tgl = date_range($this->induk['tanggal_awal'], $this->induk['tanggal_akhir']);
                }

                $this->Row_noborder(array("Periode", ": ", $tgl, "Nama Motif", ":", ($this->induk['nama_motif'] != '') ? $this->induk['nama_motif']:"Semua Motif"));
                $this->Row_noborder(array("Gudang", ": ", ($this->induk['nama_gudang'] != '') ? $this->induk['nama_gudang']:"-","Lokasi", ":", ($this->induk['nama_lokasi'] != '') ? $this->induk['nama_lokasi']:$this->induk['lokasi_gudang']));

                //if($this->induk['nama_proses'] != ''){
                //$this->Row_noborder(array("Nama Proses", ": ", ($this->induk['nama_proses'] != '') ? $this->induk['nama_proses']:"-"));
                //}
                $this->Row_noborder(array("Nama Proses", ": ", ($this->induk['nama_proses'] != '') ? $this->induk['nama_proses']:"-", "Warna", ": ", ($this->induk['inisial_komposisi'] != '') ? $this->induk['inisial_komposisi']:"-"));


                $this->Ln();
                $this->setFont('arial','B', 8);
                
                $y = $this->getY();
                $this->Cell(10, 14, "NO", 1, 0, "C", 1);
                $this->Cell(17, 14, "TANGGAL", 1, 0, "C", 1);
                $this->Cell(50, 14, "KETERANGAN", 1, 0, "C", 1);
                $this->Cell(50, 14, "KET. BARANG", 1, 0, "C", 1);
                $this->Cell(60, 7, "JUMLAH BARANG", 1, 0, "C", 1);
                $this->Cell(15, 14, "SATUAN", 1, 0, "C", 1);

                
                $this->SetXY(132, $y+7);
                $this->Cell(20, 7, "MASUK", 1, 0, "C", 1);
                $this->SetXY(152, $y+7);
                $this->Cell(20, 7, "KELUAR", 1, 0, "C", 1);
                $this->SetXY(172, $y+7);
                $this->Cell(20, 7, "JUMLAH", 1, 0, "C", 1);

                $this->Ln();

                $this->SetWidths(array(10, 17, 50, 50, 20, 20, 20, 15));
                $this->setFont('arial','',8);
                $this->SetAligns(array('C', 'L', 'L', 'L', 'R', 'R', 'R', 'C'));
            }
				
	}
	
	function Content()
	{   
        $this->SetWidths(array(10, 17, 50, 50, 20, 20, 20, 15));
        $this->setFont('arial','',8);
        $this->SetAligns(array('C', 'L', 'L', 'L', 'R', 'R', 'R', 'C'));
        
    
        $no = 1;
        $arr = array();        
        $jumlah = array();
        if(count($this->detail) > 0){
            $key = '';
            foreach ($this->detail as $k) {
                $nama_proses = ($this->induk['nama_proses'] != '') ? '':get_empty($k['nama_proses']);
                $nama_komposisi = ($this->induk['nama_komposisi'] != '') ? '':get_empty($k['inisial_komposisi']);
                $nama_warna = ($k['inisial_warna'] != '' ) ?  get_empty($k['inisial_warna']) : '';

                if($k['jenis'] == 0){
                    $this->setFont('arial','B',8);             
                }else{
                    $this->setFont('arial','',8);
                }
                $garis = '';
                $garis_proses = '';
                $ket_proses = '';
                $nama_barang = ''; $barang = '';
                if($k['nama_motif'] != ''){
                    $garis = " -";
                }
                if($nama_proses != ''){
                    $garis_proses = " -";
                }
                if($k['ket_proses'] != ''){
                    $ket_proses  = ' -'.get_empty($k['ket_proses']);
                }

                if($k['id_motif'] != ''){
                    $motif = $k['nama_motif'];
                }else{
                    $motif = $k['nama_corak'];
                }

                if($this->induk['id_proses'] == ''){
                    $proses = $k['nama_proses'];
                }

                if($motif == ''){
                    $barang = $k['nama_barang'];
                }


            //    $nama_barang = get_empty($k['nama_subkategori']).get_empty($k['nama_barang']).$garis.get_empty($k['nama_motif']).$garis_proses.$nama_proses.$ket_proses;
                               
            //    $nama_barang = $k['nama_subkategori'].get_stripe([get_empty($k['nama_barang']), get_empty($motif)]).get_stripe([get_empty($k['inisial_warna']), ($k['id_workorder_barang'] == '') ? "":get_empty($k['no_workorder_barang']), ($k['id_kualitas'] == '') ? "":" Tenun ".$k['nama_kualitas'], ($k['id_kualitas_dudul'] == '') ? "":" Dudulan ".$k['kualitas_dudul'], ($k['id_label'] == '') ? "":get_empty($k['nama_label']), ($k['id_kemasansatuan'] == '') ? "":get_empty($k['nama_kemasansatuan']), ($k['id_kemasansepuluh'] == '') ? "":get_empty($k['nama_kemasansepuluh']), ($k['id_kemasanseratus'] == '') ? "":get_empty($k['nama_kemasanseratus'])]); //get_empty($proses)
            
            //    $nama_barang = get_stripe([get_empty($barang), get_empty($motif), $nama_komposisi, ($k['id_workorder_barang'] == '') ? "":get_empty($k['no_workorder_barang'])]);
                $nama_barang = $k['concat_nama'];

                $masuk = 0;
                $keluar = 0;
                $awal_masuk = 0;
                $awal_keluar = 0;
                if($k['jenis'] == 0){
                $this->Row(array($no, '', get_empty($k['ket_3']).get_empty($k['ket_4']), '', 0, 0, format_angkav2($k['total']), $k['nama_satuan'] ));
                $awal_masuk = 0;
                $awal_keluar = 0;
                }else{
                
                $ket_5 = "";
                if($this->induk['id_lokasi'] != 1 && $this->induk['_proses'] == 24){
                    if($k['ket_3'] == "Saldo Awal" && $k['ket_5'] != ""){
                        $nama_barang = $k['concat_nama']." - ".$k['ket_5'];
                    }

                    if($k['ket_3'] == "Bon Bahan"){
                        $d_keluar = DB::table("pro_barangkeluar AS pb")->join("ms_mesin AS mm", "pb.id_mesin", "mm.id_mesin")->where("pb.id_barangkeluar", $k['id'])->select("mm.nama AS nama_mesin")->get();
                        $d_count = $d_keluar->count();

                        if($d_count > 0){
                            $nama_barang = $k['concat_nama']." - ".$d_keluar->first()->nama_mesin;
                        }
                    }
                }
                
                $this->Row(array($no, tgl_full($k['tanggal'], ''), get_empty($k['ket_3']).get_empty($k['ket_4']), $nama_barang, format_angkav2($k['masuk']), format_angkav2($k['keluar']), format_angkav2($k['total']), $k['nama_satuan'] ));
                $masuk = $k['masuk'];
                $keluar = $k['keluar'];
                }
                    $no++; 

                    if(isset($jumlah[$k['id_satuan']])){
                    $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'masuk' => $jumlah[$k['id_satuan']]['masuk']+$awal_masuk+$masuk, 'keluar' => $jumlah[$k['id_satuan']]['keluar']+$awal_keluar+$keluar, 'jumlah' => $jumlah[$k['id_satuan']]['jumlah']+($k['jumlah']+$k['masuk'])-$k['keluar']];
                    }else{
                        $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'masuk' => $awal_masuk+$masuk, 'keluar' => $awal_keluar+$keluar, 'jumlah' => ($k['jumlah']+$k['masuk'])-$k['keluar']];
                    }  

                
            }
        }else{
            $this->Row(array("", "", "", "", "", "", "", "")); 
        }

        $jum = array_merge($jumlah, array());
        $jumlah_total = 0;
        if($jumlah > 0){
            for($i=0; $i<count($jumlah); $i++){
                $this->SetWidths(array(127, 20, 20, 20, 15));
                $this->SetAligns(array('R', 'R', 'R', 'R', 'C'));
                $this->Row(array("Total", format_angkav2($jum[$i]['masuk']), format_angkav2($jum[$i]['keluar']), format_angkav2($jum[$i]['jumlah']), $jum[$i]['nama_satuan'] ));
            }
        }else{
            foreach($jumlah as $j){
                $this->SetWidths(array(127, 20, 20, 20, 15));
                $this->SetAligns(array('R', 'R', 'R', 'R', 'R', 'C'));
                $this->Row(array("Total", format_angkav2($j['masuk']), format_angkav2($j['keluar']), format_angkav2($jum[$i]['jumlah']), $j['nama_satuan'] ));    
            }  
        }

        $this->Ln(5);
        $this->setFont('arial','',9);
        $ttd = get_ttdproduksi();
        $p = 210;
        $arr_width = array(); $arr_align = array(); $arr_isi = array(); $arr_ttd = array();
        if(count($ttd) > 0){
            $width = 210/count($ttd);

            for($i = 0; $i < count($ttd); $i++){
                $arr_width[] = $width;
                $arr_align[] = "C";
                $arr_isi[] = $ttd[$i]['nama'];
                $arr_ttd[] = ($ttd[$i]['ttd'] == '') ? "_____________":$ttd[$i]['ttd'];
            }

            $this->SetWidths($arr_width);
            $this->SetAligns($arr_align);
            $this->Row_noborder($arr_isi);
            $this->Ln(15);
            $this->Row_noborder($arr_ttd);
        }

        
        $this->setFillColor(255,255,255);
        
        
	}
	
	function Footer()
	{
        $this->Ln();
        $this->SetY(-15);
        $this->SetFont('Arial','',9);
        $this->Cell(0,10, "Dicetak tanggal : ".tgl_full(date("d-m-Y"), 6),0,0,'L');
        
		$this->Ln();
        $this->SetY(-15);
        $this->SetFont('Arial','',9);
        $this->Cell(0,10, $this->PageNo(),0,0,'C');
	}
	
}
$Pdf = new Pdf();
$Pdf->induk=$data['induk'];
$Pdf->where=$data['where'];
$Pdf->detail=$data['detail'];
$Pdf->SetAutoPageBreak(true ,15);
$barang = 'Semua Barang';
if($data['induk']['barang'] != false){
    $barang = $data['induk']['barang']->nama_barang;
}

$Pdf->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Mutasi ".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']));
$Pdf->Output("Mutasi ".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']).".pdf", "I");

?>