<?php
#combo
$sql['get_sumber_dana'] = "
SELECT
   sumberdanaId AS id,
   sumberdanaNama AS name
FROM
   sumber_dana_ref
ORDER BY sumberdanaNama
";

$sql['get_data_gedung_last_insert'] ="
	SELECT
		gedungId   AS gdId,
		gedungKode AS gdKode,
		gedungNama AS gdNama
	FROM gedung
	WHERE gedungId = (SELECT
							MAX(gedungId)
							FROM gedung)
";

$sql['get_data_ruang_last_insert'] ="
	SELECT
	  ruangId              AS ruId,
	  ruangKode            AS ruKode,
	  ruangNama            AS ruNama,
	  ruangGedungId        AS ruGdId,
	  ruangKapasitas       AS ruKapasitas,
	  ruangJenisRuangId    AS ruGruId,
	  ruangTipeRuangId     AS ruIsKuliah,
	  ruangKondisiBarangId AS ruIsLayakPakai,
	  ruangLuasRuang       AS ruLuas
	FROM ruang
	WHERE ruangId = (SELECT
							 MAX(ruangId)
						  FROM ruang)
";

$sql['get_kepemilikan'] = "
SELECT
   kepemilikanbrgId AS id,
   kepemilikanbrgNama AS name
FROM
   kepemilikan_barang_ref
ORDER BY kepemilikanbrgNama
";

$sql['get_lokasi_kampus'] = "
SELECT
   kampusId AS id,
   kampusNama AS name
FROM
   kampus_ref
ORDER BY kampusNama
";

$sql['get_kondisi'] = "
SELECT
   kondisibrgId AS id,
   kondisibrgNama AS name
FROM
   kondisi_barang_ref
ORDER BY kondisibrgNama
";

$sql['get_peruntukan_bangunan'] = "
SELECT
   peruntukanBangunanId AS id,
   peruntukanBangunanNama AS name
FROM
   peruntukan_bangunan_ref
ORDER BY peruntukanBangunanNama
";

$sql['get_penguasaan_barang'] = "
SELECT
   statuspengbrgId AS id,
   statuspengbrgNama AS name
FROM
   status_penguasaan_barang_ref
ORDER BY statuspengbrgNama
";

#count & get data
$sql['get_count']="
SELECT FOUND_ROWS() AS total
";

$sql['get_data_user_by_user_name'] = "
SELECT 
   UserId AS user_id,
   UserName AS user_name,
   RealName AS real_name,
   a.Description AS description,
   Active AS is_active,
   a.GroupId AS group_id,
   GroupName AS group_name,
   unitkerjaId,
   unitkerjaNama,
   userunitkerjaRoleId AS role_id,
   unitkerjaKodeSistem
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = userunitkerjaUnitkerjaId
WHERE
   UserName = %s
";

$sql['get_data_unit_by_unit_name'] = "
SELECT
   *
FROM
   unit_kerja_ref
WHERE
   unitkerjaNama = %s
";

$sql['get_count_gedung'] = "
SELECT
   count(a.gedungId) AS total 
FROM
   gedung a
   LEFT JOIN sumber_dana_ref b ON a.gedungSumberDanaId = b.sumberdanaId
	LEFT JOIN unit_kerja_ref c ON a.gedungUnitkerjaId = c.unitkerjaId
WHERE
   (a.gedungKode LIKE '%s' OR a.gedungNama LIKE '%s') AND
   (a.gedungUnitkerjaId = %s OR c.unitkerjaParentId = %s OR '1' = %s)
   %s
";

$sql['get_data_gedung_old'] = "
SELECT
   SQL_CALC_FOUND_ROWS
   a.gedungId AS gedung_id,
   a.gedungKode AS gedung_kode,
   a.gedungNama AS gedung_nama,
   a.gedungNilaiResidu AS nilai_residu,
   b.sumberdanaNama AS sumber_dana,
   left(a.gedungThnPerolehan, 4) AS thn_perolehan,
   a.gedungBiayaPembangunan AS nilai_pokok,
   ifnull((SELECT SUM(gedungBiayaNilai) FROM gedung_biaya WHERE gedungBiayaGedungId = a.gedungId) + a.gedungBiayaPembangunan, a.gedungBiayaPembangunan) AS nilai_gedung,
   (SELECT COUNT(ruangGedungId) FROM ruang
		JOIN aset_lantai
			ON lantaiId = ruangLantaiId
	WHERE ruangGedungId = a.gedungId) AS jml_ruang,
   a.gedungDenahBangunan as foto_denah,
   a.gedungFoto as foto_gedung,
	c.unitKerjaId as unitkerjaid,
	c.unitKerjaNama AS unitkerjanama
