<?php
$sql['get_data_user_by_username'] = "
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,
userunitkerjaUnitkerjaId AS unit_kerja_id,
userunitkerjaRoleId AS role_id,
unitkerjaKodeSistem,
unitkerjaNama
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_jenis_kib'] = "
	SELECT
		golbrgId AS id,
		golbrgNama AS name
	FROM
		golongan_barang_ref
	ORDER BY golbrgNama
";

$sql['get_combo_lokasi'] = "
	SELECT 
		ruangId AS id,
		ruangNama AS name
	FROM 
		ruang
";

$sql['get_combo_unit'] = "
	SELECT 
		unitkerjaId AS id,
		unitkerjaNama AS name
	FROM 
		unit_kerja_ref
	WHERE (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
	ORDER BY Name
";

$sql['get_combo_unit_tujuan'] = "
	SELECT 
		unitkerjaId AS id,
		unitkerjaNama AS name
	FROM 
		unit_kerja_ref
	WHERE 
		unitkerjaId != '%s'
	ORDER BY Name
";

$sql['get_lokasi_barang'] = "
	SELECT 
		ruangId AS id,
		ruangNama AS name
	FROM ruang
	WHERE ruangUnitId = '%s' AND ruangLantaiId != '' [UNION]
";

$sql['barang_lokasi'] = "
	SELECT
		invDetKodeBarang AS aset_kode,
		b.invDetMstLabel AS aset_label,
		b.invDetMerek AS merek,
		b.invDetSpesifikasi AS spesifikasi
	   FROM
		  inventarisasi_mst a
		  LEFT JOIN inventarisasi_detail b ON a.invMstId = b.invDetMstId
		  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,
				unitkerjaKode 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
		  JOIN (SELECT invKondisiBrgBrgDetId, invKondisiBrgKondId FROM (SELECT * FROM inv_kondisi_history ORDER BY invKondisiBrgTglKondisi DESC) a GROUP BY invKondisiBrgBrgDetId) g ON g.invKondisiBrgBrgDetId = b.invDetId
		  JOIN kondisi_barang_ref h ON h.kondisibrgId = g.invKondisiBrgKondId
		  JOIN sub_kelompok_barang_ref i ON barangSubkelbrgId = subkelbrgId
		  JOIN kelompok_barang_ref j ON subkelbrgKelbrgId = kelbrgId
		  JOIN bidang_barang_ref k ON kelbrgBidangbrgId = bidangbrgId
		  JOIN golongan_barang_ref l ON bidangbrgGolbrgId = golbrgId
	   WHERE
		  b.invDetRuanganId = '%s'
		  ORDER BY b.invDetMstLabel ASC;
";

$sql['add_mutasi_master'] = "
	INSERT INTO mutasi_aset_mst(
		mtsAsetMstUnitIdAsal,
		mtsAsetMstLokasiIdAsal,
		mtsAsetMstUnitIdTuj,
		mtsAsetMstLokasiIdTuj,
		mtsAsetMstTglMutasi,
		mtsAsetMstPIC,
		mtsAsetMstBAMutasi,
		mtsAsetMstFile,
		mtsAsetMstKet,
		mstAsetMstUserId,
		mstAsetMstTglUbah
	)VALUES(
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		(SELECT userId FROM gtfw_user WHERE userName = '%s'),
		now()
	)
";

$sql['add_mutasi_detail'] = "
	INSERT INTO 
		mutasi_aset_det(mtsAsetDetMstId,mtsAsetDetBrgId) 
	SELECT 
		mtsAsetMstId,'%s' 
	FROM 
		mutasi_aset_mst 
	ORDER BY 
		mtsAsetMstId 
	DESC LIMIT 0,1
";

$sql['update_mutasi']="
	UPDATE mutasi_aset_mst SET 
		mtsAsetMstUnitIdAsal = '%s',
		mtsAsetMstLokasiIdAsal = '%s',
		mtsAsetMstUnitIdTuj = '%s',
		mtsAsetMstLokasiIdTuj = '%s',
		mtsAsetMstTglMutasi = '%s',
		mtsAsetMstPIC = '%s',
		mtsAsetMstBAMutasi = '%s',
		mtsAsetMstFile = '%s',
		mtsAsetMstKet = '%s',
		mstAsetMstUserId = (SELECT userId FROM gtfw_user WHERE userName = '%s'),
		mstAsetMstTglUbah = NOW()
	WHERE 
		mtsAsetMstId = '%s'
";

$sql['update_data_inventarisasi'] = "
	UPDATE inventarisasi_detail
	SET 
		invDetGedungId = '%s',
		invDetRuanganId = '%s',
		invDetUnitKerja = '%s',
		invDetTglUbah = NOW()
	WHERE invDetId = '%s'
";

$sql['add_mutasi_detail_update'] = "
	INSERT INTO 
		mutasi_aset_det(mtsAsetDetMstId,mtsAsetDetBrgId) 
	VALUES
		('%s','%s')
";

$sql['add_inventarisasi_lokasi_history'] = "
	INSERT INTO inventarisasi_lokasi_history
	SET 
	  invHisInvDetId = %s,
	  invHisGedungId = '%s',
	  InvHisKampusId = (SELECT
								 invDetKampus
							  FROM inventarisasi_detail
							  WHERE invDetId =  '%s'),
	  InvHisRuangId =  '%s',
	  InvHisTanggal = NOW(),
	  InvHisUserId = '%s'
";

$sql['get_data_gedung'] = "
	SELECT
	  ruangGedungId AS gedung_id
	FROM ruang
	  LEFT JOIN mutasi_aset_mst
		 ON ruangId = mtsAsetMstLokasiIdTuj
	WHERE ruangId = %s
	ORDER BY mtsAsetMstId DESC
	LIMIT 1
";

$sql['delete_mutasi_detail'] = "
	DELETE FROM mutasi_aset_det WHERE mtsAsetDetMstId = '%s'
";

$sql['get_list_mutasi_old'] = "
	SELECT 
		mtsAsetMstId AS id,
		mtsAsetMstTglMutasi AS tgl,
		mtsAsetMstBAMutasi AS berita_acara,
		mtsAsetMstPIC AS pic
	FROM 
		mutasi_aset_mst 
	LEFT JOIN mutasi_aset_det ON mtsAsetDetMstId = mtsAsetMstId
	LEFT JOIN inventarisasi_detail ON mtsAsetDetBrgId = invDetId
	LEFT JOIN inventarisasi_mst ON invDetMstId = invMstId
	JOIN barang_ref ON invBarangId = barangId
	JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
	JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
	JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
	JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
	LEFT JOIN unit_kerja_ref b ON b.unitkerjaId = mtsAsetMstUnitIdTuj
	LEFT JOIN ruang d ON d.ruangId = mtsAsetMstLokasiIdTuj
	WHERE 
		golbrgKode LIKE '%s' 
	AND 
		mtsAsetMstTglMutasi BETWEEN '%s' AND '%s'
	AND 
		(invDetKodeBarang LIKE '%s'
	OR 
		barangNama LIKE '%s')
	AND 
		mtsAsetMstUnitIdTuj LIKE '%s'
	AND 
		mtsAsetMstLokasiIdTuj LIKE '%s'
	GROUP BY mtsAsetMstId
	ORDER BY mtsAsetMstTglMutasi DESC
";

$sql['get_list_mutasi']="
SELECT 
	mtsAsetMstId AS id,
	mtsAsetMstTglMutasi AS tgl,
	mtsAsetMstBAMutasi AS berita_acara,
	mtsAsetMstPIC AS pic,
	b.namaBarang,
	c.unitkerjaNama AS unitAsal,
	d.unitkerjaNama AS unitTujuan,
	b.cantDelete
FROM mutasi_aset_mst a
LEFT JOIN 
	(SELECT 
     md.mtsAsetDetMstId, 
     GROUP_CONCAT(CONCAT('|',invDetGolId,'|') SEPARATOR ',') AS golId,
     GROUP_CONCAT(invDetKodeBarang SEPARATOR ', ') AS kodeBarang,
     GROUP_CONCAT(invDetMstLabel SEPARATOR '<br>') AS namaBarang,
     GROUP_CONCAT((
		   SELECT IF(COUNT(md2.mtsAsetDetId) > 0,1,0)
		   FROM mutasi_aset_det md2 
		   WHERE 
		      md2.mtsAsetDetBrgId = md.mtsAsetDetBrgId AND 
		      md2.mtsAsetDetId > md.mtsAsetDetId) SEPARATOR '') AS cantDelete
	 FROM mutasi_aset_det md
     LEFT JOIN inventarisasi_detail ON invDetId = md.mtsAsetDetBrgId
     GROUP BY md.mtsAsetDetMstId) b
ON b.mtsAsetDetMstId = a.mtsAsetMstId
LEFT JOIN unit_kerja_ref c ON c.unitkerjaId = a.mtsAsetMstUnitIdAsal
LEFT JOIN unit_kerja_ref d ON d.unitkerjaId = a.mtsAsetMstUnitIdTuj
WHERE 
	(c.unitkerjaKodeSistem = '%s' OR c.unitkerjaKodeSistem LIKE '%s')
	AND a.mtsAsetMstTglMutasi BETWEEN '%s' AND '%s'
	[SEARCH_GOLBRG]
	[SEARCH_KODEBRG_NAMABRG]
	[SEARCH_UNITTUJUAN]
	[SEARCH_LOKASI]
ORDER BY a.mtsAsetMstTglMutasi DESC
";

$sql['get_detail_mutasi'] = "
	SELECT 
		mtsAsetMstTglMutasi AS tgl,
		mtsAsetMstBAMutasi AS ba_mutasi,
		mtsAsetDetBrgId AS barangId,
		invDetKodeBarang AS kode_aset,
		barangNama AS nama_aset,
		e.invDetMerek AS merk,
		e.invDetSpesifikasi AS spesifikasi,
		a.unitkerjaId AS unit_asal_id,
		a.unitkerjaNama AS unit_asal,
		b.unitkerjaNama AS unit_tujuan,
		b.unitkerjaId AS unit_tujuan_id,
		c.ruangNama AS lokasi_asal,
		c.ruangId AS lokasi_asal_id,
		d.ruangNama AS lokasi_tujuan,
		d.ruangId AS lokasi_tujuan_id,
		mtsAsetMstPIC AS pic,
		mtsAsetMstKet AS keterangan,
		mtsAsetMstFile AS FILE
	FROM 
		mutasi_aset_mst 
	LEFT JOIN mutasi_aset_det ON mtsAsetDetMstId = mtsAsetMstId
	LEFT JOIN inventarisasi_detail e ON mtsAsetDetBrgId = invDetId
	LEFT JOIN inventarisasi_mst ON invDetMstId = invMstId
	JOIN barang_ref ON invBarangId = barangId
	JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
	JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
	JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
	JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
	LEFT JOIN unit_kerja_ref a ON a.unitkerjaId = mtsAsetMstUnitIdAsal
	LEFT JOIN unit_kerja_ref b ON b.unitkerjaId = mtsAsetMstUnitIdTuj
	LEFT JOIN ruang c ON c.ruangId = mtsAsetMstLokasiIdAsal
	LEFT JOIN ruang d ON d.ruangId = mtsAsetMstLokasiIdTuj
	WHERE 
		mtsAsetMstId = '%s'
";

$sql['delete_mutasi']="
	DELETE FROM mutasi_aset_mst WHERE mtsAsetMstId = '%s'
";

$sql['delete_new_history']="
DELETE FROM inventarisasi_lokasi_history
WHERE invHisInvDetId = %s AND invHisLokasiId > %s
";

$sql['get_history_lokasi_barang_id'] = "
SELECT invHisLokasiId
FROM inventarisasi_lokasi_history
WHERE
	invHisInvDetId = '%s' AND
	InvHisKampusId = (SELECT invDetKampus FROM inventarisasi_detail WHERE invDetId =  '%s') AND
	invHisGedungId = %s AND
	InvHisRuangId = %s
";

?>
