<?php
$sql['count_inventarisasi_detail']="
SELECT COUNT(invDetId) AS total
FROM inventarisasi_detail
";

# ============ FIRST CONVERSION (FC_) ================ #

$sql['FC_delete_all_data']="
DELETE FROM do_detail_new;
ALTER TABLE do_detail_new AUTO_INCREMENT = 1;
DELETE FROM do_mst_new;
ALTER TABLE do_mst_new AUTO_INCREMENT = 1;
DELETE FROM pengendalian_barang_det;
ALTER TABLE pengendalian_barang_det AUTO_INCREMENT = 1;
DELETE FROM aset_perbaikan_det;
ALTER TABLE aset_perbaikan_det AUTO_INCREMENT = 1;
DELETE FROM aset_lelang_det;
ALTER TABLE aset_lelang_det AUTO_INCREMENT = 1;
DELETE FROM mutasi_aset_det;
ALTER TABLE mutasi_aset_det AUTO_INCREMENT = 1;
DELETE FROM mutasi_aset_mst;
ALTER TABLE mutasi_aset_mst AUTO_INCREMENT=1;
DELETE FROM aset_sewa_det;
ALTER TABLE aset_sewa_det AUTO_INCREMENT = 1;
DELETE FROM aset_sewa_pengembalian_detil;
ALTER TABLE aset_sewa_pengembalian_detil AUTO_INCREMENT = 1;
DELETE FROM aset_ref_biaya_sewa;
ALTER TABLE aset_ref_biaya_sewa AUTO_INCREMENT = 1;
DELETE FROM penyusutan_det;
ALTER TABLE penyusutan_det AUTO_INCREMENT = 1;
DELETE FROM do_detail_new;
ALTER TABLE do_detail_new AUTO_INCREMENT = 1;
-- noted
DELETE FROM inventarisasi_detail;
ALTER TABLE inventarisasi_detail AUTO_INCREMENT = 1;
DELETE FROM inventarisasi_mst;
ALTER TABLE inventarisasi_mst AUTO_INCREMENT = 1;
DELETE FROM pengadaan_detail;
ALTER TABLE pengadaan_detail AUTO_INCREMENT = 1;
DELETE FROM pengadaan_detail_rekap_usulan;
ALTER TABLE pengadaan_detail_rekap_usulan AUTO_INCREMENT = 1;
DELETE FROM pengadaan_panitia;
ALTER TABLE pengadaan_panitia AUTO_INCREMENT = 1;
DELETE FROM do_mst_new;
ALTER TABLE do_mst_new AUTO_INCREMENT = 1;
DELETE FROM pengadaan_mst;
ALTER TABLE pengadaan_mst AUTO_INCREMENT = 1;
DELETE FROM aset_ruang_penggunaan;
ALTER TABLE aset_ruang_penggunaan AUTO_INCREMENT = 1;
DELETE FROM log_atk;
ALTER TABLE log_atk AUTO_INCREMENT = 1;
DELETE FROM inv_atk_gudang;
ALTER TABLE inv_atk_gudang AUTO_INCREMENT = 1;
DELETE FROM pengendalian_barang_mst;
ALTER TABLE pengendalian_barang_mst AUTO_INCREMENT = 1;
DELETE FROM aset_ref_sewa_ruang;
ALTER TABLE aset_ref_sewa_ruang AUTO_INCREMENT = 1;
DELETE FROM setting_stok_atk;
ALTER TABLE setting_stok_atk AUTO_INCREMENT = 1;
DELETE FROM ruang;
ALTER TABLE ruang AUTO_INCREMENT = 1;
DELETE FROM aset_lantai;
ALTER TABLE aset_lantai AUTO_INCREMENT = 1;
DELETE FROM gedung;
ALTER TABLE gedung AUTO_INCREMENT = 1;
DELETE FROM pic_ref;
ALTER TABLE pic_ref AUTO_INCREMENT = 1;
DELETE FROM usulan_det_brg_rkp;
ALTER TABLE usulan_det_brg_rkp AUTO_INCREMENT = 1;
DELETE FROM usulan_brg_det;
ALTER TABLE usulan_brg_det AUTO_INCREMENT = 1;
DELETE FROM inv_atk_gudang;
ALTER TABLE inv_atk_gudang AUTO_INCREMENT = 1;
DELETE FROM inv_atk_brg;
ALTER TABLE inv_atk_brg AUTO_INCREMENT = 1;
DELETE FROM aset_transaksi_atk_detil;
ALTER TABLE aset_transaksi_atk_detil AUTO_INCREMENT = 1;
DELETE FROM inv_atk_master;
ALTER TABLE inv_atk_master AUTO_INCREMENT = 1;
DELETE FROM inv_atk;
ALTER TABLE inv_atk AUTO_INCREMENT = 1;
DELETE FROM barang_ref;
ALTER TABLE barang_ref AUTO_INCREMENT = 1;
DELETE FROM sub_kelompok_barang_ref;
ALTER TABLE sub_kelompok_barang_ref AUTO_INCREMENT = 1;
DELETE FROM kelompok_barang_ref;
ALTER TABLE kelompok_barang_ref AUTO_INCREMENT = 1;
DELETE FROM bidang_barang_ref;
ALTER TABLE bidang_barang_ref AUTO_INCREMENT = 1;
DELETE FROM golongan_barang_ref;
ALTER TABLE golongan_barang_ref AUTO_INCREMENT = 1;
DELETE FROM usulan_brg_mst;
ALTER TABLE usulan_brg_mst AUTO_INCREMENT = 1;
DELETE FROM satuan_barang_ref;
ALTER TABLE satuan_barang_ref AUTO_INCREMENT = 1;
--
DELETE FROM aset_pb_pengadaan_brg_detil;
ALTER TABLE aset_pb_pengadaan_brg_detil AUTO_INCREMENT = 1;
DELETE FROM aset_pb_usulan_brg;
ALTER TABLE aset_pb_usulan_brg AUTO_INCREMENT = 1;
DELETE FROM aset_sewa_pengembalian;
ALTER TABLE aset_sewa_pengembalian AUTO_INCREMENT = 1;
DELETE FROM aset_sewa;
ALTER TABLE aset_sewa AUTO_INCREMENT = 1;
DELETE FROM aset_kdp_ref_jenis_kdp;
ALTER TABLE aset_kdp_ref_jenis_kdp AUTO_INCREMENT = 1;
DELETE FROM aset_kdp_trans_kdp;
ALTER TABLE aset_kdp_trans_kdp AUTO_INCREMENT = 1;
DELETE FROM aset_kdp_ref_pembangunan_cara;
ALTER TABLE aset_kdp_ref_pembangunan_cara AUTO_INCREMENT = 1;
DELETE FROM aset_lelang;
ALTER TABLE aset_lelang AUTO_INCREMENT = 1;
DELETE FROM aset_perbaikan;
ALTER TABLE aset_perbaikan AUTO_INCREMENT = 1;
DELETE FROM bhp_mutasi_mst;
ALTER TABLE bhp_mutasi_mst AUTO_INCREMENT = 1;
DELETE FROM bhp_usulan_brg;
ALTER TABLE bhp_usulan_brg AUTO_INCREMENT = 1;
DELETE FROM aset_transaksi_atk;
ALTER TABLE aset_transaksi_atk AUTO_INCREMENT = 1;
-- noted
DELETE FROM gtfw_group WHERE GroupId > 2;
ALTER TABLE gtfw_group AUTO_INCREMENT = 1;
DELETE FROM gtfw_unit WHERE UnitId > 1;
ALTER TABLE gtfw_unit AUTO_INCREMENT = 1;
ALTER TABLE unit_kerja_ref ADD INDEX(unitkerjaKode);
DELETE FROM unit_kerja_ref WHERE unitkerjaId > 1;
ALTER TABLE unit_kerja_ref AUTO_INCREMENT = 2
;";