FROM
   gedung a
   LEFT JOIN sumber_dana_ref b ON a.gedungSumberDanaId = b.sumberdanaId
	LEFT JOIN unit_kerja_ref c ON a.gedungUnitkerjaId = c.unitkerjaId
WHERE
   (a.gedungKode LIKE '%s' OR a.gedungNama LIKE '%s') AND
   (a.gedungUnitkerjaId = %s OR c.unitkerjaParentId = %s OR '1' = %s)
    %s
ORDER BY a.gedungKode ASC
LIMIT %s,%s
";

$sql['get_data_gedung'] = "
SELECT
SQL_CALC_FOUND_ROWS
a.gedungId AS gedung_id,
a.gedungKode AS gedung_kode,
a.gedungNama AS gedung_nama,
a.gedungNilaiResidu AS nilai_residu,
b.sumberdanaNama AS sumber_dana,
LEFT(a.gedungThnPerolehan, 4) AS thn_perolehan,
a.gedungBiayaPembangunan AS nilai_pokok,
IFNULL((SELECT SUM(gedungBiayaNilai) FROM gedung_biaya WHERE gedungBiayaGedungId = a.gedungId) + a.gedungBiayaPembangunan, a.gedungBiayaPembangunan) AS nilai_gedung,
(SELECT COUNT(ruangGedungId) FROM ruang
JOIN aset_lantai
ON lantaiId = ruangLantaiId
WHERE ruangGedungId = a.gedungId) AS jml_ruang,
a.gedungDenahBangunan AS foto_denah,
a.gedungFoto AS foto_gedung,
c.unitKerjaId AS unitkerjaid,
c.unitKerjaNama AS unitkerjanama
FROM
[DATA_MASTER] a
LEFT JOIN sumber_dana_ref b ON a.gedungSumberDanaId = b.sumberdanaId
LEFT JOIN unit_kerja_ref c ON a.gedungUnitkerjaId = c.unitkerjaId
WHERE
(a.gedungKode LIKE '%s' OR a.gedungNama LIKE '%s')
%s
ORDER BY a.gedungThnPerolehan DESC
LIMIT %s,%s
";

#get data by Id
$sql['get_gedung_by_id'] = "
SELECT
   gedungId AS gd_id,
   gedungBarangId AS gd_brg_id,
   barangKode AS aset_kode,
   (
      SELECT 
         CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) 
      FROM 
         golongan_barang_ref
         JOIN bidang_barang_ref ON (bidangbrgGolbrgId = golbrgId)
         JOIN kelompok_barang_ref ON (kelbrgBidangbrgId = bidangbrgId)
         JOIN sub_kelompok_barang_ref ON (subkelbrgKelbrgId = kelbrgId)
         JOIN barang_ref ON (barangSubkelbrgId = subkelbrgId)
         JOIN gedung ON barangId = gedungBarangId
      WHERE
         gedungId = '%s'
   ) AS aset_kode,
   barangNama AS aset_nama,
   gedungKode AS gd_kode,
   gedungNama AS gd_nama,
   left(gedungThnPerolehan, 4)  AS gd_thn_perolehan,
   gedungSumberDanaId AS gd_sbr_dana,
   gedungBiayaPembangunan AS biaya_pembangunan,
   gedungNilaiResidu AS nilai_residu,
   gedungNilaiPerolehan AS nilai_perolehan,
   gedungUmurEkonomis AS umur_ekonomis,
   gedungUmurTeknis AS umur_teknis,
   gedungLuasTanah AS gd_luas_tanah,
   gedungLuasBangunan AS gd_luas_bangunan,
   gedungSifatBangunan AS gd_sifat_bangunan,
   gedungIMB AS gd_imb,
   gedungPeruntukanBangunanId AS gd_peruntukan,
   gedungKepemilikanBrgId AS gd_milik_brg_id,
   gedungStatusPengBrgId AS gd_peng_brg_id,
   gedungKondisiBrgId AS gd_kondisi_id,
   gedungKampusId AS gd_kmps_id,
   gedungKeterangan AS gd_keterangan,
   gedungDenahBangunan AS gd_denah,
   gedungFoto AS gd_foto,
	unitkerjaId,
   unitkerjaNama
FROM
   gedung
   LEFT JOIN barang_ref ON barangId = gedungBarangId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = gedungUnitkerjaId
WHERE
   gedungId = %s
";

