<?php


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





class Save_pka extends CI_Controller {


	


    public function __construct(){


        parent::__construct();		


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


    }


    private function kode_save(){


        $token = "";


        $codeAlphabet = "abcdefghijklmnopqrstuvwxyz";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


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


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


        } 


        return $token;


    }


    private function ubahDate($param){


        if($param == "00"){


            return "";


        }else{


            $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");


            // return $bulan[intval($param)-1];


            return $bulan[intval($param)-1];


        }


    }


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


        }


    }


	public function insert_save_pka($name){


        $token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


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


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


        } 











        $d["kode_save"] = $this->kode_save();


        $d["nama_save"] = str_replace("%20"," ",$name);


        $r = $this->M_save_pka->insert_save($d);





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








        // $a = "";


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


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





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


            $data["kode_save"] = $d["kode_save"];


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








            if($w_awal_pp[$i] == ""){


                $data["w_awal_pp"] = "";


            }else{


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


            }


            if ($w_akhir_pp[$i] =="") {


                $data["w_akhir_pp"] ="";


            }else{


                $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_save_pka->insert_save_pka($data);








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


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





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


                


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


                $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"] = $nilai_satuan[$p];


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


                $resp = $this->M_save_pka->insert_save_pka_pengeluaran($dat);





            }


            //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_save_pka->insert_save_pka_penerimaan($dat);





                }


            }


        }








        


        echo json_encode($r);


    }





	public function select_data_save(){


        $result = $this->M_save_pka->select_data_save()->result_array();


        echo json_encode($result);


    }


    public function select_data_save_pka($kode_save){


        $d['kode_save'] = $kode_save;


        $result = $this->M_save_pka->select_data_save_pka($d)->result_array();


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


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


            $pengeluaran = $this->M_save_pka->select_data_save_pka_pengeluaran($data)->result_array();


            $penerimaan = $this->M_save_pka->select_data_save_pka_penerimaan($data)->result_array();


            // $a=0;


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


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


            // }


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


            $result[$i]['save_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->ubahDate(explode("-", $result[$i]['w_awal_pp'])[1])." ".explode("-", $result[$i]['w_awal_pp'])[0] ;


            }


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


            }


            


        }


        echo json_encode($result);


    }





    public function update_save_pka($ks,$nama){


        $token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


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


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


        } 


        $dd["kode_save"] = $ks;


        $rr = $this->M_save_pka->delete_save($dd);


        if ($rr) {


            $d["kode_save"] = $this->kode_save();


            $d["nama_save"] = $nama;


            $r = $this->M_save_pka->insert_save($d);


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








            // $a = "";


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


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





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


                $data["kode_save"] = $d["kode_save"];


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








                if($w_awal_pp[$i] == ""){


                    $data["w_awal_pp"] = "";


                }else{


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


                }


                if ($w_akhir_pp[$i] =="") {


                    $data["w_akhir_pp"] ="";


                }else{


                    $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_save_pka->insert_save_pka($data);








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


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


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


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


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


                    $resp = $this->M_save_pka->insert_save_pka_pengeluaran($dat);





                }


                //penerimaan


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


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


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


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


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


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


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


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


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





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


                        


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


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


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


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


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


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


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


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


                        $resp = $this->M_save_pka->insert_save_pka_penerimaan($dat);





                    }


                }


            }


        }





        








        


        echo json_encode($r);


    }


    public function delete_save_pka($ks){


        $dd["kode_save"] = $ks;


        $rr = $this->M_save_pka->delete_save($dd);


        echo json_encode($rr);


    }


}