$sql['FC_insert_golongan_barang_ref']='
INSERT INTO golongan_barang_ref(
   golbrgId,
   golbrgCoaId,
   golbrgKode,
   golbrgKibKode,
   golbrgNama
)
SELECT
   kd_gol AS golbrgId, 
   kd_gol AS golbrgCoaId,
   kd_gol AS golbrgKode,
   CASE
      WHEN kd_gol=1 THEN "c" 
      WHEN kd_gol=2 THEN "a" 
      WHEN kd_gol=3 THEN "b" 
      WHEN kd_gol=4 THEN "d" 
      WHEN kd_gol=5 THEN "e" 
      WHEN kd_gol=6 THEN "b" 
      WHEN kd_gol=7 THEN "g" 
      WHEN kd_gol=8 THEN "h" 
      WHEN kd_gol=9 THEN "i" 
      WHEN kd_gol=10 THEN "j" 
   END AS golbrgKibKode,
   CONCAT("KIB ",
      CASE
      WHEN kd_gol=1 THEN "A" 
      WHEN kd_gol=2 THEN "B" 
      WHEN kd_gol=3 THEN "C" 
      WHEN kd_gol=4 THEN "D" 
      WHEN kd_gol=5 THEN "E" 
      WHEN kd_gol=6 THEN "F" 
      WHEN kd_gol=7 THEN "G" 
      WHEN kd_gol=8 THEN "H" 
      WHEN kd_gol=9 THEN "I" 
      WHEN kd_gol=10 THEN "J" 
      END,": ", ur_gol
   ) AS golbrgNama
FROM `gtaset_dbbmn`.t_gol';

$sql['FC_insert_bidang_barang_ref']='
INSERT INTO bidang_barang_ref(
   bidangbrgId,
   bidangbrgKode,
   bidangbrgNama,
   bidangbrgGolbrgId
)
SELECT 
   kd_bidbrg AS bidangbrgId, 
   kd_bid AS bidangbrgKode,
   TRIM(UCASE(ur_bid)) AS bidangbrgNama,
   kd_gol AS bidangbrgGolbrgId -- golongan_barang_ref
FROM `gtaset_dbbmn`.t_bid 
ORDER BY ur_bid
;';

$sql['FC_insert_kelompok_barang_ref']="
INSERT INTO kelompok_barang_ref(
   kelbrgId,   
   kelbrgKode,
   kelbrgNama,
   kelbrgBidangbrgId
)
SELECT 
   kd_kelbrg AS kelbrgId,
   kd_kel AS kelbrgKode,
   TRIM(UCASE(ur_kel)) AS kelbrgNama,
   CONCAT(kd_gol, kd_bid)AS kelbrgBidangbrgId -- bidang_barang_ref
FROM `gtaset_dbbmn`.t_kel
ORDER BY ur_kel
;";

$sql['FC_insert_sub_kelompok_barang_ref']="
INSERT INTO sub_kelompok_barang_ref(
   subkelbrgId,
   subkelbrgKode,
   subkelbrgNama,
   subkelbrgKelbrgId
)
SELECT 
   kd_skelbrg AS subkelbrgId,
   kd_skel AS subkelbrgKode,
   TRIM(UCASE(ur_skel)) AS subkelbrgNama,
   CONCAT(kd_gol, kd_bid, kd_kel) AS subkelbrgKelbrgId -- kelompok_barang_ref
FROM `gtaset_dbbmn`.t_skel
ORDER BY ur_skel
;";

$sql['FC_insert_satuan_barang_ref']='
ALTER TABLE `gtaset_dbbmn`.t_sskel CONVERT TO CHARACTER SET utf8;
INSERT INTO satuan_barang_ref(
   satuanbrgNama
)
SELECT 
   satuan AS satuanbrgNama
FROM(
   SELECT 
   satuan
   FROM `gtaset_dbbmn`.t_sskel
   WHERE satuan != ""
   GROUP BY satuan
) AS t_sskel
WHERE satuan NOT IN (
   SELECT satuanbrgNama FROM satuan_barang_ref
);';

