  <?php
class Pdf extends PDF_MC_Table{
	//Page header
	
    public $height_p = 210;
    public $width_p = 330;
    public $head = 0;
	function __construct() {
		//set Page 
		parent::__construct('P','mm',array($this->height_p , $this->width_p));
		
    }
	function Header(){
        
		if($this->PageNo() != 0){		
                $this->setFont('Arial','B',14);
                $this->setFillColor(255,255,255);
                
                $this->cell(0,7,'Laporan Pengambilan Beam',0,0,'L',1);
                $this->Ln(10);
                $this->setFont('Arial','',11);
                
                $text_makelar = ''; $text_petenun = '';
                $nama_makelar = ''; $nama_petenun = '';


                if($this->header['id_makelar'] != ''){
                    $text_makelar = "Makelar";
                    $nama_makelar = $this->header['nama_makelar'];

                    $text_petenun = "Petenun";
                    $nama_petenun = $this->header['nama_rekanan'];
                }else{
                    $text_makelar = "Makelar";
                    $nama_makelar = "Semua Makelar";

                    $text_petenun = "Petenun";
                    $nama_petenun = "Semua Petenun";
                }

                if($this->header['id_rekanan'] != ''){
                    $text_petenun = "Petenun";
                    $nama_petenun = $this->header['nama_rekanan'];

                    if($this->header['id_makelar'] == ''){
                        $text_makelar = "Makelar";
                        $nama_makelar = DB::table('ms_rekanan AS mr')->leftjoin("ms_rekanan_kelompok AS mrk", "mr.id_kelompok", "mrk.id_kelompok")->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->where("mr.id_rekanan", $this->header['id_rekanan'])->first()->nama_kelompok;
                    }
                }

                $this->SetWidths(array($this->width_p*5/100, $this->width_p*2/100, $this->width_p*20/100, $this->width_p*5/100, $this->width_p*8/100, $this->width_p*2/100, $this->width_p*20/100));
                $this->SetAligns(array("L", "L", "L", "L", "L", "L", "L"));
                $this->Row_noborder(array("Periode", ":", $this->header['tanggal_range'], '', $text_makelar, ($text_makelar != '') ? ":":" ", $nama_makelar));
                $this->Row_noborder(array("Lokasi", ":", $this->header['nama_lokasi'], '', $text_petenun, ($text_petenun != '') ? ":":" ", $nama_petenun));
                



                $this->Ln();
                $this->setFont('arial','B', 10);

                $this->SetWidths(array($this->width_p*4/100, $this->width_p*8/100, $this->width_p*6/100, $this->width_p*25/100, $this->width_p*8/100, $this->width_p*10/100));
                $this->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C'));
                $this->Row(array("No", "Tanggal", "No Prod", "Keterangan", "Qty", "Target"), 7);

            /*    $this->setFont('arial', '', 8);
                $this->SetWidths(array($this->width_p*5/100, $this->width_p*10/100, $this->width_p*9/100, $this->width_p*27/100, $this->width_p*10/100));
                $this->SetAligns(array('C', 'C', 'C', 'L', 'R'));*/

                if($this->head == 0){
                    $this->setFont('arial', '', 8);
                    $this->SetWidths(array($this->width_p*4/100, $this->width_p*8/100, $this->width_p*6/100, $this->width_p*25/100, $this->width_p*8/100, $this->width_p*10/100));
                    $this->SetAligns(array('C', 'C', 'L', 'L', 'R', 'R'));
                }else if($this->head == 1){
                    $this->setFont('arial', 'B', 9);
                    $this->SetWidths(array($this->width_p*43/100, $this->width_p*8/100, $this->width_p*10/100));
                    $this->SetAligns(array('C', 'R', 'R'));
                }else{
                    $this->setFont('arial', 'B', 9);
                    $this->SetWidths(array($this->width_p*43/100, $this->width_p*8/100, $this->width_p*10/100));
                    $this->SetAligns(array('C', 'R', 'R'));
                }
            }
				
	}
	
