<?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
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_unit'] = "
	SELECT 
		unitkerjaId AS id,
		unitkerjaNama AS name
	FROM unit_kerja_ref
	LEFT JOIN 
			inventarisasi_detail ON invDetUnitKerja = unitkerjaId
	WHERE
		invDetUnitKerja = %s OR unitkerjaParentId = %s OR '1' = %s
	GROUP BY id
	ORDER BY name
";

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

$sql['get_kondisi_barang'] = "
	SELECT
		kondisibrgId,
		kondisibrgNama
	FROM 
		kondisi_barang_ref
	ORDER BY kondisibrgId
";

$sql['get_combo_pic'] = "
	SELECT 
		ruangId AS id,
		CONCAT(ruangKode,' - ',ruangNama) AS name
	FROM ruang
	WHERE 
		ruangGedungId = '%s'
		";
		
//query untuk report masih perlu di benahi di sub query nya apabila data sudah mulai membengkak /  banyak.
$sql['get_list_li'] = "
	SELECT
		unitkerjaKode, 
		unitkerjaNama, 
		CONCAT(LPAD(golbrgKode, 1, '0'), '.', LPAD(bidangbrgKode, 2, '0'), '.', LPAD(kelbrgKode, 2, '0'), '.', LPAD(subkelbrgKode, 2, '0'), '.', LPAD(barangKode, 3, '0')) as kode_barang, 
		barangNama AS nama_barang, 
		a.invMerek AS merk, 
		invTipe AS tipe,
		date_format(a.invTglPembelian, '%%Y') AS tahun_beli_buat, 
		count(invDetId) AS jumlah_barang, 
		satuanbrgNama AS satuan, 
		sumberdanaNama AS asal,
		a.invNilaiPerolehanSatuan AS satuan_perolehan, 
		count(invDetId)*a.invNilaiPerolehanSatuan AS jumlah_perolehan, 
		invPengBrgId,
		jumlahPeng,
		ada_bk,
		tidak_ada_bk,
		invKondisiBrgKondId,
		jumlahPerKond,
		count(invDetId) AS ada_kib,
		0 as tidak_ada_kib,
		pengadaanBAVerifikasi AS keterangan,
		DATE_FORMAT(a.invTglUbah, '%%d-%%M-%%Y') AS tanggal_pembukuan
	FROM
		inventarisasi_mst a
	LEFT JOIN barang_ref ON invBarangId = barangId
	LEFT JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
	LEFT JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
	LEFT JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
	LEFT JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
	LEFT JOIN inventarisasi_detail ON invDetMstId = invMstId
	LEFT JOIN unit_kerja_ref ON invUnitKerja = unitkerjaId
	LEFT JOIN satuan_barang_ref ON a.invSatuanBarang = satuanbrgId
	LEFT JOIN pengadaan_mst ON invMstPengadaanId = pengadaanId
	LEFT JOIN sumber_dana_ref ON a.invSumberDana = sumberdanaId 
	LEFT JOIN (
				SELECT invPengId,  GROUP_CONCAT(invPenguasaanBarang SEPARATOR '|') as invPengBrgId,  GROUP_CONCAT(jumlah SEPARATOR '|') AS jumlahPeng,  buktiMilik AS ada_bk,  jumlah-buktiMilik AS tidak_ada_bk FROM( 
			SELECT 
				invDetMstId AS invPengId,  
				invPenguasaanBarang,  
				count(invPenguasaanBarang) AS jumlah, 
				IFNULL(SUM(CASE WHEN invSertifikat !='' THEN 1
				WHEN invAkta !='' THEN 1
				WHEN invBPKB !='' THEN 1
				WHEN invSTNK !='' THEN 1
				WHEN invNoPolisi !='' THEN 1
				WHEN invNoLain !='' THEN 1
				END), 0) AS buktiMilik
			FROM inventarisasi_detail
			RIGHT JOIN status_penguasaan_barang_ref ON invPenguasaanBarang = statuspengbrgId
			GROUP BY invPengId,  invPenguasaanBarang
			ORDER BY invPengId,  invPenguasaanBarang
		) a GROUP BY invPengId 
		ORDER BY invPengId
	) penguasaan ON invPengId = invMstId
	LEFT JOIN (
				SELECT invUnitKerja AS invUnitKerjaKond, kondisiBrgId,  GROUP_CONCAT(invKondisiBrgKondId SEPARATOR '|') AS invKondisiBrgKondId,  GROUP_CONCAT(jumlahPerKond SEPARATOR '|') AS jumlahPerKond
		FROM (

		SELECT	
			invUnitKerja,
			invBarangId AS kondisiBrgId, 
			invKondisiBrgKondId, 
			COUNT(invKondisiBrgKondId) AS jumlahPerKond
		FROM
			inventarisasi_detail
		LEFT JOIN inventarisasi_mst ON invDetMstId = invMstId
		LEFT JOIN(
		SELECT 
			invKondisiBrgBrgDetId, 
			invKondisiBrgKondId
		FROM (SELECT 
			invKondisiBrgBrgDetId, 
			invKondisiBrgKondId
		FROM inv_kondisi_history
		ORDER BY invKondisiBrgBrgDetId ASC,  invKondisiBrgId DESC
		) a GROUP BY invKondisiBrgBrgDetId
		) a ON invKondisiBrgBrgDetId = invDetId
		GROUP BY invUnitKerja, kondisiBrgId,  invKondisiBrgKondId
		ORDER BY invUnitKerja, kondisiBrgId,  invKondisiBrgKondId
)a GROUP BY invUnitKerja, kondisiBrgId
	) AS kondisi_barang ON kondisiBrgId = invBarangId AND invUnitKerjaKond = invUnitKerja
	WHERE invUnitKerja LIKE '%s'
	AND invBarangId LIKE '%s'
	GROUP BY invUnitKerja,  invBarangId