$sql['FC_insert_barang_ref']='
INSERT INTO barang_ref(
barangId,
barangKode,
barangNama,
barangJenisbrgId,
barangSatuanbrgId,
barangSubkelbrgId,
barangHps,
barangFoto,
barangUmurEkonomis,
barangNilaiResidu
)
SELECT 
kd_brg AS barangId,
kd_sskel AS barangKode,
ur_sskel AS barangNama,
IF(kd_gol IN (1), "2", "1") AS barangJenisbrgId, -- jenis_barang_ref, 1 = aset, 2 = Habis Pakai
satuanbrgId AS barangSatuanbrgId, -- satuan_barang_ref
CONCAT(kd_gol, kd_bid, kd_kel, kd_skel) AS barangSubkelbrgId, -- sub_kelompok_barang_ref
"1" AS barangHps,
NULL AS barangFoto,
IF(kd_gol IN (3,6), "60", "1") AS barangUmurEkonomis,
"1" AS barangNilaiResidu
FROM `gtaset_dbbmn`.t_sskel -- 11728
LEFT JOIN satuan_barang_ref ON satuan = satuanbrgNama
;';

$sql['FC_insert_unit_kerja_ref']='
UPDATE unit_kerja_ref
SET 
unitkerjaId = 1,
unitkerjaKodeSistem = 1,
unitkerjaKode = "000",
unitkerjaNama = "[UNIV_NAME]",
unitkerjaParentId = 0;
--
INSERT INTO unit_kerja_ref (
#unitkerjaId, -- auto increamen
unitkerjaKodeSistem,
unitkerjaKode, --
unitkerjaNama,
unitkerjaNamaPimpinan,
unitkerjaTipeunitId,
unitKerjaUnitStatusId,
unitkerjaParentId --
)
SELECT
#kd_subupb as unitkerjaId,
NULL AS unitkerjaKodeSistem,
kd_subupb AS unitkerjaKode,
ur_upb AS unitkerjaNama,
NULL AS unitkerjaNamaPimpinan,
"1" AS unitkerjaTipeunitId,
IF(kd_subupb = 0, "1", "2") AS unitKerjaUnitStatusId, -- 1 : gedung pusat, 2 : fakultas
IF(kd_subupb = 0, "0", "1") unitkerjaParentId
FROM `gtaset_dbbmn`.t_upb
WHERE kd_subupb > 0
ORDER BY kd_subupb
;
--
UPDATE unit_kerja_ref SET unitkerjaKodeSistem = CONCAT("1.", unitkerjaId - 1) WHERE unitkerjaKode > 0
;';

$sql['FC_insert_gtfw_unit']='
UPDATE gtfw_unit
SET 
UnitName = "[UNIV_NAME]",
Description = NULL;
--
INSERT INTO gtfw_unit (
   UnitId, 
   UnitName,
   Description,
   ApplicationId,
   UnitUnitKerjaId
)
SELECT
unitkerjaId,
unitkerjaNama,
NULL,
ApplicationId, -- gtfw_application
unitkerjaId
FROM unit_kerja_ref
JOIN gtfw_application ON ApplicationName = "gtAset"
WHERE unitkerjaId NOT IN (
   SELECT UnitId FROM gtfw_unit
)
ORDER BY unitkerjaId
;';

$sql['FC_insert_gedung_t_ruang']='
ALTER TABLE gedung ADD INDEX(gedungKode);
ALTER TABLE `gtaset_dbbmn`.t_ruang CONVERT TO CHARACTER SET utf8;
--
INSERT INTO gedung(
gedungBarangId,
gedungKode,
gedungNama,
gedungSumberDanaId,
gedungNilaiResidu,
gedungNilaiPerolehan, 
gedungUmurEkonomis,
gedungSifatBangunan,
gedungIMB,
gedungPeruntukanBangunanId,
gedungKepemilikanBrgId,
gedungStatusPengBrgId,
gedungKondisiBrgId,
gedungKampusId,
gedungUnitkerjaId,
gedungTglUbah, 
gedungUserId
)
SELECT 
barangId AS gedungBarangId,
kd_lokasi AS gedungKode,
CONCAT("Gedung ",kd_lokasi) AS gedungNama,
sumberdanaId AS gedungSumberDanaId, -- sumber_barang_ref
"1" AS gedungNilaiResidu,
"1" AS gedungNilaiPerolehan, 
"1" AS gedungUmurEkonomis,
"Permanen" AS gedungSifatBangunan,
"Ya" AS gedungIMB,
peruntukanBangunanId AS gedungPeruntukanBangunanId, -- peruntukan_bangunan_ref
kepemilikanbrgId AS gedungKepemilikanBrgId, -- kepemilikan_barang_ref
statusPengBrgId AS gedungStatusPengBrgId, -- status_penguasaan_barang_ref
kondisibrgId AS gedungKondisiBrgId, -- kondisi_barang_ref
kampusId AS gedungKampusId, -- kampus_ref
unitkerjaId AS gedungUnitkerjaId, -- unit_kerja_ref
NOW() AS gedungTglUbah,
UserId AS gedungUserId
FROM `gtaset_dbbmn`.t_ruang -- 7684
JOIN barang_ref ON barangNama = "Bangunan Gedung Kantor Permanen"
JOIN sumber_dana_ref ON sumberdanaNama = "RUPIAH MURNI"
JOIN peruntukan_bangunan_ref ON peruntukanBangunanNama = "Kuliah"
JOIN kepemilikan_barang_ref ON kepemilikanbrgNama = "Universitas"
JOIN status_penguasaan_barang_ref ON statuspengbrgNama = "Hak Penuh"
JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
JOIN kampus_ref ON kampusNama = "Kampus 1"
JOIN gtfw_user ON UserName = "admin"
JOIN unit_kerja_ref ON unitkerjaKode = LEFT(RIGHT(kd_lokasi, 5), 3)
GROUP BY kd_lokasi
;';

