<?php
$sql['get_user_kodesistem']="
SELECT unitkerjaKodeSistem
FROM unit_kerja_ref
LEFT JOIN user_unit_kerja ON userunitkerjaUnitkerjaId = unitkerjaId
WHERE userunitkerjaUserId = %s
";

$sql['get_data_unit_kerja_by_id']="
SELECT unitkerjaKodeSistem, unitkerjaNama, unitkerjaKode
FROM unit_kerja_ref
WHERE unitkerjaId = %s
";


$sql['get_user_data']="
SELECT UserId, RealName, UserName
FROM gtfw_user
WHERE UserId = %s
";

$sql['get_inv_oldest_date']="
SELECT YEAR(MIN(invDetTglPembelian)) AS oldest_date 
FROM inventarisasi_detail
LEFT JOIN unit_kerja_ref ON unitkerjaId = invDetUnitKerja
WHERE unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s'
";

$sql['get_data_laporan']="
SELECT 
	golbrgId AS id,
	golbrgNama AS golongan,
	golbrgCoaKode AS kode,
	-- SUM(IF(golbrgId = 2,mstPenystnNilaiPerolehan,invDetNilaiPerolehanSatuan)) AS jumlah
	SUM(IF(mstPenystnDisusutkan != 0,mstPenystnDisusutkan,invDetNilaiPerolehanSatuan)) AS jumlah  
