<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Laporan_rekap_bidang extends CI_Controller {

	public function __construct(){

        parent::__construct();		

        $this->load->model('M_laporan_rekap_bidang');

    }

    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_bidang_all($periode_thn){

    //     $d['periode_thn'] = str_replace('-', '/', $periode_thn);

    //     $result = $this->M_laporan_rekap_bidang->data_pka_periode($d)->result_array();

    //     // echo json_encode($result);

    //     // var_dbidangp($result);

    //     $total_penerimaan = 0;

    //     $total_pengeluaran = 0;

    //     for($i=0; $i<count($result);$i++){

    //         // echo $result[$i]['nama_bidang']."==".$result[$i]['kode_pka']."<br>";

    //         $dd['bidang']=$result[$i]['bidang'];

    //         $dd['periode_thn'] = $d['periode_thn'];

    //         $r = $this->M_laporan_rekap_bidang->data_pka_periode_kode_pka($dd)->result_array();

    //         $jj = 0;

    //         $gg = 0;

    //         for($ii=0; $ii<count($r);$ii++){

    //             $ddd['kode_pka']=$r[$ii]['kode_pka'];

    //             ///penerimaan///

    //             $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

    //             $j = 0;

    //             for($iii=0; $iii<count($rr);$iii++){

    //                 // echo $rr[$iii]['penerimaan']."=======<br>";

    //                 $j +=$rr[$iii]['penerimaan'];

    //             }

    //             $jj += $j;

    //             ///END penerimaan///

    //             ///pengeluaran///

    //             $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

    //             $g = 0;

    //             for($iii=0; $iii<count($rrp);$iii++){

    //                 // echo $rr[$iii]['penerimaan']."=======<br>";

    //                 $g +=$rrp[$iii]['pengeluaran'];

    //             }

    //             $gg += $g;

    //             ///END penerimaan///

    //             // echo $j."---<br>";

    //         }

    //         $result[$i]['penerimaan'] = $jj;

    //         $result[$i]['pengeluaran'] = $gg;

    //         $total_penerimaan +=$jj;

    //         $total_pengeluaran +=$gg;

    //         // echo "<hr>".$jj."<br>";

    //     }

    //     $data['result'] = $result;

    //     $data['periode_thn'] = $d['periode_thn'];

    //     $data['total_penerimaan'] = $total_penerimaan;

    //     $data['total_pengeluaran'] = $total_pengeluaran;

    //     $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel',$data);

    // }

    public function laporan_rekap_bidang_pertriwulan($periode_thn){

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $result = $this->M_laporan_rekap_bidang->data_pka_periode($d)->result_array();

        // echo json_encode($result);

        // var_dbidangp($result);

        $total_penerimaan = 0;

        $total_pengeluaran = 0;

        $t_pen_t1 = 0;

        $t_pen_t2 = 0;

        $t_pen_t3 = 0;

        $t_pen_t4 = 0;

        $t_peng_t1 = 0;

        $t_peng_t2 = 0;

        $t_peng_t3 = 0;

        $t_peng_t4 = 0;

        for($i=0; $i<count($result);$i++){

            // echo $result[$i]['nama_bidang']."==".$result[$i]['kode_pka']."<br>";

            $dd['bidang']=$result[$i]['bidang'];

            $dd['periode_thn'] = $d['periode_thn'];

            $r = $this->M_laporan_rekap_bidang->data_pka_periode_kode_pka($dd)->result_array();

            $jj = 0;

            $gg = 0;

            $bidang_pen_t1 = 0;

            $bidang_pen_t2 = 0;

            $bidang_pen_t3 = 0;

            $bidang_pen_t4 = 0;

            $bidang_peng_t1 = 0;

            $bidang_peng_t2 = 0;

            $bidang_peng_t3 = 0;

            $bidang_peng_t4 = 0;

            for($ii=0; $ii<count($r);$ii++){

                $ddd['kode_pka']=$r[$ii]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                $j = 0;

                for($iii=0; $iii<count($rr);$iii++){

                    // echo $rr[$iii]['penerimaan']."=======<br>";

                    $j +=$rr[$iii]['penerimaan'];

                }

                $jj += $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    // echo $rr[$iii]['penerimaan']."=======<br>";

                    $g +=$rrp[$iii]['pengeluaran'];

                }

                $gg += $g;

                ///END penerimaan///

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                // echo $ct['triwulan']."////";

                // echo preg_replace("/[^0-9]/", "", $ju[0]);

                // echo ";;;".count($ju)."---";

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) { 

                    // echo preg_replace("/[^0-9]/", "", $ju[$tt])."<br>";

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                        // echo $pen_t1."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                        // echo $pen_t2."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                        // echo $pen_t3."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                        // echo $pen_t4."<br>";

                    }

                    // echo "<br>";

                }

                // echo "......".$g."......";

                // echo $pen_t1."|t1|";

                // echo $pen_t2."|t2|";

                // echo $pen_t3."|t3|";

                // echo $pen_t4."|t4|";

                // echo "<hr>";

                $bidang_pen_t1 += $pen_t1;

                $bidang_pen_t2 += $pen_t2;

                $bidang_pen_t3 += $pen_t3;

                $bidang_pen_t4 += $pen_t4;

                $bidang_peng_t1 += $peng_t1;

                $bidang_peng_t2 += $peng_t2;

                $bidang_peng_t3 += $peng_t3;

                $bidang_peng_t4 += $peng_t4;

                // echo $j."---<br>";

            }

            $result[$i]['penerimaan'] = $jj;

            $result[$i]['pengeluaran'] = $gg;

            $result[$i]['pen_t1'] = $bidang_pen_t1;

            $result[$i]['pen_t2'] = $bidang_pen_t2;

            $result[$i]['pen_t3'] = $bidang_pen_t3;

            $result[$i]['pen_t4'] = $bidang_pen_t4;

            $result[$i]['peng_t1'] = $bidang_peng_t1;

            $result[$i]['peng_t2'] = $bidang_peng_t2;

            $result[$i]['peng_t3'] = $bidang_peng_t3;

            $result[$i]['peng_t4'] = $bidang_peng_t4;

            // echo $bidang_pen_t1."<br>";

            // echo $bidang_pen_t2."<br>";

            // echo $bidang_pen_t3."<br>";

            // echo $bidang_pen_t4."<br>";

            // echo "<hr>";

            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

            $t_pen_t1 += $bidang_pen_t1;

            $t_pen_t2 += $bidang_pen_t2;

            $t_pen_t3 += $bidang_pen_t3;

            $t_pen_t4 += $bidang_pen_t4;

            // echo "<hr>".$jj."<br>";

            $t_peng_t1 += $bidang_peng_t1;

            $t_peng_t2 += $bidang_peng_t2;

            $t_peng_t3 += $bidang_peng_t3;

            $t_peng_t4 += $bidang_peng_t4;

        }

        $data['result'] = $result;

        $data['periode_thn'] = $d['periode_thn'];

        $data['total_penerimaan'] = $total_penerimaan;

        $data['total_pengeluaran'] = $total_pengeluaran;

        $data['t_pen_t1'] = $t_pen_t1;

        $data['t_pen_t2'] = $t_pen_t2;

        $data['t_pen_t3'] = $t_pen_t3;

        $data['t_pen_t4'] = $t_pen_t4;

        $data['t_peng_t1'] = $t_peng_t1;

        $data['t_peng_t2'] = $t_peng_t2;

        $data['t_peng_t3'] = $t_peng_t3;

        $data['t_peng_t4'] = $t_peng_t4;

        $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_pertriwulan',$data);

    }

    public function laporan_rekap_bidang_pertriwulan_pdf($periode_thn){

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $result = $this->M_laporan_rekap_bidang->data_pka_periode($d)->result_array();

        $total_penerimaan = 0;

        $total_pengeluaran = 0;

        $t_pen_t1 = 0;

        $t_pen_t2 = 0;

        $t_pen_t3 = 0;

        $t_pen_t4 = 0;

        $t_peng_t1 = 0;

        $t_peng_t2 = 0;

        $t_peng_t3 = 0;

        $t_peng_t4 = 0;

        for($i=0; $i<count($result);$i++){

            $dd['bidang']=$result[$i]['bidang'];

            $dd['periode_thn'] = $d['periode_thn'];

            $r = $this->M_laporan_rekap_bidang->data_pka_periode_kode_pka($dd)->result_array();

            $jj = 0;

            $gg = 0;

            $bidang_pen_t1 = 0;

            $bidang_pen_t2 = 0;

            $bidang_pen_t3 = 0;

            $bidang_pen_t4 = 0;

            $bidang_peng_t1 = 0;

            $bidang_peng_t2 = 0;

            $bidang_peng_t3 = 0;

            $bidang_peng_t4 = 0;

            for($ii=0; $ii<count($r);$ii++){

                $ddd['kode_pka']=$r[$ii]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                $j = 0;

                for($iii=0; $iii<count($rr);$iii++){

                    $j +=$rr[$iii]['penerimaan'];

                }

                $jj += $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    $g +=$rrp[$iii]['pengeluaran'];

                }

                $gg += $g;

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) {

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                    }

                }

                $bidang_pen_t1 += $pen_t1;

                $bidang_pen_t2 += $pen_t2;

                $bidang_pen_t3 += $pen_t3;

                $bidang_pen_t4 += $pen_t4;

                $bidang_peng_t1 += $peng_t1;

                $bidang_peng_t2 += $peng_t2;

                $bidang_peng_t3 += $peng_t3;

                $bidang_peng_t4 += $peng_t4;

            }

            $result[$i]['penerimaan'] = $jj;

            $result[$i]['pengeluaran'] = $gg;

            $result[$i]['pen_t1'] = $bidang_pen_t1;

            $result[$i]['pen_t2'] = $bidang_pen_t2;

            $result[$i]['pen_t3'] = $bidang_pen_t3;

            $result[$i]['pen_t4'] = $bidang_pen_t4;

            $result[$i]['peng_t1'] = $bidang_peng_t1;

            $result[$i]['peng_t2'] = $bidang_peng_t2;

            $result[$i]['peng_t3'] = $bidang_peng_t3;

            $result[$i]['peng_t4'] = $bidang_peng_t4;

            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

            $t_pen_t1 += $bidang_pen_t1;

            $t_pen_t2 += $bidang_pen_t2;

            $t_pen_t3 += $bidang_pen_t3;

            $t_pen_t4 += $bidang_pen_t4;

            $t_peng_t1 += $bidang_peng_t1;

            $t_peng_t2 += $bidang_peng_t2;

            $t_peng_t3 += $bidang_peng_t3;

            $t_peng_t4 += $bidang_peng_t4;

        }

        $data['result'] = $result;

        $data['periode_thn'] = $d['periode_thn'];

        $data['total_penerimaan'] = $total_penerimaan;

        $data['total_pengeluaran'] = $total_pengeluaran;

        $data['t_pen_t1'] = $t_pen_t1;

        $data['t_pen_t2'] = $t_pen_t2;

        $data['t_pen_t3'] = $t_pen_t3;

        $data['t_pen_t4'] = $t_pen_t4;

        $data['t_peng_t1'] = $t_peng_t1;

        $data['t_peng_t2'] = $t_peng_t2;

        $data['t_peng_t3'] = $t_peng_t3;

        $data['t_peng_t4'] = $t_peng_t4;

        // $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_pertriwulan',$data);

        $mpdf = new \Mpdf\Mpdf();

        // $data = $this->load->view('laporan/v_laporan_pka_excel',$data);

        $dat = $this->load->view('laporan/rekap/bidang/pdf/v_laporan_rekap_bidang_pdf_pertriwulan', $data, TRUE);

        // $data ="<h1>jhkjhkjh</h1>";

        $mpdf->AddPage("P","","","","","10","10","15","10","","","","","","","","","","","","Legal");

        $mpdf->WriteHTML($dat);

        $mpdf->Output();

    }

    public function laporan_rekap_bidang_pertriwulan_2($periode_thn,$id_bidang){

        // echo $periode_thn."==".$id_bidang;

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $d['id_bidang'] = explode("_", $id_bidang)[0];

    

        $tot_pen_t1 = 0;

        $tot_pen_t2 = 0;

        $tot_pen_t3 = 0;

        $tot_pen_t4 = 0;

        $tot_peng_t1 = 0;

        $tot_peng_t2 = 0;

        $tot_peng_t3 = 0;

        $tot_peng_t4 = 0;

        $tot_pen = 0;

        $tot_peng = 0;

        $result = $this->M_laporan_rekap_bidang->data_pka_periode_2($d)->result_array();

        for($i=0; $i<count($result);$i++){

                $ddd['kode_pka']=$result[$i]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                // echo $i."--".$ddd['kode_pka'].json_encode($rr).count($rr);

                $j = 0;

                for($ii=0; $ii<count($rr);$ii++){

                    if (empty($rr[$ii]['penerimaan'])) {

                        $j +=0;

                    }else{

                        $j +=$rr[$ii]['penerimaan'];    

                    }

                    

                }

                $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    // echo $rr[$iii]['penerimaan']."=======<br>";

                    if (empty($rrp[$iii]['pengeluaran'])) {

                        $g +=0;

                    }else{

                        $g +=$rrp[$iii]['pengeluaran'];

                    }

                }

                $g;

                ///END penerimaan///

                // echo $ddd['kode_pka']."==".$j."<hr>";

            // echo json_encode($g/*."==".$j*/) ."<br>";

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) { 

                    // echo preg_replace("/[^0-9]/", "", $ju[$tt])."<br>";

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                        // echo $pen_t1."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                        // echo $pen_t2."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                        // echo $pen_t3."<br>";

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                        // echo $pen_t4."<br>";

                    }

                    // echo "<br>";

                }

                // echo count($ju)."<br>";

                // echo $ddd['kode_pka']."===".$pen_t1."<br>";

                // echo $ddd['kode_pka']."===".$pen_t2."<br>";

                // echo $ddd['kode_pka']."===".$pen_t3."<br>";

                // echo $ddd['kode_pka']."===".$pen_t4."<br>";

                // echo "<br>";

                // echo $ddd['kode_pka']."===".$peng_t1."<br>";

                // echo $ddd['kode_pka']."===".$peng_t2."<br>";

                // echo $ddd['kode_pka']."===".$peng_t3."<br>";

                // echo $ddd['kode_pka']."===".$peng_t4."<br>";

                // echo "<hr>";

                $tot_pen_t1 += $pen_t1;

                $tot_pen_t2 += $pen_t2;

                $tot_pen_t3 += $pen_t3;

                $tot_pen_t4 += $pen_t4;

                $tot_peng_t1 += $peng_t1;

                $tot_peng_t2 += $peng_t2;

                $tot_peng_t3 += $peng_t3;

                $tot_peng_t4 += $peng_t4;

                $data[$i]["pen_t1"] = $pen_t1;

                $data[$i]["pen_t2"] = $pen_t2;

                $data[$i]["pen_t3"] = $pen_t3;

                $data[$i]["pen_t4"] = $pen_t4;

                $data[$i]["peng_t1"] = $peng_t1;

                $data[$i]["peng_t2"] = $peng_t2;

                $data[$i]["peng_t3"] = $peng_t3;

                $data[$i]["peng_t4"] = $peng_t4;

                $data[$i]["penerimaan"] = $j;

                $data[$i]["pengeluaran"] = $g;

                $tot_pen    += $j;

                $tot_peng   += $g;

                $data[$i]["kma"] = $result[$i]['kma'];

                $data[$i]["nama_um"] = $result[$i]['nama_um'];

                $data[$i]["nama_kegiatan"] = $result[$i]['nama_kegiatan'];

        }

        $data['result'] = $data;

        $data['nama_bidang'] = explode("_", $id_bidang)[1];

        $data["t_pen_t1"] = $tot_pen_t1;

        $data["t_pen_t2"] = $tot_pen_t2;

        $data["t_pen_t3"] = $tot_pen_t3;

        $data["t_pen_t4"] = $tot_pen_t4;

        $data["t_peng_t1"] = $tot_peng_t1;

        $data["t_peng_t2"] = $tot_peng_t2;

        $data["t_peng_t3"] = $tot_peng_t3;

        $data["t_peng_t4"] = $tot_peng_t4;

        $data["tot_pen"]   = $tot_pen;

        $data["tot_peng"]    = $tot_peng;

        $data['periode_thn'] = $d['periode_thn'];

        $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_pertriwulan_2',$data);

    }

    public function laporan_rekap_bidang_pertriwulan_pdf_2($periode_thn,$id_bidang){

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $d['id_bidang'] = explode("_", $id_bidang)[0];

    

        $tot_pen_t1 = 0;

        $tot_pen_t2 = 0;

        $tot_pen_t3 = 0;

        $tot_pen_t4 = 0;

        $tot_peng_t1 = 0;

        $tot_peng_t2 = 0;

        $tot_peng_t3 = 0;

        $tot_peng_t4 = 0;

        $tot_pen = 0;

        $tot_peng = 0;

        $result = $this->M_laporan_rekap_bidang->data_pka_periode_2($d)->result_array();

        for($i=0; $i<count($result);$i++){

                $ddd['kode_pka']=$result[$i]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                $j = 0;

                for($ii=0; $ii<count($rr);$ii++){

                    if (empty($rr[$ii]['penerimaan'])) {

                        $j +=0;

                    }else{

                        $j +=$rr[$ii]['penerimaan'];    

                    }

                }

                $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    if (empty($rrp[$iii]['pengeluaran'])) {

                        $g +=0;

                    }else{

                        $g +=$rrp[$iii]['pengeluaran'];

                    }

                }

                $g;

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) {

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                    }

                }

                $tot_pen_t1 += $pen_t1;

                $tot_pen_t2 += $pen_t2;

                $tot_pen_t3 += $pen_t3;

                $tot_pen_t4 += $pen_t4;

                $tot_peng_t1 += $peng_t1;

                $tot_peng_t2 += $peng_t2;

                $tot_peng_t3 += $peng_t3;

                $tot_peng_t4 += $peng_t4;

                $data[$i]["pen_t1"] = $pen_t1;

                $data[$i]["pen_t2"] = $pen_t2;

                $data[$i]["pen_t3"] = $pen_t3;

                $data[$i]["pen_t4"] = $pen_t4;

                $data[$i]["peng_t1"] = $peng_t1;

                $data[$i]["peng_t2"] = $peng_t2;

                $data[$i]["peng_t3"] = $peng_t3;

                $data[$i]["peng_t4"] = $peng_t4;

                $data[$i]["penerimaan"] = $j;

                $data[$i]["pengeluaran"] = $g;

                $tot_pen    += $j;

                $tot_peng   += $g;

                $data[$i]["kma"] = $result[$i]['kma'];

                $data[$i]["nama_um"] = $result[$i]['nama_um'];

                $data[$i]["nama_kegiatan"] = $result[$i]['nama_kegiatan'];

        }

        $data['result'] = $data;

        $data['nama_bidang'] = explode("_", $id_bidang)[1];

        $data["t_pen_t1"] = $tot_pen_t1;

        $data["t_pen_t2"] = $tot_pen_t2;

        $data["t_pen_t3"] = $tot_pen_t3;

        $data["t_pen_t4"] = $tot_pen_t4;

        $data["t_peng_t1"] = $tot_peng_t1;

        $data["t_peng_t2"] = $tot_peng_t2;

        $data["t_peng_t3"] = $tot_peng_t3;

        $data["t_peng_t4"] = $tot_peng_t4;

        $data["tot_pen"]   = $tot_pen;

        $data["tot_peng"]    = $tot_peng;

        $data['periode_thn'] = $d['periode_thn'];

        // $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_pertriwulan_2',$data);

        $mpdf = new \Mpdf\Mpdf();

        $dat = $this->load->view('laporan/rekap/bidang/pdf/v_laporan_rekap_bidang_pdf_pertriwulan_2', $data, TRUE);

        $mpdf->AddPage("L","","","","","10","10","15","5","","","","","","","","","","","","Legal");

        $mpdf->WriteHTML($dat);

        $mpdf->Output();

    }

    public function laporan_rekap_bidang_pertriwulan_pdf_3($periode_thn,$id_bidang){

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $d['id_bidang'] = explode("_", $id_bidang)[0];

    

        $tot_pen_t1 = 0;

        $tot_pen_t2 = 0;

        $tot_pen_t3 = 0;

        $tot_pen_t4 = 0;

        $tot_peng_t1 = 0;

        $tot_peng_t2 = 0;

        $tot_peng_t3 = 0;

        $tot_peng_t4 = 0;

        $tot_pen = 0;

        $tot_peng = 0;

        $result = $this->M_laporan_rekap_bidang->data_pka_periode_3($d)->result_array();

        for($i=0; $i<count($result);$i++){

                $ddd['kode_pka']=$result[$i]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                $j = 0;

                for($ii=0; $ii<count($rr);$ii++){

                    if (empty($rr[$ii]['penerimaan'])) {

                        $j +=0;

                    }else{

                        $j +=$rr[$ii]['penerimaan'];    

                    }

                }

                $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    if (empty($rrp[$iii]['pengeluaran'])) {

                        $g +=0;

                    }else{

                        $g +=$rrp[$iii]['pengeluaran'];

                    }

                }

                $g;

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) {

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                    }

                }

                $tot_pen_t1 += $pen_t1;

                $tot_pen_t2 += $pen_t2;

                $tot_pen_t3 += $pen_t3;

                $tot_pen_t4 += $pen_t4;

                $tot_peng_t1 += $peng_t1;

                $tot_peng_t2 += $peng_t2;

                $tot_peng_t3 += $peng_t3;

                $tot_peng_t4 += $peng_t4;

                $data[$i]["pen_t1"] = $pen_t1;

                $data[$i]["pen_t2"] = $pen_t2;

                $data[$i]["pen_t3"] = $pen_t3;

                $data[$i]["pen_t4"] = $pen_t4;

                $data[$i]["peng_t1"] = $peng_t1;

                $data[$i]["peng_t2"] = $peng_t2;

                $data[$i]["peng_t3"] = $peng_t3;

                $data[$i]["peng_t4"] = $peng_t4;

                $data[$i]["penerimaan"] = $j;

                $data[$i]["pengeluaran"] = $g;

                $tot_pen    += $j;

                $tot_peng   += $g;

                $data[$i]["kma"] = $result[$i]['kma'];

                $data[$i]["nama_um"] = $result[$i]['nama_um'];

                $data[$i]["nama_kegiatan"] = $result[$i]['nama_kegiatan'];

                $data[$i]["nama_jk"] = $result[$i]['nama_jk'];

                $data[$i]["tujuan_kegiatan"] = $result[$i]['tujuan_kegiatan'];

                $data[$i]["lokasi_pp"] = $result[$i]['lokasi_pp'];

                $data[$i]["pendukung_program"] = $result[$i]['pendukung_program'];

                $data[$i]["pen_jawab_program"] = $result[$i]['pen_jawab_program'];

                $data[$i]["w_awal_pp"] = $result[$i]['w_awal_pp'];

                $data[$i]["w_akhir_pp"] = $result[$i]['w_akhir_pp'];

                $data[$i]["indikator_kuantitatif"] = $result[$i]['indikator_kuantitatif'];

                $data[$i]["indikator_kualitatif"] = $result[$i]['indikator_kualitatif'];

                $data[$i]["keterangan"] = $result[$i]['keterangan'];

        }

        $data['result'] = $data;

        $data['nama_bidang'] = explode("_", $id_bidang)[1];

        $data["t_pen_t1"] = $tot_pen_t1;

        $data["t_pen_t2"] = $tot_pen_t2;

        $data["t_pen_t3"] = $tot_pen_t3;

        $data["t_pen_t4"] = $tot_pen_t4;

        $data["t_peng_t1"] = $tot_peng_t1;

        $data["t_peng_t2"] = $tot_peng_t2;

        $data["t_peng_t3"] = $tot_peng_t3;

        $data["t_peng_t4"] = $tot_peng_t4;

        $data["tot_pen"]   = $tot_pen;

        $data["tot_peng"]    = $tot_peng;

        $data['periode_thn'] = $d['periode_thn'];

        // $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_pertriwulan_2',$data);

        $mpdf = new \Mpdf\Mpdf();

        $dat = $this->load->view('laporan/rekap/bidang/pdf/v_laporan_rekap_bidang_pdf_pertriwulan_3', $data, TRUE);

        $mpdf->AddPage("L","","","","","5","5","15","5","","","","","","","","","","","","Legal");

        $mpdf->WriteHTML($dat);

        $mpdf->Output();

    }

    public function laporan_rekap_bidang_3($periode_thn,$id_bidang){

        $d['periode_thn'] = str_replace('-', '/', $periode_thn);

        $d['id_bidang'] = explode("_", $id_bidang)[0];

    

        $tot_pen_t1 = 0;

        $tot_pen_t2 = 0;

        $tot_pen_t3 = 0;

        $tot_pen_t4 = 0;

        $tot_peng_t1 = 0;

        $tot_peng_t2 = 0;

        $tot_peng_t3 = 0;

        $tot_peng_t4 = 0;

        $tot_pen = 0;

        $tot_peng = 0;

        $result = $this->M_laporan_rekap_bidang->data_pka_periode_3($d)->result_array();

        for($i=0; $i<count($result);$i++){

                $ddd['kode_pka']=$result[$i]['kode_pka'];

                ///penerimaan///

                $rr = $this->M_laporan_rekap_bidang->ambil_penerimaan($ddd)->result_array();

                $j = 0;

                for($ii=0; $ii<count($rr);$ii++){

                    if (empty($rr[$ii]['penerimaan'])) {

                        $j +=0;

                    }else{

                        $j +=$rr[$ii]['penerimaan'];    

                    }

                }

                $j;

                ///END penerimaan///

                ///pengeluaran///

                $rrp = $this->M_laporan_rekap_bidang->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

                for($iii=0; $iii<count($rrp);$iii++){

                    if (empty($rrp[$iii]['pengeluaran'])) {

                        $g +=0;

                    }else{

                        $g +=$rrp[$iii]['pengeluaran'];

                    }

                }

                $g;

                $ct = $this->M_laporan_rekap_bidang->ambil_triwulan($ddd)->row_array();

                $ju = explode(",", $ct['triwulan']);

                $pen_t1 = 0;

                $pen_t2 = 0;

                $pen_t3 = 0;

                $pen_t4 = 0;

                $peng_t1 = 0;

                $peng_t2 = 0;

                $peng_t3 = 0;

                $peng_t4 = 0;

                for ($tt=0; $tt < count($ju); $tt++) {

                    if (preg_replace("/[^0-9]/", "", $ju[$tt]) == 1) {

                        $pen_t1 += $j/count($ju);

                        $peng_t1 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 2) {

                        $pen_t2 += $j/count($ju);

                        $peng_t2 +=$g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 3) {

                        $pen_t3 += $j/count($ju);

                        $peng_t3 += $g/count($ju);

                    }elseif (preg_replace("/[^0-9]/", "", $ju[$tt]) == 4) {

                        $pen_t4 += $j/count($ju);

                        $peng_t4 += $g/count($ju);

                    }

                }

                $tot_pen_t1 += $pen_t1;

                $tot_pen_t2 += $pen_t2;

                $tot_pen_t3 += $pen_t3;

                $tot_pen_t4 += $pen_t4;

                $tot_peng_t1 += $peng_t1;

                $tot_peng_t2 += $peng_t2;

                $tot_peng_t3 += $peng_t3;

                $tot_peng_t4 += $peng_t4;

                $data[$i]["pen_t1"] = $pen_t1;

                $data[$i]["pen_t2"] = $pen_t2;

                $data[$i]["pen_t3"] = $pen_t3;

                $data[$i]["pen_t4"] = $pen_t4;

                $data[$i]["peng_t1"] = $peng_t1;

                $data[$i]["peng_t2"] = $peng_t2;

                $data[$i]["peng_t3"] = $peng_t3;

                $data[$i]["peng_t4"] = $peng_t4;

                $data[$i]["penerimaan"] = $j;

                $data[$i]["pengeluaran"] = $g;

                $tot_pen    += $j;

                $tot_peng   += $g;

                $data[$i]["kma"] = $result[$i]['kma'];

                $data[$i]["nama_um"] = $result[$i]['nama_um'];

                $data[$i]["nama_kegiatan"] = $result[$i]['nama_kegiatan'];

                $data[$i]["nama_jk"] = $result[$i]['nama_jk'];

                $data[$i]["tujuan_kegiatan"] = $result[$i]['tujuan_kegiatan'];

                $data[$i]["lokasi_pp"] = $result[$i]['lokasi_pp'];

                $data[$i]["pendukung_program"] = $result[$i]['pendukung_program'];

                $data[$i]["pen_jawab_program"] = $result[$i]['pen_jawab_program'];

                $data[$i]["w_awal_pp"] = $result[$i]['w_awal_pp'];

                $data[$i]["w_akhir_pp"] = $result[$i]['w_akhir_pp'];

                $data[$i]["indikator_kuantitatif"] = $result[$i]['indikator_kuantitatif'];

                $data[$i]["indikator_kualitatif"] = $result[$i]['indikator_kualitatif'];

                $data[$i]["keterangan"] = $result[$i]['keterangan'];

        }

        $data['result'] = $data;

        $data['nama_bidang'] = explode("_", $id_bidang)[1];

        $data["t_pen_t1"] = $tot_pen_t1;

        $data["t_pen_t2"] = $tot_pen_t2;

        $data["t_pen_t3"] = $tot_pen_t3;

        $data["t_pen_t4"] = $tot_pen_t4;

        $data["t_peng_t1"] = $tot_peng_t1;

        $data["t_peng_t2"] = $tot_peng_t2;

        $data["t_peng_t3"] = $tot_peng_t3;

        $data["t_peng_t4"] = $tot_peng_t4;

        $data["tot_pen"]   = $tot_pen;

        $data["tot_peng"]    = $tot_peng;

        $data['periode_thn'] = $d['periode_thn'];

        $this->load->view('laporan/rekap/bidang/excel/v_laporan_rekap_bidang_excel_3',$data);

    }

}