$sql['FC_insert_gedung_t_dir']='
ALTER TABLE gedung AUTO_INCREMENT = 0;
ALTER TABLE `gtaset_dbbmn`.t_dir CONVERT TO CHARACTER SET utf8;
--
INSERT INTO gedung(
gedungBarangId,
gedungKode,
gedungNama,
gedungSumberDanaId,
gedungNilaiResidu,
gedungNilaiPerolehan, 
gedungUmurEkonomis,
gedungSifatBangunan,
gedungIMB,
gedungPeruntukanBangunanId,
gedungKepemilikanBrgId,
gedungStatusPengBrgId,
gedungKondisiBrgId,
gedungKampusId,
gedungUnitkerjaId,
gedungTglUbah, 
gedungUserId
)
SELECT * FROM (
   SELECT 
   barangId AS gedungBarangId,
   kd_lokasi AS gedungKode,
   CONCAT("Gedung ",kd_lokasi) AS gedungNama,
   sumberdanaId AS gedungSumberDanaId, -- sumber_barang_ref
   "1" AS gedungNilaiResidu,
   "1" AS gedungNilaiPerolehan, 
   "1" AS gedungUmurEkonomis,
   "Permanen" AS gedungSifatBangunan,
   "Ya" AS gedungIMB,
   peruntukanBangunanId AS gedungPeruntukanBangunanId, -- peruntukan_bangunan_ref
   kepemilikanbrgId AS gedungKepemilikanBrgId, -- kepemilikan_barang_ref
   statusPengBrgId AS gedungStatusPengBrgId, -- status_penguasaan_barang_ref
   kondisibrgId AS gedungKondisiBrgId, -- kondisi_barang_ref
   kampusId AS gedungKampusId, -- kampus_ref
   unitkerjaId AS gedungUnitkerjaId, -- unit_kerja_ref
   NOW() AS gedungTglUbah,
   UserId AS gedungUserId
   FROM `gtaset_dbbmn`.t_dir -- 475929
   JOIN barang_ref ON barangNama = "Bangunan Gedung Kantor Permanen"
   JOIN sumber_dana_ref ON sumberdanaNama = "RUPIAH MURNI"
   JOIN peruntukan_bangunan_ref ON peruntukanBangunanNama = "Kuliah"
   JOIN kepemilikan_barang_ref ON kepemilikanbrgNama = "Universitas"
   JOIN status_penguasaan_barang_ref ON statuspengbrgNama = "Hak Penuh"
   JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
   JOIN kampus_ref ON kampusNama = "Kampus 1"
   JOIN gtfw_user ON UserName = "admin"
   JOIN unit_kerja_ref ON unitkerjaKode = LEFT(RIGHT(kd_lokasi, 5), 3) -- 475929
   GROUP BY kd_lokasi
) AS hasil
WHERE gedungKode NOT IN (
   SELECT gedungKode FROM gedung
)
;';

$sql['FC_pic_ref'] = '
INSERT INTO pic_ref(
   picNama
)
SELECT
TRIM(pj_ruang) AS picNama
FROM `gtaset_dbbmn`.t_ruang
WHERE pj_ruang NOT IN ("", "-")
GROUP BY pj_ruang
;';

$sql['FC_insert_lantai'] = '
INSERT INTO aset_lantai (
lantaiGedungId,
lantaiKode,
lantaiNama  
)
SELECT
gedungId,
"L1",
"Lantai 1"
FROM gedung
;';

$sql['FC_insert_ruang_t_ruang']='
ALTER TABLE ruang ADD COLUMN masterLokasi VARCHAR(50) NULL;
ALTER TABLE ruang ADD INDEX(masterLokasi);
--
INSERT INTO ruang(
   ruangKode,
   ruangNama,
   ruangGedungId,
   ruangLantaiId,
   ruangJenisRuangId,
   ruangTipeRuangId,
   ruangKondisiBarangId,
   ruangUnitId,
   ruangTglUbah,
   ruangPicId,
   ruangUserId,
   masterLokasi
)
SELECT 
kd_ruang AS ruangKode,
IF(ur_ruang IS NULL OR ur_ruang = "", kd_ruang, ur_ruang) AS ruangNama,
gedungId AS ruangGedungId,
lantaiId AS ruangLantaiId,
jenisruangId AS ruangJenisRuangId, -- jenis_ruang_ref
tipeRuangId AS ruangTipeRuangId, -- tipe_ruang_ref
kondisibrgId AS ruangKondisiBarangId, -- kondisi_barang_ref
unitkerjaId AS ruangUnitId, -- unit_kerja_ref
NOW() asruangTglUbah,
picId AS ruangPicId, -- pic_ref
UserId AS ruangUserId, -- gtfw_user
CONCAT(kd_lokasi, kd_ruang) AS masterLokasi
FROM `gtaset_dbbmn`.t_ruang -- 7684
JOIN gedung ON gedungKode = kd_lokasi
JOIN aset_lantai ON lantaiGedungId = gedungId
LEFT JOIN pic_ref ON picNama = pj_ruang
JOIN jenis_ruang_ref ON jenisruangNama = "Kuliah"
JOIN tipe_ruang_ref ON  tipeRuangNama = "ruang kuliah"
JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
JOIN gtfw_user ON UserName = "admin"
JOIN unit_kerja_ref ON unitkerjaKode = LEFT(RIGHT(kd_lokasi, 5), 3) -- 7684
GROUP BY kd_lokasi, kd_ruang -- 7684
;';