$sql['get_data_print_gedung'] ="
SELECT
   a.gedungId AS gedung_id,
   a.gedungKode AS gedung_kode,
   a.gedungNama AS gedung_nama,
   a.gedungNilaiResidu AS nilai_residu,
   b.sumberdanaNama AS sumber_dana,
   left(a.gedungThnPerolehan, 4) AS thn_perolehan,
   a.gedungBiayaPembangunan AS nilai_pokok,
   ifnull((SELECT SUM(gedungBiayaNilai) FROM gedung_biaya WHERE gedungBiayaGedungId = a.gedungId) + a.gedungBiayaPembangunan, a.gedungBiayaPembangunan) AS nilai_gedung,
   (SELECT COUNT(ruangGedungId) FROM ruang 
		JOIN aset_lantai
			ON lantaiId = ruangLantaiId
	WHERE ruangGedungId = a.gedungId) AS jml_ruang,
   a.gedungDenahBangunan as foto_denah,
   a.gedungFoto as foto_gedung
FROM
   gedung a
   LEFT JOIN sumber_dana_ref b ON a.gedungSumberDanaId = b.sumberdanaId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = gedungUnitkerjaId
WHERE
   (a.gedungKode LIKE '%s' OR a.gedungNama LIKE '%s')
   %s

";

$sql['insert_gedung'] = "
INSERT INTO gedung
   (gedungBarangId,
   gedungKode,
   gedungNama,
   gedungThnPerolehan, 
   gedungSumberDanaId,
   gedungLuasTanah,
   gedungLuasBangunan,
   gedungSifatBangunan,
   gedungIMB,
   gedungPeruntukanBangunanId,
   gedungKepemilikanBrgId,
   gedungStatusPengBrgId,
   gedungKondisiBrgId,
   gedungKampusId,
   gedungKeterangan,
   gedungDenahBangunan,
   gedungFoto,
   gedungBiayaPembangunan,
   gedungUmurEkonomis,
   gedungUmurTeknis,
   gedungNilaiResidu,
   gedungNilaiPerolehan,
   gedungUnitKerjaId,
   gedungTglUbah,
   gedungUserId)
VALUES 
   ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), '%s')
";

$sql['update_gedung'] = "
UPDATE 
   gedung
SET
   gedungBarangId = '%s',
   gedungKode = '%s',
   gedungNama = '%s',
   gedungThnPerolehan = '%s', 
   gedungSumberDanaId = '%s',
   gedungLuasTanah = '%s',
   gedungLuasBangunan = '%s',
   gedungSifatBangunan = '%s',
   gedungIMB = '%s',
   gedungPeruntukanBangunanId = '%s',
   gedungKepemilikanBrgId = '%s',
   gedungStatusPengBrgId = '%s',
   gedungKondisiBrgId = '%s',
   gedungKampusId = '%s',
   gedungKeterangan = '%s',
   gedungDenahBangunan = '%s',
   gedungFoto = '%s',
   gedungBiayaPembangunan = '%s',
   gedungUmurEkonomis = '%s',
   gedungUmurTeknis = '%s',
   gedungNilaiResidu = '%s',
   gedungNilaiPerolehan = '%s',
   gedungUnitKerjaId = '%s',
   gedungTglUbah = now(),
   gedungUserId = '%s'
WHERE
   gedungId = '%s'
";

$sql['update_gedung_no_denah'] = "
UPDATE 
   gedung
SET
   gedungBarangId = '%s',
   gedungKode = '%s',
   gedungNama = '%s',
   gedungThnPerolehan = '%s', 
   gedungSumberDanaId = '%s',
   gedungLuasTanah = '%s',
   gedungLuasBangunan = '%s',
   gedungSifatBangunan = '%s',
   gedungIMB = '%s',
   gedungPeruntukanBangunanId = '%s',
   gedungKepemilikanBrgId = '%s',
   gedungStatusPengBrgId = '%s',
   gedungKondisiBrgId = '%s',
   gedungKampusId = '%s',
   gedungKeterangan = '%s',
   gedungDenahBangunan = '%s',
   gedungFoto = '%s',
   gedungBiayaPembangunan = '%s',
   gedungUmurEkonomis = '%s',
   gedungUmurTeknis = '%s',
   gedungNilaiResidu = '%s',
   gedungNilaiPerolehan = '%s',
   gedungUnitKerjaId = '%s',
   gedungTglUbah = now(),
   gedungUserId = '%s'
WHERE
   gedungId = '%s'
";

$sql['delete_gedung'] = "
DELETE FROM gedung
WHERE
   gedungId IN ('%s')
";

#gedung dan biaya
$sql['get_biaya_ref'] = "
SELECT
   biayaId AS id,
   biayaName AS nama
FROM
   biaya_ref
ORDER BY biayaName
";

$sql['get_biaya_bedung'] = "
SELECT
   gedungBiayaId AS id_gedung_biaya,
   gedungBiayaBiayaId AS id_biaya,
   gedungBiayaNilai AS nilai_biaya
FROM
   gedung_biaya
WHERE
   gedungBiayaGedungId = '%s'
";

