 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	//public $kas_masuk,$tgl1,$tgl2;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array(110 ,150 ));
		
    }
	function Header(){
        
		if($this->PageNo() == 1){		
                $this->setFont('Arial','B',9);
                $this->setFillColor(255,255,255);
                
                $this->SetWidths(array(52, 18, 30));
                $this->SetAligns(array("L", "L", "L"));
                $this->Row_noborder(array("Laporan Hasil ".strtoupper($this->data->nama_proses), "Tanggal ", ": ".tgl_full($this->data->tanggal_selesai, '')));

                
                $peg = '';
                $text_nomor = '';
                $isi_nomor = '';
                if($this->data->nama_pegawai != ''){
                    $peg = $this->data->nama_pegawai;
                }else{
                    $peg = $this->data->nama_rekanan;
                }
                $this->SetWidths(array(16, 38, 18, 30));
                $this->SetAligns(array("L", "L", "L"));

                if($this->data->id_rekanan != 0){
                    $this->Row_noborder(array("Rekanan", ": ".$this->data->nama_kelompok, "Nomor ", ": ".$this->data->noAuto.$this->data->noAuto_ext));
                }else{
                    $text_nomor = "Nama ";
                    $isi_nomor = ": ".$this->data->noAuto.$this->data->noAuto_ext;
                }

                $this->Row_noborder(array("Nama ", ": ".$peg, $text_nomor, $isi_nomor));
                
                $this->ln();                                            

            }
				
	}
	
	function Content()
	{    
        $key = array();
        $count = 0;
        foreach($this->detail as $g){
            if($g->id_perubahansatuan != 0){
                $key[] = [$g->id_perubahansatuan];
            }
        }
        $count = count($key);
        $row_header = array("NO", "KETERANGAN", "QTY", "SATUAN", "");
        $align_data = array('C', 'L', 'R', 'L');
        if($count > 0){
            $row_header = array("NO", "KETERANGAN", "QTY", "QTY", "");
            $align_data = array('C', 'L', 'R', 'R');
        }

        $this->setFont('arial','',8);
        $this->SetWidths(array(10, 50, 16, 16, 6));
        $this->SetAligns(array('C', 'C', 'C', 'C', 'C'));
        $this->Row($row_header);  

        $this->SetAligns($align_data);

        $no = 1;
        $jumlah = array();
        $jumlah_konv = array();
        if($this->detail->count() > 0){
            foreach ($this->detail as $k) {
            //    $nama_barang = get_empty($k->nama_barang).get_empty($k->inisial_komposisi).get_stripe([get_empty($k->nama_motif), ($k->nama_kualitas == '') ? "":" Tenun ".get_empty($k->nama_kualitas), ($k->kualitas_dudul == '') ? "":" Dudulan ".get_empty($k->kualitas_dudul)]);
                $nama_barang = get_stripe([get_empty($k->nama_barang), get_empty($k->nama_motif)]);
                $arr_data = array($no, $nama_barang, format_angka($k->jumlah), $k->nama_satuan, '');
                if($count > 0){
                    $arr_data = array($no, $nama_barang, format_angka($k->jumlah*$k->jumlah_rencana)." ".$k->nama_perubahansatuan, format_angka($k->jumlah)." ".$k->nama_satuan, '');
                }

                $this->Row($arr_data);

                $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];
                }

                if(isset($jumlah_konv[$k->id_perubahansatuan])){
                    $jumlah_konv[$k->id_perubahansatuan] = ['nama_satuan' => $k->nama_perubahansatuan, 'jumlah' => $jumlah_konv[$k->id_perubahansatuan]['jumlah']+($k->jumlah*$k->jumlah_rencana)];
                }else{
                    $jumlah_konv[$k->id_perubahansatuan] = ['nama_satuan' => $k->nama_perubahansatuan, 'jumlah' => ($k->jumlah*$k->jumlah_rencana)];
                }
            }
        }else{
            $this->Row(array("", "", "", "", ""));
        }

        $jum = array_merge($jumlah, array());
        $jum_konv = array_merge($jumlah_konv, array());

        if($count > 0){
            for($i=0; $i<count($jumlah); $i++){
                $this->SetWidths(array(60, 16, 16, 6));
                $this->SetAligns(array('R', 'R', 'R', 'C'));
                $this->Row(array("Total", $jum_konv[$i]['jumlah']." ".$jum_konv[$i]['nama_satuan'], $jum[$i]['jumlah']." ".$jum[$i]['nama_satuan'], ''));
            }
        }else{
            foreach($jumlah as $j){
                $this->SetWidths(array(60, 16, 16, 6));
                $this->SetAligns(array('R', 'R', 'L', 'C'));
                $this->Row(array("Total", format_angka($j['jumlah']), $j['nama_satuan'], ''));    
            }  
        }


        $this->Ln();

        $this->SetWidths(array(49, 49));
        $this->SetAligns(array('C', 'C'));
        $this->Row(array("Mengetahui", "Penerima"));
        $this->Row(array("\n\n\n", ''));



	}
	
	function Footer()
	{
		$this->Ln();
		
		
		
		$this->SetY(-15);
		//buat garis horizontal
		
		//Arial italic 9
		$this->SetFont('Arial','',9);
		//nomor halaman
		//$this->SetFont('tahoma','',9);
		$this->Cell(0,10, $this->PageNo(),0,0,'C');
	}
	
}
$Pdf = new Pdf();
$Pdf->detail = $data['detail'];
$Pdf->data = $data['data'];
$Pdf->SetAutoPageBreak(true ,15);

$Pdf->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Laporan Hasil - ".$data['data']->noAuto.$data['data']->noAuto_ext);
$Pdf->Output("Laporan Hasil - ".$data['data']->noAuto.$data['data']->noAuto_ext.".pdf", "I");

?>