Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
59 kB
2
Indexable
Never
    public function getSkp($pegawai_id, $force) {
        if ($pegawai_id == 'self') {
            $pegawai_id = Auth::user()->id;
            $list_pmm_error = KinerjaGuru::getListPmmErrorSyncByPegawaiId($pegawai_id);
            if($list_pmm_error) {
                $list_pmm_error_id = $list_pmm_error->id;
            } else {
                $list_pmm_error_id = null;
            }
            if (!$pegawai = Pegawai::getDetilPegawai($pegawai_id)) {
                return $this->errorJsonMessage("Data Pegawai tidak ditemukan");
            }
            // if (env("APP_ENV") == 'production' && $pegawai->last_sync_pmm) {
            //     if (date("Y-m-d H:i", strtotime("-10 minutes")) < $pegawai->last_sync_pmm && $force == 0) {
            //         return $this->errorJsonMessage("Lakukan Sinkro di 10 Menit kedepan");
            //     }
            // }
        } else {
            $list_pmm_error = KinerjaGuru::getListPmmErrorSyncByPegawaiId($pegawai_id);
            if($list_pmm_error) {
                $list_pmm_error_id = $list_pmm_error->id;
            } else {
                $list_pmm_error_id = null;
            }
            if (!$pegawai = Pegawai::getDetilPegawai($pegawai_id)) {
                return $this->errorJsonMessage("Data Pegawai tidak ditemukan");
            }
        }

        $data = $this->pmmSinkroService->getDataSkp($pegawai_id , $pegawai->nip);

        $data_log = [
            'jenis' => "skp",
            'url' => "skp/". $pegawai_id . '?nip=' . $pegawai->nip,
            'payload' => $data,
            'success' => false,
            'success_sinkro' => true,
            'data' => null,
            'error_log' => null,
            'catatan' => null,
        ];

        if (!Arrays::keyExists($data, "data")) {
            $data_log['error_log'][0] = $data['error']['message'];
            $this->logService->log_sinkro_pmm($data_log);
            return $this->errorJsonMessage("Gagal melakukan sinkro PMM dikarenakan " . $data['error']['message']);
        }

        $data_log['payload'] = $data['data'];
        $data_log['success'] = true;


        $model_skp = null;

        $i = 0;
        $data_return[] = [];
        foreach ($data['data'] as $item) {
            if ($item['skp']['isPlt'] == false) {
                try {
                    try {
                            $periode_awal = $item['skp']['periodeAwal'];
                            $periode_akhir = $item['skp']['periodeAkhir'];

                            DB::beginTransaction();
                            $error_log = [];
                            $catatan = [];
                            $return_force = 1;
                            $error_info = 0;
                            $tipe_skp = null;

                            if(!$periode_akhir) {
                                $error_info = 1;
                                throw new AppException('Pegawai belum membuat SKP di PMM sampai selesai');
                            }

                            if ($item['pegawai']['tipeSkp'] == 'kepala sekolah'){
                                $tipe_skp = 'ks';
                                $jenis_jabatan = 1;
                                DB::table('unor')
                                    ->where('id', $pegawai->unor_id)
                                    ->update(['unor_mandiri' => '1', 'updated_by' => "API"]);
                            } else {
                                $tipe_skp = $item['pegawai']['tipeSkp'];
                                $jenis_jabatan = 2;
                            }

                            DB::table('pegawai')
                                ->where('id', $pegawai_id)
                                ->update([
                                    'jenis_jabatan' => $jenis_jabatan,
                                    'updated_at'    => date('Y-m-d H:i:s'),
                                    'updated_by'    => "API"]);

                            if ($force == 2) {
                                $old_skp = Skp::cekApakahAdaSkpBeririsan($pegawai_id, $periode_awal, $periode_akhir);
                                $skp['model'] = Skp::getJenisSkp($pegawai_id);
                                $skp['id'] = $old_skp->id;
                                $skp['skp_organisasi_id'] = $old_skp->skp_organisasi_id;
                                $skp['atasan_id'] = $old_skp->atasan_id;
                                $skp['pendekatan'] = $item['skp']['pendekatan'];
                                $skp['pegawai_jenis_jabatan'] = $old_skp->pegawai_jenis_jabatan;
                                $skp['skp_id'] = $old_skp->id;
                                $skp['pegawai_nip'] = $pegawai->nip;
                                $skp['status'] = $old_skp->status;
                                $skp['pegawai_unor_id'] = $old_skp->pegawai_unor_id;

                                $skp_rhk = (object) $skp;
                                $rhk_lama =  $this->rhkService->getRhk($skp_rhk, null);
                                $rhk_lama_id = [];
                                $rhk_indikator_lama_id = [];
                                foreach ($rhk_lama['rhk'] as $data_rhk) {
                                    if ($data_rhk['bagian'] == 'individu') {
                                        array_push($rhk_lama_id, $data_rhk['rhk_id']);
                                        foreach($data_rhk['indikators'] as $data_rhk_indikator) {
                                            array_push($rhk_indikator_lama_id, $data_rhk_indikator['id']);
                                        }
                                    }
                                }

                                $data_hapus = [
                                    'deleted_at' => date('Y-m-d H:i:s'),
                                    'deleted_by' => "SINKRO PMM"
                                ];

                                if ($rhk_lama_id) {
                                    DB::table("skp_rhk")
                                        ->whereIn('id', $rhk_lama_id)
                                        ->update($data_hapus);
                                }

                                if($rhk_indikator_lama_id) {
                                    DB::table("skp_indikator")
                                        ->whereIn('id', $rhk_indikator_lama_id)
                                        ->update($data_hapus);
                                }

                                $data_log['data'][$i]['skp_hapus'] = $old_skp->id;

                                DB::table("skp")
                                    ->where(["id" => $old_skp->id,"pegawai_id" => $pegawai_id])
                                    ->update([
                                        'deleted_at'    => date('Y-m-d H:i:s')]);
                            }


                            if ($old_skp = Skp::cekApakahAdaSkpBeririsan($pegawai_id, $periode_awal, $periode_akhir)) {
                                $new_skp = false;
                                $success_skp = true;
                                $update_skp = [];

                                if ($old_skp->pegawai_unor_id != $pegawai->unor_id) {
                                    $return_force = 2;
                                    array_push($catatan, "SKP yang ada memiliki Unor yang berbeda dengan Pegawai saat ini.");
                                }

                                if ($old_skp->skp_organisasi_id == null) {
                                    throw new AppException('Atasan belum membuat Periode SKP');
                                }

                                $skp['model'] = Skp::getJenisSkp($pegawai_id);
                                $skp['id'] = $old_skp->id;
                                $skp['skp_organisasi_id'] = $old_skp->skp_organisasi_id;
                                $skp['atasan_id'] = $old_skp->atasan_id;
                                $skp['pendekatan'] = $item['skp']['pendekatan'];
                                $skp['pegawai_jenis_jabatan'] = $old_skp->pegawai_jenis_jabatan;
                                $skp['skp_id'] = $old_skp->id;
                                $skp['pegawai_nip'] = $pegawai->nip;
                                $skp['status'] = $old_skp->status;
                                $skp['pegawai_unor_id'] = $old_skp->pegawai_unor_id;

                                if($old_skp->pendekatan != $item['skp']['pendekatan']) {
                                    $update_skp['pendekatan'] = $item['skp']['pendekatan'];
                                }

                                if($old_skp->model != $skp['model']) {
                                    $update_skp['model'] = $skp['model'];
                                }

                                if ($old_skp->periode_awal != $periode_awal || $old_skp->periode_akhir != $periode_akhir) {
                                    $update_skp['periode_awal'] = $periode_awal;
                                    $update_skp['periode_akhir'] = $periode_akhir;
                                }

                                if ($update_skp) {
                                    DB::table('skp')
                                        ->where(['id' => $skp['id']])
                                        ->update($update_skp);
                                }

                            } else {

                                $new_skp = true;
                                $date_periode_awal = strtotime($periode_awal);
                                $date_periode_akhir = strtotime($periode_akhir);

                                // pengecekan tanggal periode akhir harus setelah periode awal
                                if($date_periode_akhir <= $date_periode_awal) {
                                    throw new AppException('Tanggal Periode Akhir harus setelah Periode Awal');
                                }

                                // pengecekan periode akhir dan periode awal harus dalam tahun yang sama
                                if(date('Y', $date_periode_awal) != date('Y', $date_periode_akhir)) {
                                    throw new AppException('Periode Awal dan Periode Akhir harus dalam tahun yang sama');
                                }

                                // pengecekan data unor apa ada
                                if (!Unor::isUnorExists($pegawai->unor_id, $pegawai->ref_instansi_id)) {
                                    $error_info = 3;
                                    throw new AppException('Data Unit Kerja Pegawai tidak terdaftar di Referensi Unit Kerja');
                                }

                                // siapkan data SKP

                                $skp = [
                                    "id"                => Skp::generateId(),
                                    "pegawai_unor_id"   => $pegawai->unor_id,
                                    "periode_awal"      => $periode_awal,
                                    "periode_akhir"     => $periode_akhir,
                                    "pendekatan"        => $item['skp']['pendekatan'],
                                    "pegawai_id"        => $pegawai_id,
                                    "status"            => "draft"
                                ];

                                DB::table('skp')->insert($skp);

                                $info_skp = Skp::detilSkpRingkas($skp['id']);

                                /// $result = $this->reloadDataPegawai($info_skp);

                                // cek apakah pada SKP tersebut merupakan SKP PLT/PLH
                                if($plt_plh = PltPlh::getPltPlhBySkpId($info_skp->skp_id)) {
                                    // ambil data pegawai berdasarkan data PLT/PLH
                                    $data_pegawai = (array) Pegawai::getDataPegawaiPltPlh($plt_plh->id);
                                    $jenis_skp = Skp::getJenisSkpPltPlh($plt_plh->id);
                                } else {
                                    // ambil data pegawai berdasarkan tabel pegawai
                                    $data_pegawai = (array) Skp::getDataPegawai($info_skp->pegawai_id);
                                    $jenis_skp = Skp::getJenisSkp($info_skp->pegawai_id);
                                }

                                if(!$jenis_skp) {
                                    throw new AppException("Gagal mendapatkan Jenis SKP Pegawai");
                                }

                                $skp_organisasi = null;

                                if ($info_skp->status != 'persetujuan' || $info_skp->pegawai_unor_id == null || $info_skp->pegawai_unor_id == '') {
                                    $data_reload = [
                                        "pegawai_id"            => $data_pegawai["id"],
                                        "pegawai_unor_id"       => $data_pegawai["unor_id"],
                                        "pegawai_jabatan"       => $data_pegawai["jabatan"],
                                        "pegawai_golru"         => $data_pegawai["golru"],
                                        "pegawai_unit_kerja"    => $data_pegawai["unor_nama"],
                                        "pegawai_jenis_jabatan" => $data_pegawai["jenis_jabatan"],
                                        "model"                 => $jenis_skp,
                                        "updated_at"            => date("Y-m-d H:i:s"),
                                        "pegawai_instansi_kerja_id" => $data_pegawai["instansi_id_real"]
                                    ];

                                    // update table pegawai
                                    DB::table('skp')
                                        ->where(['id' => $info_skp->skp_id])
                                        ->update($data_reload);

                                    // SKP Organisasi dan SKP Organisasi Bawahan
                                    if($data_pegawai['jenis_jabatan'] == '1') {
                                        // cek SKP Organisasi unor, jika belum dibuat maka buat baru
                                        // berguna ketika SKP Organisasi di unor lama sudah dibuat dan terjadi perubahan unor
                                        // sehingga perlu insert row SKP Organisasi untuk unor yang baru
                                        $tahun = date('Y', strtotime($info_skp->periode_awal));
                                        if(!$skp_organisasi = Skp::getSkpOrganisasiByUnorAndTahun($data_pegawai['unor_id'], $tahun)) {
                                            $skp_organisasi = (object) Skp::generateSkpOrganisasi($data_pegawai['unor_id'], $tahun);
                                        }
                                    } else {
                                        // cek apakah SKP organisasi sudah dibuat
                                        $date_periode_awal = strtotime($info_skp->periode_awal);
                                        if(!$skp_organisasi = Skp::getSkpOrganisasiByUnorAndTahun($data_pegawai['unor_id'], date('Y', $date_periode_awal))) {
                                            $error_info = 4;
                                            throw new AppException('Data SKP Organisasi belum dibuat oleh atasan');
                                        }

                                        // buat SKP Organisasi Bawahan jika belum terdapat record SKP Organisasi Bawahan
                                        // dan yang buat SKP bukan pimpinan unor
                                        DB::table('skp_organisasi_bawahan')
                                            ->where(['skp_organisasi_id' => $skp_organisasi->id, 'skp_bawahan_id' => $info_skp->skp_id, 'dari_matriks' => '0'])
                                            ->update(['deleted_at' => NULL]);

                                        if(!Skp::cekSkpOrganisasiBawahanBySkpId($skp_organisasi->id, $info_skp->skp_id)) {
                                            DB::table('skp_organisasi_bawahan')
                                                ->insert([
                                                    'skp_organisasi_id' => $skp_organisasi->id,
                                                    'skp_bawahan_id' => $info_skp->skp_id
                                                ]);
                                        }
                                    }

                                    // if($reset_skp_organisasi_bawahan) {
                                    //     $this->resetSkpOrganisasiBawahan($skp_organisasi->id, $info_skp->skp_id);
                                    // }
                                } else {
                                    // jika persetujuan, maka cuman update golongan ruang dan model
                                    $data_reload = [
                                        'pegawai_golru' => $data_pegawai['golru'],
                                        'pegawai_jabatan' => $data_pegawai['jabatan'],
                                        'updated_at'    => date('Y-m-d H:i:s')
                                    ];

                                    // update table pegawai
                                    DB::table('skp')
                                        ->where(['id' => $info_skp->skp_id])
                                        ->update($data_reload);
                                }

                                if (isset($skp_organisasi->id)) {
                                    $data_reload['skp_organisasi_id'] = $skp_organisasi->id;
                                } else {
                                    $data_reload['skp_organisasi_id'] = null;
                                }

                                ///

                                $info_skp->pegawai_unit_kerja = $data_reload['pegawai_unit_kerja'];
                                $info_skp->pegawai_jabatan = $data_reload['pegawai_jabatan'];
                                $info_skp->pegawai_jenis_jabatan = $data_reload['pegawai_jenis_jabatan'];

                                ///// $atasan = $this->reloadDataAtasan($info_skp);

                                ////// $data_atasan = $this->getDataAtasanTerbaru($info_skp);

                                $data_atasan = null;
                                $unor_atasan_id = null;

                                if ($info_skp->pegawai_jenis_jabatan == '1' && $info_skp->unor_jenis == '00') {
                                    // jika jenis unor = 00 maka sudah tidak ambil atasan lagi karena sudah eselon tertinggi
                                    $data_atasan = null;
                                } else {
                                    // cek jika SKP merupakan SKP PLT / PLH
                                    if ($plt_plh = PltPlh::getPltPlhBySkpId($info_skp->skp_id)) {
                                        $unor_atasan_id = $plt_plh->unor_atasan_id;
                                    } else {
                                        if ($info_skp->pegawai_jenis_jabatan == '1') {
                                            // jika merupakan pimpinan unor, ambil ID unor atasan unor tsb
                                            $unor_atasan_id = Unor::detilUnor($info_skp->pegawai_unor_id)->unor_atasan_id;
                                        } else {
                                            // jika merupakan pegawai, ambil ID unor pada SKP sebagai unor atasan
                                            $unor_atasan_id = $info_skp->pegawai_unor_id;
                                        }
                                    }

                                    ////// $data_atasan = $this->geztPimpinanUnor($unor_atasan_id);
                                    if ($plt_plh = Pegawai::getPltPlhByUnorId($unor_atasan_id)) {
                                        if ($data_atasan = (array) Pegawai::getDataPegawaiPltPlh($plt_plh->id)) {
                                        }
                                    }

                                    if (!$data_atasan) {
                                        // gunakan data atasan definitif
                                        $data_atasan = (array) Pegawai::getDataAtasanByUnorId($unor_atasan_id);
                                        // jika data atasan masih kosong, atau atasan merupakan diri sendiri (karena sebagai PLT/PLH),
                                        // cari 1 tingkat di atasannya lagi
                                        $unor_atasan = Unor::detilUnor($unor_atasan_id);
                                        if (($data_atasan == null || $data_atasan['id'] == null || ($unor_atasan->jenis != '00' && $data_atasan['id'] == Auth::user()->id))
                                            && ($info_skp->unor_jenis != '00' && $info_skp->unor_jenis != '10' && $info_skp->unor_jenis != '11')) {
                                            // mengganti unor atasan ke unor diatasnya lagi
                                            if (!$unor_atasan || !$unor_atasan->unor_atasan_id) {
                                                $error_info = 5;
                                                throw new AppException('Unit Kerja atasan tidak ditemukan.');
                                            }

                                            $unor_atasan_id = $unor_atasan->unor_atasan_id;
                                            $data_atasan = $this->getPimpinanUnor($unor_atasan_id);
                                        }

                                        if ($data_atasan == null || $data_atasan['id'] == null || $data_atasan['id'] == Auth::user()->id) {
                                            // jika masih kosong, cari ke atasannya lagi
                                            if ($info_skp->unor_jenis != '00' && $info_skp->unor_jenis != '10' && $info_skp->unor_jenis != '11') {
                                                $unor_atasan = Unor::detilUnor($unor_atasan->unor_atasan_id);

                                                if ($unor_atasan && $unor_atasan->jenis != '00') {
                                                    $unor_atasan = Unor::detilUnor($unor_atasan->unor_atasan_id);
                                                    $data_atasan = $this->getPimpinanUnor($unor_atasan->id);
                                                }
                                            }
                                        }
                                    }
                                }

                                if ( ! $data_atasan) {
                                    $data_atasan = [
                                        "id" => null,
                                        "unor_id" => null,
                                        "jabatan" => null,
                                        "golru" => null,
                                        "unor_nama" => null,
                                        "jenis_jabatan" => null
                                    ];
                                }
                                //////

                                // siapkan data atasan
                                $data_reload_atasan = [
                                    "pegawai_atasan_id"             => $data_atasan["id"],
                                    "pegawai_atasan_unor_id"        => $data_atasan["unor_id"],
                                    "pegawai_atasan_jabatan"        => $data_atasan["jabatan"],
                                    "pegawai_atasan_golru"          => $data_atasan["golru"],
                                    "pegawai_atasan_unit_kerja"     => $data_atasan["unor_nama"],
                                    "pegawai_atasan_jenis_jabatan"  => $data_atasan["jenis_jabatan"],
                                    "updated_at"                    => date('Y-m-d H:i:s')
                                ];

                                // update table skp
                                DB::table('skp')
                                    ->where(['id' => $info_skp->skp_id])
                                    ->update($data_reload_atasan);
                                ////

                                // insert data awal untuk tabel skp_perilaku
                                if(!Skp::generateRefPerilaku($skp['id'])) {
                                    DB::rollback();
                                    throw new AppException('Gagal membuat data Referensi Perilaku');
                                }

                                $skp['skp_organisasi_id'] = $data_reload['skp_organisasi_id'];
                                $skp['model'] = $data_reload['model'];
                                $skp['atasan_id'] = $data_reload_atasan['pegawai_atasan_id'];
                                $success_skp = true;
                            }
                    }  catch (AppException $ex) {
                        array_push($error_log, $ex->getMessage());
                        $success_skp = false;
                    }

                    $data_return[$i] = [
                        "nama_pegawai" => $pegawai->nama,
                        "nip_pegawai" => $pegawai->nip,
                        "periode_awal" => $periode_awal,
                        "periode_akhir" => $periode_akhir,
                        "pendekatan" => $item['skp']['pendekatan'],
                        "jenis_skp" => $item['pegawai']['tipeSkp'],
                        "pegawai_unor" => $pegawai->unor_nama,
                        "pegawai_unor_pmm" => $item['pegawai']['namaSekolah'],
                        "pegawai_atasan_data" => null,
                        "pegawai_atasan_data_pmm" => null,
                        "error_log" => null,
                        "success" => true,
                        "catatan" => null,
                        "force" => $return_force,
                        "error_info" => null
                    ];

                    if (strtoupper($pegawai->unor_nama) != strtoupper($item['pegawai']['namaSekolah'])) {
                        array_push($catatan, "Nama Sekolah atau Nama Unor tidak sama dengan data PMM, data PMM '" . $item['pegawai']['namaSekolah'] . "'");
                    }

                    if ($item['pegawai']['pnsIdKs'] && $return_force != 2) {
                        $data_return[$i]['pegawai_atasan_data_pmm'] = Pegawai::getPegawaiSingkat($item['pegawai']['pnsIdKs']);
                        if ($success_skp) {
                            if ($skp['atasan_id']) {
                                $data_return[$i]['pegawai_atasan_data'] = Pegawai::getPegawaiSingkat($skp['atasan_id']);
                                if ($skp['atasan_id'] != $item['pegawai']['pnsIdKs']) {
                                    if ($new_skp == false) {
                                        $info_skp = Skp::detilSkpRingkas($skp['id']);
                                        ///// $atasan = $this->reloadDataAtasan($info_skp);

                                        ////// $data_atasan = $this->getDataAtasanTerbaru($info_skp);

                                        $data_atasan = null;
                                        $unor_atasan_id = null;

                                        if ($info_skp->pegawai_jenis_jabatan == '1' && $info_skp->unor_jenis == '00') {
                                            // jika jenis unor = 00 maka sudah tidak ambil atasan lagi karena sudah eselon tertinggi
                                            $data_atasan = null;
                                        } else {
                                            // cek jika SKP merupakan SKP PLT / PLH
                                            if ($plt_plh = PltPlh::getPltPlhBySkpId($info_skp->skp_id)) {
                                                $unor_atasan_id = $plt_plh->unor_atasan_id;
                                            } else {
                                                if ($info_skp->pegawai_jenis_jabatan == '1') {
                                                    // jika merupakan pimpinan unor, ambil ID unor atasan unor tsb
                                                    $unor_atasan_id = Unor::detilUnor($info_skp->pegawai_unor_id)->unor_atasan_id;
                                                } else {
                                                    // jika merupakan pegawai, ambil ID unor pada SKP sebagai unor atasan
                                                    $unor_atasan_id = $info_skp->pegawai_unor_id;
                                                }
                                            }

                                            ////// $data_atasan = $this->geztPimpinanUnor($unor_atasan_id);
                                            if ($plt_plh = Pegawai::getPltPlhByUnorId($unor_atasan_id)) {
                                                if ($data_atasan = (array) Pegawai::getDataPegawaiPltPlh($plt_plh->id)) {
                                                }
                                            }

                                            if (!$data_atasan) {
                                                // gunakan data atasan definitif
                                                $data_atasan = (array) Pegawai::getDataAtasanByUnorId($unor_atasan_id);
                                                // jika data atasan masih kosong, atau atasan merupakan diri sendiri (karena sebagai PLT/PLH),
                                                // cari 1 tingkat di atasannya lagi
                                                $unor_atasan = Unor::detilUnor($unor_atasan_id);
                                                if (($data_atasan == null || $data_atasan['id'] == null || ($unor_atasan->jenis != '00' && $data_atasan['id'] == Auth::user()->id))
                                                    && ($info_skp->unor_jenis != '00' && $info_skp->unor_jenis != '10' && $info_skp->unor_jenis != '11')) {
                                                    // mengganti unor atasan ke unor diatasnya lagi
                                                    if (!$unor_atasan || !$unor_atasan->unor_atasan_id) {
                                                        $error_info = 5;
                                                        throw new AppException('Unit Kerja atasan tidak ditemukan.');
                                                    }

                                                    $unor_atasan_id = $unor_atasan->unor_atasan_id;
                                                    $data_atasan = $this->getPimpinanUnor($unor_atasan_id);
                                                }

                                                if ($data_atasan == null || $data_atasan['id'] == null || $data_atasan['id'] == Auth::user()->id) {
                                                    // jika masih kosong, cari ke atasannya lagi
                                                    if ($info_skp->unor_jenis != '00' && $info_skp->unor_jenis != '10' && $info_skp->unor_jenis != '11') {
                                                        $unor_atasan = Unor::detilUnor($unor_atasan->unor_atasan_id);

                                                        if ($unor_atasan && $unor_atasan->jenis != '00') {
                                                            $unor_atasan = Unor::detilUnor($unor_atasan->unor_atasan_id);
                                                            $data_atasan = $this->getPimpinanUnor($unor_atasan->id);
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if ( ! $data_atasan) {
                                            $data_atasan = [
                                                "id" => null,
                                                "unor_id" => null,
                                                "jabatan" => null,
                                                "golru" => null,
                                                "unor_nama" => null,
                                                "jenis_jabatan" => null
                                            ];
                                        }
                                        //////

                                        // siapkan data atasan
                                        $data_reload_atasan = [
                                            "pegawai_atasan_id"             => $data_atasan["id"],
                                            "pegawai_atasan_unor_id"        => $data_atasan["unor_id"],
                                            "pegawai_atasan_jabatan"        => $data_atasan["jabatan"],
                                            "pegawai_atasan_golru"          => $data_atasan["golru"],
                                            "pegawai_atasan_unit_kerja"     => $data_atasan["unor_nama"],
                                            "pegawai_atasan_jenis_jabatan"  => $data_atasan["jenis_jabatan"],
                                            "updated_at"                    => date('Y-m-d H:i:s')
                                        ];

                                        // update table skp
                                        DB::table('skp')
                                            ->where(['id' => $info_skp->skp_id])
                                            ->update($data_reload_atasan);
                                        ////

                                        $data_return[$i]['pegawai_atasan_data'] = Pegawai::getPegawaiSingkat($data_atasan["id"]);
                                        if ($data_atasan["id"] != $item['pegawai']['pnsIdKs']) {
                                            $error_info = 6;
                                            array_push($error_log, "Data atasan di PMM berbeda dengan Ekin, data PMM '" . $data_return[$i]['pegawai_atasan_data_pmm']->nama . "'");
                                        }
                                    } else {
                                        $error_info = 6;
                                        array_push($error_log, "Data atasan di PMM berbeda dengan Ekin, data PMM '" . $data_return[$i]['pegawai_atasan_data_pmm']->nama . "'");
                                    }
                                }
                            }
                        }
                    }

                    if ($catatan) {
                        $data_return[$i]['catatan'] =  $catatan;
                        $data_log['catatan'][$i] = $catatan;
                        if ($error_log) {
                            $data_return[$i]['error_log'] =  $error_log;
                            $data_return[$i]['error_info'] =  $error_info;
                            $data_log['error_log'][$i] = $error_log;
                        }
                        if($force == 0) {
                            $data_return[$i]['success'] = false;
                            throw new AppException("a");
                        }
                    }

                    if ($error_log) {
                        $data_return[$i]['success'] = false;
                        $data_return[$i]['error_log'] =  $error_log;
                        $data_return[$i]['error_info'] =  $error_info;
                        $data_log['error_log'][$i] = $error_log;
                        throw new AppException('a');
                    }

                    if ($new_skp == false) {
                        $skp_rhk = (object) $skp;
                        $rhk_lama =  $this->rhkService->getRhk($skp_rhk, null);
                        $rhk_indikator_lama = [];
                        foreach ($rhk_lama['rhk'] as $data_rhk) {
                            foreach($data_rhk['indikators'] as $data_rhk_indikator) {
                                $data_rhk_indikator['bagian'] = $data_rhk['bagian'];
                                $data_rhk_indikator['rhk'] = $data_rhk['rhk'];
                                array_push($rhk_indikator_lama, $data_rhk_indikator);
                            }
                        }
                    }

                    if (Arrays::keyExists($item, "rhkIndividu") && $item['rhkIndividu'] != null)  {
                        try {
                            $table_rhk = 'skp_rhk';
                            $table_rhk_indikator = "skp_indikator";

                            $data_rhk_insert = [];
                            $data_rhk_indikator_insert = [];
                            $count_rhk_new = 0 ;
                            $count_rhk_indikator_new = 0 ;
                            $c = 0 ;
                            $d = 0 ;

                            if ($skp['model'] == 'jajf') {
                                $rhk_atasan_indikator = Rhk::getRhkOrganisasi($skp['id'], true);
                                $rhk_atasan_rhk = Rhk::getRhkOrganisasi($skp['id']);
                            }


                            foreach($item['rhkIndividu'] as $rhk) {
                                $new_rhk = true;
                                $rhk_atasan_id = null;

                                if ($new_skp == false) {
                                    foreach($rhk_lama['rhk'] as $key => $data_rhk_lama) {
                                        if ($data_rhk_lama['bagian'] == 'individu') {
                                            if($rhk['deskripsi'] == $data_rhk_lama['rhk']) {
                                                $data_rhk['id'] = $data_rhk_lama['rhk_id'];
                                                $new_rhk = false;

                                                // update jenis RHK jika ada perubahan
                                                if ($data_rhk_lama['jenis'] != $rhk['jenis']) {
                                                    DB::table('skp_rhk')->where('id', $data_rhk_lama['rhk_id'])->update(['jenis' => $rhk['jenis']]);
                                                }

                                                unset($rhk_lama['rhk'][$key]);
                                                break;
                                            }
                                        }
                                    }
                                }


                                if ($new_rhk == true) {

                                    if ($skp['model'] == 'jajf') {
                                        $persamaan_deskripsi = 0;
                                        if($rhk['rhkAtasanJenis'] == 'indikator') {
                                            foreach($rhk_atasan_indikator as $data_rhk_atasan_indikator) {
                                                $check_persamaan = 0;
                                                similar_text($rhk['rhkAtasan'], $data_rhk_atasan_indikator->deskripsi, $check_persamaan);
                                                if ($check_persamaan == 100) {
                                                    $rhk_atasan_id = $data_rhk_atasan_indikator->id;
                                                    $persamaan_deskripsi = $check_persamaan;
                                                    break;
                                                } else if ($check_persamaan > 85 && $check_persamaan > $persamaan_deskripsi) {
                                                    $persamaan_deskripsi = $check_persamaan;
                                                    $rhk_atasan_id = $data_rhk_atasan_indikator->id;
                                                }
                                            }
                                        } else {
                                            foreach($rhk_atasan_rhk as $data_rhk_atasan_rhk) {
                                                $check_persamaan = 0;
                                                similar_text($rhk['rhkAtasan'], $data_rhk_atasan_rhk->deskripsi, $check_persamaan);
                                                if ($check_persamaan == 100) {
                                                    $rhk_atasan_id = $data_rhk_atasan_rhk->id;
                                                    $persamaan_deskripsi = $check_persamaan;
                                                    break;
                                                } else if ($check_persamaan > 70 && $check_persamaan > $persamaan_deskripsi) {
                                                    $persamaan_deskripsi = $check_persamaan;
                                                    $rhk_atasan_id = $data_rhk_atasan_rhk->id;
                                                }
                                            }
                                        }
                                        if ($persamaan_deskripsi == 0) {
                                            $error_info = 7;
                                            throw new AppException("Tidak ada RHK atasan yang bisa dikaitkan, yaitu '" . $rhk['rhkAtasan'] . "' di RHK '" . $rhk['deskripsi'] . "'.");
                                        }
                                    }

                                    $data_rhk = $this->addRhkSinkroPmm($skp['id'], $skp['skp_organisasi_id'], $rhk, $rhk_atasan_id);
                                    $count_rhk_new++;
                                    array_push($data_rhk_insert, $data_rhk);
                                }


                                foreach($rhk['indikator'] as $rhk_indikator) {
                                    $new_indikator = true;

                                    if ($new_skp == false) {

                                        foreach($rhk_indikator_lama as $key => $data_rhk_indikator_lama) {
                                            if($rhk_indikator['deskripsi'] == $data_rhk_indikator_lama['indikator'] && $rhk['deskripsi'] == $data_rhk_indikator_lama['rhk']) {
                                                $new_indikator = false;
                                                unset($rhk_indikator_lama[$key]);
                                                break;
                                            }
                                        }
                                    }

                                    if($new_indikator == true) {
                                        $data_rhk_indikator = $this->addIndSinkroPmm($skp, $data_rhk['id'], $rhk_indikator);
                                        $count_rhk_indikator_new++;
                                        array_push($data_rhk_indikator_insert, $data_rhk_indikator);
                                    }
                                }

                            }

                            if ($data_rhk_insert) {
                                DB::table($table_rhk)->insert($data_rhk_insert);
                            }

                            if ($data_rhk_indikator_insert) {
                                DB::table($table_rhk_indikator)->insert($data_rhk_indikator_insert);
                            }
                        } catch (AppException $ex) {
                            array_push($error_log, $ex->getMessage());
                        }

                    }

                    if ($error_log) {
                        $data_return[$i]['success'] = false;
                        $data_return[$i]['error_log'] =  $error_log;
                        $data_return[$i]['error_info'] =  $error_info;
                        $data_log['error_log'][$i] = $error_log;
                        throw new AppException('s');
                    }


                    if (Arrays::keyExists($item, "rhkOrganisasi") && $item['rhkOrganisasi'] != null) {
                        try {
                            $table_rhk = 'skp_organisasi_rhk';
                            $table_rhk_indikator = "skp_organisasi_indikator";


                            $data_rhk_insert = [];
                            $data_rhk_indikator_insert = [];
                            $count_rhk_organisasi_new = 0 ;
                            $count_rhk_indikator_organisasi_new = 0 ;
                            $c = 0 ;
                            $d = 0 ;

                            foreach($item['rhkOrganisasi'] as $rhk) {
                                $new_rhk = true;
                                $rhk_atasan_id = null;


                                if ($new_skp == false) {
                                    foreach($rhk_lama['rhk'] as $key => $data_rhk_lama) {
                                        if ($data_rhk_lama['bagian'] == 'organisasi') {
                                            if($rhk['deskripsi'] == $data_rhk_lama['rhk']) {
                                                $data_rhk['id'] = $data_rhk_lama['rhk_id'];
                                                $new_rhk = false;

                                                // update jenis RHK jika ada perubahan
                                                if ($data_rhk_lama['jenis'] != $rhk['jenis']) {
                                                    DB::table('skp_organisasi_rhk')->where('id', $data_rhk_lama['rhk_id'])->update(['jenis' => $rhk['jenis']]);
                                                }

                                                unset($rhk_lama['rhk'][$key]);
                                                break;
                                            }
                                        }
                                    }
                                }

                                if ($new_rhk == true) {
                                    $data_rhk = $this->addRhkSinkroPmm($skp['id'], $skp['skp_organisasi_id'], $rhk, $rhk_atasan_id);
                                    $count_rhk_organisasi_new++;
                                    array_push($data_rhk_insert, $data_rhk);
                                }


                                foreach($rhk['indikator'] as $rhk_indikator) {
                                    $new_indikator = true;

                                    if ($new_skp == false) {

                                        foreach($rhk_indikator_lama as $key => $data_rhk_indikator_lama) {
                                            if ($data_rhk_indikator_lama['bagian'] == 'organisasi') {
                                                if($rhk_indikator['deskripsi'] == $data_rhk_indikator_lama['indikator']) {
                                                    $new_indikator = false;
                                                    unset($rhk_indikator_lama[$key]);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if($new_indikator == true) {
                                        $data_rhk_indikator = $this->addIndSinkroPmm($skp, $data_rhk['id'], $rhk_indikator);
                                        $count_rhk_indikator_organisasi_new++;
                                        array_push($data_rhk_indikator_insert, $data_rhk_indikator);
                                    }
                                }

                            }
                            if ($data_rhk_insert) {
                                DB::table($table_rhk)->insert($data_rhk_insert);
                            }

                            if ($data_rhk_indikator_insert) {
                                DB::table($table_rhk_indikator)->insert($data_rhk_indikator_insert);
                            }
                            $data_return[$i]['success_rhk_organisasi'] = [
                                "rhk_masuk" => $count_rhk_organisasi_new,
                                "rhk_indikator_masuk" => $count_rhk_indikator_organisasi_new,
                            ];
                            $data_log['data'][$i]['success_rhk_organisasi'] = $data_return[$i]['success_rhk_organisasi'];
                        } catch (AppException $ex) {
                            array_push($error_log, $ex->getMessage());
                        }
                    }

                    if ($error_log) {
                        $data_return[$i]['success'] = false;
                        $data_return[$i]['error_log'] =  $error_log;
                        $data_return[$i]['error_info'] =  $error_info;
                        $data_log['error_log'][$i] = $error_log;
                        throw new AppException('s');
                    }

                    $data_return[$i]['success_rhk_individu'] = [
                        "rhk_masuk" => $count_rhk_new,
                        "rhk_indikator_masuk" => $count_rhk_indikator_new,
                    ];

                    $data_log['data'][$i]['success_rhk_individu'] = $data_return[$i]['success_rhk_individu'];

                    $data_return[$i]['rhk_hapus'] = [
                        'rhk_individu' => 0,
                        'rhk_organisasi' => 0,
                    ];

                    $data_return[$i]['rhk_indikator_hapus'] = [
                        'rhk_individu' => 0,
                        'rhk_organisasi' => 0,
                    ];

                    if ($new_skp == false) {
                        if ($rhk_lama['rhk']){
                            $rhk_lama_individu_hapus = [];
                            $rhk_lama_organisasi_hapus = [];
                            foreach($rhk_lama['rhk'] as $data_rhk) {
                                if ($data_rhk['bagian'] == 'organisasi') {
                                    // if (!Rhk::isRhkOrganisasiIntervensi($data_rhk['rhk_id']) && !Rhk::isRhkOrganisasiAdaRealisasi($data_rhk['rhk_id'])) {
                                    //     array_push($rhk_lama_organisasi_hapus, $data_rhk['rhk_id']);
                                    // }
                                } else {
                                    array_push($rhk_lama_individu_hapus, $data_rhk['rhk_id']);
                                }
                            }

                            $data_hapus = [
                                'deleted_at' => date('Y-m-d H:i:s'),
                                'deleted_by' => "SINKRO PMM"
                            ];
                            if ($rhk_lama_individu_hapus) {
                                $data_return[$i]['rhk_hapus']['rhk_individu'] = count($rhk_lama_individu_hapus);
                                $data_log['data'][$i]['rhk_hapus'] = $rhk_lama_individu_hapus;
                                DB::table('skp_rhk')
                                    ->whereIn('id', $rhk_lama_individu_hapus)
                                    ->update($data_hapus);
                            }
                            if ($rhk_lama_organisasi_hapus) {
                                $data_return[$i]['rhk_hapus']['rhk_organisasi'] = count($rhk_lama_organisasi_hapus);
                                $data_log['data'][$i]['rhk_organisasi_hapus'] = $rhk_lama_organisasi_hapus;
                                DB::table('skp_organisasi_rhk')
                                    ->whereIn('id', $rhk_lama_organisasi_hapus)
                                    ->update($data_hapus);
                            }
                        }

                        if ($rhk_indikator_lama) {
                            $rhk_indikator_lama_individu_hapus = [];
                            $rhk_indikator_lama_organisasi_hapus = [];
                            foreach($rhk_indikator_lama as $data_rhk_indikator) {
                                if ($data_rhk_indikator['bagian'] == 'organisasi') {
                                    // if (!Rhk::isIndikatorOrganisasiIntervensi($data_rhk_indikator['id']) && !Rhk::isIndikatorOrganisasiAdaRealisasi($data_rhk_indikator['id'])) {
                                    //     array_push($rhk_indikator_lama_organisasi_hapus, $data_rhk_indikator['id']);
                                    // }
                                } else {
                                    array_push($rhk_indikator_lama_individu_hapus, $data_rhk_indikator['id']);
                                }
                            }

                            $data_hapus = [
                                'deleted_at' => date('Y-m-d H:i:s'),
                                'deleted_by' => "SINKRO PMM"
                            ];
                            if ($rhk_indikator_lama_individu_hapus) {
                                $data_return[$i]['rhk_indikator_hapus']['rhk_individu'] = count($rhk_indikator_lama_individu_hapus);
                                $data_log['data'][$i]['rhk_indikator_hapus'] = $rhk_indikator_lama_individu_hapus;
                                DB::table('skp_indikator')
                                    ->whereIn('id', $rhk_indikator_lama_individu_hapus)
                                    ->update($data_hapus);
                            }
                            if ($rhk_indikator_lama_organisasi_hapus) {
                                $data_return[$i]['rhk_indikator_hapus']['rhk_organisasi'] = count($rhk_indikator_lama_organisasi_hapus);
                                $data_log['data'][$i]['rhk_indikator_organisasi_hapus'] = $rhk_indikator_lama_organisasi_hapus;
                                DB::table('skp_organisasi_indikator')
                                    ->whereIn('id', $rhk_indikator_lama_organisasi_hapus)
                                    ->update($data_hapus);
                            }
                        }
                    }

                    if ($skp['status'] != 'persetujuan') {
                        DB::table('skp')
                            ->where(['id' => $skp['id']])
                            ->update([
                                'status' => "persetujuan",
                                'updated_at' => date('Y-m-d H:i:s')
                            ]);

                            $data = [
                                'skp_id' => $skp['id'],
                                'status' => "persetujuan",
                                'pegawai_perubah' => "SINKRO PMM"
                        ];

                        $this->logService->log('UPDATE_STATUS_SKP_API_BY_SINKRO_PMM', $data);
                    }
                    $i++;
                    DB::commit();
                    $payload_success = [
                        "pnsIdBkn" => $pegawai_id,
                        "skpIdBkn" => $skp['id'],
                        "status" => "success",
                        "periodeAwal" => $periode_awal,
                        "periodeAkhir" => $periode_akhir,
                        "pendekatan" => $item['skp']['pendekatan'],
                        "jenisSkp" => $item['pegawai']['tipeSkp'],
                        "message" => null,
                    ];

                    $this->pmmSinkroService->postSuccessSkp($payload_success);
                } catch (AppException $ex) {
                    if ($data_return[$i]['error_log']) {
                        $payload_success = [
                            "pnsIdBkn" => $pegawai_id,
                            "skpIdBkn" => null,
                            "status" => "failed",
                            "periodeAwal" => $periode_awal,
                            "periodeAkhir" => $periode_akhir,
                            "pendekatan" => $item['skp']['pendekatan'],
                            "jenisSkp" => $item['pegawai']['tipeSkp'],
                            "message" => $data_return[$i]['error_log'][0],
                        ];

                        $this->pmmSinkroService->postSuccessSkp($payload_success);
                    }
                    $data_log['success_sinkro'] = false;
                    $i++;
                    DB::rollback();
                }
            }
        }

        DB::table('pegawai')
        ->where(['id' => $pegawai->id])
        ->update([
            'last_sync_pmm' => date('Y-m-d H:i:s')
        ]);


        if($list_pmm_error_id) {
            DB::table('list_pmm_error_sync')
            ->where(['id' => $list_pmm_error_id])
            ->update([
                'status' => $data_log['success_sinkro'],
                'error_log' => $data_log['error_log'] ? json_encode($data_log['error_log'][0][0]) : null,
                'catatan' => $data_log['catatan'] ? json_encode($data_log['catatan'][0][0]) : null,
                'updated_at' => date('Y-m-d H:i:s')
            ]);
        } else {
            DB::table('list_pmm_error_sync')
            ->insert([
                'pns_id' => $pegawai->id,
                'created_at' => date('Y-m-d H:i:s'),
                'ref_instansi_id' => $pegawai->ref_instansi_id,
                'tahun' => date('Y'),
                'tipe_skp' => $tipe_skp,
                'is_pegawai_exists' => 1,
                'status' => $data_log['success_sinkro'],
                'error_log' => $data_log['error_log'] ? json_encode($data_log['error_log'][0][0]) : null,
                'catatan' => $data_log['catatan'] ? json_encode($data_log['catatan'][0][0]) : null,
            ]);
        }

        $this->logService->log_sinkro_pmm($data_log);
        return $this->successJsonResponse($data_return);

    }
Leave a Comment