 <?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,'KARTU STOK',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->Row_noborder(array("Motif", ": ", ($this->induk['nama_motif'] != '') ? $this->induk['nama_motif']:"-", "Lokasi", ":", ($this->induk['nama_lokasi'] != '') ? $this->induk['nama_lokasi']:$this->induk['lokasi_gudang']));
                $arr_pemroses = array();
                $pemroses = '';
                if($this->induk['kelompok'] != '' || $this->induk['rekanan']){
                    $arr_pemroses[] = $this->induk['kelompok'];
                    $arr_pemroses[] = $this->induk['rekanan'];

                    $pemroses = get_stripe($arr_pemroses);
                }

                $this->Row_noborder(array("Corak", ": ", ($this->induk['nama_corak'] != '') ? $this->induk['nama_corak']:"-", "Pemroses", ":", $pemroses));



                $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){
            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_warna']);
                $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);
                }

                $garis = '';
                $ket_proses = '';
                if($k['nama_corak'] != '' || $k['nama_motif'] != '' || $nama_warna != ''){
                    $garis = " -";
                }
                if($k['ket_proses'] != ''){
                    $ket_proses  = ' -'.get_empty($k['ket_proses']);
                }
            //    $nama_barang = get_empty($k['nama_subkategori']).get_empty($k['nama_barang']).$garis.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']).get_empty($k['ket_5']).$ket_proses;
                $nama_barang = $k['concat_nama'];

                $masuk = 0;
                $keluar = 0;
                $awal_masuk = 0;
                $awal_keluar = 0;
                $total = 0;
                if($k['jenis'] == 0){
                    $this->Row(array($no, tgl_full($k['tanggal'], ''), 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;
                    $total = $k['total'];

                    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']+$total];
                    }else{
                        $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'masuk' => $awal_masuk+$masuk, 'keluar' => $awal_keluar+$keluar, 'jumlah' => $total];
                    }
                }else{
                    $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'];
                    $total = 0;

                    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']+$total];
                    }else{
                        $jumlah[$k['id_satuan']] = ['nama_satuan' => $k['nama_satuan'], 'masuk' => $awal_masuk+$masuk, 'keluar' => $awal_keluar+$keluar, 'jumlah' => $total];
                    }
                }
                $no++; 

                    

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

        $jum = array_merge($jumlah, array());
        $jumlah_total = 0;

        error_reporting(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'));
                $jumlah_total = $jum[$i]['jumlah']+$jum[$i]['masuk']-$jum[$i]['keluar'];
                $this->Row(array("Total", format_angkav2($jum[$i]['masuk']), format_angkav2($jum[$i]['keluar']), format_angkav2($jumlah_total), $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'));
                $jumlah_total = $jum[$i]['jumlah']+$jum[$i]['masuk']-$jum[$i]['keluar'];
                $this->Row(array("Total", format_angkav2($j['masuk']), format_angkav2($j['keluar']), format_angkav2($jumlah_total), $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->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 Dalam Proses Tenun _".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']));
$Pdf->Output("Mutasi Rekap Dalam Proses Tenun _".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']).".pdf", "I");

?>