$sql['FC_insert_ruang_t_dir']='
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(kd_lokasi);
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(kd_ruang);
ALTER TABLE aset_lantai ADD INDEX(lantaiGedungId);
ALTER TABLE ruang AUTO_INCREMENT = 0;
--
INSERT INTO ruang(
   ruangKode,
   ruangNama,
   ruangGedungId,
   ruangLantaiId,
   ruangJenisRuangId,
   ruangTipeRuangId,
   ruangKondisiBarangId,
   ruangUnitId,
   ruangTglUbah,
   ruangPicId,
   ruangUserId,
   masterLokasi
)
SELECT * FROM (
   SELECT 
   kd_ruang AS ruangKode,
   kd_ruang AS ruangNama,
   gedungId AS ruangGedungId,
   lantaiId AS ruangLantaiId,
   jenisruangId AS ruangJenisRuangId, -- jenis_ruang_ref
   tipeRuangId AS ruangTipeRuangId, -- tipe_ruang_ref
   kondisibrgId AS ruangKondisiBarangId, -- kondisi_barang_ref
   unitkerjaId AS ruangUnitId, -- unit_kerja_ref
   NOW() asruangTglUbah,
   NULL AS ruangPicId, -- pic_ref
   UserId AS ruangUserId, -- gtfw_user
   CONCAT(kd_lokasi, kd_ruang) AS masterLokasi
   FROM `gtaset_dbbmn`.t_dir -- 475929
   JOIN gedung ON gedungKode = kd_lokasi
   JOIN aset_lantai ON lantaiGedungId = gedungId
   JOIN jenis_ruang_ref ON jenisruangNama = "Kuliah"
   JOIN tipe_ruang_ref ON  tipeRuangNama = "ruang kuliah"
   JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
   JOIN gtfw_user ON UserName = "admin"
   JOIN unit_kerja_ref ON unitkerjaKode = LEFT(RIGHT(kd_lokasi, 5), 3) -- 475929
   WHERE kd_ruang NOT IN ("")
   GROUP BY kd_lokasi, kd_ruang
) AS hasil
WHERE masterLokasi NOT IN (
   SELECT masterLokasi FROM ruang
)
;';

$sql['FC_update_t_sskel']='
ALTER TABLE `gtaset_dbbmn`.t_sskel ADD COLUMN `barangId` BIGINT (20) NULL;
ALTER TABLE `gtaset_dbbmn`.t_sskel ADD INDEX(barangId);
ALTER TABLE `gtaset_dbbmn`.t_sskel ADD INDEX(kd_brg);
--
UPDATE `gtaset_dbbmn`.t_sskel s
JOIN (
   SELECT 
   barangId,
   bidangbrgGolbrgId,
   bidangbrgKode,
   kelbrgKode,
   subkelbrgKode,
   barangKode,
   CONCAT(
      bidangbrgGolbrgId,
      IF(LENGTH(bidangbrgKode)<2,CONCAT("0",bidangbrgKode),bidangbrgKode),
      IF(LENGTH(kelbrgKode)<2,CONCAT("0",kelbrgKode),kelbrgKode),
      IF(LENGTH(subkelbrgKode)<2,CONCAT("0",subkelbrgKode),subkelbrgKode),
      CASE
      WHEN LENGTH(barangKode)=1 THEN CONCAT("00",barangKode)
      WHEN LENGTH(barangKode)=2 THEN CONCAT("0",barangKode)
      WHEN LENGTH(barangKode)>2 THEN barangKode
      END 
   ) AS kode_barang
   FROM  barang_ref
   JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
) AS d 
ON d.kode_barang = s.kd_brg
SET s.barangId = d.barangId
;';

$sql['FC_update_t_dir_fase_1']='
ALTER TABLE `gtaset_dbbmn`.t_dir ADD COLUMN `masterLokasi` VARCHAR(50) NULL;
ALTER TABLE `gtaset_dbbmn`.t_dir ADD COLUMN `masterBarang` VARCHAR(50) NULL;
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(masterLokasi);
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(masterBarang);
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(kd_brg);
ALTER TABLE `gtaset_dbbmn`.t_dir ADD INDEX(no_aset)
;';

$sql['FC_update_t_dir_fase_2']='
UPDATE `gtaset_dbbmn`.t_dir SET masterLokasi = CONCAT(kd_lokasi, kd_ruang)
;';

$sql['FC_update_t_dir_fase_3']='
-- unit, kd_brg, no_aset
UPDATE `gtaset_dbbmn`.t_dir SET masterBarang = CONCAT(LEFT(RIGHT(kd_lokasi, 5), 3), kd_brg, LPAD(no_aset, 6, "0"))
;';

$sql['FC_delete_t_masteru']='
-- pesenan dari pak nanang
DELETE FROM `gtaset_dbbmn`.t_masteru WHERE LEFT(jns_trn, 1) = 3
;';

$sql['FC_update_t_masteru_fase_1']='
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD COLUMN `barangId` BIGINT (20) NULL;
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD COLUMN `masterId` BIGINT (20) NULL;
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(barangId);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(masterId);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(kd_brg);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(kd_lokasi);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(no_aset);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(merk_type);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(no_sppa);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(tgl_perlh);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD INDEX(rph_aset)
;';

$sql['FC_update_t_masteru_fase_2']='
UPDATE `gtaset_dbbmn`.t_masteru m
JOIN `gtaset_dbbmn`.t_sskel s ON s.kd_brg = m.kd_brg
SET m.barangId = s.barangId
;';

$sql['FC_update_t_masteru_fase_3']='
#UPDATE `gtaset_dbbmn`.t_masteru SET masterId = CONCAT(barangId, "0", no_aset);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD COLUMN ID INT(11) AUTO_INCREMENT FIRST, ADD INDEX(ID);
ALTER TABLE `gtaset_dbbmn`.t_masteru ADD COLUMN masterJoin VARCHAR(100), ADD INDEX(masterJoin)
;';

$sql['FC_update_t_masteru_fase_4']='
UPDATE `gtaset_dbbmn`.t_masteru
SET masterJoin = CONCAT(barangId, merk_type, rph_aset, tgl_perlh)
;';

$sql['FC_update_t_masteru_fase_5']='
DROP TABLE IF EXISTS `gtaset_dbbmn`.t_masteru_master;

CREATE TABLE `gtaset_dbbmn`.t_masteru_master AS
SELECT 
COUNT(*) AS invJumlahAset, thn_ang, periode, kd_lokasi, no_sppa, kd_brg, no_aset, tgl_perlh, tercatat, 
kondisi, tgl_buku, jns_trn, dsr_hrg, kd_data, flag_sap, kuantitas, rph_sat, rph_aset, flag_kor, keterangan, 
merk_type, asal_perlh, no_bukti, no_dsr_mts, tgl_dsr_mts, flag_ttp, flag_krm, kdblu, setatus, noreg, 
kdbapel, kdkpknl, umeko, rph_res, kdkppn, barangId, masterId, masterJoin
FROM `gtaset_dbbmn`.t_masteru
WHERE jns_trn = "100"
GROUP BY barangId, merk_type, rph_aset, tgl_perlh
; -- 93293

