 <?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 PERSEDIAAN GUDANG CORAK',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 Corak", ":", ($this->induk['nama_corak'] != '') ? $this->induk['nama_corak']:"Semua Corak"));
                $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->Ln();
                $this->setFont('arial','B', 8);
                
                $y = $this->getY();
                $this->Cell(10, 14, "NO", 1, 0, "C", 1);
                $this->Cell(109, 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, 109, 17, 17, 17, 17, 15));
                $this->setFont('arial','',8);
                $this->SetAligns(array('C', 'L', 'R', 'R', 'R', 'R', 'C'));
            }
				
	}
	
	function Content()
	{   
        $this->SetWidths(array(10, 109, 17, 17, 17, 17, 15));
        $this->setFont('arial','',8);
        $this->SetAligns(array('C', 'L', 'R', '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']);

                $garis = '';
                $garis_proses = '';
                if($k['nama_corak'] != '' || $nama_komposisi){
                    $garis = " -";
                }
                if($nama_proses != ''){
                    $garis_proses = " -";
                }
               

            //    $nama_barang = get_empty($k['nama_subkategori']).get_empty($k['nama_barang']).$garis.get_empty($k['nama_corak']).$nama_komposisi.$garis_proses.$nama_proses;
                $nama_barang = $k['concat_nama'];

                if($k['awal'] != 0 || $k['masuk'] != 0 || $k['keluar'] != 0 || $k['sisa'] != 0){
                    $this->Row(array($no, $nama_barang, format_angkav2($k['awal']), format_angkav2($k['masuk']), format_angkav2($k['keluar']), format_angkav2($k['sisa']), $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']+$k['awal'] ,'masuk' => $jumlah[$k['id_satuan']]['masuk']+$k['masuk'], 'keluar' => $jumlah[$k['id_satuan']]['keluar']+$k['keluar'], 'sisa' => $jumlah[$k['id_satuan']]['sisa']+$k['sisa']];
                    }else{
                        $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'awal' => $k['awal'], 'masuk' => $k['masuk'], 'keluar' => $k['keluar'], 'sisa' => $k['sisa']];
                    }  

                
            }
        }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'));
                $this->Row(array("Total", format_angkav2($jum[$i]['awal']), format_angkav2($jum[$i]['masuk']), format_angkav2($jum[$i]['keluar']), format_angkav2($jum[$i]['sisa']), $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'));
                $this->Row(array("Total", format_angkav2($j['awal']), format_angkav2($j['masuk']), format_angkav2($j['keluar']), format_angkav2($j['sisa']), $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 Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']));
$Pdf->Output("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']).".pdf", "I");

?>