	function Content()
	{          
    
        $no = 1;
        $arr = array();
        
        $jumlah = array();
        $jumlah_konv = array();
        if(count($this->hasil) > 0){
            foreach($this->hasil as $h){
                $nama_barang = ''; 
                $motif = '';
                $proses = '';
                $barang = ''; $tenun = ''; $dudul = ''; $label = ''; $work = '';
                
                if($h->id_sort == '0'){
                    $this->head = 0;

                    $nama_barang = ''; $target = ''; $target_rubah = '';
                    if($h->id_motif != ''){
                        $nama_barang = get_stripe([$h->nama_motif, $h->inisial_komposisi]);
                    }
                    error_reporting(0);
                    if($h->id_perubahansatuan != ''){
                        $target = $h->jumlah*$h->jumlah_rencana;
                        $target_rubah = " ".$h->inisial_perubahan;

                        
                    }


                    //get_empty($h->inisial_komposisi)." Tenun".get_empty($h->nama_kualitas)." Dudulan ".get_empty($h->kualitas_dudul);
                    $this->setFont('arial', '', 9);
                    $this->SetWidths(array($this->width_p*4/100, $this->width_p*8/100, $this->width_p*6/100, $this->width_p*25/100, $this->width_p*8/100, $this->width_p*10/100));
                    $this->SetAligns(array('C', 'C', 'L', 'L', 'R', 'R'));

                    $this->Row(array($no, tgl_full($h->tanggal, ''), $h->noAuto.$h->noAuto_ext, $nama_barang, format_angkav2($h->jumlah)." ".$h->inisial_satuan, format_angkav2($target).$target_rubah));

                    $no++;

                    if(isset($jumlah[$h->id_satuan])){
                        $jumlah[$h->id_satuan] = ['nama_satuan' => $h->inisial_satuan, 'jumlah' => $jumlah[$h->id_satuan]['jumlah']+$h->jumlah];
                    }else{
                        $jumlah[$h->id_satuan] = ['nama_satuan' => $h->inisial_satuan, 'jumlah' => $h->jumlah];
                    }

                    if(isset($jumlah_konv[$h->id_perubahansatuan])){
                        $jumlah_konv[$h->id_perubahansatuan] = ['nama_satuan' => $h->inisial_perubahan, 'jumlah' => $jumlah_konv[$h->id_perubahansatuan]['jumlah']+($h->jumlah*$h->jumlah_rencana)];
                    }else{
                        $jumlah_konv[$h->id_perubahansatuan] = ['nama_satuan' => $h->inisial_perubahan, 'jumlah' => ($h->jumlah*$h->jumlah_rencana)];
                    }
                }

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

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

        $j_jumlah = (count($jumlah) > count($jumlah_konv)) ? count($jumlah):count($jumlah_konv);


        for($i=0; $i<$j_jumlah; $i++){
            $this->head = 1;

            $this->setFont('arial', 'B', 9);
            $this->SetWidths(array($this->width_p*43/100, $this->width_p*8/100, $this->width_p*10/100));
            $this->SetAligns(array('C', 'R', 'R'));

            $jum_awal = (isset($jumlah[$i]['jumlah'])) ? $jumlah[$i]['jumlah']:"";
            $jum_awal_satuan = (isset($jumlah[$i]['nama_satuan'])) ? $jumlah[$i]['nama_satuan']:"";
            $jum_akhir = (isset($jumlah_konv[$i]['jumlah'])) ? $jumlah_konv[$i]['jumlah']:"";
            $jum_akhir_satuan = (isset($jumlah_konv[$i]['nama_satuan'])) ? $jumlah_konv[$i]['nama_satuan']:"";
            
            $this->Row(array("Total", format_angkav2($jum_awal)." ".$jum_awal_satuan, format_angkav2($jum_akhir)." ".$jum_akhir_satuan), 7);
        }


        
        $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->header=$data['header'];
$Pdf->hasil=$data['detail'];


$Pdf->SetMargins(5,5,5);
$Pdf->AliasNbPages();
$Pdf->AddPage();
$Pdf->SetFont('Arial','',12);
$Pdf->Content();
$Pdf->SetTitle("Laporan Pengambilan Beam - ".$data['header']['nama_proses']."_".$data['header']['tanggal_range']);
$Pdf->Output("Laporan Pengambilan Beam - ".$data['header']['nama_proses']."_".$data['header']['tanggal_range'].".pdf", "I");

?>