ALTER TABLE `gtaset_dbbmn`.t_masteru_master ADD COLUMN ID INT(11) AUTO_INCREMENT FIRST, ADD INDEX(ID);

ALTER TABLE `gtaset_dbbmn`.t_masteru_master
  ADD INDEX (`barangId`),
  ADD INDEX (`masterId`),
  ADD INDEX (`kd_brg`),
  ADD INDEX (`kd_lokasi`),
  ADD INDEX (`no_aset`),
  ADD INDEX (`merk_type`),
  ADD INDEX (`no_sppa`),
  ADD INDEX (`tgl_perlh`),
  ADD INDEX (`rph_aset`),
  ADD INDEX (`masterJoin`)
;';

$sql['FC_update_t_masteru_fase_6']='
UPDATE `gtaset_dbbmn`.t_masteru t
JOIN `gtaset_dbbmn`.t_masteru_master m USING(masterJoin)
SET t.masterId = m.ID
WHERE t.jns_trn = "100"
;';

$sql['FC_insert_inventarisasi_mst']='
ALTER TABLE inventarisasi_detail CHANGE `invDetMerek` `invDetMerek` VARCHAR(55) NOT NULL;
ALTER TABLE inventarisasi_mst CHANGE `invMerek` `invMerek` VARCHAR(55) NOT NULL;
ALTER TABLE inventarisasi_mst ADD INDEX(invKodeAset);
DELETE FROM inventarisasi_mst;
ALTER TABLE inventarisasi_mst AUTO_INCREMENT = 1;
--
INSERT INTO inventarisasi_mst(
invMstId                ,
invMstPengadaanId       ,
invBarangId             ,
invKodeAset             ,
invBarcode              ,
invMstLabel             ,
invMerek                ,
invSpesifikasi          ,
invTglPembelian         ,
invSumberDana           ,
invJumlahAset           ,
invSatuanBarang         ,
invLuasTanah            ,
invNilaiPerolehanSatuan ,
invNilaiFaktur          ,
invTotalPerolehan       ,
invSPMNumber            ,
invTglUbah              ,
invUserId
)
SELECT
ID AS invMstId,
NULL AS invMstPengadaanId,
m.barangId AS invBarangId,
no_sppa AS invKodeAset,
NULL AS invBarcode,
barangNama AS invMstLabel,
merk_type AS merk_type ,
keterangan AS invSpesifikasi,
tgl_perlh AS invTglPembelian,
sumberDanaId AS invSumberDana, -- sumber_dana_ref
invJumlahAset AS invJumlahAset,
satuanbrgId AS invSatuanBarang,-- satuan_barang_ref
kuantitas AS invLuasTanah,
rph_sat AS invNilaiPerolehanSatuan,
NULL AS invNilaiFaktur,
rph_sat AS invTotalPerolehan,
no_sppa AS invSPMNumber,
tgl_perlh AS invTglUbah,
UserId AS invUserId -- gtfw_user]
FROM `gtaset_dbbmn`.t_masteru_master m -- 93293
JOIN `gtaset_dbbmn`.t_sskel s ON s.barangId = m.barangId
JOIN barang_ref b ON b.barangId = m.barangId
JOIN sumber_dana_ref ON sumberdanaNama = "RUPIAH MURNI"
JOIN gtfw_user ON UserName = "admin"
LEFT JOIN satuan_barang_ref ON satuanbrgNama = satuan -- 764919
ORDER BY ID
;';

$sql['FC_update_inventarisasi_mst']='
UPDATE inventarisasi_mst 
SET 
invNilaiFaktur = 1, 
invTotalPerolehan = invNilaiPerolehanSatuan * invJumlahAset
WHERE invNilaiFaktur IS NULL AND invTotalPerolehan IS NULL;

UPDATE inventarisasi_mst
SET 
invKodeAset = 
   CONCAT(
      MID(invBarangId, 1, 1), ".", 
      MID(invBarangId, 2, 2), ".", 
      MID(invBarangId, 4, 2), ".", 
      MID(invBarangId, 6, 2), ".", 
      MID(invBarangId, 8, 3)
   ),
invBarcode = invBarangId
;';

$sql['FC_insert_inventarisasi_detail']='
ALTER TABLE inventarisasi_detail CHANGE `invDetSertifikat` `invDetSertifikat` VARCHAR(55) NULL;
ALTER TABLE inventarisasi_detail ADD COLUMN `masterBarang` VARCHAR(50) NULL;
ALTER TABLE inventarisasi_detail ADD INDEX(masterBarang);
DELETE FROM inventarisasi_detail;
ALTER TABLE inventarisasi_detail AUTO_INCREMENT = 1;
ALTER TABLE inventarisasi_detail CHANGE `invDetKodeBarang` `invDetKodeBarang` VARCHAR(100) NULL;
ALTER TABLE `gtaset_dbbmn`.t_masteru CONVERT TO CHARACTER SET utf8;

/*
ALTER TABLE inventarisasi_detail
ADD COLUMN masterLokasi VARCHAR(50),
ADD INDEX(masterLokasi)
*/

