<?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_sampah_save(){

        $result = $this->M_save_pka->select_data_sampah_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,$new_kode_save){

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

        

        

        

        

        $dd["kode_save"] = $ks;

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

            $this->M_save_pka->delete_save_pka($dd);

        if ($rr) {

            $d["kode_save"] = $new_kode_save;

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

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

            

            // $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_penerimaan1 = $this->input->post('jpn_'.$no_page[$i],TRUE);

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

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

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

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

                    $penerimaan1 = $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"];

                        

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

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

                        $dat1["jenis_penerimaan"] = $jenis_penerimaan1[$p];

                        $dat1["jumlah_satuan"] = $jumlah_satuan1[$p];

                        $dat1["bulanan"] = $bulanan1[$p];

                        $dat1["tahunan"] = $tahunan1[$p];

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

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

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

                    }

                }

            }

        }

        

        echo json_encode($rr);

    }

    public function delete_save_pka($ks){

        $dd["kode_save"] = $ks;

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

        echo json_encode($rr);

    }

    public function restore_save_pka(){

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

        $rr = $this->M_save_pka->restore_save_pka($data);

        echo json_encode($rr);

    }

    public function ambil_save_pka_tanpa_sendiri(){

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

        $result = $this->M_save_pka->ambil_save_pka_tanpa_sendiri($data)->result_array();

        echo json_encode($result);

    }

    public function join_data_save_pka(){

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

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

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



        $token = "";

        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        $codeAlphabet.= "0123456789";

        $max = strlen($codeAlphabet) - 1;

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

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

        } 

        $data['new_ks'] = $token;



        $insert_new_save = $this->M_save_pka->insert_new_save($data);



        $ambil_ks_self = $this->M_save_pka->ambil_pka_self_untuk_dicopy($data)->result_array();

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

            $token1 = "";

            $codeAlphabet1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            $codeAlphabet1.= "0123456789";

            $max1 = strlen($codeAlphabet1) - 1;

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

                $token1 .= $codeAlphabet1[mt_rand(0, $max1)];

            } 

            $data['new_kode_pka'] = $token1;

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

            $cp_ks_self = $this->M_save_pka->cp_ks_self($data);



            $ambil_save_pka_penerimaan = $this->M_save_pka->ambil_save_pka_penerimaan($data)->result_array();

            for ($a=0; $a < count($ambil_save_pka_penerimaan); $a++) { 

                $data['id_save_pka_penerimaan'] = $ambil_save_pka_penerimaan[$a]['id_save_pka_penerimaan'];

                $copy_save_pka_penerimaan = $this->M_save_pka->copy_save_pka_penerimaan($data);

            }

            $ambil_save_pka_pengeluaran = $this->M_save_pka->ambil_save_pka_pengeluaran($data)->result_array();

            for ($a=0; $a < count($ambil_save_pka_pengeluaran); $a++) { 

                $data['id_save_pka_pengeluaran'] = $ambil_save_pka_pengeluaran[$a]['id_save_pka_pengeluaran'];

                $copy_save_pka_pengeluaran = $this->M_save_pka->copy_save_pka_pengeluaran($data);

            }

        }

        $a = array();

        $apsucupb = $this->M_save_pka->ambil_pka_self_untuk_cari_urutan_paling_besar($data)->result_array();

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

            array_push($a,intval(explode ("-",$apsucupb[$i]["kma"])[3]));

        }

        $max_self = max($a)+1;

        $apoudu = $this->M_save_pka->ambil_pka_other_untuk_dicopy($data)->result_array();

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

                $token1 = "";

                $codeAlphabet1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

                $codeAlphabet1.= "0123456789";

                $max1 = strlen($codeAlphabet1) - 1;

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

                    $token1 .= $codeAlphabet1[mt_rand(0, $max1)];

                } 

                $data['new_kode_pka'] = $token1;

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



                if (strlen($max_self) == 1) {

                    $ms = "00".$max_self++;

                }elseif(strlen($max_self) == 2){

                    $ms = "0".$max_self++;

                }else{

                    $ms = $max_self++;

                }

                $data["new_kma"] = explode ("-",$apoudu[$i]["kma"])[0]."-".explode ("-",$apoudu[$i]["kma"])[1]."-".explode ("-",$apoudu[$i]["kma"])[2]."-".$ms."-".explode ("-",$apoudu[$i]["kma"])[4];

                $data["kode_pka"] = $apoudu[$i]["kode_pka"];

                $this->M_save_pka->cp_ks_other($data);

                $ambil_save_pka_penerimaan = $this->M_save_pka->ambil_save_pka_penerimaan($data)->result_array();

                for ($a=0; $a < count($ambil_save_pka_penerimaan); $a++) { 

                    $data['id_save_pka_penerimaan'] = $ambil_save_pka_penerimaan[$a]['id_save_pka_penerimaan'];

                    $copy_save_pka_penerimaan = $this->M_save_pka->copy_save_pka_penerimaan($data);

                }

                $ambil_save_pka_pengeluaran = $this->M_save_pka->ambil_save_pka_pengeluaran($data)->result_array();

                for ($a=0; $a < count($ambil_save_pka_pengeluaran); $a++) { 

                    $data['id_save_pka_pengeluaran'] = $ambil_save_pka_pengeluaran[$a]['id_save_pka_pengeluaran'];

                    $copy_save_pka_pengeluaran = $this->M_save_pka->copy_save_pka_pengeluaran($data);

                }





        }

        echo json_encode($insert_new_save);







        // $a = array();

        // $apsucupb = $this->M_save_pka->ambil_pka_self_untuk_cari_urutan_paling_besar($data)->result_array();

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

        //     array_push($a,intval(explode ("-",$apsucupb[$i]["kma"])[3]));

        // }

        // $max_self = max($a)+1;

        // // echo json_encode(strlen($max_self));

        // // $hapus_save_join = $this->M_save_pka->hapus_save_join($data);

        // // if ($hapus_save_join) {

        //     $apoudu = $this->M_save_pka->ambil_pka_other_untuk_diupdate($data)->result_array();

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

        //         if (strlen($max_self) == 1) {

        //             $ms = "00".$max_self++;

        //         }elseif(strlen($max_self) == 2){

        //             $ms = "0".$max_self++;

        //         }else{

        //             $ms = $max_self++;

        //         }

        //         $data["new_kma"] = explode ("-",$apoudu[$i]["kma"])[0]."-".explode ("-",$apoudu[$i]["kma"])[1]."-".explode ("-",$apoudu[$i]["kma"])[2]."-".$ms."-".explode ("-",$apoudu[$i]["kma"])[4];

        //         $data["kode_pka"] = $apoudu[$i]["kode_pka"];

        //         $this->M_save_pka->update_kma_join($data);

        //     } 

        //     $update_name_join = $this->M_save_pka->update_name_join($data);

        //     echo json_encode($update_name_join);

        // // }

        

    }

    

    public function arsip_jadi_file_pka(){

        $token = "";

        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz";

        $codeAlphabet .= "0123456789";

        $max = strlen($codeAlphabet) - 1;

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

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

        }

        

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

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

        // $kode_arsip = $token;

        $kode_save = $token;

        

        $result = $this->M_save_pka->add_save($kode_save,$nama_save);

        if ($result) {

                $rr = $this->M_save_pka->save_ambil_kode_pka_dari_arsip_pka($no_pka)->result_array();

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

                    $new_kode_pka = "";

                    $codeAlphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

                    $codeAlphabet2 .= "0123456789";

                    $max2 = strlen($codeAlphabet2) - 1;

                    for ($u=0; $u < 8; $u++) {

                        $new_kode_pka .= $codeAlphabet2[mt_rand(0, $max2)];

                    }

                    $this->M_save_pka->pindah_ke_save_pka($rr[$i]['kode_pka'],$kode_save,$new_kode_pka);

                    $this->M_save_pka->pindah_ke_save_pka_pengeluaran($rr[$i]['kode_pka'],$kode_save,$new_kode_pka);

                    $this->M_save_pka->pindah_ke_save_pka_penerimaan($rr[$i]['kode_pka'],$kode_save,$new_kode_pka);

                    // echo json_encode($rr[$i]['kode_pka']."<br>");

                }

            echo json_encode($result);

        }else{

            echo json_encode($result);

        }

    }

    

}

