<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Laporan_rekap_ev_tri extends CI_Controller {
	public function __construct(){
        parent::__construct();		
        $this->load->model('M_laporan_rekap_ev_tri');
        $this->load->model('M_unit_misioner');
    }
    private function ubahDateTime($dateTime){
        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        $date = explode(" ", $dateTime)[0];
        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0]." ".explode(" ", $dateTime)[1];
    }
    private function date_to_date($date){
        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0];
    }
    public function laporan_rekap_evaluasi_pertriwulan_pdf($periode_thn,$triwulan,$unit_misioner){
        $result = $this->M_laporan_rekap_ev_tri->laporan_rekap_evaluasi_pertriwulan_pdf($periode_thn,$triwulan,$unit_misioner)->result_array();
        for($i=0;$i<count($result);$i++){
            $trw = count(json_decode($result[$i]['triwulan'], true)); 
            $result[$i]['pen'] = $result[$i]['spn']/$trw;
            $result[$i]['peng'] = $result[$i]['spg']/$trw;
        }
        if($unit_misioner == "all"){
            $data['unit_misioner'] = $unit_misioner;
        }else{
            $data["id_unit_misioner"] = $unit_misioner;
            $respon = $this->M_unit_misioner->ambil_unit_misioner($data)->row_array();
            $data['unit_misioner'] = $respon['nama'];
        }
        // echo json_encode($respon);
        $data['result'] = $result;
        $data['periode_thn'] = $periode_thn;
        $data['triwulan'] = $triwulan;
        
        $mpdf = new \Mpdf\Mpdf();
        $dat = $this->load->view('laporan/rekap/ev_tri/pdf/v_laporan_rekap_ev_tri_pdf', $data, TRUE);
        $mpdf->AddPage("L","","","","","5","5","15","5","","","","","","","","","","","","Legal");
        $mpdf->WriteHTML($dat);
        $mpdf->Output();
    }
    public function laporan_rekap_evaluasi_pertriwulan_excel($periode_thn,$triwulan,$unit_misioner){
        $result = $this->M_laporan_rekap_ev_tri->laporan_rekap_evaluasi_pertriwulan_pdf($periode_thn,$triwulan,$unit_misioner)->result_array();
        for($i=0;$i<count($result);$i++){
            $trw = count(json_decode($result[$i]['triwulan'], true)); 
            $result[$i]['pen'] = $result[$i]['spn']/$trw;
            $result[$i]['peng'] = $result[$i]['spg']/$trw;
        }
        if($unit_misioner == "all"){
            $data['unit_misioner'] = $unit_misioner;
        }else{
            $data["id_unit_misioner"] = $unit_misioner;
            $respon = $this->M_unit_misioner->ambil_unit_misioner($data)->row_array();
            $data['unit_misioner'] = $respon['nama'];
        }
        // echo json_encode($respon);
        $data['result'] = $result;
        $data['periode_thn'] = $periode_thn;
        $data['triwulan'] = $triwulan;
        $this->load->view('laporan/rekap/ev_tri/excel/v_laporan_rekap_ev_tri_excel', $data);
    }
    public function laporan_rekap_evaluasi_pertriwulan_pdf_user($periode_thn,$triwulan){
        $result = $this->M_laporan_rekap_ev_tri->laporan_rekap_evaluasi_pertriwulan_pdf_user($periode_thn,$triwulan)->result_array();
        for($i=0;$i<count($result);$i++){
            $trw = count(json_decode($result[$i]['triwulan'], true)); 
            $result[$i]['pen'] = $result[$i]['spn']/$trw;
            $result[$i]['peng'] = $result[$i]['spg']/$trw;
        }
        $unit_misioner = $this->session->userdata("unit_misioner");
        if($unit_misioner == "all"){
            $data['unit_misioner'] = $unit_misioner;
        }else{
            $data["id_unit_misioner"] = $unit_misioner;
            $respon = $this->M_unit_misioner->ambil_unit_misioner($data)->row_array();
            $data['unit_misioner'] = $respon['nama'];
        }
        // echo json_encode($respon);
        $data['result'] = $result;
        $data['periode_thn'] = $periode_thn;
        $data['triwulan'] = $triwulan;
        
        $mpdf = new \Mpdf\Mpdf();
        $dat = $this->load->view('laporan/rekap/ev_tri/pdf/v_laporan_rekap_ev_tri_pdf_user', $data, TRUE);
        $mpdf->AddPage("L","","","","","5","5","15","5","","","","","","","","","","","","Legal");
        $mpdf->WriteHTML($dat);
        $mpdf->Output();
    }
    public function laporan_rekap_evaluasi_pertriwulan_excel_user($periode_thn,$triwulan){
        $result = $this->M_laporan_rekap_ev_tri->laporan_rekap_evaluasi_pertriwulan_pdf_user($periode_thn,$triwulan)->result_array();
        for($i=0;$i<count($result);$i++){
            $trw = count(json_decode($result[$i]['triwulan'], true)); 
            $result[$i]['pen'] = $result[$i]['spn']/$trw;
            $result[$i]['peng'] = $result[$i]['spg']/$trw;
        }
        $unit_misioner = $this->session->userdata("unit_misioner");
        if($unit_misioner == "all"){
            $data['unit_misioner'] = $unit_misioner;
        }else{
            $data["id_unit_misioner"] = $unit_misioner;
            $respon = $this->M_unit_misioner->ambil_unit_misioner($data)->row_array();
            $data['unit_misioner'] = $respon['nama'];
        }
        // echo json_encode($respon);
        $data['result'] = $result;
        $data['periode_thn'] = $periode_thn;
        $data['triwulan'] = $triwulan;
        $this->load->view('laporan/rekap/ev_tri/excel/v_laporan_rekap_ev_tri_excel_user', $data);
    }
}