FROM inventarisasi_detail
LEFT JOIN golongan_barang_ref ON golbrgId = invDetGolId
LEFT JOIN unit_kerja_ref ON unitkerjaId = invDetUnitKerja
LEFT JOIN penyusutan_brg_mst ON mstPenystnBarangId = invDetId
WHERE
	(unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
	AND invDetTglPembelian <= '%s'
	AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
GROUP BY invDetGolId
";

$sql['get_data_unitkerja'] = "
SELECT
SQL_CALC_FOUND_ROWS
LPAD((if(tempUnitKode IS NULL,a.unitkerjaKode,tempUnitKode))*100,4,'0') AS kodesatker,
(if(tempUnitNama IS NULL,a.unitkerjaNama,tempUnitNama)) AS satker,
(if(tempUnitId IS NULL,a.unitkerjaId,a.unitkerjaId)) AS id,
a.unitkerjaKode /*LPAD(if(tempUnitKode IS NULL,unitkerjaKode*100,tempUnitKode*100+unitkerjaKode),4,'0')*/ AS kodeunit,
(if(tempUnitNama IS NULL,a.unitkerjaNama,a.unitkerjaNama)) AS unit,
a.unitkerjaParentId AS parentId,
(SELECT COUNT(b.unitkerjaId) FROM unit_kerja_ref b WHERE b.unitkerjaParentId = a.unitkerjaId) AS isParent
FROM unit_kerja_ref a
LEFT JOIN
(SELECT
unitkerjaId AS tempUnitId,
unitkerjaKode AS tempUnitKode,
unitkerjaNama AS tempUnitNama,
unitkerjaParentId AS tempParentId
FROM unit_kerja_ref
WHERE unitkerjaParentId = 0) tmpUnitKerja ON (a.unitkerjaParentId=tempUnitId)
WHERE
(a.unitkerjaKode /*LPAD(if(tempUnitKode IS NULL,unitkerjaKode*100,tempUnitKode*100+unitkerjaKode),4,'0')*/ LIKE '%s' OR
LPAD((if(tempUnitKode IS NULL,unitkerjaKode,tempUnitKode))*100,4,'0') LIKE '%s' ) AND
(a.unitkerjaNama LIKE '%s' OR tempUnitNama LIKE '%s') AND
(a.unitkerjaKodeSistem = '%s' OR a.unitkerjaKodeSistem LIKE '%s')
GROUP BY a.unitkerjaId
ORDER BY a.unitkerjaKodeSistem
LIMIT %s, %s
";

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

# ======= Inventarisasi Detail
$sql['get_inventarisasi'] = "
SELECT
SQL_CALC_FOUND_ROWS
invDetId AS id,
invDetKodeBarang AS KIB,
invDetBarangId AS id_barang,
barangKode AS kode_barang,
invDetMstLabel AS nama_aset,
CONCAT(kampusNama,IF(gedungNama IS NULL,'',CONCAT('@@',gedungNama,IF(ruangNama IS NULL,'',CONCAT('@@',ruangNama))))) AS lokasi_aset,
unitkerjaNama AS unit_pj,
unitkerjaKodeSistem,
LEFT(invDetTglPembelian, 4) AS thn_pengadaan,
IF(mstPenystnNilaiPerolehan IS NOT NULL, mstPenystnNilaiPerolehan,IF(invDetNilaiPerolehanSatuan IS NULL, invTotalPerolehan, invDetNilaiPerolehanSatuan)) AS nilai_perolehan,
IF(mstPenystnDisusutkan IS NOT NULL, mstPenystnDisusutkan,IF(invDetNilaiPerolehanSatuan IS NULL, invTotalPerolehan, invDetNilaiPerolehanSatuan)) AS nilai_buku,
kepemilikanbrgNama AS status_kepemilikan,
IF(statusAsetNama != 'Aktif',CONCAT(kondisibrgNama,' (',statusAsetNama,')'),kondisibrgNama) AS kondisi,
golbrgId AS golBarang
FROM
inventarisasi_detail
LEFT JOIN inventarisasi_mst ON invDetMstId = invMstId
LEFT JOIN barang_ref ON invDetBarangId = barangId
LEFT JOIN kampus_ref ON invDetKampus = kampusId
LEFT JOIN unit_kerja_ref  ON invDetUnitKerja = unitkerjaId
LEFT JOIN kepemilikan_barang_ref ON kepemilikanbrgId = invDetKepemilikan
LEFT JOIN kondisi_barang_ref ON kondisibrgId = invDetKondisiId
LEFT JOIN sub_kelompok_barang_ref ON invDetSubkelbrgId = subkelbrgId
LEFT JOIN kelompok_barang_ref ON invDetKelbrgId = kelbrgId
LEFT JOIN bidang_barang_ref ON invDetBidangbrngId = bidangbrgId
LEFT JOIN golongan_barang_ref ON invDetGolId = golbrgId
LEFT JOIN gedung ON invDetGedungId = gedungId
LEFT JOIN ruang ON invDetRuanganId = ruangId
LEFT JOIN penyusutan_brg_mst ON invDetId = mstPenystnBarangId
LEFT JOIN aset_ref_aset_status ON statusAsetId = invDetAsetStatusId
LEFT JOIN do_detail_new ON doDetInvDetId = invDetId
WHERE
[CARI_GOL][CARI_THN][CARI_SUMBER][CARI_KEPEMILIKAN][CARI_KAMPUS]
[CARI_KONDISI][CARI_STATUS][CARI_UNIT]
(
invSPMNumber LIKE '%s' OR invDetMstLabel LIKE '%s' OR
invDetKodeBarang LIKE '%s'
)
/*tampilkan hasil konversi*/
AND (/*invMstPengadaanId = 1 OR */invMstPengadaanId IS NULL
/*tampilkan pengadaan yang sudah di DO dan diterima*/
OR (/*invMstPengadaanId <> 1 AND */invMstPengadaanId IS NOT NULL AND doDetId IS NOT NULL AND doDetRuangId IS NOT NULL))
ORDER BY
invDetTglPembelian DESC,invDetKodeBarang ASC
LIMIT %s, %s
";

#combo
$sql['get_combo_jenis_kib'] = "
SELECT
golbrgId AS id,
golbrgKibKode AS kode,
golbrgNama AS name
FROM
golongan_barang_ref
ORDER BY golbrgNama
";

$sql['get_combo_status_aset'] = "
SELECT
statusAsetId   AS id,
statusAsetNama AS `name`
FROM aset_ref_aset_status
";

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

$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
";

?>