<?php


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





class Pka extends CI_Controller {


	


    public function __construct(){


        parent::__construct();		


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


        $this->load->library('mailer');


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


    }


    private function ubahDate($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];


    }


    private function ubahTime($dateTime){


        return explode(" ", $dateTime)[1];


    }


    private function date_to_date($date){


        $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];


        // return $date;





    }


    private function switch_bulan($param){


        switch ($param) {


          case "Januari":


            return "01";


            break;


          case "Februari":


            return "02";


            break;


          case "Maret":


            return "03";


            break;


          case "April":


            return "04";


            break;


          case "Mei":


            return "05";


            break;


          case "Juni":


            return "06";


            break;


          case "Juli":


            return "07";


            break;


          case "Agustus":


            return "08";


            break;


            case "September":


            return "09";


            break;


          case "Oktober":


            return "10";


            break;


          case "November":


            return "11";


            break;


          default:


            return "12";


        }


    }


    private function send_mail($penerima,$pesan,$subjek){





        $email_penerima = $penerima;


        


        $logo = base_url('assets/img/logo.png');


        $template_email = $this->M_template_email->select_data_template_email()->row_array();


        // $attachment = $_FILES['attachment'];


        $content = $this->load->view('template_email/content_mail', array('pesan'=>$pesan,'logo'=>$logo,'template_email'=>$template_email), true); // Ambil isi file content.php dan masukan ke variabel $content


        $sendmail = array(


          'email_penerima'=>$email_penerima,


          'subjek'=>$subjek,


          'content'=>$content


          // 'attachment'=>$attachment


        );





        $send = $this->mailer->send($sendmail); // Panggil fungsi send yang ada di librari Mailer





        // echo "<b>".$send['status']."</b><br />";


        // echo $send['message'];


        // echo "<br /><a href='".base_url("index.php/email")."'>Kembali ke Form</a>";


        return $send;


      }


    public function data_pka(){


        $result = $this->M_pka->data_pka()->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);


        }


        echo json_encode($result);


    }


    public function data_refisi_pka(){


        $result = $this->M_pka->data_refisi_pka()->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);


        }


        echo json_encode($result);


    }


	public function insert_pka(){


        $token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


        for ($i=0; $i < 5; $i++) {


            $token .= $codeAlphabet[mt_rand(0, $max)];


        } 


        $no_pka = "";


        $nom = "0123456789";


        $m = strlen($nom) - 1;


        for ($i=0; $i < 5; $i++) {


            $no_pka .= $nom[mt_rand(0, $m)];


        } 








        $no_page                = $this->input->post('no_page',TRUE);


        $kma                    = $this->input->post('kma',TRUE);


        $unit_misioner          = $this->input->post('unit_misioner',TRUE);


        $jenis_kegiatan         = $this->input->post('jenis_kegiatan',TRUE);


        $bidang                 = $this->input->post('bidang',TRUE);


        $nama_kegiatan          = $this->input->post('nama_kegiatan',TRUE);


        $nomor_strategi         = $this->input->post('nomor_strategi',TRUE);


        $tujuan_kegiatan        = $this->input->post('tujuan_kegiatan',TRUE);


        $lokasi_pp              = $this->input->post('lokasi_pp',TRUE);


        // $pendukung_program      = $this->input->post('pendukung_program',TRUE);


        // $pen_jawab_program      = $this->input->post('pen_jawab_program',TRUE);


        $w_awal_pp              = $this->input->post('w_awal_pp',TRUE);


        $w_akhir_pp             = $this->input->post('w_akhir_pp',TRUE);


        $indikator_kuantitatif  = $this->input->post('indikator_kuantitatif',TRUE);


        $indikator_kualitatif   = $this->input->post('indikator_kualitatif',TRUE);


        $prioritas_kegiatan     = $this->input->post('prioritas_kegiatan',TRUE);


        // $triwulan               = $this->input->post('triwulan',TRUE);


        $keterangan             = $this->input->post('keterangan',TRUE);


        $pt                     = $this->input->post('pt',TRUE);








        // $a = "";


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


             // $a.= "<br>jp_".$no_page[$i];


            $data["periode_thn"] = $pt;


            $data["no_pka"] = $no_pka;


            $data["kode_pka"] = $token.$no_page[$i];


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


            $data["unit_misioner"] = explode(",", $unit_misioner[$i])[0];


            $data["jenis_kegiatan"] = explode(",", $jenis_kegiatan[$i])[0];


            $data["bidang"] = explode(",", $bidang[$i])[0];


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


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


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


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


            // $data["pendukung_program"] = $pendukung_program[$i];


            // $data["pen_jawab_program"] = $pen_jawab_program[$i];





            $data["pendukung_program"] = json_encode($this->input->post('pendukung_program_'.$no_page[$i],TRUE));


            $data["pen_jawab_program"] = json_encode($this->input->post('pen_jawab_program_'.$no_page[$i],TRUE));





            $data["w_awal_pp"] = explode(" ", $w_awal_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_awal_pp[$i])[1])."-".explode(" ", $w_awal_pp[$i])[0];


            $data["w_akhir_pp"] = explode(" ", $w_akhir_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_akhir_pp[$i])[1])."-".explode(" ", $w_akhir_pp[$i])[0];


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


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


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


            $data["triwulan"] = json_encode($this->input->post('triwulan_'.$no_page[$i],TRUE));


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


            





            $respon = $this->M_pka->insert_pka($data);





            //pengeluaran


            $jenis_pengeluaran = $this->input->post('jp_'.$no_page[$i],TRUE);


            $jumlah_satuan = $this->input->post('js_'.$no_page[$i],TRUE);


            $bulanan = $this->input->post('bl_'.$no_page[$i],TRUE);


            $tahunan = $this->input->post('th_'.$no_page[$i],TRUE);


            $nilai_satuan = $this->input->post('ns_'.$no_page[$i],TRUE);


            $pengeluaran = $this->input->post('pn_'.$no_page[$i],TRUE);


            $s= $this->input->post('jp_'.$no_page[$i],TRUE);


            // echo json_encode($pengeluaran);


            for ($p=0; $p < count($s); $p++) { 





                // $a.="==".$data["kode_pka"];





                $dat["kode_pka"] = $data["kode_pka"];


                $dat["jenis_pengeluaran"] = $jenis_pengeluaran[$p];


                $dat["jumlah_satuan"] = $jumlah_satuan[$p];


                $dat["bulanan"] = $bulanan[$p];


                $dat["tahunan"] = $tahunan[$p];


                $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan[$p]);


                $dat["pengeluaran"] = str_replace(".", "", $pengeluaran[$p]);


                $respon = $this->M_pka->insert_pka_pengeluaran($dat);


                // echo json_encode($dat["nilai_satuan"]);





            }





            //penerimaan


            if (!empty($this->input->post('jpn_'.$no_page[$i],TRUE))) {


                


                $jenis_penerimaan = $this->input->post('jpn_'.$no_page[$i],TRUE);


                $jumlah_satuan_pen = $this->input->post('jspn_'.$no_page[$i],TRUE);


                $bulanan_pen = $this->input->post('blpn_'.$no_page[$i],TRUE);


                $tahunan_pen = $this->input->post('thpn_'.$no_page[$i],TRUE);


                $nilai_satuan_pen = $this->input->post('nspn_'.$no_page[$i],TRUE);


                $penerimaan = $this->input->post('pnpn_'.$no_page[$i],TRUE);


                $jpn= $this->input->post('jpn_'.$no_page[$i],TRUE);


                for ($p=0; $p < count($jpn); $p++) { 





                    // $a.="==".$data["kode_pka"];





                    $dat["kode_pka"] = $data["kode_pka"];


                    $dat["jenis_penerimaan"] = $jenis_penerimaan[$p];


                    $dat["jumlah_satuan"] = $jumlah_satuan_pen[$p];


                    $dat["bulanan"] = $bulanan_pen[$p];


                    $dat["tahunan"] = $tahunan_pen[$p];


                    $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan_pen[$p]);


                    $dat["penerimaan"] = str_replace(".", "", $penerimaan[$p]);


                    $respon = $this->M_pka->insert_pka_penerimaan($dat);





                }


            }


            


        }








        


        echo json_encode(true);


    }


    public function select_data_refisi_pka($no_pka){


        $d['no_pka'] = $no_pka;


        $result = $this->M_pka->data_detail_pka($d)->result_array();


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


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


            $pengeluaran = $this->M_pka->select_data_pka_pengeluaran($data)->result_array();


            $penerimaan = $this->M_pka->select_data_pka_penerimaan($data)->result_array();


            // $a=0;


            // for($o=0; $o<count($pengeluaran);$o++){


            //     $a+=$pengeluaran[$o]['pengeluaran'];


            // }


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


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


            if($result[$i]['w_awal_pp'] == "00-00-000"){


                $result[$i]['w_awal_pp_case'] ="";


            }else{


                // $result[$i]['w_awal_pp_case']= explode("-", $result[$i]['w_awal_pp'])[2]." ".$this->date_to_date(explode("-", $result[$i]['w_awal_pp'])[1])." ".explode("-", $result[$i]['w_awal_pp'])[0] ;


                $result[$i]['w_awal_pp_case']= $this->date_to_date($result[$i]['w_awal_pp']);


            }


            if($result[$i]['w_akhir_pp'] == "00-00-000"){


                $result[$i]['w_akhir_pp_case']="";


            }else{


                // $result[$i]['w_akhir_pp_case']=explode("-", $result[$i]['w_akhir_pp'])[2]." ".$this->ubahDate(explode("-", $result[$i]['w_akhir_pp'])[1])." ".explode("-", $result[$i]['w_akhir_pp'])[0] ;


                 $result[$i]['w_akhir_pp_case']= $this->date_to_date($result[$i]['w_akhir_pp']);


            }


            if($result[$i]['evaluasi']=="1"){


                $r = $this->M_pka->ambil_detail_pka_evaluasi($data['kode_pka'])->row_array();


                $result[$i]['tot_ev_peng'] = $r['realisasi_pengeluaran'];


                $result[$i]['tot_ev_pen'] = $r['realisasi_penerimaan'];


            }else{


                $result[$i]['tot_ev_peng'] = "0";


                $result[$i]['tot_ev_pen'] = "0";


            }


        }


        echo json_encode($result);


    }


    public function insert_refisi_pka(){


        $token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


        for ($i=0; $i < 5; $i++) {


            $token .= $codeAlphabet[mt_rand(0, $max)];


        } 





        $no_page                = $this->input->post('no_page',TRUE);


        $kma                    = $this->input->post('kma',TRUE);


        $unit_misioner          = $this->input->post('unit_misioner',TRUE);


        $jenis_kegiatan         = $this->input->post('jenis_kegiatan',TRUE);


        $bidang                 = $this->input->post('bidang',TRUE);


        $nama_kegiatan          = $this->input->post('nama_kegiatan',TRUE);


        $nomor_strategi         = $this->input->post('nomor_strategi',TRUE);


        $tujuan_kegiatan        = $this->input->post('tujuan_kegiatan',TRUE);


        $lokasi_pp              = $this->input->post('lokasi_pp',TRUE);


        $pendukung_program      = $this->input->post('pendukung_program',TRUE);


        $pen_jawab_program      = $this->input->post('pen_jawab_program',TRUE);


        $w_awal_pp              = $this->input->post('w_awal_pp',TRUE);


        $w_akhir_pp             = $this->input->post('w_akhir_pp',TRUE);


        $indikator_kuantitatif  = $this->input->post('indikator_kuantitatif',TRUE);


        $indikator_kualitatif   = $this->input->post('indikator_kualitatif',TRUE);


        $prioritas_kegiatan     = $this->input->post('prioritas_kegiatan',TRUE);


        // $triwulan               = $this->input->post('triwulan',TRUE);


        $keterangan             = $this->input->post('keterangan',TRUE);


        $pt                     = $this->input->post('periode_thn',TRUE);





        $kode_pka               = $this->input->post('kode_pka',TRUE); 


        $no_pka                 = $this->input->post('no_pka',TRUE); 








        $d["no_pka"] = $no_pka;


        $resu = $this->M_pka->select_hapus_pka_admin($d)->result_array();


         for ($p=0; $p < count($resu); $p++) { 


            $this->M_pka->delete_refisi_pka_pengeluaran($resu[$p]["kode_pka"]);


            $this->M_pka->delete_refisi_pka_penerimaan($resu[$p]["kode_pka"]);


         }


         if ($resu != "") {


             $del = $this->M_pka->delete_refisi_pka($no_pka);


         }











        


        if($del){





        





        // $a = "";


        $u = 1;


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


             // $a.= "<br>jp_".$no_page[$i];


            $u++;


            $data["periode_thn"] = $pt;


            $data["kode_pka"] = $token.$u;


            $data["no_pka"] = $no_pka;


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


            $data["unit_misioner"] = explode(",", $unit_misioner[$i])[0];


            $data["jenis_kegiatan"] = explode(",", $jenis_kegiatan[$i])[0];


            $data["bidang"] = explode(",", $bidang[$i])[0];


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


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


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


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


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


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


            $data["w_awal_pp"] = explode(" ", $w_awal_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_awal_pp[$i])[1])."-".explode(" ", $w_awal_pp[$i])[0];


            $data["w_akhir_pp"] = explode(" ", $w_akhir_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_akhir_pp[$i])[1])."-".explode(" ", $w_akhir_pp[$i])[0];


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


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


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


            $data["triwulan"] = json_encode($this->input->post('triwulan_'.$no_page[$i],TRUE));


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


            





            $respon = $this->M_pka->insert_pka($data);





            //pengeluaran


            $jenis_pengeluaran = $this->input->post('jp_'.$no_page[$i],TRUE);


            $jumlah_satuan = $this->input->post('js_'.$no_page[$i],TRUE);


            $bulanan = $this->input->post('bl_'.$no_page[$i],TRUE);


            $tahunan = $this->input->post('th_'.$no_page[$i],TRUE);


            $nilai_satuan = $this->input->post('ns_'.$no_page[$i],TRUE);


            $pengeluaran = $this->input->post('pn_'.$no_page[$i],TRUE);


            $s= $this->input->post('jp_'.$no_page[$i],TRUE);


            // echo json_encode($pengeluaran);


            


            for ($p=0; $p < count($s); $p++) { 





                // $a.="==".$data["kode_pka"];





                $dat["kode_pka"] = $data["kode_pka"];


                $dat["jenis_pengeluaran"] = $jenis_pengeluaran[$p];


                $dat["jumlah_satuan"] = $jumlah_satuan[$p];


                $dat["bulanan"] = $bulanan[$p];


                $dat["tahunan"] = $tahunan[$p];


                $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan[$p]);


                $dat["pengeluaran"] = str_replace(".", "", $pengeluaran[$p]);


                $respon = $this->M_pka->insert_pka_pengeluaran($dat);


                // echo json_encode($dat["nilai_satuan"]);





            }





            //penerimaan


            if (!empty($this->input->post('jpn_'.$no_page[$i],TRUE))) {


                


                $jenis_penerimaan = $this->input->post('jpn_'.$no_page[$i],TRUE);


                $jumlah_satuan_pen = $this->input->post('jspn_'.$no_page[$i],TRUE);


                $bulanan_pen = $this->input->post('blpn_'.$no_page[$i],TRUE);


                $tahunan_pen = $this->input->post('thpn_'.$no_page[$i],TRUE);


                $nilai_satuan_pen = $this->input->post('nspn_'.$no_page[$i],TRUE);


                $penerimaan = $this->input->post('pnpn_'.$no_page[$i],TRUE);


                $jpn= $this->input->post('jpn_'.$no_page[$i],TRUE);


                for ($p=0; $p < count($jpn); $p++) { 





                    // $a.="==".$data["kode_pka"];





                    $dat["kode_pka"] = $data["kode_pka"];


                    $dat["jenis_penerimaan"] = $jenis_penerimaan[$p];


                    $dat["jumlah_satuan"] = $jumlah_satuan_pen[$p];


                    $dat["bulanan"] = $bulanan_pen[$p];


                    $dat["tahunan"] = $tahunan_pen[$p];


                    $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan_pen[$p]);


                    $dat["penerimaan"] = str_replace(".", "", $penerimaan[$p]);


                    $respon = $this->M_pka->insert_pka_penerimaan($dat);





                }


            }


            


        }





        } 


        


        echo json_encode(true);


    }


    public function data_evaluasi_pka(){


        $result = $this->M_pka->data_evaluasi_pka()->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);


        }


        echo json_encode($result);


    }


     public function ambil_data_pka_evaluasi($no_pka){


        $data["no_pka"] = $no_pka;


        $result = $this->M_pka->ambil_data_pka_evaluasi($data)->result_array();


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


            $result[$i]['jml_pengeluaran'] = 0;


            $rp = $this->M_pka->ambil_data_pka_evaluasi_pengeluaran($result[$i]['kode_pka'])->result_array();


            for($u=0; $u<count($rp);$u++){


                $result[$i]['jml_pengeluaran'] += $rp[$u]['pengeluaran'];


            }


            $result[$i]['jml_penerimaan'] = 0;


            $rpn = $this->M_pka->ambil_data_pka_evaluasi_penerimaan($result[$i]['kode_pka'])->result_array();


            // if(rp)


            for($u=0; $u<count($rpn);$u++){


                $result[$i]['jml_penerimaan'] += $rpn[$u]['penerimaan'];


            }


        }


        echo json_encode($result);


    }


    


    public function insert_evaluasi_pka(){


        $data["no_pka"] = $this->input->post('no_pka',TRUE);


        $data["jml_page"] = $this->input->post('jml_page',TRUE);


        $kode_pka             = $this->input->post('kode_pka',TRUE);


        $pencapaian             = $this->input->post('pencapaian',TRUE);


        $atp                    = $this->input->post('atp',TRUE);


        $target_kehadiran       = $this->input->post('target_kehadiran',TRUE);


        $realisasi_kehadiran    = $this->input->post('realisasi_kehadiran',TRUE);


        $realisasi_penerimaan   = $this->input->post('realisasi_penerimaan',TRUE);


        $realisasi_pengeluaran  = $this->input->post('realisasi_pengeluaran',TRUE);


        $epsk                   = $this->input->post('epsk',TRUE);


        


        for ($i=0; $i < $data['jml_page']; $i++) { 


            $data['kode_pka']  = $kode_pka[$i];


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


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


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


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


            $data["realisasi_penerimaan"] = str_replace(".","",$realisasi_penerimaan[$i]);


            $data["realisasi_pengeluaran"] = str_replace(".","",$realisasi_pengeluaran[$i]);


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


            $this->M_pka->insert_evaluasi_pka($data);


        }


         $res = $this->M_pka->update_evaluasi_pka($data);


        if ($res) {


            echo json_encode($res);


        }else{


            echo json_encode($res);


        }


        


    }


    function update_evaluasi_pka($data){


        $sql = "UPDATE `tb_pka` SET `evaluasi`= 1 WHERE no_pka = '$data[no_pka]'";


        return $this->db->query($sql);


    }


    public function ambil_detail_pka_evaluasi($no_pka){


        $data["no_pka"] = $no_pka;


        $result = $this->M_pka->ambil_data_pka_evaluasi($data)->result_array();





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





            $row = $this->M_pka->ambil_detail_pka_evaluasi($result[$i]['kode_pka'])->row_array();


            $result[$i]['pencapaian'] = $row['pencapaian'];


            $result[$i]['atp'] = $row['atp'];


            $result[$i]['target_kehadiran'] = $row['target_kehadiran'];


            $result[$i]['realisasi_kehadiran'] = $row['realisasi_kehadiran'];


            $result[$i]['realisasi_penerimaan'] = $row['realisasi_penerimaan'];


            $result[$i]['realisasi_pengeluaran'] = $row['realisasi_pengeluaran'];


            $result[$i]['epsk'] = $row['epsk'];





            $result[$i]['jml_pengeluaran'] = 0;


            $rp = $this->M_pka->ambil_data_pka_evaluasi_pengeluaran($result[$i]['kode_pka'])->result_array();


            for($u=0; $u<count($rp);$u++){


                $result[$i]['jml_pengeluaran'] += $rp[$u]['pengeluaran'];


            }


            $result[$i]['jml_penerimaan'] = 0;


            $rpn = $this->M_pka->ambil_data_pka_evaluasi_penerimaan($result[$i]['kode_pka'])->result_array();


            // if(rp)


            for($u=0; $u<count($rpn);$u++){


                $result[$i]['jml_penerimaan'] += $rpn[$u]['penerimaan'];


            }


        }


        echo json_encode($result);


    }


    public function data_rev_ev_pka(){


        $result = $this->M_pka->data_rev_ev_pka()->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);





            $r = $this->M_pka->ambil_detail_pka( $result[$i]['kode_pka'])->row_array();


            $result[$i]['nm_um'] = $r['nm_um'];


            $result[$i]['email'] = $r['email'];


        }


        echo json_encode($result);


    }


     public function update_revisi_evaluasi_pka(){


        $data["no_pka"] = $this->input->post('no_pka',TRUE);


        $data["jml_page"] = $this->input->post('jml_page',TRUE);


        $kode_pka             = $this->input->post('kode_pka',TRUE);


        $pencapaian             = $this->input->post('pencapaian',TRUE);


        $atp                    = $this->input->post('atp',TRUE);


        $target_kehadiran       = $this->input->post('target_kehadiran',TRUE);


        $realisasi_kehadiran    = $this->input->post('realisasi_kehadiran',TRUE);


        $realisasi_penerimaan   = $this->input->post('realisasi_penerimaan',TRUE);


        $realisasi_pengeluaran  = $this->input->post('realisasi_pengeluaran',TRUE);


        $epsk                   = $this->input->post('epsk',TRUE);





        $del = $this->M_pka->delete_evaluasi_pka($data["no_pka"]);


        if($del){


            for ($i=0; $i < $data['jml_page']; $i++) { 


                $data['kode_pka']  = $kode_pka[$i];


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


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


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


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


                $data["realisasi_penerimaan"] = str_replace(".","",$realisasi_penerimaan[$i]);


                $data["realisasi_pengeluaran"] = str_replace(".","",$realisasi_pengeluaran[$i]);


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


                $this->M_pka->update_revisi_evaluasi_pka($data);


            }


            echo json_encode($del);


        }else{


            echo json_encode($del);


        }


        


    }


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


    ////////////////////////////Admin/////////////////////////////////////////////////////////////////////////////


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


    public function select_periode_thn(){


        $result = $this->M_pka->select_periode_thn()->result_array();


        echo json_encode($result);


    }


    public function data_pka_admin($periode_thn){


        $result = $this->M_pka->data_pka_admin($periode_thn)->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);


        }


        echo json_encode($result);


    }


    public function data_sesi_pka(){


        $result = $this->M_pka->data_sesi_pka()->row_array();


            $result['dari_tgl'] = $this->ubahDate($result['dari']);


            $result['sampai_tgl'] = $this->ubahDate($result['sampai']);


            $result['tgl_update'] = $this->ubahDate(explode(" ", $result['updated_at'])[0])." ".explode(" ", $result['updated_at'])[1];


        


        echo json_encode($result);


    }


    public function update_sesi_pka(){


        $dari = $this->input->post('dari',TRUE);


        $sampai = $this->input->post('sampai',TRUE);





        $data["kode_sesi"] = $this->input->post('kode_sesi',TRUE);


        $data["dari"] = explode(" ", $dari)[2]."-".$this->switch_bulan(explode(" ", $dari)[1])."-".explode(" ", $dari)[0];


        $data["sampai"] = explode(" ", $sampai)[2]."-".$this->switch_bulan(explode(" ", $sampai)[1])."-".explode(" ", $sampai)[0];


        $data["keterangan"] = $this->input->post('keterangan',TRUE);


        $respon = $this->M_pka->update_sesi_pka($data);


        echo json_encode($respon);


    }


    public function publish_sesi_pka(){


        $data["kode_sesi"] = $this->input->post('kode_sesi',TRUE);


        $respon = $this->M_pka->publish_sesi_pka($data);


        echo json_encode($respon);


    }


    public function cek_sesi_pka(){


        $result = $this->M_pka->cek_sesi_pka()->row_array();


        


        echo json_encode($result);


    }


    public function data_detail_pka($no_pka){


        $d['no_pka'] = $no_pka;


        $result = $this->M_pka->data_detail_pka($d)->result_array();


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


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


            $pengeluaran = $this->M_pka->select_data_pka_pengeluaran($data)->result_array();


            $penerimaan = $this->M_pka->select_data_pka_penerimaan($data)->result_array();


            // $a=0;


            // for($o=0; $o<count($pengeluaran);$o++){


            //     $a+=$pengeluaran[$o]['pengeluaran'];


            // }


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


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


            if($result[$i]['w_awal_pp'] == "00-00-000"){


                $result[$i]['w_awal_pp_case'] ="";


            }else{


                // $result[$i]['w_awal_pp_case']= explode("-", $result[$i]['w_awal_pp'])[2]." ".$this->date_to_date(explode("-", $result[$i]['w_awal_pp'])[1])." ".explode("-", $result[$i]['w_awal_pp'])[0] ;


                $result[$i]['w_awal_pp_case']= $this->date_to_date($result[$i]['w_awal_pp']);


            }


            if($result[$i]['w_akhir_pp'] == "00-00-000"){


                $result[$i]['w_akhir_pp_case']="";


            }else{


                // $result[$i]['w_akhir_pp_case']=explode("-", $result[$i]['w_akhir_pp'])[2]." ".$this->ubahDate(explode("-", $result[$i]['w_akhir_pp'])[1])." ".explode("-", $result[$i]['w_akhir_pp'])[0] ;


                 $result[$i]['w_akhir_pp_case']= $this->date_to_date($result[$i]['w_akhir_pp']);


            }


            


        }


        echo json_encode($result);


    }


    public function acc_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        $result = $this->M_pka->acc_pka_admin($data);


        if ($result) {


            $ambil_email= $this->M_pka->ambil_email($data)->row_array();


            $template_email = $this->M_template_email->select_data_template_email()->row_array();


            $subjek = $template_email['sjk_user_acc_pka'];


            $pesan = str_replace("%login%",'<a href="'.base_url('login.aspx').'">Login</a>',$template_email['msg_user_acc_pka']);


            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);


            if($send_mail['status'] == "Sukses"){


                echo json_encode(true);


            }else{


                echo json_encode(false);


            }


        }else{


            echo json_encode($result);


        }


        


    }


    public function refisi_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        $data['message'] = $this->input->post('message',TRUE);


        $result = $this->M_pka->refisi_pka_admin($data);


        if ($result) {


            $ambil_email= $this->M_pka->ambil_email($data)->row_array();


            $template_email = $this->M_template_email->select_data_template_email()->row_array();


            $subjek = $template_email['sjk_user_rev_pka'];





            $login = '<a href="'.base_url('login.aspx').'">Login</a>';


            $str = array('%login%','%rev_message%');


            $rplc =array($login,$data['message']);


            $pesan = str_replace($str,$rplc,$template_email['msg_user_rev_pka']);


            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);


            if($send_mail['status'] == "Sukses"){


                echo json_encode(true);


            }else{


                echo json_encode(false);


            }


        }


        


    }


    public function hapus_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        


        


         $res = $this->M_pka->select_hapus_pka_admin($data)->result_array();


         for ($p=0; $p < count($res); $p++) { 


            $this->M_pka->hapus_pka_pengeluaran_admin($res[$p]["kode_pka"]);


            $this->M_pka->hapus_pka_penerimaan_admin($res[$p]["kode_pka"]);


         }


         if ($res != "") {


             $result = $this->M_pka->hapus_pka_admin($data);


         }





        


        echo json_encode($result);


    }


    public function insert_edit_pka_admin(){


        $token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


        for ($i=0; $i < 5; $i++) {


            $token .= $codeAlphabet[mt_rand(0, $max)];


        } 





        $no_page                = $this->input->post('no_page',TRUE);


        $kma                    = $this->input->post('kma',TRUE);


        $unit_misioner          = $this->input->post('unit_misioner',TRUE);


        $jenis_kegiatan         = $this->input->post('jenis_kegiatan',TRUE);


        $bidang                 = $this->input->post('bidang',TRUE);


        $nama_kegiatan          = $this->input->post('nama_kegiatan',TRUE);


        $nomor_strategi         = $this->input->post('nomor_strategi',TRUE);


        $tujuan_kegiatan        = $this->input->post('tujuan_kegiatan',TRUE);


        $lokasi_pp              = $this->input->post('lokasi_pp',TRUE);


        // $pendukung_program      = $this->input->post('pendukung_program',TRUE);


        // $pen_jawab_program      = $this->input->post('pen_jawab_program',TRUE);


        $w_awal_pp              = $this->input->post('w_awal_pp',TRUE);


        $w_akhir_pp             = $this->input->post('w_akhir_pp',TRUE);


        $indikator_kuantitatif  = $this->input->post('indikator_kuantitatif',TRUE);


        $indikator_kualitatif   = $this->input->post('indikator_kualitatif',TRUE);


        $prioritas_kegiatan     = $this->input->post('prioritas_kegiatan',TRUE);


        // $triwulan               = $this->input->post('triwulan',TRUE);


        $keterangan             = $this->input->post('keterangan',TRUE);


        // $pt                     = $this->input->post('periode_thn',TRUE);





        $kode_pka               = $this->input->post('kode_pka',TRUE); 


        $no_pka                 = $this->input->post('no_pka',TRUE); 











        $ambil = $this->M_pka->ambil_detail_pka_edit($no_pka)->row_array();





        $data['created_at'] = $ambil['created_at'];


        $data['created_by'] = $ambil['created_by'];


        // $data['kode_pka'] = $ambil['kode_pka'];


        $data['no_pka'] = $no_pka;


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


        $data['acc'] = $ambil['acc'];


        $data['evaluasi'] = $ambil['evaluasi'];














        $d["no_pka"] = $no_pka;


        $resu = $this->M_pka->select_hapus_pka_admin($d)->result_array();


         for ($p=0; $p < count($resu); $p++) { 


            $this->M_pka->delete_refisi_pka_pengeluaran($resu[$p]["kode_pka"]);


            $this->M_pka->delete_refisi_pka_penerimaan($resu[$p]["kode_pka"]);


         }


         if ($resu != "") {


             $del = $this->M_pka->delete_refisi_pka($no_pka);


         }











        


        if($del){





        





        // $a = "";


        $u = 1;


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


             // $a.= "<br>jp_".$no_page[$i];


            $u++;


            // $data["periode_thn"] = $pt;


            $data["kode_pka"] = $token.$u;


            // $data["no_pka"] = $no_pka;


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


            $data["unit_misioner"] = explode(",", $unit_misioner[$i])[0];


            $data["jenis_kegiatan"] = explode(",", $jenis_kegiatan[$i])[0];


            $data["bidang"] = explode(",", $bidang[$i])[0];


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


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


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


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


            // $data["pendukung_program"] = $pendukung_program[$i];


            // $data["pen_jawab_program"] = $pen_jawab_program[$i];


            $data["pendukung_program"] = json_encode($this->input->post('pendukung_program_'.$no_page[$i],TRUE));


            $data["pen_jawab_program"] = json_encode($this->input->post('pen_jawab_program_'.$no_page[$i],TRUE));





            $data["w_awal_pp"] = explode(" ", $w_awal_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_awal_pp[$i])[1])."-".explode(" ", $w_awal_pp[$i])[0];


            $data["w_akhir_pp"] = explode(" ", $w_akhir_pp[$i])[2]."-".$this->switch_bulan(explode(" ", $w_akhir_pp[$i])[1])."-".explode(" ", $w_akhir_pp[$i])[0];


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


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


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


            // $data["triwulan"] = $triwulan[$i];


            $data["triwulan"] = json_encode($this->input->post('triwulan_'.$no_page[$i],TRUE));


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
































            





            $respon = $this->M_pka->insert_edit_pka($data);





            //pengeluaran


            $jenis_pengeluaran = $this->input->post('jp_'.$no_page[$i],TRUE);


            $jumlah_satuan = $this->input->post('js_'.$no_page[$i],TRUE);


            $bulanan = $this->input->post('bl_'.$no_page[$i],TRUE);


            $tahunan = $this->input->post('th_'.$no_page[$i],TRUE);


            $nilai_satuan = $this->input->post('ns_'.$no_page[$i],TRUE);


            $pengeluaran = $this->input->post('pn_'.$no_page[$i],TRUE);


            $s= $this->input->post('jp_'.$no_page[$i],TRUE);


            // echo json_encode($pengeluaran);


            


            for ($p=0; $p < count($s); $p++) { 





                // $a.="==".$data["kode_pka"];





                $dat["kode_pka"] = $data["kode_pka"];


                $dat["jenis_pengeluaran"] = $jenis_pengeluaran[$p];


                $dat["jumlah_satuan"] = $jumlah_satuan[$p];


                $dat["bulanan"] = $bulanan[$p];


                $dat["tahunan"] = $tahunan[$p];


                $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan[$p]);


                $dat["pengeluaran"] = str_replace(".", "", $pengeluaran[$p]);


                $respon = $this->M_pka->insert_pka_pengeluaran($dat);


                // echo json_encode($dat["nilai_satuan"]);





            }





            //penerimaan


            if (!empty($this->input->post('jpn_'.$no_page[$i],TRUE))) {


                


                $jenis_penerimaan = $this->input->post('jpn_'.$no_page[$i],TRUE);


                $jumlah_satuan_pen = $this->input->post('jspn_'.$no_page[$i],TRUE);


                $bulanan_pen = $this->input->post('blpn_'.$no_page[$i],TRUE);


                $tahunan_pen = $this->input->post('thpn_'.$no_page[$i],TRUE);


                $nilai_satuan_pen = $this->input->post('nspn_'.$no_page[$i],TRUE);


                $penerimaan = $this->input->post('pnpn_'.$no_page[$i],TRUE);


                $jpn= $this->input->post('jpn_'.$no_page[$i],TRUE);


                for ($p=0; $p < count($jpn); $p++) { 





                    // $a.="==".$data["kode_pka"];





                    $dat["kode_pka"] = $data["kode_pka"];


                    $dat["jenis_penerimaan"] = $jenis_penerimaan[$p];


                    $dat["jumlah_satuan"] = $jumlah_satuan_pen[$p];


                    $dat["bulanan"] = $bulanan_pen[$p];


                    $dat["tahunan"] = $tahunan_pen[$p];


                    $dat["nilai_satuan"] = str_replace(".", "", $nilai_satuan_pen[$p]);


                    $dat["penerimaan"] = str_replace(".", "", $penerimaan[$p]);


                    $respon = $this->M_pka->insert_pka_penerimaan($dat);





                }


            }


            


        }





        } 


        


        echo json_encode(true);


    }


    public function kirim_evaluasi_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        $respon = $this->M_pka->kirim_evaluasi_pka_admin($data);


        if ($respon) {


            $ambil_email= $this->M_pka->ambil_email($data)->row_array();


            $template_email = $this->M_template_email->select_data_template_email()->row_array();


            $subjek = $template_email['sjk_adm_send_ev_pka'];


            $pesan = str_replace("%login%",'<a href="'.base_url('login.aspx').'">Login</a>',$template_email['msg_adm_send_ev_pka']);


            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);


            if($send_mail['status'] == "Sukses"){


                echo json_encode(true);


            }else{


                echo json_encode(false);


            }


        }else{


            echo json_encode(false);


        }


        


    }


    public function data_evaluasi_pka_admin($periode_thn){


        $result = $this->M_pka->data_evaluasi_pka_admin($periode_thn)->result_array();


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


            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);


            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);





            $r = $this->M_pka->ambil_detail_pka_evaluasi( $result[$i]['kode_pka'])->row_array();


            $result[$i]['acc_ev'] = $r['acc_ev'];


        }


        echo json_encode($result);


    }


    public function data_sesi_ev_pka(){


        $result = $this->M_pka->data_sesi_ev_pka()->row_array();


            $result['dari_tgl'] = $this->date_to_date($result['dari']);


            $result['sampai_tgl'] = $this->date_to_date($result['sampai']);


            $result['tgl_update'] = $this->ubahDate(explode(" ", $result['updated_at'])[0])." ".explode(" ", $result['updated_at'])[1];


        


        echo json_encode($result);


    }


    public function update_sesi_ev_pka(){


        $dari = $this->input->post('dari',TRUE);


        $sampai = $this->input->post('sampai',TRUE);





        $data["kode_sesi"] = $this->input->post('kode_sesi',TRUE);


        $data['periode_thn'] = $this->input->post('periode_thn',TRUE);


        $data["dari"] = explode(" ", $dari)[2]."-".$this->switch_bulan(explode(" ", $dari)[1])."-".explode(" ", $dari)[0];


        $data["sampai"] = explode(" ", $sampai)[2]."-".$this->switch_bulan(explode(" ", $sampai)[1])."-".explode(" ", $sampai)[0];


        $data["keterangan"] = $this->input->post('keterangan',TRUE);


        $respon = $this->M_pka->update_sesi_ev_pka($data);


        echo json_encode($respon);


    }


    public function publish_sesi_ev_pka(){


        $data["kode_sesi"] = $this->input->post('kode_sesi',TRUE);


        $respon = $this->M_pka->publish_sesi_ev_pka($data);


        echo json_encode($respon);


    }


    public function refisi_ev_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        $data['message'] = $this->input->post('message',TRUE);


        $result = $this->M_pka->refisi_ev_pka_admin($data);


        if ($result){


            $ambil_email= $this->M_pka->ambil_email($data)->row_array();


            $template_email = $this->M_template_email->select_data_template_email()->row_array();


            $subjek = $template_email['sjk_user_rev_ev_pka'];


            $login = '<a href="'.base_url('login.aspx').'">Login</a>';


            $str = array('%login%','%rev_message%');


            $rplc =array($login,$data['message']);


            $pesan = str_replace($str,$rplc,$template_email['msg_user_rev_ev_pka']);


            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);


            if($send_mail['status'] == "Sukses"){


                echo json_encode(true);


            }else{


                echo json_encode(false);


            }


        }


        


    }


    public function acc_ev_pka_admin(){


        $data['no_pka'] = $this->input->post('no_pka',TRUE);


        $result = $this->M_pka->acc_ev_pka_admin($data);


        if ($result){


            $ambil_email= $this->M_pka->ambil_email($data)->row_array();


            $template_email = $this->M_template_email->select_data_template_email()->row_array();


            $subjek = $template_email['sjk_user_acc_ev_pka'];


            $pesan = str_replace("%login%",'<a href="'.base_url('login.aspx').'">Login</a>',$template_email['msg_user_acc_ev_pka']);


            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);


            if($send_mail['status'] == "Sukses"){


                echo json_encode(true);


            }else{


                echo json_encode(false);


            }


        }


        


    }


    public function update_data_evaluasi_pka(){


        $data["no_pka"] = $this->input->post('no_pka',TRUE);


        $data["jml_page"] = $this->input->post('jml_page',TRUE);


        $kode_pka             = $this->input->post('kode_pka',TRUE);


        $pencapaian             = $this->input->post('pencapaian',TRUE);


        $atp                    = $this->input->post('atp',TRUE);


        $target_kehadiran       = $this->input->post('target_kehadiran',TRUE);


        $realisasi_kehadiran    = $this->input->post('realisasi_kehadiran',TRUE);


        $realisasi_penerimaan   = $this->input->post('realisasi_penerimaan',TRUE);


        $realisasi_pengeluaran  = $this->input->post('realisasi_pengeluaran',TRUE);


        $epsk                   = $this->input->post('epsk',TRUE);





        $del = $this->M_pka->delete_evaluasi_pka($data["no_pka"]);


        if($del){


            for ($i=0; $i < $data['jml_page']; $i++) { 


                $data['kode_pka']  = $kode_pka[$i];


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


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


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


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


                $data["realisasi_penerimaan"] = str_replace(".","",$realisasi_penerimaan[$i]);


                $data["realisasi_pengeluaran"] = str_replace(".","",$realisasi_pengeluaran[$i]);


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


                $this->M_pka->update_data_evaluasi_pka($data);


            }


            echo json_encode($del);


        }else{


            echo json_encode($del);


        }


        


    }


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


    ////////////////////////////End Admin/////////////////////////////////////////////////////////////////////////////


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


	


}