$sql['insert_biaya_gedung'] = "
INSERT INTO gedung_biaya
   (gedungBiayaBiayaId, gedungBiayaGedungId, gedungBiayaNilai)
VALUES
   ('%s', '%s', '%s');
";

$sql['update_biaya_gedung'] = "
UPDATE
   gedung_biaya
SET
   gedungBiayaNilai = '%s'
WHERE
   gedungBiayaId = '%s'
";

#gedung dan fasilitas
$sql['get_fasilitas_gedung_ref'] = "
SELECT
   gdgFasilitasId AS id_fasilitas,
   gdgFasilitasNama AS nama_fas,
   gdgFasilitasKet AS keterangan
FROM
   gedung_fasilitas_ref
ORDER BY
   gdgFasilitasNama
";

$sql['get_fasilitas_gedung'] = "
SELECT 
   a.gedungNama,
   b.gdgFasilitasId AS id_fasilitas,
   b.gdgFasilitasNama AS nama_fas,
   b.gdgFasilitasKet AS keterangan,
   c.gdgFasilitasId AS id_fas_gdg,
   c.gdgFasilitasJumlah AS jml_fasilitas
FROM 
   gedung a,
   gedung_fasilitas_ref b 
   LEFT JOIN gedung_fasilitas c ON b.gdgFasilitasId = c.gdgFasilitasGdgFasilitasId AND c.gdgFasilitasGdgId = %s
   LEFT JOIN gedung d ON c.gdgFasilitasGdgId = d.gedungId AND d.gedungId = %s
WHERE  
   a.gedungId = %s
ORDER BY
   b.gdgFasilitasNama ASC;
";

$sql['get_max_id_gedung'] = "
SELECT
   MAX(gedungId) AS id
FROM
   gedung
";

$sql['insert_gedung_fasilitas'] = "
INSERT INTO gedung_fasilitas
   (gdgFasilitasGdgId, gdgFasilitasGdgFasilitasId, gdgFasilitasJumlah)
VALUES
   ('%s', '%s', '%s');
";

$sql['delete_gedung_fasilitas'] = "
DELETE FROM
   gedung_fasilitas
WHERE
   gdgFasilitasId = %s
";

$sql['cek_gedung'] = "
   SELECT
      gedungId AS id_gedung
   FROM
      gedung
   WHERE
      gedungKode = '%s'
";

/*$sql['get_penanggungjawab'] = "
   SELECT
      unitkerjaNama 
   FROM
      unit_kerja_ref
   WHERE
      unitkerjaId = (SELECT settingValue FROM setting WHERE settingNama = 'Penanggung_Jawab_Gedung')
";*/

#insert ke penyusuran_brg_master
$sql['insert_penyusutan'] = "
   INSERT INTO penyusutan_brg_mst
      (mstPenystnGedungId, mstPenystnNilaiPerolehan, mstPenystnNilaiResidu, mstPenystnUmrEko, mstPenystnDisusutkan, mstPenystnNilaiPenyusutan, mstPenystnTglPerubahan)
   VALUES
      ('%s', '%s', '%s', '%s', '%s', '%s', now())
";

$sql['update_penyusutan'] = "
   UPDATE 
      penyusutan_brg_mst
   SET
      mstPenystnNilaiPerolehan = '%s', 
      mstPenystnNilaiResidu = '%s', 
      mstPenystnUmrEko = '%s', 
      mstPenystnDisusutkan = '%s', 
      mstPenystnNilaiPenyusutan = '%s', 
      mstPenystnTglPerubahan = now()
   WHERE
      mstPenystnGedungId = '%s'
";

$sql['get_data_penyusutan_gedung'] = "
   SELECT
      penyusutanDetNilaiAkhir AS nilai_akhir,
      penyusutanDetSisaUmrEk AS sisa_umur_ekonomis
   FROM
      penyusutan_det
      JOIN penyusutan_mst ON (penyusutanDetMst = penyusutanMstId)
   WHERE
      penyusutanDetGedungId = %s
      AND penyusutanMstPeriode = (
                                 SELECT 
                                    MAX(penyusutanMstPeriode)
                                 FROM
                                    penyusutan_mst
                                    JOIN penyusutan_det ON (penyusutanDetMst = penyusutanMstId)
                                 WHERE
                                    penyusutanDetGedungId = %s 
                                 )
";

$sql['penyesuaian_gedung'] = "
   UPDATE 
      gedung
   SET
      gedungUmurEkonomis = '%s',
      gedungUmurTeknis = '%s',
      gedungNilaiResidu = '%s',
      gedungNilaiPerolehan = '%s',
      gedungTglUbah = now(),
      gedungUserId = '%s'
   WHERE
      gedungId = '%s'
";

?>