INSERT INTO inventarisasi_detail(
invDetGolId                ,
invDetBidangbrngId         ,
invDetKelbrgId             ,
invDetSubkelbrgId          ,
invDetBarangId             ,
invDetMstId                , --
invDetKodeBarang           , --
invDetKodeAsli             ,
invDetBarcode              ,
invDetMstLabel             ,
invDetMerek                ,
invDetSpesifikasi          ,
invDetTglPembelian         ,
#invNilaiResidu          ,
#invTipe                 ,
invDetSumberDana           ,
invDetSatuanBarang         ,
invDetNilaiPerolehanSatuan ,
invDetNJOP                 ,
#invUmurEkonomis         ,
#invLokasi               ,
#invKegunaan             ,
invDetSertifikat           ,
#invAkta                 ,
#invBPKB                 ,
#invSTNK                 ,
#invNoMesin              ,
#invNoRangka             ,
#invNoPolisi             ,
#invNoLain               ,
#invWarna                ,
#invPagar                ,
#invBatasUtara           ,
#invBatasTimur           ,
#invBatasSelatan         ,
#invBatasBarat           ,
invDetKepemilikan          ,
invDetPenguasaanBarang     ,
#invKeteranganLain       ,
invDetKampus               ,
invDetGedungId             ,
invDetRuanganId            ,
invDetUnitKerja            ,
invDetKondisiId            ,
invDetTglUbah              ,
invDetUserId               ,
masterBarang 
)
SELECT 
LEFT(barangId, 1) AS invDetGolId,
LEFT(barangId, 3) AS invDetBidangbrngId,
LEFT(barangId, 5) AS invDetKelbrgId,
LEFT(barangId, 7) AS invDetSubkelbrgId, 
kd_brg AS invDetBarangId,
masterId AS invDetMstId, -- inventarisasi_mst
CONCAT(invKodeAset, ".", no_aset) AS invDetKodeBarang, --
kd_brg AS invDetKodeAsli,
CONCAT(invBarcode, no_aset) AS invDetBarcode, 
invMstLabel AS invDetMstLabel,
invMerek AS invDetMerek,
invSpesifikasi AS invDetSpesifikasi,
invTglPembelian AS invDetTglPembelian,
invSumberDana AS invDetSumberDana,
IFNULL(invSatuanBarang, 1) AS invDetSatuanBarang,
IF(kd_brg LIKE "101%" OR kd_brg LIKE "102%", rph_aset, invNilaiPerolehanSatuan) AS invDetNilaiPerolehanSatuan,
rph_aset AS invDetNJOP,
no_bukti AS invDetSertifikat,
kepemilikanbrgId AS invDetKepemilikan, -- kepemilikan_barang_ref
statuspengbrgId AS invDetPenguasaanBarang, -- status_penguasaan_barang_ref
kampusId AS invDetKampus, -- kampus_ref
NULL AS invDetGedungId, -- gedung
NULL AS invDetRuanganId,
unitkerjaId AS invDetUnitKerja, -- unit_kerja_ref
kondisibrgId AS invDetDetKondisiId, -- kondisi_barang_ref
invTglPembelian AS invDetTglUbah,
UserId AS invDetUserId, -- gtfw_user
CONCAT(LEFT(RIGHT(kd_lokasi, 5), 3), kd_brg, LPAD(no_aset, 6, "0")) AS masterBarang
FROM `gtaset_dbbmn`.t_masteru -- 764919
JOIN inventarisasi_mst ON invMstId = masterId -- 308596  
JOIN unit_kerja_ref ON unitkerjaKode = LEFT(RIGHT(kd_lokasi, 5), 3)
JOIN gtfw_user ON UserName = "admin"
JOIN kepemilikan_barang_ref ON kepemilikanbrgNama = "Universitas"
JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
JOIN status_penguasaan_barang_ref ON statuspengbrgNama = "Hak Penuh"
JOIN kampus_ref ON kampusNama = "Kampus 1"
WHERE jns_trn = "100"
GROUP BY barangId, merk_type, rph_aset, tgl_perlh, no_aset
;';

$sql['FC_update_total_perolehan']='
-- tanah
UPDATE inventarisasi_mst
SET
invTotalPerolehan = invLuasTanah*invNilaiPerolehanSatuan
WHERE
LEFT(invBarangId,1)=2;

-- non tanah
UPDATE inventarisasi_mst
SET
invTotalPerolehan = invJumlahAset*invNilaiPerolehanSatuan
WHERE
LEFT(invBarangId,1)<>2
;';

$sql['FC_update_inventarisasi_detail_fase1']='
UPDATE inventarisasi_detail
JOIN unit_kerja_ref ON unitkerjaId = invDetUnitKerja
JOIN gedung ON LEFT(RIGHT(gedungKode, 5), 3) = unitkerjaKode
SET invDetGedungId = gedungId
;';

$sql['FC_update_inventarisasi_detail_fase2']='
-- TODO: convertion stuck here
/*UPDATE inventarisasi_detail
JOIN `gtaset_dbbmn`.t_dir USING(masterbarang)
JOIN ruang USING(masterLokasi)
SET invDetRuanganId = ruangId*/

UPDATE inventarisasi_detail 
SET 
invDetNilaiResidu=1,
invDetNJOP = 1,
invDetUmurEkonomis = 1
WHERE invDetNilaiResidu IS NULL
AND invDetNJOP IS NULL
AND invDetUmurEkonomis IS NULL;

UPDATE inventarisasi_detail 
SET invDetUmurEkonomis = 60-(IF(YEAR(NOW())-YEAR(invDetTglPembelian)=0,
  YEAR(NOW())-YEAR(invDetTglPembelian),
  (YEAR(NOW())-YEAR(invDetTglPembelian))*12+(MONTH(NOW())-MONTH(invDetTglPembelian))))
WHERE LEFT(invDetKodeAsli,1)IN(3,6);

UPDATE inventarisasi_detail 
SET invDetUmurEkonomis = 0
WHERE LEFT(invDetKodeAsli,1)IN(3,6) AND invDetUmurEkonomis<0
;';

$sql['FC_insert_gedung_dr_inventarisasi_mst']="
INSERT INTO gedung
(
gedungBarangId,
gedungKode,
gedungNama,
gedungUmurEkonomis,
gedungSumberDanaId,
gedungNilaiPerolehan,
gedungNilaiResidu,
gedungSifatBangunan,
gedungIMB,
gedungPeruntukanBangunanId,
gedungKepemilikanBrgId,
gedungStatusPengBrgId,
gedungKondisiBrgId,
gedungKampusId,
gedungUnitkerjaId,
gedungDataAsalId,
gedungTglUbah,
gedungUserId
)
SELECT
'4010101001' AS idbarang,
invKodeAset AS kodeGedung,
invMstLabel AS namaGedung,
1 AS umurEkonomis,
1 AS sumberdana,
1.00 AS nilaiPerolehan,
1.00 AS nilaiResidu,
'Permanen' AS sifat,
'Ya' AS imb,
1 AS peruntukan,
1 AS kepemilikan,
1 AS statusbrg,
1 AS kondisi,
1 AS kampus,
1 AS unit,
1 AS dataasal,
NOW() AS tglUbah,
2 AS userId
FROM inventarisasi_mst
WHERE
LEFT (invKodeAset,1)=4
GROUP BY invKodeAset
;";

