<?php
$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_combo_gedung_old'] = "
SELECT
	gedungId AS id,
	gedungNama AS name
FROM 
	gedung
JOIN unit_kerja_ref ON unitkerjaId = gedungUnitkerjaId AND gedungNama is not null
WHERE
	gedungUnitkerjaId = %s OR (unitkerjaParentId = %s OR '1' = %s)
";

$sql['get_combo_gedung'] = "
SELECT 
	a.gedungId AS id,
 	a.gedungNama AS name
FROM
  (SELECT g1.gedungId,g1.gedungNama
   FROM gedung g1
   LEFT JOIN unit_kerja_ref ON unitkerjaId = g1.gedungUnitkerjaId
   WHERE (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
   UNION
   SELECT g2.gedungId,g2.gedungNama 
   FROM gedung g2
   LEFT JOIN ruang ON ruangGedungId = g2.gedungId
   LEFT JOIN unit_kerja_ref u3 ON u3.unitkerjaId = g2.gedungUnitKerjaId
   LEFT JOIN unit_kerja_ref u2 ON u2.unitkerjaId = ruangUnitId
   WHERE (u3.unitkerjaKodeSistem != '%s' AND u3.unitkerjaKodeSistem NOT LIKE '%s') AND (u2.unitkerjaKodeSistem = '%s' OR u2.unitkerjaKodeSistem LIKE '%s')
  ) a
";

$sql['get_combo_lantai'] = "
SELECT 
   lantaiId AS id,
   CONCAT(lantaiKode, ' - ', lantaiNama) AS `name` 
FROM
   aset_lantai 
WHERE lantaiGedungId = '%s' 
";

$sql['template'] =
   ("
      INSERT INTO
      VALUES
      SELECT
      UPDATE
      SET
      DELETE
      FROM
      WHERE
      GROUP BY
      ORDER BY
      LIMIT
   ");

// get
$sql['get_gedung_lantai'] =
   ("
      SELECT
		  gedungId   AS gedung_id,
		  gedungNama,
		  gedungKode,
		  lantaiId,
		  lantaiKode,
		  lantaiNama
		FROM gedung
		  LEFT JOIN aset_lantai
			 ON lantaiGedungId = gedungId
		WHERE lantaiId = %s
   ");

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

$sql['get_data_gedung_by_id'] =
   ("
      SELECT
         gedungId as gedungId,
         gedungKode as gedungKode,
         gedungNama as gedungNama
      FROM
         gedung
      WHERE
         gedungId = %d
   ");

$sql['get_list_count'] =
   ("
      SELECT
         COUNT(ruangId) as total
      FROM
         ruang
			  LEFT JOIN aset_lantai
				ON lantaiId = ruangLantaiId
      WHERE
         ruangGedungId = %d AND
         (ruangJenisRuangId = %d OR 'all' = %s) AND
         (ruangTipeRuangId = %d OR 'all' = %s) AND
         (ruangKode LIKE %s OR ruangNama LIKE %s) AND
			(lantaiId = %d)
   ");

$sql['get_count']="
SELECT FOUND_ROWS() AS total
";

$sql['get_list'] =
   ("
      SELECT
   		  SQL_CALC_FOUND_ROWS
		  ruangId        AS id,
		  ruangKode      AS kode,
		  ruangNama      AS nama,
		  ruangKapasitas AS kapasitas,
		  ruangLuasRuang AS luas,
		  ruangKapasitas AS dayaTampung,
		  tipeRuangNama  AS tipeRuang,
		  kondisibrgNama AS kondisi,
		  picNama        AS pic,
		  ruangPhoto     AS foto,
		  gedungId       AS gedung_id,
		  gedungNama,
		  gedungKode,
		  gedungKampusId,
		  lantaiId,
		  lantaiKode,
		  lantaiNama
		FROM ruang
		  LEFT JOIN tipe_ruang_ref
			 ON (tipeRuangId = ruangTipeRuangId)
		  LEFT JOIN kondisi_barang_ref
			 ON (kondisibrgId = ruangKondisiBarangId)
		  LEFT JOIN pic_ref
			 ON (picId = ruangPicId)
		  LEFT JOIN gedung
			 ON gedungId = ruangGedungId
		  LEFT JOIN aset_lantai
			 ON lantaiId = ruangLantaiId
   		  LEFT JOIN unit_kerja_ref ON unitkerjaId = ruangUnitId
		WHERE
         ruangGedungId = %d AND
         (ruangJenisRuangId = %d OR 'all' = %s) AND
         (ruangTipeRuangId = %d OR 'all' = %s) AND
         (ruangKode LIKE %s OR ruangNama LIKE %s) AND
		 (lantaiId = %d) AND
   		 (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
      LIMIT
         %d, %d
   ");

$sql['get_list_by_gedung_id_count'] =
   ("
      SELECT
         COUNT(ruangId) as total
      FROM
         ruang
      WHERE
         ruangGedungId = %d
   ");

$sql['get_list_by_gedung_id'] =
   ("
      SELECT
         ruangId as id,
         ruangKode as kode,
         ruangNama as nama,
         ruangLuasRuang as luas,
         ruangKapasitas as dayaTampung,
         tipeRuangNama as tipeRuang,
         kondisibrgNama as kondisi,
         picNama as pic,
         ruangPhoto as foto
      FROM
         ruang
         LEFT JOIN tipe_ruang_ref ON (tipeRuangId = ruangTipeRuangId)
         LEFT JOIN kondisi_barang_ref ON (kondisibrgId = ruangKondisiBarangId)
         LEFT JOIN pic_ref ON (picId = ruangPicId)
      WHERE
         ruangGedungId = %d
      LIMIT
         %d, %d
   ");

$sql['get_last_ruang_id'] =
   ("
      SELECT
         MAX(ruangId) as id
      FROM
         ruang
   ");

// $sql['get_fasilitas_by_ruang_id'] =
//    ("
//       SELECT
//          ruangFasilitasFasilitasId as id
//       FROM
//          ruang_fasilitas
//       WHERE
//          ruangFasilitasRuangId = %d
//    ");

$sql['get_fasilitas_by_ruang_id'] = "
SELECT
   a.ruangNama,
   b.fasilitasId AS id_fasilitas,
   b.fasilitasNama AS nama_fas,
   c.ruangFasilitasFasilitasId AS id_fas_ruang,
   c.ruangFasilitasJumlah AS jml_fasilitas
FROM
   ruang a,
   fasilitas_ref b
   LEFT JOIN ruang_fasilitas c ON b.fasilitasId = c.ruangFasilitasFasilitasId AND c.ruangFasilitasRuangId = %s
   LEFT JOIN ruang d ON c.ruangFasilitasRuangId = d.ruangId AND d.ruangId = %s
WHERE
   a.ruangId = %s
ORDER BY
   b.fasilitasNama ASC
";

$sql['get_konstruksi_by_ruang_id'] =
   ("
      SELECT
         ruangKonstruksiKonstruksiRuangId as id,
         ruangKonstruksiJenisKonstruksiId as value
      FROM
         ruang_konstruksi
      WHERE
         ruangKonstruksiRuangId = %d
   ");

$sql['get_data_by_id'] =
   ("
      SELECT
         ruangId as id,
         ruangKode as kode,
         ruangNama as nama,
         ruangGedungId as gId,
         ruangJenisRuangId as jnsRuangId,
         ruangKapasitas as kapasitas,
         ruangLuasRuang as luas,
         ruangPanjang as panjang,
         ruangLebar as lebar,
         ruangTipeRuangId as tipeRuangId,
         ruangKondisiBarangId as kondisiRuangId,
         ruangUnitId as unit_id,
         (SELECT unitKerjaNama FROM unit_kerja_ref WHERE unitkerjaId = ruangUnitId) as unit_nama,
         ruangPicId as picId,
         ruangPhoto as oldFoto,
			ruangWarnaCat as warna_cat,
			ruangMerkCat as merk_cat ,
			ruangNoSeriCat as nomor_cat,
			lantaiId
		FROM ruang
		  LEFT JOIN aset_lantai
			 ON lantaiId = ruangLantaiId
      WHERE
         ruangId = %d
   ");

$sql['get_fasilitas_list'] =
   ("
      SELECT
         fasilitasId as id_fasilitas,
         fasilitasNama as nama_fas
      FROM
         fasilitas_ref
      ORDER BY
         fasilitasNama
   ");

$sql['get_konstruksi_ruang_list'] =
   ("
      SELECT
         konstruksiId as id,
         konstruksiNama as name
      FROM
         konstruksi_ruang_ref
      ORDER BY
         konstruksiId
   ");

//untuk print rtf Ruang---
$sql['get_data_print_ruang'] =
   ("
      SELECT
         ruangId as id,
         ruangKode as kode,
         ruangNama as nama,
			ruangKapasitas as kapasitas,
         ruangLuasRuang as luas,
         ruangKapasitas as dayaTampung,
         tipeRuangNama as tipeRuang,
         kondisibrgNama as kondisi,
         picNama as pic,
         ruangPhoto as foto,
		 gedungKode AS kode_gedung,
		 gedungNama AS nama_gedung
      FROM
         ruang
         LEFT JOIN tipe_ruang_ref ON (tipeRuangId = ruangTipeRuangId)
         LEFT JOIN kondisi_barang_ref ON (kondisibrgId = ruangKondisiBarangId)
         LEFT JOIN pic_ref ON (picId = ruangPicId)
			LEFT JOIN gedung ON gedungId=ruangGedungId
			JOIN aset_lantai ON lantaiId = ruangLantaiId
      WHERE
         ruangGedungId = %d AND
         (ruangJenisRuangId = %d OR 'all' = %s) AND
         (ruangTipeRuangId = %d OR 'all' = %s) AND
         (ruangKode LIKE %s OR ruangNama LIKE %s) AND
			lantaiId = %s
   ");

// combo box
$sql['combo_jenis_konstruksi'] =
   ("
      SELECT
         jeniskonId as id,
         jeniskonNama as name
      FROM
         jenis_konstruksi_ref
      ORDER BY
         jeniskonNama
   ");

$sql['combo_jenis_ruang'] =
   ("
      SELECT
         jenisruangId as id,
         jenisruangNama as name
      FROM
         jenis_ruang_ref
      ORDER BY
         jenisruangNama
   ");

$sql['combo_tipe_ruang'] =
   ("
      SELECT
         tipeRuangId as id,
         tipeRuangNama as name
      FROM
         tipe_ruang_ref
      ORDER BY
         tipeRuangNama
   ");

$sql['combo_kondisi_ruang'] =
   ("
      SELECT
         kondisibrgId as id,
         kondisibrgNama as name
      FROM
         kondisi_barang_ref
      ORDER BY
         kondisibrgId
   ");

$sql['combo_pic_ruangan'] =
   ("
      SELECT
         picId as id,
         picNama as name
      FROM
         pic_ref
      ORDER BY
         picNama
   ");

// Do Query
$sql['add_ruang'] =
   ("
      INSERT INTO
         ruang
            (
               ruangKode,
               ruangNama,
               ruangGedungId,
               ruangJenisRuangId,
               ruangKapasitas,
               ruangLuasRuang,
               ruangPanjang,
               ruangLebar,
               ruangTipeRuangId,
               ruangKondisiBarangId,
               ruangUnitId,
               ruangPicId,
               ruangPhoto,
               ruangTglUbah,
					ruangWarnaCat,
					ruangMerkCat,
					ruangNoSeriCat,
               ruangUserId,
					ruangLantaiId
            )
      VALUES
         (%d, '%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, '%s', now(), '%s', '%s', '%s', '%s', %s)
   ");

$sql['update_ruang'] =
   ("
      UPDATE
         ruang
      SET
         ruangKode = %d,
         ruangNama = %s,
         ruangGedungId = %d,
         ruangJenisRuangId = %d,
         ruangKapasitas = %d,
         ruangLuasRuang = %d,
         ruangPanjang = %d,
         ruangLebar = %d,
         ruangTipeRuangId = %d,
         ruangKondisiBarangId = %d,
         ruangUnitId = %d,
         ruangPicId = %d,
         ruangPhoto = '%s',
         ruangTglUbah = now(),
		 ruangWarnaCat = '%s',
		 ruangMerkCat = '%s',
		 ruangNoSeriCat = '%s',
         ruangUserId = '%s'
      WHERE
         ruangId = %d
   ");

// $sql['delete_fasilitas_ruang'] =
//    ("
//       DELETE FROM
//          ruang_fasilitas
//       WHERE
//          ruangFasilitasRuangId = %d
//    ");

$sql['delete_fasilitas_ruang'] = "
DELETE FROM
   ruang_fasilitas
WHERE
   ruangFasilitasRuangId = %s
   AND ruangFasilitasFasilitasId = %s
";

// $sql['add_fasilitas_ruang'] =
//    ("
//       INSERT INTO
//          ruang_fasilitas
//       VALUES
//          %REPLACE%
//    ");

$sql['get_max_id_ruang'] = "
SELECT
   MAX(ruangId) AS id
FROM
   ruang
";

$sql['add_fasilitas_ruang'] = "
INSERT INTO
   ruang_fasilitas
VALUES
   (%s,%s,%s)
";

$sql['delete_konstruksi_ruang'] =
   ("
      DELETE FROM
         ruang_konstruksi
      WHERE
         ruangKonstruksiRuangId = %d
   ");

$sql['add_konstruksi_ruang'] =
   ("
      INSERT INTO
         ruang_konstruksi
      VALUES
         %REPLACE%
   ");

$sql['delete_ruang'] =
   ("
      DELETE FROM
         ruang
      WHERE
         ruangId IN (%REPLACE%)
   ");

$sql['get_inventaris_ruang'] = "
   SELECT
      a.ruangKode AS ruang_kode,
      a.ruangNama AS ruang_nama,
      b.jenisruangNama AS ruang_jenis,
      c.gedungKode AS gedung_kode,
      c.gedungNama AS gedung_nama,
      a.ruangKapasitas AS ruang_kapasitas,
      a.ruangLuasRuang AS ruang_luas,
      d.tipeRuangNama AS ruang_tipe,
      e.jenisruangNama AS ruang_jenis
   FROM
      ruang a
      LEFT JOIN jenis_ruang_ref b ON a.ruangJenisRuangId =  jenisruangId
      LEFT JOIN tipe_ruang_ref d ON a.ruangTipeRuangId = d.tipeRuangId
      LEFT JOIN jenis_ruang_ref e ON a.ruangJenisRuangId = e.jenisruangId
      LEFT JOIN gedung c ON a.ruangGedungId = c.gedungId
   WHERE
      a.ruangId = '%s'
";

$sql['get_list_inventaris_ruang'] = "
   SELECT 
	   invDetKodeBarang AS aset_kode,
	   b.invDetMstLabel AS aset_label,
	   h.kondisibrgNama AS kondisi 
	FROM
	   inventarisasi_mst a 
	   LEFT JOIN inventarisasi_detail b 
		  ON a.invMstId = b.invDetMstId 
	   LEFT JOIN aset_ref_aset_status 
		  ON statusAsetId = invDetAsetStatusId 
	   LEFT JOIN barang_ref c 
		  ON a.invBarangId = barangId 
	   LEFT JOIN kampus_ref d 
		  ON b.invDetKampus = d.kampusId 
	   LEFT JOIN 
		  (SELECT 
			 (
				IF(
				   tempUnitId IS NULL,
				   unitkerjaId,
				   unitkerjaId
				)
			 ) AS unitkerjaId,
			 LPAD(
				IF(
				   tempUnitKode IS NULL,
				   unitkerjaKode * 100,
				   tempUnitKode * 100+ unitkerjaKode
				),
				4,
				'0'
			 ) AS unitkerjaKode,
			 (
				IF(
				   tempUnitNama IS NULL,
				   unitkerjaNama,
				   unitkerjaNama
				)
			 ) AS unitkerjaNama,
			 unitkerjaParentId 
		  FROM
			 unit_kerja_ref 
			 LEFT JOIN 
				(SELECT 
				   unitkerjaId AS tempUnitId,
				   unitkerjaKode AS tempUnitKode,
				   unitkerjaNama AS tempUnitNama,
				   unitkerjaParentId AS tempParentId 
				FROM
				   unit_kerja_ref 
				WHERE unitkerjaParentId = 0) tmpUnitKerja 
				ON (unitkerjaParentId = tempUnitId)) e 
		  ON b.invDetUnitKerja = e.unitkerjaId 
	   LEFT JOIN kepemilikan_barang_ref f 
		  ON f.kepemilikanbrgId = b.invDetKepemilikan 
	   LEFT JOIN kondisi_barang_ref h 
		  ON h.kondisibrgId = b.invDetKondisiId 
	   LEFT JOIN sub_kelompok_barang_ref i 
		  ON barangSubkelbrgId = subkelbrgId 
	   LEFT JOIN kelompok_barang_ref j 
		  ON subkelbrgKelbrgId = kelbrgId 
	   LEFT JOIN bidang_barang_ref k 
		  ON kelbrgBidangbrgId = bidangbrgId 
	   LEFT JOIN golongan_barang_ref l 
		  ON bidangbrgGolbrgId = golbrgId 
	WHERE b.invDetRuanganId = '%s' 
	   AND statusAsetId != '3' 
	ORDER BY invDetKodeBarang ASC
";

$sql['cek_ruang'] = "
   SELECT
      ruangId AS id_ruang
   FROM
      ruang
   WHERE
      ruangKode = '%s'
";

#unit_pengguna_ruang
$sql['get_unit_pengguna_ruang'] = "
   SELECT
      a.unitKerPengRuangId AS id,
      a.unitKerPengRuangUnit AS unit_id,
      b.unitkerjaNama AS unit_nama,
      a.unitKerPengRuangRuang AS ruang_id
   FROM
      unit_ker_peng_ruang a
      JOIN unit_kerja_ref b ON a.unitKerPengRuangUnit = b.unitkerjaId
   WHERE
      a.unitKerPengRuangRuang = '%s'
";

$sql['insert_unit_pengguna_ruang'] = "
   INSERT INTO unit_ker_peng_ruang
      (unitKerPengRuangUnit, unitKerPengRuangRuang)
   VALUES
      ('%s', '%s')
";

$sql['update_pindah_ruang'] = "
   UPDATE ruang SET
	   ruangGedungId = '%s',
	   ruangLantaiId = '%s',
	   ruangTglUbah = NOW(),
	   ruangUserId = '%s'
	WHERE
		ruangId = '%s'
";

$sql['delete_pengguna_ruang'] = "
   DELETE FROM
      unit_ker_peng_ruang
   WHERE
      unitKerPengRuangRuang = %s
";

?>
