<?php

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

class Laporan_rekap_unit_misioner extends CI_Controller {

	public function __construct(){

        parent::__construct();		

        $this->load->model('M_laporan_rekap_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_um_all($periode_thn){

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

        ////////////////////////////////////////

        //All Unit Misioner//////////////////////////////////

        //////////////////////////////////////////////

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

        // echo json_encode($result);

        // var_dump($result);

        $total_penerimaan = 0;

        $total_pengeluaran = 0;

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

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

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

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

            $r = $this->M_laporan_rekap_unit_misioner->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_unit_misioner->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_unit_misioner->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;



            // echo $result[$i]['nama_um']."-----".$jj."<br>";



            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

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

        }

        



        ////////////////////////////////////////

        //END All Unit Misioner//////////////////////////////////

        //////////////////////////////////////////////



        ////////////////////////////////////////

        //Unit Misioner Without PPG//////////////////////////////////

        //////////////////////////////////////////////

        function removeElementWithValue($array, $key, $value){

             foreach($array as $subKey => $subArray){

                  if($subArray[$key] == $value){

                       unset($array[$subKey]);

                  }

             }

             return $array;

        }

        $array_no_ppg = removeElementWithValue($result, "unit_misioner", 18);

        $total_penerimaan_no_ppg = 0;

        $total_pengeluaran_no_ppg = 0;

        // var_dump($array_no_ppg);

        foreach($array_no_ppg as $subKey){

            // echo $subKey['nama_um']."====".$subKey['penerimaan']."<br>";

            $total_penerimaan_no_ppg += $subKey['penerimaan'];

            $total_pengeluaran_no_ppg += $subKey['pengeluaran'];

        }

        // echo "=========".$total_penerimaan_no_ppg."======================<br>";



        ////////////////////////////////////////

        //END Unit Misioner Without PPG//////////////////////////////////

        //////////////////////////////////////////////





        $data['result'] = $result;

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

        $data['total_penerimaan']   = $total_penerimaan;

        $data['total_pengeluaran']  = $total_pengeluaran;

        $data['saldo']              = $total_penerimaan - $total_pengeluaran;



        $data['total_penerimaan_no_ppg']   = $total_penerimaan_no_ppg;

        $data['total_pengeluaran_no_ppg']  = $total_pengeluaran_no_ppg;

        $data['saldo_no_ppg']              = $total_penerimaan_no_ppg - $total_pengeluaran_no_ppg;

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

    }

    public function laporan_rekap_um_all_pdf($periode_thn){

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

        ////////////////////////////////////////

        //All Unit Misioner//////////////////////////////////

        //////////////////////////////////////////////

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

        // echo json_encode($result);

        // var_dump($result);

        $total_penerimaan = 0;

        $total_pengeluaran = 0;

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

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

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

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

            $r = $this->M_laporan_rekap_unit_misioner->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_unit_misioner->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_unit_misioner->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;



            // echo $result[$i]['nama_um']."-----".$jj."<br>";



            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

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

        }

        



        ////////////////////////////////////////

        //END All Unit Misioner//////////////////////////////////

        //////////////////////////////////////////////



        ////////////////////////////////////////

        //Unit Misioner Without PPG//////////////////////////////////

        //////////////////////////////////////////////

        function removeElementWithValue($array, $key, $value){

             foreach($array as $subKey => $subArray){

                  if($subArray[$key] == $value){

                       unset($array[$subKey]);

                  }

             }

             return $array;

        }

        $array_no_ppg = removeElementWithValue($result, "unit_misioner", 18);

        $total_penerimaan_no_ppg = 0;

        $total_pengeluaran_no_ppg = 0;

        // var_dump($array_no_ppg);

        foreach($array_no_ppg as $subKey){

            // echo $subKey['nama_um']."====".$subKey['penerimaan']."<br>";

            $total_penerimaan_no_ppg += $subKey['penerimaan'];

            $total_pengeluaran_no_ppg += $subKey['pengeluaran'];

        }

        // echo "=========".$total_penerimaan_no_ppg."======================<br>";



        ////////////////////////////////////////

        //END Unit Misioner Without PPG//////////////////////////////////

        //////////////////////////////////////////////





        $data['result'] = $result;

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

        $data['total_penerimaan']   = $total_penerimaan;

        $data['total_pengeluaran']  = $total_pengeluaran;

        $data['saldo']              = $total_penerimaan - $total_pengeluaran;



