 <?php
class Pdf extends PDF_MC_Table{
	//Page header
	
    public $height_p = 210;
    public $width_p = 330;
    public $head = 0;
    public $base_width = array();
    public $base_width_header_1 = array();
    public $base_width_last = array();
    public $base_align = array();
    public $base_align_header = array();
    public $base_row_data = array();
    public $base_align_data = array();
    public $base_align_data_last = array();
	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,'Rekap Laporan Hasil Produksi Tenun / Jenis Produksi',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);
                $row_kualitas = 36;

                $this->base_width = [$this->width_p*3/100, $this->width_p*16/100];
                $base_width_header = [$this->width_p*$row_kualitas/100];
                $this->base_width_last = [$this->width_p*6/100];
                $this->base_width_header_1 = array();

                $this->base_align = array('C', 'C');
                $this->base_align_header = array();

                $base_text = array();

                $this->base_align_data = ["C", "L"];


                $this->SetWidths(array_merge($this->base_width, $base_width_header, $this->base_width_last));
                $this->SetAligns(array('C', 'C', 'C', 'C'));
                $this->Row_custom(array("No", "Jenis Produksi", "K w a l i t a s", "Qty"), 7, "T");

                $wid = $row_kualitas/$this->header['count_kualitas'];
                
                for($i = 0; $i < $this->header['count_kualitas']; $i++){
                    $this->base_width_header_1[] = $this->width_p*$wid/100;
                    $this->base_align_header[] = "C";
                    $base_text[] = $this->header['nama_kualitas'][$i];
                    $this->base_row_data[] = "tenun_".$this->header['id_kualitas'][$i];
                    $this->base_align_data_last[] = "R";
                }

                $this->SetWidths(array_merge($this->base_width, $this->base_width_header_1, $this->base_width_last));
                $this->SetAligns(array_merge($this->base_align, $this->base_align_header));
                $this->Row_custom(array_merge(array("", ""), $base_text, array("")), 7, "B");

                


            /*    $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', '', 9);
                    $this->SetWidths(array_merge($this->base_width, $this->base_width_header_1, $this->base_width_last));
                    $this->SetAligns(array_merge($this->base_align, $this->base_align_header));
                }else if($this->head == 1){
                    $this->setFont('arial', 'B', 9);
                    $this->SetWidths(array_merge([$this->width_p*19/100], $this->base_width_header_1, $this->base_width_last));
                    $this->SetAligns(array_merge(['R'], $this->base_align_data_last, ["R"]));
                }else{
                    $this->setFont('arial', 'B', 9);
                    $this->SetWidths(array($this->width_p*51/100, $this->width_p*10/100));
                    $this->SetAligns(array('C', 'R'));
                }
            }
				
	}
	
	function Content()
	{          
    
        $no = 1;
        $arr = array();
        $jum = array();

        if(count($this->hasil) > 0){
            
            foreach($this->hasil as $h){
                if($h->id_sort == 0){
                    $this->head = 0;
                    $nama_barang = ''; 
                    $motif = '';
                    $proses = '';
                    $barang = ''; $tenun = ''; $dudul = ''; $label = ''; $work = '';

                    $nama_barang = '';
                    if($h->nama_barang != ''){
                        $nama_barang = get_empty($h->nama_barang);
                    }
                    $this->setFont('arial', '', 9);


                    $this->SetWidths(array_merge($this->base_width, $this->base_width_header_1, $this->base_width_last));
                    $this->SetAligns(array_merge($this->base_align_data, $this->base_align_data_last, ["R"]));


                    $row_data = array();
                    foreach($this->base_row_data as &$d){
                        $row_data[] = $h->$d;
                    }
                   // unset($row_data);

                    $this->Row_custom(array_merge([$no, $nama_barang], $row_data, [format_angkav2($h->row_total)." ".$h->inisial_satuan]), 5, "B"); 

                    $no++;
                }else{
                    $this->head = 1;
                    
                    $row_data = array();
                    foreach($this->base_row_data as &$d){
                        $row_data[] = $h->$d;
                    }

                    $this->setFont('arial', 'B', 9);
                    $this->SetWidths(array_merge([$this->width_p*19/100], $this->base_width_header_1, $this->base_width_last));
                    $this->SetAligns(array_merge(['R'], $this->base_align_data_last, ["R"]));

                    $this->Row_custom(array_merge(["T o t a l"], $row_data, [format_angkav2($h->row_total)." ".$h->inisial_satuan]), 7, "B");   
                }

            }
        }else{
            $row_data = array();
            foreach($this->base_row_data as &$d){
                $row_data[] = "";
            }
            $this->head = 1;
            $this->Row_custom(array_merge(["", ""], $row_data, [""]), 5, "TB"); 
        }

   
        
        $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("Rekap Laporan Hasil Produksi Tenun per Jenis Produksi - ".$data['header']['nama_proses']."_".$data['header']['tanggal_range']);
$Pdf->Output("Rekap Laporan Hasil Produksi Tenun per Jenis Produksi - ".$data['header']['nama_proses']."_".$data['header']['tanggal_range'].".pdf", "I");

?>