USE [HRISDEV]
GO
/****** Object:  StoredProcedure [dbo].[usp_UpdateTglEndRiwayatJabatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_UpdateTglEndRiwayatJabatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_UpdateTglEndRiwayatJabatan]
GO
/****** Object:  StoredProcedure [dbo].[usp_test]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_test]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_test]
GO
/****** Object:  StoredProcedure [dbo].[usp_rpt_Get_Master_Kategori]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_rpt_Get_Master_Kategori]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_rpt_Get_Master_Kategori]
GO
/****** Object:  StoredProcedure [dbo].[usp_NonAktifCLTO]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_NonAktifCLTO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_NonAktifCLTO]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookListAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookListAktifitas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookListAktifitas]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookEditAssessment]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookEditAssessment]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookEditAssessment]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookEditAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookEditAktifitas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookEditAktifitas]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDetailAssessment]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookDetailAssessment]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookDetailAssessment]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDetailAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookDetailAktifitas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookDetailAktifitas]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDeleteAssessment]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookDeleteAssessment]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookDeleteAssessment]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDeleteAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookDeleteAktifitas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookDeleteAktifitas]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookAddAssessment]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookAddAssessment]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookAddAssessment]
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookAddAktivitas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogbookAddAktivitas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_LogbookAddAktivitas]
GO
/****** Object:  StoredProcedure [dbo].[usp_list_pegawai_satker_lintas_bidang]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_list_pegawai_satker_lintas_bidang]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_list_pegawai_satker_lintas_bidang]
GO
/****** Object:  StoredProcedure [dbo].[usp_insertHistoryRegional]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_insertHistoryRegional]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_insertHistoryRegional]
GO
/****** Object:  StoredProcedure [dbo].[usp_InsertFormasiEfektif]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_InsertFormasiEfektif]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_InsertFormasiEfektif]
GO
/****** Object:  StoredProcedure [dbo].[usp_getUserAccess]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getUserAccess]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getUserAccess]
GO
/****** Object:  StoredProcedure [dbo].[usp_getSpParameter]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getSpParameter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getSpParameter]
GO
/****** Object:  StoredProcedure [dbo].[usp_getSimfosiaWebService]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getSimfosiaWebService]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getSimfosiaWebService]
GO
/****** Object:  StoredProcedure [dbo].[usp_getSatker]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getSatker]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getSatker]
GO
/****** Object:  StoredProcedure [dbo].[usp_getRoleEmail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getRoleEmail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getRoleEmail]
GO
/****** Object:  StoredProcedure [dbo].[usp_getPemenuhanSDM]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getPemenuhanSDM]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getPemenuhanSDM]
GO
/****** Object:  StoredProcedure [dbo].[usp_getNotification]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getNotification]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getNotification]
GO
/****** Object:  StoredProcedure [dbo].[usp_GetHistorisPayTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_GetHistorisPayTunjangan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_GetHistorisPayTunjangan]
GO
/****** Object:  StoredProcedure [dbo].[usp_getFieldTVAlias]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getFieldTVAlias]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getFieldTVAlias]
GO
/****** Object:  StoredProcedure [dbo].[usp_getEmployeeBirthday]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getEmployeeBirthday]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getEmployeeBirthday]
GO
/****** Object:  StoredProcedure [dbo].[usp_getDMSAccessScope]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getDMSAccessScope]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getDMSAccessScope]
GO
/****** Object:  StoredProcedure [dbo].[usp_GetDetailHistorisPayTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_GetDetailHistorisPayTunjangan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_GetDetailHistorisPayTunjangan]
GO
/****** Object:  StoredProcedure [dbo].[usp_getDataRiwayatTugasDefinitif]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getDataRiwayatTugasDefinitif]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getDataRiwayatTugasDefinitif]
GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarJabatanStruktural]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getDaftarJabatanStruktural]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getDaftarJabatanStruktural]
GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarJabatanAktif]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getDaftarJabatanAktif]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getDaftarJabatanAktif]
GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarCutiGantung]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getDaftarCutiGantung]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getDaftarCutiGantung]
GO
/****** Object:  StoredProcedure [dbo].[usp_getBebanKerja]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getBebanKerja]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getBebanKerja]
GO
/****** Object:  StoredProcedure [dbo].[usp_getAtasanSimfosia]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getAtasanSimfosia]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getAtasanSimfosia]
GO
/****** Object:  StoredProcedure [dbo].[usp_getAllTipeForAlias]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getAllTipeForAlias]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getAllTipeForAlias]
GO
/****** Object:  StoredProcedure [dbo].[usp_getAktivitasIndividu]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_getAktivitasIndividu]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_getAktivitasIndividu]
GO
/****** Object:  StoredProcedure [dbo].[usp_generateCutiPegawai]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_generateCutiPegawai]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_generateCutiPegawai]
GO
/****** Object:  StoredProcedure [dbo].[usp_duplikasi_formasi_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_duplikasi_formasi_pegawai]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_duplikasi_formasi_pegawai]
GO
/****** Object:  StoredProcedure [dbo].[usp_drag_drop_struktur_organisasi]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_drag_drop_struktur_organisasi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_drag_drop_struktur_organisasi]
GO
/****** Object:  StoredProcedure [dbo].[usp_deleteRumahSakit]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_deleteRumahSakit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_deleteRumahSakit]
GO
/****** Object:  StoredProcedure [dbo].[usp_deleteCutiGantung]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_deleteCutiGantung]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_deleteCutiGantung]
GO
/****** Object:  StoredProcedure [dbo].[usp_CleansingSisaCuti]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_CleansingSisaCuti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_CleansingSisaCuti]
GO
/****** Object:  StoredProcedure [dbo].[usp_cekLepasCuti]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_cekLepasCuti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_cekLepasCuti]
GO
/****** Object:  StoredProcedure [dbo].[usp_CaptureHistoryTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_CaptureHistoryTunjangan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_CaptureHistoryTunjangan]
GO
/****** Object:  StoredProcedure [dbo].[usp_calendar_workday]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_calendar_workday]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_calendar_workday]
GO
/****** Object:  StoredProcedure [dbo].[usp_AktivasiTunjanganRumah]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_AktivasiTunjanganRumah]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_AktivasiTunjanganRumah]
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_kepangkatan_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_tmt_riwayat_kepangkatan_bak_20160219]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_tmt_riwayat_kepangkatan_bak_20160219]
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_kepangkatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_tmt_riwayat_kepangkatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_tmt_riwayat_kepangkatan]
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan_bak_20191114]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_tmt_riwayat_jabatan_bak_20191114]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_tmt_riwayat_jabatan_bak_20191114]
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_tmt_riwayat_jabatan_bak_20160219]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_tmt_riwayat_jabatan_bak_20160219]
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_tmt_riwayat_jabatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_tmt_riwayat_jabatan]
GO
/****** Object:  StoredProcedure [dbo].[update_status_tunjangan_keluarga_peg]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_status_tunjangan_keluarga_peg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_status_tunjangan_keluarga_peg]
GO
/****** Object:  StoredProcedure [dbo].[update_status_tunjangan_keluarga]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_status_tunjangan_keluarga]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_status_tunjangan_keluarga]
GO
/****** Object:  StoredProcedure [dbo].[update_data_tmt_lvl_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_data_tmt_lvl_jabatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_data_tmt_lvl_jabatan]
GO
/****** Object:  StoredProcedure [dbo].[update_data_tmt_all]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_data_tmt_all]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_data_tmt_all]
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_nip_trg_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_data_current_nip_trg_bak_20160219]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_data_current_nip_trg_bak_20160219]
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_nip_trg]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_data_current_nip_trg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_data_current_nip_trg]
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_all]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[update_data_current_all]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[update_data_current_all]
GO
/****** Object:  StoredProcedure [dbo].[sp_send_invitation]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_send_invitation]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_send_invitation]
GO
/****** Object:  StoredProcedure [dbo].[sp_get_error_message]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_get_error_message]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_get_error_message]
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_sk_pengangkatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_generate_sk_pengangkatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_generate_sk_pengangkatan]
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_ranking]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_generate_ranking]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_generate_ranking]
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_pencarian_pegawai_mst]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_generate_pencarian_pegawai_mst]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_generate_pencarian_pegawai_mst]
GO
/****** Object:  StoredProcedure [dbo].[SaldoCutiTahunan_backup]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SaldoCutiTahunan_backup]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SaldoCutiTahunan_backup]
GO
/****** Object:  StoredProcedure [dbo].[SaldoCutiTahunan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SaldoCutiTahunan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SaldoCutiTahunan]
GO
/****** Object:  StoredProcedure [dbo].[rep_tidak_lolos_shortlist]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_tidak_lolos_shortlist]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_tidak_lolos_shortlist]
GO
/****** Object:  StoredProcedure [dbo].[rep_shortlist_per_depkom]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_shortlist_per_depkom]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_shortlist_per_depkom]
GO
/****** Object:  StoredProcedure [dbo].[rep_shortlist_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_shortlist_per_bidang]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_shortlist_per_bidang]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_sebelum_rekomendasi_pelamar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_pelamar]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_sebelum_rekomendasi_lowjab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_lowjab]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_detail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_sebelum_rekomendasi_detail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_detail]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_sebelum_rekomendasi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan_excel]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_per_lowongan_excel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_per_lowongan_excel]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan_detail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_per_lowongan_detail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_per_lowongan_detail]
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pelamar_per_lowongan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pelamar_per_lowongan]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_pelamar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_pelamar]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_selisih]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_selisih]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_selisih]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_melamar]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_melamar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_melamar]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_eligible]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_eligible]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_eligible]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_eligible]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar_eligible]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_eligible]
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_pegawai_eligible_yang_melamar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_pelamar_longlist]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_pelamar_longlist]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_pelamar_longlist]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_shortlist]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_shortlist]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_shortlist]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist_depkom]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist_depkom]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist_depkom]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_internal_per_bidang_pelamar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_pelamar]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_internal_per_bidang_lowjab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_lowjab]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_internal_per_bidang_detail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_detail]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_internal_per_bidang]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_lowjab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_lowjab]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_eligible]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_eligible]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_eligible]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_detail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_detail]
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang]
GO
/****** Object:  StoredProcedure [dbo].[proc_list_surat_tugas_20201230]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_list_surat_tugas_20201230]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[proc_list_surat_tugas_20201230]
GO
/****** Object:  StoredProcedure [dbo].[proc_list_surat_tugas]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_list_surat_tugas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[proc_list_surat_tugas]
GO
/****** Object:  StoredProcedure [dbo].[proc_insert_presensi_upacara]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_insert_presensi_upacara]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[proc_insert_presensi_upacara]
GO
/****** Object:  StoredProcedure [dbo].[proc_generate_random_string]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_generate_random_string]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[proc_generate_random_string]
GO
/****** Object:  StoredProcedure [dbo].[PrintLongString]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PrintLongString]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PrintLongString]
GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pergerakan_skala_gaji_bak_20160219]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pergerakan_skala_gaji_bak_20160219]
GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji_bak]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pergerakan_skala_gaji_bak]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pergerakan_skala_gaji_bak]
GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pergerakan_skala_gaji]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pergerakan_skala_gaji]
GO
/****** Object:  StoredProcedure [dbo].[newKasusnomor]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[newKasusnomor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[newKasusnomor]
GO
/****** Object:  StoredProcedure [dbo].[loader_pengaturan_absensi]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[loader_pengaturan_absensi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[loader_pengaturan_absensi]
GO
/****** Object:  StoredProcedure [dbo].[list_pegawai_logbook]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[list_pegawai_logbook]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[list_pegawai_logbook]
GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata_peg_pensiun]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[insert_tempdata_peg_pensiun]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[insert_tempdata_peg_pensiun]
GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata_peg_baru]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[insert_tempdata_peg_baru]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[insert_tempdata_peg_baru]
GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[insert_tempdata]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[insert_tempdata]
GO
/****** Object:  StoredProcedure [dbo].[insert_dump_error]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[insert_dump_error]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[insert_dump_error]
GO
/****** Object:  StoredProcedure [dbo].[getMaxIdPermohonanCuti]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getMaxIdPermohonanCuti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getMaxIdPermohonanCuti]
GO
/****** Object:  StoredProcedure [dbo].[generate_kasusnomor]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[generate_kasusnomor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[generate_kasusnomor]
GO
/****** Object:  StoredProcedure [dbo].[FW_userPermission]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FW_userPermission]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[FW_userPermission]
GO
/****** Object:  StoredProcedure [dbo].[EIS_PROMOSI]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EIS_PROMOSI]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[EIS_PROMOSI]
GO
/****** Object:  StoredProcedure [dbo].[cek_pergerakan_skala_gaji]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cek_pergerakan_skala_gaji]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[cek_pergerakan_skala_gaji]
GO
/****** Object:  StoredProcedure [db_mart].[usp_insert_data_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[db_mart].[usp_insert_data_pegawai]') AND type in (N'P', N'PC'))
DROP PROCEDURE [db_mart].[usp_insert_data_pegawai]
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[db_mart].[usp_data_pegawai]') AND type in (N'P', N'PC'))
DROP PROCEDURE [db_mart].[usp_data_pegawai]
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_eligible_promosi]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[db_mart].[usp_data_eligible_promosi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [db_mart].[usp_data_eligible_promosi]
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_bulan_tahun]    Script Date: 29/03/2023 10:07:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[db_mart].[usp_data_bulan_tahun]') AND type in (N'P', N'PC'))
DROP PROCEDURE [db_mart].[usp_data_bulan_tahun]
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_bulan_tahun]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author		: Reza Andrian
-- Create date	: 17 Feb 2020
-- Description	: Data Bulan dan Tahun
-- =============================================
CREATE PROCEDURE [db_mart].[usp_data_bulan_tahun]

AS
BEGIN
SELECT
  CASE DATEPART(MONTH, GETDATE())
    WHEN 1 THEN 'JANUARI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 2 THEN 'FEBRUARI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 3 THEN 'MARET' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 4 THEN 'APRIL' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 5 THEN 'MEI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 6 THEN 'JUNI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 7 THEN 'JULI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 8 THEN 'AGUSTUS' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 9 THEN 'SEPTEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 10 THEN 'OKTOBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 11 THEN 'NOVEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
    WHEN 12 THEN 'DESEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
  END AS BULAN_TAHUN,
  CASE DATEPART(MONTH, GETDATE())
    WHEN 1 THEN 'JANUARI'
    WHEN 2 THEN 'FEBRUARI'
    WHEN 3 THEN 'MARET'
    WHEN 4 THEN 'APRIL'
    WHEN 5 THEN 'MEI'
    WHEN 6 THEN 'JUNI'
    WHEN 7 THEN 'JULI'
    WHEN 8 THEN 'AGUSTUS'
    WHEN 9 THEN 'SEPTEMBER'
    WHEN 10 THEN 'OKTOBER'
    WHEN 11 THEN 'NOVEMBER'
    WHEN 12 THEN 'DESEMBER'
  END AS BULAN,
  CONVERT(varchar(10), CONVERT(varchar(10), YEAR(GETDATE()))) AS TAHUN
END
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_eligible_promosi]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [db_mart].[usp_data_eligible_promosi]
	-- Add the parameters for the stored procedure here
	@STS_PENSIUN int,@Year1 int, @Year2 int
AS
	IF(@STS_PENSIUN = 0 OR @STS_PENSIUN = 1 )
BEGIN
SELECT *,
CASE when rumus_masadinas_tinggi = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk2 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK' THEN 'IYA' else 'TIDAK' END RUMUS_FINAL1,
CASE when rumus_masadinas_reguler = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk3 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK' THEN 'IYA' else 'TIDAK' END RUMUS_FINAL2,
CASE when ((rumus_masadinas_tinggi = 'IYA' AND rumus_pensiun_reguler = 'IYA' AND rumus_nk2 = 'IYA' AND rumus_pendidikan = 'IYA' AND  status_sanksi = 'TIDAK') or (rumus_masadinas_reguler = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk3 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK')) THEN 'ELIGIBLE' ELSE 'BELUM' END RUMUS_FINAL3
 FROM (
SELECT dc.NIP,dc.NM_PEG,
	CASE
		WHEN cast(LEFT(HRIS.dbo.MasaDinasTahunBulan(dc.TMT_GOL, GETDATE()),2) as int) >= cast(left(es.TINGGI,2) as int) THEN 'IYA'
	ELSE 'TIDAK'
	END AS RUMUS_MASADINAS_TINGGI,
	CASE
		WHEN cast(LEFT(HRIS.dbo.MasaDinasTahunBulan(dc.TMT_GOL, GETDATE()),2) as int) >= cast(left(es.REGULER,2) as int) THEN 'IYA'
		ELSE 'TIDAK'
		END AS RUMUS_MASADINAS_REGULER,
	CASE
		WHEN cast(left(HRIS.dbo.MasaDinasTahunBulan(GETDATE(), dc.TMT_PENSIUN),2) as int) >= 1 THEN 'IYA'
		ELSE 'TIDAK'
		END AS RUMUS_PENSIUN_REGULER,
	CASE 
		WHEN nk.nilai1 >= 4 and nk.nilai2 >=4 then 'IYA' else 'TIDAK' END RUMUS_NK1,
	CASE 
		WHEN nk.nilai1 >= 3.5 and nk.nilai2 >=3.5 then 'IYA' else 'TIDAK' END RUMUS_NK2,
	CASE 
		WHEN nilai1 >= 3 and nilai2 >=3 then 'IYA' else 'TIDAK' END RUMUS_NK3,
	CASE 
		WHEN dc.nm_pend_umum in ('S1','S2','S3') then 'IYA' else 'TIDAK' END RUMUS_PENDIDIKAN,ISNULL(SRHD.HUKUMAN,'Tidak') AS STATUS_SANKSI
	 FROM HRIS.dbo.SPG_DATA_CURRENT dc
	OUTER APPLY 
	(
	SELECT es.KD_ESELON,es.NM_ESELON,es.TINGGI,es.REGULER FROM (SELECT KD_ESELON,NM_ESELON,
		CASE 
			WHEN KD_ESELON IN (50) THEN '5 Tahun' WHEN KD_ESELON IN (21,30,40) THEN '3 Tahun' WHEN KD_ESELON IN (11,20) THEN '2 Tahun' end TINGGI,
		CASE
			 WHEN KD_ESELON IN (50) THEN '6 Tahun' WHEN KD_ESELON IN (21,30,40) THEN '4 Tahun' WHEN KD_ESELON IN (11,20) THEN '3 Tahun' end REGULER
		FROM HRIS.dbo.SPG_ESELON) as es
		WHERE dc.KD_ESELON = es.KD_ESELON
		) es
	OUTER APPLY
	(
		select NIP
		,max(case when periode = @Year1 THEN periode END) PERIODE1
		,max(case when periode = @Year1 THEN nilai END) NILAI1
		,max(case when periode = @Year1 THEN LEVEL_KINERJA END) LEVEL_KINERJA1
		,max(case when periode = @Year2 THEN periode END) PERIODE2
		,max(case when periode = @Year2 THEN nilai END) NILAI2
		,max(case when periode = @Year2 THEN LEVEL_KINERJA END) LEVEL_KINERJA2
		from 
		(select nip,year(TANGGAL_MULAI) periode,nilai,LEVEL_KINERJA from HRIS.dbo.PFM_RIWAYAT_KINERJA) a 
		where periode in (@Year1 ,@Year2) AND a.NIP = dc.NIP
		group by nip
	) nk
	OUTER APPLY
	(
		SELECT TOP 1
			KET_MASALAH,JNS_TINDAKAN_ADM_LAIN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_MULAI_HUKUMAN 
				ELSE NULL 
			END TGL_MULAI_HUKUMAN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_AKHIR_HUKUMAN 
				ELSE NULL 
			END TGL_AKHIR_HUKUMAN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN 'Ya' 
				ELSE 'Tidak' 
			END HUKUMAN,KD_JNS_SANKSI,
		CASE
			WHEN KD_JNS_SANKSI = '1' THEN 'Sanksi Administrasi'
			WHEN KD_JNS_SANKSI = '2' THEN 'Sanksi Financial'
		END AS JENIS_SANKSI,KD_TINGKAT_SANKSI,
		CASE
			WHEN SR.KD_TINGKAT_SANKSI = '1' THEN 'Sanksi Ringan'
			WHEN SR.KD_TINGKAT_SANKSI = '2' THEN 'Sanksi Sedang'
			WHEN SR.KD_TINGKAT_SANKSI = '3' THEN 'Sanksi Berat'
			WHEN SR.KD_TINGKAT_SANKSI = '4' THEN 'Sanksi Financial'
		END AS TINGKAT_SANKSI
		FROM HRIS.dbo.SPG_RIWAYAT_HUKUMAN_DISIPLIN (NOLOCK) AS SR
		WHERE
			SR.NIP = dc.NIP
			AND SR.KD_STATUS_VALIDASI='1'
		ORDER BY
			SR.TGL_AKHIR_HUKUMAN DESC
	) AS SRHD
	WHERE dc.STS_PENSIUN=@STS_PENSIUN
) AS t 
END
ELSE IF(@STS_PENSIUN = 2)
BEGIN
SELECT *,
CASE when rumus_masadinas_tinggi = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk2 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK' THEN 'IYA' else 'TIDAK' END RUMUS_FINAL1,
CASE when rumus_masadinas_reguler = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk3 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK' THEN 'IYA' else 'TIDAK' END RUMUS_FINAL2,
CASE when ((rumus_masadinas_tinggi = 'IYA' AND rumus_pensiun_reguler = 'IYA' AND rumus_nk2 = 'IYA' AND rumus_pendidikan = 'IYA' AND  status_sanksi = 'TIDAK') or (rumus_masadinas_reguler = 'IYA' AND rumus_pensiun_reguler = 'IYA' and rumus_nk3 = 'IYA' and rumus_pendidikan = 'IYA' AND status_sanksi = 'TIDAK')) THEN 'ELIGIBLE' ELSE 'BELUM' END RUMUS_FINAL3
 FROM (
SELECT dc.NIP,dc.NM_PEG,
	CASE
		WHEN cast(LEFT(HRIS.dbo.MasaDinasTahunBulan(dc.TMT_GOL, GETDATE()),2) as int) >= cast(left(es.TINGGI,2) as int) THEN 'IYA'
	ELSE 'TIDAK'
	END AS RUMUS_MASADINAS_TINGGI,
	CASE
		WHEN cast(LEFT(HRIS.dbo.MasaDinasTahunBulan(dc.TMT_GOL, GETDATE()),2) as int) >= cast(left(es.REGULER,2) as int) THEN 'IYA'
		ELSE 'TIDAK'
		END AS RUMUS_MASADINAS_REGULER,
	CASE
		WHEN cast(left(HRIS.dbo.MasaDinasTahunBulan(GETDATE(), dc.TMT_PENSIUN),2) as int) >= 1 THEN 'IYA'
		ELSE 'TIDAK'
		END AS RUMUS_PENSIUN_REGULER,
	CASE 
		WHEN nk.nilai1 >= 4 and nk.nilai2 >=4 then 'IYA' else 'TIDAK' END RUMUS_NK1,
	CASE 
		WHEN nk.nilai1 >= 3.5 and nk.nilai2 >=3.5 then 'IYA' else 'TIDAK' END RUMUS_NK2,
	CASE 
		WHEN nilai1 >= 3 and nilai2 >=3 then 'IYA' else 'TIDAK' END RUMUS_NK3,
	CASE 
		WHEN dc.nm_pend_umum in ('S1','S2','S3') then 'IYA' else 'TIDAK' END RUMUS_PENDIDIKAN,ISNULL(SRHD.HUKUMAN,'Tidak') AS STATUS_SANKSI
	 FROM HRIS.dbo.SPG_DATA_CURRENT dc
	OUTER APPLY 
	(
	SELECT es.KD_ESELON,es.NM_ESELON,es.TINGGI,es.REGULER FROM (SELECT KD_ESELON,NM_ESELON,
		CASE 
			WHEN KD_ESELON IN (50) THEN '5 Tahun' WHEN KD_ESELON IN (21,30,40) THEN '3 Tahun' WHEN KD_ESELON IN (11,20) THEN '2 Tahun' end TINGGI,
		CASE
			 WHEN KD_ESELON IN (50) THEN '6 Tahun' WHEN KD_ESELON IN (21,30,40) THEN '4 Tahun' WHEN KD_ESELON IN (11,20) THEN '3 Tahun' end REGULER
		FROM HRIS.dbo.SPG_ESELON) as es
		WHERE dc.KD_ESELON = es.KD_ESELON
		) es
	OUTER APPLY
	(
		select NIP
		,max(case when periode = @Year1 THEN periode END) PERIODE1
		,max(case when periode = @Year1 THEN nilai END) NILAI1
		,max(case when periode = @Year1 THEN LEVEL_KINERJA END) LEVEL_KINERJA1
		,max(case when periode = @Year2 THEN periode END) PERIODE2
		,max(case when periode = @Year2 THEN nilai END) NILAI2
		,max(case when periode = @Year2 THEN LEVEL_KINERJA END) LEVEL_KINERJA2
		from 
		(select nip,year(TANGGAL_MULAI) periode,nilai,LEVEL_KINERJA from HRIS.dbo.PFM_RIWAYAT_KINERJA) a 
		where periode in (@Year1 ,@Year2) AND a.NIP = dc.NIP
		group by nip
	) nk
	OUTER APPLY
	(
		SELECT TOP 1
			KET_MASALAH,JNS_TINDAKAN_ADM_LAIN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_MULAI_HUKUMAN 
				ELSE NULL 
			END TGL_MULAI_HUKUMAN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_AKHIR_HUKUMAN 
				ELSE NULL 
			END TGL_AKHIR_HUKUMAN,
			CASE
				WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN 'Ya' 
				ELSE 'Tidak' 
			END HUKUMAN,KD_JNS_SANKSI,
		CASE
			WHEN KD_JNS_SANKSI = '1' THEN 'Sanksi Administrasi'
			WHEN KD_JNS_SANKSI = '2' THEN 'Sanksi Financial'
		END AS JENIS_SANKSI,KD_TINGKAT_SANKSI,
		CASE
			WHEN SR.KD_TINGKAT_SANKSI = '1' THEN 'Sanksi Ringan'
			WHEN SR.KD_TINGKAT_SANKSI = '2' THEN 'Sanksi Sedang'
			WHEN SR.KD_TINGKAT_SANKSI = '3' THEN 'Sanksi Berat'
			WHEN SR.KD_TINGKAT_SANKSI = '4' THEN 'Sanksi Financial'
		END AS TINGKAT_SANKSI
		FROM HRIS.dbo.SPG_RIWAYAT_HUKUMAN_DISIPLIN (NOLOCK) AS SR
		WHERE
			SR.NIP = dc.NIP
			AND SR.KD_STATUS_VALIDASI='1'
		ORDER BY
			SR.TGL_AKHIR_HUKUMAN DESC
	) AS SRHD
) AS t 
END
ELSE 
BEGIN
 PRINT N'Please add sts_pensiun 0 OR 1 and year';  
END;
GO
/****** Object:  StoredProcedure [db_mart].[usp_data_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author		: Reza Andrian
-- Create date	: 02 Feb 2020
-- Description	: Data Mart Pegawai 
-- =============================================
CREATE PROCEDURE [db_mart].[usp_data_pegawai]
AS
BEGIN
 SELECT PGW.NIP,PGW.NM_PEG,PGW.NIP_LAMA,PGW.NM_STATUS_KEPEG,PGW.KD_AGAMA,PGW.NM_AGAMA,PGW.TGL_LAHIR_PEG,
		HRIS.dbo.MasaDinasTahunBulanDataMart(PGW.TGL_LAHIR_PEG,GETDATE()) USIA_SAAT_INI,
		CASE
			WHEN YEAR(PGW.TGL_LAHIR_PEG) >= 1940 and YEAR(PGW.TGL_LAHIR_PEG) <= 1964 THEN 'BOOMERS'
			WHEN YEAR(PGW.TGL_LAHIR_PEG) >= 1981 and YEAR(PGW.TGL_LAHIR_PEG) <= 1994 THEN 'Y'
			WHEN YEAR(PGW.TGL_LAHIR_PEG) >= 1965 and YEAR(PGW.TGL_LAHIR_PEG) <= 1980 THEN 'X'
			WHEN YEAR(PGW.TGL_LAHIR_PEG) >= 1995 and YEAR(PGW.TGL_LAHIR_PEG) <= YEAR(GETDATE()) THEN 'Z'
		END AS GEN,PGW.TEMPAT_LAHIR_PEG,
		PGW.JNS_KELAMIN_PEG,JKL.NM_KELAMIN_PEG,PGW.STS_PERKAWINAN,KWN.KET_STS_KAWIN,
		CASE 
			WHEN SPG.STS_PERKAWINAN = '1' THEN '' 
			WHEN SPG.STS_PERKAWINAN = '2' THEN ''
			WHEN SPG.STS_PERKAWINAN = '3' THEN SPG.STS_SIPIL
			WHEN SPG.STS_PERKAWINAN = '4' THEN SPG.STS_SIPIL     
			END AS STS_SIPIL,
		CASE 
			WHEN SPG.STS_SIPIL = '1' THEN 'Cerai'
			WHEN SPG.STS_SIPIL = '2' THEN 'Meninggal' ELSE '' END AS KET_STS_SIPIL,
		PGW.NIP_PASANGAN,
		PGW.EMAIL,PGW.EMAIL_2,
		PGW.KD_PROPINSI,PRO.NM_PROPINSI,PGW.KD_KABUPATEN,KAB.NM_KABUPATEN,PGW.KD_KECAMATAN,KEC.NM_KECAMATAN,PGW.KD_KELURAHAN,KEL.NM_KELURAHAN,
		PGW.JL_TMP_TINGGAL_CUR,
		CASE
			WHEN COALESCE(SPG.KD_PROPINSI_HOMEBASE2,'') = '' THEN SPG.KD_PROPINSI_HOMEBASE
			ELSE SPG.KD_PROPINSI_HOMEBASE2
		END AS KD_PROPINSI_HOMEBASE,
		CASE
			WHEN COALESCE(SPG.KD_PROPINSI_HOMEBASE2,'') = '' THEN PROL.NM_PROPINSI
			ELSE PROR.NM_PROPINSI
		END AS NM_PROPINSI_HOMEBASE,
		CASE
			WHEN COALESCE(SPG.KD_PROPINSI_HOMEBASE2,'') = '' THEN SPG.KD_KABUPATEN_HOMEBASE
			ELSE SPG.KD_KABUPATEN_HOMEBASE2
		END AS KD_KABUPATEN_HOMEBASE,
		CASE
			WHEN COALESCE(SPG.KD_PROPINSI_HOMEBASE2,'') = '' THEN KABL.NM_KABUPATEN
			ELSE KABR.NM_KABUPATEN
		END AS NM_KABUPATEN_HOMEBASE,
		SPG.KD_PROPINSI_HOMEBASE as KD_PROPINSI_HOMEBASE_AWAL,PROL.NM_PROPINSI AS NM_PROPINSI_HOMEBASE_AWAL,SPG.KD_KABUPATEN_HOMEBASE AS KD_KABUPATEN_HOMEBASE_AWAL,KABL.NM_KABUPATEN AS NM_KABUPATEN_HOMEBASE_AWAL,
		SPG.KD_PROPINSI_HOMEBASE2 as KD_PROPINSI_HOMEBASE_AKHIR,PROR.NM_PROPINSI AS NM_PROPINSI_HOMEBASE_AKHIR,SPG.KD_KABUPATEN_HOMEBASE2 AS KD_KABUPATEN_HOMEBASE_AKHIR,KABR.NM_KABUPATEN AS NM_KABUPATEN_HOMEBASE_AKHIR,
		PGW.NPWP,SPG.PHONE_KANTOR,SPG.PHONE_KANTOR_EXT,SPG.HANDPHONE_1,SPG.HANDPHONE_2,
		SPG.STS_PENERIMAAN_PEG AS KD_STS_PENERIMAAN_PEG,
		CASE
			WHEN SPG.STS_PENERIMAAN_PEG = '100' THEN 'PCT (Pendidikan Calon PTU)'
			WHEN SPG.STS_PENERIMAAN_PEG = '200' THEN 'PCS (Pendidikan Calon Staf)'
			WHEN SPG.STS_PENERIMAAN_PEG = '300' THEN 'PCP (Pendidikan Calon Pejabat)'
			WHEN SPG.STS_PENERIMAAN_PEG = '400' THEN 'PCE (Pendidikan Calon Staf Edukasi dan Perlindungan Konsumen)'
			WHEN SPG.STS_PENERIMAAN_PEG = '500' THEN 'Ex Penugasan'
			WHEN SPG.STS_PENERIMAAN_PEG = '600' THEN 'Ex PKWT'
		END AS  STS_PENERIMAAN_PEG,SPG.ANGKATAN,SPG.TAHUN_PEREKRUTAN,
		PGW.STS_PENSIUN,PGW.KD_TIDAK_AKTIF,
		CASE
			WHEN PGW.KD_TIDAK_AKTIF = '1' THEN 'Pensiun Normal'
			WHEN PGW.KD_TIDAK_AKTIF = '2' THEN 'Pensiun Dini'
			WHEN PGW.KD_TIDAK_AKTIF = '3' THEN 'Meninggal Dunia'
			WHEN PGW.KD_TIDAK_AKTIF = '4' THEN 'Mengundurkan Diri'
			WHEN PGW.KD_TIDAK_AKTIF = '5' THEN 'Diberhentikan'
			WHEN PGW.KD_TIDAK_AKTIF = '8' THEN 'Cuti Luar Tanggungan OJK'
			WHEN PGW.KD_TIDAK_AKTIF = '9' THEN 'Diangkat Menjadi Pegawai'
			WHEN PGW.KD_TIDAK_AKTIF = '10' THEN 'Kembali ke Lembaga Asal'
		END AS STS_TIDAK_AKTIF,SPG.NO_KTP,PGW.NO_REKENING,PGW.NM_REKENING,PGW.KD_BANK,BNK.BANK,SPG.NO_KARTU_KELUARGA,PGW.KD_KANTOR,PGW.NM_KANTOR,KOT.NM_KOTA AS NM_KANTOR_KOTA,
		PGW.KD_GOL,PGW.NM_GOL,PGW.KD_JABATAN_STR,
		JBT.NM_JABATAN_STR,JBT.NM_JABATAN_STR_SINGKAT,JBT.JENIS,
		PGW.NM_UNIT_ES1,PGW.NM_UNIT_ES2,PGW.NM_UNIT_ES3,PGW.NM_UNIT_ES4,PGW.NM_UNIT_ES5,PGW.NM_UNIT_ES6,PGW.NM_UNIT_ES7,
		PGW.TMT_OJK,PGW.TMT_LEMBAGA_ASAL,PGW.LEMBAGA_ASAL AS KD_LEMBAGA_ASAL,
		CASE
			WHEN PGW.LEMBAGA_ASAL = '1' THEN 'BI'
			WHEN PGW.LEMBAGA_ASAL = '2' THEN 'POLRI'
			WHEN PGW.LEMBAGA_ASAL = '3' THEN 'BPK'
			WHEN PGW.LEMBAGA_ASAL = '4' THEN 'BPKP'
			WHEN PGW.LEMBAGA_ASAL = '5' THEN 'KEJAKSAAN'
			WHEN PGW.LEMBAGA_ASAL = '6' THEN 'KEMENTERIAN KEUANGAN'
			WHEN PGW.LEMBAGA_ASAL = '7' THEN 'BAPEPAM-LK'
			WHEN PGW.LEMBAGA_ASAL = '8' THEN 'BANK MANDIRI'
		END AS KET_LEMBAGA_ASAL,
		PGW.TMT_SATKER,PGW.TMT_PENEMPATAN,PGW.TMT_GOL,PGW.TMT_JABATAN,
		CASE
			WHEN PGW.STS_PENSIUN = '0' THEN HRIS.dbo.MasaDinasTahunBulanDataMart(PGW.TMT_JABATAN,GETDATE()) 
			WHEN PGW.STS_PENSIUN = '1' THEN HRIS.dbo.MasaDinasTahunBulanDataMart(PGW.TMT_JABATAN,PGW.TGL_BERHENTI) 
		END AS MASA_DINAS_JABATAN,
		CASE
			WHEN PGW.STS_PENSIUN = '0' THEN 
				CASE 
					WHEN PGW.KD_STATUS_KEPEG = '200' THEN HRIS.dbo.MasaDinasTahunBulanDataMart(GETDATE(), PGW.TGL_SELESAI_KONTRAK) 
					ELSE HRIS.dbo.MasaDinasTahunBulanDataMart(GETDATE(), PGW.TMT_PENSIUN) 
				END 
			WHEN PGW.STS_PENSIUN = '1' THEN HRIS.dbo.MasaDinasTahunBulanDataMart(GETDATE(),PGW.TGL_BERHENTI) 
		END AS SISA_MASA_DINAS_JABATAN,PGW.MASA_TAHUN_OJK,PGW.MASA_BULAN_OJK,
		PGW.TMT_PANGKAT,PGW.TGL_MASUK,PGW.TGL_BERHENTI,PGW.TMT_PENSIUN,PGW.TGL_MULAI_KONTRAK,PGW.TGL_SELESAI_KONTRAK,SPG.TMT_PENETAPAN,
		PGW.KD_PEND_UMUM,PGW.NM_PEND_UMUM,PGW.NM_LEMB_PEND_FORMAL,PDK.JURUSAN,PDK.FAKULTAS,PDK.NILAI,PDK.NEGARA,
		CASE
			WHEN PDK.LOKASI_PEND = '1' THEN 'Luar Negeri'
			WHEN PDK.LOKASI_PEND = '2' THEN 'Dalam Negeri'
		END AS LOKASI_PEND,
		CASE
			WHEN PDK.STS_LULUS = '1' THEN 'Lulus'
			WHEN PDK.STS_LULUS = '2' THEN 'Tidak Lulus'
			WHEN PDK.STS_LULUS = '3' THEN 'Permufakatan'
		END AS STATUS_LULUS,
		PGW.TGL_IJAZAH,
		PFM1.PFM_ID AS PFM_ID1,PFM1.TANGGAL_AWAL AS TANGGAL_AWAL1,PFM1.TANGGAL_AKHIR AS TANGGAL_AKHIR1,PFM1.NILAI AS NILAI1,PFM1.LEVEL_KINERJA AS LEVEL_KINERJA1,PFM1.RANGKING AS RANGKING1,
		PFM2.PFM_ID AS PFM_ID2,PFM2.TANGGAL_AWAL AS TANGGAL_AWAL2,PFM2.TANGGAL_AKHIR AS TANGGAL_AKHIR2,PFM2.NILAI AS NILAI2,PFM2.LEVEL_KINERJA AS LEVEL_KINERJA2,PFM2.RANGKING AS RANGKING2,
		PFM3.PFM_ID AS PFM_ID3,PFM3.TANGGAL_AWAL AS TANGGAL_AWAL3,PFM3.TANGGAL_AKHIR AS TANGGAL_AKHIR3,PFM3.NILAI AS NILAI3,PFM3.LEVEL_KINERJA AS LEVEL_KINERJA3,PFM3.RANGKING AS RANGKING3,
		ISNULL(SRHD.HUKUMAN,'Tidak') AS HUKUMAN,SRHD.TGL_MULAI_HUKUMAN,SRHD.TGL_AKHIR_HUKUMAN,SRHD.KET_MASALAH,SRHD.JNS_TINDAKAN_ADM_LAIN,SRHD.KD_JNS_SANKSI,SRHD.JENIS_SANKSI,SRHD.KD_TINGKAT_SANKSI,SRHD.TINGKAT_SANKSI,
		KTF.KATEGORI_FUNGSI,KTF.NAMA_FUNGSI,KTF.POSISI,KTF.POSISI_SINGKAT,
		ISNULL(CLT.CUTI_CLTO,'Tidak') AS CUTI_CLTO,CLT.TGL_AWAL_CUTI_PEG_CLTO,CLT.TGL_AKHIR_CUTI_PEG_CLTO,
		ISNULL(CIK.CUTI_KEAGAMAAN,'Tidak') AS CUTI_KEAGAMAAN,CIK.TGL_AWAL_CUTI_PEG_KEAG,CIK.TGL_AKHIR_CUTI_PEG_KEAG,
		ISNULL(PTB.PEGAWAI_TUGAS_BELAJAR,'Tidak') AS PEGAWAI_TUGAS_BELAJAR,PTB.NM_PEND_PTB,PTB.NM_UNIVERSITAS_PTB,PTB.NM_FAKULTAS_PTB,PTB.NM_JURUSAN_PTB,PTB.TGL_MULAI_PTB,PTB.TGL_SELESAI_PTB,PTB.KOTA AS KOTA_PTB,
		RWJ.NM_STATUS_JABATAN,
		CASE
			WHEN RWJ.NM_STATUS_JABATAN = 'PLT' OR RWJ.NM_STATUS_JABATAN ='PLH' THEN 'Ya'
			ELSE 'Tidak'
		END KET_STATUS_JABATAN,RWJ.TANGGAL_END_PLT_PLH,RWJ.NM_JABATAN_PLT_PLH,RWJ.NM_UNIT_ES_PLT_PLH,
		CASE
			WHEN GETDATE() BETWEEN SPP.TGL_MULAI and SPP.TGL_SELESAI  THEN 'Ya' 
			ELSE 'Tidak' 
		END AS PESERTA_SURAT_TUGAS,SPP.STU_ID,SPP.KSO_KASUSNOMOR,SPP.NO_SURAT AS NO_SURAT_TUGAS,SPP.TGL_MULAI AS TGL_MULAI_SURAT_TUGAS,SPP.TGL_SELESAI AS TGL_SELESAI_SURAT_TUGAS,SPP.KETERANGAN AS KETERANGAN_SURAT_TUGAS,SPP.LOKASI AS LOKASI_SURAT_TUGAS,SPP.JENIS_SURAT_TUGAS, ORG.nama_satker_organisasi AS SATUAN_KERJA,
		CASE DATEPART(MONTH, GETDATE())
			WHEN 1 THEN 'JANUARI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 2 THEN 'FEBRUARI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 3 THEN 'MARET' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 4 THEN 'APRIL' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 5 THEN 'MEI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 6 THEN 'JUNI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 7 THEN 'JULI' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 8 THEN 'AGUSTUS' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 9 THEN 'SEPTEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 10 THEN 'OKTOBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 11 THEN 'NOVEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
			WHEN 12 THEN 'DESEMBER' + ' ' + CONVERT(varchar(10), YEAR(GETDATE()))
		  END AS BULAN_TAHUN, 
		  CASE DATEPART(MONTH, GETDATE())
			WHEN 1 THEN 'JANUARI'
			WHEN 2 THEN 'FEBRUARI'
			WHEN 3 THEN 'MARET'
			WHEN 4 THEN 'APRIL'
			WHEN 5 THEN 'MEI'
			WHEN 6 THEN 'JUNI'
			WHEN 7 THEN 'JULI'
			WHEN 8 THEN 'AGUSTUS'
			WHEN 9 THEN 'SEPTEMBER'
			WHEN 10 THEN 'OKTOBER'
			WHEN 11 THEN 'NOVEMBER'
			WHEN 12 THEN 'DESEMBER'
		END AS BULAN,
		CONVERT(varchar(10), CONVERT(varchar(10), YEAR(GETDATE()))) AS TAHUN
		FROM HRIS.dbo.SPG_DATA_CURRENT (NOLOCK) PGW 
			LEFT JOIN HRIS.dbo.SPG_PEGAWAI (NOLOCK) SPG ON PGW.NIP = SPG.NIP
			LEFT JOIN HRIS.dbo.SPG_JNS_KELAMIN (NOLOCK) JKL ON PGW.JNS_KELAMIN_PEG = JKL.JNS_KELAMIN_PEG
			LEFT JOIN HRIS.dbo.SPG_STS_KAWIN (NOLOCK) KWN ON PGW.STS_PERKAWINAN = KWN.KD_STS_KAWIN 
			LEFT JOIN HRIS.dbo.SPG_PROPINSI (NOLOCK) PRO ON PGW.KD_PROPINSI = PRO.KD_PROPINSI
			LEFT JOIN HRIS.dbo.SPG_KABUPATEN (NOLOCK) KAB ON PGW.KD_PROPINSI = KAB.KD_PROPINSI AND PGW.KD_KABUPATEN = KAB.KD_KABUPATEN 
			LEFT JOIN HRIS.dbo.SPG_KECAMATAN (NOLOCK) KEC ON PGW.KD_PROPINSI = KEC.KD_PROPINSI AND PGW.KD_KABUPATEN = KEC.KD_KABUPATEN AND PGW.KD_KECAMATAN = KEC.KD_KECAMATAN 
			LEFT JOIN HRIS.dbo.SPG_KELURAHAN (NOLOCK) KEL ON PGW.KD_PROPINSI = KEL.KD_PROPINSI AND PGW.KD_KABUPATEN = KEL.KD_KABUPATEN AND PGW.KD_KECAMATAN = KEL.KD_KECAMATAN AND PGW.KD_KELURAHAN = KEL.KD_KELURAHAN
			LEFT JOIN HRIS.dbo.PAY_BANK (NOLOCK) BNK ON PGW.KD_BANK = BNK.BANK_ID
			LEFT JOIN HRIS.dbo.SPG_08_JABATAN_STR (NOLOCK) JBT ON PGW.KD_JABATAN_STR = JBT.KD_JABATAN_STR
			LEFT JOIN HRIS.dbo.SPG_PROPINSI (NOLOCK) PROL ON SPG.KD_PROPINSI_HOMEBASE = PROL.KD_PROPINSI
			LEFT JOIN HRIS.dbo.SPG_KABUPATEN (NOLOCK) KABL ON SPG.KD_PROPINSI_HOMEBASE = KABL.KD_PROPINSI AND SPG.KD_KABUPATEN_HOMEBASE = KABL.KD_KABUPATEN 
			LEFT JOIN HRIS.dbo.SPG_PROPINSI (NOLOCK) PROR ON SPG.KD_PROPINSI_HOMEBASE2 = PROR.KD_PROPINSI
			LEFT JOIN HRIS.dbo.SPG_KABUPATEN (NOLOCK) KABR ON SPG.KD_PROPINSI_HOMEBASE2 = KABR.KD_PROPINSI AND SPG.KD_KABUPATEN_HOMEBASE2 = KABR.KD_KABUPATEN
			LEFT JOIN HRIS.dbo.MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 (NOLOCK) ORG ON PGW.KD_UNIT_ORG = ORG.KD_UNIT_ORG 
			LEFT JOIN HRIS.dbo.SPG_08_KANTOR (NOLOCK) KTR ON  PGW.KD_KANTOR = KTR.KD_KANTOR
			LEFT JOIN HRIS.dbo.SPG_KOTA (NOLOCK) KOT ON KTR.KD_KOTA = KOT.ID
			OUTER APPLY
			(
				SELECT TOP 1
					*
				FROM
					HRIS.dbo.SPG_RIWAYAT_PEND_FORMAL (NOLOCK) AS SRPF
				WHERE
					SRPF.NIP = PGW.NIP
					AND PGW.KD_PEND_UMUM = SRPF.KD_PEND_UMUM 
					AND SRPF.KD_STATUS_VALIDASI='1'
				ORDER BY
					SRPF.NO_URUT_PEND_FORMAL DESC
			) AS PDK
			OUTER APPLY(
				SELECT PFMRK.PFM_ID,PFMRK.NIP,PFMRK.NILAI,PFMRK.RANGKING,PFMRK.LEVEL_KINERJA,PFMPR.TANGGAL_AWAL,PFMPR.TANGGAL_AKHIR 
					FROM HRIS.dbo.PFM_RIWAYAT_KINERJA (NOLOCK) AS PFMRK
						INNER JOIN HRIS.dbo.PFM_PERIODE (NOLOCK) PFMPR ON PFMRK.PFM_ID = PFMPR.PFM_ID
						WHERE 
						PFMRK.NIP = PGW.NIP
						AND PFMRK.PFM_ID = (
							SELECT  TOP  1  lag(PFM_ID, 2,0) OVER (ORDER BY PFM_ID)  
								FROM HRIS.dbo.PFM_PERIODE (NOLOCK) 
							ORDER BY PFM_ID DESC
						)
			) AS PFM1
			OUTER APPLY(
				SELECT PFMRK.PFM_ID,PFMRK.NIP,PFMRK.NILAI,PFMRK.RANGKING,PFMRK.LEVEL_KINERJA,PFMPR.TANGGAL_AWAL,PFMPR.TANGGAL_AKHIR 
					FROM HRIS.dbo.PFM_RIWAYAT_KINERJA (NOLOCK) AS PFMRK
						INNER JOIN HRIS.dbo.PFM_PERIODE (NOLOCK) PFMPR ON PFMRK.PFM_ID = PFMPR.PFM_ID
						WHERE 
						PFMRK.NIP = PGW.NIP
						AND PFMRK.PFM_ID = (
							SELECT  TOP  1  lag(PFM_ID, 1,0) OVER (ORDER BY PFM_ID)  
								FROM HRIS.dbo.PFM_PERIODE (NOLOCK) 
							ORDER BY PFM_ID DESC
						)
			) AS PFM2
			OUTER APPLY
			(
				SELECT PFMRK.PFM_ID,PFMRK.NIP,PFMRK.NILAI,PFMRK.RANGKING,PFMRK.LEVEL_KINERJA,PFMPR.TANGGAL_AWAL,PFMPR.TANGGAL_AKHIR 
					FROM HRIS.dbo.PFM_RIWAYAT_KINERJA (NOLOCK) AS PFMRK
						INNER JOIN HRIS.dbo.PFM_PERIODE PFMPR (NOLOCK) ON PFMRK.PFM_ID = PFMPR.PFM_ID
						WHERE 
						PFMRK.NIP = PGW.NIP
						AND PFMRK.PFM_ID = (
							SELECT MAX(PFM_ID) From HRIS.dbo.PFM_PERIODE (NOLOCK) where STATUS ='1'
						)
			) AS PFM3
			OUTER APPLY
			(
				SELECT TOP 1
					KET_MASALAH,JNS_TINDAKAN_ADM_LAIN,
					CASE
						WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_MULAI_HUKUMAN 
						ELSE NULL 
					END TGL_MULAI_HUKUMAN,
					CASE
						WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN TGL_AKHIR_HUKUMAN 
						ELSE NULL 
					END TGL_AKHIR_HUKUMAN,
					CASE
						WHEN GETDATE() BETWEEN TGL_MULAI_HUKUMAN and TGL_AKHIR_HUKUMAN  THEN 'Ya' 
						ELSE 'Tidak' 
					END HUKUMAN,KD_JNS_SANKSI,
				CASE
					WHEN KD_JNS_SANKSI = '1' THEN 'Sanksi Administrasi'
					WHEN KD_JNS_SANKSI = '2' THEN 'Sanksi Financial'
				END AS JENIS_SANKSI,KD_TINGKAT_SANKSI,
				CASE
					WHEN SR.KD_TINGKAT_SANKSI = '1' THEN 'Sanksi Ringan'
					WHEN SR.KD_TINGKAT_SANKSI = '2' THEN 'Sanksi Sedang'
					WHEN SR.KD_TINGKAT_SANKSI = '3' THEN 'Sanksi Berat'
					WHEN SR.KD_TINGKAT_SANKSI = '4' THEN 'Sanksi Financial'
				END AS TINGKAT_SANKSI
				FROM HRIS.dbo.SPG_RIWAYAT_HUKUMAN_DISIPLIN (NOLOCK) AS SR
				WHERE
					SR.NIP = PGW.NIP
					AND SR.KD_STATUS_VALIDASI='1'
				ORDER BY
					SR.TGL_AKHIR_HUKUMAN DESC
			) AS SRHD
			OUTER APPLY
			(
				SELECT TOP 1
					( CASE c.kategori_fungsi 
						WHEN '1' THEN 'AIMRPK' 
						WHEN '2' THEN 'EPK' 
						WHEN '3' THEN 'Kepala OJK Regional dan Kepala OJK' 
						WHEN '4' THEN 'Mitra Strategis' 
						WHEN '5' THEN 'Pengaturan' 
						WHEN '6' THEN 'Pengawasan'
						WHEN '7' THEN 'Penyidikan' 
						WHEN '8' THEN 'Perizinan'
						WHEN '9' THEN 'Riset' 
						WHEN '10' THEN 'Sekretariat Lembaga dan Hubungan Eksternal' 
						WHEN '11' THEN 'Sekretariat Lembaga dan Hubungan Internal' 
						ELSE 'Ex-Officio' END) AS NAMA_FUNGSI, c.KATEGORI_FUNGSI AS KATEGORI_FUNGSI,c.POSISI,c.POSISI_SINGKAT
				FROM					HRIS.dbo.SPG_DATA_CURRENT (NOLOCK) AS a LEFT OUTER JOIN
										HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS b ON a.KD_UNIT_ORG = b.KD_UNIT_ORG LEFT OUTER JOIN
										HRIS.dbo.SPG_08_JABATAN_UNIT (NOLOCK) AS c ON a.KD_JABATAN_STR = c.KD_JABATAN AND a.KD_UNIT_ORG = c.KD_UNIT_ORG
				WHERE PGW.NIP = a.NIP AND PGW.KD_UNIT_ORG = b.KD_UNIT_ORG AND PGW.KD_JABATAN_STR = c.KD_JABATAN 
				AND (ISNULL(a.STS_PENSIUN, '0') = '0') AND (ISNULL(a.KD_KANTOR, '0') <> '0')
				GROUP BY C.ID,
					(CASE c.kategori_fungsi 
						WHEN '1' THEN 'AIMRPK' 
						WHEN '2' THEN 'EPK' 
						WHEN '3' THEN 'Kepala OJK Regional dan Kepala OJK' 
						WHEN '4' THEN 'Mitra Strategis' 
						WHEN '5' THEN 'Pengaturan' 
						WHEN '6' THEN 'Pengawasan'
						WHEN '7' THEN 'Penyidikan' 
						WHEN '8' THEN 'Perizinan' 
						WHEN '9' THEN 'Riset' 
						WHEN '10' THEN 'Sekretariat Lembaga dan Hubungan Eksternal' 
						WHEN '11' THEN 'Sekretariat Lembaga dan Hubungan Internal' 
						ELSE 'Ex-Officio' END), c.KATEGORI_FUNGSI,c.POSISI,c.POSISI_SINGKAT
				ORDER BY C.ID DESC
			) AS KTF
			OUTER APPLY
			(
				SELECT TOP 1
					--TGL_AWAL_CUTI_PEG AS TGL_AWAL_CUTI_PEG_CLTO,
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG  THEN TGL_AWAL_CUTI_PEG 
						ELSE NULL 
					END TGL_AWAL_CUTI_PEG_CLTO,
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG  THEN TGL_AKHIR_CUTI_PEG 
						ELSE NULL 
					END TGL_AKHIR_CUTI_PEG_CLTO,
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG THEN 'Ya' 						
						ELSE 'Tidak' 
					END CUTI_CLTO
				FROM HRIS.dbo.SPG_CUTI_PEGAWAI (NOLOCK) AS SPGC
				WHERE PGW.NIP = SPGC.NIP
				AND JNS_CUTI = '16'
			) AS CLT
			OUTER APPLY
			(
				SELECT TOP 1
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG  THEN TGL_AWAL_CUTI_PEG
						ELSE NULL
					END TGL_AWAL_CUTI_PEG_KEAG,
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG  THEN TGL_AKHIR_CUTI_PEG
						ELSE NULL
					END TGL_AKHIR_CUTI_PEG_KEAG,
					CASE
						WHEN GETDATE() BETWEEN TGL_AWAL_CUTI_PEG and TGL_AKHIR_CUTI_PEG  THEN 'Ya'
						ELSE 'Tidak' 
					END CUTI_KEAGAMAAN
				FROM HRIS.dbo.SPG_CUTI_PEGAWAI (NOLOCK) AS SPGC
				WHERE PGW.NIP = SPGC.NIP
				AND JNS_CUTI = '17'
			) AS CIK
			OUTER APPLY
			(
			SELECT 
				CASE
					WHEN KD_PEND_UMUM = '43' THEN 'D-III'
					WHEN KD_PEND_UMUM = '44' THEN 'D-IV'
					WHEN KD_PEND_UMUM = '50' THEN 'S1'
					WHEN KD_PEND_UMUM = '60' THEN 'S2'
					WHEN KD_PEND_UMUM = '70' THEN 'S3'
				END AS NM_PEND_PTB,SPTB.NM_UNIVERSITAS AS NM_UNIVERSITAS_PTB,SPTB.NM_FAKULTAS AS NM_FAKULTAS_PTB,SPTB.NM_JURUSAN AS NM_JURUSAN_PTB,
				SPTB.TGL_MULAI AS TGL_MULAI_PTB,SPTB.TGL_SELESAI AS TGL_SELESAI_PTB,
					CASE
						WHEN GETDATE() BETWEEN SPTB.TGL_MULAI and SPTB.TGL_SELESAI THEN 'Ya'
						ELSE 'Tidak'
					END PEGAWAI_TUGAS_BELAJAR,SPTB.KOTA
				FROM HRIS.dbo.SPG_TUGAS_BELAJAR_DEFINITIF (NOLOCK) AS SPTB
				WHERE PGW.NIP = SPTB.NIP
			) AS PTB
			OUTER APPLY
			(
				SELECT 
					sdc.NIP,
					sdc.NM_PEG,
					-- jabatan PLT/PLH saat ini
					ssj.NM_STATUS_JABATAN,
					srj2.NM_JABATAN as NM_JABATAN_PLT_PLH,
					COALESCE('' + NULLIF(plth_es1.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es2.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es3.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es4.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es5.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es6.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es7.NM_UNIT_ORG, ''), '') + 
					COALESCE(' > ' + NULLIF(plth_es8.NM_UNIT_ORG, ''), '') AS NM_UNIT_ES_PLT_PLH,
					srj2.TANGGAL_END AS TANGGAL_END_PLT_PLH
				FROM 
					HRIS.dbo.SPG_DATA_CURRENT (NOLOCK) AS sdc
					CROSS APPLY
					(
						SELECT TOP 1
							*
						FROM
							HRIS.dbo.SPG_08_RIWAYAT_JABATAN (NOLOCK) AS tmp_srj
						WHERE
							tmp_srj.STATUS_JABATAN IN ('0', '1')	-- definitif
							AND tmp_srj.NIP = sdc.NIP
						ORDER BY
							tmp_srj.NO_URUT_JAB DESC
					) AS srj	-- jabatan tetap, saat ini
					CROSS APPLY--OUTER APPLY
					(
						SELECT
							*
						FROM
							HRIS.dbo.SPG_08_RIWAYAT_JABATAN (NOLOCK) AS tmp_srj2
						WHERE
							tmp_srj2.STATUS_JABATAN IN ('4', '5')	-- PLT, PLH
							AND tmp_srj2.NIP = sdc.NIP
							AND tmp_srj2.TANGGAL_END >= CAST(GETDATE() AS DATE)	-- yang hari ini masih aktif
					) AS srj2
					INNER JOIN
					HRIS.dbo.SPG_STATUS_JABATAN (NOLOCK) AS ssj ON srj2.STATUS_JABATAN = ssj.STATUS_JABATAN	-- master jabatan
					INNER JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS suo ON srj.KD_UNIT_ORG = suo.KD_UNIT_ORG	-- unit org jabatan definitif
					INNER JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS suo2 ON srj2.KD_UNIT_ORG = suo2.KD_UNIT_ORG	-- unit org jabatan PLT/PLH

					-- dibawah ini join-join untuk mendapatkan definisi unit organisasi jabatan definitif saat ini
					LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es1 ON suo.KD_UNIT_ES1 = es1.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es2 ON suo.KD_UNIT_ES2 = es2.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es3 ON suo.KD_UNIT_ES3 = es3.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es4 ON suo.KD_UNIT_ES4 = es4.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es5 ON suo.KD_UNIT_ES5 = es5.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es6 ON suo.KD_UNIT_ES6 = es6.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS es7 ON suo.KD_UNIT_ES7 = es7.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK)  es8 ON suo.KD_UNIT_ES8 = es8.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_ESELON (NOLOCK) AS esl ON srj.KD_ESELON = esl.KD_ESELON

					-- dibawah ini join-join untuk mendapatkan definisi unit organisasi jabatan PLT/PLH saat ini
					LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es1 ON suo2.KD_UNIT_ES1 = plth_es1.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es2 ON suo2.KD_UNIT_ES2 = plth_es2.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es3 ON suo2.KD_UNIT_ES3 = plth_es3.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es4 ON suo2.KD_UNIT_ES4 = plth_es4.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es5 ON suo2.KD_UNIT_ES5 = plth_es5.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es6 ON suo2.KD_UNIT_ES6 = plth_es6.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es7 ON suo2.KD_UNIT_ES7 = plth_es7.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_08_UNIT_ORGANISASI (NOLOCK) AS plth_es8 ON suo2.KD_UNIT_ES8 = plth_es8.KD_UNIT_ORG LEFT JOIN
					HRIS.dbo.SPG_ESELON (NOLOCK) AS plth_esl ON srj2.KD_ESELON = plth_esl.KD_ESELON
				WHERE sdc.NIP = PGW.NIP 
			) AS RWJ
			OUTER APPLY
			(
			SELECT SPR.STU_ID,NIP,SPR.KSO_KASUSNOMOR,SPR.NO_SURAT,TGL_MULAI,z.TGL_SELESAI,SPK.KETERANGAN,SPR.RENTANG_TANGGAL,SPR.LOKASI,SPR.JENIS_SURAT_TUGAS 
				FROM (SELECT NIP,TGL_MULAI,TGL_SELESAI FROM (
					SELECT x.NIP,
						(SELECT MIN(TGL_MULAI) AS TGL_MULAI FROM HRIS.dbo.SPG_SURAT_TUGAS a 
							inner join HRIS.dbo.SPG_PESERTA_SURAT_TUGAS b on a.STU_ID=b.STU_ID
							inner join HRIS.dbo.SPG_JNS_SURAT_TUGAS c on c.JENIS_ST=b.TYPE
							WHERE b.RENTANG_TANGGAL<>'' and b.STATUS=1 AND x.NIP = b.NIP
							GROUP BY b.NIP) as TGL_MULAI,
						(SELECT MAX(TGL_SELESAI) AS TGL_SELESAI FROM HRIS.dbo.SPG_SURAT_TUGAS a 
							inner join HRIS.dbo.SPG_PESERTA_SURAT_TUGAS b on a.STU_ID=b.STU_ID
							inner join HRIS.dbo.SPG_JNS_SURAT_TUGAS c on c.JENIS_ST=b.TYPE
							WHERE b.RENTANG_TANGGAL<>'' and b.STATUS=1 AND x.NIP = b.NIP
							GROUP BY b.NIP) as TGL_SELESAI FROM HRIS.dbo.SPG_PESERTA_SURAT_TUGAS x
						GROUP BY X.NIP ) AS t
					GROUP BY NIP,TGL_MULAI,TGL_SELESAI) AS z
					OUTER APPLY
					(
					SELECT top 1 MAX(b.TGL_SELESAI) AS TGL_SELESAI,CAST(a.KETERANGAN AS NVARCHAR(4000)) AS KETERANGAN FROM HRIS.dbo.SPG_SURAT_TUGAS a 
						inner join HRIS.dbo.SPG_PESERTA_SURAT_TUGAS b on a.STU_ID=b.STU_ID
						inner join HRIS.dbo.SPG_JNS_SURAT_TUGAS c on c.JENIS_ST=b.TYPE
						WHERE b.RENTANG_TANGGAL<>'' and b.STATUS=1 AND z.NIP = b.NIP
						GROUP BY CAST(a.KETERANGAN AS NVARCHAR(4000))
					ORDER BY TGL_SELESAI DESC
					) AS SPK
					OUTER APPLY
					(
					SELECT top 1 a.STU_ID,a.KSO_KASUSNOMOR,a.NO_SURAT,MAX(b.TGL_SELESAI) AS TGL_SELESAI,b.RENTANG_TANGGAL,a.LOKASI,c.KETERANGAN AS JENIS_SURAT_TUGAS FROM HRIS.dbo.SPG_SURAT_TUGAS a 
						inner join HRIS.dbo.SPG_PESERTA_SURAT_TUGAS b on a.STU_ID=b.STU_ID
						inner join HRIS.dbo.SPG_JNS_SURAT_TUGAS c on c.JENIS_ST=b.TYPE
						WHERE b.RENTANG_TANGGAL<>'' and b.STATUS=1 AND z.NIP = b.NIP
						GROUP BY a.STU_ID,a.KSO_KASUSNOMOR,a.NO_SURAT,b.RENTANG_TANGGAL,a.LOKASI,c.KETERANGAN
					ORDER BY TGL_SELESAI DESC
					) AS SPR
			WHERE z.NIP = PGW.NIP 
			) AS SPP
ORDER BY PGW.NIP ASC;
END

GO
/****** Object:  StoredProcedure [db_mart].[usp_insert_data_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author		: Reza Andrian
-- Create date	: 02 Feb 2020
-- Description	: Insert Data Mart Profile Pegawai 
-- =============================================
CREATE PROCEDURE [db_mart].[usp_insert_data_pegawai]
	
AS

BEGIN TRAN

	BEGIN TRY
		DECLARE @temp_table TABLE (
				   NIP varchar(32),
				   NM_PEG varchar(100),
				   NIP_LAMA varchar(50),
				   NM_STATUS_KEPEG varchar(200),
				   KD_AGAMA char(1),
				   NM_AGAMA varchar(50),
				   TGL_LAHIR_PEG date,
				   USIA_SAAT_INI varchar(20),
				   GEN varchar(20),
				   TEMPAT_LAHIR_PEG varchar(200),
				   JNS_KELAMIN_PEG char(1),
				   NM_KELAMIN_PEG varchar(50),
				   STS_PERKAWINAN char(1),
				   KET_STS_KAWIN varchar(100),
				   STS_SIPIL char(1),
				   KET_STS_SIPIL varchar(100),
				   NIP_PASANGAN varchar(20),
				   EMAIL varchar(255),
				   EMAIL_2 varchar(255),
				   KD_PROPINSI char(10),
				   NM_PROPINSI varchar(255),
				   KD_KABUPATEN char(10),
				   NM_KABUPATEN varchar(255),
				   KD_KECAMATAN char(10),
				   NM_KECAMATAN varchar(255),
				   KD_KELURAHAN char(10),
				   NM_KELURAHAN varchar(255),
				   JL_TMP_TINGGAL_CUR varchar(500),
				   KD_PROPINSI_HOMEBASE varchar(10),
				   NM_PROPINSI_HOMEBASE varchar(255),
				   KD_KABUPATEN_HOMEBASE varchar(10),
				   NM_KABUPATEN_HOMEBASE varchar(255),
				   KD_PROPINSI_HOMEBASE_AWAL varchar(10),
				   NM_PROPINSI_HOMEBASE_AWAL varchar(255),
				   KD_KABUPATEN_HOMEBASE_AWAL varchar(10),
				   NM_KABUPATEN_HOMEBASE_AWAL varchar(255),
				   KD_PROPINSI_HOMEBASE_AKHIR varchar(10),
				   NM_PROPINSI_HOMEBASE_AKHIR varchar(255),
				   KD_KABUPATEN_HOMEBASE_AKHIR varchar(10),
				   NM_KABUPATEN_HOMEBASE_AKHIR varchar(255),
				   NPWP varchar(30),
				   PHONE_KANTOR varchar(20),
				   PHONE_KANTOR_EXT varchar(50),
				   HANDPHONE_1 varchar(300),
				   HANDPHONE_2 varchar(300),
				   KD_STS_PENERIMAAN_PEG varchar(3),
				   STS_PENERIMAAN_PEG varchar(100),
				   ANGKATAN varchar(3),
				   TAHUN_PEREKRUTAN varchar(4),
				   STS_PENSIUN char(1),
				   KD_TIDAK_AKTIF varchar(5),
				   STS_TIDAK_AKTIF varchar(100),
				   NO_KTP varchar(50),
				   NO_REKENING varchar(200),
				   NM_REKENING varchar(100),
				   KD_BANK varchar(50),
				   BANK varchar(100),
				   NO_KARTU_KELUARGA varchar(50),
				   KD_KANTOR varchar(50),
				   NM_KANTOR varchar(200),
				   NM_KANTOR_KOTA varchar(255),
				   KD_GOL varchar(5),
				   NM_GOL varchar(30),
				   KD_JABATAN_STR varchar(3),
				   NM_JABATAN_STR varchar(110),
				   NM_JABATAN_STR_SINGKAT varchar(225),
				   JENIS varchar(32),
				   NM_UNIT_ES1 varchar(500),
				   NM_UNIT_ES2 varchar(500),
				   NM_UNIT_ES3 varchar(500),
				   NM_UNIT_ES4 varchar(500),
				   NM_UNIT_ES5 varchar(200),
				   NM_UNIT_ES6 varchar(200),
				   NM_UNIT_ES7 varchar(200),
				   TMT_OJK date,
				   TMT_LEMBAGA_ASAL date,
				   KD_LEMBAGA_ASAL varchar(50),
				   KET_LEMBAGA_ASAL varchar(255),
				   TMT_SATKER date,
				   TMT_PENEMPATAN date,
				   TMT_GOL date,
				   TMT_JABATAN date,
				   MASA_DINAS_JABATAN varchar(50),
				   SISA_MASA_DINAS_JABATAN varchar(50),
				   MASA_TAHUN_OJK int,
				   MASA_BULAN_OJK int,
				   TMT_PANGKAT date,
				   TGL_MASUK date,
				   TGL_BERHENTI date,
				   TMT_PENSIUN date,
				   TGL_MULAI_KONTRAK date,
				   TGL_SELESAI_KONTRAK date,
				   TMT_PENETAPAN date,
				   KD_PEND_UMUM varchar(10),
				   NM_PEND_UMUM varchar(200),
				   NM_LEMB_PEND_FORMAL varchar(200),
				   JURUSAN varchar(200),
				   FAKULTAS varchar(200),
				   NILAI varchar(100),
				   NEGARA varchar(200),
				   LOKASI_PEND varchar(50),
				   STATUS_LULUS varchar(50),
				   TGL_IJAZAH date,
				   PFM_ID1 varchar(10),
				   TANGGAL_AWAL1 date,
				   TANGGAL_AKHIR1 date,
				   NILAI1 float,
				   LEVEL_KINERJA1 int,
				   RANGKING1 int,
				   PFM_ID2 varchar(10),
				   TANGGAL_AWAL2 date,
				   TANGGAL_AKHIR2 date,
				   NILAI2 float,
				   LEVEL_KINERJA2 int,
				   RANGKING2 int,
				   PFM_ID3 varchar(10),
				   TANGGAL_AWAL3 date,
				   TANGGAL_AKHIR3 date,
				   NILAI3 float,
				   LEVEL_KINERJA3 int,
				   RANGKING3 int,
				   HUKUMAN varchar(50),
				   TGL_MULAI_HUKUMAN date,
				   TGL_AKHIR_HUKUMAN date,
				   KET_MASALAH varchar(2000),
				   JNS_TINDAKAN_ADM_LAIN varchar(2000),
				   KD_JNS_SANKSI int,
				   JENIS_SANKSI varchar(50),
				   KD_TINGKAT_SANKSI char(3),
				   TINGKAT_SANKSI varchar(50),
				   KATEGORI_FUNGSI char(2),
				   NAMA_FUNGSI varchar(100),
				   POSISI varchar(255),
				   POSISI_SINGKAT varchar(255),
				   CUTI_CLTO varchar(10),
				   TGL_AWAL_CUTI_PEG_CLTO date,
				   TGL_AKHIR_CUTI_PEG_CLTO date,
				   CUTI_KEAGAMAAN varchar(10),
				   TGL_AWAL_CUTI_PEG_KEAG date,
				   TGL_AKHIR_CUTI_PEG_KEAG date,
				   PEGAWAI_TUGAS_BELAJAR varchar(10),
				   NM_PEND_PTB varchar(10),
				   NM_UNIVERSITAS_PTB varchar(255),
				   NM_FAKULTAS_PTB varchar(255),
				   NM_JURUSAN_PTB varchar(255),
				   TGL_MULAI_PTB date,
				   TGL_SELESAI_PTB date,
				   KOTA_PTB varchar(255),
				   NM_STATUS_JABATAN varchar(15),
				   KET_STATUS_JABATAN varchar(10),
				   TANGGAL_END_PLT_PLH date,
				   NM_JABATAN_PLT_PLH varchar(100),
				   NM_UNIT_ES_PLT_PLH varchar(2000),
				   PESERTA_SURAT_TUGAS varchar(10),
				   STU_ID varchar(10),
				   KSO_KASUSNOMOR varchar(20),
				   NO_SURAT_TUGAS varchar (255),
				   TGL_MULAI_SURAT_TUGAS date,
				   TGL_SELESAI_SURAT_TUGAS date,
				   KETERANGAN_SURAT_TUGAS nvarchar(4000),
				   LOKASI_SURAT_TUGAS varchar(255),
				   JENIS_SURAT_TUGAS varchar(32),
				   SATUAN_KERJA varchar(255),
				   BULAN_TAHUN varchar(30),
				   BULAN varchar(20),
				   TAHUN int,
				   CREATE_DATE datetime,
				   CREATE_TIME datetime
				 )

		INSERT INTO @temp_table
				   ([NIP]
				   ,[NM_PEG]
				   ,[NIP_LAMA]
				   ,[NM_STATUS_KEPEG]
				   ,[KD_AGAMA]
				   ,[NM_AGAMA]
				   ,[TGL_LAHIR_PEG]
				   ,[USIA_SAAT_INI]
				   ,[GEN]
				   ,[TEMPAT_LAHIR_PEG]
				   ,[JNS_KELAMIN_PEG]
				   ,[NM_KELAMIN_PEG]
				   ,[STS_PERKAWINAN]
				   ,[KET_STS_KAWIN]
				   ,[STS_SIPIL]
				   ,[KET_STS_SIPIL]
				   ,[NIP_PASANGAN]
				   ,[EMAIL]
				   ,[EMAIL_2]
				   ,[KD_PROPINSI]
				   ,[NM_PROPINSI]
				   ,[KD_KABUPATEN]
				   ,[NM_KABUPATEN]
				   ,[KD_KECAMATAN]
				   ,[NM_KECAMATAN]
				   ,[KD_KELURAHAN]
				   ,[NM_KELURAHAN]
				   ,[JL_TMP_TINGGAL_CUR]
				   ,[KD_PROPINSI_HOMEBASE]
				   ,[NM_PROPINSI_HOMEBASE]
				   ,[KD_KABUPATEN_HOMEBASE]
				   ,[NM_KABUPATEN_HOMEBASE]
				   ,[KD_PROPINSI_HOMEBASE_AWAL]
				   ,[NM_PROPINSI_HOMEBASE_AWAL]
				   ,[KD_KABUPATEN_HOMEBASE_AWAL]
				   ,[NM_KABUPATEN_HOMEBASE_AWAL]
				   ,[KD_PROPINSI_HOMEBASE_AKHIR]
				   ,[NM_PROPINSI_HOMEBASE_AKHIR]
				   ,[KD_KABUPATEN_HOMEBASE_AKHIR]
				   ,[NM_KABUPATEN_HOMEBASE_AKHIR]
				   ,[NPWP]
				   ,[PHONE_KANTOR]
				   ,[PHONE_KANTOR_EXT]
				   ,[HANDPHONE_1]
				   ,[HANDPHONE_2]
				   ,[KD_STS_PENERIMAAN_PEG]
				   ,[STS_PENERIMAAN_PEG]
				   ,[ANGKATAN]
				   ,[TAHUN_PEREKRUTAN]
				   ,[STS_PENSIUN]
				   ,[KD_TIDAK_AKTIF]
				   ,[STS_TIDAK_AKTIF]
				   ,[NO_KTP]
				   ,[NO_REKENING]
				   ,[NM_REKENING]
				   ,[KD_BANK]
				   ,[BANK]
				   ,[NO_KARTU_KELUARGA]
				   ,[KD_KANTOR]
				   ,[NM_KANTOR]
				   ,[NM_KANTOR_KOTA]
				   ,[KD_GOL]
				   ,[NM_GOL]
				   ,[KD_JABATAN_STR]
				   ,[NM_JABATAN_STR]
				   ,[NM_JABATAN_STR_SINGKAT]
				   ,[JENIS]
				   ,[NM_UNIT_ES1]
				   ,[NM_UNIT_ES2]
				   ,[NM_UNIT_ES3]
				   ,[NM_UNIT_ES4]
				   ,[NM_UNIT_ES5]
				   ,[NM_UNIT_ES6]
				   ,[NM_UNIT_ES7]
				   ,[TMT_OJK]
				   ,[TMT_LEMBAGA_ASAL]
				   ,[KD_LEMBAGA_ASAL]
				   ,[KET_LEMBAGA_ASAL]
				   ,[TMT_SATKER]
				   ,[TMT_PENEMPATAN]
				   ,[TMT_GOL]
				   ,[TMT_JABATAN]
				   ,[MASA_DINAS_JABATAN]
				   ,[SISA_MASA_DINAS_JABATAN]
				   ,[MASA_TAHUN_OJK]
				   ,[MASA_BULAN_OJK]
				   ,[TMT_PANGKAT]
				   ,[TGL_MASUK]
				   ,[TGL_BERHENTI]
				   ,[TMT_PENSIUN]
				   ,[TGL_MULAI_KONTRAK]
				   ,[TGL_SELESAI_KONTRAK]
				   ,[TMT_PENETAPAN]
				   ,[KD_PEND_UMUM]
				   ,[NM_PEND_UMUM]
				   ,[NM_LEMB_PEND_FORMAL]
				   ,[JURUSAN]
				   ,[FAKULTAS]
				   ,[NILAI]
				   ,[NEGARA]
				   ,[LOKASI_PEND]
				   ,[STATUS_LULUS]
				   ,[TGL_IJAZAH]
				   ,[PFM_ID1]
				   ,[TANGGAL_AWAL1]
				   ,[TANGGAL_AKHIR1]
				   ,[NILAI1]
				   ,[LEVEL_KINERJA1]
				   ,[RANGKING1]
				   ,[PFM_ID2]
				   ,[TANGGAL_AWAL2]
				   ,[TANGGAL_AKHIR2]
				   ,[NILAI2]
				   ,[LEVEL_KINERJA2]
				   ,[RANGKING2]
				   ,[PFM_ID3]
				   ,[TANGGAL_AWAL3]
				   ,[TANGGAL_AKHIR3]
				   ,[NILAI3]
				   ,[LEVEL_KINERJA3]
				   ,[RANGKING3]
				   ,[HUKUMAN]
				   ,[TGL_MULAI_HUKUMAN]
				   ,[TGL_AKHIR_HUKUMAN]
				   ,[KET_MASALAH]
				   ,[JNS_TINDAKAN_ADM_LAIN]
				   ,[KD_JNS_SANKSI]
				   ,[JENIS_SANKSI]
				   ,[KD_TINGKAT_SANKSI]
				   ,[TINGKAT_SANKSI]
				   ,[KATEGORI_FUNGSI]
				   ,[NAMA_FUNGSI]
				   ,[POSISI]
				   ,[POSISI_SINGKAT]
				   ,[CUTI_CLTO]
				   ,[TGL_AWAL_CUTI_PEG_CLTO]
				   ,[TGL_AKHIR_CUTI_PEG_CLTO]
				   ,[CUTI_KEAGAMAAN]
				   ,[TGL_AWAL_CUTI_PEG_KEAG]
				   ,[TGL_AKHIR_CUTI_PEG_KEAG]
				   ,[PEGAWAI_TUGAS_BELAJAR]
				   ,[NM_PEND_PTB]
				   ,[NM_UNIVERSITAS_PTB]
				   ,[NM_FAKULTAS_PTB]
				   ,[NM_JURUSAN_PTB]
				   ,[TGL_MULAI_PTB]
				   ,[TGL_SELESAI_PTB]
				   ,[KOTA_PTB]
				   ,[NM_STATUS_JABATAN]
				   ,[KET_STATUS_JABATAN]
				   ,[TANGGAL_END_PLT_PLH]
				   ,[NM_JABATAN_PLT_PLH]
				   ,[NM_UNIT_ES_PLT_PLH]
				   ,[PESERTA_SURAT_TUGAS]
				   ,[STU_ID]
				   ,[KSO_KASUSNOMOR]
				   ,[NO_SURAT_TUGAS]
				   ,[TGL_MULAI_SURAT_TUGAS]
				   ,[TGL_SELESAI_SURAT_TUGAS]
				   ,[KETERANGAN_SURAT_TUGAS]
				   ,[LOKASI_SURAT_TUGAS]
				   ,[JENIS_SURAT_TUGAS]
				   ,[SATUAN_KERJA]
				   ,[BULAN_TAHUN]
				   ,[BULAN]
				   ,[TAHUN])
		EXEC [db_mart].[usp_data_pegawai]


		UPDATE @temp_table SET CREATE_DATE = GETDATE(), CREATE_TIME = SYSDATETIME() 

		DELETE FROM [db_mart].[mart_profile_pegawai] WHERE CAST([CREATE_DATE] AS DATE) = CAST(GETDATE() AS DATE)

		INSERT INTO [db_mart].[mart_profile_pegawai]
				   ([NIP]
				   ,[NM_PEG]
				   ,[NIP_LAMA]
				   ,[NM_STATUS_KEPEG]
				   ,[KD_AGAMA]
				   ,[NM_AGAMA]
				   ,[TGL_LAHIR_PEG]
				   ,[USIA_SAAT_INI]
				   ,[GEN]
				   ,[TEMPAT_LAHIR_PEG]
				   ,[JNS_KELAMIN_PEG]
				   ,[NM_KELAMIN_PEG]
				   ,[STS_PERKAWINAN]
				   ,[KET_STS_KAWIN]
				   ,[STS_SIPIL]
				   ,[KET_STS_SIPIL]
				   ,[NIP_PASANGAN]
				   ,[EMAIL]
				   ,[EMAIL_2]
				   ,[KD_PROPINSI]
				   ,[NM_PROPINSI]
				   ,[KD_KABUPATEN]
				   ,[NM_KABUPATEN]
				   ,[KD_KECAMATAN]
				   ,[NM_KECAMATAN]
				   ,[KD_KELURAHAN]
				   ,[NM_KELURAHAN]
				   ,[JL_TMP_TINGGAL_CUR]
				   ,[KD_PROPINSI_HOMEBASE]
				   ,[NM_PROPINSI_HOMEBASE]
				   ,[KD_KABUPATEN_HOMEBASE]
				   ,[NM_KABUPATEN_HOMEBASE]
				   ,[KD_PROPINSI_HOMEBASE_AWAL]
				   ,[NM_PROPINSI_HOMEBASE_AWAL]
				   ,[KD_KABUPATEN_HOMEBASE_AWAL]
				   ,[NM_KABUPATEN_HOMEBASE_AWAL]
				   ,[KD_PROPINSI_HOMEBASE_AKHIR]
				   ,[NM_PROPINSI_HOMEBASE_AKHIR]
				   ,[KD_KABUPATEN_HOMEBASE_AKHIR]
				   ,[NM_KABUPATEN_HOMEBASE_AKHIR]
				   ,[NPWP]
				   ,[PHONE_KANTOR]
				   ,[PHONE_KANTOR_EXT]
				   ,[HANDPHONE_1]
				   ,[HANDPHONE_2]
				   ,[KD_STS_PENERIMAAN_PEG]
				   ,[STS_PENERIMAAN_PEG]
				   ,[ANGKATAN]
				   ,[TAHUN_PEREKRUTAN]
				   ,[STS_PENSIUN]
				   ,[KD_TIDAK_AKTIF]
				   ,[STS_TIDAK_AKTIF]
				   ,[NO_KTP]
				   ,[NO_REKENING]
				   ,[NM_REKENING]
				   ,[KD_BANK]
				   ,[BANK]
				   ,[NO_KARTU_KELUARGA]
				   ,[KD_KANTOR]
				   ,[NM_KANTOR]
				   ,[NM_KANTOR_KOTA]
				   ,[KD_GOL]
				   ,[NM_GOL]
				   ,[KD_JABATAN_STR]
				   ,[NM_JABATAN_STR]
				   ,[NM_JABATAN_STR_SINGKAT]
				   ,[JENIS]
				   ,[NM_UNIT_ES1]
				   ,[NM_UNIT_ES2]
				   ,[NM_UNIT_ES3]
				   ,[NM_UNIT_ES4]
				   ,[NM_UNIT_ES5]
				   ,[NM_UNIT_ES6]
				   ,[NM_UNIT_ES7]
				   ,[TMT_OJK]
				   ,[TMT_LEMBAGA_ASAL]
				   ,[KD_LEMBAGA_ASAL]
				   ,[KET_LEMBAGA_ASAL]
				   ,[TMT_SATKER]
				   ,[TMT_PENEMPATAN]
				   ,[TMT_GOL]
				   ,[TMT_JABATAN]
				   ,[MASA_DINAS_JABATAN]
				   ,[SISA_MASA_DINAS_JABATAN]
				   ,[MASA_TAHUN_OJK]
				   ,[MASA_BULAN_OJK]
				   ,[TMT_PANGKAT]
				   ,[TGL_MASUK]
				   ,[TGL_BERHENTI]
				   ,[TMT_PENSIUN]
				   ,[TGL_MULAI_KONTRAK]
				   ,[TGL_SELESAI_KONTRAK]
				   ,[TMT_PENETAPAN]
				   ,[KD_PEND_UMUM]
				   ,[NM_PEND_UMUM]
				   ,[NM_LEMB_PEND_FORMAL]
				   ,[JURUSAN]
				   ,[FAKULTAS]
				   ,[NILAI]
				   ,[NEGARA]
				   ,[LOKASI_PEND]
				   ,[STATUS_LULUS]
				   ,[TGL_IJAZAH]
				   ,[PFM_ID1]
				   ,[TANGGAL_AWAL1]
				   ,[TANGGAL_AKHIR1]
				   ,[NILAI1]
				   ,[LEVEL_KINERJA1]
				   ,[RANGKING1]
				   ,[PFM_ID2]
				   ,[TANGGAL_AWAL2]
				   ,[TANGGAL_AKHIR2]
				   ,[NILAI2]
				   ,[LEVEL_KINERJA2]
				   ,[RANGKING2]
				   ,[PFM_ID3]
				   ,[TANGGAL_AWAL3]
				   ,[TANGGAL_AKHIR3]
				   ,[NILAI3]
				   ,[LEVEL_KINERJA3]
				   ,[RANGKING3]
				   ,[HUKUMAN]
				   ,[TGL_MULAI_HUKUMAN]
				   ,[TGL_AKHIR_HUKUMAN]
				   ,[KET_MASALAH]
				   ,[JNS_TINDAKAN_ADM_LAIN]
				   ,[KD_JNS_SANKSI]
				   ,[JENIS_SANKSI]
				   ,[KD_TINGKAT_SANKSI]
				   ,[TINGKAT_SANKSI]
				   ,[KATEGORI_FUNGSI]
				   ,[NAMA_FUNGSI]
				   ,[POSISI]
				   ,[POSISI_SINGKAT]
				   ,[CUTI_CLTO]
				   ,[TGL_AWAL_CUTI_PEG_CLTO]
				   ,[TGL_AKHIR_CUTI_PEG_CLTO]
				   ,[CUTI_KEAGAMAAN]
				   ,[TGL_AWAL_CUTI_PEG_KEAG]
				   ,[TGL_AKHIR_CUTI_PEG_KEAG]
				   ,[PEGAWAI_TUGAS_BELAJAR]
				   ,[NM_PEND_PTB]
				   ,[NM_UNIVERSITAS_PTB]
				   ,[NM_FAKULTAS_PTB]
				   ,[NM_JURUSAN_PTB]
				   ,[TGL_MULAI_PTB]
				   ,[TGL_SELESAI_PTB]
				   ,[KOTA_PTB]
				   ,[NM_STATUS_JABATAN]
				   ,[KET_STATUS_JABATAN]
				   ,[TANGGAL_END_PLT_PLH]
				   ,[NM_JABATAN_PLT_PLH]
				   ,[NM_UNIT_ES_PLT_PLH]
				   ,[PESERTA_SURAT_TUGAS]
				   ,[STU_ID]
				   ,[KSO_KASUSNOMOR]
				   ,[NO_SURAT_TUGAS]
				   ,[TGL_MULAI_SURAT_TUGAS]
				   ,[TGL_SELESAI_SURAT_TUGAS]
				   ,[KETERANGAN_SURAT_TUGAS]
				   ,[LOKASI_SURAT_TUGAS]
				   ,[JENIS_SURAT_TUGAS]
				   ,[SATUAN_KERJA]
				   ,[BULAN_TAHUN]
				   ,[BULAN]
				   ,[TAHUN]
				   ,[CREATE_DATE]
				   ,[CREATE_TIME])

		SELECT * FROM @temp_table

		COMMIT TRAN
	END TRY

	BEGIN CATCH
		  ROLLBACK TRAN
	END CATCH  


GO
/****** Object:  StoredProcedure [dbo].[cek_pergerakan_skala_gaji]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[cek_pergerakan_skala_gaji] @nip varchar(10)
AS
BEGIN
  -- routine body goes here, e.g.
  -- SELECT 'Navicat for SQL Server'

	--SPG_DATA_CURRENT
	DECLARE
	@tmt_pangkat date,
	@tmt_gol date,
	@kd_skala_gaji_lama int,
	@kd_skala_gaji_baru int,
	@tgl_grade varchar(5),
  @tgl_skg varchar(5),
	@kd_pangkat varchar(2),
	@kd_gol varchar(2),
	@kd_jabatan_str varchar(3),
	@kd_unit_org varchar(24),
	@status_pergerakan varchar(20),
	@kd_pangkat_baru varchar(3),
	@tmt_grade date
	

	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@no_urut_pang_peg int,
	@id_pang int,
	@sts_riwayat_grade varchar(2),
	@check_nip varchar(10)
	
	
	--SPG_SKALA_GAJI
	DECLARE
	@kd_skala_gaji_max int

	--SPG_PANGKAT
	DECLARE
	@kd_pangkat_max varchar(3)

	select  @tmt_gol=tmt_gol, @tmt_pangkat=tmt_pangkat, @kd_skala_gaji_lama=kd_skala_gaji, 
			@kd_pangkat=kd_pangkat, @kd_jabatan_str=kd_jabatan_str, @kd_unit_org=kd_unit_org,
			@tgl_grade=substring(cast(tmt_pangkat as varchar(10)),6,5),
			@tgl_skg=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,5),
			@tmt_grade=cast((cast(year(getdate()) as varchar(4))+'-'+substring(cast(tmt_pangkat as varchar(10)),6,2)+'-01') as date)
			from spg_data_current where nip=@nip
			
			-- GET KD_GOL SPG_PANGKAT
			select @kd_gol=kd_gol from spg_pangkat where kd_pangkat=@kd_pangkat
			
			-- GET BATAS MAKSIMAL KD_SKALA_GAJI 
			select @kd_skala_gaji_max=max(skala_gaji) from spg_skala_gaji where kd_gol=@kd_gol
			
			-- GET BATAS MAKSIMAL KD_PANGKAT 
			select @kd_pangkat_max=max(cast(kd_pangkat as int)) from spg_pangkat where kd_gol=@kd_gol
			
			IF(@kd_skala_gaji_lama < @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=@kd_skala_gaji_lama+1
				SET @status_pergerakan='Bergulir'
				SET @kd_pangkat_baru=@kd_pangkat
				SET @tmt_grade=@tmt_pangkat
			END
			-- JIKA SG SUDAH MAKSIMAL MAKA GRADE BERUBAH
			ELSE IF(@kd_skala_gaji_lama >= @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=1
				SET @kd_pangkat_baru=(cast(@kd_pangkat as int)+1)
				SET @status_pergerakan='Bergulir'
				SET @tmt_grade=@tmt_grade
				
				--print 'KD_PANGKAT_BARU : '+@kd_pangkat_baru
				--print 'KD_PANGKAT_MAX : '+@kd_pangkat_max
				--print 'TMT_GRADEEE : '+cast(@tmt_grade as varchar(10))
				-- JIKA GRADE & SG SUDAH MAKSIMAL MAKA TIDAK ADA PERUBAHAN
				IF(@kd_pangkat_baru > @kd_pangkat_max)
				BEGIN
					SET @kd_skala_gaji_baru=@kd_skala_gaji_lama
					SET @kd_pangkat_baru=@kd_pangkat_max
					SET @status_pergerakan='Tetap'
					SET @tmt_grade=@tmt_pangkat
				END
			END
	
				
			-- GET MAX ID SPG_RIWAYAT_KEPANGKATAN
			select @id_pang=(max(id)+1) from SPG_RIWAYAT_KEPANGKATAN
			
			-- GET NO URUT SPG_RIWAYAT_KEPANGKATAN 
			select @no_urut_pang_peg=(max(no_urut_pang_peg)+1) from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1'
			
			--select @check_nip=nip from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1' and sts_riwayat_grade='4' and dbo.dateToPeriode(tanggal_buat)=dbo.dateToPeriode(getdate())
			--print 'cek nip : '+@check_nip;
			
			print 'NIP : '+@nip
			print 'SKALA LAMA : '+cast(@kd_skala_gaji_lama as varchar(10))
			print 'SKALA BARU : '+cast(@kd_skala_gaji_baru as varchar(10))
			print 'PANGKAT LAMA : '+@kd_pangkat
			print 'PANGKAT BARU : '+@kd_pangkat_baru
			print 'TMT GRADE : '+cast(@tmt_grade as varchar(10))
			print 'TMT LVL JABATAN : '+cast(@tmt_gol as varchar(10))

			--IF(@check_nip is null or @check_nip = '')
			--BEGIN
				IF(@status_pergerakan = 'Bergulir')
				BEGIN
					-- INSERT RIWAYAT SPG_RIWAYAT_KEPANGKATAN
					insert into SPG_RIWAYAT_KEPANGKATAN (NIP,NO_URUT_PANG_PEG,KD_PANGKAT,TMT_PANG_PEG,ID,KD_STATUS_VALIDASI,TANGGAL_BUAT,NAMA_BUAT,STS_RIWAYAT_GRADE,KD_SKALA_GAJI,TMT_GOL,NO_SK,TGL_SK)
					values (@nip,@no_urut_pang_peg,@kd_pangkat,@tmt_grade,@id_pang,'1',getdate(),'SYSTEM','4',@kd_skala_gaji_baru,@tmt_gol,@nip+'/'+substring(convert(varchar(10),@tmt_pangkat,105),1,5)+'-'+cast(YEAR(getdate()) as varchar(4)),cast(YEAR(getdate()) as varchar(4))+'-'+@tgl_grade)
				END

				-- INSERT LOG TBL_LOG_PERGERAKAN_SG_PEG
				insert into tbl_log_pergerakan_sg_peg (NIP,KD_SKALA_GAJI_LAMA,KD_SKALA_GAJI_BARU,KD_PANGKAT_LAMA,KD_JABATAN_STR,KD_UNIT_ORG,TMT_GRADE,STATUS,CTIME,MTIME,KD_PANGKAT_BARU,TMT_GOL)
				values (@nip,@kd_skala_gaji_lama,@kd_skala_gaji_baru,@kd_pangkat,@kd_jabatan_str,@kd_unit_org,@tmt_grade,@status_pergerakan,getdate(),getdate(),@kd_pangkat_baru,@tmt_gol)
			--END

END
GO
/****** Object:  StoredProcedure [dbo].[EIS_PROMOSI]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[EIS_PROMOSI] 
	-- Add the parameters for the stored procedure here
     @querydate DATE = '1900-01-01',
	 @year_kinerja1 int = 0,
	 @year_kinerja2 int = 0
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
IF @querydate = '1900-01-01' SET @querydate = getdate()
IF @year_kinerja1 = 0 select @year_kinerja1 = max(year(TANGGAL_MULAI)) from PFM_RIWAYAT_KINERJA
IF @year_kinerja2 = 0 select @year_kinerja2 = max(year(TANGGAL_MULAI)) from PFM_RIWAYAT_KINERJA where year(TANGGAL_MULAI) != @year_kinerja1
select 
dc.nip
,dc.nm_peg
,CASE WHEN sp.lembaga_asal = '1' THEN 'Pengalihan BI' WHEN sp.lembaga_asal = '2' THEN 'Penugasan POLRI' WHEN sp.lembaga_asal = '3' THEN (CASE sp.STS_PENERIMAAN_PEG WHEN '500' THEN 'Penugasan BPK' ELSE 'Pengalihan BPK' END) WHEN sp.lembaga_asal = '4' THEN (CASE sp.STS_PENERIMAAN_PEG WHEN '500' THEN 'Penugasan BPKP' ELSE 'Pengalihan BPKP' END) WHEN sp.lembaga_asal = '5' THEN 'Penugasan Kejaksaan' WHEN sp.lembaga_asal = '6' THEN (CASE sp.STS_PENERIMAAN_PEG WHEN '500' THEN 'Penugasan KEMENKEU' ELSE 'Pengalihan KEMENKEU' END) WHEN sp.lembaga_asal = '7' THEN 'BAPEPAM-LK' WHEN sp.lembaga_asal = '8' THEN 'Penugasan Bank Mandiri' END lembaga_asal
,CASE WHEN sp.sts_penerimaan_peg = '100' THEN 'PCT' WHEN sp.sts_penerimaan_peg = '200' THEN 'PCS' WHEN sp.sts_penerimaan_peg = '300' THEN 'PCP' WHEN sp.sts_penerimaan_peg = '400' THEN 'PCE' WHEN sp.sts_penerimaan_peg = '500' THEN 'Ex Penugasan' WHEN sp.sts_penerimaan_peg = '600' THEN 'Ex PKWT' END jalun_penerimaan
,sp.ANGKATAN angkatan_penerimaan
,CASE WHEN sp.TAHUN_PEREKRUTAN = 0 then '' else sp.TAHUN_PEREKRUTAN end tahun_penerimaan
,dc.NM_ESELON level_jabatan
,dc.NM_JABATAN jabatan
,sp.TGL_LAHIR_PEG
,dbo.MasaDinasTahunBulan(sp.TGL_LAHIR_PEG,@querydate) usia_saat_ini
,dc.tmt_OJK
,dc.TMT_LEMBAGA_ASAL
,dc.TMT_JABATAN
,dc.TMT_PANGKAT TMT_LEVEL_JABATAN
,dbo.MasaDinasTahunBulan(dc.TMT_PANGKAT,@querydate) masa_dinas_level_jabatan
,dc.TMT_PENSIUN
,dc.TMT_BERAKHIR_KONTRAK
,datediff(year,sp.tgl_lahir_peg, dc.tmt_pensiun) BUP
,dbo.MasaDinasTahunBulan(@querydate,dc.TMT_PENSIUN) sisa_masa_dinas
,kp.periode1
,kp.nilai1
,kp.level_kinerja1
,kp.periode2
,kp.nilai2
,kp.level_kinerja2
,dc.ket_pend_formal pendidikan_terakhir
,dc.nm_unit_es1 bidang
,dc.nm_unit_es2 deputi_komisioner
,dc.nm_unit_es3 departemen
,dc.nm_unit_es4 direktorat
,dc.nm_unit_es5 deputi_direktorat
,dc.nm_unit_es6 bagian
,dc.nm_unit_es7 subbagian
from 
SPG_DATA_CURRENT dc 
left outer join SPG_PEGAWAI sp on dc.NIP = sp.NIP
left outer join (
select NIP
,max(case when periode = @year_kinerja1 THEN periode END) periode1
,max(case when periode = @year_kinerja1 THEN nilai END) nilai1
,max(case when periode = @year_kinerja1 THEN LEVEL_KINERJA END) level_kinerja1
,max(case when periode = @year_kinerja2 THEN periode END) periode2
,max(case when periode = @year_kinerja2 THEN nilai END) nilai2
,max(case when periode = @year_kinerja2 THEN LEVEL_KINERJA END) level_kinerja2
from (select nip,year(TANGGAL_MULAI) periode,nilai,LEVEL_KINERJA from PFM_RIWAYAT_KINERJA) a where periode in (@year_kinerja1,@year_kinerja2) group by nip
) KP on dc.nip = kp.nip
where isnull(dc.sts_pensiun,0) <> 1
and dc.nip not in --tidak dalam masa sanksi dan tugas belajar
(select NIP from SPG_RIWAYAT_HUKUMAN_DISIPLIN where @querydate BETWEEN TGL_MULAI_HUKUMAN AND TGL_AKHIR_HUKUMAN
union 
select NIP from SPG_TUGAS_BELAJAR_DEFINITIF where @querydate BETWEEN TGL_MULAI AND TGL_SELESAI)
END
GO
/****** Object:  StoredProcedure [dbo].[FW_userPermission]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Yulius
-- Create date: 28 Okt 2010
-- Description:	Cek apakah suatu user memiliki hak akses modul atau tidak
-- =============================================
--[FW_userPermission] @role_id = 'DeptHead1'
CREATE PROCEDURE [dbo].[FW_userPermission] 
	@user_id VARCHAR(50) = NULL,
	@mod_kode VARCHAR(500) = NULL,		
	@role_id VARCHAR(200) = NULL,
	@satker_id BIGINT = NULL

AS
BEGIN
DECLARE @sql NVARCHAR(4000)
SELECT @sql ='
SELECT um.user_id,ur.role_id AS role_id, r.role_id AS mod_kode, ur.satker_id
FROM dbo.User_Master um 
inner join FW_User_Role ur on um.user_id = ur.user_id
INNER JOIN FW_Ref_Role r ON r.role_id = ur.role_id AND r.stsrc = ''A''
Where ur.stsrc = ''A'' and um.stsrc = ''A''
'

IF @user_id IS NOT NULL 
	SELECT @sql = @sql + ' AND um.user_id = @user_id'



IF @mod_kode IS NOT NULL 
	SELECT @sql = @sql + ' AND r.role_id in (select split from dbo.Split('','',@mod_kode))'

IF @role_id IS NOT NULL	
	SELECT @sql = @sql + ' and r.role_id = @role_id'
	
IF @satker_id IS NOT NULL
BEGIN	
	SELECT @sql = @sql + ' and ur.satker_id = @satker_id'
END


PRINT @sql
EXEC sp_executesql @sql,N'@user_id varchar(50), @mod_kode varchar(500), @role_id varchar(200),@satker_id bigint',
		@user_id, @mod_kode, @role_id,@satker_id
END


GO
/****** Object:  StoredProcedure [dbo].[generate_kasusnomor]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[generate_kasusnomor]
AS
BEGIN

WITH T
     AS (SELECT TOP 100 *
         FROM TBL_KASUSNOMOR
         ORDER BY KSO_KASUSNOMOR ASC)
DELETE FROM T 

INSERT INTO 
TBL_KASUSNOMOR
SELECT
	TOP 100 
	'KSO-' + RIGHT (
		'z0000000000000' + CONVERT (
			VARCHAR (30),
			(
				(
					SELECT
						dbo.nvl (
							CAST (
								dbo.substr (MAX(kso_kasusnomor), 5, 6) AS INTEGER
							),
							0
						)
					FROM
						TBL_KASUSNOMOR
				) + (
					row_number () OVER (ORDER BY kso_kasusnomor)
				)
			)
		),
		6
	) AS KSO_KASUSNOMOR, '0' AS STATUS , NULL
FROM 
	TBL_KASUSNOMOR 

END

GO
/****** Object:  StoredProcedure [dbo].[getMaxIdPermohonanCuti]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[getMaxIdPermohonanCuti]
@Session_ID VARCHAR(100)=NULL
AS 
BEGIN
	DECLARE @max_id int
	DECLARE @returnCode INT

	BEGIN TRY     
		            		
	EXEC @returnCode = sp_getapplock 
			@Resource = 'getMaxIdPermohonanCuti', 
			@LockMode = 'Exclusive', 
			@LockOwner = 'Session', 
			@LockTimeout = 50,
			@DbPrincipal  = 'public'
	
	WHILE @returnCode NOT IN (0, 1)
    BEGIN
        EXEC @returnCode = sp_getapplock @Resource='getMaxIdPermohonanCuti', @LockMode='Exclusive',
				@LockOwner='Session', @LockTimeout=50, @DbPrincipal='public'
    END 
    
    --SLEEP FOR 10 SECONDS
    --WAITFOR DELAY '00:00:03';
                    
		select @max_id=max(id)+1
		from spg_permohonan_cuti_tempid

		insert into spg_permohonan_cuti_tempid (id, created_date, session_id) values (@max_id, getdate(), @Session_ID)

		select max(id) as max_id
		from spg_permohonan_cuti_tempid
    
    --release the lock on stored proc
    EXEC @returnCode = sp_releaseapplock 
                        @Resource = 'getMaxIdPermohonanCuti',                        
                        @LockOwner = 'Session',
                        @DbPrincipal  = 'public'
    
    END TRY      
    BEGIN CATCH
		
		IF @returnCode IN (0, 1)
		BEGIN
			 EXEC @returnCode = sp_releaseapplock 
					@Resource = 'getMaxIdPermohonanCuti',                        
					@LockOwner = 'Session',
					@DbPrincipal  = 'public'
		END		
    
		DECLARE @ErrMsg VARCHAR(4000)
		SELECT @ErrMsg = ERROR_MESSAGE() 	    
		RAISERROR(@ErrMsg, 15, 50)        
	
	END CATCH  
END

GO
/****** Object:  StoredProcedure [dbo].[insert_dump_error]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[insert_dump_error](@term_id integer,@fp_id integer,@t_date datetime,@fkey int,@ref_id int)
as

insert into spg_absensi_dump_error (term_id,FP_ID,T_DATE,FKEY,REF_ID)  
    values (@term_id,@fp_id,@t_date,@fkey,@ref_id) 



GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[insert_tempdata] @kso_kasusnomor varchar(20), @action VARCHAR(1)
AS
BEGIN

	DECLARE 
	@jns_mutasi varchar(2),
	@tgl_mulai date,
	@nip varchar(20),
	@nip_lama varchar(20),
	@nip_baru varchar(20),
	@nama varchar(200),
	@id_kota1 varchar(10),
	@id_kota2 varchar(10),
	@statdata varchar(2),
	@kd_kantor_lama varchar(200),
	@kd_kantor_baru varchar(200),
	@kd_kantor varchar(200),
	@stataktiv varchar(2),
	@kd_unit_org_lama varchar(24),
	@kd_unit_org_baru varchar(24),
	@kd_unit_es1_lama varchar(24),
	@kd_unit_es2_lama varchar(24),
	@kd_unit_es3_lama varchar(24),
	@kd_unit_es1_baru varchar(24),
	@kd_unit_es2_baru varchar(24),
	@kd_unit_es3_baru varchar(24),
	@nm_unit_org_lama varchar(255),
	@nm_unit_org_baru varchar(255)
	
	/* get detail pegawai */
	SELECT 
		@jns_mutasi=a.KD_JNS_MUTASI,
		@nip=a.NIP,
		@nama=b.NM_PEG,
		@tgl_mulai=a.TGL_MULAI,
		@kd_kantor_baru=a.KD_KANTOR_BARU,
		@kd_kantor_lama=a.KD_KANTOR,
		@kd_unit_org_baru=a.KD_UNIT_ORG_BARU,
		@kd_unit_org_lama=a.KD_UNIT_ORG
	FROM 
		SPG_PMK AS a 
	LEFT JOIN 
		SPG_PEGAWAI AS b 
	ON a.NIP=b.NIP 
	WHERE a.KSO_KASUSNOMOR=@kso_kasusnomor
	
	IF(@action!='D')
		BEGIN 
			SET @kd_kantor = @kd_kantor_baru
		END 
	ELSE 
		BEGIN 
			SET @kd_kantor = @kd_kantor_lama
		END
	
	/* get detail kota 1 */
	SELECT 
		@id_kota1=KD_KOTA 
	FROM 
		SPG_08_KANTOR WHERE KD_KANTOR=@kd_kantor_lama

	/* get detail kota 2 */
	SELECT 
		@id_kota2=KD_KOTA 
	FROM 
		SPG_08_KANTOR WHERE KD_KANTOR=@kd_kantor_baru
	
	
	/* get detail nm unit org baru */
	SELECT 
		@kd_unit_es1_baru=kd_unit_es1,@kd_unit_es2_baru=kd_unit_es2,@kd_unit_es3_baru=kd_unit_es3
	FROM 
		SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_org_baru
	
	IF(@kd_unit_es3_baru is not null or @kd_unit_es3_baru != '')
		BEGIN
			SELECT 
				@nm_unit_org_baru=nm_unit_org
			FROM 
				SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es3_baru
		END
	ELSE
		BEGIN
			IF(@kd_unit_es2_baru is not null or @kd_unit_es2_baru != '')
				BEGIN
					SELECT 
						@nm_unit_org_baru=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es2_baru
				END
			ELSE
				BEGIN
					SELECT 
						@nm_unit_org_baru=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es1_baru
				END
		END

	/*get detail nm unit org lama*/
	SELECT 
		@kd_unit_es1_lama=kd_unit_es1,@kd_unit_es2_lama=kd_unit_es2,@kd_unit_es3_lama=kd_unit_es3
	FROM 
		SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_org_lama
	
	IF(@kd_unit_es3_lama is not null or @kd_unit_es3_lama != '')
		BEGIN
			SELECT 
				@nm_unit_org_lama=nm_unit_org
			FROM 
				SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es3_lama
		END
	ELSE
		BEGIN
			IF(@kd_unit_es2_lama is not null or @kd_unit_es2_lama != '')
				BEGIN
					SELECT 
						@nm_unit_org_lama=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es2_lama
				END
			ELSE
				BEGIN
					SELECT 
						@nm_unit_org_lama=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es1_lama
				END
		END

	/* check jenis mutasi */
	IF(@jns_mutasi=5)
		BEGIN
			SET @statdata = 1
			SET @stataktiv = 0
			SET @nip_lama = NULL
			SET @nip_baru = @nip
			SET @id_kota1 = @id_kota2
			SET @id_kota2 = NULL
		END
	ELSE 
		BEGIN 
			SET @statdata = 2
			SET @stataktiv = 0
			SET @nip_lama = @nip
			SET @nip_baru = @nip
			SET @id_kota1 = @id_kota1
			SET @id_kota2 = @id_kota2
		END
		
	IF(@action!='D')
		BEGIN
			insert into OJKSIAP.dbo.temp_DataDiri (NIP,NAMA,ID_KOTA1,ID_KOTA2,TGL_TMTSATKER,STATDATA,STATUS_AKTIF,NAMAUNIT,NIP_LAMA,NIP_BARU)
			values(@nip,@nama,@id_kota1,@id_kota2,@tgl_mulai,@statdata,@stataktiv,@nm_unit_org_baru,@nip_lama,@nip_baru);
		END
	ELSE
		BEGIN
			insert into OJKSIAP.dbo.temp_DataDiri (NIP,NAMA,ID_KOTA1,ID_KOTA2,TGL_TMTSATKER,STATDATA,STATUS_AKTIF,NAMAUNIT,NIP_LAMA,NIP_BARU)
			values(@nip,@nama,@id_kota2,@id_kota1,@tgl_mulai,@statdata,@stataktiv,@nm_unit_org_lama,@nip_lama,@nip_baru);
			
			--DELETE FROM SPG_PMK WHERE KSO_KASUSNOMOR=@kso_kasusnomor and nip=@nip
		END
END


GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata_peg_baru]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[insert_tempdata_peg_baru] @nip varchar(20)
AS
BEGIN
	
	DECLARE 
	@jns_mutasi varchar(2),
	@tgl_mulai date,
	@nip_lama varchar(20),
	@nama varchar(200),
	@id_kota1 varchar(10),
	@statdata varchar(2),
	@kd_kantor varchar(200),
	@stataktiv varchar(2),
	@kd_unit_org varchar(24),
	@kd_unit_es1 varchar(24),
	@kd_unit_es2 varchar(24),
	@kd_unit_es3 varchar(24),
	@nm_unit_org varchar(255)

	/* get detail pegawai */
	SELECT 
		@nama=a.NM_PEG,
		@nip_lama=a.NIP_LAMA,
		@tgl_mulai=a.TGL_MASUK,
		@kd_kantor=a.KD_KANTOR,
		@kd_unit_org=a.KD_UNIT_ORG
	FROM 
		SPG_DATA_CURRENT AS a 
	WHERE a.NIP=@nip
	
	/* get detail kota */
	SELECT 
		@id_kota1=KD_KOTA 
	FROM 
		SPG_08_KANTOR WHERE KD_KANTOR=@kd_kantor
	
	/* get detail nm unit org */	
	SELECT 
		@kd_unit_es1=kd_unit_es1,@kd_unit_es2=kd_unit_es2,@kd_unit_es3=kd_unit_es3
	FROM 
		SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_org
	
	IF(@kd_unit_es3 is not null or @kd_unit_es3 != '')
		BEGIN
			SELECT 
				@nm_unit_org=nm_unit_org
			FROM 
				SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es3
		END
	ELSE
		BEGIN
			IF(@kd_unit_es2 is not null or @kd_unit_es2 != '')
				BEGIN
					SELECT 
						@nm_unit_org=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es2
				END
			ELSE
				BEGIN
					SELECT 
						@nm_unit_org=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es1
				END
		END
	
	IF(@nama is not null or @nama != '')
	BEGIN
		IF(@nip_lama is not null or @nip_lama != '')
		BEGIN
			/* set old nip */
			insert into OJKSIAP.dbo.temp_DataDiri (NIP,NAMA,ID_KOTA1,TGL_TMTSATKER,STATDATA,STATUS_AKTIF,NAMAUNIT,NIP_LAMA,NIP_BARU)
			values(@nip_lama,@nama,@id_kota1,@tgl_mulai,'2','1',@nm_unit_org,@nip_lama,@nip);
		END

			/* set new nip */
			insert into OJKSIAP.dbo.temp_DataDiri (NIP,NIP_LAMA,NAMA,ID_KOTA1,TGL_TMTSATKER,STATDATA,STATUS_AKTIF,NAMAUNIT,NIP_BARU)
			values(@nip,@nip_lama,@nama,@id_kota1,@tgl_mulai,'1','0',@nm_unit_org,@nip);
	END
	
	

END
GO
/****** Object:  StoredProcedure [dbo].[insert_tempdata_peg_pensiun]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[insert_tempdata_peg_pensiun] @nip varchar(20)
AS
BEGIN
	
	DECLARE 
	@tgl_mulai date,
	@nip_lama varchar(20),
	@nama varchar(200),
	@id_kota1 varchar(10),
	@statdata varchar(2),
	@kd_kantor varchar(200),
	@stataktiv varchar(2),
	@kd_unit_org varchar(24),
	@kd_unit_es1 varchar(24),
	@kd_unit_es2 varchar(24),
	@kd_unit_es3 varchar(24),
	@nm_unit_org varchar(255),
	@sts_pensiun char(1),
	@kd_tidak_aktif varchar(5)

	/* get detail pegawai */
	SELECT 
		@nama=a.NM_PEG,
		@nip_lama=a.NIP_LAMA,
		@kd_kantor=a.KD_KANTOR,
		@kd_unit_org=a.KD_UNIT_ORG,
		@sts_pensiun=sts_pensiun
	FROM 
		SPG_DATA_CURRENT AS a 
	WHERE a.NIP=@nip and sts_pensiun='1'

	/* get detail pegawai pensiun*/
	SELECT @tgl_mulai=max(tmt_pensiun) from spg_pegawai_pensiun where sts_pensiun='1' and nip=@nip and kd_status_validasi='1'
	
	SELECT @kd_tidak_aktif=kd_tidak_aktif from spg_pegawai_pensiun where sts_pensiun='1' and nip=@nip and kd_status_validasi='1' and tmt_pensiun=@tgl_mulai

	/* get detail kota */
	SELECT 
		@id_kota1=KD_KOTA 
	FROM 
		SPG_08_KANTOR WHERE KD_KANTOR=@kd_kantor
	
	/* get detail nm unit org */
	SELECT 
		@kd_unit_es1=kd_unit_es1,@kd_unit_es2=kd_unit_es2,@kd_unit_es3=kd_unit_es3
	FROM 
		SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_org
	
	IF(@kd_unit_es3 is not null or @kd_unit_es3 != '')
		BEGIN
			SELECT 
				@nm_unit_org=nm_unit_org
			FROM 
				SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es3
		END
	ELSE
		BEGIN
			IF(@kd_unit_es2 is not null or @kd_unit_es2 != '')
				BEGIN
					SELECT 
						@nm_unit_org=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es2
				END
			ELSE
				BEGIN
					SELECT 
						@nm_unit_org=nm_unit_org
					FROM 
						SPG_08_UNIT_ORGANISASI WHERE KD_UNIT_ORG=@kd_unit_es1
				END
		END
	
/*IF((@nama is not null or @nama != '') and @sts_pensiun = '1' and @kd_tidak_aktif != '9') -- 9 => dari perubahan status pegawai
	BEGIN
			insert into OJKSIAP.dbo.temp_DataDiri (NIP,NAMA,ID_KOTA1,TGL_TMTSATKER,STATDATA,STATUS_AKTIF,NAMAUNIT,NIP_LAMA)
			values(@nip,@nama,@id_kota1,@tgl_mulai,'2','1',@nm_unit_org,@nip);
	END
	*/
	

END

GO
/****** Object:  StoredProcedure [dbo].[list_pegawai_logbook]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[list_pegawai_logbook]
	@email varchar(100)
AS
BEGIN
SET nocount off;
	DECLARE @jenis varchar(10);
	DECLARE @hak_akses int = 0;
	DECLARE @is_pltplh int = 0;
	DECLARE @kd_eselon int = -1;
	
	CREATE TABLE #list_logbook (
	KODE_SATKER_ORGANISASI varchar(100),
	NAMA_SATKER_ORGANISASI varchar(200),
	KODE_UNIT_ORGANISASI varchar(100),
	NAMA_UNIT_ORGANISASI varchar(200),
	SINGKATAN_NAMA_UNIT_ORGANISASI varchar(50),
	KODE_LEVEL_JABATAN varchar(100),
	NAMA_LEVEL_JABATAN varchar(200),
	KODE_JABATAN varchar(100),
	NAMA_JABATAN varchar(200),
	NIP varchar(10),
	NM_PEG varchar(200),
	EMAIL varchar(100),
	status_pegawai varchar(10),
	admin_satker int,
	admin_dosm int
	)

	select @jenis = b.JENIS, @hak_akses = c.hak_akses FROM SPG_DATA_CURRENT a
	LEFT OUTER JOIN SPG_08_JABATAN_STR b ON dbo.TRIM(a.KD_JABATAN_STR) = dbo.TRIM(b.KD_JABATAN_STR)
	LEFT OUTER JOIN konfigurasi_logbook c on a.nip = c.nip and getdate() between c.tgl_aktif and isnull(c.tgl_end,'2999-12-01')
	where a.email = @email and isnull(a.sts_pensiun,0) <> 1
	select @kd_eselon = case when @jenis = 'fungsional' then KD_ESELON else -1 end from SPG_DATA_CURRENT where email = @email and STS_PENSIUN='0'

	;with plt_plh as (
		SELECT 
		B.KODE_SATKER_ORGANISASI,B.NAMA_SATKER_ORGANISASI,
		B.KD_UNIT_ORG AS KODE_UNIT_ORGANISASI,B.NM_UNIT_ORG AS NAMA_UNIT_ORGANISASI,
		B.SINGKATAN_UNIT_ORG AS SINGKATAN_NAMA_UNIT_ORGANISASI,
		D.KD_GOL AS KODE_LEVEL_JABATAN, D.NM_GOLONGAN AS NAMA_LEVEL_JABATAN,
		C.KD_JABATAN_STR AS KODE_JABATAN, C.NM_JABATAN_STR AS NAMA_JABATAN,
		A.NIP,A.NM_PEG AS NAMA,A.EMAIL, CASE When C.JENIS = 'struktural' then 'S' else 'F' end status_pegawai ,
		case when ','+E.operator+',' like '%,'+NIP+',%' then 1 when ','+E.validator+',' like '%,'+NIP+',%' then 2 else 0 end admin_satker,
		1 admin_dosm
		--f.admin_dosm
		FROM SPG_DATA_CURRENT A
		LEFT JOIN MARP_UNIT_ORGANISASI_ACTIVE_ONLY_V2 B ON dbo.TRIM(A.KD_UNIT_ORG) = dbo.TRIM(B.KD_UNIT_ORG)
		LEFT JOIN SPG_08_JABATAN_STR C ON dbo.TRIM(A.KD_JABATAN_STR) = dbo.TRIM(C.KD_JABATAN_STR)
		LEFT JOIN SPG_GOLONGAN D ON A.KD_GOL = D.KD_GOL
		LEFT JOIN SPG_ADMIN_SATKER E on a.KD_UNIT_ORG = E.KD_UNIT_ORG
		LEFT JOIN 
		(select 
			a.login,
			case 
			when x2.FUA_NAME is not null and x3.FUA_NAME is not null then 3
			when x2.FUA_NAME is null and x3.FUA_NAME is not null then 2
			when x2.FUA_NAME is not null and x3.FUA_NAME is null then 1
			else 0 end admin_dosm
			from
			tbl_user a
			left outer join
			(select a.fua_name from 
			WF_FUNCTIONACCESS a
			join
			WF_MENU b on a.MEN_NOMORURUT = b.MEN_NOMORURUT and b.MEN_URL = '/logbook_kesehatan.php'
			where fua_read = 1 and FUA_DELETE = 1 and fua_edit = 1 and fua_add = 1
			group by a.fua_name) x2
			on a.FUA_NAME = x2.FUA_NAME
			left outer join
			(select a.fua_name from 
			WF_FUNCTIONACCESS a
			join
			WF_MENU b on a.MEN_NOMORURUT = b.MEN_NOMORURUT and b.MEN_URL = '/logbook_aktifitas.php'
			where fua_read = 1 and FUA_DELETE = 1 and fua_edit = 1 and fua_add = 1
			group by a.fua_name) x3
			on a.FUA_NAME = x3.FUA_NAME
		) as f on a.nip = f.login
		WHERE A.STS_PENSIUN = '0' 
		AND B.KODE_SATKER_ORGANISASI IN (
			SELECT B.KODE_SATKER_ORGANISASI 
			FROM vw_PLTPLH A
			LEFT JOIN SPG_08_UNIT_ORGANISASI B ON dbo.TRIM(A.KD_UNIT_ORG_PLTPLH) = dbo.TRIM(B.KD_UNIT_ORG) or dbo.TRIM(A.KD_UNIT_ORG_PLTPLH) = dbo.TRIM(B.KD_UNIT_ORG_INDUK)
			WHERE A.EMAIL = @email and (ISNULL(A.Tanggal_end,'2999-12-31') >= GETDATE() or A.TANGGAL_END = '1900-01-01')
		) 
		AND A.KD_UNIT_ORG like (select left(kd_unit_org_pltplh, charindex('000', kd_unit_org_pltplh) - 1)+'%' from vw_PLTPLH where email = @email and (ISNULL(Tanggal_end,'2999-12-31') >= GETDATE() or TANGGAL_END = '1900-01-01'))
		) 

		insert into #list_logbook
		(KODE_SATKER_ORGANISASI,NAMA_SATKER_ORGANISASI,
	KODE_UNIT_ORGANISASI,
	NAMA_UNIT_ORGANISASI,
	SINGKATAN_NAMA_UNIT_ORGANISASI,
	KODE_LEVEL_JABATAN,
	NAMA_LEVEL_JABATAN,
	KODE_JABATAN,
	NAMA_JABATAN,
	NIP ,
	NM_PEG,
	EMAIL,
	status_pegawai,
	admin_satker,
	admin_dosm)
		SELECT 
		B.KODE_SATKER_ORGANISASI,B.NAMA_SATKER_ORGANISASI,
		B.KD_UNIT_ORG AS KODE_UNIT_ORGANISASI,B.NM_UNIT_ORG AS NAMA_UNIT_ORGANISASI,
		B.SINGKATAN_UNIT_ORG AS SINGKATAN_NAMA_UNIT_ORGANISASI,
		D.KD_GOL AS KODE_LEVEL_JABATAN, D.NM_GOLONGAN AS NAMA_LEVEL_JABATAN,
		C.KD_JABATAN_STR AS KODE_JABATAN, C.NM_JABATAN_STR AS NAMA_JABATAN,
		A.NIP,A.NM_PEG AS NAMA,A.EMAIL, CASE When C.JENIS = 'struktural' then 'S' else 'F' end status_pegawai ,
		case when ','+E.operator+',' like '%,'+NIP+',%' then 1 when ','+E.validator+',' like '%,'+NIP+',%' then 2 else 0 end admin_satker,
		1 admin_dosm
		--f.admin_dosm
		FROM SPG_DATA_CURRENT A
		LEFT JOIN MARP_UNIT_ORGANISASI_ACTIVE_ONLY_V2 B ON dbo.TRIM(A.KD_UNIT_ORG) = dbo.TRIM(B.KD_UNIT_ORG)
		LEFT JOIN SPG_08_JABATAN_STR C ON dbo.TRIM(A.KD_JABATAN_STR) = dbo.TRIM(C.KD_JABATAN_STR)
		LEFT JOIN SPG_GOLONGAN D ON A.KD_GOL = D.KD_GOL
		LEFT JOIN SPG_ADMIN_SATKER E on a.KD_UNIT_ORG = E.KD_UNIT_ORG
		LEFT JOIN 
		(select 
			a.login,
			case 
			when x2.FUA_NAME is not null and x3.FUA_NAME is not null then 3
			when x2.FUA_NAME is null and x3.FUA_NAME is not null then 2
			when x2.FUA_NAME is not null and x3.FUA_NAME is null then 1
			else 0 end admin_dosm
			from
			tbl_user a
			left outer join
			(select a.fua_name from 
			WF_FUNCTIONACCESS a
			join
			WF_MENU b on a.MEN_NOMORURUT = b.MEN_NOMORURUT and b.MEN_URL = '/logbook_kesehatan.php'
			where fua_read = 1 and FUA_DELETE = 1 and fua_edit = 1 and fua_add = 1
			group by a.fua_name) x2
			on a.FUA_NAME = x2.FUA_NAME
			left outer join
			(select a.fua_name from 
			WF_FUNCTIONACCESS a
			join
			WF_MENU b on a.MEN_NOMORURUT = b.MEN_NOMORURUT and b.MEN_URL = '/logbook_aktifitas.php'
			where fua_read = 1 and FUA_DELETE = 1 and fua_edit = 1 and fua_add = 1
			group by a.fua_name) x3
			on a.FUA_NAME = x3.FUA_NAME
		) as f on a.nip = f.login
		WHERE A.STS_PENSIUN = '0' 
		AND B.KODE_SATKER_ORGANISASI IN (
			SELECT B.KODE_SATKER_ORGANISASI 
			FROM SPG_DATA_CURRENT A
			LEFT JOIN SPG_08_UNIT_ORGANISASI B ON dbo.TRIM(A.KD_UNIT_ORG) = dbo.TRIM(B.KD_UNIT_ORG) or dbo.TRIM(A.KD_UNIT_ORG) = dbo.TRIM(B.KD_UNIT_ORG_INDUK)
			WHERE A.STS_PENSIUN='0' AND A.EMAIL = @email
		)
		AND A.KD_UNIT_ORG like (select 
			case @hak_akses when 1 then left(kd_unit_org, charindex('000', kd_unit_org) - 1)+'%' 
			when 2 then kd_unit_org
			else A.KD_UNIT_ORG end
			from SPG_DATA_CURRENT where email = @email and isnull(sts_pensiun,0) <> 1)
		AND (A.KD_ESELON > @kd_eselon or a.email = @email)
		UNION
		select * from plt_plh
		order by KODE_LEVEL_JABATAN desc

		select * from #list_logbook
		drop table #list_logbook
	END
GO
/****** Object:  StoredProcedure [dbo].[loader_pengaturan_absensi]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE proc [dbo].[loader_pengaturan_absensi](@login_nip varchar(50),@nip varchar(50),@kd_shift varchar(20),@tgl_mulai date,@tgl_selesai date)      

AS        

declare @id_kelompok_absensi int;    

declare @id_pengaturan_absensi int;    

    

       

        

DECLARE @TranName VARCHAR(20);        

SELECT @TranName = 'MyTransaction';        

      

     

BEGIN TRANSACTION @TranName;        

    

if (@tgl_selesai='')    

begin    

 set @tgl_selesai=null;    

    

end    

    

select @id_pengaturan_absensi=b.ID from SPG_NMPAKET_ABSENSI a inner join     

SPG_PENGATURAN_ABSENSI b on a.ID=b.ID_NMPAKET_DETAIL    

where a.NAMA=@kd_shift    

    

select top 1 @id_kelompok_absensi=id from SPG_KELOMPOK_ABSENSI where NIP=@nip    

order by tgl_mulai desc  





  

 --end kan shift yg berlaku    

 update SPG_KELOMPOK_ABSENSI set tgl_selesai=DATEADD(DAY,-1,@tgl_mulai),tanggal_ubah=getdate(),nama_ubah=@login_nip where id=@id_kelompok_absensi;    

     

 -- create baru shift    

 insert into spg_kelompok_absensi (nip,id_pengaturan_absensi,tgl_mulai,tgl_selesai,nama_buat)    

 values (@nip,@id_pengaturan_absensi,@tgl_mulai,@tgl_selesai,@login_nip);    

 --      

  

COMMIT TRANSACTION @TranName;



GO
/****** Object:  StoredProcedure [dbo].[newKasusnomor]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[newKasusnomor](@sesi varchar(100),@kso varchar(20) OUTPUT)
AS 
BEGIN
	DECLARE @jumlah int
	DECLARE @returnCode INT

	BEGIN TRY     
		            		
	EXEC @returnCode = sp_getapplock 
			@Resource = 'newKasusnomor', 
			@LockMode = 'Exclusive', 
			@LockOwner = 'Session', 
			@LockTimeout = 50,
			@DbPrincipal  = 'public'
	
	WHILE @returnCode NOT IN (0, 1)
    BEGIN
        EXEC @returnCode = sp_getapplock @Resource='newKasusnomor', @LockMode='Exclusive',
				@LockOwner='Session', @LockTimeout=50, @DbPrincipal='public'
    END 
    
    --SLEEP FOR 10 SECONDS
    --WAITFOR DELAY '00:00:10';
                    
		SET @kso = NULL

		--SELECT top 1 @kso=kso_kasusnomor from TBL_KASUSNOMOR WHERE STATUS=0 ORDER BY kso_kasusnomor asc 

		SELECT @jumlah = COUNT (KSO_KASUSNOMOR)
		FROM TBL_KASUSNOMOR
		WHERE STATUS = 0
		
		IF (@jumlah <= 10 AND (@kso is null OR @kso = ''))
		BEGIN 
			exec dbo.generate_kasusnomor
		END

		SELECT top 1 @kso=kso_kasusnomor from TBL_KASUSNOMOR WHERE STATUS=0 ORDER BY kso_kasusnomor asc 
		update TBL_KASUSNOMOR set status=1 , session=@sesi where kso_kasusnomor=@kso and status=0

		SELECT @kso as kso
	  -- routine body goes here, e.g.
	  -- SELECT 'Navicat for SQL Server'
    
    --release the lock on stored proc
    EXEC @returnCode = sp_releaseapplock 
                        @Resource = 'newKasusnomor',                        
                        @LockOwner = 'Session',
                        @DbPrincipal  = 'public'
    
    END TRY      
    BEGIN CATCH
		
		IF @returnCode IN (0, 1)
		BEGIN
			 EXEC @returnCode = sp_releaseapplock 
					@Resource = 'newKasusnomor',                        
					@LockOwner = 'Session',
					@DbPrincipal  = 'public'
		END		
    
		DECLARE @ErrMsg VARCHAR(4000)
		SELECT @ErrMsg = ERROR_MESSAGE() 	    
		RAISERROR(@ErrMsg, 15, 50)        
	
	END CATCH  
END
GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[pergerakan_skala_gaji]
AS
BEGIN

	DECLARE 
	@id int
	
	--SPG_DATA_CURRENT
	DECLARE
	@nip varchar(10),
	@tmt_pangkat date,
	@tmt_gol date,
	@kd_skala_gaji_lama int,
	@kd_skala_gaji_baru int,
	@tgl_grade varchar(5),
  @tgl_skg varchar(5),
	@kd_pangkat varchar(2),
	@kd_gol varchar(2),
	@kd_jabatan_str varchar(3),
	@kd_unit_org varchar(24),
	@status_pergerakan varchar(20),
	@kd_pangkat_baru varchar(3),
	@tmt_grade date
	

	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@no_urut_pang_peg int,
	@id_pang int,
	@sts_riwayat_grade varchar(2),
	@check_nip varchar(10)
	
	
	--SPG_SKALA_GAJI
	DECLARE
	@kd_skala_gaji_max int

	--SPG_PANGKAT
	DECLARE
	@kd_pangkat_max varchar(3)

	declare cur CURSOR LOCAL for
	    /*select id
			from spg_data_current where sts_pensiun='0' 
			and tmt_pangkat<=getdate()
			and year(tmt_pangkat)=(substring(CONVERT(VARCHAR(10),GETDATE(),21),1,4)-1)
			and month(tmt_pangkat)=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2)
			and kd_status_kepeg in ('100','300')*/
			select a.id
			from spg_data_current a
			left join (SELECT * FROM(
				SELECT nip, tmt_pang_peg, sts_riwayat_grade, kd_skala_gaji, 
				case when (tmt_pang_peg is null or tmt_pang_peg = '1900-01-01') then 0
					else ROUND(CAST(DATEDIFF(DAY, tmt_pang_peg, GETDATE()) / 365.2425 AS FLOAT),0)
				end as md_pangkat, 
				tanggal_buat, row_number() OVER (partition BY nip ORDER BY tmt_pang_peg DESC, kd_skala_gaji DESC, no_urut_pang_peg DESC) AS rn
				FROM SPG_RIWAYAT_KEPANGKATAN
				WHERE kd_status_validasi = '1' and sts_riwayat_grade <> '6'
			)x WHERE rn = 1) b on a.nip=b.nip and a.tmt_pangkat=b.tmt_pang_peg
			where a.sts_pensiun='0' 
			and month(a.tmt_pangkat)=cast(substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2) as int)
			and day(a.tmt_pangkat)=cast(substring(CONVERT(VARCHAR(10),GETDATE(),21),9,2) as int)
			and a.kd_status_kepeg in ('100','300')
			and b.md_pangkat>='1'
			and dbo.dateToPeriode(b.tanggal_buat) < dbo.dateToPeriode(getdate())
	open cur
	
	fetch next from cur into @id
	
	while @@FETCH_STATUS = 0 
	BEGIN
		  --print @id;
			select @nip=nip, @tmt_gol=tmt_gol, @tmt_pangkat=tmt_pangkat, @kd_skala_gaji_lama=kd_skala_gaji, 
			@kd_pangkat=kd_pangkat, @kd_jabatan_str=kd_jabatan_str, @kd_unit_org=kd_unit_org,
			@tgl_grade=substring(cast(tmt_pangkat as varchar(10)),6,5),
			@tgl_skg=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,5),
			@tmt_grade=cast((cast(year(getdate()) as varchar(4))+'-'+substring(cast(tmt_pangkat as varchar(10)),6,2)+'-01') as date)
			from spg_data_current where id=@id
			
			-- GET KD_GOL SPG_PANGKAT
			select @kd_gol=kd_gol from spg_pangkat where kd_pangkat=@kd_pangkat
			
			-- GET BATAS MAKSIMAL KD_SKALA_GAJI 
			select @kd_skala_gaji_max=max(skala_gaji) from spg_skala_gaji where kd_gol=@kd_gol
			
			-- GET BATAS MAKSIMAL KD_PANGKAT 
			select @kd_pangkat_max=max(cast(kd_pangkat as int)) from spg_pangkat where kd_gol=@kd_gol
			
			IF(@kd_skala_gaji_lama < @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=@kd_skala_gaji_lama+1
				SET @status_pergerakan='Bergulir'
				SET @kd_pangkat_baru=@kd_pangkat
				SET @tmt_grade=@tmt_pangkat
			END
			-- JIKA SG SUDAH MAKSIMAL MAKA GRADE BERUBAH
			ELSE IF(@kd_skala_gaji_lama >= @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=1
				SET @kd_pangkat_baru=(cast(@kd_pangkat as int)+1)
				SET @status_pergerakan='Bergulir'
				SET @tmt_grade=@tmt_grade
				
				--print 'KD_PANGKAT_BARU : '+@kd_pangkat_baru
				--print 'KD_PANGKAT_MAX : '+@kd_pangkat_max
				--print 'TMT_GRADEEE : '+cast(@tmt_grade as varchar(10))
				-- JIKA GRADE & SG SUDAH MAKSIMAL MAKA TIDAK ADA PERUBAHAN
				IF(@kd_pangkat_baru > @kd_pangkat_max)
				BEGIN
					SET @kd_skala_gaji_baru=@kd_skala_gaji_lama
					SET @kd_pangkat_baru=@kd_pangkat_max
					SET @status_pergerakan='Tetap'
					SET @tmt_grade=@tmt_pangkat
				END
			END
	
				
			-- GET MAX ID SPG_RIWAYAT_KEPANGKATAN
			select @id_pang=(max(id)+1) from SPG_RIWAYAT_KEPANGKATAN
			
			-- GET NO URUT SPG_RIWAYAT_KEPANGKATAN 
			select @no_urut_pang_peg=(max(no_urut_pang_peg)+1) from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1'
			
			--select @check_nip=nip from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1' and sts_riwayat_grade='4' and dbo.dateToPeriode(tanggal_buat)=dbo.dateToPeriode(getdate())
			--print 'cek nip : '+@check_nip;
			
			print 'NIP : '+@nip
			print 'SKALA LAMA : '+cast(@kd_skala_gaji_lama as varchar(10))
			print 'SKALA BARU : '+cast(@kd_skala_gaji_baru as varchar(10))
			print 'PANGKAT LAMA : '+@kd_pangkat
			print 'PANGKAT BARU : '+@kd_pangkat_baru
			print 'TMT GRADE : '+cast(@tmt_grade as varchar(10))

			--IF(@check_nip is null or @check_nip = '')
			--BEGIN
				IF(@status_pergerakan = 'Bergulir')
				BEGIN
					-- INSERT RIWAYAT SPG_RIWAYAT_KEPANGKATAN
					insert into SPG_RIWAYAT_KEPANGKATAN (NIP,NO_URUT_PANG_PEG,KD_PANGKAT,TMT_PANG_PEG,ID,KD_STATUS_VALIDASI,TANGGAL_BUAT,NAMA_BUAT,STS_RIWAYAT_GRADE,KD_SKALA_GAJI,TMT_GOL,NO_SK,TGL_SK)
					values (@nip,@no_urut_pang_peg,@kd_pangkat,@tmt_grade,@id_pang,'1',getdate(),'SYSTEM','4',@kd_skala_gaji_baru,@tmt_gol,@nip+'/'+substring(convert(varchar(10),@tmt_pangkat,105),1,5)+'-'+cast(YEAR(getdate()) as varchar(4)),cast(YEAR(getdate()) as varchar(4))+'-'+@tgl_grade)

					insert into spg_sk (no_sk,jns_sk,tgl_sk,nip) values (@nip+'/'+substring(convert(varchar(10),@tmt_pangkat,105),1,5)+'-'+cast(YEAR(getdate()) as varchar(4)),'02',cast(YEAR(getdate()) as varchar(4))+'-'+@tgl_grade,@nip)
				END

				-- INSERT LOG TBL_LOG_PERGERAKAN_SG_PEG
				insert into tbl_log_pergerakan_sg_peg (NIP,KD_SKALA_GAJI_LAMA,KD_SKALA_GAJI_BARU,KD_PANGKAT_LAMA,KD_JABATAN_STR,KD_UNIT_ORG,TMT_GRADE,STATUS,CTIME,MTIME,KD_PANGKAT_BARU,TMT_GOL)
				values (@nip,@kd_skala_gaji_lama,@kd_skala_gaji_baru,@kd_pangkat,@kd_jabatan_str,@kd_unit_org,@tmt_grade,@status_pergerakan,getdate(),getdate(),@kd_pangkat_baru,@tmt_gol)
			--END

	    fetch next from cur into @id
	END
	
	close cur
	deallocate cur

END







GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji_bak]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[pergerakan_skala_gaji_bak]
AS
BEGIN

	DECLARE 
	@id int
	
	--SPG_DATA_CURRENT
	DECLARE
	@nip varchar(10),
	@tmt_pangkat date,
	@tmt_gol date,
	@kd_skala_gaji_lama int,
	@kd_skala_gaji_baru int,
	@tgl_grade varchar(5),
  @tgl_skg varchar(5),
	@kd_pangkat varchar(2),
	@kd_gol varchar(2),
	@kd_jabatan_str varchar(3),
	@kd_unit_org varchar(24),
	@status_pergerakan varchar(20)
	
	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@no_urut_pang_peg int,
	@id_pang int,
	@sts_riwayat_grade varchar(2),
	@check_nip varchar(10)
	
	--SPG_SKALA_GAJI
	DECLARE
	@kd_skala_gaji_max int
	
	declare cur CURSOR LOCAL for
	    /*select id
			from spg_data_current where sts_pensiun='0' 
			and tmt_pangkat<=getdate()
			and year(tmt_pangkat)=(substring(CONVERT(VARCHAR(10),GETDATE(),21),1,4)-1)
			and month(tmt_pangkat)=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2)
			and kd_status_kepeg in ('100','300')*/
			select a.id
			from spg_data_current a
			left join (SELECT * FROM(
				SELECT nip, tmt_pang_peg, sts_riwayat_grade, kd_skala_gaji, 
				case when (tmt_pang_peg is null or tmt_pang_peg = '1900-01-01') then 0
					else ROUND(CAST(DATEDIFF(DAY, tmt_pang_peg, GETDATE()) / 365.2425 AS FLOAT),0)
				end as md_pangkat, 
				tanggal_buat, row_number() OVER (partition BY nip ORDER BY tmt_pang_peg,tanggal_buat DESC) AS rn
				FROM SPG_RIWAYAT_KEPANGKATAN
				WHERE kd_status_validasi = '1' and sts_riwayat_grade <> '6'
			)x WHERE rn = 1) b on a.nip=b.nip and a.tmt_pangkat=b.tmt_pang_peg
			where a.sts_pensiun='0' 
			and month(a.tmt_pangkat)=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2)
			and a.kd_status_kepeg in ('100','300')
			and b.md_pangkat>='1'
			and dbo.dateToPeriode(b.tanggal_buat) < dbo.dateToPeriode(getdate())
	open cur
	
	fetch next from cur into @id
	
	while @@FETCH_STATUS = 0 
	BEGIN
		  --print @id;
			select @nip=nip, @tmt_gol=tmt_gol, @tmt_pangkat=tmt_pangkat, @kd_skala_gaji_lama=kd_skala_gaji, 
			@kd_pangkat=kd_pangkat, @kd_jabatan_str=kd_jabatan_str, @kd_unit_org=kd_unit_org,
			@tgl_grade=substring(cast(tmt_pangkat as varchar(10)),6,5),
			@tgl_skg=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,5)
			from spg_data_current where id=@id
			
			-- GET KD_GOL SPG_PANGKAT
			select @kd_gol=kd_gol from spg_pangkat where kd_pangkat=@kd_pangkat
			
			-- GET BATAS MAKSIMAL KD_SKALA_GAJI 
			select @kd_skala_gaji_max=max(skala_gaji) from spg_skala_gaji where kd_gol=@kd_gol
			
			IF(@kd_skala_gaji_lama < @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=@kd_skala_gaji_lama+1
				SET @status_pergerakan='Bergulir'
			END
			ELSE IF(@kd_skala_gaji_lama >= @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=@kd_skala_gaji_lama
				SET @status_pergerakan='Tetap'
			END
				
			-- GET MAX ID SPG_RIWAYAT_KEPANGKATAN
			select @id_pang=(max(id)+1) from SPG_RIWAYAT_KEPANGKATAN
			
			-- GET NO URUT SPG_RIWAYAT_KEPANGKATAN 
			select @no_urut_pang_peg=(max(no_urut_pang_peg)+1) from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1'
			
			--select @check_nip=nip from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1' and sts_riwayat_grade='4' and dbo.dateToPeriode(tanggal_buat)=dbo.dateToPeriode(getdate())
			--print 'cek nip : '+@check_nip;
			

			--IF(@check_nip is null or @check_nip = '')
			--BEGIN
				IF(@status_pergerakan = 'Bergulir')
				BEGIN
					-- INSERT RIWAYAT SPG_RIWAYAT_KEPANGKATAN
					insert into SPG_RIWAYAT_KEPANGKATAN (NIP,NO_URUT_PANG_PEG,KD_PANGKAT,TMT_PANG_PEG,ID,KD_STATUS_VALIDASI,TANGGAL_BUAT,NAMA_BUAT,STS_RIWAYAT_GRADE,KD_SKALA_GAJI,TMT_GOL,NO_SK,TGL_SK)
					values (@nip,@no_urut_pang_peg,@kd_pangkat,@tmt_pangkat,@id_pang,'1',getdate(),'SYSTEM','4',@kd_skala_gaji_baru,@tmt_gol,@nip+'/'+substring(convert(varchar(10),@tmt_pangkat,105),1,5)+'-'+cast(YEAR(getdate()) as varchar(4)),cast(YEAR(getdate()) as varchar(4))+'-'+@tgl_grade)
				END

				-- INSERT LOG TBL_LOG_PERGERAKAN_SG_PEG
				insert into tbl_log_pergerakan_sg_peg (NIP,KD_SKALA_GAJI_LAMA,KD_SKALA_GAJI_BARU,KD_PANGKAT_LAMA,KD_JABATAN_STR,KD_UNIT_ORG,TMT_GRADE,STATUS,CTIME,MTIME)
				values (@nip,@kd_skala_gaji_lama,@kd_skala_gaji_baru,@kd_pangkat,@kd_jabatan_str,@kd_unit_org,@tmt_pangkat,@status_pergerakan,getdate(),getdate())
			--END

	    fetch next from cur into @id
	END
	
	close cur
	deallocate cur

END





GO
/****** Object:  StoredProcedure [dbo].[pergerakan_skala_gaji_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[pergerakan_skala_gaji_bak_20160219]
AS
BEGIN

	DECLARE 
	@id int
	
	--SPG_DATA_CURRENT
	DECLARE
	@nip varchar(10),
	@tmt_pangkat date,
	@tmt_gol date,
	@kd_skala_gaji_lama int,
	@kd_skala_gaji_baru int,
	@tgl_grade varchar(5),
  @tgl_skg varchar(5),
	@kd_pangkat varchar(2),
	@kd_gol varchar(2),
	@kd_jabatan_str varchar(3),
	@kd_unit_org varchar(24),
	@status_pergerakan varchar(20),
	@kd_pangkat_baru varchar(3),
	@tmt_grade date
	

	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@no_urut_pang_peg int,
	@id_pang int,
	@sts_riwayat_grade varchar(2),
	@check_nip varchar(10)
	
	
	--SPG_SKALA_GAJI
	DECLARE
	@kd_skala_gaji_max int

	--SPG_PANGKAT
	DECLARE
	@kd_pangkat_max varchar(3)

	declare cur CURSOR LOCAL for
	    /*select id
			from spg_data_current where sts_pensiun='0' 
			and tmt_pangkat<=getdate()
			and year(tmt_pangkat)=(substring(CONVERT(VARCHAR(10),GETDATE(),21),1,4)-1)
			and month(tmt_pangkat)=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2)
			and kd_status_kepeg in ('100','300')*/
			select a.id
			from spg_data_current a
			left join (SELECT * FROM(
				SELECT nip, tmt_pang_peg, sts_riwayat_grade, kd_skala_gaji, 
				case when (tmt_pang_peg is null or tmt_pang_peg = '1900-01-01') then 0
					else ROUND(CAST(DATEDIFF(DAY, tmt_pang_peg, GETDATE()) / 365.2425 AS FLOAT),0)
				end as md_pangkat, 
				tanggal_buat, row_number() OVER (partition BY nip ORDER BY tmt_pang_peg,tanggal_buat DESC) AS rn
				FROM SPG_RIWAYAT_KEPANGKATAN
				WHERE kd_status_validasi = '1' and sts_riwayat_grade <> '6'
			)x WHERE rn = 1) b on a.nip=b.nip and a.tmt_pangkat=b.tmt_pang_peg
			where a.sts_pensiun='0' 
			and month(a.tmt_pangkat)=cast(substring(CONVERT(VARCHAR(10),GETDATE(),21),6,2) as int)
			and day(a.tmt_pangkat)=cast(substring(CONVERT(VARCHAR(10),GETDATE(),21),9,2) as int)
			and a.kd_status_kepeg in ('100','300')
			and b.md_pangkat>='1'
			and dbo.dateToPeriode(b.tanggal_buat) < dbo.dateToPeriode(getdate())
	open cur
	
	fetch next from cur into @id
	
	while @@FETCH_STATUS = 0 
	BEGIN
		  --print @id;
			select @nip=nip, @tmt_gol=tmt_gol, @tmt_pangkat=tmt_pangkat, @kd_skala_gaji_lama=kd_skala_gaji, 
			@kd_pangkat=kd_pangkat, @kd_jabatan_str=kd_jabatan_str, @kd_unit_org=kd_unit_org,
			@tgl_grade=substring(cast(tmt_pangkat as varchar(10)),6,5),
			@tgl_skg=substring(CONVERT(VARCHAR(10),GETDATE(),21),6,5),
			@tmt_grade=cast((cast(year(getdate()) as varchar(4))+'-'+substring(cast(tmt_pangkat as varchar(10)),6,2)+'-01') as date)
			from spg_data_current where id=@id
			
			-- GET KD_GOL SPG_PANGKAT
			select @kd_gol=kd_gol from spg_pangkat where kd_pangkat=@kd_pangkat
			
			-- GET BATAS MAKSIMAL KD_SKALA_GAJI 
			select @kd_skala_gaji_max=max(skala_gaji) from spg_skala_gaji where kd_gol=@kd_gol
			
			-- GET BATAS MAKSIMAL KD_PANGKAT 
			select @kd_pangkat_max=max(cast(kd_pangkat as int)) from spg_pangkat where kd_gol=@kd_gol
			
			IF(@kd_skala_gaji_lama < @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=@kd_skala_gaji_lama+1
				SET @status_pergerakan='Bergulir'
				SET @kd_pangkat_baru=@kd_pangkat
				SET @tmt_grade=@tmt_pangkat
			END
			-- JIKA SG SUDAH MAKSIMAL MAKA GRADE BERUBAH
			ELSE IF(@kd_skala_gaji_lama >= @kd_skala_gaji_max)
			BEGIN
				SET @kd_skala_gaji_baru=1
				SET @kd_pangkat_baru=(cast(@kd_pangkat as int)+1)
				SET @status_pergerakan='Bergulir'
				SET @tmt_grade=@tmt_grade
				
				--print 'KD_PANGKAT_BARU : '+@kd_pangkat_baru
				--print 'KD_PANGKAT_MAX : '+@kd_pangkat_max
				--print 'TMT_GRADEEE : '+cast(@tmt_grade as varchar(10))
				-- JIKA GRADE & SG SUDAH MAKSIMAL MAKA TIDAK ADA PERUBAHAN
				IF(@kd_pangkat_baru > @kd_pangkat_max)
				BEGIN
					SET @kd_skala_gaji_baru=@kd_skala_gaji_lama
					SET @kd_pangkat_baru=@kd_pangkat_max
					SET @status_pergerakan='Tetap'
					SET @tmt_grade=@tmt_pangkat
				END
			END
	
				
			-- GET MAX ID SPG_RIWAYAT_KEPANGKATAN
			select @id_pang=(max(id)+1) from SPG_RIWAYAT_KEPANGKATAN
			
			-- GET NO URUT SPG_RIWAYAT_KEPANGKATAN 
			select @no_urut_pang_peg=(max(no_urut_pang_peg)+1) from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1'
			
			--select @check_nip=nip from SPG_RIWAYAT_KEPANGKATAN where nip=@nip and kd_status_validasi='1' and sts_riwayat_grade='4' and dbo.dateToPeriode(tanggal_buat)=dbo.dateToPeriode(getdate())
			--print 'cek nip : '+@check_nip;
			
			print 'NIP : '+@nip
			print 'SKALA LAMA : '+cast(@kd_skala_gaji_lama as varchar(10))
			print 'SKALA BARU : '+cast(@kd_skala_gaji_baru as varchar(10))
			print 'PANGKAT LAMA : '+@kd_pangkat
			print 'PANGKAT BARU : '+@kd_pangkat_baru
			print 'TMT GRADE : '+cast(@tmt_grade as varchar(10))

			--IF(@check_nip is null or @check_nip = '')
			--BEGIN
				IF(@status_pergerakan = 'Bergulir')
				BEGIN
					-- INSERT RIWAYAT SPG_RIWAYAT_KEPANGKATAN
					insert into SPG_RIWAYAT_KEPANGKATAN (NIP,NO_URUT_PANG_PEG,KD_PANGKAT,TMT_PANG_PEG,ID,KD_STATUS_VALIDASI,TANGGAL_BUAT,NAMA_BUAT,STS_RIWAYAT_GRADE,KD_SKALA_GAJI,TMT_GOL,NO_SK,TGL_SK)
					values (@nip,@no_urut_pang_peg,@kd_pangkat,@tmt_grade,@id_pang,'1',getdate(),'SYSTEM','4',@kd_skala_gaji_baru,@tmt_gol,@nip+'/'+substring(convert(varchar(10),@tmt_pangkat,105),1,5)+'-'+cast(YEAR(getdate()) as varchar(4)),cast(YEAR(getdate()) as varchar(4))+'-'+@tgl_grade)
				END

				-- INSERT LOG TBL_LOG_PERGERAKAN_SG_PEG
				insert into tbl_log_pergerakan_sg_peg (NIP,KD_SKALA_GAJI_LAMA,KD_SKALA_GAJI_BARU,KD_PANGKAT_LAMA,KD_JABATAN_STR,KD_UNIT_ORG,TMT_GRADE,STATUS,CTIME,MTIME,KD_PANGKAT_BARU,TMT_GOL)
				values (@nip,@kd_skala_gaji_lama,@kd_skala_gaji_baru,@kd_pangkat,@kd_jabatan_str,@kd_unit_org,@tmt_grade,@status_pergerakan,getdate(),getdate(),@kd_pangkat_baru,@tmt_gol)
			--END

	    fetch next from cur into @id
	END
	
	close cur
	deallocate cur

END





GO
/****** Object:  StoredProcedure [dbo].[PrintLongString]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Yulius
-- Create date: 16 Juni 2009
-- Description:	Print String yang sangat panjang
-- =============================================
CREATE PROCEDURE [dbo].[PrintLongString]
@str VARCHAR(MAX) 
AS
BEGIN

DECLARE @count INT, @i INT
SELECT @count = CAST((LEN(@str) / 4000) AS INT) + 1

SELECT @i = 0
WHILE @count > @i
BEGIN
	PRINT SUBSTRING(@str,@i * 4000,4000)
	SET @i = @i + 1

END 

End


GO
/****** Object:  StoredProcedure [dbo].[proc_generate_random_string]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- Generate random string (dengan usaha agar bisa dibaca, disusun vokal dan konsonan)
-- by Pribadi Pamungkas. 2020-05-15

CREATE PROCEDURE [dbo].[proc_generate_random_string]
	@pi_min TINYINT = 5,
	@pi_max TINYINT = 20,
	@po_text VARCHAR(50) OUTPUT
AS
BEGIN
	
	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	DECLARE
		@text nvarchar(255) = '',
		@length int,
		@i int = 0;

	SET @length = RAND() * (@pi_max - @pi_min) + @pi_min
	IF @length>@pi_max SET @length = @pi_max

	WHILE (@i < @length)
	BEGIN

		WITH letters as
		(
		  SELECT 'BCDFGHJKLMNPQRSTVWXYZ' c, 'AEIOU' v
		),
		CTE as
		(
			SELECT
				CASE
					WHEN CRYPT_GEN_RANDOM(1) % 2 = 1 THEN SUBSTRING(v, CAST(rand()*5 as int)+1, 1)
					ELSE SUBSTRING(c, CAST(rand()*21 as int)+1, 1)
				END AS x
			FROM
				letters
		)
		SELECT @text = @text + x FROM CTE

		SET @i = @i + 1
	END

	SET @po_text = @text

END
GO
/****** Object:  StoredProcedure [dbo].[proc_insert_presensi_upacara]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[proc_insert_presensi_upacara]
	@pi_nip VARCHAR(10),
	@pi_tanggal_presensi DATETIME,
	@pi_latitude VARCHAR(20),
	@pi_longitude VARCHAR(20),
	@po_status BIT OUTPUT,
	@po_message VARCHAR(200) OUTPUT
AS
BEGIN

	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--DECLARE
	--	@po_status BIT = 0,
	--	@po_message VARCHAR(200)

	DECLARE
		@NM_PEG VARCHAR(100), 
		@kode_satker_organisasi VARCHAR(300), 
		@nama_satker_organisasi VARCHAR(300), 
		@KD_JABATAN_STR VARCHAR(3), 
		@NM_JABATAN VARCHAR(200),
		@status_upacara VARCHAR(10),
		@keterangan_upacara VARCHAR(200)

	-- cari data pegawai
	SELECT 
		@NM_PEG = sdc.NM_PEG, 
		@kode_satker_organisasi = uo.kode_satker_organisasi, 
		@nama_satker_organisasi = uo.nama_satker_organisasi, 
		@KD_JABATAN_STR = sdc.KD_JABATAN_STR, 
		@NM_JABATAN = sdc.NM_JABATAN
	FROM
		SPG_DATA_CURRENT AS sdc 
		INNER JOIN
		MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 AS uo ON sdc.KD_UNIT_ORG = uo.KD_UNIT_ORG 
	WHERE 
		sdc.NIP = @pi_nip
	IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
		BEGIN
			SET @po_status = 0
			SET @po_message = 'NIP tidak ditemukan'
			GOTO END_RESULT
		END

	-- validasi pengaturan upacara
	IF NOT EXISTS(
				SELECT
					*
				FROM
					dbo.SPG_PENGATURAN_ABSENSI_UPACARA
				WHERE
					TANGGAL = CAST(@pi_tanggal_presensi AS DATE)
			)
		BEGIN
			--PRINT 'Upacara tidak dikenal'
			SET @po_status = 0
			SET @po_message = 'Tidak ada upacara untuk hari ini'
		END
	ELSE
		BEGIN
			DECLARE
				@ID_PENGATURAN INT, 
				@NAMA VARCHAR(200), 
				@TANGGAL DATE, 
				@BATAS_TIDAK_TELAT VARCHAR(10), 
				@BATAS_AWAL VARCHAR(10), 
				@BATAS_AKHIR VARCHAR(10),
				@LOKASI_KERJA INT,
				@GEO1 GEOGRAPHY,
				@LokasiId NVARCHAR(50),
				@NamaLokasi VARCHAR(50)

			SELECT
				@ID_PENGATURAN = ID_PENGATURAN, 
				@NAMA = NAMA, 
				@TANGGAL = TANGGAL, 
				@BATAS_TIDAK_TELAT = BATAS_TIDAK_TELAT, 
				@BATAS_AWAL = BATAS_AWAL, 
				@BATAS_AKHIR = BATAS_AKHIR,
				@LOKASI_KERJA = LOKASI_KERJA
			FROM
				dbo.SPG_PENGATURAN_ABSENSI_UPACARA
			WHERE
				TANGGAL = CAST(@pi_tanggal_presensi AS DATE)

			IF @pi_tanggal_presensi > CAST(CONVERT(VARCHAR, @TANGGAL, 23) + ' ' + COALESCE(@BATAS_AKHIR, '00:00:00') AS DATETIME)
				BEGIN
					--PRINT 'batas presensi telah berakhir'
					SET @po_status = 0
					SET @po_message = 'Batas presensi telah berakhir'
				END
			ELSE IF @pi_tanggal_presensi < CAST(CONVERT(VARCHAR, @TANGGAL, 23) + ' ' + COALESCE(@BATAS_AWAL, '00:00:00') AS DATETIME)
				BEGIN
					--PRINT 'anda belum bisa melakukan presensi'
					SET @po_status = 0
					SET @po_message = 'Presensi belum bisa dilakukan'
				END
			ELSE 
				BEGIN
					DECLARE
						@last_lokasi VARCHAR(200),
						@last_presensi VARCHAR(5)
					SELECT
						@last_lokasi = LOKASI_UPACARA,
						--@last_presensi = TANGGAL_UPACARA
						@last_presensi = LEFT(CAST(CONVERT(VARCHAR(8), TANGGAL_UPACARA, 108) AS VARCHAR(8)),5)
					FROM
						dbo.SPG_REPORT_ABSENSI_UPACARA
					WHERE
						NIP = @pi_nip
						AND CAST(TANGGAL_UPACARA AS DATE) = CAST(@pi_tanggal_presensi AS DATE) 
					IF (@@ROWCOUNT = 0)
					--IF NOT EXISTS(SELECT
					--					*
					--				FROM
					--					dbo.SPG_REPORT_ABSENSI_UPACARA
					--				WHERE
					--					NIP = @pi_nip
					--					AND CAST(TANGGAL_UPACARA AS DATE) = CAST(@pi_tanggal_presensi AS DATE) 
					--				)
						BEGIN

							IF @pi_tanggal_presensi > CAST(CONVERT(VARCHAR, @TANGGAL, 23) + ' ' + COALESCE(@BATAS_TIDAK_TELAT, '00:00:00') AS DATETIME) 
								--print 'telat'
								SET @status_upacara = 'TERLAMBAT'
							ELSE
								--print 'oke'
								SET @status_upacara = 'HADIR'

							SET @geo1= geography::Point(@pi_latitude, @pi_longitude, 4326)
							SELECT TOP 1 -- pakai top 1 agar menghasilkan lokasi yang terdekat saja
								@LokasiId = LokasiId, 
								@NamaLokasi = NamaLokasi
								--LEFT(CONVERT(VARCHAR,(@geo1.STDistance(geography::Point(ISNULL(Latitude,0), ISNULL(Longitude,0), 4326))/1000)),5)+' Km' as DISTANCE ,
								--LEFT(CONVERT(VARCHAR,(@geo1.STDistance(geography::Point(ISNULL(Latitude,0), ISNULL(Longitude,0), 4326)))),5)+' m' as DISTANCE_m,
								--@geo1.STDistance(geography::Point(ISNULL(Latitude,0), ISNULL(Longitude,0), 4326))
							FROM 
								dbo.LOKASI
							WHERE
								@geo1.STDistance(geography::Point(ISNULL(Latitude,0), ISNULL(Longitude,0), 4326)) <= Radius
								--AND KDJenisLokasi = '3'
								AND IsActive = '1'
								AND IsDeleted = '0'
								AND LokasiId IN (SELECT ID_LOKASI FROM SPG_PENGATURAN_ABSENSI_UPACARA_LOKASI WHERE ID_PENGATURAN = @ID_PENGATURAN AND AKTIF = 1)
							ORDER BY
								@geo1.STDistance(geography::Point(ISNULL(Latitude,0), ISNULL(Longitude,0), 4326))
							IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
								BEGIN
									SET @po_status = 0
									SET @po_message = 'Anda belum berada di lokasi upacara.'
								END
							ELSE
								BEGIN
									-- oke

									SET @keterangan_upacara = @pi_longitude + ';' + @pi_latitude

									INSERT INTO [dbo].[SPG_REPORT_ABSENSI_UPACARA]
											   ([ID_PENGATURAN_UPACARA]
											   ,[NAMA_UPACARA]
											   ,[LOKASI_UPACARA]
											   ,[TANGGAL_UPACARA]
											   ,[NIP]
											   ,[NM_PEG]
											   ,[KD_SATKER]
											   ,[SATKER]
											   ,[KD_LEVEL_JABATAN]
											   ,[LEVEL_JABATAN]
											   ,[STATUS]
											   ,[KETERANGAN])
										 VALUES
											   (@ID_PENGATURAN
											   ,@NAMA
											   ,@NamaLokasi
											   ,@pi_tanggal_presensi
											   ,@pi_nip
											   ,@NM_PEG
											   ,@kode_satker_organisasi
											   ,@nama_satker_organisasi
											   ,@KD_JABATAN_STR
											   ,@NM_JABATAN
											   ,@status_upacara
											   ,@keterangan_upacara)
									IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
										BEGIN
											SET @po_status = 0
											SET @po_message = 'Gagal melakukan presensi. Mohon coba kembali.'
										END

									SET @po_status = 1
									SET @po_message = 'Berhasil melakukan presensi upacara di ' + @NamaLokasi + ' pada pukul ' + LEFT(CAST(CONVERT(VARCHAR(8), @pi_tanggal_presensi, 108) AS VARCHAR(8)),5)
								END
						END
					ELSE
						BEGIN
							SET @po_status = 1
							--SET @po_message = 'Anda telah berhasil melakukan presensi sebelumnya.'
							SET @po_message = 'Presensi upacara Anda telah tercatat di ' + @last_lokasi + ' pada pukul ' + @last_presensi
						END
				END

		END

		END_RESULT:

		SELECT @po_status AS status, @po_message AS message

		IF @po_status = 1
			RETURN 0
		ELSE
			RETURN -1
END
GO
/****** Object:  StoredProcedure [dbo].[proc_list_surat_tugas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- by Pribadi Pamungkas. 2020-12-23
-- last modified by Pribadi Pamungkas. 2020-12-30 => perubahan banyak logic

CREATE PROCEDURE [dbo].[proc_list_surat_tugas]
	@pi_nip VARCHAR(6),
	@pi_nama VARCHAR(100),
	@pi_no_surat VARCHAR(100),	-- 'ST-23/PM.1/2016'
	@pi_lokasi VARCHAR(255),
	@pi_sender VARCHAR(6),		-- '01845',	--'H02935',	--'03313',
	@pi_limit SMALLINT,
	@pi_offset SMALLINT
AS
BEGIN
	
	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	---- GET HAK AKSES ----

	DECLARE @hak_akses VARCHAR(20)
	DECLARE @list_nip TABLE (nip VARCHAR(6), nm_peg VARCHAR(100))

	SELECT
		@hak_akses = ia.WIA_INQUIRYACCESS
	FROM
		dbo.TBL_USER AS tu
		INNER JOIN
		dbo.WF_INQUIRY_ACCESS AS ia ON tu.WIA_ACCESSNAME = ia.WIA_ACCESSNAME
	WHERE
		ia.WIA_INQUIRYNAME = 'UPK'
		AND tu.NIP = @pi_sender

	IF COALESCE(@hak_akses, '') = ''
		INSERT INTO @list_nip
		SELECT 
			NIP,
			NM_PEG
		FROM
			dbo.SPG_DATA_CURRENT
		WHERE
			STS_PENSIUN = '0'
			AND NIP = @pi_sender
	ELSE 
		BEGIN

			INSERT INTO @list_nip
			SELECT DISTINCT
				RTRIM(LTRIM(LEFT(list.NIP,6))),
				NM_PEG
			FROM
				(
					SELECT 
						satker.NIP,
						satker.NM_PEG
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1 INNER JOIN
						dbo.SPG_08_UNIT_ORGANISASI AS suo ON sdc.KD_UNIT_ORG = suo.KD_UNIT_ORG INNER JOIN
						(
							SELECT
								sdc2.NIP,
								sdc2.NM_PEG,
								suo2.kode_satker_organisasi
							FROM
								dbo.SPG_DATA_CURRENT AS sdc2 INNER JOIN
								dbo.SPG_08_UNIT_ORGANISASI AS suo2 ON sdc2.KD_UNIT_ORG = suo2.KD_UNIT_ORG
							WHERE
								sdc2.STS_PENSIUN = '0'
						) AS satker ON suo.kode_satker_organisasi = satker.kode_satker_organisasi
					WHERE
						h.Item = 'lokal'
						AND sdc.STS_PENSIUN = '0'
						AND sdc.NIP = @pi_sender
						AND (satker.NIP = @pi_nip OR ISNULL(@pi_nip, '') = '')
						AND (satker.NM_PEG LIKE '%'+@pi_nama+'%' OR ISNULL(@pi_nama, '') = '')

					UNION ALL

					SELECT 
						sdc2.NIP,
						sdc2.NM_PEG
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1 INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc2 ON LEFT(sdc.KD_UNIT_ORG, 3) = LEFT(sdc2.KD_UNIT_ORG, 3)
					WHERE
						h.Item = 'wilayah'
						AND sdc.NIP = @pi_sender
						AND sdc.STS_PENSIUN = '0'
						AND sdc2.STS_PENSIUN = '0'
						AND (sdc2.NIP = @pi_nip OR ISNULL(@pi_nip, '') = '')
						AND (sdc2.NM_PEG LIKE '%'+@pi_nama+'%' OR ISNULL(@pi_nama, '') = '')

					UNION ALL

					SELECT 
						sdc.NIP,
						sdc.NM_PEG
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1
					WHERE
						h.Item = 'pusat'
						AND sdc.STS_PENSIUN = '0'
						AND (sdc.NIP = @pi_nip OR ISNULL(@pi_nip, '') = '')
						AND (sdc.NM_PEG LIKE '%'+@pi_nama+'%' OR ISNULL(@pi_nama, '') = '')
				) AS list

		END



	---- DATA ST ----
	SELECT
		*
	FROM
		(
			SELECT
				ROW_NUMBER() OVER (ORDER BY l.TGL_SURAT_DATE DESC) AS row_num,
				l.stu_id,
				l.kso_kasusnomor,
				l.no_surat,
				l.tgl_surat,
				l.keterangan_surat_tugas,
				l.lokasi_surat_tugas,
				l.jenis_surat_tugas,
				l.nip_pembuat_st,
				l.nama_pembuat_st,
				l.satker_pembuat_st,
				l.nip_pengubah_st,
				l.nama_pengubah_st,
				l.nip_ttd_st,
				l.nama_ttd_st,
				l.jabatan_ttd_st,
				l.jml_pst_st,
				l.list_pst
			FROM
				(
					SELECT DISTINCT
						sst.STU_ID AS stu_id,
						sst.KSO_KASUSNOMOR AS kso_kasusnomor,
						sst.TYPE,
						sst.NO_SURAT AS no_surat,
						sst.TGL_SURAT AS TGL_SURAT_DATE,
						dbo.func_datetostring(sst.TGL_SURAT) AS tgl_surat,
						CAST(sst.KETERANGAN AS VARCHAR(MAX)) AS keterangan_surat_tugas,
						sst.LOKASI AS lokasi_surat_tugas,
						'' AS jenis_surat_tugas,
						sst.NAMA_BUAT AS nip_pembuat_st,
						pembuat_st.nm_peg_pembuat AS nama_pembuat_st,
						pembuat_st.satker_pembuat AS satker_pembuat_st,
						sst.NAMA_UPDATE AS nip_pengubah_st,
						pengubah_st.NM_PEG AS nama_pengubah_st,
						sst.NIP_TTD AS nip_ttd_st,
						ttd_st.NM_PEG AS nama_ttd_st,
						d.POSISI AS jabatan_ttd_st,
						jmlpst.jml AS jml_pst_st,
						listpst.pst AS list_pst
					FROM
						SPG_SURAT_TUGAS AS sst 
						INNER JOIN
						(
							SELECT DISTINCT
								STU_ID
							FROM
								(
									SELECT STU_ID FROM SPG_SURAT_TUGAS AS sst INNER JOIN @list_nip AS list ON sst.NAMA_BUAT = list.nip WHERE COALESCE(sst.STATUS, '0') <> '0'
									UNION ALL
									SELECT STU_ID FROM SPG_SURAT_TUGAS AS sst INNER JOIN @list_nip AS list ON sst.NAMA_UPDATE = list.nip WHERE COALESCE(sst.STATUS, '0') <> '0'
									UNION ALL
									SELECT STU_ID FROM SPG_SURAT_TUGAS AS sst INNER JOIN @list_nip AS list ON sst.NIP_TTD = list.nip WHERE COALESCE(sst.STATUS, '0') <> '0'
									UNION ALL
									SELECT STU_ID FROM SPG_PESERTA_SURAT_TUGAS AS spst INNER JOIN @list_nip AS list ON spst.NIP = list.nip WHERE spst.STATUS = '1'
									UNION ALL
									SELECT STU_ID FROM SPG_PESERTA_SURAT_TUGAS AS spst INNER JOIN @list_nip AS list ON spst.NAMA_UPDATE = list.nip WHERE spst.STATUS = '1'
								) AS x
						) AS akses ON sst.STU_ID = akses.STU_ID
						OUTER APPLY
						(
							SELECT STUFF((SELECT ',' + spst2.NIP + ':' + sdc2.NM_PEG + ':' + COALESCE(dbo.func_datetostring(spst2.TGL_MULAI),'') + ':' + COALESCE(dbo.func_datetostring(spst2.TGL_SELESAI),'')
							FROM SPG_PESERTA_SURAT_TUGAS AS spst2 INNER JOIN SPG_DATA_CURRENT AS sdc2 ON spst2.NIP = sdc2.NIP WHERE spst2.STATUS = '1' AND spst2.STU_ID = sst.STU_ID
							FOR XML PATH('')) ,1,1,'') AS pst
						 ) AS listpst
						 OUTER APPLY
						 (
							SELECT COUNT(*) AS jml FROM SPG_PESERTA_SURAT_TUGAS WHERE STATUS = '1' AND STU_ID = sst.STU_ID
						 ) AS jmlpst 
						 LEFT JOIN
						 (
							SELECT
								sdc2.NIP,
								sdc2.NM_PEG AS nm_peg_pembuat,
								suo2.nama_satker_organisasi AS satker_pembuat
							FROM
								dbo.SPG_DATA_CURRENT AS sdc2 INNER JOIN
								dbo.SPG_08_UNIT_ORGANISASI AS suo2 ON sdc2.KD_UNIT_ORG = suo2.KD_UNIT_ORG
							WHERE
								sdc2.STS_PENSIUN = '0'
						) AS pembuat_st ON sst.NAMA_BUAT = pembuat_st.NIP
						LEFT JOIN
						SPG_DATA_CURRENT AS pengubah_st ON sst.NAMA_UPDATE = pengubah_st.NIP
						LEFT JOIN
						SPG_DATA_CURRENT AS ttd_st ON sst.NIP_TTD = ttd_st.NIP
						LEFT JOIN
						(select kd_jabatan,kd_unit_org,max(posisi) POSISI from SPG_08_JABATAN_UNIT  where kd_jabatan != '' and KD_UNIT_ORG != '' group by KD_JABATAN,KD_UNIT_ORG) d
						on ttd_st.KD_JABATAN_STR = d.KD_JABATAN and ttd_st.KD_UNIT_ORG = d.KD_UNIT_ORG
					WHERE
						COALESCE(sst.STATUS, '0') <> '0'
						AND (sst.NO_SURAT LIKE '%'+@pi_no_surat+'%' OR ISNULL(@pi_no_surat, '') = '')
						AND (sst.LOKASI LIKE '%'+@pi_lokasi+'%' OR ISNULL(@pi_lokasi, '') = '')
				) AS l
			--ORDER BY
			--	l.TGL_SURAT_DATE DESC
		) AS lst
	WHERE
		lst.row_num >= @pi_offset
		AND lst.row_num < (@pi_offset + @pi_limit)



	--SELECT
	--	sdc.NM_PEG AS nama,
	--	sdc.NIP AS nip,
	--	--sdc.NM_JABATAN,
	--	sjs.NM_JABATAN_STR AS jabatan,
	--	sdc.NM_GOL AS level_jabatan,
	--	sdc.KD_GRADE AS grade,
	--	bidang.nama_bidang AS bidang,
	--	suo.nama_satker_organisasi AS satuan_kerja,
	--	suo.NM_UNIT_ORG AS unit_kerja,
	--	dbo.MasaDinasTahunBulanDataMart(sdc.TMT_JABATAN,GETDATE()) AS md_jabatan,
	--	cast((datediff(day,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)/365) as varchar(3))+' Thn '+cast(((datediff(month,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)) - ((datediff(day,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)/365)*12)) as varchar(3))+' Bln' AS md_satuan_kerja,
	--	CONCAT(sdc.masa_tahun_ojk, ' Thn ', sdc.masa_bulan_ojk, ' Bln') AS md_ojk,
	--	--cast((datediff(day,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)/365) as varchar(3))+' Thn '+cast(((datediff(month,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)) - ((datediff(day,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)/365)*12)) as varchar(3))+' Bln' as masa_dinas_ojk,
	--	CASE 
	--		WHEN sdc.KD_STATUS_KEPEG = '200' THEN dbo.MasaDinasTahunBulanDataMart(GETDATE(), sdc.TGL_SELESAI_KONTRAK) 
	--		ELSE dbo.MasaDinasTahunBulanDataMart(GETDATE(), sdc.TMT_PENSIUN) 
	--	END AS sisa_md,
	--	dbo.MasaDinasTahunBulanDataMart(sdc.TGL_LAHIR_PEG,GETDATE()) AS usia,
	--	--st.*
	--	'' AS sanksi,
	--	'' AS no_telepon,
	--	'' AS no_ekstension,
	--	'' AS email,
	--	'' AS keahlian,
	--	'' AS aktualisasi_diri,
	--	'' AS status_pegawai,
	--	'' AS md_lembaga_asal,
	--	'' AS md_level_jabatan,
	--	'' AS tmt_berakhir_kontrak,
	--	'' AS tmt_pensiun,
	--	'' AS posisi,
	--	'' AS nm_agama,
	--	'' AS sts_perkawinan,
	--	'' AS nm_jns_kelamin_peg,
	--	'' AS nm_propinsi_homebase,
	--	'' AS ket_lembaga_asal,
	--	'' AS tmt_lembaga_asal,
	--	'' AS tmt_level_jabatan,
	--	'' AS tmt_jabatan,
	--	'' AS tmt_satker,
	--	'' AS email_pribadi,
	--	'' AS handphone_1,
	--	'' AS handphone_2,
	--	'' AS peserta_surat_tugas,
	--	st.STU_ID AS stu_id,
	--	st.KSO_KASUSNOMOR AS kso_kasusnomor,
	--	st.NO_SURAT AS no_surat,
	--	dbo.func_datetostring(st.TGL_MULAI) AS tgl_mulai_surat_tugas,
	--	dbo.func_datetostring(st.TGL_SELESAI) AS tgl_selesai_surat_tugas,
	--	st.KETERANGAN AS keterangan_surat_tugas,
	--	st.LOKASI AS lokasi_surat_tugas,
	--	'' AS jenis_surat_tugas,
	--	st.NAMA_BUAT AS nip_pembuat_st,
	--	pembuat_st.NM_PEG AS nama_pembuat_st,
	--	'' AS satker_pembuat_st,
	--	st.NAMA_UPDATE AS nip_pengubah_st,
	--	pengubah_st.NM_PEG AS nama_pengubah_st,
	--	st.NIP_TTD AS nip_ttd_st,
	--	ttd_st.NM_PEG AS nama_ttd_st,
	--	st.PST_NAMA_UPDATE AS nip_pengubah_pst_st,
	--	'' AS nama_pengubah_pst_st,
	--	jmlpst.jml AS jml_pst_st,
	--	listpst.pst AS list_pst
	--FROM
	--	@list_nip AS list 
	--	INNER JOIN
	--	SPG_DATA_CURRENT AS sdc ON list.NIP = sdc.NIP
	--	OUTER APPLY
	--	(
	--		SELECT
	--			sst.STU_ID,
	--			sst.KSO_KASUSNOMOR,
	--			sst.TYPE,
	--			sst.NO_SURAT,
	--			sst.TGL_SURAT,
	--			sst.LOKASI,
	--			sst.NAMA_BUAT,
	--			sst.NAMA_UPDATE,
	--			sst.NIP_TTD,
	--			spst.KETERANGAN,
	--			spst.TGL_MULAI,
	--			spst.TGL_SELESAI,
	--			spst.NAMA_UPDATE AS PST_NAMA_UPDATE
	--		FROM
	--			SPG_SURAT_TUGAS AS sst INNER JOIN
	--			SPG_PESERTA_SURAT_TUGAS AS spst ON sst.STU_ID = spst.STU_ID
	--		WHERE
	--			(
	--				(
	--					sst.NAMA_BUAT = sdc.NIP
	--					OR sst.NAMA_UPDATE = sdc.NIP
	--					OR sst.NIP_TTD = sdc.NIP
	--					OR spst.NIP = sdc.NIP
	--					OR spst.NAMA_UPDATE = sdc.NIP
	--				) OR ISNULL(sdc.NIP, '') = ''
	--			)
	--			AND (sst.NO_SURAT LIKE '%'+@pi_no_surat+'%' OR ISNULL(@pi_no_surat, '') = '')
	--	) AS st 
	--	LEFT JOIN
	--	dbo.SPG_08_JABATAN_STR AS sjs ON sdc.KD_JABATAN_STR = sjs.KD_JABATAN_STR 
	--	OUTER APPLY
	--	(
	--		SELECT
	--			NM_UNIT_ORG AS nama_bidang
	--		FROM
	--			dbo.SPG_08_UNIT_ORGANISASI
	--		WHERE
	--			KD_ESELON = '00'
	--			AND LEFT(KD_UNIT_ORG, 3) = LEFT(sdc.KD_UNIT_ORG, 3)
	--	) AS bidang 
	--	INNER JOIN
	--	dbo.SPG_08_UNIT_ORGANISASI AS suo ON sdc.KD_UNIT_ORG = suo.KD_UNIT_ORG
	--	OUTER APPLY
	--	(
	--		SELECT STUFF((SELECT ',' + spst2.NIP + ':' + sdc2.NM_PEG
	--		FROM SPG_PESERTA_SURAT_TUGAS AS spst2 INNER JOIN SPG_DATA_CURRENT AS sdc2 ON spst2.NIP = sdc2.NIP WHERE spst2.STU_ID = st.STU_ID
	--		FOR XML PATH('')) ,1,1,'') AS pst
	--	 ) AS listpst
	--	 OUTER APPLY
	--	 (
	--		SELECT COUNT(*) AS jml FROM SPG_PESERTA_SURAT_TUGAS WHERE STU_ID = st.STU_ID
	--	 ) AS jmlpst 
	--	 LEFT JOIN
	--	 SPG_DATA_CURRENT AS pembuat_st ON st.NAMA_BUAT = pembuat_st.NIP
	--	 LEFT JOIN
	--	 SPG_DATA_CURRENT AS pengubah_st ON st.NAMA_UPDATE = pengubah_st.NIP
	--	 LEFT JOIN
	--	 SPG_DATA_CURRENT AS ttd_st ON st.NIP_TTD = ttd_st.NIP
	--WHERE
	--	(sdc.NIP = @pi_nip OR ISNULL(@pi_nip, '') = '')
	--	AND (sdc.NM_PEG LIKE '%'+@pi_nama+'%' OR ISNULL(@pi_nama, '') = '')
	--	AND st.STU_ID IS NOT NULL
	--ORDER BY
	--	TGL_SURAT DESC

END
GO
/****** Object:  StoredProcedure [dbo].[proc_list_surat_tugas_20201230]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- by Pribadi Pamungkas.2020-12-23

CREATE PROCEDURE [dbo].[proc_list_surat_tugas_20201230]
	@pi_nip VARCHAR(6),
	@pi_nama VARCHAR(100),
	@pi_no_surat VARCHAR(100),	-- 'ST-23/PM.1/2016'
	@pi_sender VARCHAR(6)		-- '01845',	--'H02935',	--'03313',
AS
BEGIN
	
	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	---- GET HAK AKSES ----

	DECLARE @hak_akses VARCHAR(20)
	DECLARE @list_nip TABLE (nip VARCHAR(10))

	SELECT
		@hak_akses = ia.WIA_INQUIRYACCESS
	FROM
		dbo.TBL_USER AS tu
		INNER JOIN
		dbo.WF_INQUIRY_ACCESS AS ia ON tu.WIA_ACCESSNAME = ia.WIA_ACCESSNAME
	WHERE
		ia.WIA_INQUIRYNAME = 'UPK'
		AND tu.NIP = @pi_sender

	IF COALESCE(@hak_akses, '') = ''
		INSERT INTO @list_nip
		SELECT @pi_sender
	ELSE 
		BEGIN
			INSERT INTO @list_nip
			SELECT DISTINCT
				list.NIP
			FROM
				(
					SELECT 
						satker.NIP
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1 INNER JOIN
						dbo.SPG_08_UNIT_ORGANISASI AS suo ON sdc.KD_UNIT_ORG = suo.KD_UNIT_ORG INNER JOIN
						(
							SELECT
								sdc2.NIP,
								suo2.kode_satker_organisasi
							FROM
								dbo.SPG_DATA_CURRENT AS sdc2 INNER JOIN
								dbo.SPG_08_UNIT_ORGANISASI AS suo2 ON sdc2.KD_UNIT_ORG = suo2.KD_UNIT_ORG
							WHERE
								sdc2.STS_PENSIUN = '0'
						) AS satker ON suo.kode_satker_organisasi = satker.kode_satker_organisasi
					WHERE
						h.Item = 'lokal'
						AND sdc.NIP = @pi_sender

					UNION ALL

					SELECT 
						sdc2.NIP
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1 INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc2 ON LEFT(sdc.KD_UNIT_ORG, 3) = LEFT(sdc2.KD_UNIT_ORG, 3)
					WHERE
						h.Item = 'wilayah'
						AND sdc.NIP = @pi_sender
						AND sdc2.STS_PENSIUN = '0'

					UNION ALL

					SELECT 
						sdc.NIP
					FROM 
						dbo.SplitStrings_CTE(@hak_akses, '|') AS h INNER JOIN
						dbo.SPG_DATA_CURRENT AS sdc ON 1 = 1
					WHERE
						h.Item = 'pusat'
						AND sdc.STS_PENSIUN = '0'
				) AS list

		END


	---- DATA ST ----

	SELECT
		sdc.NM_PEG AS nama,
		sdc.NIP AS nip,
		--sdc.NM_JABATAN,
		sjs.NM_JABATAN_STR AS jabatan,
		sdc.NM_GOL AS level_jabatan,
		sdc.KD_GRADE AS grade,
		bidang.nama_bidang AS bidang,
		suo.nama_satker_organisasi AS satuan_kerja,
		suo.NM_UNIT_ORG AS unit_kerja,
		dbo.MasaDinasTahunBulanDataMart(sdc.TMT_JABATAN,GETDATE()) AS md_jabatan,
		cast((datediff(day,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)/365) as varchar(3))+' Thn '+cast(((datediff(month,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)) - ((datediff(day,sdc.tmt_satker,case when isnull(sdc.sts_pensiun,0) <> 1 then getdate() else sdc.tmt_pensiun end)/365)*12)) as varchar(3))+' Bln' AS md_satuan_kerja,
		CONCAT(sdc.masa_tahun_ojk, ' Thn ', sdc.masa_bulan_ojk, ' Bln') AS md_ojk,
		--cast((datediff(day,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)/365) as varchar(3))+' Thn '+cast(((datediff(month,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)) - ((datediff(day,a.tgl_masuk,case when isnull(b.sts_pensiun,0) <> 1 then getdate() else b.tmt_pensiun end)/365)*12)) as varchar(3))+' Bln' as masa_dinas_ojk,
		CASE 
			WHEN sdc.KD_STATUS_KEPEG = '200' THEN dbo.MasaDinasTahunBulanDataMart(GETDATE(), sdc.TGL_SELESAI_KONTRAK) 
			ELSE dbo.MasaDinasTahunBulanDataMart(GETDATE(), sdc.TMT_PENSIUN) 
		END AS sisa_md,
		dbo.MasaDinasTahunBulanDataMart(sdc.TGL_LAHIR_PEG,GETDATE()) AS usia,
		--st.*
		'' AS sanksi,
		'' AS no_telepon,
		'' AS no_ekstension,
		'' AS email,
		'' AS keahlian,
		'' AS aktualisasi_diri,
		'' AS status_pegawai,
		'' AS md_lembaga_asal,
		'' AS md_level_jabatan,
		'' AS tmt_berakhir_kontrak,
		'' AS tmt_pensiun,
		'' AS posisi,
		'' AS nm_agama,
		'' AS sts_perkawinan,
		'' AS nm_jns_kelamin_peg,
		'' AS nm_propinsi_homebase,
		'' AS ket_lembaga_asal,
		'' AS tmt_lembaga_asal,
		'' AS tmt_level_jabatan,
		'' AS tmt_jabatan,
		'' AS tmt_satker,
		'' AS email_pribadi,
		'' AS handphone_1,
		'' AS handphone_2,
		'' AS peserta_surat_tugas,
		st.STU_ID AS stu_id,
		st.KSO_KASUSNOMOR AS kso_kasusnomor,
		st.NO_SURAT AS no_surat,
		dbo.func_datetostring(st.TGL_MULAI) AS tgl_mulai_surat_tugas,
		dbo.func_datetostring(st.TGL_SELESAI) AS tgl_selesai_surat_tugas,
		st.KETERANGAN AS keterangan_surat_tugas,
		st.LOKASI AS lokasi_surat_tugas,
		'' AS jenis_surat_tugas,
		st.NAMA_BUAT AS nip_pembuat_st,
		pembuat_st.NM_PEG AS nama_pembuat_st,
		'' AS satker_pembuat_st,
		st.NAMA_UPDATE AS nip_pengubah_st,
		pengubah_st.NM_PEG AS nama_pengubah_st,
		st.NIP_TTD AS nip_ttd_st,
		ttd_st.NM_PEG AS nama_ttd_st,
		st.PST_NAMA_UPDATE AS nip_pengubah_pst_st,
		'' AS nama_pengubah_pst_st,
		jmlpst.jml AS jml_pst_st,
		listpst.pst AS list_pst
	FROM
		@list_nip AS list 
		INNER JOIN
		SPG_DATA_CURRENT AS sdc ON list.NIP = sdc.NIP
		OUTER APPLY
		(
			SELECT
				sst.STU_ID,
				sst.KSO_KASUSNOMOR,
				sst.TYPE,
				sst.NO_SURAT,
				sst.TGL_SURAT,
				sst.LOKASI,
				sst.NAMA_BUAT,
				sst.NAMA_UPDATE,
				sst.NIP_TTD,
				spst.KETERANGAN,
				spst.TGL_MULAI,
				spst.TGL_SELESAI,
				spst.NAMA_UPDATE AS PST_NAMA_UPDATE
			FROM
				SPG_SURAT_TUGAS AS sst INNER JOIN
				SPG_PESERTA_SURAT_TUGAS AS spst ON sst.STU_ID = spst.STU_ID
			WHERE
				(
					(
						sst.NAMA_BUAT = sdc.NIP
						OR sst.NAMA_UPDATE = sdc.NIP
						OR sst.NIP_TTD = sdc.NIP
						OR spst.NIP = sdc.NIP
						OR spst.NAMA_UPDATE = sdc.NIP
					) OR ISNULL(sdc.NIP, '') = ''
				)
				AND (sst.NO_SURAT LIKE '%'+@pi_no_surat+'%' OR ISNULL(@pi_no_surat, '') = '')
		) AS st 
		LEFT JOIN
		dbo.SPG_08_JABATAN_STR AS sjs ON sdc.KD_JABATAN_STR = sjs.KD_JABATAN_STR 
		OUTER APPLY
		(
			SELECT
				NM_UNIT_ORG AS nama_bidang
			FROM
				dbo.SPG_08_UNIT_ORGANISASI
			WHERE
				KD_ESELON = '00'
				AND LEFT(KD_UNIT_ORG, 3) = LEFT(sdc.KD_UNIT_ORG, 3)
		) AS bidang 
		INNER JOIN
		dbo.SPG_08_UNIT_ORGANISASI AS suo ON sdc.KD_UNIT_ORG = suo.KD_UNIT_ORG
		OUTER APPLY
		(
			SELECT STUFF((SELECT ',' + spst2.NIP + ':' + sdc2.NM_PEG
			FROM SPG_PESERTA_SURAT_TUGAS AS spst2 INNER JOIN SPG_DATA_CURRENT AS sdc2 ON spst2.NIP = sdc2.NIP WHERE spst2.STU_ID = st.STU_ID
			FOR XML PATH('')) ,1,1,'') AS pst
		 ) AS listpst
		 OUTER APPLY
		 (
			SELECT COUNT(*) AS jml FROM SPG_PESERTA_SURAT_TUGAS WHERE STU_ID = st.STU_ID
		 ) AS jmlpst 
		 LEFT JOIN
		 SPG_DATA_CURRENT AS pembuat_st ON st.NAMA_BUAT = pembuat_st.NIP
		 LEFT JOIN
		 SPG_DATA_CURRENT AS pengubah_st ON st.NAMA_UPDATE = pengubah_st.NIP
		 LEFT JOIN
		 SPG_DATA_CURRENT AS ttd_st ON st.NIP_TTD = ttd_st.NIP
	WHERE
		(sdc.NIP = @pi_nip OR ISNULL(@pi_nip, '') = '')
		AND (sdc.NM_PEG LIKE '%'+@pi_nama+'%' OR ISNULL(@pi_nama, '') = '')
		AND st.STU_ID IS NOT NULL
	ORDER BY
		TGL_SURAT DESC

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang]
	@mpp_id varchar(10)	-- contoh = 'MPP-000002'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						lowjab_90 SMALLINT NULL,
						eligible_90 SMALLINT NULL,
						selisih_90 SMALLINT NULL,
						lowjab_80 SMALLINT NULL,
						eligible_80 SMALLINT NULL,
						selisih_80 SMALLINT NULL,
						lowjab_70 SMALLINT NULL,
						eligible_70 SMALLINT NULL,
						selisih_70 SMALLINT NULL,
						lowjab_60 SMALLINT NULL,
						eligible_60 SMALLINT NULL,
						selisih_60 SMALLINT NULL,
						lowjab_50 SMALLINT NULL,
						eligible_50 SMALLINT NULL,
						selisih_50 SMALLINT NULL,
						lowjab_40 SMALLINT NULL,
						eligible_40 SMALLINT NULL,
						selisih_40 SMALLINT NULL,
						lowjab_30 SMALLINT NULL,
						eligible_30 SMALLINT NULL,
						selisih_30 SMALLINT NULL
					)

	INSERT INTO #hasil
		(
		    kd_bidang,
		    bidang,
		    lowjab_90,
		    eligible_90,
		    selisih_90,
		    lowjab_80,
		    eligible_80,
		    selisih_80,
		    lowjab_70,
		    eligible_70,
		    selisih_70,
		    lowjab_60,
		    eligible_60,
		    selisih_60,
		    lowjab_50,
		    eligible_50,
		    selisih_50,
		    lowjab_40,
		    eligible_40,
		    selisih_40,
		    lowjab_30,
		    eligible_30,
		    selisih_30
		)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@lowjab_kd_bid VARCHAR(3),
		@lowjab_kd_gol VARCHAR(2),
		@lowjab_jml SMALLINT

	DECLARE lowjab_cursor CURSOR FAST_FORWARD FOR
	SELECT
		lowjab.bid,
		lowjab.KD_GOL,
		--lowjab.nm_bidang,
		--COUNT(*) AS jml
		SUM(lowjab.JUMLAH) AS jml
	FROM
		(
			select
				--r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				r.KD_GOL,
				--CASE
				--	WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
				--													WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
				--													ELSE f.nm_unit_org
				--												END
				--	ELSE d.nama_satker_organisasi
				--END AS satuan_kerja,
				--c.nm_kantor as kantor,
				--j.posisi as posisi_jabatan,
				--l.KD_GOL,
				--r.JNS_JABATAN,
				a.JUMLAH,
				g.NM_UNIT_ORG AS nm_bidang
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS lowjab
	GROUP BY
		lowjab.bid,
		--lowjab.satuan_kerja,
		--lowjab.kantor,
		--lowjab.posisi_jabatan,
		lowjab.KD_GOL--,
		--lowjab.id_formasi,
		--lowjab.JNS_JABATAN,
		--lowjab.nm_bidang
	ORDER BY
		lowjab.bid,
		lowjab.KD_GOL DESC

	OPEN lowjab_cursor
	FETCH NEXT FROM lowjab_cursor INTO @lowjab_kd_bid, @lowjab_kd_gol, @lowjab_jml

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @lowjab_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'lowjab_' + @lowjab_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET lowjab_' + @lowjab_kd_gol + ' = @lowjab_jml WHERE kd_bidang = @lowjab_kd_bid'
						EXEC sp_executesql @sql, N'@lowjab_jml SMALLINT, @lowjab_kd_bid VARCHAR(3)', @lowjab_jml = @lowjab_jml, @lowjab_kd_bid = @lowjab_kd_bid
					END
			END


	  FETCH NEXT FROM lowjab_cursor INTO @lowjab_kd_bid, @lowjab_kd_gol, @lowjab_jml
	END

	CLOSE lowjab_cursor
	DEALLOCATE lowjab_cursor


	DECLARE
		@eligible_kd_bid VARCHAR(3),
		@eligible_kd_gol VARCHAR(2),
		@eligible_jml SMALLINT

	DECLARE eligible_cursor CURSOR FAST_FORWARD FOR
	SELECT
		--elig.BIDANG,
		elig.KD_BIDANG,
		elig.KD_GOL,
		COUNT(*) AS jml
	FROM
		(
			SELECT DISTINCT
				a.NIP,
				a.NM_PEG,
				a.BIDANG,
				a.SATUAN_KERJA,
				a.LEVEL_JABATAN,
				a.NM_JABATAN,
				a.FORMASI,
				a.MASA_DINAS_LEVEL_JABATAN,
				a.KETERANGAN,
				--a.KD_BIDANG,
				LEFT(a.KD_BIDANG,3) AS KD_BIDANG,
				a.LEVEL_JABATAN_PEMENUHAN,
				--a.KD_GOL,	-- bukan eligible
				b.KD_GOL,	-- tapi lowjabnya
				a.KD_JABATAN_STR
			FROM 
				dbo.SPG_PEGAWAI_ELIGIBLE AS a
				INNER JOIN 
				(
					SELECT 
						b.req_id, 
						a.ptk_id, 
						c.satker, 
						e.NM_UNIT_ORG AS depkom, 
						c.nm_kantor AS kantor, 
						d.KD_UNIT_ES1 AS kd_bidang, 
						m.masa_dinas_minimal, 
						m.sisa_dinas, 
						m.tanggal_dinas, 
						c.bidang, 
						c.FORMASI AS posisi_jabatan, 
						a.kd_jabatan, 
						b.tanggal_publish, 
						b.tanggal_selesai, 
						b.jam_selesai, 
						--m.kd_gol, 
						c.KD_GOL,
						a.mpp_id, 
						a.NM_GOLONGAN
					FROM 
						rec_ptk AS a
						LEFT JOIN
						rec_permintaan_advertising AS b ON a.ptk_id = b.req_id
						LEFT JOIN
						REC_RENCANA_PEMENUHAN_PEG AS c ON a.ID_FORMASI = c.ID_FORMASI
						LEFT JOIN
						SPG_08_UNIT_ORGANISASI AS d ON a.KD_UNIT_ORG = d.KD_UNIT_ORG
						LEFT JOIN
						SPG_08_UNIT_ORGANISASI AS e ON d.KD_UNIT_ES2 = e.KD_UNIT_ORG
						LEFT JOIN
						REC_PERSYARATAN AS m ON m.KD_GOL = a.KD_GOL AND m.periode = a.mpp_id
						LEFT JOIN
						SPG_JNS_PEND_UMUM AS n ON m.PENDIDIKAN = n.KD_PEND_UMUM
					WHERE
						b.ram_id = 'RAM-000020'
						AND b.rac_id = 'RAC-000001'
						AND c.STATUS_KIRIM = 1
						AND c.STATUS_AKTIF = 1
						AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
						--AND (c.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
				) AS b ON a.periode_pemenuhan = b.mpp_id AND a.level_jabatan_pemenuhan = b.nm_golongan
			WHERE
				a.PERIODE_PEMENUHAN = @mpp_id
				AND a.ELIGIBLE_COMBINE = 'ELIGIBLE'
				AND a.STS_PENSIUN = 0
				--AND (LEFT(a.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')	-- bukan bidang lowongan, tapi bidang eligible
		) AS elig
	GROUP BY
		elig.BIDANG,
		elig.KD_BIDANG,
		elig.KD_GOL
	ORDER BY
		elig.KD_BIDANG,
		elig.KD_GOL DESC

	OPEN eligible_cursor
	FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @eligible_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'lowjab_' + @eligible_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						DECLARE @sql2 nvarchar(1000)
						SET @sql2 = 'UPDATE #hasil SET eligible_' + @eligible_kd_gol + ' = @eligible_jml WHERE kd_bidang = @eligible_kd_bid'
						EXEC sp_executesql @sql2, N'@eligible_jml SMALLINT, @eligible_kd_bid VARCHAR(3)', @eligible_jml = @eligible_jml, @eligible_kd_bid = @eligible_kd_bid
					END
			END


	  FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml
	END

	CLOSE eligible_cursor
	DEALLOCATE eligible_cursor

	UPDATE
		#hasil
	SET
		selisih_60 = eligible_60 - lowjab_60,
		selisih_50 = eligible_50 - lowjab_50,
		selisih_40 = eligible_40 - lowjab_40

	SELECT * FROM #hasil ORDER BY kd_bidang

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
	    DROP TABLE #hasil;

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_detail]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3)	-- contoh  =  = '101'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	SELECT
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.kantor,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan,
		SUM(COALESCE(peg.eligible,0)) AS eligible
		--SUM(COALESCE(peg.eligible,0) - COALESCE(lowjab.jml,0)) AS selisih
		
	FROM
		--dbo.SPG_08_UNIT_ORGANISASI suo
		--CROSS APPLY
		--(
		--	SELECT
		--		a.MPP_ID,
		--		LEFT(a.KD_UNIT_ORG, 3) AS bid,
		--		a.KD_GOL,
		--		a.KD_JABATAN,
		--		SUM(b.JUMLAH) AS jml
		--	FROM
		--		dbo.REC_RENCANA_PEMENUHAN_PEG AS a
		--		INNER JOIN
		--		dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		--		LEFT JOIN
		--		dbo.REC_PERMINTAAN_ADVERTISING AS c ON b.PTK_ID = c.REQ_ID
		--	WHERE
		--	    a.STATUS_KIRIM = 1
		--	    AND a.STATUS_AKTIF = 1
		--	    AND LEFT(a.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
		--	    --AND kd_kantor = '100000000000000'	--?
		--	    AND c.JAM_SELESAI IS NOT NULL	-- sudah posting
		--	GROUP BY
		--		a.MPP_ID,
		--		LEFT(a.KD_UNIT_ORG, 3),
		--		a.KD_GOL,
		--		a.KD_JABATAN
		--) AS lowjab
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				r.JNS_JABATAN,
				g.NM_UNIT_ORG AS nm_bidang
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				--AND r.KD_GOL = @kd_gol	--varchar(2)
		) AS lowjab
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS eligible
			FROM
				dbo.SPG_PEGAWAI_ELIGIBLE AS d
			WHERE
				d.PERIODE_PEMENUHAN = @mpp_id
				AND d.ELIGIBLE_COMBINE = 'ELIGIBLE'
				AND d.STS_PENSIUN = 0
				AND d.KD_GOL = lowjab.KD_GOL
				--AND d.KD_JABATAN_STR = lowjab.KD_JABATAN	-- ?
				AND LEFT(d.KD_BIDANG,3) = lowjab.bid
		) AS peg
	--WHERE
	--	suo.KD_ESELON = '00'
	--	AND LEFT(suo.KD_UNIT_ORG,3) >= '101'
	--	AND LEFT(suo.KD_UNIT_ORG,3) <= '107'
	--	AND lowjab.MPP_ID = @mpp_id
	GROUP BY
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.JNS_JABATAN,
		lowjab.nm_bidang
		--suo.NM_UNIT_ORG,
		--LEFT(suo.KD_UNIT_ORG,3)
	ORDER BY
		--LEFT(suo.KD_UNIT_ORG,3),
		lowjab.KD_GOL DESC;
END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_eligible]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_eligible]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)	-- contoh  = '60'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--SELECT
	--	*
	--FROM
	--	dbo.SPG_PEGAWAI_ELIGIBLE AS d
	--WHERE
	--	d.PERIODE_PEMENUHAN = @mpp_id
	--	AND d.STS_PENSIUN = 0
	--	AND (d.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')		-- kalau query-nya begini, KD_GOL-nya salah, karena ini posisi pegawai saat ini, bukan eligible
	--	AND (LEFT(d.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')

	SELECT DISTINCT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATUAN_KERJA,
		a.LEVEL_JABATAN,
		a.NM_JABATAN,
		a.FORMASI,
		a.MASA_DINAS_LEVEL_JABATAN,
		a.KETERANGAN,
		a.KD_BIDANG,
		a.LEVEL_JABATAN_PEMENUHAN,
		a.KD_GOL, 
		a.KD_JABATAN_STR
	FROM 
		dbo.SPG_PEGAWAI_ELIGIBLE AS a
		INNER JOIN 
		(
			SELECT 
				b.req_id, 
				a.ptk_id, 
				c.satker, 
				e.NM_UNIT_ORG AS depkom, 
				c.nm_kantor AS kantor, 
				d.KD_UNIT_ES1 AS kd_bidang, 
				m.masa_dinas_minimal, 
				m.sisa_dinas, 
				m.tanggal_dinas, 
				c.bidang, 
				c.FORMASI AS posisi_jabatan, 
				a.kd_jabatan, 
				b.tanggal_publish, 
				b.tanggal_selesai, 
				b.jam_selesai, 
				m.kd_gol, 
				a.mpp_id, 
				a.NM_GOLONGAN
			FROM 
				rec_ptk AS a
				LEFT JOIN
				rec_permintaan_advertising AS b ON a.ptk_id = b.req_id
				LEFT JOIN
				REC_RENCANA_PEMENUHAN_PEG AS c ON a.ID_FORMASI = c.ID_FORMASI
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS d ON a.KD_UNIT_ORG = d.KD_UNIT_ORG
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS e ON d.KD_UNIT_ES2 = e.KD_UNIT_ORG
				LEFT JOIN
				REC_PERSYARATAN AS m ON m.KD_GOL = a.KD_GOL AND m.periode = a.mpp_id
				LEFT JOIN
				SPG_JNS_PEND_UMUM AS n ON m.PENDIDIKAN = n.KD_PEND_UMUM
			WHERE
				b.ram_id = 'RAM-000020'
				AND b.rac_id = 'RAC-000001'
				AND c.STATUS_KIRIM = 1
				AND c.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (c.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS b ON a.periode_pemenuhan = b.mpp_id AND a.level_jabatan_pemenuhan = b.nm_golongan
	WHERE
		a.PERIODE_PEMENUHAN = @mpp_id
		AND a.ELIGIBLE_COMBINE = 'ELIGIBLE'
		AND a.STS_PENSIUN = 0
		AND (LEFT(a.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')	-- bukan bidang lowongan, tapi bidang eligible
	ORDER BY 
		a.KD_BIDANG, 
		a.KD_GOL DESC, 
		a.KD_JABATAN_STR DESC,
		a.MASA_DINAS_LEVEL_JABATAN DESC,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_dan_pegawai_eligible_per_bidang_lowjab]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	SELECT
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.kantor,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan,
		lowjab.JNS_JABATAN,
		SUM(lowjab.JUMLAH) AS JUMLAH
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				r.JNS_JABATAN,
				g.NM_UNIT_ORG AS nm_bidang,
				a.JUMLAH
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND (LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS lowjab
	GROUP BY
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.JNS_JABATAN,
		lowjab.nm_bidang
	ORDER BY
		--LEFT(suo.KD_UNIT_ORG,3),
		lowjab.KD_GOL DESC,
		lowjab.JNS_JABATAN DESC;
END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang]
	@mpp_id VARCHAR(10)		-- contoh: 'MPP-000002'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						lowjab_80 SMALLINT NULL,
						internal_80 SMALLINT NULL,
						eksternal_80 SMALLINT NULL,
						lowjab_70 SMALLINT NULL,
						internal_70 SMALLINT NULL,
						eksternal_70 SMALLINT NULL,
						lowjab_60 SMALLINT NULL,
						internal_60 SMALLINT NULL,
						eksternal_60 SMALLINT NULL,
						lowjab_50 SMALLINT NULL,
						internal_50 SMALLINT NULL,
						eksternal_50 SMALLINT NULL,
						lowjab_40 SMALLINT NULL,
						internal_40 SMALLINT NULL,
						eksternal_40 SMALLINT NULL,
						lowjab_30 SMALLINT NULL,
						internal_30 SMALLINT NULL,
						eksternal_30 SMALLINT NULL
					)

	INSERT INTO #hasil
		(
		    kd_bidang,
		    bidang,
		    lowjab_80,
		    internal_80,
		    eksternal_80,
		    lowjab_70,
		    internal_70,
		    eksternal_70,
		    lowjab_60,
		    internal_60,
		    eksternal_60,
		    lowjab_50,
		    internal_50,
		    eksternal_50,
		    lowjab_40,
		    internal_40,
		    eksternal_40,
		    lowjab_30,
		    internal_30,
		    eksternal_30
		)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@kd_bidang VARCHAR(3),
		@bidang VARCHAR(100),
		@kd_gol VARCHAR(2),
		--@kd_jabatan VARCHAR(3),
		@jml SMALLINT,
		@internal SMALLINT,
		@eksternal SMALLINT

	DECLARE list_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.NM_UNIT_ORG,3),
		suo.NM_UNIT_ORG,
		lowjab.KD_GOL,
		--lowjab.KD_JABATAN,
		SUM(COALESCE(lowjab.jml,0)),
		--g.jml_pelamar,
		SUM(COALESCE(internal.jml_pelamar_int,0)),
		SUM(COALESCE(eksternal.jml_pelamar_ext,0))
	FROM
		dbo.SPG_08_UNIT_ORGANISASI suo
		CROSS APPLY
		(
			SELECT
				a.MPP_ID,
				LEFT(a.KD_UNIT_ORG, 3) AS bid,
				a.KD_GOL,
				a.KD_JABATAN,
				SUM(b.JUMLAH) AS jml,
				b.PTK_ID,
				c.REQ_ID,
				b.KD_UNIT_ORG
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS a
				INNER JOIN
				dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
				LEFT JOIN
				dbo.REC_PERMINTAAN_ADVERTISING AS c ON b.PTK_ID = c.REQ_ID
			WHERE
			    a.STATUS_KIRIM = 1
			    AND a.STATUS_AKTIF = 1
			    AND LEFT(a.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
			    --AND kd_kantor = '100000000000000'	--?
			    AND c.JAM_SELESAI IS NOT NULL	-- sudah posting
			GROUP BY
				a.MPP_ID,
				LEFT(a.KD_UNIT_ORG, 3),
				a.KD_GOL,
				a.KD_JABATAN,
				b.PTK_ID,
				c.REQ_ID,
				b.KD_UNIT_ORG
		) AS lowjab
		--OUTER APPLY
		--(
		--	SELECT
		--		COUNT(*) AS jml_pelamar
		--	FROM
		--		dbo.REC_PELAMAR_INTERNAL f
		--	WHERE
		--		f.REQ_ID = lowjab.PTK_ID
		--		AND f.REQ_ID = lowjab.REQ_ID
		--		AND f.NIP_REKOMENDASI <> ''		-- jika 1 berarti sudah melalui rekomendasi pimpinan
		--) g
		-------
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS jml_pelamar_int
			FROM
				dbo.REC_PELAMAR_INTERNAL AS h
			WHERE
				lowjab.PTK_ID = h.REQ_ID
				AND lowjab.REQ_ID = h.REQ_ID
				--AND COALESCE(h.NIP_REKOMENDASI, '') <> ''	-- sudah melalui rekomendasi pimpinan
				AND COALESCE(h.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND LEFT(lowjab.KD_UNIT_ORG, 3) = LEFT(h.KD_UNIT_ORG, 3)	-- internal, maksudnya bidang yang sama

		) AS internal
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS jml_pelamar_ext
			FROM
				dbo.REC_PELAMAR_INTERNAL AS i
			WHERE
				lowjab.PTK_ID = i.REQ_ID
				AND lowjab.REQ_ID = i.REQ_ID
				--AND COALESCE(i.NIP_REKOMENDASI, '') <> ''	-- sudah melalui rekomendasi pimpinan
				AND COALESCE(i.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND LEFT(lowjab.KD_UNIT_ORG, 3) <> LEFT(i.KD_UNIT_ORG, 3)	-- external, maksudnya bidang yang berbeda

		) AS eksternal
	WHERE
		suo.KD_ESELON = '00'
		AND LEFT(suo.KD_UNIT_ORG,3) >= '101'
		AND LEFT(suo.KD_UNIT_ORG,3) <= '107'
		AND lowjab.MPP_ID = @mpp_id
	GROUP BY
		suo.NM_UNIT_ORG,
		lowjab.KD_GOL,
		LEFT(suo.KD_UNIT_ORG,3)
	ORDER BY
		LEFT(suo.KD_UNIT_ORG,3),
		lowjab.KD_GOL DESC;

	OPEN list_cursor
	FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @jml, @internal, @eksternal

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE bidang = @bidang)
			BEGIN
				DECLARE @sql nvarchar(1000)
				SET @sql = 'UPDATE #hasil SET lowjab_' + @kd_gol + ' = @jml, internal_' + @kd_gol + ' = @internal, eksternal_' + @kd_gol + ' = @eksternal WHERE bidang = @bidang'
				EXEC sp_executesql @sql, N'@jml SMALLINT, @internal SMALLINT, @eksternal SMALLINT, @bidang VARCHAR(100)', @jml = @jml, @internal = @internal, @eksternal = @eksternal, @bidang = @bidang
			END


	  FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @jml, @internal, @eksternal
	END

	CLOSE list_cursor
	DEALLOCATE list_cursor

	SELECT * FROM #hasil ORDER BY kd_bidang

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
	    DROP TABLE #hasil;

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- by Pribadi Pamungkas 2020-12-02
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_detail]
	@mpp_id     VARCHAR(10), -- contoh 'MPP-000003'
	@kd_bidang VARCHAR(3),
	@kd_gol VARCHAR(2)
AS
BEGIN
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		SUM(COALESCE(pelamarint.jml_int,0)) AS jml_int,
		SUM(COALESCE(pelamareks.jml_eks,0)) AS jml_eks,
		lowjab.ID_FORMASI,
		SUM(lowjab.JUMLAH) AS JUMLAH
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				g.nm_unit_org as nm_bidang,
				a.JUMLAH
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, ,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND (LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')	--varchar(2)
		) AS lowjab
		OUTER APPLY
		(
			SELECT 
				COUNT(d.id_formasi) AS jml_int
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
			WHERE
				--COALESCE(d.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
				COALESCE(d.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND d.id_formasi = lowjab.id_formasi
				AND LEFT(d.KD_UNIT_ORG, 3) = lowjab.bid
			GROUP BY
				d.id_formasi
		) AS pelamarint
		OUTER APPLY
		(
			SELECT 
				COUNT(d.id_formasi) AS jml_eks
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
			WHERE
				--COALESCE(d.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
				COALESCE(d.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND d.id_formasi = lowjab.id_formasi
				AND LEFT(d.KD_UNIT_ORG, 3) <> lowjab.bid
			GROUP BY
				d.id_formasi
		) AS pelamareks
	GROUP BY
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		lowjab.ID_FORMASI
END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_lowjab]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	SELECT
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.kantor,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan,
		lowjab.JNS_JABATAN
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				r.JNS_JABATAN,
				g.NM_UNIT_ORG AS nm_bidang
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS lowjab
	GROUP BY
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.JNS_JABATAN,
		lowjab.nm_bidang
	ORDER BY
		--LEFT(suo.KD_UNIT_ORG,3),
		lowjab.KD_GOL DESC,
		lowjab.JNS_JABATAN DESC;
END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_internal_per_bidang_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- by Pribadi Pamungkas. 2021-01-12
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_internal_per_bidang_pelamar]
--DECLARE
	@mpp_id varchar(10),	-- = 'MPP-000002',	-- contoh  = 'MPP-000002',
	--@kd_bidang varchar(3) = '101',	-- contoh  = '101'
	--@kd_gol VARCHAR(2) = '60'
	@id_formasi int,
	@internal bit
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		a.MASA_DINAS_LVL_JABATAN,
		r.NM_ROOM,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.kantor,
		b.satuan_kerja,
		b.posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				j.POSISI AS posisi_jabatan
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI
		LEFT JOIN
		dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r ON a.ID_ROOM = r.ID
	WHERE
		(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		--AND COALESCE(NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
		AND (LEFT(a.KD_UNIT_ORG,3) = LEFT(b.KD_UNIT_ORG,3) OR ISNULL(@internal, 0) = 0)
		AND (LEFT(a.KD_UNIT_ORG,3) <> LEFT(b.KD_UNIT_ORG,3) OR ISNULL(@internal, 0) = 1)
	ORDER BY
		r.NM_ROOM,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang]
	@mpp_id VARCHAR(10)	-- contoh 'MPP-000003'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						lowjab_80 SMALLINT NULL,
						longlist_80 SMALLINT NULL,
						shortlist_80 SMALLINT NULL,
						lowjab_70 SMALLINT NULL,
						longlist_70 SMALLINT NULL,
						shortlist_70 SMALLINT NULL,
						lowjab_60 SMALLINT NULL,
						longlist_60 SMALLINT NULL,
						shortlist_60 SMALLINT NULL,
						lowjab_50 SMALLINT NULL,
						longlist_50 SMALLINT NULL,
						shortlist_50 SMALLINT NULL,
						lowjab_40 SMALLINT NULL,
						longlist_40 SMALLINT NULL,
						shortlist_40 SMALLINT NULL,
						lowjab_30 SMALLINT NULL,
						longlist_30 SMALLINT NULL,
						shortlist_30 SMALLINT NULL
					)

	INSERT INTO #hasil
		(
		    kd_bidang,
		    bidang,
		    lowjab_80,
		    longlist_80,
		    shortlist_80,
		    lowjab_70,
		    longlist_70,
		    shortlist_70,
		    lowjab_60,
		    longlist_60,
		    shortlist_60,
		    lowjab_50,
		    longlist_50,
		    shortlist_50,
		    lowjab_40,
		    longlist_40,
		    shortlist_40,
		    lowjab_30,
		    longlist_30,
		    shortlist_30
		)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@kd_bidang VARCHAR(3),
		@bidang VARCHAR(100),
		@kd_gol VARCHAR(2),
		--@kd_jabatan VARCHAR(3),
		@lowjab SMALLINT,
		@longlist SMALLINT,
		@shortlist SMALLINT

	DECLARE list_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.NM_UNIT_ORG,3),
		suo.NM_UNIT_ORG,
		lowjab.KD_GOL,
		--lowjab.KD_JABATAN,
		SUM(lowjab.jml),
		SUM(g.jml_longlist),
		SUM(h.jml_shortlist)
	FROM
		dbo.SPG_08_UNIT_ORGANISASI suo
		CROSS APPLY
		(
			SELECT
				a.MPP_ID,
				LEFT(a.KD_UNIT_ORG, 3) AS bid,
				a.KD_GOL,
				a.KD_JABATAN,
				SUM(b.JUMLAH) AS jml,
				b.PTK_ID,
				c.REQ_ID
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS a
				INNER JOIN
				dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
				LEFT JOIN
				dbo.REC_PERMINTAAN_ADVERTISING AS c ON b.PTK_ID = c.REQ_ID
			WHERE
				a.STATUS_KIRIM = 1
				AND a.STATUS_AKTIF = 1
				AND LEFT(a.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
				--AND kd_kantor = '100000000000000'	--?
				AND c.JAM_SELESAI IS NOT NULL	-- sudah posting
			GROUP BY
				a.MPP_ID,
				LEFT(a.KD_UNIT_ORG, 3),
				a.KD_GOL,
				a.KD_JABATAN,
				b.PTK_ID,
				c.REQ_ID
		) AS lowjab
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS jml_longlist
			FROM
				dbo.REC_PELAMAR_INTERNAL AS f
			WHERE
				f.REQ_ID = lowjab.PTK_ID
				AND f.REQ_ID = lowjab.REQ_ID
				--AND f.NIP_REKOMENDASI <> ''		-- jika 1 berarti sudah melalui rekomendasi pimpinan
				AND COALESCE(f.REKOMENDASI_PIMPINAN,'') = 'Y'
		) g	-- longslist
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS jml_shortlist
			FROM
				dbo.REC_PELAMAR_INTERNAL AS f INNER JOIN
				dbo.REC_PELAMAR_RANKING AS g ON f.RPI_ID = g.RPI_ID
											AND f.REQ_ID = g.REQ_ID
			WHERE
				f.REQ_ID = lowjab.PTK_ID
				AND f.REQ_ID = lowjab.REQ_ID
				--AND f.NIP_REKOMENDASI <> ''		-- jika 1 berarti sudah melalui rekomendasi pimpinan
				AND COALESCE(f.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND g.SHORT_LIST = 1	-- shortlist
		) h
	WHERE
		suo.KD_ESELON = '00'
		AND LEFT(suo.KD_UNIT_ORG,3) >= '101'
		AND LEFT(suo.KD_UNIT_ORG,3) <= '107'
		AND lowjab.MPP_ID = @mpp_id
	GROUP BY
		suo.NM_UNIT_ORG,
		lowjab.KD_GOL,
		LEFT(suo.KD_UNIT_ORG,3)
	ORDER BY
		LEFT(suo.KD_UNIT_ORG,3),
		lowjab.KD_GOL DESC;

	OPEN list_cursor
	FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @lowjab, @longlist, @shortlist

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE bidang = @bidang)
			BEGIN
				DECLARE @sql nvarchar(1000)
				SET @sql = 'UPDATE #hasil SET lowjab_' + @kd_gol + ' = @lowjab, longlist_' + @kd_gol + ' = @longlist, shortlist_' + @kd_gol + ' = @shortlist WHERE bidang = @bidang'
				EXEC sp_executesql @sql, N'@lowjab SMALLINT, @longlist SMALLINT, @shortlist SMALLINT, @bidang VARCHAR(100)', @lowjab = @lowjab, @longlist = @longlist, @shortlist = @shortlist, @bidang = @bidang
			END

	  FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @lowjab, @longlist, @shortlist
	END

	CLOSE list_cursor
	DEALLOCATE list_cursor

	SELECT * FROM #hasil ORDER BY kd_bidang

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
	    DROP TABLE #hasil;

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- by Pribadi Pamungkas. 2020-12-06

CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail]
	@mpp_id varchar(10),
	@kd_bidang varchar(3),
	@kd_gol VARCHAR(2)
AS
BEGIN
	
	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan + ' ' + COALESCE(lowjab.NM_ROOM,'') AS posisi_jabatan,
		SUM(COALESCE(terisi.id_formasi_terisi,0)) AS pelamar,
		SUM(COALESCE(shotlist.jml_shortlist,0)) AS shortlist,
		lowjab.id_formasi,
		lowjab.NM_ROOM,
		lowjab.ID_ROOM
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				g.NM_UNIT_ORG AS nm_bidang,
				room.ID AS ID_ROOM,
				room.NM_ROOM
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS room ON a.ID_FORMASI = room.ID_FORMASI
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				AND (LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS lowjab
		OUTER APPLY
		(
			SELECT 
				COUNT(*) AS id_formasi_terisi
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
			WHERE
				--COALESCE(d.NIP_REKOMENDASI, '') <> ''
				COALESCE(d.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND d.id_formasi = lowjab.id_formasi
				AND (d.ID_ROOM = lowjab.ID_ROOM OR ISNULL(lowjab.ID_ROOM, '') = '') 
			GROUP BY
				d.id_formasi
		) AS terisi
		OUTER APPLY
		(
			SELECT
				COUNT(*) AS jml_shortlist
			FROM
				dbo.REC_PELAMAR_INTERNAL AS f INNER JOIN
				dbo.REC_PELAMAR_RANKING AS g ON f.RPI_ID = g.RPI_ID
											AND f.REQ_ID = g.REQ_ID
			WHERE
				--f.REQ_ID = lowjab.PTK_ID
				--AND f.REQ_ID = lowjab.REQ_ID
				f.id_formasi = lowjab.id_formasi
				AND (f.ID_ROOM = lowjab.ID_ROOM OR ISNULL(lowjab.ID_ROOM, '') = '') 
				--AND COALESCE(f.NIP_REKOMENDASI, '') <> ''		-- jika 1 berarti sudah melalui rekomendasi pimpinan
				AND COALESCE(f.REKOMENDASI_PIMPINAN,'') = 'Y'
				AND g.SHORT_LIST = 1	-- shortlist
		) AS shotlist
	GROUP BY
		lowjab.bid,
		lowjab.nm_bidang,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.NM_ROOM,
		lowjab.ID_ROOM
	ORDER BY
		lowjab.bid,
		lowjab.KD_GOL DESC,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan
		
END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2021-02-16
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist]
	@mpp_id varchar(10),
	@id_formasi int
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--SELECT
	--	NIP,
	--	NM_PEG,
	--	BIDANG,
	--	SATKER,
	--	FORMASI,
	--	MASA_DINAS_LVL_JABATAN
	--FROM
	--	dbo.REC_PELAMAR_INTERNAL
	--WHERE
	--	ID_FORMASI = @id_formasi
	--	AND COALESCE(NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		spe.MASA_DINAS_LEVEL_JABATAN AS MASA_DINAS_LVL_JABATAN,
		--pr.RANKING,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(b.KD_UNIT_ORG,3),
				b.deputi_komisioner,
				b.KD_GOL,
				b.satuan_kerja,
				b.posisi_jabatan,
				b.NM_ROOM
			ORDER BY 
				LEFT(b.KD_UNIT_ORG,3),
				b.URUTAN_DEPKOM,
			    b.kd_satuan_kerja,
				b.KD_GOL DESC,
			    b.JNS_JABATAN DESC,
				b.posisi_jabatan,
				b.NM_ROOM,
				--CASE
				--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
				--	ELSE pr.total_skor
				--END DESC
				pr.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.deputi_komisioner,
		b.kantor,
		b.NM_GOL,
		b.satuan_kerja,
		b.posisi_jabatan + ' ' + COALESCE(b.NM_ROOM,'') AS posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
               CASE
                    WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
                                                                    WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
                                                                    ELSE f.KD_UNIT_ORG
                                                                END
                    ELSE d.kode_satker_organisasi
                END AS kd_satuan_kerja,
				j.POSISI AS posisi_jabatan,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				f.NM_UNIT_ORG AS deputi_komisioner,
			    x.JNS_JABATAN,
			    f.KD_UNIT_ORG AS kd_deputi_komisioner,
			    s.URUTAN_DEPKOM
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r
				ON y.ID_FORMASI = r.ID_FORMASI
			    LEFT JOIN (
                    SELECT t.KD_UNIT_ORG as KD_DEPKOM,
                           CASE WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
                               THEN CASE WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 ELSE case WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 else ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) end end
                        ELSE '0' END URUTAN_DEPKOM
                           FROM MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
                        WHERE t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
                 ) s ON f.KD_UNIT_ORG=s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI AND (b.ID_ROOM = a.ID_ROOM OR ISNULL(b.ID_ROOM, '') = '') 
		LEFT JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.ID = pr.ID_PELAMAR
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
	ORDER BY
		LEFT(b.KD_UNIT_ORG,3),
		b.URUTAN_DEPKOM,
		b.kd_satuan_kerja,
	    b.KD_GOL DESC,
	    b.JNS_JABATAN DESC,
		b.posisi_jabatan,
		b.NM_ROOM

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist_depkom]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2022-03-10
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_longlist_depkom]
	@mpp_id varchar(10),
	--@id_formasi int
	@kd_depkom varchar(24)
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--SELECT
	--	NIP,
	--	NM_PEG,
	--	BIDANG,
	--	SATKER,
	--	FORMASI,
	--	MASA_DINAS_LVL_JABATAN
	--FROM
	--	dbo.REC_PELAMAR_INTERNAL
	--WHERE
	--	ID_FORMASI = @id_formasi
	--	AND COALESCE(NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		spe.MASA_DINAS_LEVEL_JABATAN AS MASA_DINAS_LVL_JABATAN,
		--pr.RANKING,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(b.KD_UNIT_ORG,3),
				b.deputi_komisioner,
				b.KD_GOL,
				b.satuan_kerja,
				b.posisi_jabatan,
				b.NM_ROOM
			ORDER BY 
				LEFT(b.KD_UNIT_ORG,3),
				b.URUTAN_DEPKOM,
			    b.kd_satuan_kerja,
				b.KD_GOL DESC,
			    b.JNS_JABATAN DESC,
				b.posisi_jabatan,
				b.NM_ROOM,
				--CASE
				--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
				--	ELSE pr.total_skor
				--END DESC
				pr.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.deputi_komisioner,
		b.kantor,
		b.NM_GOL,
		b.satuan_kerja,
		b.posisi_jabatan + ' ' + COALESCE(b.NM_ROOM,'') AS posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
               CASE
                    WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
                                                                    WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
                                                                    ELSE f.KD_UNIT_ORG
                                                                END
                    ELSE d.kode_satker_organisasi
                END AS kd_satuan_kerja,
				j.POSISI AS posisi_jabatan,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				f.NM_UNIT_ORG AS deputi_komisioner,
			    x.JNS_JABATAN,
			    f.KD_UNIT_ORG AS kd_deputi_komisioner,
			    s.URUTAN_DEPKOM
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r
				ON y.ID_FORMASI = r.ID_FORMASI
			    LEFT JOIN (
                    SELECT t.KD_UNIT_ORG as KD_DEPKOM,
                           CASE WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
                               THEN CASE WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 ELSE case WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 else ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) end end
                        ELSE '0' END URUTAN_DEPKOM
                           FROM MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
                        WHERE t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
                 ) s ON f.KD_UNIT_ORG=s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI AND (b.ID_ROOM = a.ID_ROOM OR ISNULL(b.ID_ROOM, '') = '') 
		LEFT JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.ID = pr.ID_PELAMAR
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		--(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		b.kd_deputi_komisioner = @kd_depkom
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
	ORDER BY
		LEFT(b.KD_UNIT_ORG,3),
		b.URUTAN_DEPKOM,
		b.kd_satuan_kerja,
	    b.KD_GOL DESC,
	    b.JNS_JABATAN DESC,
		b.posisi_jabatan,
		b.NM_ROOM

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_shortlist]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




-- by Pribadi Pamungkas. 2020-12-06
CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_detail_shortlist]
	@mpp_id varchar(10),
	@id_formasi int,
	@id_room int
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.MASA_DINAS_LVL_JABATAN,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(c.KD_UNIT_ORG,3),
				c.deputi_komisioner,
				c.KD_GOL,
				c.satuan_kerja,
				c.posisi_jabatan,
				c.NM_ROOM
			ORDER BY 
				LEFT(c.KD_UNIT_ORG,3),
				c.URUTAN_DEPKOM,
				c.kd_satuan_kerja,
				c.KD_GOL DESC,
				c.JNS_JABATAN DESC,
				c.posisi_jabatan,
				c.NM_ROOM,
				b.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		c.nm_bidang,
		c.deputi_komisioner,
		c.kantor,
		c.NM_GOL,
		c.satuan_kerja,
		--c.posisi_jabatan,
		c.posisi_jabatan + ' ' + COALESCE(c.NM_ROOM,'') AS posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a 
		INNER JOIN
		dbo.REC_PELAMAR_RANKING AS b ON a.RPI_ID = b.RPI_ID
								AND a.REQ_ID = b.REQ_ID 
		INNER JOIN
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				CASE
					WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
																	ELSE f.KD_UNIT_ORG
																END
					ELSE d.kode_satker_organisasi
				END AS kd_satuan_kerja,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				x.JNS_JABATAN,
				j.POSISI AS posisi_jabatan,
				f.NM_UNIT_ORG AS deputi_komisioner,
				s.URUTAN_DEPKOM
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG) LEFT JOIN 
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r ON y.ID_FORMASI = r.ID_FORMASI LEFT JOIN
				( SELECT 
					t.KD_UNIT_ORG as KD_DEPKOM,
                         CASE 
						WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%' THEN 
																CASE 
																	WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 
																	ELSE 
																		CASE 
																			WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 
																			ELSE ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) 
																		END 
																END
						ELSE '0' 
					END AS URUTAN_DEPKOM
				  FROM 
					MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
				  WHERE 
					t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
                 ) AS s ON f.KD_UNIT_ORG = s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS c ON a.ID_FORMASI = c.ID_FORMASI AND (c.ID_ROOM = a.ID_ROOM OR ISNULL(c.ID_ROOM, '') = '') 
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND b.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		AND (a.ID_ROOM = @id_room OR ISNULL(@id_room, '') = '')
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
		AND b.SHORT_LIST = 1	-- shortlist
	ORDER BY
		LEFT(c.KD_UNIT_ORG,3),
		c.URUTAN_DEPKOM,
		c.kd_satuan_kerja,
		c.KD_GOL DESC,
		c.JNS_JABATAN DESC,
		c.posisi_jabatan,
		c.NM_ROOM,
		b.RANKING ASC,
		spe.ELIGIBLE_POTENSI_TINGGI ASC, 
		spe.ELIGIBLE_REGULER ASC

END
GO
/****** Object:  StoredProcedure [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_pelamar_longlist]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[rep_lowongan_jabatan_longlist_dan_shortlist_per_bidang_pelamar_longlist]
	@id_formasi int,
	@id_room int
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.MASA_DINAS_LVL_JABATAN,
		d.NM_ROOM
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		LEFT JOIN
		dbo.REC_ROOM_FORMASI_FUNCTIONAL AS d
		ON a.ID_ROOM = d.ID AND a.ID_FORMASI = d.ID_FORMASI
		LEFT JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.RPI_ID = pr.RPI_ID
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		a.ID_FORMASI = @id_formasi
		AND (a.ID_ROOM = @id_room OR ISNULL(@id_room, '') = '')
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
	ORDER BY
		--CASE
		--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
		--	ELSE pr.total_skor
		--END DESC,
		pr.RANKING ASC,
		spe.ELIGIBLE_POTENSI_TINGGI ASC, 
		spe.ELIGIBLE_REGULER ASC



END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar]
	@mpp_id VARCHAR(10)	-- contoh 'MPP-000003'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						eligible_80 SMALLINT NULL,
						internal_80 SMALLINT NULL,
						eksternal_80 SMALLINT NULL,
						eligible_70 SMALLINT NULL,
						internal_70 SMALLINT NULL,
						eksternal_70 SMALLINT NULL,
						eligible_60 SMALLINT NULL,
						internal_60 SMALLINT NULL,
						eksternal_60 SMALLINT NULL,
						eligible_50 SMALLINT NULL,
						internal_50 SMALLINT NULL,
						eksternal_50 SMALLINT NULL,
						eligible_40 SMALLINT NULL,
						internal_40 SMALLINT NULL,
						eksternal_40 SMALLINT NULL,
						eligible_30 SMALLINT NULL,
						internal_30 SMALLINT NULL,
						eksternal_30 SMALLINT NULL
					)

	INSERT INTO #hasil
		(
		    kd_bidang,
		    bidang,
		    eligible_80,
		    internal_80,
		    eksternal_80,
		    eligible_70,
		    internal_70,
		    eksternal_70,
		    eligible_60,
		    internal_60,
		    eksternal_60,
		    eligible_50,
		    internal_50,
		    eksternal_50,
		    eligible_40,
		    internal_40,
		    eksternal_40,
		    eligible_30,
		    internal_30,
		    eksternal_30
		)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@eligible_kd_bid VARCHAR(3),
		--@bidang VARCHAR(100),
		@eligible_kd_gol VARCHAR(2),
		@eligible_jml SMALLINT--,
		--@jml_pelamar SMALLINT,
		--@selisih SMALLINT

	DECLARE eligible_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.KD_UNIT_ORG,3),
		--suo.NM_UNIT_ORG,
		eligible.KD_GOL_PEMENUHAN,
		eligible.JML
	FROM
		dbo.SPG_08_UNIT_ORGANISASI AS suo
		LEFT JOIN
		(
			SELECT 
				LEFT(KD_BIDANG,3) AS KD_BID,
				KD_GOL_PEMENUHAN,
				LEVEL_JABATAN_PEMENUHAN,
				COUNT(NIP) AS JML
			FROM 
				dbo.SPG_PEGAWAI_ELIGIBLE 
			WHERE 
				PERIODE_PEMENUHAN = @mpp_id 
				AND ELIGIBLE_COMBINE = 'ELIGIBLE'
				AND STS_PENSIUN = 0 
				--AND LEFT(KD_BIDANG,3) = @kd_bidang 
				--AND KD_GOL_PEMENUHAN = @kd_gol
			GROUP BY
				LEFT(KD_BIDANG,3),
				KD_GOL_PEMENUHAN,
				LEVEL_JABATAN_PEMENUHAN
		) AS eligible ON LEFT(suo.KD_UNIT_ORG,3) = eligible.KD_BID
	WHERE
		suo.KD_ESELON = '00'
		AND suo.SINGKATAN_UNIT_ORG <> '-'
	ORDER BY
		LEFT(suo.KD_UNIT_ORG,3),
		eligible.KD_GOL_PEMENUHAN DESC

	OPEN eligible_cursor
	FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @eligible_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'eligible_' + @eligible_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET eligible_' + @eligible_kd_gol + ' = @eligible_jml WHERE kd_bidang = @eligible_kd_bid'
						EXEC sp_executesql @sql, N'@eligible_jml SMALLINT, @eligible_kd_bid VARCHAR(3)', @eligible_jml = @eligible_jml, @eligible_kd_bid = @eligible_kd_bid
					END
			END


	  FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml
	END

	CLOSE eligible_cursor
	DEALLOCATE eligible_cursor

	---------------------------------------- internal

	DECLARE
		@pelamar_kd_bid VARCHAR(3),
		@pelamar_kd_gol VARCHAR(2),
		@pelamar_int SMALLINT,
		@pelamar_eks SMALLINT

	DECLARE pelamar_internal_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.KD_UNIT_ORG,3),
		pelamar_int.KD_GOL,
		COALESCE(pelamar_int.JML_INT, 0) AS JML_INT
	FROM
		dbo.SPG_08_UNIT_ORGANISASI AS suo
		LEFT JOIN
		(
			SELECT
				x.KD_BID,
				x.KD_GOL,
				COUNT(x.NIP) AS JML_INT
			FROM
				(
					SELECT DISTINCT
						LEFT(rpi.KD_UNIT_ORG,3) AS KD_BID,
						rp.KD_GOL,
						rpi.NIP
					FROM
						dbo.REC_PELAMAR_INTERNAL AS rpi INNER JOIN
						dbo.REC_PTK AS rp ON rpi.ID_FORMASI = rp.ID_FORMASI
					WHERE
						rp.MPP_ID = @mpp_id
						--AND rp.KD_GOL = @kd_gol
						--AND LEFT(rpi.KD_UNIT_ORG,3) = @kd_bidang	-- bidang si pelamar
						--AND COALESCE(rpi.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
						AND COALESCE(rpi.REKOMENDASI_PIMPINAN,'') = 'Y'
						AND LEFT(rpi.KD_UNIT_ORG,3) = LEFT(rp.KD_UNIT_ORG,3)
				) AS x
			GROUP BY
				x.KD_BID,
				x.KD_GOL
		) AS pelamar_int ON LEFT(suo.KD_UNIT_ORG,3) = pelamar_int.KD_BID
	WHERE
		suo.KD_ESELON = '00'
		AND suo.SINGKATAN_UNIT_ORG <> '-'

	OPEN pelamar_internal_cursor
	FETCH NEXT FROM pelamar_internal_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_int

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @pelamar_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'eligible_' + @pelamar_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						--DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET internal_' + @pelamar_kd_gol + ' = @pelamar_int  WHERE kd_bidang = @pelamar_kd_bid'
						EXEC sp_executesql @sql, N'@pelamar_int SMALLINT, @pelamar_kd_bid VARCHAR(3)', @pelamar_int = @pelamar_int, @pelamar_kd_bid = @pelamar_kd_bid
					END
			END


	  FETCH NEXT FROM pelamar_internal_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_int
	END

	CLOSE pelamar_internal_cursor
	DEALLOCATE pelamar_internal_cursor

	---------------------------------------- ekternal

	SET @pelamar_kd_bid = NULL
	SET @pelamar_kd_gol = NULL

	DECLARE pelamar_eksternal_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.KD_UNIT_ORG,3),
		pelamar_eks.KD_GOL,
		COALESCE(pelamar_eks.JML_EKS, 0) AS JML_EKS
	FROM
		dbo.SPG_08_UNIT_ORGANISASI AS suo
		LEFT JOIN
		(
			SELECT
				x.KD_BID,
				x.KD_GOL,
				COUNT(x.NIP) AS JML_EKS
			FROM
				(
					SELECT DISTINCT
						LEFT(rpi.KD_UNIT_ORG,3) AS KD_BID,
						rp.KD_GOL,
						rpi.NIP
					FROM
						dbo.REC_PELAMAR_INTERNAL AS rpi INNER JOIN
						dbo.REC_PTK AS rp ON rpi.ID_FORMASI = rp.ID_FORMASI
					WHERE
						rp.MPP_ID = @mpp_id
						--AND rp.KD_GOL = @kd_gol
						--AND LEFT(rpi.KD_UNIT_ORG,3) = @kd_bidang	-- bidang si pelamar
						--AND COALESCE(rpi.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
						AND COALESCE(rpi.REKOMENDASI_PIMPINAN,'') = 'Y'
						AND LEFT(rpi.KD_UNIT_ORG,3) <> LEFT(rp.KD_UNIT_ORG,3)
				) AS x
			GROUP BY
				x.KD_BID,
				x.KD_GOL
		) AS pelamar_eks ON LEFT(suo.KD_UNIT_ORG,3) = pelamar_eks.KD_BID
	WHERE
		suo.KD_ESELON = '00'
		AND suo.SINGKATAN_UNIT_ORG <> '-'

	OPEN pelamar_eksternal_cursor
	FETCH NEXT FROM pelamar_eksternal_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_eks

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @pelamar_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'eligible_' + @pelamar_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						--DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET eksternal_' + @pelamar_kd_gol + ' = @pelamar_eks WHERE kd_bidang = @pelamar_kd_bid'
						EXEC sp_executesql @sql, N'@pelamar_eks SMALLINT, @pelamar_kd_bid VARCHAR(3)', @pelamar_eks = @pelamar_eks, @pelamar_kd_bid = @pelamar_kd_bid
					END
			END


	  FETCH NEXT FROM pelamar_eksternal_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_eks
	END

	CLOSE pelamar_eksternal_cursor
	DEALLOCATE pelamar_eksternal_cursor

	SELECT * FROM #hasil ORDER BY kd_bidang

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
	    DROP TABLE #hasil;

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_eligible]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_eligible]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)	-- contoh  = '60'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--SELECT
	--	*
	--FROM
	--	dbo.SPG_PEGAWAI_ELIGIBLE AS d
	--WHERE
	--	d.PERIODE_PEMENUHAN = @mpp_id
	--	AND d.STS_PENSIUN = 0
	--	AND (d.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')		-- kalau query-nya begini, KD_GOL-nya salah, karena ini posisi pegawai saat ini, bukan eligible
	--	AND (LEFT(d.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')

	SELECT DISTINCT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATUAN_KERJA,
		a.LEVEL_JABATAN,
		a.NM_JABATAN,
		a.FORMASI,
		a.MASA_DINAS_LEVEL_JABATAN,
		a.KETERANGAN,
		a.KD_BIDANG,
		a.LEVEL_JABATAN_PEMENUHAN,
		a.KD_GOL, 
		a.KD_JABATAN_STR
	FROM 
		dbo.SPG_PEGAWAI_ELIGIBLE AS a
		INNER JOIN 
		(
			SELECT 
				b.req_id, 
				a.ptk_id, 
				c.satker, 
				e.NM_UNIT_ORG AS depkom, 
				c.nm_kantor AS kantor, 
				d.KD_UNIT_ES1 AS kd_bidang, 
				m.masa_dinas_minimal, 
				m.sisa_dinas, 
				m.tanggal_dinas, 
				c.bidang, 
				c.FORMASI AS posisi_jabatan, 
				a.kd_jabatan, 
				b.tanggal_publish, 
				b.tanggal_selesai, 
				b.jam_selesai, 
				m.kd_gol, 
				a.mpp_id, 
				a.NM_GOLONGAN
			FROM 
				rec_ptk AS a
				LEFT JOIN
				rec_permintaan_advertising AS b ON a.ptk_id = b.req_id
				LEFT JOIN
				REC_RENCANA_PEMENUHAN_PEG AS c ON a.ID_FORMASI = c.ID_FORMASI
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS d ON a.KD_UNIT_ORG = d.KD_UNIT_ORG
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS e ON d.KD_UNIT_ES2 = e.KD_UNIT_ORG
				LEFT JOIN
				REC_PERSYARATAN AS m ON m.KD_GOL = a.KD_GOL AND m.periode = a.mpp_id
				LEFT JOIN
				SPG_JNS_PEND_UMUM AS n ON m.PENDIDIKAN = n.KD_PEND_UMUM
			WHERE
				b.ram_id = 'RAM-000020'
				AND b.rac_id = 'RAC-000001'
				AND c.STATUS_KIRIM = 1
				AND c.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (c.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS b ON a.periode_pemenuhan = b.mpp_id AND a.level_jabatan_pemenuhan = b.nm_golongan
	WHERE
		a.PERIODE_PEMENUHAN = @mpp_id
		AND a.ELIGIBLE_COMBINE = 'ELIGIBLE'
		AND a.STS_PENSIUN = 0
		AND (LEFT(a.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')	-- bukan bidang lowongan, tapi bidang eligible
	ORDER BY 
		a.KD_BIDANG, 
		a.KD_GOL DESC, 
		a.KD_JABATAN_STR DESC,
		a.MASA_DINAS_LEVEL_JABATAN DESC,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan]
	@mpp_id VARCHAR(10)	-- contoh 'MPP-000003'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						eligible_80 SMALLINT NULL,
						melamar_80 SMALLINT NULL,
						selisih_80 SMALLINT NULL,
						eligible_70 SMALLINT NULL,
						melamar_70 SMALLINT NULL,
						selisih_70 SMALLINT NULL,
						eligible_60 SMALLINT NULL,
						melamar_60 SMALLINT NULL,
						selisih_60 SMALLINT NULL,
						eligible_50 SMALLINT NULL,
						melamar_50 SMALLINT NULL,
						selisih_50 SMALLINT NULL,
						eligible_40 SMALLINT NULL,
						melamar_40 SMALLINT NULL,
						selisih_40 SMALLINT NULL,
						eligible_30 SMALLINT NULL,
						melamar_30 SMALLINT NULL,
						selisih_30 SMALLINT NULL
					)

	INSERT INTO #hasil
		(
		    kd_bidang,
		    bidang,
		    eligible_80,
		    melamar_80,
		    selisih_80,
		    eligible_70,
		    melamar_70,
		    selisih_70,
		    eligible_60,
		    melamar_60,
		    selisih_60,
		    eligible_50,
		    melamar_50,
		    selisih_50,
		    eligible_40,
		    melamar_40,
		    selisih_40,
		    eligible_30,
		    melamar_30,
		    selisih_30
		)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@eligible_kd_bid VARCHAR(3),
		--@bidang VARCHAR(100),
		@eligible_kd_gol VARCHAR(2),
		@eligible_jml SMALLINT--,
		--@jml_pelamar SMALLINT,
		--@selisih SMALLINT

	DECLARE eligible_cursor CURSOR FAST_FORWARD FOR
	--SELECT
	--	a.BIDANG,
	--	a.KD_GOL,
	--	--a.NM_GOL,
	--	a.KD_JABATAN,
	--	SUM(peg.eligible) AS eligible,
	--	SUM(g.jml_pelamar) AS jml_pelamar,
	--	SUM(peg.eligible) - SUM(g.jml_pelamar) AS selisih
	--FROM
	--	REC_RENCANA_PEMENUHAN_PEG a
	--	INNER JOIN
	--	REC_PTK b ON
	--		a.ID_FORMASI = b.ID_FORMASI
	--	INNER JOIN
	--	REC_PERMINTAAN_ADVERTISING c ON
	--		b.PTK_ID = c.REQ_ID

	--	OUTER APPLY
	--	(
	--		SELECT
	--			COUNT(*) AS eligible
	--		FROM
	--			dbo.SPG_PEGAWAI_ELIGIBLE AS d
	--			INNER JOIN
	--			SPG_GOLONGAN AS sg ON d.LEVEL_JABATAN_PEMENUHAN = sg.NM_GOLONGAN 
	--		WHERE
	--			d.PERIODE_PEMENUHAN = a.MPP_ID
	--			AND d.STS_PENSIUN = 0
	--			--AND d.KD_GOL = a.KD_GOL
	--			AND sg.KD_GOL = a.KD_GOL
	--			--AND d.KD_JABATAN_STR = a.KD_JABATAN	-- ?
	--			AND LEFT(d.KD_BIDANG,3) = LEFT(a.KD_UNIT_ORG,3)
	--	) AS peg
	--	OUTER APPLY
	--	(
	--		SELECT
	--			COUNT(*) AS jml_pelamar
	--		FROM
	--			REC_PELAMAR_INTERNAL f
	--		WHERE
	--			b.PTK_ID = f.REQ_ID
	--			AND c.REQ_ID = f.REQ_ID
	--			AND COALESCE(f.NIP_REKOMENDASI, '') <> ''	-- sudah melalui rekomendasi pimpinan
	--	) g
	--WHERE
	--	a.STATUS_KIRIM = 1
	--	AND a.STATUS_AKTIF = 1
	--	AND a.MPP_ID = @mpp_id
	--GROUP BY
	--	LEFT(a.KD_UNIT_ORG,3),
	--	a.BIDANG,
	--	a.KD_GOL,
	--	--a.NM_GOL
	--	a.KD_JABATAN
	--ORDER BY
	--	LEFT(a.KD_UNIT_ORG,3),
	--	a.KD_GOL DESC;

	SELECT
		LEFT(suo.KD_UNIT_ORG,3),
		--suo.NM_UNIT_ORG,
		eligible.KD_GOL_PEMENUHAN,
		eligible.JML
	FROM
		dbo.SPG_08_UNIT_ORGANISASI AS suo
		LEFT JOIN
		(
			SELECT 
				LEFT(KD_BIDANG,3) AS KD_BID,
				KD_GOL_PEMENUHAN,
				LEVEL_JABATAN_PEMENUHAN,
				COUNT(NIP) AS JML
			FROM 
				dbo.SPG_PEGAWAI_ELIGIBLE 
			WHERE 
				PERIODE_PEMENUHAN = @mpp_id 
				AND ELIGIBLE_COMBINE = 'ELIGIBLE'
				AND STS_PENSIUN = 0 
				--AND LEFT(KD_BIDANG,3) = @kd_bidang 
				--AND KD_GOL_PEMENUHAN = @kd_gol
			GROUP BY
				LEFT(KD_BIDANG,3),
				KD_GOL_PEMENUHAN,
				LEVEL_JABATAN_PEMENUHAN
		) AS eligible ON LEFT(suo.KD_UNIT_ORG,3) = eligible.KD_BID
	WHERE
		suo.KD_ESELON = '00'
		AND suo.SINGKATAN_UNIT_ORG <> '-'
	ORDER BY
		LEFT(suo.KD_UNIT_ORG,3),
		eligible.KD_GOL_PEMENUHAN DESC

	OPEN eligible_cursor
	FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @eligible_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'eligible_' + @eligible_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET eligible_' + @eligible_kd_gol + ' = @eligible_jml WHERE kd_bidang = @eligible_kd_bid'
						EXEC sp_executesql @sql, N'@eligible_jml SMALLINT, @eligible_kd_bid VARCHAR(3)', @eligible_jml = @eligible_jml, @eligible_kd_bid = @eligible_kd_bid
					END
			END


	  FETCH NEXT FROM eligible_cursor INTO @eligible_kd_bid, @eligible_kd_gol, @eligible_jml
	END

	CLOSE eligible_cursor
	DEALLOCATE eligible_cursor


	DECLARE
		@pelamar_kd_bid VARCHAR(3),
		@pelamar_kd_gol VARCHAR(2),
		@pelamar_jml SMALLINT--,

	DECLARE pelamar_cursor CURSOR FAST_FORWARD FOR
	SELECT
		LEFT(suo.KD_UNIT_ORG,3),
		--suo.NM_UNIT_ORG,
		pelamar.KD_GOL,
		pelamar.JML
	FROM
		dbo.SPG_08_UNIT_ORGANISASI AS suo
		LEFT JOIN
		(
			--SELECT
			--	LEFT(rpi.KD_UNIT_ORG,3) AS KD_BID,
			--	rp.KD_GOL,
			--	rp.NM_GOLONGAN,
			--	COUNT(rpi.NIP) AS JML
			--FROM
			--	dbo.REC_PELAMAR_INTERNAL AS rpi INNER JOIN
			--	dbo.REC_PTK AS rp ON rpi.ID_FORMASI = rp.ID_FORMASI
			--WHERE
			--	rp.MPP_ID = @mpp_id
			--	--AND rp.KD_GOL = @kd_gol
			--	--AND LEFT(rpi.KD_UNIT_ORG,3) = @kd_bidang	-- bidang si pelamar
			--	AND COALESCE(rpi.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
			--GROUP BY
			--	LEFT(rpi.KD_UNIT_ORG,3),
			--	rp.KD_GOL,
			--	rp.NM_GOLONGAN

			SELECT
				x.KD_BID,
				x.KD_GOL,
				--x.NM_GOLONGAN,
				COUNT(x.NIP) AS JML
			FROM
				(
					SELECT DISTINCT
						LEFT(rpi.KD_UNIT_ORG,3) AS KD_BID,
						rp.KD_GOL,
						--rp.NM_GOLONGAN,
						rpi.NIP
					FROM
						dbo.REC_PELAMAR_INTERNAL AS rpi INNER JOIN
						dbo.REC_PTK AS rp ON rpi.ID_FORMASI = rp.ID_FORMASI
					WHERE
						rp.MPP_ID = @mpp_id
						--AND rp.KD_GOL = @kd_gol
						--AND LEFT(rpi.KD_UNIT_ORG,3) = @kd_bidang	-- bidang si pelamar
						--AND COALESCE(rpi.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
						AND COALESCE(rpi.REKOMENDASI_PIMPINAN,'') = 'Y'
				) AS x
			GROUP BY
				x.KD_BID,
				x.KD_GOL--,
				--x.NM_GOLONGAN
		) AS pelamar ON LEFT(suo.KD_UNIT_ORG,3) = pelamar.KD_BID
	WHERE
		suo.KD_ESELON = '00'
		AND suo.SINGKATAN_UNIT_ORG <> '-'

	OPEN pelamar_cursor
	FETCH NEXT FROM pelamar_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_jml

	WHILE @@FETCH_STATUS = 0
	BEGIN

		IF EXISTS(SELECT 1 FROM #hasil WHERE kd_bidang = @pelamar_kd_bid)
			BEGIN
				IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'eligible_' + @pelamar_kd_gol AND TABLE_NAME LIKE '#hasil%')
					BEGIN
						--DECLARE @sql nvarchar(1000)
						SET @sql = 'UPDATE #hasil SET melamar_' + @pelamar_kd_gol + ' = @pelamar_jml WHERE kd_bidang = @pelamar_kd_bid'
						EXEC sp_executesql @sql, N'@pelamar_jml SMALLINT, @pelamar_kd_bid VARCHAR(3)', @pelamar_jml = @pelamar_jml, @pelamar_kd_bid = @pelamar_kd_bid
					END
			END


	  FETCH NEXT FROM pelamar_cursor INTO @pelamar_kd_bid, @pelamar_kd_gol, @pelamar_jml
	END

	CLOSE pelamar_cursor
	DEALLOCATE pelamar_cursor

	UPDATE
		#hasil
	SET
		selisih_60 = eligible_60 - melamar_60,
		selisih_50 = eligible_50 - melamar_50,
		selisih_40 = eligible_40 - melamar_40

	SELECT * FROM #hasil ORDER BY kd_bidang

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
	    DROP TABLE #hasil;

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_eligible]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_eligible]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)	-- contoh  = '60'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--SELECT
	--	*
	--FROM
	--	dbo.SPG_PEGAWAI_ELIGIBLE AS d
	--WHERE
	--	d.PERIODE_PEMENUHAN = @mpp_id
	--	AND d.STS_PENSIUN = 0
	--	AND (d.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')		-- kalau query-nya begini, KD_GOL-nya salah, karena ini posisi pegawai saat ini, bukan eligible
	--	AND (LEFT(d.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')

	SELECT DISTINCT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATUAN_KERJA,
		a.LEVEL_JABATAN,
		a.NM_JABATAN,
		a.FORMASI,
		a.MASA_DINAS_LEVEL_JABATAN,
		a.KETERANGAN,
		a.KD_BIDANG,
		a.LEVEL_JABATAN_PEMENUHAN,
		a.KD_GOL, 
		a.KD_JABATAN_STR
	FROM 
		dbo.SPG_PEGAWAI_ELIGIBLE AS a
		INNER JOIN 
		(
			SELECT 
				b.req_id, 
				a.ptk_id, 
				c.satker, 
				e.NM_UNIT_ORG AS depkom, 
				c.nm_kantor AS kantor, 
				d.KD_UNIT_ES1 AS kd_bidang, 
				m.masa_dinas_minimal, 
				m.sisa_dinas, 
				m.tanggal_dinas, 
				c.bidang, 
				c.FORMASI AS posisi_jabatan, 
				a.kd_jabatan, 
				b.tanggal_publish, 
				b.tanggal_selesai, 
				b.jam_selesai, 
				m.kd_gol, 
				a.mpp_id, 
				a.NM_GOLONGAN
			FROM 
				rec_ptk AS a
				LEFT JOIN
				rec_permintaan_advertising AS b ON a.ptk_id = b.req_id
				LEFT JOIN
				REC_RENCANA_PEMENUHAN_PEG AS c ON a.ID_FORMASI = c.ID_FORMASI
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS d ON a.KD_UNIT_ORG = d.KD_UNIT_ORG
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS e ON d.KD_UNIT_ES2 = e.KD_UNIT_ORG
				LEFT JOIN
				REC_PERSYARATAN AS m ON m.KD_GOL = a.KD_GOL AND m.periode = a.mpp_id
				LEFT JOIN
				SPG_JNS_PEND_UMUM AS n ON m.PENDIDIKAN = n.KD_PEND_UMUM
			WHERE
				b.ram_id = 'RAM-000020'
				AND b.rac_id = 'RAC-000001'
				AND c.STATUS_KIRIM = 1
				AND c.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (c.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS b ON a.periode_pemenuhan = b.mpp_id AND a.level_jabatan_pemenuhan = b.nm_golongan
	WHERE
		a.PERIODE_PEMENUHAN = @mpp_id
		AND a.ELIGIBLE_COMBINE = 'ELIGIBLE'
		AND a.STS_PENSIUN = 0
		AND (LEFT(a.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')	-- bukan bidang lowongan, tapi bidang eligible
	ORDER BY 
		a.KD_BIDANG, 
		a.KD_GOL DESC, 
		a.KD_JABATAN_STR DESC,
		a.MASA_DINAS_LEVEL_JABATAN DESC,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_melamar]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





-- by Pribadi Pamungkas. 2021-02-16
CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_melamar]
	@mpp_id varchar(10),
	@kd_bidang VARCHAR(3),
	@kd_gol VARCHAR(2)
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		a.MASA_DINAS_LVL_JABATAN,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.kantor,
		b.satuan_kerja,
		b.posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				j.POSISI AS posisi_jabatan
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (x.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')	--varchar(2)
		) AS b ON a.ID_FORMASI = b.ID_FORMASI
	WHERE
		(LEFT(a.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
	ORDER BY
		LEFT(a.KD_UNIT_ORG, 3),
		a.NM_PEG,
		a.NIP

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_selisih]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_lowongan_jabatan_selisih]
	@mpp_id varchar(10),	-- contoh  = 'MPP-000002',
	@kd_bidang varchar(3),	-- contoh  = '101'
	@kd_gol VARCHAR(2)	-- contoh  = '60'
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT DISTINCT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATUAN_KERJA,
		a.LEVEL_JABATAN,
		a.NM_JABATAN,
		a.FORMASI,
		a.MASA_DINAS_LEVEL_JABATAN,
		a.KETERANGAN,
		a.KD_BIDANG,
		a.LEVEL_JABATAN_PEMENUHAN,
		a.KD_GOL, 
		a.KD_JABATAN_STR
	FROM 
		dbo.SPG_PEGAWAI_ELIGIBLE AS a
		INNER JOIN 
		(
			SELECT 
				b.req_id, 
				a.ptk_id, 
				c.satker, 
				e.NM_UNIT_ORG AS depkom, 
				c.nm_kantor AS kantor, 
				d.KD_UNIT_ES1 AS kd_bidang, 
				m.masa_dinas_minimal, 
				m.sisa_dinas, 
				m.tanggal_dinas, 
				c.bidang, 
				c.FORMASI AS posisi_jabatan, 
				a.kd_jabatan, 
				b.tanggal_publish, 
				b.tanggal_selesai, 
				b.jam_selesai, 
				m.kd_gol, 
				a.mpp_id, 
				a.NM_GOLONGAN
			FROM 
				rec_ptk AS a
				LEFT JOIN
				rec_permintaan_advertising AS b ON a.ptk_id = b.req_id
				LEFT JOIN
				REC_RENCANA_PEMENUHAN_PEG AS c ON a.ID_FORMASI = c.ID_FORMASI
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS d ON a.KD_UNIT_ORG = d.KD_UNIT_ORG
				LEFT JOIN
				SPG_08_UNIT_ORGANISASI AS e ON d.KD_UNIT_ES2 = e.KD_UNIT_ORG
				LEFT JOIN
				REC_PERSYARATAN AS m ON m.KD_GOL = a.KD_GOL AND m.periode = a.mpp_id
				LEFT JOIN
				SPG_JNS_PEND_UMUM AS n ON m.PENDIDIKAN = n.KD_PEND_UMUM
			WHERE
				b.ram_id = 'RAM-000020'
				AND b.rac_id = 'RAC-000001'
				AND c.STATUS_KIRIM = 1
				AND c.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (c.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS b ON a.periode_pemenuhan = b.mpp_id AND a.level_jabatan_pemenuhan = b.nm_golongan
	WHERE
		a.PERIODE_PEMENUHAN = @mpp_id
		AND a.ELIGIBLE_COMBINE = 'ELIGIBLE'
		AND a.STS_PENSIUN = 0
		AND (LEFT(a.KD_BIDANG,3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')	-- bukan bidang lowongan, tapi bidang eligible
		AND a.NIP NOT IN (
						SELECT
							a.NIP
						FROM
							dbo.REC_PELAMAR_INTERNAL AS a
							INNER JOIN
							(
								SELECT
									x.ID_FORMASI,
									x.KD_UNIT_ORG,
									g.NM_UNIT_ORG AS nm_bidang,
									c.NM_KANTOR AS kantor,
									CASE
										WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																						WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																						ELSE f.NM_UNIT_ORG
																					END
										ELSE d.nama_satker_organisasi
									END AS satuan_kerja,
									j.POSISI AS posisi_jabatan
								FROM
									dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
									dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
									dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
									dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
									dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
									dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
									dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
									dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
								WHERE
									z.RAM_ID = 'RAM-000020' 
									AND z.RAC_ID = 'RAC-000001'
									AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
									AND x.STATUS_KIRIM = 1
									AND x.STATUS_AKTIF = 1
									AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
									AND (x.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')	--varchar(2)
							) AS b ON a.ID_FORMASI = b.ID_FORMASI
						WHERE
							(LEFT(a.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
							--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
							AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
					)
	ORDER BY 
		a.KD_BIDANG, 
		a.KD_GOL DESC, 
		a.KD_JABATAN_STR DESC,
		a.MASA_DINAS_LEVEL_JABATAN DESC,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pegawai_eligible_yang_melamar_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




-- by Pribadi Pamungkas. 2021-02-16
CREATE PROCEDURE [dbo].[rep_pegawai_eligible_yang_melamar_pelamar]
	@mpp_id varchar(10),
	@kd_bidang VARCHAR(3),
	@kd_gol VARCHAR(2),
	@internal bit	-- 1: internal, 0/null: eksternal
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		a.MASA_DINAS_LVL_JABATAN,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.kantor,
		b.satuan_kerja,
		b.posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				j.POSISI AS posisi_jabatan
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (x.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')	--varchar(2)
		) AS b ON a.ID_FORMASI = b.ID_FORMASI
	WHERE
		(LEFT(a.KD_UNIT_ORG, 3) = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
		AND (LEFT(a.KD_UNIT_ORG,3) = LEFT(b.KD_UNIT_ORG,3) OR ISNULL(@internal, 0) = 0)
		AND (LEFT(a.KD_UNIT_ORG,3) <> LEFT(b.KD_UNIT_ORG,3) OR ISNULL(@internal, 0) = 1)
	ORDER BY
		LEFT(a.KD_UNIT_ORG, 3),
		a.NM_PEG,
		a.NIP
		

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[rep_pelamar_per_lowongan]
	@mpp_id varchar(10),
	@kd_bidang varchar(50)
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		lowjab.bid,
		lowjab.BIDANG,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		SUM(COALESCE(terisi.id_formasi_terisi,0)) AS pelamar,
		lowjab.id_formasi,
		lowjab.KD_GOL,
		SUM(lowjab.jumlah) AS jumlah
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				r.BIDANG,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.kd_kantor,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				r.JNS_JABATAN,
				a.JUMLAH AS jumlah
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
				--CROSS APPLY dbo.SplitStrings_CTE(@kd_bidang, ',') AS bid
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				--AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				--AND LEFT(r.KD_UNIT_ORG, 3) = bid.Item
				AND LEFT(r.KD_UNIT_ORG, 3) IN (SELECT Item FROM  dbo.SplitStrings_CTE(@kd_bidang, ','))
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				--AND r.KD_GOL = @kd_gol	--varchar(2)
		) AS lowjab
		OUTER APPLY
		(
			SELECT 
				COUNT(*) AS id_formasi_terisi
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
			WHERE
				COALESCE(d.NIP_REKOMENDASI, '') <> ''
				AND d.id_formasi = lowjab.id_formasi
			GROUP BY
				d.id_formasi
		) AS terisi
	GROUP BY
		lowjab.bid,
		lowjab.BIDANG,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.kd_kantor,
		lowjab.posisi_jabatan,
		lowjab.KD_GOL,
		lowjab.id_formasi,
		lowjab.JNS_JABATAN,
		lowjab.KD_GOL
	ORDER BY
		lowjab.bid,
		lowjab.KD_GOL DESC,
		lowjab.kd_kantor,
		lowjab.JNS_JABATAN DESC
END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan_detail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[rep_pelamar_per_lowongan_detail]
	@id_formasi int
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.MASA_DINAS_LVL_JABATAN,
		d.NM_ROOM
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		LEFT JOIN
		dbo.REC_ROOM_FORMASI_FUNCTIONAL AS d
		ON a.ID_ROOM = d.ID AND a.ID_FORMASI = d.ID_FORMASI
	WHERE
		a.ID_FORMASI = @id_formasi
		AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
	ORDER BY
		d.NM_ROOM,
		a.MASA_DINAS_LVL_JABATAN DESC,
		a.NM_PEG

END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_per_lowongan_excel]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- by Pribadi Pamungkas. 2021-01-19

CREATE PROCEDURE [dbo].[rep_pelamar_per_lowongan_excel]
	@mpp_id varchar(10),
	@kd_bidang varchar(50)
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		lowjab.bid,
		lowjab.BIDANG AS BIDANG_LOWJAB,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.posisi_jabatan,
		r.NM_ROOM,
		--SUM(COALESCE(terisi.id_formasi_terisi,0)) AS pelamar,
		--lowjab.id_formasi
		rpi.NIP,
		rpi.NM_PEG,
		rpi.BIDANG,
		rpi.SATKER,
		rpi.FORMASI,
		rpi.MASA_DINAS_LVL_JABATAN
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				r.BIDANG,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.kd_kantor,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				l.KD_GOL,
				r.JNS_JABATAN
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
				--CROSS APPLY dbo.SplitStrings_CTE(@kd_bidang, ',') AS bid
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				--AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				--AND LEFT(r.KD_UNIT_ORG, 3) = bid.Item
				AND LEFT(r.KD_UNIT_ORG, 3) IN (SELECT Item FROM  dbo.SplitStrings_CTE(@kd_bidang, ','))
				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				--AND r.KD_GOL = @kd_gol	--varchar(2)
		) AS lowjab
		--OUTER APPLY
		--(
		--	SELECT 
		--		COUNT(*) AS id_formasi_terisi
		--	FROM
		--		dbo.REC_PELAMAR_INTERNAL AS d
		--	WHERE
		--		COALESCE(d.NIP_REKOMENDASI, '') <> ''
		--		AND d.id_formasi = lowjab.id_formasi
		--	GROUP BY
		--		d.id_formasi
		--) AS terisi
		LEFT JOIN
		dbo.REC_PELAMAR_INTERNAL AS rpi ON lowjab.ID_FORMASI = rpi.ID_FORMASI
		LEFT JOIN
		dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r ON rpi.ID_ROOM = r.ID AND rpi.ID_FORMASI = r.ID_FORMASI
	WHERE
		COALESCE(rpi.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
	--GROUP BY
	--	lowjab.bid,
	--	lowjab.BIDANG,
	--	lowjab.satuan_kerja,
	--	lowjab.kantor,
	--	lowjab.kd_kantor,
	--	lowjab.posisi_jabatan,
	--	lowjab.KD_GOL,
	--	lowjab.id_formasi,
	--	lowjab.JNS_JABATAN
	ORDER BY
		lowjab.bid,
		lowjab.kd_kantor,
		lowjab.KD_GOL DESC,
		lowjab.JNS_JABATAN DESC,
		r.NM_ROOM,
		rpi.NM_PEG
		--rpi.NIP
END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- last modified by Pribadi Pamungkas 2021-01-08 => jadi akumulasi (history hari tsb dan sebelumnya, asal belum rekomendasi, tambah jml_pelamar)

CREATE PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi]
	@mpp_id     VARCHAR(10), -- contoh 'MPP-000003'
	@tgl_posisi DATE       	-- contoh '2020-11-04'
AS
BEGIN
	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	CREATE TABLE #hasil  (
    						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
    						lowjab_90 SMALLINT NULL,
    						terisi_90 SMALLINT NULL,
    						belum_terisi_90 SMALLINT NULL,
						jml_pelamar_90 SMALLINT NULL,
    						lowjab_80 SMALLINT NULL,
    						terisi_80 SMALLINT NULL,
    						belum_terisi_80 SMALLINT NULL,
						jml_pelamar_80 SMALLINT NULL,
    						lowjab_70 SMALLINT NULL,
    						terisi_70 SMALLINT NULL,
    						belum_terisi_70 SMALLINT NULL,
						jml_pelamar_70 SMALLINT NULL,
    						lowjab_60 SMALLINT NULL,
    						terisi_60 SMALLINT NULL,
    						belum_terisi_60 SMALLINT NULL,
						jml_pelamar_60 SMALLINT NULL,
    						lowjab_50 SMALLINT NULL,
    						terisi_50 SMALLINT NULL,
    						belum_terisi_50 SMALLINT NULL,
						jml_pelamar_50 SMALLINT NULL,
    						lowjab_40 SMALLINT NULL,
    						terisi_40 SMALLINT NULL,
    						belum_terisi_40 SMALLINT NULL,
						jml_pelamar_40 SMALLINT NULL,
    						lowjab_30 SMALLINT NULL,
    						terisi_30 SMALLINT NULL,
    						belum_terisi_30 SMALLINT NULL,
						jml_pelamar_30 SMALLINT NULL
    					)

	INSERT INTO #hasil (
    						kd_bidang,
						bidang,
    						lowjab_90,
    						terisi_90,
    						belum_terisi_90,
						jml_pelamar_90,
    						lowjab_80,
    						terisi_80,
    						belum_terisi_80,
						jml_pelamar_80,
    						lowjab_70,
    						terisi_70,
    						belum_terisi_70,
						jml_pelamar_70,
    						lowjab_60,
    						terisi_60,
    						belum_terisi_60,
						jml_pelamar_60,
    						lowjab_50,
    						terisi_50,
    						belum_terisi_50,
						jml_pelamar_50,
    						lowjab_40,
    						terisi_40,
    						belum_terisi_40,
						jml_pelamar_40,
    						lowjab_30,
    						terisi_30,
    						belum_terisi_30,
						jml_pelamar_30
					)
	SELECT
		LEFT(KD_UNIT_ORG,3), NM_UNIT_ORG,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	FROM
		dbo.SPG_08_UNIT_ORGANISASI
	WHERE
		KD_ESELON = '00'
		AND LEFT(KD_UNIT_ORG,3) >= '101'
		AND LEFT(KD_UNIT_ORG,3) <= '107'
	ORDER BY
		NO_URUT

	DECLARE
		@kd_bidang VARCHAR(3),
		@bidang VARCHAR(100),
		@kd_gol VARCHAR(2),
		@jml SMALLINT,
		@terisi SMALLINT,
		@belum_terisi SMALLINT,
		@jml_pelamar SMALLINT
	DECLARE list_cursor CURSOR FAST_FORWARD FOR
	SELECT
		--LEFT(x.NM_UNIT_ORG,3),
		x.bid,
		x.NM_UNIT_ORG,
		x.KD_GOL,
		SUM(x.jml) AS jml,
		SUM(x.terisi) AS terisi,
		SUM(x.jml) - SUM(x.terisi) AS belum_terisi,
		SUM(x.jml_pelamar) AS jml_pelamar
	FROM
		(
		    SELECT
				suo.NM_UNIT_ORG,
				LEFT(suo.KD_UNIT_ORG, 3) AS bid,
				lowjab.KD_GOL,
				lowjab.jml,
				--coalesce(terisi.id_formasi_terisi,0) as id_formasi_terisi,
				IIF(terisi.id_formasi_terisi IS NULL,0,1) as terisi,
				COALESCE(jml_pelamar.jml_pelamar,0) AS jml_pelamar
		    FROM
				dbo.SPG_08_UNIT_ORGANISASI AS suo
				CROSS APPLY
				(
					SELECT
						a.MPP_ID,
						LEFT(a.KD_UNIT_ORG, 3) AS bid,
						a.KD_GOL,
						a.ID_FORMASI,
						COALESCE(CAST(r.ID AS VARCHAR(10)), '-') AS ID_ROOM,
						--SUM(b.JUMLAH) AS jml
						COUNT(b.JUMLAH) AS jml
					FROM
						dbo.REC_RENCANA_PEMENUHAN_PEG AS a
						INNER JOIN
						dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
						LEFT JOIN
						dbo.REC_PERMINTAAN_ADVERTISING AS c ON b.PTK_ID = c.REQ_ID
						LEFT JOIN
						dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r ON a.ID_FORMASI = r.ID_FORMASI
					WHERE
						a.STATUS_KIRIM = 1
						AND a.STATUS_AKTIF = 1
						AND LEFT(a.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
						AND c.JAM_SELESAI IS NOT NULL	-- sudah posting
						AND a.MPP_ID = @mpp_id
					GROUP BY
						a.MPP_ID,
						LEFT(a.KD_UNIT_ORG, 3),
						a.KD_GOL,
						a.ID_FORMASI,
						r.ID
				) AS lowjab
				OUTER APPLY
				(
					SELECT 
						d.id_formasi AS id_formasi_terisi
					FROM
						dbo.REC_PELAMAR_INTERNAL AS d
						CROSS APPLY
						(
							SELECT 
								ID_PELAMAR 
							FROM 
								dbo.REC_PELAMAR_AKTIFITAS AS e
							WHERE
								d.ID = e.ID_PELAMAR
								--COALESCE(dbo.func_get_first_id_pelamar(d.ID_SEBELUMNYA), d.ID) = e.ID_PELAMAR
								AND e.STS_PELAMAR = 0
								AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) <= @tgl_posisi
								--AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) >= @tgl_posisi
							GROUP BY 
								ID_PELAMAR
						) AS c
					WHERE
						COALESCE(d.TGL_REKOMENDASI_PIMPINAN, GETDATE()) >= @tgl_posisi
						AND d.id_formasi = lowjab.id_formasi
						AND COALESCE(CAST(d.ID_ROOM AS VARCHAR(10)), '-') = lowjab.ID_ROOM
					GROUP BY
						d.id_formasi,
						d.ID_ROOM
				) AS terisi
				OUTER APPLY
				(
					SELECT 
						COUNT(f.id_formasi) AS jml_pelamar
					FROM
						dbo.REC_PELAMAR_INTERNAL AS f
						CROSS APPLY
						(
							SELECT 
								g.ID_PELAMAR 
							FROM 
								dbo.REC_PELAMAR_AKTIFITAS AS g
							WHERE
								f.ID = g.ID_PELAMAR
								--COALESCE(dbo.func_get_first_id_pelamar(f.ID_SEBELUMNYA), f.ID) = g.ID_PELAMAR
								AND g.STS_PELAMAR = 0
								AND CAST(g.TGL_BUAT_AKTIFITAS AS DATE) <= @tgl_posisi
								--AND CAST(g.TGL_BUAT_AKTIFITAS AS DATE) >= @tgl_posisi
							GROUP BY 
								g.ID_PELAMAR
						) AS h
					WHERE
						COALESCE(f.TGL_REKOMENDASI_PIMPINAN, GETDATE()) >= @tgl_posisi
						AND f.id_formasi = lowjab.id_formasi
						AND COALESCE(CAST(f.ID_ROOM AS VARCHAR(10)), '-') = lowjab.ID_ROOM
					GROUP BY
						f.id_formasi,
						f.ID_ROOM
				) AS jml_pelamar
		    WHERE
			    suo.KD_ESELON = '00'
			    AND LEFT(suo.KD_UNIT_ORG, 3) >= '101'
			    AND LEFT(suo.KD_UNIT_ORG, 3) <= '107'
			    AND lowjab.MPP_ID = @mpp_id
		) AS x
	GROUP BY
		x.NM_UNIT_ORG,
		x.bid,
		x.KD_GOL
	ORDER BY
		x.bid,
		x.KD_GOL DESC

    OPEN list_cursor
    FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @jml, @terisi, @belum_terisi, @jml_pelamar
    WHILE @@FETCH_STATUS = 0
    BEGIN
    	IF EXISTS(SELECT 1 FROM #hasil WHERE bidang = @bidang)
    		BEGIN
    			IF EXISTS (SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'lowjab_' + @kd_gol AND TABLE_NAME LIKE '#hasil%')
    				BEGIN
    					DECLARE @sql nvarchar(1000)
    					SET @sql = 'UPDATE #hasil SET lowjab_' + @kd_gol + ' = @jml, terisi_' + @kd_gol + ' = @terisi, belum_terisi_' + @kd_gol + ' = @belum_terisi, jml_pelamar_' + @kd_gol + ' = @jml_pelamar WHERE bidang = @bidang'
    					EXEC sp_executesql @sql, N'@jml SMALLINT, @terisi SMALLINT, @belum_terisi SMALLINT, @jml_pelamar SMALLINT, @bidang VARCHAR(100)', @jml = @jml, @terisi = @terisi, @belum_terisi = @belum_terisi, @jml_pelamar = @jml_pelamar, @bidang = @bidang
    				END
    		END
      FETCH NEXT FROM list_cursor INTO @kd_bidang, @bidang, @kd_gol, @jml, @terisi, @belum_terisi, @jml_pelamar
    END
    CLOSE list_cursor
    DEALLOCATE list_cursor

    SELECT * FROM #hasil ORDER BY kd_bidang

    IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
        DROP TABLE #hasil;

END;
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_detail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- by Pribadi Pamungkas 2020-12-02
CREATE PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_detail]
	@mpp_id     VARCHAR(10), -- contoh 'MPP-000003'
	@tgl_posisi DATE,       	-- contoh '2020-11-04'
	@nama_bidang VARCHAR(100),
	@kd_gol VARCHAR(2)
AS
BEGIN
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--DECLARE @kd_bidang VARCHAR(3)
	--SELECT
	--	@kd_bidang = LEFT(KD_UNIT_ORG,3)
	--FROM
	--	dbo.SPG_08_UNIT_ORGANISASI
	--WHERE
	--	KD_ESELON = '00'
	--	--AND NM_UNIT_ORG = @nama_bidang
	--	AND (NM_UNIT_ORG = @nama_bidang OR ISNULL(@nama_bidang, '') = '')

	SELECT
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.NM_GOLONGAN,
		lowjab.posisi_jabatan,
		SUM(COALESCE(terisi.id_formasi_terisi,0)) AS pelamar,
		lowjab.ID_FORMASI,
		lowjab.NM_ROOM,
		lowjab.DAMPAK,
		SUM(lowjab.jumlah) AS jumlah
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				a.JUMLAH AS jumlah,
				h.NM_GOLONGAN,
				h.KD_GOL,
				room.ID AS ID_ROOM,
				room.NM_ROOM,
				COALESCE(room.DAMPAK,r.DAMPAK,'') AS DAMPAK
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
				INNER JOIN
				dbo.SPG_08_UNIT_ORGANISASI AS suo ON LEFT(r.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS room ON a.ID_FORMASI = room.ID_FORMASI
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				--AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				AND suo.KD_ESELON = '00'
				AND (suo.NM_UNIT_ORG = @nama_bidang OR ISNULL(@nama_bidang, '') = '')

				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				--AND r.KD_GOL = @kd_gol	--varchar(2)
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS lowjab
		OUTER APPLY
		(
			SELECT 
				COUNT(d.id_formasi) AS id_formasi_terisi
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
				CROSS APPLY
				(
					SELECT 
						e.ID_PELAMAR 
					FROM 
						dbo.REC_PELAMAR_AKTIFITAS AS e
					WHERE
						d.ID = e.ID_PELAMAR
						AND e.STS_PELAMAR = 0
						AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) <= @tgl_posisi
						--AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) >= @tgl_posisi
					GROUP BY 
						e.ID_PELAMAR
				) AS c
			WHERE
				COALESCE(d.TGL_REKOMENDASI_PIMPINAN, GETDATE()) >= @tgl_posisi
				AND d.id_formasi = lowjab.id_formasi
				AND (d.ID_ROOM = lowjab.ID_ROOM OR lowjab.ID_ROOM IS NULL)
			GROUP BY
				d.id_formasi
		) AS terisi
	GROUP BY
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.NM_GOLONGAN,
		lowjab.KD_GOL,
		lowjab.posisi_jabatan,
		lowjab.ID_FORMASI,
		lowjab.NM_ROOM,
		lowjab.DAMPAK
	ORDER BY
		lowjab.KD_GOL DESC,
		lowjab.bid,
		lowjab.kantor,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan,
		lowjab.NM_ROOM
END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_lowjab]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- by Pribadi Pamungkas 2021-06-17
CREATE PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_lowjab]
	@mpp_id     VARCHAR(10), -- contoh 'MPP-000001'
	@tgl_posisi DATE,       	-- contoh '2021-06-17'
	@nama_bidang VARCHAR(100),
	@kd_gol VARCHAR(2),
	@filter TINYINT		-- 0: all, 1: belum ada pelamar, 2: ada pelamar
AS
BEGIN
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	--DECLARE @kd_bidang VARCHAR(3)
	--SELECT
	--	@kd_bidang = LEFT(KD_UNIT_ORG,3)
	--FROM
	--	dbo.SPG_08_UNIT_ORGANISASI
	--WHERE
	--	KD_ESELON = '00'
	--	--AND NM_UNIT_ORG = @nama_bidang
	--	AND (NM_UNIT_ORG = @nama_bidang OR ISNULL(@nama_bidang, '') = '')

	SELECT
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.NM_GOLONGAN,
		lowjab.posisi_jabatan,
		SUM(COALESCE(terisi.id_formasi_terisi,0)) AS pelamar,
		lowjab.ID_FORMASI,
		lowjab.NM_ROOM,
		lowjab.DAMPAK,
		--SUM(lowjab.jumlah) AS jumlah
		COUNT(lowjab.jumlah) AS jumlah
	FROM
		(
			select
				r.ID_FORMASI,
				LEFT(r.KD_UNIT_ORG, 3) AS bid,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.nm_unit_org, '') = '' THEN g.nm_unit_org
																	ELSE f.nm_unit_org
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				c.nm_kantor as kantor,
				j.posisi as posisi_jabatan,
				a.JUMLAH AS jumlah,
				h.NM_GOLONGAN,
				h.KD_GOL,
				--COALESCE(CAST(room.ID AS VARCHAR(10)), '') AS ID_ROOM
				room.ID AS ID_ROOM,
				room.NM_ROOM,
				COALESCE(room.DAMPAK,r.DAMPAK,'') AS DAMPAK
				--b.req_id,a.ptk_id,d.kode_satker_organisasi as kd_satker,d.nama_satker_organisasi as satker,f.nm_unit_org as depkom,m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, g.nm_unit_org as bidang,n.NM_PEND_UMUM,
				--isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, a.kd_jabatan,b.tanggal_publish,b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,h.NM_GOLONGAN,
				--m.nilai_kinerja as nilai1, m.nilai_kinerja_1 as nilai2, m.nilai_kinerja_2 as nilai3,
				--DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date, d.KD_UNIT_ES1, a.id_formasi
			from 
				dbo.REC_RENCANA_PEMENUHAN_PEG AS r INNER JOIN
				dbo.REC_PTK AS a ON r.ID_FORMASI = a.ID_FORMASI
				left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
				left join SPG_08_KANTOR c on a.kd_kantor=c.kd_kantor
				left join spg_08_unit_organisasi d on a.kd_unit_org=d.kd_unit_org
				left JOIN SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES3=e.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI f on d.KD_UNIT_ES2=f.KD_UNIT_ORG
				left JOIN SPG_08_UNIT_ORGANISASI g on d.KD_UNIT_ES1=g.KD_UNIT_ORG
				left join SPG_08_JABATAN_UNIT j on (a.KD_JABATAN=j.KD_JABATAN and a.KD_UNIT_ORG=j.KD_UNIT_ORG)
				left join SPG_08_JABATAN_STR k on a.kd_jabatan=k.KD_JABATAN_STR
				left join SPG_JABATAN_PANGKAT l on a.kd_jabatan=l.KD_JABATAN_STR
				left join REC_PERSYARATAN m on (m.KD_GOL=l.KD_GOL and m.periode=a.mpp_id)
				left join spg_golongan h on m.kd_gol=h.kd_gol
				left join spg_eselon i on h.KD_ESELON=i.KD_ESELON
				--left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
				INNER JOIN
				dbo.SPG_08_UNIT_ORGANISASI AS suo ON LEFT(r.KD_UNIT_ORG, 3) = LEFT(suo.KD_UNIT_ORG, 3)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS room ON a.ID_FORMASI = room.ID_FORMASI
			where 
				b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				AND r.MPP_ID = @mpp_id
				--AND LEFT(r.KD_UNIT_ORG, 3) = @kd_bidang
				AND suo.KD_ESELON = '00'
				AND (suo.NM_UNIT_ORG = @nama_bidang OR ISNULL(@nama_bidang, '') = '')

				AND r.STATUS_KIRIM = 1
				AND r.STATUS_AKTIF = 1
				AND b.JAM_SELESAI IS NOT NULL	-- sudah posting
				AND (r.KD_GOL = @kd_gol OR ISNULL(@kd_gol, '') = '')
		) AS lowjab
		OUTER APPLY
		(
			SELECT 
				COUNT(d.id_formasi) AS id_formasi_terisi
			FROM
				dbo.REC_PELAMAR_INTERNAL AS d
				CROSS APPLY
				(
					SELECT 
						e.ID_PELAMAR 
					FROM 
						dbo.REC_PELAMAR_AKTIFITAS AS e
					WHERE
						d.ID = e.ID_PELAMAR
						AND e.STS_PELAMAR = 0
						AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) <= @tgl_posisi
						--AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) >= @tgl_posisi
					GROUP BY 
						e.ID_PELAMAR
				) AS c
			WHERE
				COALESCE(d.TGL_REKOMENDASI_PIMPINAN, GETDATE()) >= @tgl_posisi
				AND d.id_formasi = lowjab.id_formasi
				AND (d.ID_ROOM = lowjab.ID_ROOM OR lowjab.ID_ROOM IS NULL) 
			GROUP BY
				d.id_formasi
		) AS terisi
	GROUP BY
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.kantor,
		lowjab.NM_GOLONGAN,
		lowjab.KD_GOL,
		lowjab.posisi_jabatan,
		lowjab.ID_FORMASI,
		lowjab.NM_ROOM,
		lowjab.DAMPAK
	HAVING
		(SUM(COALESCE(terisi.id_formasi_terisi,0)) = 0 OR ISNULL(@filter, 0) <> 1)
		AND (SUM(COALESCE(terisi.id_formasi_terisi,0)) > 0 OR ISNULL(@filter, 0) <> 2)
	ORDER BY
		lowjab.KD_GOL DESC,
		lowjab.bid,
		lowjab.satuan_kerja,
		lowjab.posisi_jabatan,
		lowjab.NM_ROOM
END
GO
/****** Object:  StoredProcedure [dbo].[rep_pelamar_sebelum_rekomendasi_pelamar]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- by Pribadi Pamungkas. 2021-01-08
CREATE PROCEDURE [dbo].[rep_pelamar_sebelum_rekomendasi_pelamar]
	@id_formasi int,
	@tgl_posisi DATE
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		d.NM_ROOM
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		CROSS APPLY
		(
			SELECT 
				b.ID_PELAMAR 
			FROM 
				dbo.REC_PELAMAR_AKTIFITAS AS b
			WHERE
				a.ID = b.ID_PELAMAR
				AND b.STS_PELAMAR = 0
				AND CAST(b.TGL_BUAT_AKTIFITAS AS DATE) <= @tgl_posisi
				--AND CAST(e.TGL_BUAT_AKTIFITAS AS DATE) >= @tgl_posisi
			GROUP BY 
				b.ID_PELAMAR
		) AS c
		LEFT JOIN
		dbo.REC_ROOM_FORMASI_FUNCTIONAL AS d
		ON a.ID_ROOM = d.ID AND a.ID_FORMASI = d.ID_FORMASI
	WHERE
		a.ID_FORMASI = @id_formasi
		--AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.TGL_REKOMENDASI_PIMPINAN, GETDATE()) >= @tgl_posisi

END
GO
/****** Object:  StoredProcedure [dbo].[rep_shortlist_per_bidang]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- by Pribadi Pamungkas. 2022-04-26
CREATE PROCEDURE [dbo].[rep_shortlist_per_bidang]
	@mpp_id varchar(10),
	@kd_bidang varchar(24)
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		--spe.MASA_DINAS_LEVEL_JABATAN AS MASA_DINAS_LVL_JABATAN,
		a.MASA_DINAS_LVL_JABATAN,
		--pr.RANKING,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(b.KD_UNIT_ORG,3),
				b.deputi_komisioner,
				b.KD_GOL,
				b.satuan_kerja,
				b.posisi_jabatan,
				b.NM_ROOM
			ORDER BY 
				LEFT(b.KD_UNIT_ORG,3),
				b.URUTAN_DEPKOM,
				b.kd_satuan_kerja,
				b.KD_GOL DESC,
				b.JNS_JABATAN DESC,
				b.posisi_jabatan,
				b.NM_ROOM,
				--CASE
				--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
				--	ELSE pr.total_skor
				--END DESC
				pr.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.deputi_komisioner,
		b.kantor,
		b.NM_GOL,
		b.satuan_kerja,
		b.posisi_jabatan + ' ' + COALESCE(b.NM_ROOM,'') AS posisi_jabatan,
		b.kd_bidang,
		b.kd_deputi_komisioner
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				CASE
					WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
														   WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
														   ELSE f.KD_UNIT_ORG
													    END
					ELSE d.kode_satker_organisasi
				 END AS kd_satuan_kerja,
				j.POSISI AS posisi_jabatan,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				f.NM_UNIT_ORG AS deputi_komisioner,
				x.JNS_JABATAN,
				f.KD_UNIT_ORG AS kd_deputi_komisioner,
				s.URUTAN_DEPKOM,
				--g.NM_UNIT_ORG AS nm_bidang,
				g.KD_UNIT_ORG AS kd_bidang
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r
				ON y.ID_FORMASI = r.ID_FORMASI
				LEFT JOIN (
				SELECT t.KD_UNIT_ORG as KD_DEPKOM,
						CASE WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
							THEN CASE WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 ELSE case WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 else ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) end end
					ELSE '0' END URUTAN_DEPKOM
						FROM MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
					WHERE t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
				) s ON f.KD_UNIT_ORG=s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI AND (b.ID_ROOM = a.ID_ROOM OR ISNULL(b.ID_ROOM, '') = '') 
		INNER JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.ID = pr.ID_PELAMAR AND pr.MPP_ID = @mpp_id
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		--(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		--b.kd_deputi_komisioner = @kd_depkom
		(b.kd_bidang = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
		AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
		AND pr.SHORT_LIST = 1	-- shortlist
	ORDER BY
		LEFT(b.KD_UNIT_ORG,3),
		b.URUTAN_DEPKOM,
		b.kd_satuan_kerja,
		b.KD_GOL DESC,
		b.JNS_JABATAN DESC,
		b.posisi_jabatan,
		b.NM_ROOM

END
GO
/****** Object:  StoredProcedure [dbo].[rep_shortlist_per_depkom]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2022-04-25
CREATE PROCEDURE [dbo].[rep_shortlist_per_depkom]
	@mpp_id varchar(10),
	--@id_formasi int
	@kd_depkom varchar(24)
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		spe.MASA_DINAS_LEVEL_JABATAN AS MASA_DINAS_LVL_JABATAN,
		--pr.RANKING,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(b.KD_UNIT_ORG,3),
				b.deputi_komisioner,
				b.KD_GOL,
				b.satuan_kerja,
				b.posisi_jabatan,
				b.NM_ROOM
			ORDER BY 
				LEFT(b.KD_UNIT_ORG,3),
				b.URUTAN_DEPKOM,
				b.kd_satuan_kerja,
				b.KD_GOL DESC,
				b.JNS_JABATAN DESC,
				b.posisi_jabatan,
				b.NM_ROOM,
				--CASE
				--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
				--	ELSE pr.total_skor
				--END DESC
				pr.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.deputi_komisioner,
		b.kantor,
		b.NM_GOL,
		b.satuan_kerja,
		b.posisi_jabatan + ' ' + COALESCE(b.NM_ROOM,'') AS posisi_jabatan
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				CASE
					WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
															WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
															ELSE f.KD_UNIT_ORG
														END
					ELSE d.kode_satker_organisasi
					END AS kd_satuan_kerja,
				j.POSISI AS posisi_jabatan,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				f.NM_UNIT_ORG AS deputi_komisioner,
				x.JNS_JABATAN,
				f.KD_UNIT_ORG AS kd_deputi_komisioner,
				s.URUTAN_DEPKOM
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r
				ON y.ID_FORMASI = r.ID_FORMASI
				LEFT JOIN (
				SELECT t.KD_UNIT_ORG as KD_DEPKOM,
						CASE WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
							THEN CASE WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 ELSE case WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 else ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) end end
					ELSE '0' END URUTAN_DEPKOM
						FROM MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
					WHERE t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
				) s ON f.KD_UNIT_ORG=s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI AND (b.ID_ROOM = a.ID_ROOM OR ISNULL(b.ID_ROOM, '') = '') 
		INNER JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.ID = pr.ID_PELAMAR AND pr.MPP_ID = @mpp_id
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		--(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		b.kd_deputi_komisioner = @kd_depkom
		AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'	-- direkomendasikan
		AND pr.SHORT_LIST = 1	-- shortlist
	ORDER BY
		LEFT(b.KD_UNIT_ORG,3),
		b.URUTAN_DEPKOM,
		b.kd_satuan_kerja,
		b.KD_GOL DESC,
		b.JNS_JABATAN DESC,
		b.posisi_jabatan,
		b.NM_ROOM

END
GO
/****** Object:  StoredProcedure [dbo].[rep_tidak_lolos_shortlist]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- by Pribadi Pamungkas. 2022-04-27
CREATE PROCEDURE [dbo].[rep_tidak_lolos_shortlist]
--DECLARE
	@mpp_id varchar(10) = 'MPP-000002',
	@kd_bidang varchar(24)
AS
BEGIN
	
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	SELECT
		a.NIP,
		a.NM_PEG,
		a.BIDANG,
		a.SATKER,
		a.FORMASI,
		a.TANGGAL_APPLY,
		--spe.MASA_DINAS_LEVEL_JABATAN AS MASA_DINAS_LVL_JABATAN,
		a.MASA_DINAS_LVL_JABATAN,
		--pr.RANKING,
		ROW_NUMBER() OVER (
			PARTITION BY
				LEFT(b.KD_UNIT_ORG,3),
				b.deputi_komisioner,
				b.KD_GOL,
				b.satuan_kerja,
				b.posisi_jabatan,
				b.NM_ROOM
			ORDER BY 
				LEFT(b.KD_UNIT_ORG,3),
				b.URUTAN_DEPKOM,
				b.kd_satuan_kerja,
				b.KD_GOL DESC,
				b.JNS_JABATAN DESC,
				b.posisi_jabatan,
				b.NM_ROOM,
				--CASE
				--	WHEN	pr.total_skor IS NULL OR pr.total_skor = 0 THEN a.total_skor
				--	ELSE pr.total_skor
				--END DESC
				pr.RANKING ASC,
				spe.ELIGIBLE_POTENSI_TINGGI ASC, 
				spe.ELIGIBLE_REGULER ASC
		) RANKING,
		-- dibawah ini adalah lowongannya
		b.nm_bidang,
		b.deputi_komisioner,
		b.kantor,
		b.NM_GOL,
		b.satuan_kerja,
		b.posisi_jabatan + ' ' + COALESCE(b.NM_ROOM,'') AS posisi_jabatan,
		b.kd_bidang,
		b.kd_deputi_komisioner,
		pr.SHORT_LIST
	FROM
		dbo.REC_PELAMAR_INTERNAL AS a
		INNER JOIN
		--dbo.REC_PTK AS b ON a.ID_FORMASI = b.ID_FORMASI
		(
			SELECT
				x.ID_FORMASI,
				x.KD_UNIT_ORG,
				g.NM_UNIT_ORG AS nm_bidang,
				c.NM_KANTOR AS kantor,
				CASE
					WHEN COALESCE(d.nama_satker_organisasi, '') = '' THEN CASE
																	WHEN COALESCE(f.NM_UNIT_ORG, '') = '' THEN g.NM_UNIT_ORG
																	ELSE f.NM_UNIT_ORG
																END
					ELSE d.nama_satker_organisasi
				END AS satuan_kerja,
				CASE
					WHEN COALESCE(d.kode_satker_organisasi, '') = '' THEN CASE
														   WHEN COALESCE(f.KD_UNIT_ORG, '') = '' THEN g.KD_UNIT_ORG
														   ELSE f.KD_UNIT_ORG
													    END
					ELSE d.kode_satker_organisasi
				 END AS kd_satuan_kerja,
				j.POSISI AS posisi_jabatan,
				r.ID AS ID_ROOM,
				r.NM_ROOM,
				x.KD_GOL,
				x.NM_GOL,
				f.NM_UNIT_ORG AS deputi_komisioner,
				x.JNS_JABATAN,
				f.KD_UNIT_ORG AS kd_deputi_komisioner,
				s.URUTAN_DEPKOM,
				--g.NM_UNIT_ORG AS nm_bidang,
				g.KD_UNIT_ORG AS kd_bidang
			FROM
				dbo.REC_RENCANA_PEMENUHAN_PEG AS x INNER JOIN
				dbo.REC_PTK AS y ON x.ID_FORMASI = y.ID_FORMASI LEFT JOIN	-- harusnya sih inner join kali ya, tapi ngikutin yang lain aja deh
				dbo.REC_PERMINTAAN_ADVERTISING AS z ON y.PTK_ID = z.REQ_ID LEFT JOIN 
				dbo.SPG_08_KANTOR AS c ON y.KD_KANTOR = c.KD_KANTOR LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS d ON y.KD_UNIT_ORG = d.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS f ON d.KD_UNIT_ES2 = f.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_UNIT_ORGANISASI AS g ON d.KD_UNIT_ES1 = g.KD_UNIT_ORG LEFT JOIN 
				dbo.SPG_08_JABATAN_UNIT AS j ON (y.KD_JABATAN = j.KD_JABATAN AND y.KD_UNIT_ORG = j.KD_UNIT_ORG)
				LEFT JOIN
				dbo.REC_ROOM_FORMASI_FUNCTIONAL AS r
				ON y.ID_FORMASI = r.ID_FORMASI
				LEFT JOIN (
				SELECT t.KD_UNIT_ORG as KD_DEPKOM,
						CASE WHEN t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
							THEN CASE WHEN t.SINGKATAN_UNIT_ORG = 'DKID' THEN 5 ELSE case WHEN t.SINGKATAN_UNIT_ORG = 'DKIK' THEN 6 else ROW_NUMBER() over (ORDER BY t.KD_UNIT_ES1, t.KD_UNIT_ORG ASC) end end
					ELSE '0' END URUTAN_DEPKOM
						FROM MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 t
					WHERE t.NM_UNIT_ORG LIKE '%Deputi Komisioner%'
				) s ON f.KD_UNIT_ORG=s.KD_DEPKOM
			WHERE
				z.RAM_ID = 'RAM-000020' 
				AND z.RAC_ID = 'RAC-000001'
				AND (x.MPP_ID = @mpp_id OR ISNULL(@mpp_id, '') = '')
				AND x.STATUS_KIRIM = 1
				AND x.STATUS_AKTIF = 1
				AND z.JAM_SELESAI IS NOT NULL	-- sudah posting
		) AS b ON a.ID_FORMASI = b.ID_FORMASI AND (b.ID_ROOM = a.ID_ROOM OR ISNULL(b.ID_ROOM, '') = '') 
		INNER JOIN
		dbo.REC_PELAMAR_RANKING AS pr ON a.ID = pr.ID_PELAMAR AND pr.MPP_ID = @mpp_id
		LEFT JOIN
		dbo.SPG_PEGAWAI_ELIGIBLE AS spe ON a.NIP = spe.NIP AND pr.MPP_ID = spe.PERIODE_PEMENUHAN
	WHERE
		--(a.ID_FORMASI = @id_formasi OR @id_formasi IS NULL)
		--b.kd_deputi_komisioner = @kd_depkom
		(b.kd_bidang = @kd_bidang OR ISNULL(@kd_bidang, '') = '')
		AND COALESCE(a.NIP_REKOMENDASI, '') <> ''	-- sudah rekomendasi
		AND COALESCE(a.REKOMENDASI_PIMPINAN,'') = 'Y'
		--AND pr.SHORT_LIST = 1	-- shortlist
		AND COALESCE(pr.SHORT_LIST, 0) = 0	-- tidak lolos shortlist
	ORDER BY
		LEFT(b.KD_UNIT_ORG,3),
		b.URUTAN_DEPKOM,
		b.kd_satuan_kerja,
		b.KD_GOL DESC,
		b.JNS_JABATAN DESC,
		b.posisi_jabatan,
		b.NM_ROOM

END
GO
/****** Object:  StoredProcedure [dbo].[SaldoCutiTahunan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SaldoCutiTahunan]
	-- Add the parameters for the stored procedure here
	@_nip varchar(10),
	@_jns_cuti int
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @_sisa_cuti_tahun_lalu int
	, @_hak_cuti int
	, @_sisa_cuti_diambil_tahun_lalu int
	, @_hak_cuti_diambil int
	, @_cuti_bersama int
	, @_sisa_total int
	, @_tanggal_berlaku date
	, @_tanggal_pengajuan date = getdate()




	SELECT top(1) @_sisa_cuti_tahun_lalu = isnull(sisa_cuti,0) FROM V_SPG_REKAP_CUTI WHERE NIP = @_nip AND  JNS_CUTI=@_jns_cuti AND TANGGAL_BERLAKU<@_tanggal_pengajuan AND TANGGAL_BERAKHIR IS NOT NULL ORDER BY TANGGAL_BERLAKU DESC

	
	SELECT top(1) @_tanggal_berlaku = a.TANGGAL_BERLAKU FROM V_SPG_REKAP_CUTI AS a LEFT JOIN SPG_JNS_CUTI AS b ON a.JNS_CUTI=b.JNS_CUTI WHERE a.NIP=@_nip AND  a.JNS_CUTI=@_jns_cuti AND a.TANGGAL_BERLAKU<=@_tanggal_pengajuan AND a.TANGGAL_BERAKHIR is null order by a.tanggal_berlaku desc
	--1 2
	SELECT top(1) @_hak_cuti = isnull(hak_cuti,0) FROM V_SPG_REKAP_CUTI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BERLAKU<=@_tanggal_berlaku ORDER BY TANGGAL_BERLAKU DESC
	
	--3
	--SELECT @_sisa_cuti_diambil_tahun_lalu = isnull(SUM ( ISNULL( CAST (AMBIL_CUTI_LALU AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku
	--REPLACED 17/07/2018?
	SELECT @_sisa_cuti_diambil_tahun_lalu = SUM (ISNULL(CAST (AMBIL_CUTI_LALU AS INT),0)) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND  JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku

	--4
	--SELECT @_hak_cuti_diambil = isnull(SUM (JUMLAH_CUTI) - SUM ( ISNULL( CAST (JUMLAH_HARI_BATAL AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BUAT>=@_tanggal_berlaku
	--REPLACED 17/07/2018? 
	SELECT @_hak_cuti_diambil = isnull(SUM (JUMLAH_CUTI) - SUM (ISNULL(CAST (JUMLAH_HARI_BATAL AS INT),0)),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BUAT>=@_tanggal_berlaku AND (CBE_ID NOT LIKE '%CBE%' OR CBE_ID IS NULL) and isnull(tidak_akumulasi,0) <> 1
	
	--5
	--SELECT @_cuti_bersama = isnull(SUM ( ISNULL( CAST (JUMLAH_CUTI AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku
	--REPLACED 17/07/2018? 
	SELECT @_cuti_bersama = SUM (ISNULL(CAST (JUMLAH_CUTI AS INT),0)) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND  TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku AND (KSO_KASUSNOMOR LIKE 'CBE%' OR CBE_ID LIKE 'CBE%') AND TGL_AWAL_CUTI_PEG <= GETDATE()

	--2 - 4 - 5 + (1-3)

	--set @_sisa_total = isnull(@_hak_cuti,0) + isnull(@_sisa_cuti_tahun_lalu,0) - isnull(@_cuti_bersama,0)
	--REPLACED 13/09/2018

	select @_sisa_total = isnull(@_hak_cuti,0) - isnull(@_hak_cuti_diambil,0) - isnull(@_cuti_bersama,0) + (isnull(@_sisa_cuti_tahun_lalu,0) - isnull(@_sisa_cuti_diambil_tahun_lalu,0))
	--REPLACED 17/07/2018
	--select @_sisa_total =isnull( @_hak_cuti - @_hak_cuti_diambil - @_cuti_bersama ,0)
	
	insert into temp_saldocutitahunan
	select 
	@_nip [nip]
	, isnull(@_sisa_cuti_tahun_lalu,0) [sisa_cuti_tahun_lalu]
	, isnull(@_hak_cuti,0) [hak_cuti]
	, isnull(@_sisa_cuti_diambil_tahun_lalu,0) [sisa_cuti_diambil_tahun_lalu]
	, isnull(@_hak_cuti_diambil,0) [hak_cuti_diambil]
	, isnull(@_cuti_bersama,0) [cuti_bersama]
	, isnull(@_sisa_total,0) [sisa_total]
END
GO
/****** Object:  StoredProcedure [dbo].[SaldoCutiTahunan_backup]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SaldoCutiTahunan_backup]
	-- Add the parameters for the stored procedure here
	@_nip varchar(10),
	@_jns_cuti int
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @_sisa_cuti_tahun_lalu int
	, @_hak_cuti int
	, @_sisa_cuti_diambil_tahun_lalu int
	, @_hak_cuti_diambil int
	, @_cuti_bersama int
	, @_sisa_total int
	, @_tanggal_berlaku date
	, @_tanggal_pengajuan date = getdate()

	SELECT top(1) @_tanggal_berlaku = a.TANGGAL_BERLAKU FROM V_SPG_REKAP_CUTI AS a LEFT JOIN SPG_JNS_CUTI AS b ON a.JNS_CUTI=b.JNS_CUTI WHERE a.NIP=@_nip AND  a.JNS_CUTI=@_jns_cuti AND a.TANGGAL_BERLAKU<=@_tanggal_pengajuan AND a.TANGGAL_BERAKHIR is null order by a.tanggal_berlaku desc
	--1 2
	SELECT top(1) @_sisa_cuti_tahun_lalu = isnull(sisa_cuti,0), @_hak_cuti = isnull(hak_cuti,0) FROM V_SPG_REKAP_CUTI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BERLAKU<=@_tanggal_berlaku ORDER BY TANGGAL_BERLAKU DESC
	
	--3
	--SELECT @_sisa_cuti_diambil_tahun_lalu = isnull(SUM ( ISNULL( CAST (AMBIL_CUTI_LALU AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku
	--REPLACED 17/07/2018?
	SELECT @_sisa_cuti_diambil_tahun_lalu = SUM (ISNULL(CAST (AMBIL_CUTI_LALU AS INT),0)) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND  JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku

	--4
	--SELECT @_hak_cuti_diambil = isnull(SUM (JUMLAH_CUTI) - SUM ( ISNULL( CAST (JUMLAH_HARI_BATAL AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BUAT>=@_tanggal_berlaku
	--REPLACED 17/07/2018? 
	SELECT @_hak_cuti_diambil = isnull(SUM (JUMLAH_CUTI) - SUM (ISNULL(CAST (JUMLAH_HARI_BATAL AS INT),0)),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TANGGAL_BUAT>=@_tanggal_berlaku AND (CBE_ID NOT LIKE '%CBE%' OR CBE_ID IS NULL) and isnull(tidak_akumulasi,0) <> 1
	
	--5
	--SELECT @_cuti_bersama = isnull(SUM ( ISNULL( CAST (JUMLAH_CUTI AS INT), 0 ) ),0) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku
	--REPLACED 17/07/2018? 
	SELECT @_cuti_bersama = SUM (ISNULL(CAST (JUMLAH_CUTI AS INT),0)) FROM SPG_CUTI_PEGAWAI WHERE NIP = @_nip AND JNS_CUTI=@_jns_cuti AND  TGL_AWAL_CUTI_PEG>=@_tanggal_berlaku AND (KSO_KASUSNOMOR LIKE 'CBE%' OR CBE_ID LIKE 'CBE%')

	--2 - 4 - 5 + (1-3)
	select @_sisa_total =isnull( @_hak_cuti - @_hak_cuti_diambil - @_cuti_bersama + (@_sisa_cuti_tahun_lalu - @_sisa_cuti_diambil_tahun_lalu),0)
	--REPLACED 17/07/2018
	--select @_sisa_total =isnull( @_hak_cuti - @_hak_cuti_diambil - @_cuti_bersama ,0)

	insert into temp_saldocutitahunan
	select 
	@_nip [nip]
	, isnull(@_sisa_cuti_tahun_lalu,0) [sisa_cuti_tahun_lalu]
	, isnull(@_hak_cuti,0) [hak_cuti]
	, isnull(@_sisa_cuti_diambil_tahun_lalu,0) [sisa_cuti_diambil_tahun_lalu]
	, isnull(@_hak_cuti_diambil,0) [hak_cuti_diambil]
	, isnull(@_cuti_bersama,0) [cuti_bersama]
	, isnull(@_sisa_total,0) [sisa_total]

END
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_pencarian_pegawai_mst]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_generate_pencarian_pegawai_mst]
    @usia int,
    @pendidikan varchar(10),
    @sanksi varchar(10),
    @cutoff_md date,
    @md_ojk int,
    @sisa_md_ojk int,
    @md_lvl_jabatan int,
    @nk_1 float,
    @nk_2 float,
    @rerata_nk float,
    @bidang_ms1 varchar(20),
    @bidang_ms2 varchar(20),
    @bidang_perbankan varchar(20),
    @bidang_pasar_modal varchar(20),
    @bidang_iknb varchar(20),
    @bidang_aimrpk varchar(20),
    @bidang_epk varchar(20),
    @pengalaman_kr_ko varchar(20),
    @eligible_tinggi varchar(20),
    @eligible_reguler varchar(20),
    @eligible_combine varchar(20)
--     @md_lvl_jab_tinggi varchar(20),
--     @md_lvl_jab_reguler varchar(20),
--     @nk_tinggi varchar(20),
--     @nk_reguler varchar(20)

AS
BEGIN

--     PRINT 'ELIGIBLE COMBINE: ' + @eligible_combine
    IF (@pendidikan = '' OR @pendidikan = NULL)
        BEGIN
           SET @pendidikan = '44,50,60,70'
        END
            
    DELETE
    FROM MST_PENCARIAN_PEGAWAI_AKTIF
    WHERE KETERANGAN = 'GENERATE BY SYSTEM'
    --AND CUT_OFF = @cutoff_md


    INSERT INTO MST_PENCARIAN_PEGAWAI_AKTIF (NIP, NM_PEG, USIA_SAAT_INI, KD_JNS_KELAMIN, JNS_KELAMIN, EMAIL, KD_PENERIMAAN_PEG, STS_PENERIMAAN_PEG, ANGKATAN, TAHUN_PEREKRUTAN,
                                             STS_PENSIUN, KD_STATUS_KEPEG, NM_STATUS_KEPEG, KD_KANTOR, NM_KANTOR, KD_LEVEL_JABATAN, LEVEL_JABATAN, KD_JABATAN, NM_JABATAN, KD_UNIT_ORG,
                                             NM_UNIT_ORG, KD_FORMASI, FORMASI, KD_KATEGORI_FUNGSI, KATEGORI_FUNGSI, KD_BIDANG, BIDANG, KD_DEPUTI_KOMISIONER, DEPUTI_KOMISIONER,
                                             KD_DEPARTEMEN, DEPARTEMEN, KD_DIREKTUR, DIREKTUR, KD_DEPUTI_DIREKTUR, DEPUTI_DIREKTUR, KD_KEPALA_BAGIAN, KEPALA_BAGIAN, KD_KEPALA_SUBBAGIAN,
                                             KEPALA_SUBBAGIAN, KD_SATUAN_KERJA, SATUAN_KERJA, KET_LEMBAGA_ASAL, TMT_LEMBAGA_ASAL, TMT_OJK, TMT_LEVEL_JABATAN,
                                             TMT_JABATAN, TMT_SATKER, TMT_PENSIUN, TGL_MASUK, NILAI_KINERJA_1, LEVEL_KINERJA_1, NILAI_KINERJA_2, LEVEL_KINERJA_2,
                                             MASA_DINAS_OJK, SISA_MASA_DINAS_OJK, MASA_DINAS_LEVEL_JABATAN, KATEGORI_PENSIUN, KD_PEND_UMUM, NM_PEND_UMUM,
                                             SANKSI, BIDANG_MS1, BIDANG_MS2, BIDANG_PERBANKAN, BIDANG_PASAR_MODAL, BIDANG_IKNB, BIDANG_AIMRPK, BIDANG_EPK,
                                             MD_LEVEL_JABATAN_POTENSI_TINGGI, MD_LEVEL_JABATAN_REGULER, NK_POTENSI_TINGGI, NK_REGULER, ELIGIBLE_POTENSI_TINGGI,
                                             ELIGIBLE_REGULER, ELIGIBLE_COMBINE, CUT_OFF, KETERANGAN, PENGALAMAN_KR_KO, PTB, CLTO, CM, CI, UNIVERSITAS, JURUSAN, RERATA_NILAI_KINERJA)

    SELECT
          NIP, NM_PEG, USIA_SAAT_INI, KD_JNS_KELAMIN, JNS_KELAMIN, EMAIL, KD_PENERIMAAN_PEG, STS_PENERIMAAN_PEG, ANGKATAN, TAHUN_PEREKRUTAN,
          STS_PENSIUN, KD_STATUS_KEPEG, NM_STATUS_KEPEG, KD_KANTOR, NM_KANTOR, KD_LEVEL_JABATAN, LEVEL_JABATAN, KD_JABATAN, NM_JABATAN, KD_UNIT_ORG,
          NM_UNIT_ORG, KD_FORMASI, FORMASI, KD_KATEGORI_FUNGSI, KATEGORI_FUNGSI, KD_BIDANG, BIDANG, KD_DEPUTI_KOMISIONER, DEPUTI_KOMISIONER,
          KD_DEPARTEMEN, DEPARTEMEN, KD_DIREKTUR, DIREKTUR, KD_DEPUTI_DIREKTUR, DEPUTI_DIREKTUR, KD_KEPALA_BAGIAN, KEPALA_BAGIAN, KD_KEPALA_SUBBAGIAN,
          KEPALA_SUBBAGIAN, KD_SATUAN_KERJA, SATUAN_KERJA, KET_LEMBAGA_ASAL, TMT_LEMBAGA_ASAL, TMT_OJK, TMT_LEVEL_JABATAN,
          TMT_JABATAN, TMT_SATKER, TMT_PENSIUN, TGL_MASUK,  NILAI_KINERJA_1, LEVEL_KINERJA_1, NILAI_KINERJA_2, LEVEL_KINERJA_2,
          MASA_DINAS_OJK, SISA_MASA_DINAS_OJK, MASA_DINAS_LEVEL_JABATAN, KATEGORI_PENSIUN, KD_PEND_UMUM, NM_PEND_UMUM,
          SANKSI, BIDANG_MS1, BIDANG_MS2, BIDANG_PERBANKAN, BIDANG_PASAR_MODAL, BIDANG_IKNB, BIDANG_AIMRPK, BIDANG_EPK,
          MD_LEVEL_JABATAN_POTENSI_TINGGI, MD_LEVEL_JABATAN_REGULER, NK_POTENSI_TINGGI, NK_REGULER, ELIGIBLE_POTENSI_TINGGI,
          ELIGIBLE_REGULER, ELIGIBLE_COMBINE, @cutoff_md AS CUT_OFF, 'GENERATE BY SYSTEM' AS KETERANGAN, PENGALAMAN_KR_KO,
          PTB, CLTO, CM, CI, UNIVERSITAS, JURUSAN, RERATA_NILAI_KINERJA

    FROM (
                  SELECT CASE
                             WHEN b.lama_clto > 0
                                 THEN dbo.MasaDinasCLTO(CASE
                                                            WHEN STS_PENSIUN = '0'
                                                                THEN dbo.MasaDinasTahunBulanDataMart(TMT_LEVEL_JABATAN, @cutoff_md)
                                                            WHEN STS_PENSIUN = '1'
                                                                THEN dbo.MasaDinasTahunBulanDataMart(TMT_LEVEL_JABATAN, TGL_BERHENTI)
                                                            END, b.lama_clto, '-')
                             ELSE CASE
                                      WHEN STS_PENSIUN = '0'
                                          THEN dbo.MasaDinasTahunBulanDataMart(TMT_LEVEL_JABATAN, @cutoff_md)
                                      WHEN STS_PENSIUN = '1'
                                          THEN dbo.MasaDinasTahunBulanDataMart(TMT_LEVEL_JABATAN, TGL_BERHENTI)
                                 END
                         END AS MASA_DINAS_LEVEL_JABATAN,
                         CASE
                             WHEN DATEDIFF(mm, @cutoff_md, TMT_PENSIUN) <= 12 THEN '0_1'
                             WHEN DATEDIFF(mm, @cutoff_md, TMT_PENSIUN) <= 36 THEN '1_3'
                             WHEN DATEDIFF(mm, @cutoff_md, TMT_PENSIUN) > 36 THEN '_3_'
                         END AS KATEGORI_PENSIUN,
                            a.NIP, a.NM_PEG, a.USIA_SAAT_INI, a.KD_JNS_KELAMIN, a.JNS_KELAMIN, a.EMAIL, a.KD_PENERIMAAN_PEG, a.STS_PENERIMAAN_PEG, a.ANGKATAN, a.TAHUN_PEREKRUTAN,
                            a.STS_PENSIUN, a.KD_STATUS_KEPEG, a.NM_STATUS_KEPEG, a.KD_KANTOR, a.NM_KANTOR, a.KD_LEVEL_JABATAN, a.LEVEL_JABATAN, a.KD_JABATAN, a.NM_JABATAN, a.KD_UNIT_ORG,
                            NM_UNIT_ORG, a.KD_FORMASI, a.FORMASI, a.KD_KATEGORI_FUNGSI, a.KATEGORI_FUNGSI, a.KD_BIDANG, a.BIDANG, a.KD_DEPUTI_KOMISIONER, a.DEPUTI_KOMISIONER,
                            KD_DEPARTEMEN, a.DEPARTEMEN, a.KD_DIREKTUR, a.DIREKTUR, a.KD_DEPUTI_DIREKTUR, a.DEPUTI_DIREKTUR, a.KD_KEPALA_BAGIAN, a.KEPALA_BAGIAN, a.KD_KEPALA_SUBBAGIAN,
                            KEPALA_SUBBAGIAN, a.KD_SATUAN_KERJA, a.SATUAN_KERJA, a.SATKER_PUSAT, a.SATKER_KR_KO, a.KET_LEMBAGA_ASAL, a.TMT_LEMBAGA_ASAL, a.TMT_OJK, a.TMT_LEVEL_JABATAN,
                            TMT_JABATAN, a.TMT_SATKER, a.TMT_PENSIUN, a.TGL_MASUK, a.PENDIDIKAN, a.NILAI_KINERJA_1, a.LEVEL_KINERJA_1, a.NILAI_KINERJA_2, a.LEVEL_KINERJA_2,
                            MASA_DINAS_OJK, a.SISA_MASA_DINAS_OJK,  a.KD_PEND_UMUM, a.NM_PEND_UMUM, a.SANKSI, a.BIDANG_MS1, a.BIDANG_MS2, a.BIDANG_PERBANKAN,
                            a.BIDANG_PASAR_MODAL, a.BIDANG_IKNB, a.BIDANG_AIMRPK, a.BIDANG_EPK, a.MD_LEVEL_JABATAN_POTENSI_TINGGI, a.MD_LEVEL_JABATAN_REGULER,
                            a.NK_POTENSI_TINGGI, a.NK_REGULER, a.ELIGIBLE_POTENSI_TINGGI, a.ELIGIBLE_REGULER, a.ELIGIBLE_COMBINE, a.PENGALAMAN_KR_KO,
                            a.PTB, a.CLTO, a.CM, a.CI, a.UNIVERSITAS, a.JURUSAN, a.RERATA_NILAI_KINERJA
                  from MST_DAFTAR_PEGAWAI_AKTIF a
                  LEFT JOIN (
                      select nip,
                             ISNULL(SUM((DATEDIFF(DAY, TGL_AWAL_CUTI_PEG, TGL_AKHIR_CUTI_PEG) / 30)), 0) as lama_clto
                      from SPG_CUTI_PEGAWAI
                      where JNS_CUTI = '16'
                      group by nip
                  ) b on a.nip = b.nip
    ) x
    WHERE LEFT(USIA_SAAT_INI,2) >= @usia
    AND KD_PEND_UMUM IN (SELECT * FROM dbo.SplitStrings_CTE(@pendidikan, ','))
    --AND KD_PEND_UMUM = CASE
    --    WHEN LEN(@pendidikan) > 0 THEN @pendidikan
    --    ELSE KD_PEND_UMUM
    --END
    --AND SANKSI IN (SELECT * FROM dbo.SplitStrings_CTE(@sanksi, ','))
    AND SANKSI = CASE
        WHEN LEN(@sanksi) > 0 THEN @sanksi
        ELSE SANKSI
    END
    AND CAST(LEFT(MASA_DINAS_OJK,2) as INT) >= @md_ojk
    --AND KATEGORI_PENSIUN IN (SELECT * FROM dbo.SplitStrings_CTE(@kategori_pensiun, ','))
    AND CAST(LEFT(SISA_MASA_DINAS_OJK,2) as INT) >= @sisa_md_ojk
    AND CAST(LEFT(MASA_DINAS_LEVEL_JABATAN,2) as INT) >= @md_lvl_jabatan
    AND ISNULL(NILAI_KINERJA_1, 0) >= @nk_1
    AND ISNULL(NILAI_KINERJA_2, 0) >= @nk_2
    AND ISNULL(RERATA_NILAI_KINERJA, 0) >= @rerata_nk
    AND (
            BIDANG_MS1 = CASE
                WHEN LEN(@bidang_ms1) > 0 THEN  @bidang_ms1
                ELSE 
                    CASE WHEN LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_MS1
                        ELSE ''
                    END
            END
            --BIDANG_MS1 IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_ms1, ','))
            OR
            BIDANG_MS2 = CASE
                WHEN LEN(@bidang_ms2) > 0 THEN @bidang_ms2
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_MS2
                        ELSE ''
                    END
            END
            --BIDANG_MS2 IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_ms2, ','))
            OR
            BIDANG_PERBANKAN = CASE
                WHEN LEN(@bidang_perbankan) > 0 THEN @bidang_perbankan
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_PERBANKAN
                        ELSE ''
                    END
            END
            --BIDANG_PERBANKAN IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_perbankan, ','))
            OR
            BIDANG_PASAR_MODAL = CASE
                WHEN LEN(@bidang_pasar_modal) > 0 THEN @bidang_pasar_modal
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_PASAR_MODAL
                        ELSE ''
                    END
            END
            --BIDANG_PASAR_MODAL IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_pasar_modal, ','))
            OR
            BIDANG_IKNB = CASE
                WHEN LEN(@bidang_iknb) > 0 THEN @bidang_iknb
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_IKNB
                        ELSE ''
                    END
            END
            --BIDANG_IKNB IN  (SELECT * FROM dbo.SplitStrings_CTE(@bidang_iknb, ','))
            OR
            BIDANG_AIMRPK = CASE
                WHEN LEN(@bidang_aimrpk) > 0 THEN @bidang_aimrpk
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_epk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_AIMRPK
                        ELSE ''
                    END
            END
            --BIDANG_AIMRPK IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_aimrpk, ','))
            OR
            BIDANG_EPK = CASE
                WHEN LEN(@bidang_epk) > 0 THEN @bidang_epk
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@pengalaman_kr_ko) = 0
                        THEN BIDANG_EPK
                        ELSE ''
                    END
            END
            --BIDANG_EPK IN (SELECT * FROM dbo.SplitStrings_CTE(@bidang_epk, ','))
            OR
            PENGALAMAN_KR_KO = CASE
                WHEN LEN(@pengalaman_kr_ko) > 0 THEN @pengalaman_kr_ko
                ELSE 
                    CASE WHEN LEN(@bidang_ms1) = 0 AND LEN(@bidang_ms2) = 0 AND LEN(@bidang_perbankan) = 0 AND LEN(@bidang_pasar_modal) = 0 AND LEN(@bidang_iknb) = 0 AND LEN(@bidang_aimrpk) = 0 AND LEN(@bidang_epk) = 0
                        THEN PENGALAMAN_KR_KO
                        ELSE ''
                    END
            END
            --PENGALAMAN_KR_KO IN (SELECT * FROM dbo.SplitStrings_CTE(@pengalaman_kr_ko, ','))
    )
    --AND MD_LEVEL_JABATAN_POTENSI_TINGGI IN (SELECT * FROM dbo.SplitStrings_CTE(@md_lvl_jab_tinggi, ','))
--     AND MD_LEVEL_JABATAN_POTENSI_TINGGI = CASE
--         WHEN LEN(@md_lvl_jab_tinggi) > 0 THEN @md_lvl_jab_tinggi
--         ELSE MD_LEVEL_JABATAN_POTENSI_TINGGI
--     END
    --AND MD_LEVEL_JABATAN_REGULER IN (SELECT * FROM dbo.SplitStrings_CTE(@md_lvl_jab_reguler, ','))
--     AND MD_LEVEL_JABATAN_REGULER = CASE
--         WHEN LEN(@md_lvl_jab_reguler) > 0 THEN @md_lvl_jab_reguler
--         ELSE MD_LEVEL_JABATAN_REGULER
--     END
    --AND NK_POTENSI_TINGGI IN (SELECT * FROM dbo.SplitStrings_CTE(@nk_tinggi, ','))
--     AND NK_POTENSI_TINGGI = CASE
--         WHEN LEN(@nk_tinggi) > 0 THEN @nk_tinggi
--         ELSE NK_POTENSI_TINGGI
--     END
    --AND NK_REGULER IN (SELECT * FROM dbo.SplitStrings_CTE(@nk_reguler, ','))
--     AND NK_REGULER = CASE
--         WHEN LEN(@nk_reguler) > 0 THEN @nk_reguler
--         ELSE NK_REGULER
--     END
    --AND ELIGIBLE_POTENSI_TINGGI IN (SELECT * FROM dbo.SplitStrings_CTE(@eligible_tinggi, ','))
    AND ELIGIBLE_POTENSI_TINGGI = CASE
        WHEN LEN(@eligible_tinggi) > 0 THEN @eligible_tinggi
        ELSE ELIGIBLE_POTENSI_TINGGI
    END
    --AND ELIGIBLE_REGULER IN (SELECT * FROM dbo.SplitStrings_CTE(@eligible_reguler, ','))
    AND ELIGIBLE_REGULER = CASE
        WHEN LEN(@eligible_reguler) > 0 THEN @eligible_reguler
        ELSE ELIGIBLE_REGULER
    END
    --AND ELIGIBLE_COMBINE IN (SELECT * FROM dbo.SplitStrings_CTE(@eligible_combine, ','))
    AND ELIGIBLE_COMBINE = CASE
        WHEN LEN(@eligible_combine) > 0 THEN @eligible_combine
        ELSE ELIGIBLE_COMBINE
    END
END
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_ranking]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_generate_ranking]
	@mpp_id varchar(10),
	@id_formasi int,
	@req_id varchar(10),
	@tahapan int
AS
BEGIN
    With cte As
    (
        SELECT a.RANKING,
        ROW_NUMBER() OVER(ORDER BY a.TOTAL_SKOR DESC, a.POTENSI_TINGGI ASC, a.REGULER ASC, a.TOTAL_NILAI_KINERJA DESC, a.SISA_MD_OJK DESC) AS RN
        FROM REC_PELAMAR_RANKING a
        INNER JOIN REC_PELAMAR_INTERNAL b on a.ID_PELAMAR=b.ID AND a.ID_FORMASI=b.ID_FORMASI
        WHERE a.MPP_ID = @mpp_id AND a.ID_FORMASI = @id_formasi AND a.REQ_ID = @req_id AND a.TAHAPAN = @tahapan
    )
    UPDATE cte SET RANKING=RN
END
GO
/****** Object:  StoredProcedure [dbo].[sp_generate_sk_pengangkatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_generate_sk_pengangkatan]
	@mpp_id varchar(10),
	@kd_gol varchar(2),
	@no_sk varchar(100),
	@tgl_sk date,
	@tgl_mulai date,
	@tgl_efektif date
AS
BEGIN
    DECLARE @id_pelamar int

    DECLARE id_pelamar CURSOR LOCAL FOR
        SELECT a.ID AS id_pelamar
        FROM REC_PELAMAR_INTERNAL a
        INNER JOIN REC_RENCANA_PEMENUHAN_PEG b ON a.ID_FORMASI = b.ID_FORMASI
        WHERE b.KD_GOL=@kd_gol AND b.MPP_ID=@mpp_id AND a.STATUS in ('5', '7', '8') AND a.PEGAWAI = '1'
        AND COALESCE(a.pengangkatan, 0) <> '1'
    
    OPEN id_pelamar
        FETCH NEXT FROM id_pelamar INTO @id_pelamar

        WHILE @@FETCH_STATUS = 0 BEGIN

--          PRINT 'id_pelamar: ' + cast(@id_pelamar as varchar(20))
            UPDATE REC_PELAMAR_INTERNAL SET
                    NO_SK = @no_sk,
                    TGL_SK = @tgl_sk,
                    TGL_MULAI_BERLAKU = @tgl_mulai,
                    TGL_EFEKTIF = @tgl_efektif
            WHERE ID = @id_pelamar

            FETCH NEXT FROM id_pelamar INTO @id_pelamar
        END
    CLOSE id_pelamar
	DEALLOCATE id_pelamar
END
GO
/****** Object:  StoredProcedure [dbo].[sp_get_error_message]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_get_error_message]
AS
SELECT
    ERROR_NUMBER() AS ErrorNumber
    ,ERROR_SEVERITY() AS ErrorSeverity
    ,ERROR_STATE() AS ErrorState
    ,ERROR_PROCEDURE() AS ErrorProcedure
    ,ERROR_LINE() AS ErrorLine
    ,ERROR_MESSAGE() AS ErrorMessage;
GO
/****** Object:  StoredProcedure [dbo].[sp_send_invitation]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_send_invitation]
	@mpp_id varchar(10),
	@list_kd_gol varchar(100),
	@id_undangan int,
	@tujuan int,
	@nm_buat varchar(10)
AS
BEGIN
    DECLARE @kd_gol varchar(2)
    DECLARE @short_list bit
    DECLARE @lock_short_list bit
    DECLARE @tahapan varchar(1)

    IF @tujuan = 1
        BEGIN
            SET @short_list = 1
            SET @tahapan = '2'
            SET @lock_short_list = 1
        END
    ELSE
        BEGIN
            SET @short_list = 0
            SET @tahapan = '1'
            SET @lock_short_list = 0
        END

--     PRINT 'short_list: ' + cast(@short_list as varchar(1))
--     PRINT 'tahapan: ' + @tahapan

    DECLARE level_jabatan CURSOR LOCAL FOR
        select a.KD_GOL
        from REC_RENCANA_PEMENUHAN_PEG a
        inner join REC_PTK b ON a.ID_FORMASI=b.ID_FORMASI
        left join (
            select d.ID_FORMASI from rec_pelamar_internal d
            inner join rec_pelamar_ranking e on d.id=e.id_pelamar and d.id_formasi=e.id_formasi
            where e.short_list = @short_list and d.status in ('5','6','7') and d.tahapan = @tahapan
            and isnull(d.LOCK_SHORT_LIST, 0) = @lock_short_list
        ) c on b.ID_FORMASI=c.ID_FORMASI
        WHERE a.KD_GOL in (SELECT * FROM dbo.SplitStrings_CTE(@list_kd_gol, ',')) AND a.mpp_id = @mpp_id
        group by a.KD_GOL
        having count(c.ID_FORMASI) > 0

--         PRINT 'list_kd_gol: ' + @list_kd_gol
--         PRINT 'mpp_id: ' + @mpp_id
--         PRINT 'short_list: ' + cast(@short_list as varchar(1))
--         PRINT 'lock_short_list: ' + cast(@lock_short_list as varchar(1))

    OPEN level_jabatan
        FETCH NEXT FROM level_jabatan INTO @kd_gol

        WHILE @@FETCH_STATUS = 0 BEGIN

--             PRINT 'kd_gol: ' + CAST(@kd_gol as varchar(2))
            DELETE FROM REC_NOTIFIKASI_UNDANGAN WHERE MPP_ID = @mpp_id AND KD_GOL = @kd_gol AND ID_UNDANGAN = @id_undangan

            INSERT INTO REC_NOTIFIKASI_UNDANGAN (MPP_ID, ID_UNDANGAN, ID_FORMASI, REQ_ID, NIP, NM_BUAT, KD_GOL, NIP_PIMPINAN_SATKER)
            SELECT c.MPP_ID, @id_undangan as ID_UNDANGAN, c.ID_FORMASI, a.REQ_ID, a.NIP, @nm_buat as NM_BUAT, c.KD_GOL, a.NIP_REKOMENDASI as NIP_PIMPINAN_SATKER
            FROM REC_PELAMAR_INTERNAL a
            INNER JOIN REC_PELAMAR_RANKING b on a.ID_FORMASI = b.ID_FORMASI AND a.NIP = b.NIP
            INNER JOIN REC_RENCANA_PEMENUHAN_PEG c on a.ID_FORMASI = c.ID_FORMASI
            INNER JOIN SPG_08_UNIT_ORGANISASI d ON a.KD_SATKER = d.KD_UNIT_ORG
            WHERE b.SHORT_LIST = @short_list AND a.STATUS in ('5','6','7') and a.TAHAPAN = @tahapan
            AND c.KD_GOL = @kd_gol AND c.MPP_ID = @mpp_id

            FETCH NEXT FROM level_jabatan INTO @kd_gol
        END
    CLOSE level_jabatan
	DEALLOCATE level_jabatan
END
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_all]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[update_data_current_all]
AS
BEGIN

	declare @nip varchar(50)
	
	declare cur CURSOR LOCAL for
	    select nip from spg_data_current-- where isnull(STS_PENSIUN,0) <> 1
	
	open cur
	
	fetch next from cur into @nip
	
	while @@FETCH_STATUS = 0 BEGIN
	
	    --execute your sproc on each row
	    exec dbo.update_data_current_nip_trg @nip
	
	    fetch next from cur into @nip
	END
	
	close cur
	deallocate cur

END
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_nip_trg]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[update_data_current_nip_trg] @nip varchar(20), @action varchar(1) = NULL
AS
BEGIN

	--SPG_PEGAWAI
	DECLARE
	@kd_agama char(1),
	@nm_agama varchar(20),
	@nm_peg varchar(200),
	@tgl_lahir_peg DATE,
	@tempat_lahir_peg varchar(200),
	@jns_kelamin_peg 	char(1),
	@sts_perkawinan char(1),
	@email varchar(128),
  @email_2 varchar(128),
	@cmp_id varchar(10),
	@grp_id varchar(10),
	@jns_gaji char(1),
	@tgl_masuk date,
	@tgl_berhenti date,
	@gph_id varchar(10),
	@nip_baru varchar(20),
	@nip_lama varchar(20),
	@no_rekening varchar(200),
	@kd_bank varchar(50),
	@keterangan_status varchar(200),
	@metode_pajak varchar(200),
	@cabang_bank varchar(200),
	@kd_kpp varchar(20),
	@kd_costcenter varchar(50),
	@npwp varchar(20),
	@tgl_mulai_kontrak date,

	@tgl_selesai_kontrak date,
	@status_jamsostek varchar(10),
	@lembaga_asal char(1),
  @norek_dplk varchar(20),
  @norek_dppk varchar(20),
  @nm_rekening varchar(100)

	--SPG_08_RIWAYAT_JABATAN
	DECLARE
	@kd_unit_org varchar(30),
	@tmt_jabatan date,
	@kd_kantor varchar(30),
	@nm_kantor varchar(100),
	@jns_kantor varchar(3),
	@jns_jabatan_cur varchar(20),
	@kd_jabatan_str varchar(4),
	@kd_eselon varchar(5),
	@nm_unit_es1 varchar(200),
	@nm_unit_es2 varchar(200),
	@nm_unit_es3 varchar(200),
	@nm_unit_es4 varchar(200),
	@nm_unit_es5 varchar(200),
	@nm_unit_es6 varchar(200),
	@nm_unit_es7 varchar(200),
	@kd_unit_es1 varchar(30),
	@kd_unit_es2 varchar(30),
	@kd_unit_es3 varchar(30),
	@kd_unit_es4 varchar(30),
	@kd_unit_es5 varchar(30),
	@kd_unit_es6 varchar(30
),
	@kd_unit_es7 varchar(30),
	@nm_jabatan varchar(200),
	@nm_eselon varchar(200),
	@kd_grade varchar(5),
	@kd_pangkat_tgs varchar(10),
	@kd_jabatan_tgs varchar(20),
	@kd_unit_org_tgs varchar(50),
	@kd_kantor_tgs varchar(50),
	@tmt_jabatan_tgs date,
	@penugasan varchar(5),
	@no_urut_jab int

	-- SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@kd_pangkat varchar(10),
	@nm_pangkat varchar(100),
	@tmt_pangkat date,
	@kd_gol varchar(5),
	@nm_gol varchar(100),
	@kd_skala_gaji varchar(5),
	@no_urut_pang_peg int

	-- SPG_RIWAYAT_TMP_TINGGAL_PEG
	DECLARE
	@tmt_tmp_tinggal date,
	@jln_tmp_tinggal varchar(500),
	@kd_propinsi varchar(3),
	@kd_kabupaten varchar(3),
	@kd_kecamatan varchar(3),
	@kd_kelurahan varchar(3),
	@rt_tmp_tinggal varchar(5),
	@rw_tmp_tinggal varchar(5),
	@jns_tmp_tinggal varchar(3),
	@ket_tmp_tinggal varchar(50)

	-- SPG_PEGAWAI_PENSIUN / KEAKTIFAN PEGAWAI
	DECLARE
	@kd_status_kepeg char(3),
	@nm_status_kepeg varchar(100),
	@tmt_status_kepeg date,
	@sts_pensiun char(1),
	@jns_sts_keaktifan int,
	@kd_tidak_aktif varchar(5)


	-- SPG_ASURANSI_PEGAWAI
	DECLARE
	@jst_provider varchar(20),
	@jst_template varchar(20),
	@tmt_jamsostek date

	-- SPG_RIWAYAT_PEND_FORMAL
	DECLARE
	@kd_pend_umum varchar(20),
	@nm_pend_umum varchar(200),
	@nm_lemb_pend_formal varchar(200),
	@tmp_lemb_pend_formal varchar(200),
	@tgl_ijazah date

	-- SPG_SIPEG_PICTURES
	DECLARE
	@picture varchar(500)

	-- SPG_RIWAYAT_KELUARGA_PEG
	DECLARE
	@nip_pasangan varchar(20)

	-- SPG_JENIS_KELAMIN
	DECLARE
	@NM_JNS_KELAMIN_PEG varchar(30)

	-- TMT

	DECLARE
	@tmt_lembaga_asal date,
	@tmt_ojk date,
	@tmt_penempatan date,
	@tmt_satker date,
	@tmt_gol date,
	@tmt_pensiun date,

	-- MD OJK
	@masa_dinas_bulan int,
	@masa_tahun_ojk int,
	@masa_bulan_ojk int,
	@masa_bulan_cuti_luar_tanggungan int


	------
-----------
	--GET SPG_PEGAWAI
	-----------------

	SELECT @kd_agama=kd_agama,@nm_peg=nm_peg,@tgl_lahir_peg=tgl_lahir_peg,@tempat_lahir_peg=tempat_lahir_peg,@jns_kelamin_peg=jns_kelamin_peg,@sts_perkawinan=sts_perkawinan,
	@email=email,@email_2=email_2,@grp_id=grp_id,@jns_gaji=jns_gaji,@tmt_ojk=tgl_masuk,@gph_id=gph_id,
	@nip_lama=nip_lama,@nip_baru=nip_baru,@no_rekening=no_rekening,@kd_bank=kd_bank,@keterangan_status=keterangan_status,@metode_pajak=metode_pajak,
	@cabang_bank=cabang_bank ,@cmp_id=cmp_id,
@npwp=npwp,@status_jamsostek=status_jamsostek,@tmt_lembaga_asal=tmt_lembaga_asal,@lembaga_asal=lembaga_asal,
  @norek_dplk=norek_dplk, @norek_dppk=norek_dppk, @nm_rekening=nm_rekening
	FROM spg_pegawai WHERE nip=@nip

	SELECT @nm_agama=nm_agama from spg_agama WHERE kd_agama=@kd_agama

	IF ((@tmt_lembaga_asal != '' OR @tmt_lembaga_asal != '1900-01-01') AND @tmt_lembaga_asal < @tmt_ojk)
		BEGIN
			SET @tgl_masuk=@tmt_lembaga_asal
		END
	ELSE
		BEGIN
			SET @tgl_masuk=@tmt_ojk
		END

	--Penyesuaian logic MD OJK untuk pegawai penugasan eks Bapepam/Bank Indonesia agar acuan start date menggunakan tmt OJK bukan tmt lembaga asal.
	IF (@lembaga_asal in ('1', '7') AND LEFT(@nip,1) <> 'P')
		BEGIN
			SET @tgl_masuk=@tmt_ojk
		END


	----------------------
-------
	-- GET SPG_08_RIWAYAT_JABATAN
	-----------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT TOP 1 @tmt_jabatan=tmt_jab_peg, @no_urut_jab=no_urut_jab FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and tanggal_riwayat <= GETDATE() and kd_status_validasi=1 and status_jabatan='0' ORDER BY tmt_jab_peg DESC,no_urut_jab DESC

	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @kd_unit_org=kd_unit_org,@kd_kantor=kd_kantor,@kd_jabatan_str=kd_jabatan_str,
	@kd_grade=kd_grade,@tmt_penempatan=tmt_penempatan,@tmt_satker=tmt_satker
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0'
	and no_urut_jab=@no_urut_jab
	--ORDER BY tmt_jab_peg DESC,no_urut_jab DESC

	-- AMBIL ESELON UNIT ORGANISASI
	--SELECT @kd_eselon=kd_eselon FROM SPG_08_UNIT_ORGANISASI where kd_unit_org=@kd_unit_org
	SELECT @kd_eselon=kd_eselon, @jns_jabatan_cur=jenis FROM SPG_08_JABATAN_STR where kd_jabatan_str=@kd_jabatan_str
    IF (@kd_jabatan_str = '501' OR @kd_jabatan_str = '502' OR @kd_jabatan_str =  '503')
	    BEGIN
            SET @jns_jabatan_cur='fungsional'
        END

	-- AMBIL NM_UNIT ES1 - 7
	SELECT @kd_unit_es1=kd_unit_es1, @kd_unit_es2=kd_unit_es2, @kd_unit_es3=kd_unit_es3, @kd_unit_es4=kd_unit_es4,
	@kd_unit_es5=kd_unit_es5, @kd_unit_es6=kd_unit_es6, @kd_unit_es7=kd_unit_es7 from SPG_08_UNIT_ORGANISASI where kd_unit_org=@kd_unit_org

	SELECT @nm_unit_es1=nm_unit_org FROM spg_08_unit_organisasi WHERE
 kd_unit_org=@kd_unit_es1

	SELECT @nm_unit_es2=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es2

	SELECT @nm_unit_es3=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es3

	SELECT @nm_unit_es4=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es4

	SELECT @nm_unit_es5=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es5

	SELECT @nm_unit_es6=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es6

	SELECT @nm_unit_es7=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es7

	-- AMBIL NM_JABATAN
	SELECT @nm_jabatan=nm_jabatan_str FROM spg_08_jabatan_str WHERE kd_jabatan_str=@kd_jabatan_str and (tgl_end is null or tgl_end='1900-01-01')

	-- AMBIL NM_ESELON
	SELECT @nm_eselon=nm_eselon FROM spg_eselon where kd_eselon=@kd_eselon
	--print @nm_unit_es3;
	-- AMBIL PERUSAHAAN, JNS_KANTOR DAN NM KANTOR
	SELECT @cmp_id=cmp_id, @nm_kantor=nm_kantor, @jns_kantor=jns_kantor FROM spg_08_kantor WHERE kd_kantor=@kd_kantor
	--print @kd_kantor;
	IF (@cmp_id='' or @cmp_id is null)
	BEGIN
		SET @cmp_id = 'CMP-000001'
	END

	-- PENUGASAN
	SELECT @tmt_jabatan_tgs=MAX(tmt_jab_peg) FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and (tanggal_end>=GETDATE() 
OR tanggal_end='' OR tanggal_end='1900-01-01' or tanggal_end IS NULL)and kd_status_validasi=1 and status_jabatan IN ('5','4')

	IF (@tmt_jabatan_tgs>=@tmt_jabatan)
		BEGIN
			-- AMBIL RIWAYAT TERBARU
			SELECT @kd_unit_org_tgs=kd_unit_org,@kd_kantor_tgs=kd_kantor,@kd_jabatan_tgs=kd_jabatan_str
			FROM spg_08_riwayat_jabatan
			WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan_tgs and kd_status_validasi=1 and status_jabatan IN ('5','4') ORDER BY tmt_jab_peg asc,no_urut_jab asc

			SET @penugasan=1
		END
	ELSE
		
BEGIN
			SET @kd_unit_org_tgs=NULL
			SET @kd_kantor_tgs=NULL
			SET @kd_jabatan_tgs=NULL
			SET @tmt_jabatan_tgs=NULL
			SET @penugasan=0
		END

	------------------------------
	-- GET SPG_RIWAYAT_KEPANGKATAN : PANGKAT DIGABUNG KE RIWAYAT JABATAN
	------
------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT TOP 1 @tmt_pangkat=tmt_pang_peg, @no_urut_pang_peg=no_urut_pang_peg FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=GETDATE() and kd_status_validasi=1
 and sts_riwayat_grade!='6' ORDER BY tmt_pang_peg DESC,no_urut_pang_peg DESC,kd_pangkat DESC/*tmt_pang_peg DESC,kd_skala_gaji DESC,no_urut_pang_peg DESC*/

	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @kd_pangkat=kd_pangkat,@kd_skala_gaji=kd_skala_gaji,@tmt_gol=tmt_gol
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1 and sts_riwayat_grade!='6'
	and no_urut_pang_peg=@no_urut_pang_peg
	--ORDER BY tmt_pang_peg DESC, kd_skala_gaji DESC, no_urut_pang_peg DESC


	-- AMBIL NAMA PANGKAT
	SELECT @nm_pangkat=nm_pangkat, @kd_gol=kd_gol FROM spg_pangkat WHERE kd_pangkat=@kd_pangkat

	-- AMBIL NAMA GOLONGAN
	SELECT @nm_gol=nm_golongan FROM spg_golongan WHERE kd_gol=@kd_gol



	------------------------------
	-- GET SPG_RIWAYAT_TMP_TINGGAL_PEG
	------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT @tmt_tmp_tinggal=MAX(tmt_tmp_tinggal) FROM SPG_RIWAYAT_TMP_TINGGAL_PEG WHERE nip=@nip and tmt_tmp_tinggal<=GETDATE() and kd_status_validasi=1 and tmp_tinggal_ktp='1'

	-- AMBIL RIWAYAT TERBARU
	SELECT @jln_tmp_tinggal=jln_tmp_tinggal, @kd_propinsi=kd_propinsi, @kd_kabupaten=kd_kabupaten,
	@kd_kecamatan=kd_kecamatan, @kd_kelurahan=kd_kelurahan, @rt_tmp_tinggal=rt_tmp_tinggal, @rw_tmp_tinggal=rw_tmp_tinggal,
	@jns_tmp_tinggal=jns_tmp_tinggal
	FROM SPG_RIWAYAT_TMP_TINGGAL_PEG
	WHERE nip=@nip and tmt_tmp_tinggal=@tmt_tmp_tinggal and kd_status_validasi=1 and tmp_tinggal_ktp='1'

	-- AMBIL KET STATUS KEPEMILIKAN
	SELECT @ket_tmp_tinggal=ket_tmp_tinggal FROM SPG_JNS_TMP_TINGGAL WHERE jns_tmp_tinggal=@jns_tmp_tinggal


	-----------------------------------------------------
	-- GET SPG_PEGAWAI_PENSIUN / STATUS KEAKTIFAN PEGAWAI
	-----------------------------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT @tmt_status_kepeg=MAX(tmt_pensiun)	FROM spg_pegawai_pensiun WHERE nip=@nip and tmt_pensiun<=GETDATE() and kd_status_validasi=1


	-- AMBIL RIWAYAT TERBARU
	SELECT @kd_status_kepeg=kd_status_kepeg,@sts_pensiun=sts_pensiun, @tgl_mulai_kontrak=tgl_mulai,
	@tgl_selesai_kontrak=tgl_akhir, @jns_sts_keaktifan=jns_sts_keaktifan, @kd_tidak_aktif=kd_tidak_aktif
	FROM spg_pegawai_pensiun
	WHERE nip=@nip and tmt_pensiun=@tmt_status_kepeg and kd_status_validasi=1


	
-- MAPPING TGL SELESAI KONTRAK PEGAWAI PKWT (KHUSUS PKWT By:Wd) -- dipindahin ke line 402
	-- IF (@kd_status_kepeg='200')
	--	BEGIN
  --    SELECT @tmt_status_kepeg=MAX(tmt_pensiun)	FROM spg_pegawai_pensiun WHERE nip=@nip and kd_status_validasi=1
  --    
SET @tgl_selesai_kontrak=@tmt_status_kepeg
	--	END


	-- AMBIL NAMA STATUS
	SELECT @nm_status_kepeg=nm_status_kepeg FROM spg_status_kepeg WHERE kd_status_kepeg=@kd_status_kepeg

	-- CHECK STATUS NON AKTIF
	IF (@sts_pensiun='1')
		BEGIN
			SET @tgl_berhenti=@tmt_status_kepeg
		END
	ELSE
		BEGIN
			SET @tgl_berhenti=NULL
		END


	-----------------------------------------------------
	-- GET SPG_ASURANSI_PEGAWAI
	-----------------------------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	--SELECT @tmt_jamsostek=MAX(tgl_mulai_asuransi_peg) FROM spg_asuransi_pegawai WHERE nip=@nip and tgl_mulai_asuransi_peg<=GETDATE() and kd_status_validasi=1


	-- AMBIL RIWAYAT TERBARU
	--SELECT @jst_provider=jst_provider, @jst_template=jst_template
	--FROM spg_asuransi_pegawai
	--WHERE nip=@nip and tgl_mulai_asuransi_peg=@tmt_jamsostek and kd_status_validasi=1

	--IF (@jst_template is NULL)
	--BEGIN
		--select @jst_provider=jst_provider,@jst_template=jst_template FROM spg_perusahaan where cmp_id=@cmp_id
	--END

	-----------------------------------------------------
	-- GET PAY_COSTCENTER_UNIT
	-----------------------------------------------------

	--AMBIL NILAI COSTCENTER
	--SELECT @kd_costcenter=kd_costcenter FROM pay_costcenter_unit WHERE kd_unit_org=@kd_unit_org and kd_kantor=@kd_kantor
	SET @kd_costcenter=@kd_unit_es3+'|'+@kd_kantor


	-----------------------------------------------------
	-- GET SPG_KPP
	-----------------------------------------------------

	--AMBIL KODE KPP
	--SELECT @kd_kpp=kd_kpp FROM spg_08_kantor WHERE kd_kantor=@kd_kantor


	-----------------------------------------------------
	-- GET SPG_RIWAYAT_PEND_FORMAL
	-----------------------------------------------------

	SELECT @kd_pend_umum=max(kd_pend_umum) FROM 
SPG_RIWAYAT_PEND_FORMAL WHERE nip=@nip and kd_status_validasi='1'

	SELECT @nm_lemb_pend_formal=nm_lemb_pend_form,@tmp_lemb_pend_formal=tmp_lemb_pend_form,@tgl_ijazah=tgl_ijazah
	FROM SPG_RIWAYAT_PEND_FORMAL WHERE nip=@nip and kd_pend_umum=@kd_pend_umum


	SELECT @nm_pend_umum=nm_pend_umum FROM SPG_JNS_PEND_UMUM where kd_pend_umum=@kd_pend_umum


	-----------------------------------------------------
	-- GET SPG_SIPEG_PICTURES
	-----------------------------------------------------

	SELECT @picture=path FROM SPG_SIPEG_PICTURES WHERE keyid=@nip and tablename='spg_pegawai'


	-------------------------
	--TMT PENSIUN
	-------------------------

	IF @kd_gol in ('10','20','30')
	BEGIN
		SET @tmt_pensiun=dateadd(year,56,@tgl_lahir_peg)
	END
	ELSE IF @kd_gol in ('40','50')
	BEGIN
		 SET @tmt_pensiun=dateadd(year,58,@tgl_lahir_peg)
	END
	ELSE IF @kd_gol in ('60','70','80','90')
	BEGIN
		SET @tmt_pensiun=dateadd(year,60,@tgl_lahir_peg)
	END
  -- TMT PENSIUN ADK (By:Wd) MD CUMA 5 Tahun dari TMT-OJK
  ELSE IF @kd_gol in ('99')
	BEGIN
    SET @tmt_pensiun=DATEADD( day, -1, convert( date, dateadd(year,5,@tmt_ojk) ) )
	END

  -- TMT PENSIUN ADK (By:Wd) TIDAK DIMULAI DARI TANGGAL 1
  IF @kd_gol not in ('99')
  BEGIN
	  SET @tmt_pensiun=SUBSTRING(CAST(DATEADD(MONTH,1,@tmt_pensiun) as VARCHAR(10)),1,7)+'-01'
  END

	--print 'tmt_pensiun:'
	--print @tmt_pensiun
  -- AUTO GENERATE PEGAWAI NON AKTIF SESUAI TANGGAL BUP
  IF ((@action = '' OR @action IS NULL) AND (@tgl_berhenti = '' OR @tgl_berhenti IS NULL OR @tgl_berhenti = '1900-01-01') AND @kd_status_kepeg = '100' AND @tmt_pensiun = CAST(GETDATE() as DATE) AND NOT EXISTS(SELECT NIP FROM SPG_PEGAWAI_PENSIUN WHERE NIP=@nip AND TMT_PENSIUN=@tmt_pensiun))
      BEGIN
        ALTER TABLE SPG_PEGAWAI_PENSIUN DISABLE TRIGGER spg_pegawai_pensiun_trg

        INSERT INTO SPG_PEGAWAI_PENSIUN (NIP, TMT_PENSIUN, NO_SK, KET_PENSIUN, KD_TIDAK_AKTIF, ID, KD_STATUS_VALIDASI, JNS_STS_KEAKTIFAN, NAMA_BUAT, TANGGAL_BUAT, STS_PENSIUN, KD_STATUS_KEPEG, TANGGAL_EFEKTIF_PENSIUN)
        VALUES (@nip, @tmt_pensiun, @nip+'/STS/'+cast(@tmt_pensiun as varchar(10)), 'PENSIUN NORMAL - GENERATE BY SYSTEM', '1', (SELECT MAX(ID)+1 FROM SPG_PEGAWAI_PENSIUN), '1', '1', 'SYSTEM', GETDATE(), '1', @kd_status_kepeg, @tmt_pensiun)

        INSERT INTO SPG_SK (NIP, NO_SK, TGL_SK, JNS_SK) values (@nip, @nip+'/STS/'+cast(@tmt_pensiun as varchar(10)), @tmt_pensiun, '04')

        ALTER TABLE SPG_PEGAWAI_PENSIUN ENABLE TRIGGER spg_pegawai_pensiun_trg

        SET @sts_pensiun = '1'
        SET @tgl_berhenti = @tmt_pensiun
      END
  
  -- MAPPING TGL SELESAI KONTRAK PEGAWAI PKWT (KHUSUS PKWT By:Wd) pindahan dari atas
  IF (@kd_status_kepeg='200')
		BEGIN    
      SELECT @tmt_status_kepeg=MAX(tmt_pensiun)	FROM spg_pegawai_pensiun WHERE nip=@nip and kd_status_validasi=1
      SET @tgl_selesai_kontrak=@tmt_status_kepeg			
      SET @tmt_pensiun=@tmt_status_kepeg			
		END

	-------------------------
	-- GET JENIS KELAMIN ----
	-------------------------
	SELECT 
		@NM_JNS_KELAMIN_PEG=NM_KELAMIN_PEG 
	FROM 
		SPG_JNS_KELAMIN 
	WHERE 
		JNS_KELAMIN_PEG=@JNS_KELAMIN_PEG


	-------------------------
	---- MASA DINAS OJK  ----
	-------------------------
	SELECT @masa_dinas_bulan=(DATEDIFF(MONTH, @tgl_masuk, GETDATE()))
	SELECT @masa_bulan_cuti_luar_tanggungan=ISNULL(SUM(CAST
(DATEDIFF(MONTH, TGL_AWAL_CUTI_PEG, TGL_AKHIR_CUTI_PEG) AS INT )),0) FROM SPG_CUTI_PEGAWAI WHERE NIP=@nip AND JNS_CUTI='16' AND TGL_AWAL_CUTI_PEG<GETDATE()
	SET @masa_tahun_ojk = FLOOR((@masa_dinas_bulan-@masa_bulan_cuti_luar_tanggungan)/12)
	SET @masa_bulan_ojk = (@masa_dinas_bulan-@masa_bulan_cuti_luar_tanggungan)-(@masa_tahun_ojk*12)

	--print 'tmt_gol: '+cast(@tmt_gol as varchar)




	-------------------------
	-- UPDATE TABLE USER ----
	-------------------------
	UPDATE TBL_USER SET CMP_ID=@cmp_id , KD_KANTOR=@kd_kantor , KD_UNIT_ORG=@kd_unit_org , KD_JABATAN_STR = @kd_jabatan_str WHERE NIP=@nip


	-------------------------
	--UPDATE SPG_DATA_CURRENT
	-------------------------

	UPDATE SPG_DATA_CURRENT SET
	kd_agama=@kd_agama,nm_agama=@nm_agama,nm_peg=@nm_peg,tgl_lahir_peg=@tgl_lahir_peg,tempat_lahir_peg=@tempat_lahir_peg,jns_kelamin_peg=@jns_kelamin_peg,nm_jns_kelamin_peg=@nm_jns_kelamin_peg,sts_perkawinan=@sts_perkawinan,
	email=@email,email_2=@email_2,cmp_id=@cmp_id,grp_id=@grp_id,jns_gaji=@jns_gaji,tgl_masuk=@tgl_masuk,tgl_berhenti=@tgl_berhenti,gph_id=@gph_id, 
	nip_lama=@nip_lama,nip_baru=@nip_baru,no_rekening=@no_rekening,kd_bank=@kd_bank,keterangan_status=@keterangan_status,metode_pajak=@metode_pajak,
	cabang_bank=@cabang_bank,
	kd_unit_org=@kd_unit_org,kd_kantor=@kd_kantor,jns_jabatan_cur=@jns_jabatan_cur,kd_jabatan_str=@kd_jabatan_str,kd_eselon=@kd_eselon,
	nm_unit_es1=@nm_unit_es1,nm_unit_es2=@nm_unit_es2,nm_unit_es3=@nm_unit_es3,nm_unit_es4=@nm_unit_es4,nm_unit_es5=@nm_unit_es5,
	nm_unit_es6=@nm_unit_es6,nm_unit_es7=@nm_unit_es7,nm_jabatan=@nm_jabatan,nm_eselon=@nm_eselon,
	kd_pangkat=@kd_pangkat,kd_gol=@kd_gol,nm_pangkat=@nm_pangkat,tmt_pangkat=@tmt_pangkat, 
	sts_pensiun=@sts_pensiun,kd_status_kepeg=@kd_status_kepeg,nm_status_kepeg=@nm_status_kepeg,tmt_status_kepeg=@tmt_status_kepeg, 
	kd_kpp=@kd_kpp, kd_grade=@kd_grade, kd_costcenter=@kd_costcenter, jl_tmp_tinggal_cur=@jln_tmp_tinggal, jst_provider=@jst_provider, 
	jst_template=@jst_template, nm_kantor=@nm_kantor, jns_kantor=@jns_kantor, nm_gol=@nm_gol, npwp=@npwp,
	tgl_mulai_kontrak=@tgl_mulai_kontrak,tgl_selesai_kontrak=@tgl_selesai_kontrak, TMT_JABATAN=@tmt_jabatan, status_jamsostek=@status_jamsostek,
	kd_propinsi=@kd_propinsi, kd_kabupaten=@kd_kabupaten, kd_kecamatan=@kd_kecamatan,
 kd_kelurahan=@kd_kelurahan, tmt_tmp_tinggal=@tmt_tmp_tinggal,  
	rt_tmp_tinggal_cur=@rt_tmp_tinggal, rw_tmp_tinggal_cur=@rw_tmp_tinggal, jns_tmp_tinggal=@jns_tmp_tinggal, ket_tmp_tinggal=@ket_tmp_tinggal,
	jns_pend_formal=@kd_pend_umum, ket_pend_formal=@nm_pend_umum, kd_pend_umum=@kd_pend_umum, nm_pend_umum=@nm_pend_umum, 
	nm_lemb_pend_formal=@nm_lemb_pend_formal, tmp_lemb_pend_formal=@tmp_lemb_pend_formal, tgl_ijazah=@tgl_ijazah,
	kd_pangkat_tgs=@kd_pangkat_tgs, kd_jabatan_tgs=@kd_jabatan_tgs, kd_unit_org_tgs=@kd_unit_org_tgs, 
	kd_kantor_tgs=@kd_kantor_tgs, tmt_jabatan_tgs=@tmt_jabatan_tgs, penugasan=@penugasan, picture=@picture, 
	kd_skala_gaji=@kd_skala_gaji,tmt_lembaga_asal=@tmt_lembaga_asal,tmt_ojk=@tmt_ojk,tmt_penempatan=@tmt_penempatan,
	tmt_satker=@tmt_satker,tmt_gol=@tmt_gol,tmt_pensiun=@tmt_pensiun, lembaga_asal=@lembaga_asal,MASA_TAHUN_OJK=@masa_tahun_ojk, MASA_BULAN_OJK=@masa_bulan_ojk,
	jns_sts_keaktifan=@jns_sts_keaktifan, kd_tidak_aktif=@kd_tidak_aktif,
  no_dppk=@norek_dppk, no_dplk=@norek_dplk, nm_rekening=@nm_rekening
	WHERE nip=@nip

	--INSERT INTO LOG
	--INSERT INTO tbl_log_procedure (TIME,LOG) VALUES (GETDATE(), @nip)

	-----------------------------------------------------
	-- GET SPG_RIWAYAT_KELUARGA_PEG
	-------------------------
----------------------------
	-- DI REMARK dan DIUBAH QUERY-nya (ARIF)
  -- Karena data Keluarga harus 
  -- Query Asli: select @nip_pasangan=nip_pasangan from SPG_RIWAYAT_KELUARGA_PEG where nip=@nip and internal='1' and (nip_pasangan!='' and nip_pasangan is not null)
  
  select @nip_pasangan=nip_pasangan from SPG_RIWAYAT_KELUARGA_PEG where nip=@nip and internal='1' and (nip_pasangan<>'' and nip_pasangan is not null) AND KD_HUBUNGAN_KELUARGA IN ('1','2')
	if (@nip_pasangan<>'' and @nip_pasangan is not null)
  	BEGIN
  		UPDATE SPG_DATA_CURRENT SET nip_pasangan=@nip_pasangan WHERE nip=@nip
  		UPDATE SPG_DATA_CURRENT SET nip_pasangan=@nip WHERE nip=@nip_pasangan
  	END
 else
    BEGIN
      -- Untuk sementara disamakan fungsinya dengan diatas, nanti mungkin ada kebutuhan lain di bagian ELSE ini.
  		UPDATE SPG_DATA_CURRENT SET nip_pasangan='' WHERE nip=@nip
  		UPDATE SPG_DATA_CURRENT SET nip_pasangan='' WHERE nip=@nip_pasangan
  	END
END
GO
/****** Object:  StoredProcedure [dbo].[update_data_current_nip_trg_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_data_current_nip_trg_bak_20160219] @nip varchar(20)
AS
BEGIN
	
	--SPG_PEGAWAI
	DECLARE 
	@kd_agama char(1),
	@nm_agama varchar(20),
	@nm_peg varchar(200),
	@tgl_lahir_peg DATE,
	@tempat_lahir_peg varchar(200),
	@jns_kelamin_peg 	char(1),
	@sts_perkawinan char(1),
	@email varchar(128), 
	@cmp_id varchar(10),
	@grp_id varchar(10),
	@jns_gaji char(1),
	@tgl_masuk date,
	@tgl_berhenti date,
	@gph_id varchar(10),
	@nip_baru varchar(20),
	@nip_lama varchar(20),
	@no_rekening varchar(200),
	@kd_bank varchar(50),
	@keterangan_status varchar(200),
	@metode_pajak varchar(200), 
	@cabang_bank varchar(200), 
	@kd_kpp varchar(20), 
	@kd_costcenter varchar(50), 
	@npwp varchar(20),
	@tgl_mulai_kontrak date,
	@tgl_selesai_kontrak date, 
	@status_jamsostek varchar(10),
	@lembaga_asal char(1)

	--SPG_08_RIWAYAT_JABATAN
	DECLARE
	@kd_unit_org varchar(30),
	@tmt_jabatan date, 
	@kd_kantor varchar(30),
	@nm_kantor varchar(100),
	@jns_kantor char(2),
	@jns_jabatan_cur char(1),
	@kd_jabatan_str varchar(4),
	@kd_eselon varchar(5),
	@nm_unit_es1 varchar(200),
	@nm_unit_es2 varchar(200),
	@nm_unit_es3 varchar(200),
	@nm_unit_es4 varchar(200),
	@nm_unit_es5 varchar(200),
	@nm_unit_es6 varchar(200),
	@nm_unit_es7 varchar(200),
	@kd_unit_es1 varchar(20),
	@kd_unit_es2 varchar(20),
	@kd_unit_es3 varchar(20),
	@kd_unit_es4 varchar(20),
	@kd_unit_es5 varchar(20),
	@kd_unit_es6 varchar(20),
	@kd_unit_es7 varchar(20),
	@nm_jabatan varchar(200),
	@nm_eselon varchar(200), 
	@kd_grade varchar(5),
	@kd_pangkat_tgs varchar(10),
	@kd_jabatan_tgs varchar(20),
	@kd_unit_org_tgs varchar(50),
	@kd_kantor_tgs varchar(50), 
	@tmt_jabatan_tgs date, 
	@penugasan varchar(5)

	-- SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@kd_pangkat varchar(10), 
	@nm_pangkat varchar(100),
	@tmt_pangkat date,
	@kd_gol varchar(5),
	@nm_gol varchar(100),
	@kd_skala_gaji varchar(5)
	
	-- SPG_RIWAYAT_TMP_TINGGAL_PEG
	DECLARE
	@tmt_tmp_tinggal date, 
	@jln_tmp_tinggal varchar(500),
	@kd_propinsi varchar(3),
	@kd_kabupaten varchar(3),
	@kd_kecamatan varchar(3),
	@kd_kelurahan varchar(3),
	@rt_tmp_tinggal varchar(5),
	@rw_tmp_tinggal varchar(5),
	@jns_tmp_tinggal varchar(3),
	@ket_tmp_tinggal varchar(50)

	-- SPG_PEGAWAI_PENSIUN / KEAKTIFAN PEGAWAI
	DECLARE
	@kd_status_kepeg char(3),
	@nm_status_kepeg varchar(100),
	@tmt_status_kepeg date,
	@sts_pensiun char(1),
	@jns_sts_keaktifan int,
	@kd_tidak_aktif varchar(5)

	
	-- SPG_ASURANSI_PEGAWAI
	DECLARE
	@jst_provider varchar(20),
	@jst_template varchar(20),
	@tmt_jamsostek date

	-- SPG_RIWAYAT_PEND_FORMAL
	DECLARE
	@kd_pend_umum varchar(20),
	@nm_pend_umum varchar(200),
	@nm_lemb_pend_formal varchar(200),
	@tmp_lemb_pend_formal varchar(200),
	@tgl_ijazah date

	-- SPG_SIPEG_PICTURES
	DECLARE
	@picture varchar(500)

	-- SPG_RIWAYAT_KELUARGA_PEG
	DECLARE
	@nip_pasangan varchar(20)

	-- SPG_JENIS_KELAMIN
	DECLARE
	@NM_JNS_KELAMIN_PEG varchar(30)

	-- TMT
	DECLARE
	@tmt_lembaga_asal date,
	@tmt_ojk date,
	@tmt_penempatan date,
	@tmt_satker date,
	@tmt_gol date,
	@tmt_pensiun date,
	
	-- MD OJK
	@masa_dinas_bulan int,
	@masa_tahun_ojk int,
	@masa_bulan_ojk int,
	@masa_bulan_cuti_luar_tanggungan int
	

	-----------------
	--GET SPG_PEGAWAI
	-----------------
	
	SELECT @kd_agama=kd_agama,@nm_peg=nm_peg,@tgl_lahir_peg=tgl_lahir_peg,@tempat_lahir_peg=tempat_lahir_peg,@jns_kelamin_peg=jns_kelamin_peg,@sts_perkawinan=sts_perkawinan,
	@email=email,@grp_id=grp_id,@jns_gaji=jns_gaji,@tmt_ojk=tgl_masuk,@gph_id=gph_id, 
	@nip_lama=nip_lama,@nip_baru=nip_baru,@no_rekening=no_rekening,@kd_bank=kd_bank,@keterangan_status=keterangan_status,@metode_pajak=metode_pajak,
	@cabang_bank=cabang_bank ,@cmp_id=cmp_id,@npwp=npwp,@status_jamsostek=status_jamsostek,@tmt_lembaga_asal=tmt_lembaga_asal,@lembaga_asal=lembaga_asal 
	FROM spg_pegawai WHERE nip=@nip
	
	SELECT @nm_agama=nm_agama from spg_agama WHERE kd_agama=@kd_agama

	IF (@tmt_lembaga_asal<@tmt_ojk)
		BEGIN
			SET @tgl_masuk=@tmt_lembaga_asal
		END
	ELSE
		BEGIN
			SET @tgl_masuk=@tmt_ojk
		END


	-----------------------------
	-- GET SPG_08_RIWAYAT_JABATAN
	-----------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT @tmt_jabatan=MAX(tmt_jab_peg) FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and kd_status_validasi=1 and status_jabatan='0'
	
	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @kd_unit_org=kd_unit_org,@kd_kantor=kd_kantor,@jns_jabatan_cur=jns_jabatan,@kd_jabatan_str=kd_jabatan_str,
	@kd_grade=kd_grade,@tmt_penempatan=tmt_penempatan,@tmt_satker=tmt_satker  
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0'
	ORDER BY ID DESC

	-- AMBIL ESELON UNIT ORGANISASI
	--SELECT @kd_eselon=kd_eselon FROM SPG_08_UNIT_ORGANISASI where kd_unit_org=@kd_unit_org
	SELECT @kd_eselon=kd_eselon FROM SPG_08_JABATAN_STR where kd_jabatan_str=@kd_jabatan_str

	-- AMBIL NM_UNIT ES1 - 7
	SELECT @kd_unit_es1=kd_unit_es1, @kd_unit_es2=kd_unit_es2, @kd_unit_es3=kd_unit_es3, @kd_unit_es4=kd_unit_es4, 
	@kd_unit_es5=kd_unit_es5, @kd_unit_es6=kd_unit_es6, @kd_unit_es7=kd_unit_es7 from SPG_08_UNIT_ORGANISASI where kd_unit_org=@kd_unit_org

	SELECT @nm_unit_es1=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es1
	
	SELECT @nm_unit_es2=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es2

	SELECT @nm_unit_es3=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es3

	SELECT @nm_unit_es4=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es4

	SELECT @nm_unit_es5=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es5

	SELECT @nm_unit_es6=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es6

	SELECT @nm_unit_es7=nm_unit_org FROM spg_08_unit_organisasi WHERE kd_unit_org=@kd_unit_es7

	-- AMBIL NM_JABATAN
	SELECT @nm_jabatan=nm_jabatan_str FROM spg_08_jabatan_str WHERE kd_jabatan_str=@kd_jabatan_str and (tgl_end is null or tgl_end='1900-01-01')

	-- AMBIL NM_ESELON
	SELECT @nm_eselon=nm_eselon FROM spg_eselon where kd_eselon=@kd_eselon

	-- AMBIL PERUSAHAAN, JNS_KANTOR DAN NM KANTOR
	SELECT @cmp_id=cmp_id, @nm_kantor=nm_kantor, @jns_kantor=jns_kantor FROM spg_08_kantor WHERE kd_kantor=@kd_kantor

	IF (@cmp_id='' or @cmp_id is null)
	BEGIN
		SET @cmp_id = 'CMP-000001'
	END

	-- PENUGASAN
	SELECT @tmt_jabatan_tgs=MAX(tmt_jab_peg) FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and kd_status_validasi=1 and status_jabatan!='1'
	
	IF (@tmt_jabatan_tgs>@tmt_jabatan) 
		BEGIN
			-- AMBIL RIWAYAT TERBARU
			SELECT @kd_unit_org_tgs=kd_unit_org,@kd_kantor_tgs=kd_kantor,@kd_jabatan_tgs=kd_jabatan_str  
			FROM spg_08_riwayat_jabatan
			WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan_tgs and kd_status_validasi=1 and status_jabatan!='1'

			SET @penugasan=1
		END
	ELSE
		BEGIN
			SET @kd_unit_org_tgs=NULL
			SET @kd_kantor_tgs=NULL
			SET @kd_jabatan_tgs=NULL
			SET @tmt_jabatan_tgs=NULL
			SET @penugasan=0
		END

	------------------------------
	-- GET SPG_RIWAYAT_KEPANGKATAN : PANGKAT DIGABUNG KE RIWAYAT JABATAN
	------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT @tmt_pangkat=MAX(tmt_pang_peg) FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=GETDATE() and kd_status_validasi=1 and sts_riwayat_grade!='6'

	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @kd_pangkat=kd_pangkat,@kd_skala_gaji=kd_skala_gaji,@tmt_gol=tmt_gol
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1 and sts_riwayat_grade!='6'
	ORDER BY id DESC


	-- AMBIL NAMA PANGKAT
	SELECT @nm_pangkat=nm_pangkat, @kd_gol=kd_gol FROM spg_pangkat WHERE kd_pangkat=@kd_pangkat

	-- AMBIL NAMA GOLONGAN
	SELECT @nm_gol=nm_golongan FROM spg_golongan WHERE kd_gol=@kd_gol
	
	
	
	------------------------------
	-- GET SPG_RIWAYAT_TMP_TINGGAL_PEG
	------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT @tmt_tmp_tinggal=MAX(tmt_tmp_tinggal) FROM SPG_RIWAYAT_TMP_TINGGAL_PEG WHERE nip=@nip and tmt_tmp_tinggal<=GETDATE() and kd_status_validasi=1 and tmp_tinggal_ktp='1'

	-- AMBIL RIWAYAT TERBARU
	SELECT @jln_tmp_tinggal=jln_tmp_tinggal, @kd_propinsi=kd_propinsi, @kd_kabupaten=kd_kabupaten, 
	@kd_kecamatan=kd_kecamatan, @kd_kelurahan=kd_kelurahan, @rt_tmp_tinggal=rt_tmp_tinggal, @rw_tmp_tinggal=rw_tmp_tinggal,
	@jns_tmp_tinggal=jns_tmp_tinggal
	FROM SPG_RIWAYAT_TMP_TINGGAL_PEG
	WHERE nip=@nip and tmt_tmp_tinggal=@tmt_tmp_tinggal and kd_status_validasi=1 and tmp_tinggal_ktp='1'
	
	-- AMBIL KET STATUS KEPEMILIKAN
	SELECT @ket_tmp_tinggal=ket_tmp_tinggal FROM SPG_JNS_TMP_TINGGAL WHERE jns_tmp_tinggal=@jns_tmp_tinggal 


	-----------------------------------------------------
	-- GET SPG_PEGAWAI_PENSIUN / STATUS KEAKTIFAN PEGAWAI
	-----------------------------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	SELECT @tmt_status_kepeg=MAX(tmt_pensiun)	FROM spg_pegawai_pensiun WHERE nip=@nip and tmt_pensiun<=GETDATE() and kd_status_validasi=1

	
	-- AMBIL RIWAYAT TERBARU
	SELECT @kd_status_kepeg=kd_status_kepeg,@sts_pensiun=sts_pensiun, @tgl_mulai_kontrak=tgl_mulai,
	@tgl_selesai_kontrak=tgl_akhir, @jns_sts_keaktifan=jns_sts_keaktifan, @kd_tidak_aktif=kd_tidak_aktif
	FROM spg_pegawai_pensiun
	WHERE nip=@nip and tmt_pensiun=@tmt_status_kepeg and kd_status_validasi=1



	-- AMBIL NAMA STATUS
	SELECT @nm_status_kepeg=nm_status_kepeg FROM spg_status_kepeg WHERE kd_status_kepeg=@kd_status_kepeg
	
	-- CHECK STATUS NON AKTIF
	IF (@sts_pensiun='1')
		BEGIN
			SET @tgl_berhenti=@tmt_status_kepeg
		END
	ELSE
		BEGIN
			SET @tgl_berhenti=NULL
		END
	
	
	-----------------------------------------------------
	-- GET SPG_ASURANSI_PEGAWAI
	-----------------------------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	--SELECT @tmt_jamsostek=MAX(tgl_mulai_asuransi_peg) FROM spg_asuransi_pegawai WHERE nip=@nip and tgl_mulai_asuransi_peg<=GETDATE() and kd_status_validasi=1

	
	-- AMBIL RIWAYAT TERBARU
	--SELECT @jst_provider=jst_provider, @jst_template=jst_template  
	--FROM spg_asuransi_pegawai
	--WHERE nip=@nip and tgl_mulai_asuransi_peg=@tmt_jamsostek and kd_status_validasi=1
	
	--IF (@jst_template is NULL)
	--BEGIN
		--select @jst_provider=jst_provider,@jst_template=jst_template FROM spg_perusahaan where cmp_id=@cmp_id
	--END
	
	-----------------------------------------------------
	-- GET PAY_COSTCENTER_UNIT
	-----------------------------------------------------
	
	--AMBIL NILAI COSTCENTER
	--SELECT @kd_costcenter=kd_costcenter FROM pay_costcenter_unit WHERE kd_unit_org=@kd_unit_org and kd_kantor=@kd_kantor
	SET @kd_costcenter=@kd_unit_es3+'|'+@kd_kantor
	
	
	-----------------------------------------------------
	-- GET SPG_KPP
	-----------------------------------------------------
	
	--AMBIL KODE KPP
	--SELECT @kd_kpp=kd_kpp FROM spg_08_kantor WHERE kd_kantor=@kd_kantor	


	-----------------------------------------------------
	-- GET SPG_RIWAYAT_PEND_FORMAL
	-----------------------------------------------------

	SELECT @kd_pend_umum=max(kd_pend_umum) FROM SPG_RIWAYAT_PEND_FORMAL WHERE nip=@nip and kd_status_validasi='1'

	SELECT @nm_lemb_pend_formal=nm_lemb_pend_form,@tmp_lemb_pend_formal=tmp_lemb_pend_form,@tgl_ijazah=tgl_ijazah 
	FROM SPG_RIWAYAT_PEND_FORMAL WHERE nip=@nip and kd_pend_umum=@kd_pend_umum

	SELECT @nm_pend_umum=nm_pend_umum FROM SPG_JNS_PEND_UMUM where kd_pend_umum=@kd_pend_umum


	-----------------------------------------------------
	-- GET SPG_SIPEG_PICTURES
	-----------------------------------------------------

	SELECT @picture=path FROM SPG_SIPEG_PICTURES WHERE keyid=@nip and tablename='spg_pegawai'


	-------------------------
	--TMT PENSIUN
	-------------------------

	IF @kd_gol in ('10','20','30') 
	BEGIN
		SET @tmt_pensiun=dateadd(year,56,@tgl_lahir_peg)
	END
	ELSE IF @kd_gol in ('40','50')
	BEGIN
		 SET @tmt_pensiun=dateadd(year,58,@tgl_lahir_peg)
	END
	ELSE IF @kd_gol in ('60','70','80','90','99') 
	BEGIN
		SET @tmt_pensiun=dateadd(year,60,@tgl_lahir_peg)
	END
	
	SET @tmt_pensiun=SUBSTRING(CAST(DATEADD(MONTH,1,@tmt_pensiun) as VARCHAR(10)),1,7)+'-01'
	
	--print 'tmt_pensiun:'
	--print @tmt_pensiun

	-------------------------
	-- GET JENIS KELAMIN ----
	-------------------------
	SELECT 
		@NM_JNS_KELAMIN_PEG=NM_KELAMIN_PEG 
	FROM 
		SPG_JNS_KELAMIN 
	WHERE 
		JNS_KELAMIN_PEG=@JNS_KELAMIN_PEG


	-------------------------
	---- MASA DINAS OJK  ----
	-------------------------
	SELECT @masa_dinas_bulan=(DATEDIFF(MONTH, @tgl_masuk, GETDATE()))
	SELECT @masa_bulan_cuti_luar_tanggungan=ISNULL(SUM(CAST(DATEDIFF(MONTH, TGL_AWAL_CUTI_PEG, TGL_AKHIR_CUTI_PEG) AS INT )),0) FROM SPG_CUTI_PEGAWAI WHERE NIP=@nip AND JNS_CUTI='16' AND TGL_AWAL_CUTI_PEG<GETDATE()
	SET @masa_tahun_ojk = FLOOR((@masa_dinas_bulan-@masa_bulan_cuti_luar_tanggungan)/12)
	SET @masa_bulan_ojk = (@masa_dinas_bulan-@masa_bulan_cuti_luar_tanggungan)-(@masa_tahun_ojk*12)

	print 'tmt_gol: '+cast(@tmt_gol as varchar)

	-------------------------
	--UPDATE SPG_DATA_CURRENT
	-------------------------

	UPDATE SPG_DATA_CURRENT SET
	kd_agama=@kd_agama,nm_peg=@nm_peg,tgl_lahir_peg=@tgl_lahir_peg,tempat_lahir_peg=@tempat_lahir_peg,jns_kelamin_peg=@jns_kelamin_peg,nm_jns_kelamin_peg=@nm_jns_kelamin_peg,sts_perkawinan=@sts_perkawinan,
	email=@email,cmp_id=@cmp_id,grp_id=@grp_id,jns_gaji=@jns_gaji,tgl_masuk=@tgl_masuk,tgl_berhenti=@tgl_berhenti,gph_id=@gph_id, 
	nip_lama=@nip_lama,nip_baru=@nip_baru,no_rekening=@no_rekening,kd_bank=@kd_bank,keterangan_status=@keterangan_status,metode_pajak=@metode_pajak,
	cabang_bank=@cabang_bank,
	kd_unit_org=@kd_unit_org,kd_kantor=@kd_kantor,jns_jabatan_cur=@jns_jabatan_cur,kd_jabatan_str=@kd_jabatan_str,kd_eselon=@kd_eselon,
	nm_unit_es1=@nm_unit_es1,nm_unit_es2=@nm_unit_es2,nm_unit_es3=@nm_unit_es3,nm_unit_es4=@nm_unit_es4,nm_unit_es5=@nm_unit_es5,
	nm_unit_es6=@nm_unit_es6,nm_unit_es7=@nm_unit_es7,nm_jabatan=@nm_jabatan,nm_eselon=@nm_eselon,
	kd_pangkat=@kd_pangkat,kd_gol=@kd_gol,nm_pangkat=@nm_pangkat,tmt_pangkat=@tmt_pangkat, 
	sts_pensiun=@sts_pensiun,kd_status_kepeg=@kd_status_kepeg,nm_status_kepeg=@nm_status_kepeg,tmt_status_kepeg=@tmt_status_kepeg, 
	kd_kpp=@kd_kpp, kd_grade=@kd_grade, kd_costcenter=@kd_costcenter, jl_tmp_tinggal_cur=@jln_tmp_tinggal, jst_provider=@jst_provider, 
	jst_template=@jst_template, nm_kantor=@nm_kantor, jns_kantor=@jns_kantor, nm_gol=@nm_gol, npwp=@npwp,
	tgl_mulai_kontrak=@tgl_mulai_kontrak,tgl_selesai_kontrak=@tgl_selesai_kontrak, TMT_JABATAN=@tmt_jabatan, status_jamsostek=@status_jamsostek,
	kd_propinsi=@kd_propinsi, kd_kabupaten=@kd_kabupaten, kd_kecamatan=@kd_kecamatan, kd_kelurahan=@kd_kelurahan, tmt_tmp_tinggal=@tmt_tmp_tinggal,  
	rt_tmp_tinggal_cur=@rt_tmp_tinggal, rw_tmp_tinggal_cur=@rw_tmp_tinggal, jns_tmp_tinggal=@jns_tmp_tinggal, ket_tmp_tinggal=@ket_tmp_tinggal,
	jns_pend_formal=@kd_pend_umum, ket_pend_formal=@nm_pend_umum, kd_pend_umum=@kd_pend_umum, nm_pend_umum=@nm_pend_umum, 
	nm_lemb_pend_formal=@nm_lemb_pend_formal, tmp_lemb_pend_formal=@tmp_lemb_pend_formal, tgl_ijazah=@tgl_ijazah,
	kd_pangkat_tgs=@kd_pangkat_tgs, kd_jabatan_tgs=@kd_jabatan_tgs, kd_unit_org_tgs=@kd_unit_org_tgs, 
	kd_kantor_tgs=@kd_kantor_tgs, tmt_jabatan_tgs=@tmt_jabatan_tgs, penugasan=@penugasan, picture=@picture, 
	kd_skala_gaji=@kd_skala_gaji,tmt_lembaga_asal=@tmt_lembaga_asal,tmt_ojk=@tmt_ojk,tmt_penempatan=@tmt_penempatan,
	tmt_satker=@tmt_satker,tmt_gol=@tmt_gol,tmt_pensiun=@tmt_pensiun, lembaga_asal=@lembaga_asal,MASA_TAHUN_OJK=@masa_tahun_ojk, MASA_BULAN_OJK=@masa_bulan_ojk,
	jns_sts_keaktifan=@jns_sts_keaktifan, kd_tidak_aktif=@kd_tidak_aktif
	WHERE nip=@nip

	--INSERT INTO LOG
	--INSERT INTO tbl_log_procedure (TIME,LOG) VALUES (GETDATE(), @nip)

	-----------------------------------------------------
	-- GET SPG_RIWAYAT_KELUARGA_PEG
	-----------------------------------------------------
	select @nip_pasangan=nip_pasangan from SPG_RIWAYAT_KELUARGA_PEG where nip=@nip and internal='1' and (nip_pasangan!='' and nip_pasangan is not null)
	
	if (@nip_pasangan!='' and @nip_pasangan is not null)
	BEGIN

		UPDATE SPG_DATA_CURRENT SET nip_pasangan=@nip_pasangan WHERE nip=@nip

		UPDATE SPG_DATA_CURRENT SET nip_pasangan=@nip WHERE nip=@nip_pasangan

	END


END





GO
/****** Object:  StoredProcedure [dbo].[update_data_tmt_all]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[update_data_tmt_all]
AS
BEGIN

	declare @nip varchar(50)
	
	declare cur CURSOR LOCAL for
	    select nip from spg_data_current
	
	open cur
	
	fetch next from cur into @nip
	
	while @@FETCH_STATUS = 0 BEGIN
	
	    --execute your sproc on each row
	    exec dbo.update_tmt_riwayat_jabatan @nip
	
	    fetch next from cur into @nip
	END
	
	close cur
	deallocate cur

END




GO
/****** Object:  StoredProcedure [dbo].[update_data_tmt_lvl_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[update_data_tmt_lvl_jabatan]
AS
BEGIN

	declare @nip varchar(50)
	
	declare cur CURSOR LOCAL for
	    select nip from spg_data_current
	
	open cur
	
	fetch next from cur into @nip
	
	while @@FETCH_STATUS = 0 BEGIN
	
	    --execute your sproc on each row
	    exec dbo.update_tmt_riwayat_kepangkatan @nip
	
	    fetch next from cur into @nip
	END
	
	close cur
	deallocate cur

END





GO
/****** Object:  StoredProcedure [dbo].[update_status_tunjangan_keluarga]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[update_status_tunjangan_keluarga]
AS
BEGIN

	DECLARE 
	@id int,
	@tgl_lahir_keluarga date,
	@umur float,
	@tanggungan int,
	@tanggung int
	
	EXEC('DISABLE TRIGGER [dbo].[spg_riwayat_keluarga_peg_trg] ON [dbo].[SPG_RIWAYAT_KELUARGA_PEG]');
	
	declare cur CURSOR LOCAL for
	    select a.id from SPG_RIWAYAT_KELUARGA_PEG a LEFT JOIN SPG_DATA_CURRENT b ON a.nip=b.nip where b.sts_pensiun!='1' and a.kd_hubungan_keluarga in ('3','4','5')
	
	open cur
	
	fetch next from cur into @id
	
	while @@FETCH_STATUS = 0 BEGIN
	
	    --execute your sproc on each row
		--select @tgl_lahir_keluarga=tgl_lahir_keluarga from SPG_RIWAYAT_KELUARGA_PEG where id=@id

		--select @umur=(datediff(day,@tgl_lahir_keluarga,GETDATE()) / 365.2425)

		select @umur = (0 + Convert(Char(8),GETDATE(),112) - Convert(Char(8),TGL_LAHIR_KELUARGA,112)) / 10000,@tanggung=TANGGUNGAN from SPG_RIWAYAT_KELUARGA_PEG where id=@id

		IF (@umur>=25)
			BEGIN
				SET @tanggungan='0'
			END
		ELSE
			BEGIN 
				SET @tanggungan = @tanggung
			END
	    
		update SPG_RIWAYAT_KELUARGA_PEG set tanggungan=@tanggungan where id=@id

	    fetch next from cur into @id
	END
	
	close cur
	deallocate cur

	EXEC('ENABLE TRIGGER [dbo].[spg_riwayat_keluarga_peg_trg] ON [dbo].[SPG_RIWAYAT_KELUARGA_PEG]');
END

GO
/****** Object:  StoredProcedure [dbo].[update_status_tunjangan_keluarga_peg]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[update_status_tunjangan_keluarga_peg] @id varchar(20)
AS
BEGIN

	DECLARE 
	@tgl_lahir_keluarga date,
	@umur float,
	@tanggungan int,
	@tanggung int,
	@kd_hub_keluarga varchar(20)
		
		--select @tgl_lahir_keluarga=tgl_lahir_keluarga ,@kd_hub_keluarga=kd_hubungan_keluarga,@tanggung=tanggungan from SPG_RIWAYAT_KELUARGA_PEG where id=@id

		--select @umur=(datediff(day,@tgl_lahir_keluarga,GETDATE()) / 365.2425)
		
		select @umur = (0 + Convert(Char(8),GETDATE(),112) - Convert(Char(8),TGL_LAHIR_KELUARGA,112)) / 10000,@kd_hub_keluarga=kd_hubungan_keluarga,@tanggung=TANGGUNGAN from SPG_RIWAYAT_KELUARGA_PEG where id=@id and kd_hubungan_keluarga in ('3','4','5')
		
		
		--if(@kd_hub_keluarga='3' OR @kd_hub_keluarga='4' OR @kd_hub_keluarga='5')
		--BEGIN
			IF (@umur>=25)
			BEGIN
				SET @tanggungan='0'
			END

			IF (@umur<25)
			BEGIN
				SET @tanggungan=@tanggung
			END
		--END
	    

		IF (@umur is not null or @umur != '')
		BEGIN	
			update SPG_RIWAYAT_KELUARGA_PEG set tanggungan=@tanggungan where id=@id
		END
END


GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_tmt_riwayat_jabatan] @nip varchar(20)
AS
BEGIN
	
	--SPG_08_RIWAYAT_JABATAN
	DECLARE
	@id_riwayat INT,
	@id_riwayat_previous INT,
	@tmt_jabatan date, 
	@tmt_jabatan_previous date, 
	@tmt_penempatan date, 
	@tmt_penempatan_previous date, 
	@tmt_satker date, 
	@tmt_satker_previous date, 
	@kd_unit_org varchar(50),
	@kd_unit_org_previous varchar(50),
	@kd_satker varchar(50),
	@kd_satker_previous varchar(50),
	@no_urut_jab int,
	@no_urut_jab_previous int,
	@tgl_riwayat date,
	@tgl_riwayat_previous date,
	@tmt_reog date,
	@tmt_reog_previous date

	-----------------------------
	-- GET SPG_08_RIWAYAT_JABATAN
	-----------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT TOP 1 @tmt_jabatan=tmt_jab_peg, @no_urut_jab=no_urut_jab FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and kd_status_validasi=1 and status_jabatan='0' ORDER BY tmt_jab_peg DESC, no_urut_jab DESC
	
	-- print 'tmt_jabatan:'+cast(@tmt_jabatan as varchar)
	
	-- AMBIL TMT SEBELUMNYA
	-- DEFINITIF
	SELECT TOP 1 @tmt_jabatan_previous=tmt_jab_peg, @no_urut_jab_previous=no_urut_jab FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab<@no_urut_jab ORDER BY tmt_jab_peg DESC, no_urut_jab DESC
	
	--print 'tmt_jabatan_previous:'+cast(@tmt_jabatan_previous as varchar)

	-- AMBIL RIWAYAT TERBARU
	SELECT @id_riwayat=ID,@kd_unit_org=kd_unit_org, @tmt_penempatan=tmt_penempatan, @tmt_satker=tmt_satker, @tgl_riwayat=tanggal_riwayat, @tmt_reog=tmt_reog  
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab
	
	-- AMBIL RIWAYAT SEBELUMNYA
	SELECT @id_riwayat_previous=ID,@kd_unit_org_previous=kd_unit_org, @tmt_penempatan_previous=tmt_penempatan, @tmt_satker_previous=tmt_satker, @tgl_riwayat_previous=tanggal_riwayat, @tmt_reog_previous=tmt_reog
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan_previous and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab_previous

	-- AMBIL DEPARTEMEN TERBARU
	-- TODO: Ganti @kd_satker menjadi "kode_satker_organisasi"
	SELECT @kd_satker=kode_satker_organisasi FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org

	-- AMBIL DEPARTEMEN SEBELUMNYA
	-- TODO: Ganti @kd_satker menjadi "kode_satker_organisasi"
	SELECT @kd_satker_previous=kode_satker_organisasi FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org_previous
	
	IF((@tmt_jabatan_previous is null or @tmt_jabatan_previous='') and (@no_urut_jab_previous is null or @no_urut_jab_previous=''))
		BEGIN
			SET @id_riwayat_previous=@id_riwayat
			SET @no_urut_jab_previous=@no_urut_jab
			SET @kd_unit_org_previous=@kd_unit_org
			SET @tmt_penempatan_previous=@tmt_penempatan
			SET @tmt_satker_previous=@tmt_satker
			SET @kd_satker_previous=@kd_satker
		END
	--print 'kd_unit_org:'+cast(@kd_unit_org as varchar(30))
	--print 'kd_unit_org_prev:'+cast(@kd_unit_org_previous as varchar(30))
	-- PERUBAHAN UNIT KERJA	
	IF(@kd_unit_org_previous is null)
		BEGIN
			SET @kd_unit_org_previous=''
		END
	IF @kd_unit_org!=@kd_unit_org_previous
		BEGIN
			IF((@kd_unit_org_previous is null or @kd_unit_org_previous='') and (@id_riwayat_previous is null or @id_riwayat_previous=''))
				BEGIN
					SET @tmt_penempatan=@tmt_jabatan
				END
			ELSE IF((@kd_unit_org_previous is null or @kd_unit_org_previous='') and (@id_riwayat_previous is not null or @id_riwayat_previous!=''))
				BEGIN
					SET @tmt_penempatan=@tgl_riwayat
				END
			-- APABILA TERJADI REORGANISASI
			ELSE IF(@tmt_reog is not null and @tmt_reog != '1900-01-01')
				BEGIN
					SET @tmt_penempatan = @tmt_penempatan_previous
				END
			ELSE
				BEGIN
					SET @tmt_penempatan=@tgl_riwayat
				END
		END
	ELSE
		BEGIN
			SET @tmt_penempatan=@tmt_penempatan_previous
		END

	-- PERUBAHAN SATUAN KERJA (DEPARTEMEN)
	-- TODO: Perlu penyesuaian akibat "kode_satker_organisasi"
	IF(@kd_satker_previous is null)
		BEGIN
			SET @kd_satker_previous=''
		END
	IF @kd_satker!=@kd_satker_previous
		BEGIN
			IF((@kd_satker_previous is null or @kd_satker_previous='') and (@id_riwayat_previous is null or @id_riwayat_previous=''))
				BEGIN
					SET @tmt_satker=@tmt_jabatan
				END
			ELSE IF((@kd_satker_previous is null or @kd_satker_previous='') and (@id_riwayat_previous is not null or @id_riwayat_previous!=''))
				BEGIN
					SET @tmt_satker=@tgl_riwayat
				END
			-- APABILA TERJADI REORGANISASI
			ELSE IF(@tmt_reog is not null and @tmt_reog != '1900-01-01')
				BEGIN
					SET @tmt_satker = @tmt_satker_previous
				END
			ELSE
				BEGIN
					SET @tmt_satker=@tgl_riwayat
				END
		END
	ELSE
		BEGIN
			SET @tmt_satker=@tmt_satker_previous
		END
	
	IF(@id_riwayat=@id_riwayat_previous)
	BEGIN 
		SET @tmt_jabatan = @tmt_jabatan
		SET @tmt_penempatan = @tmt_jabatan
		SET @tmt_satker = @tmt_jabatan
	END

	-------------------------------
	--UPDATE SPG_08_RIWAYAT_JABATAN
	-------------------------------
	
	--print 'id_riwayat:'+cast(@id_riwayat as varchar(10))
	--print 'id_riwayat_previous:'+cast(@id_riwayat_previous as varchar(10))
	
	UPDATE SPG_08_RIWAYAT_JABATAN SET 
	tmt_penempatan=@tmt_penempatan, tmt_satker=@tmt_satker 
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab
	
END
GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_tmt_riwayat_jabatan_bak_20160219] @nip varchar(20)
AS
BEGIN
	
	--SPG_08_RIWAYAT_JABATAN
	DECLARE
	@id_riwayat INT,
	@id_riwayat_previous INT,
	@tmt_jabatan date, 
	@tmt_jabatan_previous date, 
	@tmt_penempatan date, 
	@tmt_penempatan_previous date, 
	@tmt_satker date, 
	@tmt_satker_previous date, 
	@kd_unit_org varchar(50),
	@kd_unit_org_previous varchar(50),
	@kd_satker varchar(50),
	@kd_satker_previous varchar(50)


	-----------------------------
	-- GET SPG_08_RIWAYAT_JABATAN
	-----------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT @tmt_jabatan=MAX(tmt_jab_peg) FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and kd_status_validasi=1 and status_jabatan='0'
	
	-- print 'tmt_jabatan:'+cast(@tmt_jabatan as varchar)
	
	-- AMBIL TMT SEBELUMNYA
	-- DEFINITIF
	SELECT @tmt_jabatan_previous=MAX(tmt_jab_peg) FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0'
	
	--print 'tmt_jabatan_previous:'+cast(@tmt_jabatan_previous as varchar)

	-- AMBIL RIWAYAT TERBARU
	SELECT @id_riwayat=ID,@kd_unit_org=kd_unit_org, @tmt_penempatan=tmt_penempatan, @tmt_satker=tmt_satker  
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0'
	
	-- AMBIL RIWAYAT SEBELUMNYA
	SELECT @id_riwayat_previous=ID,@kd_unit_org_previous=kd_unit_org, @tmt_penempatan_previous=tmt_penempatan, @tmt_satker_previous=tmt_satker 
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan_previous and kd_status_validasi=1 and status_jabatan='0'

	-- AMBIL DEPARTEMEN TERBARU
	SELECT @kd_satker=kd_unit_es3 FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org

	-- AMBIL DEPARTEMEN SEBELUMNYA
	SELECT @kd_satker_previous=kd_unit_es3 FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org_previous

	-- PERUBAHAN UNIT KERJA	
	IF @kd_unit_org!=@kd_unit_org_previous
		BEGIN
			SET @tmt_penempatan=@tmt_jabatan
		END
	ELSE
		BEGIN
			SET @tmt_penempatan=@tmt_penempatan_previous
		END

	-- PERUBAHAN SATUAN KERJA (DEPARTEMEN)
	IF @kd_satker!=@kd_satker_previous
		BEGIN
			SET @tmt_satker=@tmt_jabatan
		END
	ELSE
		BEGIN
			SET @tmt_satker=@tmt_satker_previous
		END
	
	IF(@id_riwayat=@id_riwayat_previous)
	BEGIN 
		SET @tmt_jabatan = @tmt_jabatan
		SET @tmt_penempatan = @tmt_jabatan
		SET @tmt_satker = @tmt_jabatan
	END
	-------------------------------
	--UPDATE SPG_08_RIWAYAT_JABATAN
	-------------------------------
	
	print 'id_riwayat:'+cast(@id_riwayat as varchar(10))
	print 'id_riwayat_previous:'+cast(@id_riwayat_previous as varchar(10))
	
	UPDATE SPG_08_RIWAYAT_JABATAN SET 
	tmt_penempatan=@tmt_penempatan, tmt_satker=@tmt_satker 
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0'
	
END






GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_jabatan_bak_20191114]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_tmt_riwayat_jabatan_bak_20191114] @nip varchar(20)
AS
BEGIN
	
	--SPG_08_RIWAYAT_JABATAN
	DECLARE
	@id_riwayat INT,
	@id_riwayat_previous INT,
	@tmt_jabatan date, 
	@tmt_jabatan_previous date, 
	@tmt_penempatan date, 
	@tmt_penempatan_previous date, 
	@tmt_satker date, 
	@tmt_satker_previous date, 
	@kd_unit_org varchar(50),
	@kd_unit_org_previous varchar(50),
	@kd_satker varchar(50),
	@kd_satker_previous varchar(50),
	@no_urut_jab int,
	@no_urut_jab_previous int,
	@tgl_riwayat date,
	@tgl_riwayat_previous date,
	@tmt_reog date,
	@tmt_reog_previous date

	-----------------------------
	-- GET SPG_08_RIWAYAT_JABATAN
	-----------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT TOP 1 @tmt_jabatan=tmt_jab_peg, @no_urut_jab=no_urut_jab FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=GETDATE() and kd_status_validasi=1 and status_jabatan='0' ORDER BY tmt_jab_peg DESC, no_urut_jab DESC
	
	-- print 'tmt_jabatan:'+cast(@tmt_jabatan as varchar)
	
	-- AMBIL TMT SEBELUMNYA
	-- DEFINITIF
	SELECT TOP 1 @tmt_jabatan_previous=tmt_jab_peg, @no_urut_jab_previous=no_urut_jab FROM spg_08_riwayat_jabatan WHERE nip=@nip and tmt_jab_peg<=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab<@no_urut_jab ORDER BY tmt_jab_peg DESC, no_urut_jab DESC
	
	--print 'tmt_jabatan_previous:'+cast(@tmt_jabatan_previous as varchar)

	-- AMBIL RIWAYAT TERBARU
	SELECT @id_riwayat=ID,@kd_unit_org=kd_unit_org, @tmt_penempatan=tmt_penempatan, @tmt_satker=tmt_satker, @tgl_riwayat=tanggal_riwayat, @tmt_reog=tmt_reog  
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab
	
	-- AMBIL RIWAYAT SEBELUMNYA
	SELECT @id_riwayat_previous=ID,@kd_unit_org_previous=kd_unit_org, @tmt_penempatan_previous=tmt_penempatan, @tmt_satker_previous=tmt_satker, @tgl_riwayat_previous=tanggal_riwayat, @tmt_reog_previous=tmt_reog
	FROM spg_08_riwayat_jabatan
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan_previous and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab_previous

	-- AMBIL DEPARTEMEN TERBARU
	SELECT @kd_satker=case when kd_unit_es3 !='' then kd_unit_es3 when KD_UNIT_ES2 !='' then KD_UNIT_ES2 end FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org

	-- AMBIL DEPARTEMEN SEBELUMNYA
	SELECT @kd_satker_previous=case when kd_unit_es3 !='' then kd_unit_es3 when KD_UNIT_ES2 !='' then KD_UNIT_ES2 end FROM spg_08_unit_organisasi where kd_unit_org=@kd_unit_org_previous
	
	IF((@tmt_jabatan_previous is null or @tmt_jabatan_previous='') and (@no_urut_jab_previous is null or @no_urut_jab_previous=''))
		BEGIN
			SET @id_riwayat_previous=@id_riwayat
			SET @no_urut_jab_previous=@no_urut_jab
			SET @kd_unit_org_previous=@kd_unit_org
			SET @tmt_penempatan_previous=@tmt_penempatan
			SET @tmt_satker_previous=@tmt_satker
			SET @kd_satker_previous=@kd_satker
		END
	print 'kd_unit_org:'+cast(@kd_unit_org as varchar(30))
	print 'kd_unit_org_prev:'+cast(@kd_unit_org_previous as varchar(30))
	-- PERUBAHAN UNIT KERJA	
	IF(@kd_unit_org_previous is null)
		BEGIN
			SET @kd_unit_org_previous=''
		END
	IF @kd_unit_org!=@kd_unit_org_previous
		BEGIN
			IF((@kd_unit_org_previous is null or @kd_unit_org_previous='') and (@id_riwayat_previous is null or @id_riwayat_previous=''))
				BEGIN
					SET @tmt_penempatan=@tmt_jabatan
				END
			ELSE IF((@kd_unit_org_previous is null or @kd_unit_org_previous='') and (@id_riwayat_previous is not null or @id_riwayat_previous!=''))
				BEGIN
					SET @tmt_penempatan=@tgl_riwayat
				END
			-- APABILA TERJADI REORGANISASI
			ELSE IF(@tmt_reog is not null and @tmt_reog != '1900-01-01')
				BEGIN
					SET @tmt_penempatan = @tmt_penempatan_previous
				END
			ELSE
				BEGIN
					SET @tmt_penempatan=@tgl_riwayat
				END
		END
	ELSE
		BEGIN
			SET @tmt_penempatan=@tmt_penempatan_previous
		END

	-- PERUBAHAN SATUAN KERJA (DEPARTEMEN)
	IF(@kd_satker_previous is null)
		BEGIN
			SET @kd_satker_previous=''
		END
	IF @kd_satker!=@kd_satker_previous
		BEGIN
			IF((@kd_satker_previous is null or @kd_satker_previous='') and (@id_riwayat_previous is null or @id_riwayat_previous=''))
				BEGIN
					SET @tmt_satker=@tmt_jabatan
				END
			ELSE IF((@kd_satker_previous is null or @kd_satker_previous='') and (@id_riwayat_previous is not null or @id_riwayat_previous!=''))
				BEGIN
					SET @tmt_satker=@tgl_riwayat
				END
			-- APABILA TERJADI REORGANISASI
			ELSE IF(@tmt_reog is not null and @tmt_reog != '1900-01-01')
				BEGIN
					SET @tmt_satker = @tmt_satker_previous
				END
			ELSE
				BEGIN
					SET @tmt_satker=@tgl_riwayat
				END
		END
	ELSE
		BEGIN
			SET @tmt_satker=@tmt_satker_previous
		END
	
	IF(@id_riwayat=@id_riwayat_previous)
	BEGIN 
		SET @tmt_jabatan = @tmt_jabatan
		SET @tmt_penempatan = @tmt_jabatan
		SET @tmt_satker = @tmt_jabatan
	END

	-------------------------------
	--UPDATE SPG_08_RIWAYAT_JABATAN
	-------------------------------
	
	print 'id_riwayat:'+cast(@id_riwayat as varchar(10))
	print 'id_riwayat_previous:'+cast(@id_riwayat_previous as varchar(10))
	
	UPDATE SPG_08_RIWAYAT_JABATAN SET 
	tmt_penempatan=@tmt_penempatan, tmt_satker=@tmt_satker 
	WHERE nip=@nip and tmt_jab_peg=@tmt_jabatan and kd_status_validasi=1 and status_jabatan='0' and no_urut_jab=@no_urut_jab
	
END








GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_kepangkatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_tmt_riwayat_kepangkatan] @nip varchar(20)
AS
BEGIN
	
	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@id_riwayat INT,
	@id_riwayat_previous INT,
	@tmt_pangkat date, 
	@tmt_pangkat_previous date, 
	@tmt_gol date, 
	@tmt_gol_previous date, 
	@kd_pangkat varchar(20),
	@kd_pangkat_previous varchar(20),
	@kd_gol varchar(20),
	@kd_gol_previous varchar(20),
	@no_urut_pang int,
	@no_urut_pang_previous int,
	@kd_eselon varchar(2),
	@kd_eselon_previous varchar(2)

	------------------------------
	-- GET SPG_RIWAYAT_KEPANGKATAN
	------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT TOP 1 @tmt_pangkat=tmt_pang_peg, @no_urut_pang=no_urut_pang_peg FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=GETDATE() and kd_status_validasi=1 ORDER BY tmt_pang_peg DESC, kd_skala_gaji DESC, no_urut_pang_peg DESC
	
	print 'tmt_pangkat:'+cast(@tmt_pangkat as varchar)
	
	-- AMBIL TMT SEBELUMNYA
	-- DEFINITIF
	SELECT TOP 1 @tmt_pangkat_previous=tmt_pang_peg, @no_urut_pang_previous=no_urut_pang_peg FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=@tmt_pangkat and kd_status_validasi=1 and no_urut_pang_peg < @no_urut_pang ORDER BY tmt_pang_peg DESC, kd_skala_gaji DESC, no_urut_pang_peg DESC
	
	print 'tmt_pangkat_previous:'+cast(@tmt_pangkat_previous as varchar)

	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @id_riwayat=ID,@kd_pangkat=kd_pangkat, @tmt_gol=tmt_gol 
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1 and no_urut_pang_peg=@no_urut_pang
	
	-- AMBIL KD GOL TERBARU
	SELECT @kd_gol=kd_gol FROM SPG_PANGKAT where kd_pangkat=@kd_pangkat
		
	-- AMBIL KD ESELON TERBARU
	SELECT @kd_eselon=kd_eselon FROM SPG_GOLONGAN where kd_gol=@kd_gol

	-- AMBIL RIWAYAT SEBELUMNYA
	SELECT TOP 1 @id_riwayat_previous=ID,@kd_pangkat_previous=kd_pangkat, @tmt_gol_previous=tmt_gol 
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat_previous and kd_status_validasi=1 and no_urut_pang_peg=@no_urut_pang_previous

	-- AMBIL KD GOL SEBELUMNYA
	SELECT @kd_gol_previous=kd_gol FROM SPG_PANGKAT where kd_pangkat=@kd_pangkat_previous
	
	-- AMBIL KD ESELON SEBELUMNYA
	SELECT @kd_eselon_previous=kd_eselon FROM SPG_GOLONGAN where kd_gol=@kd_gol_previous

	print 'kd_gol:'+@kd_gol
	print 'kd_gol_previous:'+@kd_gol_previous
	print '@no_urut_pang_previous:'+cast(@no_urut_pang_previous AS varchar)
	print 'tmt_gol:'+cast(@tmt_gol as varchar)
	print 'tmt_gol_previous:'+cast(@tmt_gol_previous as varchar)
	print 'kd_eselon:'+@kd_eselon
	print 'kd_eselon_previous:'+@kd_eselon_previous

	IF((@tmt_pangkat_previous is null or @tmt_pangkat_previous='' or @tmt_pangkat_previous='1900-01-01') and (@no_urut_pang_previous is null or @no_urut_pang_previous=''))
		BEGIN
			SET @id_riwayat_previous=@id_riwayat
			SET @no_urut_pang_previous=@no_urut_pang
			SET @kd_pangkat_previous=@kd_pangkat
			SET @tmt_gol_previous=@tmt_gol
			SET @kd_gol_previous=@kd_gol
		END

	-- PERUBAHAN UNIT KERJA	
	IF (@kd_gol!=@kd_gol_previous AND @kd_eselon!=@kd_eselon_previous)
		BEGIN
			SET @tmt_gol=@tmt_pangkat
		END
	ELSE IF (@kd_gol!=@kd_gol_previous AND @kd_eselon=@kd_eselon_previous AND @kd_eselon='50' AND @kd_eselon_previous='50')
		BEGIN
			SET @tmt_gol=@tmt_pangkat
		END
	ELSE IF (@kd_gol!=@kd_gol_previous AND @kd_eselon=@kd_eselon_previous)
		BEGIN
			SET @tmt_gol=@tmt_gol_previous
		END
	ELSE
		BEGIN
			SET @tmt_gol=@tmt_gol_previous
		END

	IF @id_riwayat=@id_riwayat_previous 
	BEGIN
		SET @tmt_gol = @tmt_pangkat
	END

	print 'tmt_gol_final:'+cast(@tmt_gol as varchar)
	
	print 'id terbaru:'+cast(@id_riwayat as varchar)

	print 'no urut grade:'+cast(@no_urut_pang as varchar)

	-------------------------------
	--UPDATE SPG_RIWAYAT_KEPANGKATAN
	-------------------------------

	UPDATE SPG_RIWAYAT_KEPANGKATAN SET 
	tmt_gol=@tmt_gol
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1 and no_urut_pang_peg=@no_urut_pang
	
END








GO
/****** Object:  StoredProcedure [dbo].[update_tmt_riwayat_kepangkatan_bak_20160219]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[update_tmt_riwayat_kepangkatan_bak_20160219] @nip varchar(20)
AS
BEGIN
	
	--SPG_RIWAYAT_KEPANGKATAN
	DECLARE
	@id_riwayat INT,
	@id_riwayat_previous INT,
	@tmt_pangkat date, 
	@tmt_pangkat_previous date, 
	@tmt_gol date, 
	@tmt_gol_previous date, 
	@kd_pangkat varchar(20),
	@kd_pangkat_previous varchar(20),
	@kd_gol varchar(20),
	@kd_gol_previous varchar(20)

	------------------------------
	-- GET SPG_RIWAYAT_KEPANGKATAN
	------------------------------

	-- AMBIL TMT TERBARU TAPI DI TANGGAL YANG SUDAH BERLAKU
	-- DEFINITIF
	SELECT @tmt_pangkat=MAX(tmt_pang_peg) FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=GETDATE() and kd_status_validasi=1
	
	print 'tmt_pangkat:'+cast(@tmt_pangkat as varchar)
	
	-- AMBIL TMT SEBELUMNYA
	-- DEFINITIF
	SELECT @tmt_pangkat_previous=MAX(tmt_pang_peg) FROM spg_riwayat_kepangkatan WHERE nip=@nip and tmt_pang_peg<=@tmt_pangkat and kd_status_validasi=1
	
	print 'tmt_pangkat_previous:'+cast(@tmt_pangkat_previous as varchar)

	-- AMBIL RIWAYAT TERBARU
	SELECT TOP 1 @id_riwayat=ID,@kd_pangkat=kd_pangkat, @tmt_gol=tmt_gol 
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1
	order by id desc

	SELECT @kd_gol=kd_gol FROM SPG_PANGKAT where kd_pangkat=@kd_pangkat
	
	-- AMBIL RIWAYAT SEBELUMNYA
	SELECT TOP 1 @id_riwayat_previous=ID,@kd_pangkat_previous=kd_pangkat, @tmt_gol_previous=tmt_gol 
	FROM spg_riwayat_kepangkatan
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat_previous and kd_status_validasi=1
	order by id asc

	SELECT @kd_gol_previous=kd_gol FROM SPG_PANGKAT where kd_pangkat=@kd_pangkat_previous

	print 'kd_gol:'+@kd_gol
	print 'kd_gol_previous:'+@kd_gol_previous
	print 'tmt_gol:'+cast(@tmt_gol as varchar)
	print 'tmt_gol_previous:'+cast(@tmt_gol_previous as varchar)

	-- PERUBAHAN UNIT KERJA	
	IF @kd_gol!=@kd_gol_previous
		BEGIN
			SET @tmt_gol=@tmt_pangkat
		END
	ELSE
		BEGIN
			SET @tmt_gol=@tmt_gol_previous
		END

	IF @id_riwayat=@id_riwayat_previous 
	BEGIN
		SET @tmt_gol = @tmt_pangkat
	END

	print 'tmt_gol_final:'+cast(@tmt_gol as varchar)
	
	print 'id terbaru:'+cast(@id_riwayat as varchar)

	-------------------------------
	--UPDATE SPG_RIWAYAT_KEPANGKATAN
	-------------------------------

	UPDATE SPG_RIWAYAT_KEPANGKATAN SET 
	tmt_gol=@tmt_gol
	WHERE nip=@nip and tmt_pang_peg=@tmt_pangkat and kd_status_validasi=1 and id=@id_riwayat
	
END






GO
/****** Object:  StoredProcedure [dbo].[usp_AktivasiTunjanganRumah]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Created By : Angga
Date Created : 8 Juli 2020
desc : untuk aktifkan tunjangan rumah
*/
CREATE PROC [dbo].[usp_AktivasiTunjanganRumah]

AS
BEGIN

	DECLARE @TJP_ID VARCHAR(10)
	DECLARE @NIP VARCHAR(20)
	DECLARE @KPG_ID VARCHAR(10) = 'KPG-000004' -- KPG_ID Tunjangan Rumah
	DECLARE @TUNJANGAN INT
	DECLARE @TMT DATE
	DECLARE @KETERANGAN VARCHAR(1000)
	DECLARE @KETERANGAN_2 VARCHAR(1000)
	DECLARE @NAMA_UBAH VARCHAR(20)
	DECLARE @TGL_UBAH DATE
	DECLARE @TMT_END DATE

	--SELECT @TJP_ID = TJP_ID, @NIP = NIP, @TUNJANGAN = TUNJANGAN, @KETERANGAN = KETERANGAN,
	--@KETERANGAN_2 = KETERANGAN_2, @NAMA_UBAH = NAMA_UBAH, @TGL_UBAH = TGL_UBAH, @TMT_END = TMT_END
	--FROM dbo.PAY_TUNJANGAN_PEGAWAI
	--WHERE KPG_ID = 'KPG-000003' AND TMT_END >= GETDATE() AND TUNJANGAN = 1

	INSERT INTO dbo.PAY_TUNJANGAN_PEGAWAI
	        ( TJP_ID ,
	          NIP ,
	          KPG_ID ,
	          TUNJANGAN ,
	          TMT ,
	          KETERANGAN ,
	          KETERANGAN_2 ,
	          NAMA_UBAH ,
	          TGL_UBAH ,
	          TMT_END
	        )

	SELECT 
			TJP_ID, NIP, 
			'KPG-000004', -- KPG_ID
			1, -- TUNJANGAN di set 1 untuk aktif
			DATEADD(DAY, 1, TMT_END), -- TMT diambil dari TMT_END Plafon RUmah + 1 hari
			KETERANGAN, 
			KETERANGAN_2, 
			NAMA_UBAH, 
			TGL_UBAH, 
			NULL
	FROM dbo.PAY_TUNJANGAN_PEGAWAI
	WHERE KPG_ID = 'KPG-000003' AND DATEADD(DAY, 1, TMT_END) < GETDATE() AND TUNJANGAN = 1 and TMT_END<>'1900-01-01'

	-- Insert ke tabel history
	INSERT INTO dbo.HIST_PAY_TUNJANGAN_PEGAWAI
        ( TJP_ID ,
          NIP ,
          KPG_ID ,
          TUNJANGAN ,
          TMT ,
          TMT_END ,
          KETERANGAN ,
          KETERANGAN_2 ,
          REMARK ,
          CREATED_BY ,
          CREATED_DATE ,
          STSRC
        )
	SELECT 
			TJP_ID,
			NIP, 
			KPG_ID, -- KPG_ID
			TUNJANGAN, -- TUNJANGAN di set 1 untuk aktif
			TMT, -- TMT diambil dari TMT_END Plafon RUmah + 1 hari
			TMT_END,
			KETERANGAN, 
			KETERANGAN_2,
			'AktivasiTunjanganRumah',
			'Scheduler',
			GETDATE(),
			'A'
	FROM dbo.PAY_TUNJANGAN_PEGAWAI
	WHERE KPG_ID = 'KPG-000003' AND DATEADD(DAY, 1, TMT_END) < GETDATE() AND TUNJANGAN = 1 and TMT_END<>'1900-01-01'

	-- Nonaktifkan Plafon sewa rumah yang sudah lewat TMT_END
	DELETE FROM dbo.PAY_TUNJANGAN_PEGAWAI
	WHERE KPG_ID = 'KPG-000003' AND DATEADD(DAY, 1, TMT_END) < GETDATE() AND TUNJANGAN = 1 and TMT_END<>'1900-01-01'

END

GO
/****** Object:  StoredProcedure [dbo].[usp_calendar_workday]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[usp_calendar_workday] 
AS
BEGIN
	select workday_year as Tahun, [Senin], [Selasa],[Rabu], [Kamis], [Jumat], [Sabtu], [Minggu]
	from (
		select workday_year, workday_dayName, 'True' as exist
		from FW_Workday
		where stsrc = 'A'
	) as SourceTable
	pivot
	(
		max(exist) for workday_dayName in ([Senin], [Selasa],[Rabu], [Kamis], [Jumat], [Sabtu], [Minggu])
	) as pivotTable
	order by workday_year
END


GO
/****** Object:  StoredProcedure [dbo].[usp_CaptureHistoryTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*
Created By : Angga
Date Created : 29 Juli 2020
desc : untuk capture ke history tunjangan pegawai
*/
CREATE PROC [dbo].[usp_CaptureHistoryTunjangan]
@NIP VARCHAR(20),
@LoginNip VARCHAR(20)

AS
BEGIN
	
	--DECLARE @NIP VARCHAR(20) = 'H02932'
	--DECLARE @LoginNip VARCHAR(20) = 'H02932'
	DECLARE @Remark VARCHAR(500) = 'CaptureHistoryTunjangan'

	INSERT INTO dbo.HIST_PAY_TUNJANGAN_PEGAWAI
        ( TJP_ID ,
          NIP ,
          KPG_ID ,
          TUNJANGAN ,
          TMT ,
          TMT_END ,
          KETERANGAN ,
          KETERANGAN_2 ,
          REMARK ,
          CREATED_BY ,
          CREATED_DATE ,
          STSRC
        )
	SELECT 
			TJP_ID,
			NIP, 
			KPG_ID, -- KPG_ID
			TUNJANGAN, -- TUNJANGAN di set 1 untuk aktif
			TMT, -- TMT diambil dari TMT_END Plafon RUmah + 1 hari
			TMT_END,
			KETERANGAN, 
			KETERANGAN_2,
			@REMARK,
			@LoginNip,
			GETDATE(),
			'A'
	FROM dbo.PAY_TUNJANGAN_PEGAWAI
	WHERE nip = @NIP

END
GO
/****** Object:  StoredProcedure [dbo].[usp_cekLepasCuti]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*
Created By : Angga
Date Created : 03 September 2020
desc : untuk cek SPG_CUTI_DILEPAS dan insert ke SPG_REKAP_CUTI
*/

CREATE PROC [dbo].[usp_cekLepasCuti]
AS
BEGIN
	
	--SELECT * FROM dbo.SPG_CUTI_DILEPAS a
	--JOIN dbo.SPG_REKAP_CUTI b ON b.NIP = a.NIP AND b.JNS_CUTI = a.JNS_CUTI 
	--AND b.TANGGAL_BERLAKU = a.TANGGAL_BERLAKU AND b.TANGGAL_BERAKHIR = a.TANGGAL_BERAKHIR
	--WHERE a.STSRC = 'A' AND a.STS_GENERATE <> 1

	UPDATE SPG_REKAP_CUTI
	SET HAK_CUTI=b.HAK_CUTI_DIJALANKAN
	FROM SPG_REKAP_CUTI a
	JOIN SPG_CUTI_DILEPAS b on a.NIP=b.NIP AND a.JNS_CUTI=b.JNS_CUTI AND a.TANGGAL_BERLAKU=b.TANGGAL_BERLAKU AND
	a.TANGGAL_BERAKHIR=b.TANGGAL_BERAKHIR AND COALESCE(STS_GENERATE, '0')<>1

	UPDATE SPG_CUTI_DILEPAS
	SET STS_GENERATE='1'
	FROM SPG_REKAP_CUTI a
	JOIN SPG_CUTI_DILEPAS b on a.NIP=b.NIP AND a.JNS_CUTI=b.JNS_CUTI AND a.TANGGAL_BERLAKU=b.TANGGAL_BERLAKU AND
	a.TANGGAL_BERAKHIR=b.TANGGAL_BERAKHIR AND COALESCE(STS_GENERATE, '0')<>1 AND a.HAK_CUTI=b.HAK_CUTI_DIJALANKAN

END
GO
/****** Object:  StoredProcedure [dbo].[usp_CleansingSisaCuti]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Lius
-- Create date: 15 Juli 2021
-- Description:	Untuk Cleansing Cuti
-- =============================================
CREATE PROCEDURE [dbo].[usp_CleansingSisaCuti] 
@NIP VARCHAR(32)
AS
BEGIN
SET NOCOUNT ON

select* into #temp_table_migrasi_rekap_cuti from (
select b.Urutan_NIP, a.NIP,
	ROW_NUMBER() OVER(PARTITION BY a.NIP ORDER BY a.NIP ASC, a.TANGGAL_BERLAKU ASC, a.ID ASC) as Urutan, b.TGL_MASUK, a.sts_cut_off,
	case when coalesce(a.sts_cut_off, 0)=0 then 'Timbul Hak Cuti' else 'Cut Off' end as Keterangan,
	JNS_CUTI='1', a.TANGGAL_BERLAKU, a.TANGGAL_BERAKHIR, SISA_CUTI=0, HAK_CUTI=coalesce(a.HAK_CUTI, 0),
	TOTAL_CUTI=0, HUTANG_CUTI=0
from SPG_REKAP_CUTI a
inner join (select ROW_NUMBER() OVER(ORDER BY NIP ASC) as Urutan_NIP, NIP, TGL_MASUK from SPG_DATA_CURRENT) b
	on a.NIP=b.NIP
where a.JNS_CUTI='1' and a.NIP=@NIP
)a


DECLARE @Urutan_NIP_Value INT, @Max_Nip INT, @Urutan_Value INT, @Max_Urutan INT
SET @Urutan_NIP_Value = 1
SET @Max_Nip = (select coalesce(max(Urutan_NIP), 0) as Max_Nip from #temp_table_migrasi_rekap_cuti)

WHILE @Urutan_NIP_Value <= @Max_Nip
BEGIN
	SET @Urutan_Value = 1
	SET @Max_Urutan = (select coalesce(max(Urutan), 0) as Max_Urutan from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value)

	WHILE @Urutan_Value <= @Max_Urutan
	BEGIN

		update #temp_table_migrasi_rekap_cuti
		set TOTAL_CUTI=coalesce(b.TOTAL_CUTI, 0), HUTANG_CUTI=coalesce(c.TOTAL_CUTI, 0)
		from #temp_table_migrasi_rekap_cuti a
		left join (select NIP, JNS_CUTI, TOTAL_CUTI=coalesce(sum(JUMLAH_CUTI), 0) from SPG_CUTI_PEGAWAI
		where NIP=(select NIP from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value)
			and JNS_CUTI=(select JNS_CUTI from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value)
			and TGL_AWAL_CUTI_PEG between
			(select TANGGAL_BERLAKU from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value) and
			coalesce((select DATEADD(day, -1, TANGGAL_BERLAKU) as TANGGAL_BERLAKU from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value+1),
			(select TANGGAL_BERAKHIR from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value))
			and isnull(tidak_akumulasi,0) <> 1
			group by NIP, JNS_CUTI) b on a.NIP=b.NIP and a.JNS_CUTI=b.JNS_CUTI
		left join (select NIP, JNS_CUTI, TOTAL_CUTI=coalesce(sum(JUMLAH_CUTI), 0) from SPG_CUTI_PEGAWAI
		where NIP=(select NIP from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value)
			and JNS_CUTI=(select JNS_CUTI from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value)
			and TGL_AWAL_CUTI_PEG <
			(select TANGGAL_BERLAKU from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value)
			and isnull(tidak_akumulasi,0) <> 1
			group by NIP, JNS_CUTI) c on a.NIP=c.NIP and a.JNS_CUTI=c.JNS_CUTI and Urutan='1'
		where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value

		update #temp_table_migrasi_rekap_cuti
		set SISA_CUTI = coalesce(case when HAK_CUTI + case when Keterangan='Cut Off' then 0 else
		coalesce((select SISA_CUTI from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value-1), 0) end
		> 24 then 24 else HAK_CUTI + case when Keterangan='Cut Off' then 0 else
		coalesce((select SISA_CUTI from #temp_table_migrasi_rekap_cuti where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value-1), 0) end end
		 - TOTAL_CUTI - case when Keterangan='Cut Off' then 0 else HUTANG_CUTI end, 0)
		where Urutan_NIP=@Urutan_NIP_Value and Urutan=@Urutan_Value and
			(TANGGAL_BERAKHIR<getdate() or (TANGGAL_BERAKHIR is null and Urutan<>@Max_Urutan))

		SET @Urutan_Value = @Urutan_Value + 1
	END

	SET @Urutan_NIP_Value = @Urutan_NIP_Value + 1
END

--select* from #temp_table_migrasi_rekap_cuti

update SPG_REKAP_CUTI
set SISA_CUTI=b.SISA_CUTI
from SPG_REKAP_CUTI a
inner join #temp_table_migrasi_rekap_cuti b on a.NIP=b.NIP and a.JNS_CUTI=b.JNS_CUTI and
	a.TANGGAL_BERLAKU=b.TANGGAL_BERLAKU and (a.TANGGAL_BERAKHIR=b.TANGGAL_BERAKHIR or a.sts_cut_off=b.sts_cut_off)


drop table #temp_table_migrasi_rekap_cuti

END

GO
/****** Object:  StoredProcedure [dbo].[usp_deleteCutiGantung]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 25 Agustus 2020
-- Description:	Untuk akses User
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_deleteCutiGantung]
@KasusNomor VARCHAR(18)
AS
    BEGIN
	
	DELETE dbo.WF_OPKASUS WHERE KSO_KASUSNOMOR =@KasusNomor
	DELETE dbo.WF_OPFORMULIRAKTIFITASLOG  WHERE KSO_KASUSNOMOR =@KasusNomor
	DELETE dbo.WF_OPKASUSPERUBAHANSTATUS  WHERE KSO_KASUSNOMOR =@KasusNomor
	DELETE dbo.wf_opkasusattribute  WHERE KSO_KASUSNOMOR =@KasusNomor
	
	END	

GO
/****** Object:  StoredProcedure [dbo].[usp_deleteRumahSakit]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_deleteRumahSakit] 
AS
    BEGIN
	
	INSERT INTO dbo.History_SPG_RUMAH_SAKIT
	        ( HPT_ID ,
	          INDUK_GUID ,
	          NAMA_INDUK ,
	          GUID ,
	          NAMA ,
	          ADDMEDIKA_KODE ,
	          JENIS ,
	          ALAMAT ,
	          PROVINSI ,
	          REGIONAL ,
	          KOTA ,
	          TELPON ,
	          STATUS ,
	          FASILITAS ,
	          PAKET ,
	          AKTIF ,
	          KETERANGAN ,
	          TGL_MULAI ,
	          TGL_SELESAI ,
	          TGL_BUAT ,
	          NAMA_BUAT ,
	          TGL_UBAH ,
	          NAMA_UBAH
	        )
	SELECT  HPT_ID,INDUK_GUID,NAMA_INDUK,GUID,NAMA,ADDMEDIKA_KODE,JENIS,ALAMAT,PROVINSI,REGIONAL,KOTA,TELPON,STATUS,FASILITAS,PAKET,AKTIF,KETERANGAN,TGL_MULAI,TGL_SELESAI,TGL_BUAT,NAMA_BUAT,TGL_UBAH,NAMA_UBAH from dbo.SPG_RUMAH_SAKIT

	DELETE dbo.SPG_RUMAH_SAKIT

    END


GO
/****** Object:  StoredProcedure [dbo].[usp_drag_drop_struktur_organisasi]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_drag_drop_struktur_organisasi] --'102009002001000000000000', '102009001000000000000000'
@kd_unit_org_dipindah varchar(50),
@kd_unit_org_lokasi_pindah varchar(50)
AS
BEGIN

declare @query varchar(max)
declare @level_tangga int

set @level_tangga = 1

select* into #temp_table_perubahan_struktur from(
	select urutan_insert=1, level_tangga=@level_tangga, tingkat_eselon=b.ID, urutan=1, KD_UNIT_ORG_BARU=KD_UNIT_ORG,
	KD_UNIT_ORG_INDUK_BARU=@kd_unit_org_lokasi_pindah,
	KD_UNIT_ES1_BARU=KD_UNIT_ORG, KD_UNIT_ES2_BARU=KD_UNIT_ORG, KD_UNIT_ES3_BARU=KD_UNIT_ORG, KD_UNIT_ES4_BARU=KD_UNIT_ORG,
	KD_UNIT_ES5_BARU=KD_UNIT_ORG, KD_UNIT_ES6_BARU=KD_UNIT_ORG, KD_UNIT_ES7_BARU=KD_UNIT_ORG, KD_UNIT_ES8_BARU=KD_UNIT_ORG,
	kode_satker_organisasi_baru=KD_UNIT_ORG, nama_satker_organisasi_baru=nama_satker_organisasi, a.*
	from SPG_08_UNIT_ORGANISASI a
	inner join SPG_ESELON b on a.KD_ESELON=b.KD_ESELON
	where (a.TGL_END='1900-01-01' or a.TGL_END is null) and a.KD_UNIT_ORG=@kd_unit_org_dipindah)a

update #temp_table_perubahan_struktur
set KD_UNIT_ORG_BARU='', KD_UNIT_ES1_BARU='', KD_UNIT_ES2_BARU='', KD_UNIT_ES3_BARU='', KD_UNIT_ES4_BARU='', KD_UNIT_ES5_BARU='',
	KD_UNIT_ES6_BARU='', KD_UNIT_ES7_BARU='', KD_UNIT_ES8_BARU='', kode_satker_organisasi_baru='', nama_satker_organisasi_baru=''

while exists (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where (TGL_END='1900-01-01' or TGL_END is null) and
		KD_UNIT_ORG_INDUK in (select KD_UNIT_ORG from #temp_table_perubahan_struktur where level_tangga=@level_tangga))
begin
	insert into #temp_table_perubahan_struktur
	select urutan_insert=0, level_tangga=@level_tangga+1, tingkat_eselon=b.ID, urutan=0, KD_UNIT_ORG_BARU='',
	KD_UNIT_ORG_INDUK_BARU='', KD_UNIT_ES1_BARU='', KD_UNIT_ES2_BARU='', KD_UNIT_ES3_BARU='', KD_UNIT_ES4_BARU='',
	KD_UNIT_ES5_BARU='', KD_UNIT_ES6_BARU='', KD_UNIT_ES7_BARU='', KD_UNIT_ES8_BARU='', kode_satker_organisasi_baru='',
	nama_satker_organisasi_baru='', a.*
	from SPG_08_UNIT_ORGANISASI a
	inner join SPG_ESELON b on a.KD_ESELON=b.KD_ESELON
	where (a.TGL_END='1900-01-01' or a.TGL_END is null) and
		a.KD_UNIT_ORG_INDUK in (select KD_UNIT_ORG from #temp_table_perubahan_struktur where level_tangga=@level_tangga)

	set @level_tangga = @level_tangga+1
end

update #temp_table_perubahan_struktur
set urutan=b.update_urutan, urutan_insert=b.update_urutan_insert
from #temp_table_perubahan_struktur a
inner join (select update_urutan_insert=row_number() over(order by level_tangga asc, tingkat_eselon asc, urutan asc),
		update_urutan=row_number() over(partition by level_tangga, tingkat_eselon order by id asc), *
		from #temp_table_perubahan_struktur) b on a.id=b.id

declare @max_tangga int
declare @max_tingkat int
declare @tingkatan int

set @level_tangga = 1
set @max_tangga = (select max(level_tangga) from #temp_table_perubahan_struktur)
set @max_tingkat = (select max(ID) from SPG_ESELON)

while @level_tangga<=@max_tangga
begin
	if @level_tangga=1
	begin
		declare @tingkat_eselon_dipindah int
		declare @tingkat_eselon_lokasi_pindah int
		declare @jumlah_tingkat_diskip int
		declare @jumlah_tingkat_sisa int
		declare @kd_unit_org_baru varchar(50)

		set @tingkat_eselon_dipindah = (select tingkat_eselon from #temp_table_perubahan_struktur
											where level_tangga=@level_tangga)
		set @tingkat_eselon_lokasi_pindah = (select tingkat_eselon=b.ID from SPG_08_UNIT_ORGANISASI a
												inner join SPG_ESELON b on a.KD_ESELON=b.KD_ESELON
												where KD_UNIT_ORG=@kd_unit_org_lokasi_pindah)
		if @tingkat_eselon_dipindah>=@tingkat_eselon_lokasi_pindah
		set @jumlah_tingkat_diskip = @tingkat_eselon_dipindah - @tingkat_eselon_lokasi_pindah - 1
		set @jumlah_tingkat_sisa = @max_tingkat - @tingkat_eselon_dipindah

		set @kd_unit_org_baru = substring(@kd_unit_org_lokasi_pindah, 1, 3*@tingkat_eselon_lokasi_pindah)

		set @tingkatan=1
		while @tingkatan<=@jumlah_tingkat_diskip
		begin
			set @kd_unit_org_baru = @kd_unit_org_baru + '000'
			set @tingkatan = @tingkatan+1
		end

		set @kd_unit_org_baru = @kd_unit_org_baru + (select coalesce(right('000'+cast(cast(max(SUBSTRING(KD_UNIT_ORG, (@tingkat_eselon_dipindah-1)*3+1, 3)) as int)+1 as varchar),3),'001')
														from SPG_08_UNIT_ORGANISASI
														where KD_UNIT_ORG_INDUK=@kd_unit_org_lokasi_pindah)

		set @tingkatan=1
		while @tingkatan<=@jumlah_tingkat_sisa
		begin
			set @kd_unit_org_baru = @kd_unit_org_baru + '000'
			set @tingkatan = @tingkatan+1
		end

		update #temp_table_perubahan_struktur
		set KD_UNIT_ORG_BARU=@kd_unit_org_baru
		where level_tangga=@level_tangga

		set @tingkatan=1
		while @tingkatan<=@tingkat_eselon_lokasi_pindah
		begin
			set @query = 'update #temp_table_perubahan_struktur
			set KD_UNIT_ES'+cast(@tingkatan as varchar)+'_BARU=b.KD_UNIT_ES'+cast(@tingkatan as varchar)+'
			from #temp_table_perubahan_struktur a
			inner join SPG_08_UNIT_ORGANISASI b on a.KD_UNIT_ORG_INDUK_BARU=b.KD_UNIT_ORG
			where level_tangga='+cast(@level_tangga as varchar)
			exec (@query)

			set @tingkatan = @tingkatan+1
		end

		set @query = 'update #temp_table_perubahan_struktur
		set KD_UNIT_ES'+cast(@tingkat_eselon_dipindah as varchar)+'_BARU='+@kd_unit_org_baru+'
		from #temp_table_perubahan_struktur a
		inner join SPG_08_UNIT_ORGANISASI b on a.KD_UNIT_ORG_INDUK_BARU=b.KD_UNIT_ORG
		where level_tangga='+cast(@level_tangga as varchar)
		exec (@query)

		if exists (select KD_UNIT_ORG_BARU from #temp_table_perubahan_struktur
					where level_tangga=@level_tangga and
					(tingkat_eselon in ('1', '2', '3') or KD_UNIT_ORG=kode_satker_organisasi))
		begin
			update #temp_table_perubahan_struktur
			set kode_satker_organisasi_baru=KD_UNIT_ORG_BARU, nama_satker_organisasi_baru=NM_UNIT_ORG
			where level_tangga=@level_tangga and (tingkat_eselon in ('1', '2', '3') or KD_UNIT_ORG=kode_satker_organisasi)
		end
		else
		begin
			update #temp_table_perubahan_struktur
			set kode_satker_organisasi_baru=b.kode_satker_organisasi, nama_satker_organisasi_baru=b.nama_satker_organisasi
			from #temp_table_perubahan_struktur a
			inner join SPG_08_UNIT_ORGANISASI b on a.KD_UNIT_ORG_INDUK_BARU=b.KD_UNIT_ORG
			where level_tangga=@level_tangga
		end
	end
	else
	begin
		update #temp_table_perubahan_struktur
		set KD_UNIT_ORG_BARU=substring(b.KD_UNIT_ORG_BARU, 1, 3*b.tingkat_eselon) +
			dbo.[GenerateKode_Skip_Sisa](a.tingkat_eselon-b.tingkat_eselon-1) +
			right('000' + cast(a.urutan as varchar),3) +
			dbo.[GenerateKode_Skip_Sisa](@max_tingkat-a.tingkat_eselon),
			KD_UNIT_ORG_INDUK_BARU=b.KD_UNIT_ORG_BARU,
			kode_satker_organisasi_baru= case when a.tingkat_eselon in ('1', '2', '3') or a.KD_UNIT_ORG=a.kode_satker_organisasi
											then substring(b.KD_UNIT_ORG_BARU, 1, 3*b.tingkat_eselon) +
													dbo.[GenerateKode_Skip_Sisa](a.tingkat_eselon-b.tingkat_eselon-1) +
													right('000' + cast(a.urutan as varchar),3) +
													dbo.[GenerateKode_Skip_Sisa](@max_tingkat-a.tingkat_eselon)
											else b.kode_satker_organisasi_baru end,
			nama_satker_organisasi_baru= case when a.tingkat_eselon in ('1', '2', '3') or a.KD_UNIT_ORG=a.kode_satker_organisasi
											then a.NM_UNIT_ORG
											else b.nama_satker_organisasi_baru end
		from #temp_table_perubahan_struktur a
		inner join (select KD_UNIT_ORG, KD_UNIT_ORG_BARU, tingkat_eselon, kode_satker_organisasi_baru, nama_satker_organisasi_baru
					from #temp_table_perubahan_struktur) b on a.KD_UNIT_ORG_INDUK=b.KD_UNIT_ORG
		where a.level_tangga=@level_tangga


		set @tingkatan=1
		while @tingkatan<=@max_tingkat
		begin
			set @query = 'update #temp_table_perubahan_struktur
			set KD_UNIT_ES'+cast(@tingkatan as varchar)+'_BARU= case when a.tingkat_eselon='+cast(@tingkatan as varchar)+
			'then a.KD_UNIT_ORG_BARU else b.KD_UNIT_ES'+cast(@tingkatan as varchar)+'_BARU end
			from #temp_table_perubahan_struktur a
			inner join (select* from #temp_table_perubahan_struktur) b on a.KD_UNIT_ORG_INDUK_BARU=b.KD_UNIT_ORG_BARU
			where a.level_tangga='+cast(@level_tangga as varchar)
			exec (@query)

			set @tingkatan = @tingkatan+1
		end
	end

	set @level_tangga = @level_tangga+1
end

--select* from #temp_table_perubahan_struktur

insert into SPG_08_UNIT_ORGANISASI (ID, TGL_AKTIF, TGL_END, KD_UNIT_ORG, KD_UNIT_ES1, KD_UNIT_ES2, KD_UNIT_ES3,
KD_UNIT_ES4, KD_UNIT_ES5, JNS_KANTOR, NM_UNIT_ORG, KD_ESELON, KD_SURAT_ORG, KD_UNIT_ORG_INDUK, CMP_ID, KD_UNIT_ES6,
KD_UNIT_ES7, KD_UNIT_ES8, SINGKATAN_UNIT_ORG, KD_UNIT_ORG_KHUSUS, NO_URUT, TIPE_PERUBAHAN, UNIT_SEBELUMNYA, KD_SATKER,
nama_satker_organisasi, kode_satker_organisasi)
select ID=(select max(id) from SPG_08_UNIT_ORGANISASI)+urutan_insert, TGL_AKTIF=getdate(), TGL_END=null,
	KD_UNIT_ORG=KD_UNIT_ORG_BARU, KD_UNIT_ES1=KD_UNIT_ES1_BARU, KD_UNIT_ES2=KD_UNIT_ES2_BARU, KD_UNIT_ES3=KD_UNIT_ES3_BARU,
	KD_UNIT_ES4=KD_UNIT_ES4_BARU, KD_UNIT_ES5=KD_UNIT_ES5_BARU, JNS_KANTOR, NM_UNIT_ORG, KD_ESELON, KD_SURAT_ORG='',
	KD_UNIT_ORG_INDUK=KD_UNIT_ORG_INDUK_BARU, CMP_ID, KD_UNIT_ES6=KD_UNIT_ES6_BARU, KD_UNIT_ES7=KD_UNIT_ES7_BARU,
	KD_UNIT_ES8=KD_UNIT_ES8_BARU, SINGKATAN_UNIT_ORG, KD_UNIT_ORG_KHUSUS, NO_URUT, TIPE_PERUBAHAN,
	UNIT_SEBELUMNYA=KD_UNIT_ORG, KD_SATKER, nama_satker_organisasi=nama_satker_organisasi_baru,
	kode_satker_organisasi=kode_satker_organisasi_baru
from #temp_table_perubahan_struktur

--update SPG_08_UNIT_ORGANISASI
--set TGL_END=dateadd(DD, -1, cast(getdate() as date))
--from SPG_08_UNIT_ORGANISASI a
--inner join #temp_table_perubahan_struktur b on a.KD_UNIT_ORG=b.KD_UNIT_ORG and a.ID=b.ID

drop table #temp_table_perubahan_struktur

END

GO
/****** Object:  StoredProcedure [dbo].[usp_duplikasi_formasi_pegawai]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[usp_duplikasi_formasi_pegawai]
	@kd_lama varchar(50),
	@kd_baru varchar(50),
	@start_date date,
	@no_sk varchar(255),
	@tgl_sk date,
	@nama_buat varchar(18)
AS
BEGIN
	SET NOCOUNT ON;
	--DECLARE @returnCode INT, @jumlah INT

	BEGIN TRANSACTION

	BEGIN TRY
		/*EXEC @returnCode = sp_getapplock 
				@Resource = 'usp_duplikasi_formasi_pegawai', 
				@LockMode = 'Exclusive', 
				@LockOwner = 'Session', 
				@LockTimeout = 50,
				@DbPrincipal  = 'public'
	
		WHILE @returnCode NOT IN (0, 1)
		BEGIN
			EXEC @returnCode = sp_getapplock @Resource='usp_duplikasi_formasi_pegawai', @LockMode='Exclusive',
					@LockOwner='Session', @LockTimeout=50, @DbPrincipal='public'
		END*/
    
		--SLEEP FOR 3 SECONDS
		--WAITFOR DELAY '00:00:03';

		--declare @kd_lama varchar(50) = '107000000000000000000000'
		--declare @kd_baru varchar(50) = '111000000000000000000000'
		--declare @start_date date = '2023-03-01'
		--declare @no_sk varchar(255) = '2/PDK.02/2023'
		--declare @tgl_sk date = '2023-03-01'
		--declare @nama_buat varchar(18) = 'SYSTEM'

		declare @counter int = 0,
		@newid int,
		@tanggal_buat date = GETDATE(),
		@tgl_mulai date = @start_date,	-- berlaku tanggal
		@tgl_efektif date = @start_date,
		@kd_status_validasi char(1) = '1',
		@kd_jns_mutasi int = 10
		-- 1 = Promosi
		-- 2 = Mutasi
		-- 3 = Demosi
		-- 5 = Pegawai Baru
		-- 6 = Promosi Mutasi
		-- 8 = Rotasi
		--10 = Reorganisasi
		--11 = Penetapan Kembali
		--12 = Orientasi
		--select @kd_lama kd_lama,@kd_baru kd_baru

		INSERT INTO [dbo].[Transaksi_Duplikasi]
					([process_start]
					,[status]
					,[started_by]
					,[kd_unit_org_old]
					,[kd_unit_org_new]
					,[no_sk]
					,[start_date])
				VALUES
					(
					GETDATE()
					,0
					,@nama_buat
					,@kd_lama
					,@kd_baru
					,@no_sk
					,@start_date)
		--status = 0:proses ; 1:selesai ;2:child tidak kosong, hapus dulu childnya
		--SELECT @newid = SCOPE_IDENTITY();


		select @counter = count(*) from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_baru
		if @counter=1
		BEGIN
			declare @_varnip varchar(50)
	
			/*declare cur CURSOR LOCAL for
				select nip from spg_data_current where left(kd_unit_org,3) = left(@kd_lama,3) and isnull(sts_pensiun,0) <> 1
	
				open cur
	
				fetch next from cur into @_varnip
	
				while @@FETCH_STATUS = 0 BEGIN
	
					--execute your sproc on each row
					exec dbo.update_data_current_nip_trg @_varnip
	
					fetch next from cur into @_varnip
				END
	
			close cur
			deallocate cur*/

			--select count(*) data_pegawai_lama from SPG_DATA_CURRENT where left(kd_unit_org,3) = left(@kd_lama,3) and isnull(sts_pensiun,0) <> 1  

			declare @tmt_jabatan date, @no_urut_jab int
			-----------section uorg----------------------------
			update SPG_08_UNIT_ORGANISASI 
			set nama_satker_organisasi = NM_UNIT_ORG, kode_satker_organisasi = kd_unit_org
			where kd_unit_org = @kd_baru 

			declare @maxid int = 0
			select @maxid = max(id) from SPG_08_UNIT_ORGANISASI
			insert into SPG_08_UNIT_ORGANISASI (
					[ID]
					,[TGL_AKTIF]
					,[TGL_END]
					,[KD_UNIT_ORG]
					,[KD_UNIT_ES1]
					,[KD_UNIT_ES2]
					,[KD_UNIT_ES3]
					,[KD_UNIT_ES4]
					,[KD_UNIT_ES5]
					,[JNS_KANTOR]
					,[NM_UNIT_ORG]
					,[KD_ESELON]
					,[KD_SURAT_ORG]
					,[KD_UNIT_ORG_INDUK]
					,[CMP_ID]
					,[KD_UNIT_ES6]
					,[KD_UNIT_ES7]
					,[KD_UNIT_ES8]
					,[SINGKATAN_UNIT_ORG]
					,[KD_UNIT_ORG_KHUSUS]
					,[NO_URUT]
					,[TIPE_PERUBAHAN]
					,[UNIT_SEBELUMNYA]
					,[KD_SATKER]
					,[nama_satker_organisasi]
					,[kode_satker_organisasi]
					,[NM_UNIT_ORG_ENG]
					,[nama_satker_organisasi_eng]
					,[IS_FUNCTIONAL])
			select
					ROW_NUMBER() OVER(ORDER BY ID ASC)+@maxid
					,@start_date
					,[TGL_END]
					,left(@kd_baru,3)+right([KD_UNIT_ORG],len([KD_UNIT_ORG])-3)
					,case when isnull([KD_UNIT_ES1],'') = '' then [KD_UNIT_ES1] else left(@kd_baru,3)+right([KD_UNIT_ES1],len([KD_UNIT_ES1])-3) end
					,case when isnull([KD_UNIT_ES2],'') = '' then [KD_UNIT_ES2] else left(@kd_baru,3)+right([KD_UNIT_ES2],len([KD_UNIT_ES2])-3) end
					,case when isnull([KD_UNIT_ES3],'') = '' then [KD_UNIT_ES3] else left(@kd_baru,3)+right([KD_UNIT_ES3],len([KD_UNIT_ES3])-3) end
					,case when isnull([KD_UNIT_ES4],'') = '' then [KD_UNIT_ES4] else left(@kd_baru,3)+right([KD_UNIT_ES4],len([KD_UNIT_ES4])-3) end
					,case when isnull([KD_UNIT_ES5],'') = '' then [KD_UNIT_ES5] else left(@kd_baru,3)+right([KD_UNIT_ES5],len([KD_UNIT_ES5])-3) end
					,[JNS_KANTOR]
					,[NM_UNIT_ORG]
					,[KD_ESELON]
					,[KD_SURAT_ORG]
					,case when isnull([KD_UNIT_ORG_INDUK],'') = '' then [KD_UNIT_ORG_INDUK] else left(@kd_baru,3)+right([KD_UNIT_ORG_INDUK],len([KD_UNIT_ORG_INDUK])-3) end
					,[CMP_ID]
					,case when isnull([KD_UNIT_ES6],'') = '' then [KD_UNIT_ES6] else left(@kd_baru,3)+right([KD_UNIT_ES6],len([KD_UNIT_ES6])-3) end
					,case when isnull([KD_UNIT_ES7],'') = '' then [KD_UNIT_ES7] else left(@kd_baru,3)+right([KD_UNIT_ES7],len([KD_UNIT_ES7])-3) end
					,case when isnull([KD_UNIT_ES8],'') = '' then [KD_UNIT_ES8] else left(@kd_baru,3)+right([KD_UNIT_ES8],len([KD_UNIT_ES8])-3) end
					,[SINGKATAN_UNIT_ORG]
					,[KD_UNIT_ORG_KHUSUS]
					,[NO_URUT]
					,[TIPE_PERUBAHAN]
					,[KD_UNIT_ORG]
					,[KD_SATKER]
					,[nama_satker_organisasi]
					,case when left(isnull([kode_satker_organisasi],''),3) != left(@kd_lama,3) then [kode_satker_organisasi] else left(@kd_baru,3)+right([kode_satker_organisasi],len([kode_satker_organisasi])-3) end 
					,[NM_UNIT_ORG_ENG]
					,[nama_satker_organisasi_eng]
					,[IS_FUNCTIONAL]
			FROM [dbo].[SPG_08_UNIT_ORGANISASI] where kd_unit_org != @kd_lama and KD_UNIT_ES1  = @kd_lama and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01') order by kd_unit_org;
			--compare
			--select count(*) SPG_08_UNIT_ORGANISASI_LAMA from SPG_08_UNIT_ORGANISASI where  KD_UNIT_ES1  = @kd_lama and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01');
			--select count(*) SPG_08_UNIT_ORGANISASI_BARU from SPG_08_UNIT_ORGANISASI where  KD_UNIT_ES1  = @kd_baru and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01');
			-----------end section uorg----------------------------

			-----------section jabatan_unit----------------------------
			set @maxid = 0
			select @maxid = max(id) from spg_08_jabatan_unit

			insert into spg_08_jabatan_unit (
			[ID]
			,[KD_JABATAN]
			,[KD_UNIT_ORG]
			,[POSISI]
			,[POSISI_SINGKAT]
			,[GAJI_PASARAN]
			,[GAJI_MAKSIMUM]
			,[URAIAN_PEKERJAAN]
			,[KEBUTUHAN_MANAJERIAL]
			,[KEBUTUHAN_TEKNIS]
			,[CCE_ID]
			,[CMP_ID]
			,[KD_KANTOR]
			,[KD_JOBTITLE]
			,[KATEGORI_FUNGSI]
			,[KD_SURAT_POSISI]
			,[STS_AKTIF]
			,[POSISI_SINGKAT_ENG]
			,[POSISI_ENG]
			,[JumlahFE]
			)
			select 
			ROW_NUMBER() OVER(ORDER BY ID ASC)+@maxid
			,[KD_JABATAN]
			,left(@kd_baru,3)+right([KD_UNIT_ORG],len([KD_UNIT_ORG])-3)
			,[POSISI]
			,[POSISI_SINGKAT]
			,[GAJI_PASARAN]
			,[GAJI_MAKSIMUM]
			,[URAIAN_PEKERJAAN]
			,[KEBUTUHAN_MANAJERIAL]
			,[KEBUTUHAN_TEKNIS]
			,[CCE_ID]
			,[CMP_ID]
			,[KD_KANTOR]
			,[KD_JOBTITLE]
			,[KATEGORI_FUNGSI]
			,[KD_SURAT_POSISI]
			,[STS_AKTIF]
			,[POSISI_SINGKAT_ENG]
			,[POSISI_ENG]
			,[JumlahFE]
			from spg_08_jabatan_unit where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_lama  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01'))

			--compare
			--select count(*) spg_08_jabatan_unit_lama from spg_08_jabatan_unit where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_lama  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01'))
			--select count(*) spg_08_jabatan_unit_baru from spg_08_jabatan_unit where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_baru  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01'))
			-----------end section jabatan_unit----------------------------

			-----------section hirartki_organisasi----------------------------
			insert into SPG_hirarki_organisasi (
				[KD_UNIT_ORG]
					,[KD_KANTOR]
					,[KD_JABATAN_STR]
					,[KD_UNIT_ORG_INDUK]
					,[KD_KANTOR_INDUK]
					,[KD_JABATAN_STR_INDUK]
					,[NAMA_BUAT]
					,[TANGGAL_BUAT]
					,[NAMA_UBAH]
					,[TANGGAL_UBAH]
					,[STS_AKTIF]
			)
			select
					left(@kd_baru,3)+right([KD_UNIT_ORG],len([KD_UNIT_ORG])-3)
					,[KD_KANTOR]
					,[KD_JABATAN_STR]
					,case when isnull([KD_UNIT_ORG_INDUK],'') = '' then [KD_UNIT_ORG_INDUK] else left(@kd_baru,3)+right([KD_UNIT_ORG_INDUK],len([KD_UNIT_ORG_INDUK])-3) end
					,[KD_KANTOR_INDUK]
					,[KD_JABATAN_STR_INDUK]
					,@nama_buat
					,@tanggal_buat
					,NULL
					,NULL
					,1
			from [SPG_HIRARKI_ORGANISASI] where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_lama  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01')) and sts_aktif = 1

			--compare
			--select count(*) [SPG_HIRARKI_ORGANISASI_LAMA] from [SPG_HIRARKI_ORGANISASI] where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_lama  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01')) and sts_aktif = 1
			--select count(*) [SPG_HIRARKI_ORGANISASI_BARU] from [SPG_HIRARKI_ORGANISASI] where kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_baru  and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01')) and sts_aktif = 1
			-----------end section hirartki_organisasi----------------------------


			--Update data riwayat
			update [dbo].[SPG_08_UNIT_ORGANISASI] 
			set --TGL_END = case when TGL_AKTIF < @start_date THEN DATEADD(DAY,-1,@start_date) else @start_date end,
			kode_satker_organisasi = left(@kd_baru,3)+right([kode_satker_organisasi],len([kode_satker_organisasi])-3) 
			where  KD_UNIT_ES1  = @kd_lama and tgl_aktif <= getDATE() and (isnull(tgl_end,'2999-12-31') >= GETDATE() or TGL_END = '1900-01-01');
	
			update [dbo].[SPG_HIRARKI_ORGANISASI] 
			set [TANGGAL_UBAH] = @start_date , NAMA_UBAH = @nama_buat, sts_aktif = 0
			where  kd_unit_org in (select KD_UNIT_ORG from SPG_08_UNIT_ORGANISASI where KD_UNIT_ES1 = @kd_lama) and sts_aktif = 1

			
			-------------------Query Baru/Tidak Terdampak------------------------------
			DECLARE @kd_bidang_baru varchar(3) = left(@kd_baru,3)
			DECLARE @nip varchar(8)

			DECLARE list_cursor CURSOR FAST_FORWARD FOR
			SELECT NIP FROM SPG_DATA_CURRENT WHERE LEFT(KD_UNIT_ORG, 3) = left(@kd_lama,3) AND STS_PENSIUN = 0 AND NIP NOT IN (SELECT NIP FROM SPG_PMK WHERE LEFT(KD_UNIT_ORG_BARU, 3) = left(@kd_lama,3) AND TGL_MULAI >= @start_date AND KD_STATUS_VALIDASI = '1') 

			OPEN list_cursor  
			FETCH NEXT FROM list_cursor INTO @nip  

			WHILE @@FETCH_STATUS = 0  
			BEGIN  
				--print @nip
				---- insert PMK --
				DECLARE 
					@pmk_id_new VARCHAR(10),
					@prefix VARCHAR(3),
					@max_num BIGINT,
					@id_pmk BIGINT
				SELECT @prefix = COALESCE(MAX(LEFT(PMK_ID, 3)), 'PMK') FROM SPG_PMK WHERE PMK_ID NOT LIKE 'TMP-%'
					SELECT @max_num = COALESCE(CAST(RIGHT(MAX(PMK_ID), 6) AS BIGINT), 0) FROM SPG_PMK WHERE LEFT(PMK_ID, 4) = @prefix + '-' AND PMK_ID NOT LIKE 'TMP-%'
					SELECT @pmk_id_new = dbo.func_generate_nomorkolom(@prefix, @max_num)
	 				INSERT INTO [dbo].[SPG_PMK]
							([PMK_ID]
							,[NM_PEG]
							,[NIP]
							,[TGL_MASUK]
							,[CMP_ID]
							,[CMP_ID_BARU]
							,[KD_KANTOR]
							,[KD_KANTOR_BARU]
							,[KD_ESELON]
							,[KD_ESELON_BARU]
							,[KD_UNIT_ORG]
							,[KD_UNIT_ORG_BARU]
							,[NM_UNIT_ORG]
							,[KD_JABATAN_STR]
							,[KD_JABATAN_STR_BARU]
							,[KD_GOL]
							,[KD_GOL_BARU]
							,[KD_PANGKAT]
							,[KD_PANGKAT_BARU]
							,[NM_GOLONGAN]
							,[NM_GOLONGAN_BARU]
							,[KD_STATUS_KEPEG]
							,[KD_STATUS_KEPEG_BARU]
							,[TGL_MULAI]
							,[KETERANGAN]
							,[KSO_KASUSNOMOR]
							,[TANGGAL_BUAT]
							,[NAMA_BUAT]
							,[KD_JNS_MUTASI]
							,[NO_SK]
							,[TGL_SK]
							,[KD_STATUS_VALIDASI]
							,[KD_SKALA_GAJI_BARU]
							,[TGL_EFEKTIF]
							,[FlagNewChange])
					SELECT
							@pmk_id_new	--<PMK_ID, char(10),>
							,sdc.NM_PEG	--<NM_PEG, varchar(255),>
							,sdc.NIP	--<NIP, varchar(18),>
							,sdc.TGL_MASUK	--<TGL_MASUK, date,>
							,sdc.CMP_ID	--<CMP_ID, varchar(10),>
							,sdc.CMP_ID	--<CMP_ID_BARU, varchar(10),>
							,sdc.KD_KANTOR	--<KD_KANTOR, varchar(32),>
							,sdc.KD_KANTOR	--<KD_KANTOR_BARU, varchar(32),>
							,sdc.KD_ESELON	--<KD_ESELON, varchar(2),>
							,sdc.KD_ESELON	--<KD_ESELON_BARU, varchar(2),>
							,sdc.KD_UNIT_ORG	--<KD_UNIT_ORG, varchar(32),>
							,@kd_bidang_baru + RIGHT(sdc.KD_UNIT_ORG, 21)	--<KD_UNIT_ORG_BARU, varchar(32),>
							,suo.NM_UNIT_ORG	--<NM_UNIT_ORG, varchar(255),>
							,sdc.KD_JABATAN_STR	--<KD_JABATAN_STR, varchar(16),>
							,sdc.KD_JABATAN_STR	--<KD_JABATAN_STR_BARU, varchar(16),>
							,sdc.KD_GOL	--<KD_GOL, varchar(2),>
							,sdc.KD_GOL	--<KD_GOL_BARU, varchar(2),>
							,sdc.KD_PANGKAT	--<KD_PANGKAT, varchar(2),>
							,sdc.KD_PANGKAT	--<KD_PANGKAT_BARU, varchar(2),>
							,sdc.NM_GOL	--<NM_GOLONGAN, varchar(20),>
							,sdc.NM_GOL	--<NM_GOLONGAN_BARU, varchar(20),>
							,sdc.KD_STATUS_KEPEG	--<KD_STATUS_KEPEG, varchar(8),>
							,sdc.KD_STATUS_KEPEG	--<KD_STATUS_KEPEG_BARU, varchar(8),>
							,@tgl_mulai	--<TGL_MULAI, date,>
							,''	--<KETERANGAN, varchar(255),>
							,@pmk_id_new	--<KSO_KASUSNOMOR, varchar(10),>
							,@tanggal_buat	--<TANGGAL_BUAT, date,>
							,@nama_buat	--<NAMA_BUAT, varchar(18),>
							,@kd_jns_mutasi	--<KD_JNS_MUTASI, int,>
							,@no_sk	--<NO_SK, varchar(255),>
							,@tgl_sk	--<TGL_SK, date,>
							,@kd_status_validasi	--<KD_STATUS_VALIDASI, char(1),>
							,sdc.KD_SKALA_GAJI	--<KD_SKALA_GAJI_BARU, varchar(5),>
							,@tgl_efektif	--<TGL_EFEKTIF, date,>
							,'Y'	--<FlagNewChange, char(1),>
					FROM  
						SPG_DATA_CURRENT AS sdc INNER JOIN
						SPG_08_UNIT_ORGANISASI AS suo ON sdc.KD_UNIT_ORG = suo.KD_UNIT_ORG
					WHERE 
						sdc.NIP = @nip


				IF @kd_status_validasi = '1'
				BEGIN

					DECLARE @max_id bigint
					set @max_id = 0

					if not exists(select * from vw_PLTPLH where nip = @nip and status = 'berjalan')
					begin		
						SELECT @max_id = MAX(ID) FROM SPG_08_RIWAYAT_JABATAN

						---- insert Riwayat Jabatan --
						INSERT INTO [dbo].[SPG_08_RIWAYAT_JABATAN]
									([ID]
									,[NIP]
									,[NO_SK]
									,[DIKELUARKAN_OLEH]
									,[TMT_JAB_PEG]
									,[KD_KANTOR]
									,[JNS_JABATAN]
									,[KD_JABATAN_STR]
									,[KD_UNIT_ORG]
									,[KD_PANGKAT]
									,[KD_ESELON]
									,[STATUS_JABATAN]
									,[KD_STATUS_VALIDASI]
									,[TMT_PENEMPATAN]
									,[NM_JABATAN]
									,[TANGGAL_BUAT]
									,[NAMA_BUAT]
									,[CMP_ID]
									,[KSO_KASUSNOMOR]
									,[STS_RIWAYAT_JABATAN]
									,[TMT_SATKER]
									,[NO_URUT_JAB]
									,[TANGGAL_RIWAYAT]
									--,[TMT_REOG]
									)
							SELECT TOP 1
									ROW_NUMBER() OVER(ORDER BY ID DESC) + @max_id	--<ID, bigint,>
									,@nip	--<NIP, varchar(18),>
									,@no_sk	--<NO_SK, varchar(50),>
									,'1'	--<DIKELUARKAN_OLEH, varchar(50),>
									,TMT_JAB_PEG	--<TMT_JAB_PEG, date,>
									,KD_KANTOR	--<KD_KANTOR, varchar(20),>
									,JNS_JABATAN	--<JNS_JABATAN, char(1),>
									,KD_JABATAN_STR	--<KD_JABATAN_STR, varchar(5),>
									,@kd_bidang_baru + RIGHT(KD_UNIT_ORG, 21)	--<KD_UNIT_ORG, varchar(30),>
									,KD_PANGKAT	--<KD_PANGKAT, varchar(15),>
									,KD_ESELON	--<KD_ESELON, char(2),>
									,STATUS_JABATAN	--<STATUS_JABATAN, varchar(5),>
									,1	--<KD_STATUS_VALIDASI, smallint,>
									,TMT_PENEMPATAN	--<TMT_PENEMPATAN, date,>
									,NM_JABATAN	--<NM_JABATAN, varchar(255),>
									,@tanggal_buat	--<TANGGAL_BUAT, date,>
									,@nama_buat	--<NAMA_BUAT, varchar(18),>
									,CMP_ID	--<CMP_ID, varchar(10),>
									,@pmk_id_new	--<KSO_KASUSNOMOR, varchar(10),>
									,CASE @kd_jns_mutasi
									WHEN '1' THEN '1'
									WHEN '2' THEN '3'
									WHEN '3' THEN '9'
									WHEN '5' THEN '6'
									WHEN '6' THEN '2'
									WHEN '8' THEN '4'
									WHEN '10' THEN '8'
									WHEN '11' THEN '7'
									WHEN '12' THEN '10'
									END	--<STS_RIWAYAT_JABATAN, varchar(20),>
									,TMT_SATKER	--<TMT_SATKER, date,>
									,NO_URUT_JAB + 1	--<NO_URUT_JAB, int,>
									,@tgl_mulai	--<TANGGAL_RIWAYAT, date,>
									--,@tgl_mulai
							FROM
								SPG_08_RIWAYAT_JABATAN
							WHERE 
								NIP = @nip 
								AND KD_STATUS_VALIDASI = '1' 
								AND TMT_JAB_PEG <= GETDATE() 
							ORDER BY 
								TMT_JAB_PEG DESC, 
								NO_URUT_JAB DESC


							INSERT INTO [dbo].[SPG_SK]
									([NO_SK]
									,[JNS_SK]
									,[TGL_SK]
									,[NIP])
							VALUES
									(@no_sk
									,'01'
									,@tgl_sk
									,@nip)
					end 
					else 
					begin
						SELECT @max_id = MAX(ID) FROM SPG_08_RIWAYAT_JABATAN

						---- insert Riwayat Jabatan --
						INSERT INTO [dbo].[SPG_08_RIWAYAT_JABATAN]
									([ID]
									,[NIP]
									,[NO_SK]
									,[DIKELUARKAN_OLEH]
									,[TMT_JAB_PEG]
									,[KD_KANTOR]
									,[JNS_JABATAN]
									,[KD_JABATAN_STR]
									,[KD_UNIT_ORG]
									,[KD_PANGKAT]
									,[KD_ESELON]
									,[STATUS_JABATAN]
									,[KD_STATUS_VALIDASI]
									,[TMT_PENEMPATAN]
									,[NM_JABATAN]
									,[TANGGAL_BUAT]
									,[NAMA_BUAT]
									,[CMP_ID]
									,[KSO_KASUSNOMOR]
									,[STS_RIWAYAT_JABATAN]
									,[TMT_SATKER]
									,[NO_URUT_JAB]
									,[TANGGAL_RIWAYAT]
									,[TMT_REOG]
									)
							SELECT TOP 2
									ROW_NUMBER() OVER(ORDER BY ID DESC) + @max_id	--<ID, bigint,>
									,@nip	--<NIP, varchar(18),>
									,@no_sk	--<NO_SK, varchar(50),>
									,'1'	--<DIKELUARKAN_OLEH, varchar(50),>
									,TMT_JAB_PEG	--<TMT_JAB_PEG, date,>
									,KD_KANTOR	--<KD_KANTOR, varchar(20),>
									,JNS_JABATAN	--<JNS_JABATAN, char(1),>
									,KD_JABATAN_STR	--<KD_JABATAN_STR, varchar(5),>
									,@kd_bidang_baru + RIGHT(KD_UNIT_ORG, 21)	--<KD_UNIT_ORG, varchar(30),>
									,KD_PANGKAT	--<KD_PANGKAT, varchar(15),>
									,KD_ESELON	--<KD_ESELON, char(2),>
									,STATUS_JABATAN	--<STATUS_JABATAN, varchar(5),>
									,1	--<KD_STATUS_VALIDASI, smallint,>
									,TMT_PENEMPATAN	--<TMT_PENEMPATAN, date,>
									,NM_JABATAN	--<NM_JABATAN, varchar(255),>
									,@tanggal_buat	--<TANGGAL_BUAT, date,>
									,@nama_buat	--<NAMA_BUAT, varchar(18),>
									,CMP_ID	--<CMP_ID, varchar(10),>
									,@pmk_id_new	--<KSO_KASUSNOMOR, varchar(10),>
									,CASE @kd_jns_mutasi
									WHEN '1' THEN '1'
									WHEN '2' THEN '3'
									WHEN '3' THEN '9'
									WHEN '5' THEN '6'
									WHEN '6' THEN '2'
									WHEN '8' THEN '4'
									WHEN '10' THEN '8'
									WHEN '11' THEN '7'
									WHEN '12' THEN '10'
									END	--<STS_RIWAYAT_JABATAN, varchar(20),>
									,TMT_SATKER	--<TMT_SATKER, date,>
									,NO_URUT_JAB + 1	--<NO_URUT_JAB, int,>
									,@tgl_mulai	--<TANGGAL_RIWAYAT, date,>
									,@tgl_mulai
							FROM
								SPG_08_RIWAYAT_JABATAN
							WHERE 
								NIP = @nip 
								AND KD_STATUS_VALIDASI = '1' 
								AND TMT_JAB_PEG <= GETDATE() 

							ORDER BY 
								TMT_JAB_PEG DESC, 
								NO_URUT_JAB DESC

							INSERT INTO [dbo].[SPG_SK]
									([NO_SK]
									,[JNS_SK]
									,[TGL_SK]
									,[NIP])
							VALUES
									(@no_sk
									,'01'
									,@tgl_sk
									,@nip)
					end
				END
				FETCH NEXT FROM list_cursor INTO @nip  
			END  

			CLOSE list_cursor  
			DEALLOCATE list_cursor 
			-------------------End Query Baru/Tidak Terdampak------------------------------


			-----------------Query Terdampak----------------------------------------------
			DECLARE @pmk_id CHAR(10)

			DECLARE list_cursor CURSOR FAST_FORWARD FOR
			SELECT PMK_ID FROM SPG_PMK WHERE LEFT(KD_UNIT_ORG_BARU, 3) = left(@kd_lama,3) AND TGL_MULAI >= @start_date 
 
			OPEN list_cursor  
			FETCH NEXT FROM list_cursor INTO @pmk_id  

			WHILE @@FETCH_STATUS = 0  
			BEGIN  
				---- update PMK --
				UPDATE
					SPG_PMK
				SET
					KD_UNIT_ORG_BARU = left(@kd_baru,3) + RIGHT(KD_UNIT_ORG, 21)
				WHERE
					PMK_ID = @pmk_id

				IF EXISTS(SELECT * FROM SPG_08_RIWAYAT_JABATAN WHERE KSO_KASUSNOMOR = @pmk_id)
				BEGIN
					---- update Riwayat Jabatan --
					UPDATE
						SPG_08_RIWAYAT_JABATAN
					SET
						KD_UNIT_ORG = left(@kd_baru,3) + RIGHT(KD_UNIT_ORG, 21)--, TMT_REOG = @tgl_mulai
					WHERE
						KSO_KASUSNOMOR = @pmk_id
				END
				ELSE
				BEGIN
					DECLARE @max_id_2 bigint

					SELECT @max_id_2 = MAX(ID) FROM SPG_08_RIWAYAT_JABATAN

					DECLARE
						@pmk_nip varchar(8),
						@pmk_no_sk varchar(50),
						@pmk_tgl_sk date,
						@pmk_tgl_mulai date
					SELECT @pmk_nip=NIP, @pmk_no_sk=NO_SK, @pmk_tgl_sk=TGL_SK, @pmk_tgl_mulai=TGL_MULAI FROM SPG_PMK WHERE PMK_ID = @pmk_id
					--PRINT @pmk_nip
					---- insert Riwayat Jabatan --
					INSERT INTO [dbo].[SPG_08_RIWAYAT_JABATAN]
								([ID]
								,[NIP]
								,[NO_SK]
								,[DIKELUARKAN_OLEH]
								,[TMT_JAB_PEG]
								,[KD_KANTOR]
								,[JNS_JABATAN]
								,[KD_JABATAN_STR]
								,[KD_UNIT_ORG]
								,[KD_PANGKAT]
								,[KD_ESELON]
								,[STATUS_JABATAN]
								,[KD_STATUS_VALIDASI]
								,[TMT_PENEMPATAN]
								,[NM_JABATAN]
								,[TANGGAL_BUAT]
								,[NAMA_BUAT]
								,[CMP_ID]
								,[KSO_KASUSNOMOR]
								,[STS_RIWAYAT_JABATAN]
								,[TMT_SATKER]
								,[NO_URUT_JAB]
								,[TANGGAL_RIWAYAT]
								,[TMT_REOG]
								)
						SELECT TOP 1
								ROW_NUMBER() OVER(ORDER BY ID DESC) + @max_id_2	--<ID, bigint,>
								,@pmk_nip	--<NIP, varchar(18),>
								,@pmk_no_sk	--<NO_SK, varchar(50),>
								,'1'	--<DIKELUARKAN_OLEH, varchar(50),>
								,TMT_JAB_PEG	--<TMT_JAB_PEG, date,>
								,KD_KANTOR	--<KD_KANTOR, varchar(20),>
								,JNS_JABATAN	--<JNS_JABATAN, char(1),>
								,KD_JABATAN_STR	--<KD_JABATAN_STR, varchar(5),>
								,@kd_bidang_baru + RIGHT(KD_UNIT_ORG, 21)	--<KD_UNIT_ORG, varchar(30),>
								,KD_PANGKAT	--<KD_PANGKAT, varchar(15),>
								,KD_ESELON	--<KD_ESELON, char(2),>
								,STATUS_JABATAN	--<STATUS_JABATAN, varchar(5),>
								,1	--<KD_STATUS_VALIDASI, smallint,>
								,TMT_PENEMPATAN	--<TMT_PENEMPATAN, date,>
								,NM_JABATAN	--<NM_JABATAN, varchar(255),>
								,@tanggal_buat	--<TANGGAL_BUAT, date,>
								,@nama_buat	--<NAMA_BUAT, varchar(18),>
								,CMP_ID	--<CMP_ID, varchar(10),>
								,@pmk_id	--<KSO_KASUSNOMOR, varchar(10),>
								,CASE @kd_jns_mutasi
								WHEN '1' THEN '1'
								WHEN '2' THEN '3'
								WHEN '3' THEN '9'
								WHEN '5' THEN '6'
								WHEN '6' THEN '2'
								WHEN '8' THEN '4'
								WHEN '10' THEN '8'
								WHEN '11' THEN '7'
								WHEN '12' THEN '10'
								END	--<STS_RIWAYAT_JABATAN, varchar(20),>
								,TMT_SATKER	--<TMT_SATKER, date,>
								,NO_URUT_JAB + 1	--<NO_URUT_JAB, int,>
								,@pmk_tgl_mulai	--<TANGGAL_RIWAYAT, date,>
								,@tgl_mulai
						FROM
							SPG_08_RIWAYAT_JABATAN
						WHERE 
							NIP = @pmk_nip 
							AND KD_STATUS_VALIDASI = '1' 
							AND TMT_JAB_PEG <= GETDATE() 
						ORDER BY 
							TMT_JAB_PEG DESC, 
							NO_URUT_JAB DESC

						INSERT INTO [dbo].[SPG_SK]
								([NO_SK]
								,[JNS_SK]
								,[TGL_SK]
								,[NIP])
						VALUES
								(@pmk_no_sk
								,'01'
								,@pmk_tgl_sk
								,@pmk_nip)
				END
				FETCH NEXT FROM list_cursor INTO @pmk_id  
			END  

			CLOSE list_cursor  
			DEALLOCATE list_cursor 
			------------------End Query Terdampak--------------------------------------

			------------------Setting Admin Satker-------------------------------------
			Update [dbo].[SPG_ADMIN_SATKER]
			SET KD_UNIT_ORG = left(@kd_baru,3)+right(KD_UNIT_ORG,LEN(KD_UNIT_ORG)-3)
				where left(kd_unit_org,3) = left(@kd_lama,3)
			------------------End Setting Admin Satker---------------------------------

			--select count(*) data_pegawai_baru from SPG_DATA_CURRENT where left(kd_unit_org,3) = left(@kd_baru,3) and isnull(sts_pensiun,0) <> 1 

			--update Transaksi_Duplikasi set [status] = 1 where transaksi_duplikasi_id=@newid

			update Transaksi_Duplikasi set [status] = 1 where kd_unit_org_old=@kd_lama and kd_unit_org_new=@kd_baru and started_by=@nama_buat and [status]=0
		
		END
		else
		begin
			--print 'child unit organisasi tidak kosong'
			--update Transaksi_Duplikasi set [status] = 2 where transaksi_duplikasi_id=@newid
			update Transaksi_Duplikasi set [status] = 2 where kd_unit_org_old=@kd_lama and kd_unit_org_new=@kd_baru and started_by=@nama_buat and [status]=0
		end
		
		--SELECT @newid as transaksi_duplikasi_id

		 --release the lock on stored proc
		/*EXEC @returnCode = sp_releaseapplock 
							@Resource = 'usp_duplikasi_formasi_pegawai',                        
							@LockOwner = 'Session',
							@DbPrincipal  = 'public'*/
		
		
		
		COMMIT TRANSACTION
    END TRY      

    BEGIN CATCH
		ROLLBACK TRANSACTION
		/*IF @returnCode IN (0, 1)
		BEGIN
			 EXEC @returnCode = sp_releaseapplock 
					@Resource = 'usp_duplikasi_formasi_pegawai',                        
					@LockOwner = 'Session',
					@DbPrincipal  = 'public'
		END		
    
		DECLARE @ErrMsg VARCHAR(4000)
		SELECT @ErrMsg = ERROR_MESSAGE() 	    
		RAISERROR(@ErrMsg, 15, 50)*/
		SELECT ERROR_MESSAGE()
		DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE()
        DECLARE @ErrorSeverity INT = ERROR_SEVERITY()
        DECLARE @ErrorState INT = ERROR_STATE()
		exec sp_get_error_message
		RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState) WITH NOWAIT
	END CATCH  
END
GO
/****** Object:  StoredProcedure [dbo].[usp_generateCutiPegawai]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 1 September 2020
-- Description:	Untuk Generate Data Cuti Pegawai Yang Menggantung
-- =============================================
CREATE		 PROCEDURE [dbo].[usp_generateCutiPegawai]
@KSO_KASUSNOMOR VARCHAR(10) = NULL 
AS
    BEGIN

BEGIN TRANSACTION [Tran1]

  BEGIN TRY


DECLARE @noUrut BIGINT = 0
DECLARE @nip VARCHAR(32) = (SELECT TOP 1 NIP FROM dbo.SPG_PERMOHONAN_CUTI WHERE kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @newId BIGINT = ( SELECT ISNULL(MAX(ID) + 1, 1) FROM dbo.SPG_CUTI_PEGAWAI )
DECLARE @TGL_SRT_IZIN_CUTI DATE = (SELECT TOP 1 TGL_SRT_IZIN_CUTI FROM dbo.SPG_PERMOHONAN_CUTI WHERE kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @no_srt_izin_cuti VARCHAR(255) =  (SELECT TOP 1 NO_SURAT_IZIN FROM  dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @multiple_date VARCHAR(1) =  (SELECT TOP 1 MULTIPLE_DATE FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @tanggal_mulai DATE =  (SELECT TOP 1 TANGGAL_MULAI FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)	
DECLARE @tanggal_selesai DATE =  (SELECT TOP 1 TANGGAL_SELESAI FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)	
DECLARE @tanggal_perawatan_mulai DATE =  (SELECT TOP 1 TANGGAL_PERAWATAN_MULAI FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)	
DECLARE @tanggal_perawatan_selesai DATE =  (SELECT TOP 1 TANGGAL_PERAWATAN_SELESAI FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)	
DECLARE @rentang_tanggal VARCHAR(MAX) =  (SELECT TOP 1 RENTANG_TANGGAL FROM dbo.SPG_PERMOHONAN_CUTI WHERE   kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @JNS_CUTI INT =  (SELECT TOP 1 JNS_CUTI FROM dbo.SPG_PERMOHONAN_CUTI WHERE kso_kasusnomor = @KSO_KASUSNOMOR)
DECLARE @FP_ID VARCHAR(100) = (SELECT TOP 1  b.FP_ID FROM SPG_DATA_CURRENT AS b WHERE b.NIP = @nip)

SET @noUrut = @noUrut +1

IF(@multiple_date ='1' OR @rentang_tanggal IS NOT NULL)
BEGIN
	INSERT INTO 
				SPG_CUTI_PEGAWAI 
			(
				ID,
				JNS_CUTI,
				NIP,
				NO_URUT_CUTI_PEG,
				NO_SRT_IZIN_CUTI,
				TGL_SRT_IZIN_CUTI,
				KD_STATUS_VALIDASI,
				JUMLAH_CUTI,
				SISA_CUTI,
				TAHUN,
				NAMA_BUAT,
				TANGGAL_BUAT,
				KSO_KASUSNOMOR,
				PERSALINAN_KE,
				NO_SKET_DOKTER,
				STATUS_PERAWATAN,
				RUMAH_SAKIT,
				TANGGAL_PERAWATAN_MULAI,
				TANGGAL_PERAWATAN_SELESAI,
				MULTIPLE_DATE,
				UANG_CUTI,
				NIP_PJS,
				PERIODE_PEMBAYARAN,
				TANGGAL_HPL,
				TANGGAL_LAHIR,
				ATURAN_WAKTU,
				RENTANG_TANGGAL
			)
		SELECT @newId,
				JNS_CUTI,
				NIP,
				@noUrut,
				NO_SRT_IZIN_CUTI,
				TGL_SRT_IZIN_CUTI,
				'1',
				REPLACE(dbo.TRIM(LAMA_CUTI),'hari',''),
				SISA_TOTAL_TAHUN_INI,
				TAHUN,
				NAMA_BUAT,
				TANGGAL_BUAT,
				KSO_KASUSNOMOR,
				PERSALINAN_KE,
				NO_SKET_DOKTER,
				STATUS_PERAWATAN,
				RUMAH_SAKIT,
				TANGGAL_PERAWATAN_MULAI,
				TANGGAL_PERAWATAN_SELESAI,
				MULTIPLE_DATE,
				UANG_CUTI,
				NIP_PJS,
				CONVERT(VARCHAR(6),TANGGAL_MULAI,112), --date("Ym",strtotime($tanggal_mulai)) => 2011-01-07 = 201101
				TANGGAL_HPL,
				TANGGAL_LAHIR,
				ATURAN_WAKTU,
				RENTANG_TANGGAL
		FROM dbo.SPG_PERMOHONAN_CUTI
		WHERE   kso_kasusnomor = @KSO_KASUSNOMOR

END
ELSE
BEGIN	
	INSERT INTO 
				SPG_CUTI_PEGAWAI 
			(
				ID,
				JNS_CUTI,
				NIP,
				NO_URUT_CUTI_PEG,
				NO_SRT_IZIN_CUTI,
				TGL_SRT_IZIN_CUTI,
				KD_STATUS_VALIDASI,
				JUMLAH_CUTI,
				SISA_CUTI,
				TAHUN,
				NAMA_BUAT,
				TANGGAL_BUAT,
				KSO_KASUSNOMOR,
				PERSALINAN_KE,
				NO_SKET_DOKTER,
				STATUS_PERAWATAN,
				RUMAH_SAKIT,
				TANGGAL_PERAWATAN_MULAI,
				TANGGAL_PERAWATAN_SELESAI,
				MULTIPLE_DATE,
				UANG_CUTI,
				NIP_PJS,
				PERIODE_PEMBAYARAN,
				TANGGAL_HPL,
				TANGGAL_LAHIR,
				ATURAN_WAKTU,
				TGL_AWAL_CUTI_PEG,
				TGL_AKHIR_CUTI_PEG
			)
			SELECT @newId,
				JNS_CUTI,
				NIP,
				@noUrut,
				NO_SRT_IZIN_CUTI,
				TGL_SRT_IZIN_CUTI,
				'1',
				REPLACE(dbo.TRIM(LAMA_CUTI),'hari',''),
				SISA_TOTAL_TAHUN_INI,
				TAHUN,
				NAMA_BUAT,
				TANGGAL_BUAT,
				KSO_KASUSNOMOR,
				PERSALINAN_KE,
				NO_SKET_DOKTER,
				STATUS_PERAWATAN,
				RUMAH_SAKIT,
				TANGGAL_PERAWATAN_MULAI,
				TANGGAL_PERAWATAN_SELESAI,
				MULTIPLE_DATE,
				UANG_CUTI,
				NIP_PJS,
				CONVERT(VARCHAR(6),TANGGAL_MULAI,112), --date("Ym",strtotime($tanggal_mulai)) => 2011-01-07 = 201101
				TANGGAL_HPL,
				TANGGAL_LAHIR,
				ATURAN_WAKTU,
				TANGGAL_MULAI, 
				TANGGAL_SELESAI  
		FROM dbo.SPG_PERMOHONAN_CUTI
		WHERE   kso_kasusnomor = @KSO_KASUSNOMOR
END

IF(@rentang_tanggal IS NOT NULL)
BEGIN
	INSERT INTO dbo.SPG_UPDATE_ABSENSI
	        ( FP_ID ,
	          TANGGAL ,
	          TANGGAL_BUAT , --date("Y-m-d")
	          STATUS ,
	          NIP_PEMBUAT
	        )
	SELECT @FP_ID,ISNULL(CONVERT(CHAR(10),CONVERT(DATETIME,split,105),101),NULL),ISNULL(CONVERT(CHAR(10),CONVERT(DATETIME,split,105),101),NULL),'0','SYSTEM_CUTI'
	FROM dbo.Split(',',@rentang_tanggal)
END
ELSE
BEGIN	
WHILE @tanggal_mulai <= @tanggal_selesai
	BEGIN
		INSERT INTO dbo.SPG_UPDATE_ABSENSI
				( FP_ID ,
				  TANGGAL ,
				  TANGGAL_BUAT , --date("Y-m-d")
				  STATUS ,
				  NIP_PEMBUAT
				)
		VALUES (@FP_ID,@tanggal_mulai,ISNULL(CONVERT(DATE,@tanggal_mulai),NULL) ,'0','SYSTEM_CUTI')
		SELECT @tanggal_mulai = DATEADD(DAY,1,@tanggal_mulai)
	END	
END

IF (@multiple_date ='1')
BEGIN

	INSERT INTO SPG_TANGGAL_CUTI_PEGAWAI (ID,NIP,TANGGAL_CUTI)
	SELECT @newId,@nip,ISNULL(CONVERT(DATE,split),NULL) 
	FROM dbo.Split(',',@rentang_tanggal) --convert_date_sql($val,1)
END

IF (@JNS_CUTI =16)
BEGIN
	DECLARE @tanggal_perhitungan DATE =NULL 
	IF((SELECT ISNULL(count(*),0) FROM SPG_REKAP_CUTI WHERE jns_cuti in('2','6') and nip=@nip) >=1)
	BEGIN

	DECLARE @Tanggal_Masuk DATE = (SELECT TOP 1 tgl_masuk from spg_data_current where nip=@nip)
	DECLARE @jumlah_cuti INT = (select sum(JUMLAH_CUTI) from SPG_CUTI_PEGAWAI where nip=@nip AND JNS_CUTI='16')
	SET @tanggal_perhitungan = CONVERT(DATE,DATEADD(DAY,@jumlah_cuti,@Tanggal_Masuk),106) --date("Y-m-d",strtotime("+".$jumlah_cuti_peg." day",strtotime($tgl_masuk_peg))) => Tanggal Masuk + Jumlah Cuti Sebagai Hari

	END
	ELSE
    BEGIN	
	SET @tanggal_perhitungan = @tanggal_selesai
	END	
	UPDATE dbo.SPG_PEGAWAI set TGL_PERHITUNGAN_CUTI = @tanggal_perhitungan WHERE NIP = @nip
END

  COMMIT TRANSACTION [Tran1]

  END TRY

  BEGIN CATCH
     ROLLBACK TRANSACTION [Tran1]

  END CATCH  
	END
GO
/****** Object:  StoredProcedure [dbo].[usp_getAktivitasIndividu]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getAktivitasIndividu] 
@NIP VARCHAR(32)= NULL,
@tahun BIGINT = NULL
AS
    BEGIN
DECLARE @kodeJabatan VARCHAR(3)	 = ( SELECT TOP 1 KD_JABATAN_STR FROM dbo.TBL_USER WHERE NIP=@NIP)
DECLARE @kodeOrganisasi VARCHAR(32)	 = ( SELECT TOP 1 KD_UNIT_ORG FROM dbo.TBL_USER WHERE NIP=@NIP)

SELECT ai.*,uo.nama_satker_organisasi,sdc.NM_PEG FROM dbo.Aktivitas_Individu ai 
LEFT JOIN dbo.SPG_08_UNIT_ORGANISASI uo ON uo.KD_UNIT_ORG = ai.KD_UNIT_ORG
LEFT JOIN dbo.SPG_DATA_CURRENT sdc ON sdc.NIP = ai.NIP
WHERE ai.nip IN (

SELECT tu.NIP FROM dbo.TBL_USER tu 
LEFT JOIN dbo.SPG_HIRARKI_ORGANISASI sho 
ON sho.KD_JABATAN_STR = tu.KD_JABATAN_STR AND sho.KD_UNIT_ORG = tu.KD_UNIT_ORG 
WHERE sho.KD_JABATAN_STR_INDUK=@kodeJabatan AND sho.KD_UNIT_ORG_INDUK=@kodeOrganisasi
) AND ai_tahun=@tahun AND stsrc='A'

    END


GO
/****** Object:  StoredProcedure [dbo].[usp_getAllTipeForAlias]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_getAllTipeForAlias]
AS 
SELECT TABLE_NAME,'Table' AS tipe
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' 
UNION
SELECT TABLE_NAME,'View' AS tipe
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_NAME LIKE 'vw_rpt%'
UNION
SELECT ROUTINE_NAME,'Store Procedure' AS tipe
  FROM INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME LIKE 'usp_rpt%'
GO
/****** Object:  StoredProcedure [dbo].[usp_getAtasanSimfosia]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Stored Procedure

-- =============================================
-- Author:		Tomi
-- Create date: 15 Maret 2021
-- Description:	Untuk SK Generator
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getAtasanSimfosia] 
AS
    BEGIN
DECLARE @kodeJabatan VARCHAR(3)	 = ( SELECT TOP 1 set_value FROM dbo.FW_Ref_Setting WHERE set_name='JabatanAtasan')
DECLARE @kodeOrganisasi VARCHAR(300)	 = ( SELECT TOP 1 set_value FROM dbo.FW_Ref_Setting WHERE set_name='OrganisasiAtasan')
DECLARE @kodeKantor VARCHAR(300)	 = ( select TOP 1 KD_KANTOR from dbo.SPG_HIRARKI_ORGANISASI WHERE KD_UNIT_ORG=@kodeOrganisasi and KD_JABATAN_STR=@kodeJabatan)
DECLARE @i INT=1
DECLARE	@posisi VARCHAR(300) = (select TOP 1 POSISI from SPG_08_JABATAN_UNIT WHERE kd_unit_org=@kodeOrganisasi and kd_jabatan=@kodeJabatan)
DECLARE	@nama VARCHAR(300) = (select TOP 1 NM_PEG from spg_data_current WHERE kd_unit_org=@kodeOrganisasi and KD_JABATAN_STR=@kodeJabatan)

IF @nama IS NULL 
BEGIN
WHILE @i<7
BEGIN


IF @nama IS NOT NULL
BREAK

DECLARE @oldkodeJabatan VARCHAR(3)	 = @kodeJabatan
SELECT @kodeJabatan =(SELECT TOP 1 KD_JABATAN_STR_INDUK FROM dbo.SPG_HIRARKI_ORGANISASI WHERE KD_JABATAN_STR=@oldkodeJabatan AND KD_UNIT_ORG =@kodeOrganisasi)
SELECT @kodeOrganisasi=(SELECT TOP 1 KD_UNIT_ORG_INDUK FROM dbo.SPG_HIRARKI_ORGANISASI WHERE KD_JABATAN_STR=@oldkodeJabatan AND KD_UNIT_ORG =@kodeOrganisasi)
SELECT @kodeKantor=ISNULL((SELECT TOP 1 KD_KANTOR_INDUK FROM dbo.SPG_HIRARKI_ORGANISASI WHERE KD_JABATAN_STR=@oldkodeJabatan AND KD_UNIT_ORG =@kodeOrganisasi),@kodeKantor)


SET	@posisi = (select TOP 1 POSISI from SPG_08_JABATAN_UNIT WHERE kd_unit_org=@kodeOrganisasi and kd_jabatan=@kodeJabatan ) --AND KD_KANTOR= @kodeKantor
SET	@nama = (select TOP 1 NM_PEG from spg_data_current WHERE kd_unit_org=@kodeOrganisasi and KD_JABATAN_STR=@kodeJabatan AND KD_KANTOR = @kodeKantor AND STS_PENSIUN=0)

SET @i=@i+1

END


end

SELECT @posisi AS Posisi,@nama AS Nama

    END


GO
/****** Object:  StoredProcedure [dbo].[usp_getBebanKerja]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getBebanKerja] @id BIGINT = NULL
AS
    BEGIN

        DECLARE @tahun AS INT = ( SELECT    ai_tahun
                                  FROM      dbo.Aktivitas_Individu
                                  WHERE     stsrc = 'A'
                                            AND ai_id = @id
                                )

        DECLARE @workDayMingguan AS INT = ( SELECT TOP 1
                                                    bk_seminggu
                                            FROM    dbo.Master_Beban_Kerja
                                            WHERE   bk_tahun = @tahun
                                                    AND stsrc = 'A'
                                          )
        DECLARE @workDayBulanan AS INT = ( SELECT TOP 1
                                                    bk_sebulan
                                           FROM     dbo.Master_Beban_Kerja
                                           WHERE    bk_tahun = @tahun
                                                    AND stsrc = 'A'
                                         )

        DECLARE @workDayTahunan AS INT = ( SELECT TOP 1
                                                    bk_setahun
                                           FROM     dbo.Master_Beban_Kerja
                                           WHERE    bk_tahun = @tahun
                                                    AND stsrc = 'A'
                                         )
	

        SELECT  aid.aid_id ,
                mp.projek_nama ,
                aid.aid_tugas ,
                aid.aid_periode ,
                aid.aid_jumlah ,
                aid.aid_status ,
                Durasi = CAST(CAST(aid.aid_durasi AS FLOAT) AS VARCHAR(10))
                + ' ' + aid.aid_satuan ,
                Beban_Kerja = CASE WHEN aid.aid_periode = 'Harian'
                                        AND aid.aid_satuan = 'Jam'
                                   THEN CAST(CAST(aid.aid_durasi * 60 AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Harian'
                                        AND aid.aid_satuan = 'Menit'
                                   THEN CAST(CAST(aid.aid_durasi AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Mingguan'
                                        AND aid.aid_satuan = 'Jam'
                                   THEN CAST(CAST(aid.aid_durasi * 60
                                        / ISNULL(@workDayMingguan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Mingguan'
                                        AND aid.aid_satuan = 'Menit'
                                   THEN CAST(CAST(aid.aid_durasi
                                        / ISNULL(@workDayMingguan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Bulanan'
                                        AND aid.aid_satuan = 'Jam'
                                   THEN CAST(CAST(aid.aid_durasi * 60
                                        / ISNULL(@workDayBulanan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Bulanan'
                                        AND aid.aid_satuan = 'Menit'
                                   THEN CAST(CAST(aid.aid_durasi
                                        / ISNULL(@workDayBulanan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Tahunan'
                                        AND aid.aid_satuan = 'Jam'
                                   THEN CAST(CAST(aid.aid_durasi * 60
                                        / ISNULL(@workDayTahunan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                                   WHEN aid.aid_periode = 'Tahunan'
                                        AND aid.aid_satuan = 'Menit'
                                   THEN CAST(CAST(aid.aid_durasi
                                        / ISNULL(@workDayTahunan, 1) AS FLOAT) AS VARCHAR(10))
                                        + ' Menit'
                              END ,
                Durasi_Menit = CASE WHEN aid.aid_periode = 'Harian'
                                         AND aid.aid_satuan = 'Jam'
                                    THEN CAST(aid.aid_durasi * 60 AS FLOAT)
                                    WHEN aid.aid_periode = 'Harian'
                                         AND aid.aid_satuan = 'Menit'
                                    THEN CAST(aid.aid_durasi AS FLOAT)
                                    WHEN aid.aid_periode = 'Mingguan'
                                         AND aid.aid_satuan = 'Jam'
                                    THEN CAST(aid.aid_durasi * 60
                                         / ISNULL(@workDayMingguan, 1) AS FLOAT)
                                    WHEN aid.aid_periode = 'Mingguan'
                                         AND aid.aid_satuan = 'Menit'
                                    THEN CAST(aid.aid_durasi
                                         / ISNULL(@workDayMingguan, 1) AS FLOAT)
                                    WHEN aid.aid_periode = 'Bulanan'
                                         AND aid.aid_satuan = 'Jam'
                                    THEN CAST(aid.aid_durasi * 60
                                         / ISNULL(@workDayBulanan, 1) AS FLOAT)
                                    WHEN aid.aid_periode = 'Bulanan'
                                         AND aid.aid_satuan = 'Menit'
                                    THEN CAST(aid.aid_durasi
                                         / ISNULL(@workDayBulanan, 1) AS FLOAT)
                                    WHEN aid.aid_periode = 'Tahunan'
                                         AND aid.aid_satuan = 'Jam'
                                    THEN CAST(aid.aid_durasi * 60
                                         / ISNULL(@workDayTahunan, 1) AS FLOAT)
                                    WHEN aid.aid_periode = 'Tahunan'
                                         AND aid.aid_satuan = 'Menit'
                                    THEN CAST(aid.aid_durasi
                                         / ISNULL(@workDayTahunan, 1) AS FLOAT)
                               END
        FROM    dbo.Aktivitas_Individu_Detail aid
                JOIN dbo.Master_Projek mp ON mp.projek_id = aid.projek_id
                                             AND mp.stsrc = 'A'
        WHERE   aid.stsrc = 'A'
                AND aid.ai_id = @id
    END


GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarCutiGantung]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 25 Agustus 2020
-- Description:	Untuk akses User
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getDaftarCutiGantung]
AS
    BEGIN
        SELECT  *
        FROM    dbo.WF_OPKASUS
        WHERE   KSO_KASUSNOMOR NOT IN ( SELECT  kso_kasusnomor
                                        FROM    dbo.SPG_PERMOHONAN_CUTI
                                        WHERE   kso_kasusnomor IS NOT NULL )
                AND KSO_WORKFLOWKODE = 'WF-000001'
                AND KSO_STATUSKASUS = 'Open'
    END	

GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarJabatanAktif]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 28 Desember 2020
-- Description:	Untuk Data Jabatan Aktif
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getDaftarJabatanAktif]

AS
    BEGIN
	
SELECT sfs.NM_GOL FROM dbo.SPG_FORMASI_STRUKTURAL sfs
LEFT JOIN dbo.SPG_08_JABATAN_STR js ON js.KD_JABATAN_STR = sfs.KD_JABATAN_STR AND js.JENIS LIKE 'struktural%'
WHERE JUMLAH_PEGAWAI<>0

END


GO
/****** Object:  StoredProcedure [dbo].[usp_getDaftarJabatanStruktural]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Firnada
-- Create date: 30 Juli 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getDaftarJabatanStruktural] 
@leveljabatan VARCHAR(100) = null,
@satker VARCHAR(100) = null,
@status VARCHAR(50) = null
AS
    BEGIN
	
	DECLARE @sql NVARCHAR(max)=''

SET @sql = 'IF OBJECT_ID(''tempdb..#TempTable'') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable
    (
      ID BIGINT ,
      Parent_ID BIGINT ,
      KD_ORG VARCHAR(50) ,
      KD_ORG_INDUK VARCHAR(50) ,
      Nama_ORG VARCHAR(500),
	  Jenis_Jabatan VARCHAR(500),
	  NIP_PREV VARCHAR(50),
	  NM_PEG VARCHAR(100), 
	  PICTURE VARCHAR(500), 
	  nama_satker_organisasi Varchar(100),
	  STS_PENSIUN Varchar(100),
	  NM_GOL Varchar(100),
	  JUMLAH_PEGAWAI int,
	  BIDANG Varchar(100),
	  NM_JABATAN Varchar(100),
	  STATUS_JABATAN Varchar(100),
	  TGL_MULAI date,
	  KD_JABATAN_TGS varchar(100)
    )

INSERT  INTO #TempTable
        ( ID ,
          KD_ORG ,
          KD_ORG_INDUK ,
          Nama_ORG, 
		  Jenis_Jabatan, 
		  NIP_PREV, 
		  NM_PEG, 
		  PICTURE, 
		  nama_satker_organisasi,
		  STS_PENSIUN,
		  NM_GOL,
		  JUMLAH_PEGAWAI,
		  BIDANG,
		  NM_JABATAN,
		  STATUS_JABATAN,
		  TGL_MULAI,
		  KD_JABATAN_TGS 
		  
        )
		   SELECT  ROW_NUMBER() OVER ( ORDER BY a.ID ) ,
                a.KD_UNIT_ORG ,
                a.KD_UNIT_ORG_INDUK ,
                a.NM_UNIT_ORG, 
				c.JENIS, 
				b.NIP_PREV, 
				d.NM_PEG, 
				d.PICTURE,
				a.nama_satker_organisasi,
				d.STS_PENSIUN,
				e.NM_GOLONGAN,
				b.JUMLAH_PEGAWAI,
				b.BIDANG, b.NM_JABATAN,
				f.STATUS_JABATAN, 
				g.TGL_MULAI,
				d.KD_JABATAN_TGS
		
        FROM    dbo.SPG_FORMASI_STRUKTURAL  b
		LEFT join SPG_08_UNIT_ORGANISASI a on a.KD_UNIT_ORG = b.KD_UNIT_ORG
		LEFT join SPG_08_JABATAN_STR c on b.KD_JABATAN_STR = c.KD_JABATAN_STR AND c.JENIS  LIKE ''struktural%''   
		LEFT join SPG_DATA_CURRENT d on b.NIP_PREV = d.NIP AND d.KD_JABATAN_STR IS NOT NULL
		LEFT join SPG_GOLONGAN e on b.KD_GOL = e.KD_GOL
		LEFT join SPG_08_RIWAYAT_JABATAN f on b.NIP_PREV = f.NIP AND f.KD_JABATAN_STR = b.KD_JABATAN_STR and f.STATUS_JABATAN in (''4'', ''5'')
		CROSS APPLY (
		 SELECT  TOP 1 TGL_MULAI 
        FROM    dbo.SPG_PMK
        WHERE   b.NIP_PREV = NIP
		) g
		--join SPG_PMK g on b.NIP_PREV = g.NIP and b.KD_JABATAN_STR = g.KD_JABATAN_STR and b.KD_UNIT_ORG = g.KD_UNIT_ORG
		group by a.ID, a.KD_UNIT_ORG, a.KD_UNIT_ORG_INDUK, a.NM_UNIT_ORG, c.JENIS, b.NIP_PREV, d.NM_PEG, d.PICTURE, a.nama_satker_organisasi, d.STS_PENSIUN, e.NM_GOLONGAN,b.JUMLAH_PEGAWAI, b.BIDANG, b.NM_JABATAN, f.STATUS_JABATAN, g.TGL_MULAI, d.KD_JABATAN_TGS

UPDATE  r
SET     r.Parent_ID = s.ID
FROM    #TempTable r
        JOIN #TempTable s ON r.KD_ORG_INDUK = s.KD_ORG
'		

SET @sql += ' SELECT  *
			FROM    #TempTable tt where 1 = 1 '

IF @leveljabatan IS NOT NULL
BEGIN
SET @sql += ' AND tt.NM_GOL = @leveljabatan '
END

IF @satker IS NOT NULL
BEGIN
SET @sql += ' AND tt.nama_satker_organisasi LIKE ''%' +@satker+'%'' '
END 

IF @status IS NOT NULL
BEGIN
SET @sql += ' AND tt.STATUS_JABATAN = @status '
END


exec sp_executesql @sql, N'@leveljabatan Varchar(100),@satker varchar(100),@status Varchar(50)', @leveljabatan,@satker,@status


END


GO
/****** Object:  StoredProcedure [dbo].[usp_getDataRiwayatTugasDefinitif]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_getDataRiwayatTugasDefinitif]
@nip VARCHAR(50)
AS 
SELECT p.NIP,
		p.KD_STATUS_VALIDASI,
       p.NM_PEG,
       p.TGL_MASUK,
       se.NM_ESELON,
       CONVERT(VARCHAR(2), mkt.masa_kerja) + ' Tahun ' + CONVERT(VARCHAR(2), mkb.masa_kerja2) + ' Bulan' AS masa_kerja,
       sdc.NM_PEND_UMUM,
       sdc.NM_LEMB_PEND_FORMAL,
	   fa.FAKULTAS,
	   tbd.NO_SK_TUGAS_BELAJAR,tbd.TGL_TUGAS_BELAJAR,jpu.NM_PEND_UMUM AS jns_pend,tbd.NM_UNIVERSITAS,tbd.NM_FAKULTAS,tbd.NM_JURUSAN,
	   tbd.LOKASI,tbd.NEGARA,tbd.KOTA,tbd.JNS_BEASISWA,tbd.FASKES_YKP_PEGAWAI,tbd.FASKES_YKP_KELUARGA,tbd.BAWA_KELUARGA,tbd.CATATAN,tbd.TGL_MULAI_BEASISWA,tbd.TGL_BERAKHIR_BEASISWA,tbd.TGL_MULAI,tbd.TGL_SELESAI
FROM dbo.SPG_PEGAWAI p
    LEFT JOIN dbo.SPG_PESERTA_TUGAS_BELAJAR tb
        ON tb.NIP = p.NIP
    LEFT JOIN dbo.SPG_DATA_CURRENT sdc
        ON sdc.NIP = p.NIP
    LEFT JOIN dbo.SPG_ESELON se
        ON se.KD_ESELON = sdc.KD_ESELON
	LEFT JOIN (SELECT TOP 1 FAKULTAS,NIP FROM dbo.SPG_RIWAYAT_PEND_FORMAL WHERE NIP=@nip ORDER BY NO_URUT_PEND_FORMAL DESC) fa ON  fa.NIP = p.NIP
    LEFT JOIN
    (
        SELECT NIP,
               DATEDIFF(MONTH, (TGL_MASUK), GETDATE()) / 12 AS masa_kerja
        FROM SPG_DATA_CURRENT
    ) mkt
        ON mkt.NIP = p.NIP
    LEFT JOIN
    (
        SELECT NIP,
               DATEDIFF(MONTH, (TGL_MASUK), GETDATE()) - (12 * (DATEDIFF(MONTH, (TGL_MASUK), GETDATE()) / 12)) AS masa_kerja2
        FROM SPG_DATA_CURRENT
    ) mkb
        ON mkb.NIP = p.NIP
	LEFT JOIN dbo.SPG_TUGAS_BELAJAR_DEFINITIF tbd ON tbd.NIP = p.NIP
	LEFT JOIN dbo.SPG_JNS_PEND_UMUM jpu ON jpu.KD_PEND_UMUM = tbd.KD_PEND_UMUM
WHERE p.NIP = @nip
GO
/****** Object:  StoredProcedure [dbo].[usp_GetDetailHistorisPayTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Tomi
-- Create date: 30 Sept 2020
-- Description:	Untuk Detail Histori Tunjangan Pegawai
-- =============================================
CREATE PROCEDURE [dbo].[usp_GetDetailHistorisPayTunjangan] 
@nip Varchar(100) = NULL, 
@periode Varchar(50) = NULL,
@tunjangan Varchar(100) = NULL

AS
DECLARE @sql NVARCHAR(MAX)=''

SET @sql =' 

SELECT DISTINCT c.NM_KMPNEN,a.TMT,a.TMT_END,a.KETERANGAN,a.KETERANGAN_2,a.REMARK,a.CREATED_DATE
	from HIST_PAY_TUNJANGAN_PEGAWAI a
	inner join PAY_KOMPONEN_GAJI c on a.KPG_ID = c.KPG_ID	
	WHERE a.NIP=@nip AND Format(a.CREATED_DATE, ''MMMM-yyyy'') = @periode

'


IF ISNULL(@tunjangan,'') <>''
BEGIN	
	SET @sql +=' AND c.NM_KMPNEN LIKE ''%' +@tunjangan+'%'' ' 
END

SET @sql += '	ORDER BY	NM_KMPNEN ASC,a.CREATED_DATE DESC '

exec sp_executesql @sql, N'@nip Varchar(100),@periode varchar(50),@tunjangan Varchar(100)', @nip,@periode,@periode

GO
/****** Object:  StoredProcedure [dbo].[usp_getDMSAccessScope]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 15 Maret 2021
-- Description:	Untuk Cek Akses Scope DMS
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getDMSAccessScope] 
@fua VARCHAR(200) = NULL
AS

DECLARE @Access VARCHAR(100)=''

SET @Access = ( SELECT TOP 1 WIA_INQUIRYACCESSMENU FROM  dbo.WF_INQUIRY_ACCESS_MENU WHERE WIA_INQUIRYMENU='DocumentManagement' AND WIA_ACCESSNAME=@fua )

SELECT @Access AS Akses

GO
/****** Object:  StoredProcedure [dbo].[usp_getEmployeeBirthday]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getEmployeeBirthday] 
AS
    BEGIN
	
SELECT NIP,NM_PEG,uo.nama_satker_organisasi,NM_JABATAN,sdc.EMAIL,sdc.EMAIL_2,Usia = (YEAR(GETDATE())-YEAR(TGL_LAHIR_PEG)) 
FROM dbo.SPG_DATA_CURRENT sdc
LEFT JOIN dbo.SPG_08_UNIT_ORGANISASI uo ON uo.KD_UNIT_ORG = sdc.KD_UNIT_ORG
WHERE DAY(TGL_LAHIR_PEG) = DAY(GETDATE()) AND MONTH(TGL_LAHIR_PEG) = MONTH(GETDATE())
AND sdc.STS_PENSIUN =0
    END


GO
/****** Object:  StoredProcedure [dbo].[usp_getFieldTVAlias]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_getFieldTVAlias]
AS 
SELECT rga_tvs,b.COLUMN_NAME,a.rga_id
FROM dbo.RptGrid_Alias a
LEFT JOIN (
SELECT COLUMN_NAME,TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
) b ON a.rga_tvs=b.TABLE_NAME
WHERE rga_tipe IN ('Table','View') AND stsrc='A'
GO
/****** Object:  StoredProcedure [dbo].[usp_GetHistorisPayTunjangan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Firnanda
-- Create date: 20 Juli 2020
-- Description:	Untuk Historis Tunjangan Pegawai
-- =============================================
CREATE PROCEDURE [dbo].[usp_GetHistorisPayTunjangan] 
@nip Varchar(100) = NULL, 
@satker VARCHAR(100) = NULL, 
@bulan Varchar(50) = NULL,
@tahun Varchar(50) = NULL,
@tunjangan Varchar(100) = NULL,
@lvlJabatan VARCHAR(2) = NULL,
@namaJabatan VARCHAR(100) = NULL

AS
DECLARE @sql NVARCHAR(MAX)=''

SET @sql =' 

select a.NIP,  Format(a.CREATED_DATE, ''MMMM-yyyy'') AS TANGGAL,FORMAT(a.CREATED_DATE,''MM'') AS bulan ,FORMAT(a.CREATED_DATE,''yyyy'') as tahun , b.NM_PEG, b.NM_JABATAN, d.NM_GOLONGAN,uo.nama_satker_organisasi
	from HIST_PAY_TUNJANGAN_PEGAWAI a
	INNER JOIN SPG_DATA_CURRENT b on a.NIP = b.NIP
	LEFT JOIN dbo.SPG_08_UNIT_ORGANISASI uo ON b.KD_UNIT_ORG = uo.KD_UNIT_ORG
	INNER JOIN PAY_KOMPONEN_GAJI c on a.KPG_ID = c.KPG_ID
	INNER JOIN spg_golongan d on b.KD_GOL = d.KD_GOL
	WHERE a.stsrc =''A''
'
IF @nip IS NOT NULL
BEGIN	
	SET @sql +=' AND a.NIP=@nip ' 
END

IF @satker IS NOT NULL
BEGIN

	SET @sql +=' AND b.KD_UNIT_ORG=@satker ' 


END


IF @bulan IS NOT NULL
BEGIN	
	SET @sql +=' AND MONTH(a.CREATED_DATE)=@bulan ' 
END

IF @tahun IS NOT NULL
BEGIN	
	SET @sql +=' AND YEAR(a.CREATED_DATE)=@tahun ' 
END

IF ISNULL(@tunjangan,'') <>''
BEGIN	
	SET @sql +=' AND c.NM_KMPNEN LIKE ''%' +@tunjangan+'%'' ' 
END


IF @lvlJabatan IS NOT NULL
BEGIN	
	SET @sql +=' AND d.KD_GOL= @lvlJabatan ' 
END


IF ISNULL(@namaJabatan,'') <>''
BEGIN	
	SET @sql +=' AND b.NM_JABATAN LIKE ''%' +@namaJabatan+'%'' ' 
END

SET @sql +=' GROUP BY a.NIP, Format(a.CREATED_DATE, ''MMMM-yyyy''),FORMAT(a.CREATED_DATE,''MM'') ,FORMAT(a.CREATED_DATE,''yyyy''),b.NM_PEG,b.NM_JABATAN,d.NM_GOLONGAN,uo.nama_satker_organisasi '

SET @sql +=' ORDER BY FORMAT(a.CREATED_DATE,''yyyy'') desc,FORMAT(a.CREATED_DATE,''MM'') desc '

EXEC sp_executesql @sql, N'@nip Varchar(100),@satker varchar(100),@bulan Varchar(50),@tahun Varchar(50),@tunjangan Varchar(100),@lvlJabatan VARCHAR(2),@namaJabatan VARCHAR(100)', @nip,@satker,@bulan,@tahun,@tunjangan,@lvlJabatan,@namaJabatan

GO
/****** Object:  StoredProcedure [dbo].[usp_getNotification]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Yulius
-- Create date: 2019-10-03
-- Description:	Melakukan email-email untuk si stakeholder ticket
-- =============================================
-- [usp_getNotification] 'admin'
CREATE PROCEDURE [dbo].[usp_getNotification]
@user_id VARCHAR(50)


AS
BEGIN

DECLARE @email VARCHAR(200)
SELECT @email = user_email FROM dbo.User_Master WHERE user_id = @user_id

SELECT TOP 20 notice_id, notice_title, notice_ref_id, notice_small_content, nott_id, date_created 
FROM dbo.FW_Notice 
WHERE stsrc = 'A' AND (notice_to LIKE '%' + @email + '%' OR notice_cc LIKE '%' + @email + '%')
ORDER BY date_created DESC

END




GO
/****** Object:  StoredProcedure [dbo].[usp_getPemenuhanSDM]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		Tomi
-- Create date: 21 Nov 2020
-- Description:	Untuk Prioritas Pemenuhan SDM
-- =============================================
CREATE PROCEDURE [dbo].[usp_getPemenuhanSDM] --@id BIGINT = NULL
AS
    BEGIN
	SELECT t.*,
	Prioritas = CASE 
	WHEN (t.Beban_Kerja ='Overload' OR t.Beban_Kerja ='Inload') AND t.JENIS LIKE '%struktural%' THEN 'Prioritas Tinggi'
	WHEN (t.Beban_Kerja ='Underload' ) AND t.JENIS LIKE '%struktural%' THEN 'Prioritas Sedang'
	WHEN (t.Beban_Kerja ='Overload') AND t.JENIS LIKE '%fungsional%' THEN 'Prioritas Tinggi'
	WHEN (t.Beban_Kerja ='Inload') AND t.JENIS LIKE '%fungsional%' THEN 'Prioritas Sedang'
	WHEN (t.Beban_Kerja ='Underload') AND t.JENIS LIKE '%fungsional%' THEN 'Prioritas Rendah'
	END
	 FROM (
SELECT  sfs.KD_UNIT_ORG ,
        sfs.NIP_PREV ,
        sfs.BIDANG ,
        sfs.SATKER ,
        sfs.DIREKTORAT ,
        sfs.NM_JABATAN ,
        sfs.JUMLAH_PEGAWAI ,
        j.JENIS ,
        sfs.ALASAN_PERUBAHAN_PREV,
		dbo.AnalisaBebanKerja(ISNULL(sfs.NIP_PREV,'')) AS 'Beban_Kerja'

FROM    dbo.SPG_FORMASI_STRUKTURAL sfs
        LEFT JOIN dbo.SPG_08_JABATAN_STR j ON j.KD_JABATAN_STR = sfs.KD_JABATAN_STR
WHERE   sfs.JUMLAH_PEGAWAI = 0
        AND ( sfs.ALASAN_PERUBAHAN_PREV LIKE '%Promosi%'
              OR sfs.ALASAN_PERUBAHAN_PREV LIKE '%Demosi%'
              OR sfs.ALASAN_PERUBAHAN_PREV LIKE '%Mutasi%'
              OR sfs.ALASAN_PERUBAHAN_PREV LIKE '%Pensiun%'
            ))
			t
       
    END




GO
/****** Object:  StoredProcedure [dbo].[usp_getRoleEmail]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Yulius
-- Create date: 2019-10-03
-- Description:	Melakukan email-email untuk si stakeholder ticket
-- =============================================
-- [usp_getTicketEmail] 252,'Konsumen,PICPUJK'
CREATE PROCEDURE [dbo].[usp_getRoleEmail]
@roleCsv VARCHAR(200)

AS
BEGIN

DECLARE @emailCsv VARCHAR(4000) = ''
SELECT @emailCsv = @emailCsv + ',' + COALESCE(um.user_email,'') FROM dbo.FW_Ref_Role r
INNER JOIN (SELECT DISTINCT split FROM dbo.Split(',',@roleCsv) WHERE LTRIM(split) <> '') x ON r.role_id = x.split
INNER JOIN dbo.FW_User_Role ur ON ur.role_id = r.role_id
INNER JOIN dbo.User_Master um ON um.user_id = ur.user_id
WHERE um.stsrc = 'A' AND ur.stsrc = 'A' AND r.stsrc = 'A'
AND COALESCE(um.user_email,'') <> ''


SELECT DISTINCT split AS email FROM dbo.Split(',',@emailCsv) WHERE LTRIM(split) <> ''


END







GO
/****** Object:  StoredProcedure [dbo].[usp_getSatker]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getSatker] 
AS
    BEGIN
	
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable
    (
      ID BIGINT ,
      Parent_ID BIGINT ,
      KD_ORG VARCHAR(50) ,
      KD_ORG_INDUK VARCHAR(50) ,
      Nama_ORG VARCHAR(500)
    )

INSERT  INTO #TempTable
        ( ID ,
          KD_ORG ,
          KD_ORG_INDUK ,
          Nama_ORG
        )
        SELECT  ROW_NUMBER() OVER ( ORDER BY ID ) ,
                KD_UNIT_ORG ,
                KD_UNIT_ORG_INDUK ,
                NM_UNIT_ORG
        FROM    dbo.SPG_08_UNIT_ORGANISASI


UPDATE  r
SET     r.Parent_ID = s.ID
FROM    #TempTable r
        JOIN #TempTable s ON r.KD_ORG_INDUK = s.KD_ORG

SELECT  *
FROM    #TempTable
    END


GO
/****** Object:  StoredProcedure [dbo].[usp_getSimfosiaWebService]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 22 Desember 2020
-- Description:	Untuk Generate Excel
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getSimfosiaWebService]
AS
    BEGIN

        SELECT  ROW_NUMBER() OVER ( ORDER BY WSV_CLIENT.WSC_ID ) AS No ,
                CLIENT_NAME AS Aplikasi ,
                METHOD AS Modul ,
                CLIENT_PIN AS PIN
        FROM    dbo.WSV_CLIENT
                LEFT JOIN dbo.WSV_METHOD_ACCESS ON WSV_METHOD_ACCESS.WSC_ID = WSV_CLIENT.WSC_ID
    END	

GO
/****** Object:  StoredProcedure [dbo].[usp_getSpParameter]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_getSpParameter]
@spName VARCHAR(500)
AS 
SELECT 'Parameter_name' = REPLACE(name, '@', '')
FROM sys.parameters
WHERE object_id = OBJECT_ID(@spName);

GO
/****** Object:  StoredProcedure [dbo].[usp_getUserAccess]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 25 Agustus 2020
-- Description:	Untuk akses User
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_getUserAccess]
    @menuKeterangan VARCHAR(255) = NULL ,
    @nip VARCHAR(255) = NULL ,
    @accessType VARCHAR(50) = NULL
AS
    BEGIN
        DECLARE @noUrut NUMERIC = NULL
        DECLARE @fua VARCHAR(255) = NULL

        SET @noUrut = ( SELECT TOP 1
                                MEN_NOMORURUT
                        FROM    dbo.WF_MENU
                        WHERE   MEN_KETERANGAN = @menuKeterangan
                      )
        SET @fua = ( SELECT TOP 1
                            FUA_NAME
                     FROM   dbo.TBL_USER
                     WHERE  LOGIN = @nip
                   )

        SELECT  canAccess = ISNULL(CASE WHEN LOWER(@accessType) = 'add' THEN FUA_ADD
                                 WHEN LOWER(@accessType) = 'read'
                                 THEN FUA_READ
                                 WHEN LOWER(@accessType) = 'edit'
                                 THEN FUA_EDIT
                                 WHEN LOWER(@accessType) = 'delete'
                                 THEN FUA_DELETE
                            END,0)
        FROM    dbo.WF_FUNCTIONACCESS
		WHERE MEN_NOMORURUT = @noUrut AND FUA_NAME =@fua

    END	

GO
/****** Object:  StoredProcedure [dbo].[usp_InsertFormasiEfektif]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Tomi>
-- Create date: <16 Februari 2021>
-- Description:	<Untuk Generate Data Formasi>
-- =============================================
CREATE PROCEDURE [dbo].[usp_InsertFormasiEfektif] 
AS
BEGIN


INSERT  INTO dbo.SPG_Formasi_Efektif
        ( mpp_id ,
          fe_is_finalized ,
          kd_jabatan_str ,
          fe_jumlah ,
          kd_unit_org ,
          stsrc ,
          created_by ,
          date_created 
        )
        SELECT DISTINCT  r.MPP_ID ,
                0 ,
                j.KD_JABATAN ,
                ISNULL(1, 0) ,
                j.KD_UNIT_ORG ,
                'A' ,
				'usp_InsertFormasiEfektif',
                GETDATE()               
        FROM    dbo.SPG_08_JABATAN_UNIT j
				INNER JOIN dbo.SPG_08_UNIT_ORGANISASI o ON o.KD_UNIT_ORG = j.KD_UNIT_ORG AND GETDATE() >= o.TGL_AKTIF AND ( GETDATE()<= o.TGL_END OR o.TGL_END IS NULL or o.TGL_END ='1900-01-01')
				INNER JOIN dbo.SPG_08_JABATAN_STR s ON s.KD_JABATAN_STR = j.KD_JABATAN AND GETDATE() >= s.TGL_AKTIF AND ( GETDATE()<= s.TGL_END OR s.TGL_END IS NULL or o.TGL_END ='1900-01-01')
                LEFT JOIN ( SELECT  COUNT(*) AS Jumlah ,
                                    KD_JABATAN_STR ,
                                    KD_UNIT_ORG
                            FROM    dbo.SPG_DATA_CURRENT
                            WHERE   ISNULL(KD_UNIT_ORG, '') <> ''
                                    AND STS_PENSIUN <> '1'
                                    AND ISNULL(KD_JABATAN_STR, '') <> ''
                            GROUP BY KD_JABATAN_STR ,
                                    KD_UNIT_ORG
                          ) a ON a.KD_JABATAN_STR = j.KD_JABATAN AND a.KD_JABATAN_STR IS NOT NULL
                                 AND a.KD_UNIT_ORG = j.KD_UNIT_ORG AND a.KD_UNIT_ORG IS NOT NULL
                CROSS JOIN dbo.REC_PERENCANAAN_PEGAWAI r
        WHERE   NOT EXISTS ( SELECT *
                             FROM   dbo.SPG_Formasi_Efektif sfe
                             WHERE  sfe.mpp_id = r.MPP_ID
                                    AND sfe.kd_jabatan_str = j.KD_JABATAN
                                    AND sfe.kd_unit_org = j.KD_UNIT_ORG )
                AND ISNULL(j.KD_UNIT_ORG, '') <> ''
                AND ISNULL(j.KD_JABATAN, '') <> ''
                AND j.KD_JABATAN IN ( SELECT    KD_JABATAN_STR
                                      FROM      dbo.SPG_08_JABATAN_STR
                                      WHERE     (JENIS LIKE '%fungsional%' 
									  OR KD_JABATAN_STR IN  ('501','502','503') )
									  AND NM_JABATAN_STR NOT LIKE '%setingkat%' 
									  )
                AND r.PERIODE = YEAR(GETDATE())
END
GO
/****** Object:  StoredProcedure [dbo].[usp_insertHistoryRegional]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Tomi
-- Create date: 6 Juni 2020
-- Description:	Untuk Man Power Planning
-- =============================================
CREATE	 PROCEDURE [dbo].[usp_insertHistoryRegional] 
AS
    BEGIN
	
	INSERT INTO dbo.History_YKP_REGIONAL_KOTA
	        ( RKT_ID ,
	          REGIONALCODE ,
	          REGIONALNAME ,
	          PROVINSINAME ,
	          PARENTREGIONAL ,
	          PARENTKOTA ,
	          KOTACODE ,
	          KOTANAME ,
	          SIM_KD_PROPINSI ,
	          SIM_KD_KABUPATEN ,
	          SIM_KD_KECAMATAN ,
	          SIM_KD_KELURAHAN ,
	          KETERANGAN ,
	          TGL_BUAT ,
	          NAMA_BUAT ,
	          TGL_UBAH ,
	          NAMA_UBAH
	        )
	SELECT  RKT_ID ,
	          REGIONALCODE ,
	          REGIONALNAME ,
	          PROVINSINAME ,
	          PARENTREGIONAL ,
	          PARENTKOTA ,
	          KOTACODE ,
	          KOTANAME ,
	          SIM_KD_PROPINSI ,
	          SIM_KD_KABUPATEN ,
	          SIM_KD_KECAMATAN ,
	          SIM_KD_KELURAHAN ,
	          KETERANGAN ,
	          TGL_BUAT ,
	          NAMA_BUAT ,
	          TGL_UBAH ,
	          NAMA_UBAH
	FROM dbo.YKP_REGIONAL_KOTA

	DELETE dbo.YKP_REGIONAL_KOTA

    END


GO
/****** Object:  StoredProcedure [dbo].[usp_list_pegawai_satker_lintas_bidang]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_list_pegawai_satker_lintas_bidang]
	@login_nip varchar(10)
AS
BEGIN
	SET NOCOUNT ON;

    CREATE TABLE #hasil(nip VARCHAR(10) NULL, nm_peg VARCHAR(225) NULL)
	DECLARE 
		@kd_unit_substring VARCHAR(24),
		@length_substring int

	--SET @login_nip='01845'

	DECLARE list_satker CURSOR FOR 
	select DISTINCT
	case c.kd_eselon 
		when '00' then LEFT(b.kode_satker_organisasi, 3)
		when '10' then b.kode_satker_organisasi
		when '11' then LEFT(b.kode_satker_organisasi, 9)
		when '20' then LEFT(b.kode_satker_organisasi, 12)
		when '21' then LEFT(b.kode_satker_organisasi, 15)
	end as kd_unit_substring,
	case c.kd_eselon 
		when '00' then 3
		when '10' then 24
		when '11' then 9
		when '20' then 12
		when '21' then 15
	end as length_substring
	from SPG_ADMIN_SATKER a
	inner join MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 b on a.KD_UNIT_ORG=b.KD_UNIT_ORG
	inner join MARP_UNIT_ORGANISASI_ACTIVE_ONLY_v2 c on b.kode_satker_organisasi=c.kd_unit_org
	where (a.operator like '%'+@login_nip+'%' or a.validator like '%'+@login_nip+'%')

	OPEN list_satker
		FETCH NEXT FROM list_satker INTO @kd_unit_substring,@length_substring

			WHILE @@FETCH_STATUS = 0 BEGIN

			INSERT INTO #hasil (nip,nm_peg)
			select nip,nm_peg from SPG_DATA_CURRENT where sts_pensiun='0' and LEFT(kd_unit_org,@length_substring)=@kd_unit_substring

				FETCH NEXT FROM list_satker INTO @kd_unit_substring,@length_substring
			END

	CLOSE list_satker
	DEALLOCATE list_satker

	SELECT nip FROM #hasil

	IF OBJECT_ID('tempdb..#hasil', 'U') IS NOT NULL
		DROP TABLE #hasil;
	END
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookAddAktivitas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Created By Lius 18-05-2022
CREATE PROC [dbo].[usp_LogbookAddAktivitas] --'Liusrizaldi@gmail.com','192.8.1.1','TestSubject','Deskripsi','2022-05-18 09:30:12.000','2022-05-18 13:28:13.124'
@InitiatorUser varchar(200),  
@UserIpAddress varchar(100),
@Subject varchar(150),
@Descriptions varchar(max),
@StartDate datetime,
@EndDate datetime,
@StatusBekerja varchar(3),
@Status varchar(1)
AS  
  
declare @LinkedServerName as varchar(100)
declare @DbName as varchar(100)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @query= 'insert into ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask] (
					InitiatorUser, 
					UserIpAddress, 
					Subject, 
					Descriptions, 
					StartDate, 
					EndDate, 
					Status,
					StatusBekerja, 
					CreatedBy, 
					CreatedDatetime
				)
				values (
					'''+@InitiatorUser+''', 
					'''+@UserIpAddress+''', 
					'''+@Subject+''', 
					'''+@Descriptions+''', 
					'''+cast(@StartDate as varchar)+''', 
					'''+cast(@EndDate as varchar)+''',
					'''+@Status+''',
					'''+@StatusBekerja+''', 
					'''+@InitiatorUser+''', 
					getdate()
				)'

--set @query= 'insert into ['+@DbName+'].dbo.[UserTask] (InitiatorUser, UserIpAddress, Subject, Descriptions, StartDate, EndDate, StatusBekerja)
--				values ('''+@InitiatorUser+''', '''+@UserIpAddress+''', '''+@Subject+''', '''+@Descriptions+''', '''+cast(@StartDate as varchar)+''', '''+cast(@EndDate as varchar)+''', '''+@StatusBekerja+'''),'

--print @query
exec (@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookAddAssessment]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--CREATED BY DIKI 23-05-2022

CREATE PROC [dbo].[usp_LogbookAddAssessment]
@Username varchar(200),
@Ans1 int,
@Ans2 int,
@Ans3 int,
@Ans4 int,
@Ans5 int,
@Ans6 int,
@Date datetime,
@StatusBekerja varchar(3)
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

SET @query = 'INSERT INTO ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessment]
				(UserEmail, TglKerja, StatusBekerja, Ans1, Ans2, Ans3, Ans4, Ans5, Ans6, CreatedBy, 
				CreatedDatetime)
			  VALUES ('''+@Username+''',
					  '''+cast(@Date as varchar)+''',
					  '''+@StatusBekerja+''',
					  '''+cast(@Ans1 as varchar)+''',
					  '''+cast(@Ans2 as varchar)+''',
					  '''+cast(@Ans3 as varchar)+''',
					  '''+cast(@Ans4 as varchar)+''',
					  '''+cast(@Ans5 as varchar)+''',
					  '''+cast(@Ans6 as varchar)+''',
					  '''+@Username+''',
					  '''+cast(getdate() as varchar)+''')'

--SET @query = 'INSERT INTO ['+@DbName+'].dbo.[UserAssessment]
--				(UserEmail, TglKerja, StatusBekerja, Ans1, Ans2, Ans3, Ans4, Ans5, Ans6, CreatedBy, 
--				CreatedDatetime)
--			  VALUES ('''+@Username+''',
--					  '''+cast(@Date as varchar)+''',
--					  '''+@StatusBekerja+''',
--					  '''+cast(@Ans1 as varchar)+''',
--					  '''+cast(@Ans2 as varchar)+''',
--					  '''+cast(@Ans3 as varchar)+''',
--					  '''+cast(@Ans4 as varchar)+''',
--					  '''+cast(@Ans5 as varchar)+''',
--					  '''+cast(@Ans6 as varchar)+''',
--					  '''+@Username+''',
--					  '''+cast(getdate() as varchar)+''')'

exec(@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDeleteAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Created by diky 23-05-2022

CREATE PROC [dbo].[usp_LogbookDeleteAktifitas]
@TaskId int,
@InitiatorUser varchar
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @field as varchar(1000)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @field = 'TaskId, InitiatorUser, UserIpAddress, Subject, Descriptions, Location, StartDate, EndDate, 
				disabled, Status, ApprovedBy, ApprovedDatetime, Priority, RecurrenceRule, RecurrenceException, 
				AllDay, CategoryId, StatusBekerja, CreatedBy, CreatedDatetime, UpdatedBy, UpdatedDatetime'

set @query = 'INSERT INTO ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTaskHist] ('+@field+')
			  SELECT '+@field+' 
				FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask]
				WHERE TaskId='+cast(@TaskId as varchar)+';
			  
			  DELETE FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask] WHERE TaskId='+cast(@TaskId as varchar)

--set @query = 'INSERT INTO ['+@DbName+'].dbo.[UserTaskHist] ('+@field+')
--			  SELECT '+@field+' 
--				FROM ['+@DbName+'].dbo.[UserTask]
--				WHERE TaskId='+cast(@TaskId as varchar)+';
			  
--			  DELETE FROM ['+@DbName+'].dbo.[UserTask] WHERE TaskId='+cast(@TaskId as varchar)

exec(@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDeleteAssessment]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Created by diki 23-05-2022

CREATE PROC [dbo].[usp_LogbookDeleteAssessment]
@UserEmail as varchar(100),
@AssessmentId as int
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @field as varchar(1000)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @field = 'AssessmentId, UserEmail, TglKerja, StatusBekerja, Ans1, Ans2, Ans3, Ans4, Ans5, Ans6, CreatedBy, CreatedDatetime'

set @query = 'INSERT INTO ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessmentHist] ('+@field+')
			  SELECT '+@field+' 
				FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessment]
				WHERE AssessmentId='+cast(@AssessmentId as varchar)+';
			  
			  DELETE FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessment] 
			  WHERE AssessmentId='+cast(@AssessmentId as varchar)

--set @query = 'INSERT INTO ['+@DbName+'].dbo.[UserAssessmentHist] ('+@field+')
--			  SELECT '+@field+' 
--				FROM ['+@DbName+'].dbo.[UserAssessment]
--				WHERE AssessmentId='+cast(@AssessmentId as varchar)+';
			  
--			  DELETE FROM ['+@DbName+'].dbo.[UserAssessment] 
--			  WHERE AssessmentId='+cast(@AssessmentId as varchar)

exec(@query)

GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDetailAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Created By Diky 2022-20-05
CREATE PROC [dbo].[usp_LogbookDetailAktifitas]
@TaskId int,
@InitiatorUser varchar(200)
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @query as varchar(1000)


set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @query= 'SELECT InitiatorUser as username, TaskId as task_id, Subject as subject, Descriptions as descriptions, StartDate as jam_mulai, EndDate as jam_selesai, StatusBekerja as status_bekerja
			 FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask]
			 WHERE InitiatorUser='''+@InitiatorUser+''' and TaskId = '+cast(@TaskID as varchar)

--set @query= 'SELECT InitiatorUser as username, TaskId as task_id, Subject as subject, Descriptions as descriptions, StartDate as jam_mulai, EndDate as jam_selesai, StatusBekerja as status_bekerja
--			  FROM ['+@DbName+'].dbo.[UserTask]
--			  WHERE TaskId = '+cast(@TaskID as varchar)

exec(@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookDetailAssessment]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--created by diki 30-05-2022

CREATE PROC [dbo].[usp_LogbookDetailAssessment]
@Username varchar(100),
@Date datetime
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @query = 'SELECT*FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessment]
				WHERE UserEmail='''+@Username+'''
				AND YEAR(TglKerja)='+CAST(YEAR(@Date) as varchar)+'
				AND MONTH(TglKerja)='+CAST(MONTH(@Date) as varchar)+'
				AND DAY(TglKerja)='+CAST(DAY(@Date) as varchar)

--print(@query)
exec(@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookEditAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Created By Diky 20-05-2022
CREATE PROC [dbo].[usp_LogbookEditAktifitas]
@TaskID int,
@InitiatorUser varchar(200),
@Subject varchar(100),
@Description varchar(max),
@StartDate datetime,
@EndDate datetime,
@StatusBekerja varchar(3)
AS  

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @query as varchar(1000)


set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @query= 'UPDATE ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask]
			 SET 
				Subject='''+cast(@Subject as varchar(max))+''', 
				Descriptions='''+cast(@Description as varchar(max))+''', 
				StartDate='''+cast(@StartDate as varchar)+''', 
				EndDate='''+cast(@EndDate as varchar)+''',
				StatusBekerja='''+@StatusBekerja+''',
				UpdatedBy='''+@InitiatorUser+''',
				UpdatedDatetime=getdate()
			  WHERE TaskId='+cast(@TaskID as varchar)

--set @query= 'UPDATE ['+@DbName+'].dbo.[UserTask]
--			 SET 
--				Subject='''+cast(@Subject as varchar)+''', 
--				Descriptions='''+cast(@Description as varchar)+''', 
--				StartDate='''+cast(@StartDate as varchar)+''', 
--				EndDate='''+cast(@EndDate as varchar)+'''
--			  WHERE TaskId='+cast(@TaskID as varchar)

--print @query
exec (@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookEditAssessment]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Created by diky 23-05-2022

CREATE PROC [dbo].[usp_LogbookEditAssessment]
@AssessmentId int,
@Username varchar(200),
@Ans1 int,
@Ans2 int,
@Ans3 int,
@Ans4 int,
@Ans5 int,
@Ans6 int,
@Date datetime,
@StatusBekerja varchar(3)
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100), @db as varchar(100)
declare @field as varchar(1000)
declare @query as varchar(1000)

set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @db = '['+@LinkedServerName+'].['+@DbName+'].dbo.[UserAssessment]'
--set @db = '['+@DbName+'].dbo.[UserAssessment]'

set @query = 'UPDATE '+@db+'
			  SET Ans1 = '''+cast(@Ans1 as varchar)+''',
				  Ans2 = '''+cast(@Ans2 as varchar)+''',
				  Ans3 = '''+cast(@Ans3 as varchar)+''',
				  Ans4 = '''+cast(@Ans4 as varchar)+''',
				  Ans5 = '''+cast(@Ans5 as varchar)+''',
				  Ans6 = '''+cast(@Ans6 as varchar)+''',
				  StatusBekerja = '''+@StatusBekerja+''',
				  UpdatedBy = '''+@Username+''',
				  UpdatedDatetime = '''+cast(getdate() as varchar)+'''
			  WHERE AssessmentId = '+cast(@AssessmentId as varchar)
--print(@query)
exec(@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_LogbookListAktifitas]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Created By Diky 20-05-2022
CREATE PROC [dbo].[usp_LogbookListAktifitas]
@InitiatorUser varchar(200),
@StartDate datetime,
@EndDate datetime
AS

declare @LinkedServerName as varchar(100), @DbName as varchar(100)
declare @query as varchar(1000)


set @LinkedServerName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_LinkedServerName')
set @DbName = (select set_value from FW_Ref_Setting where set_name='LOGBOOK_DbName')

set @query= 'SELECT InitiatorUser as username, TaskId as task_id, Subject as subject, StartDate as jam_mulai, EndDate as jam_selesai
			 FROM ['+@LinkedServerName+'].['+@DbName+'].dbo.[UserTask]
			 WHERE InitiatorUser = '''+@InitiatorUser+''' AND (StartDate BETWEEN '''+cast(@StartDate as varchar)+''' AND '''+cast(@EndDate as varchar)+''')'

--set @query= 'SELECT InitiatorUser as username, TaskId as task_id, Subject as subject, StartDate as jam_mulai, EndDate as jam_selesai
--			 FROM ['+@DbName+'].dbo.[UserTask]
--			 WHERE InitiatorUser = '''+@InitiatorUser+''' AND (StartDate BETWEEN '''+cast(@StartDate as varchar)+''' AND '''+cast(@EndDate as varchar)+''')'

--print @query
exec (@query)
GO
/****** Object:  StoredProcedure [dbo].[usp_NonAktifCLTO]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*
Created By : Angga
Date Created : 24 Agustus 2020
desc : untuk CLTO, pegawai dinonaktifkan otomatis
*/
create PROC [dbo].[usp_NonAktifCLTO]
@NIP VARCHAR(20),
@LoginNip VARCHAR(20),
@TglMulai DATE,
@TglAkhir DATE
AS
BEGIN
		INSERT INTO dbo.SPG_PEGAWAI_PENSIUN
				( NIP ,
					NO_SK ,
					KET_PENSIUN ,
					KD_TIDAK_AKTIF ,
					JNS_STS_KEAKTIFAN ,
					NAMA_BUAT ,
					TANGGAL_BUAT ,
					TMT_PENSIUN ,
					tgl_akhir
				)
		VALUES  ( @NIP , -- NIP - char(18)
					'' , -- NO_SK - varchar(50)
					'Cuti di luar Tanggungan OJK' , -- KET_PENSIUN - varchar(300)
					'CLTO' , -- KD_TIDAK_AKTIF - varchar(5)
					0 , -- JNS_STS_KEAKTIFAN - int
					@LoginNip , -- NAMA_BUAT - varchar(18)
					GETDATE() , -- TANGGAL_BUAT - date
					@TglMulai , -- tgl_mulai - date
					@TglAkhir -- tgl_akhir - date
				)
END
GO
/****** Object:  StoredProcedure [dbo].[usp_rpt_Get_Master_Kategori]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_rpt_Get_Master_Kategori] AS
SELECT * FROM dbo.Master_Kategori WHERE stsrc = 'A'
GO
/****** Object:  StoredProcedure [dbo].[usp_test]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_test]
AS
SELECT 'AS' AS tes
GO
/****** Object:  StoredProcedure [dbo].[usp_UpdateTglEndRiwayatJabatan]    Script Date: 29/03/2023 10:07:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Created By Lius 2022-09-30
CREATE PROC [dbo].[usp_UpdateTglEndRiwayatJabatan] @nip varchar(20)
AS
BEGIN
SELECT* INTO #Temp_SPG_08_RIWAYAT_JABATAN
FROM(
SELECT ROW_NUMBER() OVER (
      PARTITION BY NIP
      ORDER BY TANGGAL_RIWAYAT, ID
   ) urutan, NIP, TANGGAL_RIWAYAT, TANGGAL_RIWAYAT_END, ID_RIWAYAT_JABATAN
FROM SPG_08_RIWAYAT_JABATAN
WHERE NIP = @nip)a

UPDATE SPG_08_RIWAYAT_JABATAN
SET TANGGAL_RIWAYAT_END= COALESCE(
							CASE WHEN c.TANGGAL_RIWAYAT=b.TANGGAL_RIWAYAT
								THEN c.TANGGAL_RIWAYAT
								ELSE DATEADD(Day, -1, c.TANGGAL_RIWAYAT)
							END, d.TMT_PENSIUN)
FROM SPG_08_RIWAYAT_JABATAN a
INNER JOIN #Temp_SPG_08_RIWAYAT_JABATAN b on a.ID_RIWAYAT_JABATAN=b.ID_RIWAYAT_JABATAN
LEFT JOIN #Temp_SPG_08_RIWAYAT_JABATAN c on b.NIP=c.NIP and b.urutan+1=c.urutan
LEFT JOIN SPG_DATA_CURRENT d on b.NIP=d.NIP and d.sts_pensiun='1'
WHERE a.NIP = @nip 
AND a.TANGGAL_RIWAYAT_END IS NULL

DROP TABLE #Temp_SPG_08_RIWAYAT_JABATAN
END
GO