$sql['FC_insert_inv_kondisi_history']='
DELETE FROM inv_kondisi_history;
ALTER TABLE inv_kondisi_history AUTO_INCREMENT = 1;

INSERT INTO inv_kondisi_history(
invKondisiBrgBrgDetId,
invKondisiBrgKondId,
invKondisiBrgTglKondisi,
invKondisiTglHistory,
invKondisiUserId
)
SELECT 
invDetId,
kondisibrgId,
DATE(NOW()) AS invDetKondisiBrgTglKondisi,
NOW() AS invDetKondisiTglHistory,
UserId
FROM inventarisasi_detail 
JOIN gtfw_user ON userName = "admin"
JOIN kondisi_barang_ref ON kondisibrgNama = "Baik"
ORDER BY invDetId
;';

$sql['FC_insert_penyusutan_brg_mst']='
DELETE FROM penyusutan_brg_mst;
ALTER TABLE penyusutan_brg_mst AUTO_INCREMENT = 1;

/*
INSERT IGNORE INTO penyusutan_brg_mst(
mstPenystnBarangId,
mstPenystnGedungId,
mstPenystnNilaiPerolehan,
mstPenystnNilaiResidu,
mstPenystnUmrEko,
mstPenystnDisusutkan,
mstPenystnNilaiPenyusutan,
mstPenystnTglPerubahan
)
SELECT 
invDetId,
invDetGedungId,
invDetNilaiPerolehanSatuan,
invDetNilaiResidu,
invDetUmurEkonomis,
invDetNilaiPerolehanSatuan * invDetSatuanBarang AS mstPenystnDisusutkan,
invDetNilaiPerolehanSatuan * invDetSatuanBarang AS mstPenystnDisusutkan,
NOW() AS mstPenystnTglPerubahan
FROM inventarisasi_detail
WHERE invDetGedungId IS NOT NULL
GROUP BY invDetId, invDetGedungId
*/

INSERT INTO penyusutan_brg_mst(
mstPenystnBarangId,
mstPenystnNilaiPerolehan,
mstPenystnNilaiResidu,
mstPenystnUmrEko,
mstPenystnDisusutkan,
mstPenystnNilaiPenyusutan,
mstPenystnNilaiTotalPenyusutan,
mstPenystnTglPerubahan
)
SELECT
invDetId,
d.invDetNilaiPerolehanSatuan,
1,
invDetUmurEkonomis,
0,
(d.invDetNilaiPerolehanSatuan-1)/IF(invDetUmurEkonomis IS NULL OR invDetUmurEkonomis=0,60,invDetUmurEkonomis) AS mstPenystnNilaiPenyusutan ,-- 5x 12
(d.invDetNilaiPerolehanSatuan-1)/IF(invDetUmurEkonomis IS NULL OR invDetUmurEkonomis=0,60,invDetUmurEkonomis) AS mstPenystnNilaiTotalPenyusutan ,-- 5x 12
NOW() AS mstPenystnTglPerubahan
FROM inventarisasi_detail d
WHERE 
-- LEFT(invDetKodeAsli,1) IN (3,6)
-- AND 
invDetId NOT IN(
   SELECT mstPenystnBarangId FROM penyusutan_brg_mst
)
;';

$sql['FC_update_penyusutan_tanah']='
UPDATE penyusutan_brg_mst,
(
SELECT 
mstPenystnBrgId AS kode,
invTotalPerolehan AS total 
FROM penyusutan_brg_mst
LEFT JOIN inventarisasi_detail ON mstPenystnBarangId = invDetId
LEFT JOIN inventarisasi_mst ON invMstId = invDetMstId
WHERE
LEFT(invBarangId,1)=2
)AS grup
SET
mstPenystnNilaiPerolehan = total
WHERE
mstPenystnBrgId = kode
;';

// $sql['FC_delete_temp_table_and_column_fase_1']='
// ALTER TABLE ruang DROP COLUMN masterLokasi, DROP INDEX masterLokasi;';

// $sql['FC_delete_temp_table_and_column_fase_2']='
// ALTER TABLE inventarisasi_detail DROP COLUMN masterBarang, DROP INDEX masterBarang;';

// $sql['FC_delete_temp_table_and_column_fase_3']='
// ALTER TABLE inventarisasi_mst DROP INDEX invKodeAset;
// ALTER TABLE unit_kerja_ref DROP INDEX unitkerjaKode;
// ALTER TABLE gedung DROP INDEX gedungKode;
// DROP TABLE IF EXISTS temp_kondisi_barang_ref;
// DROP TABLE IF EXISTS temp_t_sskel
// ;';

# =============== UPDATE (UP_) =============== #

# TODO : this is undone
/*
 * Step :
 * 1. Insert new inventarisasi_mst with condition
 * 2. Insert new inventarisasi_detail with condition 
 * */
$sql['UP_insert_inventarisasi_detail'] = "
SELECT a.* FROM `unijoyo_dbbmn10_26`.`t_masteru` a
WHERE a.jns_trn = 100 AND CONCAT(a.`kd_lokasi`,a.`kd_brg`,a.`no_aset`) NOT IN 
(SELECT CONCAT(b.kd_lokasi,b.kd_brg,b.`no_aset`)
FROM `unijoyo_dbbmn10_24`.`t_masteru` b
WHERE b.jns_trn = 100
);
";

$sql['sample']='
UPDATE `unijoyo_dbbmn10_26`.t_masteru SET testing = CONCAT(kd_brg,no_aset);

SELECT COUNT(*) FROM `unijoyo_dbbmn10_26`.`t_masteru` WHERE
jns_trn="100: AND testing NOT IN
(SELECT CONCAT(invDetKodeAsli,RIGHT(invDetKodeBarang,LENGTH(invDetKodeBarang)-15)) AS test
FROM inventarisasi_detail)
';
?>