";

$sql['get_dyn_query'] = "
   SELECT ('SELECT
   	invSPMNumber,
      unitkerjaNama,
      DATE_FORMAT(a.invTglUbah, \'%%d-%%M-%%Y\') AS tanggal_pembukuan,
		invDetKodeBarang as kode_barang, 
		barangNama AS nama_barang, 
		a.invMerek AS merk, 
		invDetTipe AS tipe,
		count(invDetId) AS jumlah_barang, 
		satuanbrgNama AS satuan, 
		a.invNilaiPerolehanSatuan AS satuan_perolehan, 
		count(invDetId)*a.invNilaiPerolehanSatuan AS jumlah_perolehan, 
		')AS DYN_QUERY

      UNION

      SELECT 
      CONCAT('SUM(IF(invDetPenguasaanBarang=\'',statuspengbrgId,'\',1,0)) AS penguasaan_',statuspengbrgId,',') AS DYN_QUERY
      FROM status_penguasaan_barang_ref

      UNION 

      SELECT 
      CONCAT('SUM(IF(invDetKondisiId=\'',kondisibrgId,'\',1,0)) AS kondisi_',kondisibrgId,',') AS DYN_QUERY
      FROM kondisi_barang_ref

      UNION

      SELECT('
         pengadaanBAVerifikasi AS keterangan
         FROM
            inventarisasi_mst a
         LEFT JOIN barang_ref ON invBarangId = barangId
         LEFT JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
         LEFT JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
         LEFT JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
         LEFT JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
         LEFT JOIN inventarisasi_detail ON invDetMstId = invMstId
         LEFT JOIN unit_kerja_ref ON invDetUnitKerja = unitkerjaId
         LEFT JOIN satuan_barang_ref ON a.invSatuanBarang = satuanbrgId
         LEFT JOIN pengadaan_mst ON invMstPengadaanId = pengadaanId
         LEFT JOIN sumber_dana_ref ON a.invSumberDana = sumberdanaId
      ')AS DYN_QUERY
";

$sql['get_dyn_query_additional'] = "
   WHERE (invDetUnitKerja = '%s' OR 'all' = '%s')
	AND (invBarangId = '%s' OR 'all' = '%s')
	AND (invDetUnitKerja = '%s' OR unitkerjaParentId = '%s' OR '1' = '%s')
	AND invDetAsetStatusId = 1
   GROUP BY
		golbrgId,
		bidangbrgId,
		kelbrgId,
		subkelbrgId,
		invDetKodeBarang
   ORDER BY kode_barang, a.invTglUbah DESC, YEAR(a.invTglPembelian), invBarangId, a.invSumberDana
";
?>
