 <?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);
                
                $this->cell(0,7,'MUTASI 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_kategori." - ".$this->induk['barang']->nama_sub." - ".$this->induk['barang']->nama_barang;
                    $barang = $this->induk['barang']->nama_barang;
                }                

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

                $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, "Warna", ":", ($this->induk['nama_komposisi'] != '') ? $this->induk['nama_komposisi']:"Semua Warna"));
                $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']));
                


                $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(33, 14, "KETERANGAN", 1, 0, "C", 1);
                $this->Cell(59, 14, "KET. BARANG", 1, 0, "C", 1);
                $this->Cell(68, 7, "JUMLAH BARANG", 1, 0, "C", 1);
                $this->Cell(15, 14, "SATUAN", 1, 0, "C", 1);

                $this->SetXY(124, $y+7);
                $this->cell(17, 7, "Awal", 1, 0, "C", 1);
                $this->SetXY(141, $y+7);
                $this->Cell(17, 7, "MASUK", 1, 0, "C", 1);
                $this->SetXY(158, $y+7);
                $this->Cell(17, 7, "KELUAR", 1, 0, "C", 1);
                $this->SetXY(175, $y+7);
                $this->Cell(17, 7, "SISA", 1, 0, "C", 1);

                $this->Ln();

                $this->SetWidths(array(10, 17, 33, 59, 17, 17, 17, 17, 15));
                $this->setFont('arial','',8);
                $this->SetAligns(array('C', 'L', 'L', 'L', 'R', 'R', 'R', 'C'));
            }
				
	}
	
	function Content()
	{   
        $this->SetWidths(array(10, 17, 33, 59, 17, 17, 17, 17, 15));
        $this->setFont('arial','',8);
        $this->SetAligns(array('C', 'L', 'L', 'L', 'R', 'R', 'R', 'R', 'C'));
        
    
        $no = 1;
        $arr = array();        
        $jumlah = array();
        if(count($this->detail) > 0){
            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 = (get_empty($k['inisial_warna']) != '' ) ?  get_empty($k['inisial_warna']) : '';

                if($k['jenis'] == 0){
                    $this->setFont('arial','B',8);
                }else{
                    $this->setFont('arial','',8);
                }
                if($k['id_proses']==25 || $k['id_proses']==26 || $k['id_proses']==27 || $k['id_proses']==28 || $k['id_proses']==29 || $k['id_proses']==30 || $k['id_proses']==31 || $k['id_proses']==26 || $k['id_proses']==33){
                $nama_barang = get_empty($k['nama_kualitas_proses']).get_empty($k['nama_corak']).get_empty($k['nama_motif']).$nama_warna.get_empty($k['no_workorder_barang']).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']);
                }elseif($k['id_proses']==''){
                    $nama_barang = get_empty($k['nama_barang']).get_empty($k['nama_kualitas_proses']).get_empty($k['nama_corak']).get_empty($k['nama_motif']).$nama_warna.get_empty($k['no_workorder_barang']).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']);
                }else{
                $nama_barang = get_empty($k['nama_barang']).get_empty($k['nama_kualitas_proses']).get_empty($k['nama_corak']).get_empty($k['nama_motif']).$nama_warna.get_empty($k['no_workorder_barang']).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($k['jenis'] == 0){
                    $key = $k['masuk'];
                }else{
                    $this->Row(array($no, tgl_full($k['tanggal'], ''), get_empty($k['ket_3']).get_empty($k['ket_4']), $nama_barang, $key, $k['masuk'], $k['keluar'], $k['total'], $k['nama_satuan']));  
                    $no++;
                    if(isset($jumlah[$k['id_satuan']])){
                    $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'awal' => $jumlah[$k['id_satuan']]['awal']+$key ,'masuk' => $jumlah[$k['id_satuan']]['masuk']+$k['masuk'], 'keluar' => $jumlah[$k['id_satuan']]['keluar']+$k['keluar'], 'jumlah' => $jumlah[$k['id_satuan']]['jumlah']+$k['total']];
                    }else{
                        $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'awal' => $key, 'masuk' => $k['masuk'], 'keluar' => $k['keluar'], 'jumlah' => $k['total']];
                    }     

                    $key = 0;
                
                }
            }
        }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(119, 17, 17, 17, 17, 15));
                $this->SetAligns(array('R', 'R', 'R', 'R', 'R', 'C'));
                $jumlah_total = ($jum[$i]['awal']+$jum[$i]['masuk'])-$jum[$i]['keluar'];
                $this->Row(array("Total", $jum[$i]['awal'], $jum[$i]['masuk'], $jum[$i]['keluar'], $jumlah_total, $jum[$i]['nama_satuan'] ));
            }
        }else{
            foreach($jumlah as $j){
                $this->SetWidths(array(119, 17, 17, 17, 17, 15));
                $this->SetAligns(array('R', 'R', 'R', 'R', 'R', 'C'));
                $jumlah_total = ($j['awal']+$j['masuk'])-$j['keluar'];
                $this->Row(array("Total", $j['awal'], $j['masuk'], $j['keluar'], $jumlah_total, $j['nama_satuan'] ));    
            }  
        }
        
        $this->setFillColor(255,255,255);
        
        
        
        

    
	}
	
	function Footer()
	{
		$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 _".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']));
$Pdf->Output("Mutasi _".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']).".pdf", "I");

?>