        $data['total_penerimaan_no_ppg']   = $total_penerimaan_no_ppg;

        $data['total_pengeluaran_no_ppg']  = $total_pengeluaran_no_ppg;

        $data['saldo_no_ppg']              = $total_penerimaan_no_ppg - $total_pengeluaran_no_ppg;



        // var_dump($data);

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

        $mpdf = new \Mpdf\Mpdf();

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

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

        $mpdf->WriteHTML($dat);

        $mpdf->Output();

    }

    public function laporan_rekap_um_pertriwulan($periode_thn){

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

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

        // echo json_encode($result);

        // var_dump($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_um']."==".$result[$i]['kode_pka']."<br>";

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

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

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

            $jj = 0;

            $gg = 0;

            $um_pen_t1 = 0;

            $um_pen_t2 = 0;

            $um_pen_t3 = 0;

            $um_pen_t4 = 0;

            $um_peng_t1 = 0;

            $um_peng_t2 = 0;

            $um_peng_t3 = 0;

            $um_peng_t4 = 0;

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

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

                ///penerimaan///

                $rr = $this->M_laporan_rekap_unit_misioner->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_unit_misioner->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_unit_misioner->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]);

                    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);

                    }

                }

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

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

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

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

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

                // echo "<hr>";

                $um_pen_t1 += $pen_t1;

                $um_pen_t2 += $pen_t2;

                $um_pen_t3 += $pen_t3;

                $um_pen_t4 += $pen_t4;

                $um_peng_t1 += $peng_t1;

                $um_peng_t2 += $peng_t2;

                $um_peng_t3 += $peng_t3;

                $um_peng_t4 += $peng_t4;

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

            }

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

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

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

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

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

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

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

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

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

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

            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

            $t_pen_t1 += $um_pen_t1;

            $t_pen_t2 += $um_pen_t2;

            $t_pen_t3 += $um_pen_t3;

            $t_pen_t4 += $um_pen_t4;

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

            $t_peng_t1 += $um_peng_t1;

            $t_peng_t2 += $um_peng_t2;

            $t_peng_t3 += $um_peng_t3;

            $t_peng_t4 += $um_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/unit_misioner/excel/v_laporan_rekap_um_excel_pertriwulan',$data);

    }

    public function laporan_rekap_um_pertriwulan_pdf($periode_thn){

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

        $result = $this->M_laporan_rekap_unit_misioner->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['unit_misioner']=$result[$i]['unit_misioner'];

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

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

            $jj = 0;

            $gg = 0;

            $um_pen_t1 = 0;

            $um_pen_t2 = 0;

            $um_pen_t3 = 0;

            $um_pen_t4 = 0;

            $um_peng_t1 = 0;

            $um_peng_t2 = 0;

            $um_peng_t3 = 0;

            $um_peng_t4 = 0;

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

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

                ///penerimaan///

                $rr = $this->M_laporan_rekap_unit_misioner->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_unit_misioner->ambil_pengeluaran($ddd)->result_array();

                $g = 0;

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

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

                }

                $gg += $g;

                ///END penerimaan///

                $ct = $this->M_laporan_rekap_unit_misioner->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);

                    }

                }

                $um_pen_t1 += $pen_t1;

                $um_pen_t2 += $pen_t2;

                $um_pen_t3 += $pen_t3;

                $um_pen_t4 += $pen_t4;

                $um_peng_t1 += $peng_t1;

                $um_peng_t2 += $peng_t2;

                $um_peng_t3 += $peng_t3;

                $um_peng_t4 += $peng_t4;

            }

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

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

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

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

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

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

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

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

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

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

            $total_penerimaan +=$jj;

            $total_pengeluaran +=$gg;

            $t_pen_t1 += $um_pen_t1;

            $t_pen_t2 += $um_pen_t2;

            $t_pen_t3 += $um_pen_t3;

            $t_pen_t4 += $um_pen_t4;

            $t_peng_t1 += $um_peng_t1;

            $t_peng_t2 += $um_peng_t2;

            $t_peng_t3 += $um_peng_t3;

            $t_peng_t4 += $um_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/unit_misioner/excel/v_laporan_rekap_um_excel_pertriwulan_pdf',$data);

        $mpdf = new \Mpdf\Mpdf();

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

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

        $mpdf->WriteHTML($dat);

        $mpdf->Output();

    }

}