<?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_unit'] = "
	SELECT 
		unitkerjaId AS id,
		unitkerjaNama AS name
	FROM unit_kerja_ref
	LEFT JOIN 
			inventarisasi_detail ON invDetUnitKerja = unitkerjaId
	WHERE
		unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE %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_lii'] = "
	SELECT 
	   DATE_FORMAT(
		  tanggal_pembukuan,
		  '%%d-%%M-%%Y'
	   ) AS tanggal_pembukuan,
	   kode_barang,
	   nama_barang,
	   merk,
	   tipe,
	   COUNT(invBarangId) AS jumlah,
	   satuan,
	   asal,
	   satuan_perolehan,
	   COUNT(invDetId) * invNilaiPerolehanSatuan AS jumlah_perolehan,
	   COUNT(invDetId) AS ada_kib,
	   '0' AS tidak_ada_kib,
	   SUM(bk) AS ada_bk,
	   COUNT(invBarangId) - SUM(bk) AS tidak_ada_bk,
	   invKondisiBrgKondId,
	   COUNT(invKondisiBrgKondId) AS jumlahPerKond,
	   invDetPenguasaanBarang AS invPengBrgId,
	   COUNT(invDetPenguasaanBarang) AS jumlahPeng,
	   DATE_FORMAT(tahun_beli_buat, '%%Y') AS tahun_beli_buat 
	FROM
	   (SELECT 
		  invDetUnitKerja,
		  invBarangId,
		  a.invTglUbah AS tanggal_pembukuan,
		  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,
		  invDetTipe AS tipe,
		  a.invNilaiPerolehanSatuan,
		  a.invTglPembelian AS tahun_beli_buat,
		  satuanbrgNama AS satuan,
		  sumberdanaNama AS asal,
		  a.invNilaiPerolehanSatuan AS satuan_perolehan,
		  0 AS tidak_ada_kib,
		  IFNULL(
			 CASE
				WHEN invDetSertifikat != '' 
				THEN 1 
				WHEN invDetAkta != '' 
				THEN 1 
				WHEN invDetBPKB != '' 
				THEN 1 
				WHEN invDetSTNK != '' 
				THEN 1 
				WHEN invDetNoPolisi != '' 
				THEN 1 
				WHEN invDetNoLain != '' 
				THEN 1 
			 END,
			 0
		  ) AS bk,
		  invDetId,
		  (SELECT 
			 invKondisiBrgKondId 
		  FROM
			 inv_kondisi_history 
		  WHERE invKondisiBrgBrgDetId = invDetId 
		  ORDER BY invKondisiBrgId 
		  LIMIT 0, 1) AS invKondisiBrgKondId,
		  invDetPenguasaanBarang 
	   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) a 
	WHERE invDetUnitKerja LIKE '%s' 
	GROUP BY a.tanggal_pembukuan,
	   invBarangId,
	   invKondisiBrgKondId,
	   a.tahun_beli_buat,
	   asal,
	   invDetPenguasaanBarang,
	   a.merk,
	   tipe,
	   satuan 
	ORDER BY a.tanggal_pembukuan DESC,
	   a.tahun_beli_buat,
	   invBarangId,
	   invDetPenguasaanBarang,
	   invKondisiBrgKondId ASC 
";


$sql['get_dyn_query'] = "
   SELECT ('SELECT
      unitkerjaNama,
      DATE_FORMAT(a.invTglUbah, \'%%d-%%M-%%Y\') AS tanggal_pembukuan,
		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, 
		invDetTipe AS tipe,
		YEAR(a.invTglPembelian) 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, 
		')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 ('SUM(
         IF(invDetSertifikat !=\'\',1,(
         IF(invDetAkta !=\'\',1,(
         IF(invDetBPKB !=\'\',1,(
         IF(invDetSTNK !=\'\',1,(
         IF(invDetNoPolisi !=\'\',1,(
         IF(invDetNoLain !=\'\',1,0)))))))))))) AS ada_bm,
         count(invDetId) - SUM(
         IF(invDetSertifikat !=\'\',1,(
         IF(invDetAkta !=\'\',1,(
         IF(invDetBPKB !=\'\',1,(
         IF(invDetSTNK !=\'\',1,(
         IF(invDetNoPolisi !=\'\',1,(
         IF(invDetNoLain !=\'\',1,0)))))))))))) AS tidak_ada_bm,') AS DYN_QUERY
      
      UNION

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

      UNION

      SELECT('
         SUM(IF(invDetIdentitasBarang = \'Ada\', 1, 0)) AS ada_kib,
		   SUM(IF(invDetIdentitasBarang = \'Tidak Ada\', 1, 0)) AS tidak_ada_kib,
         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 (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s') /*(invDetUnitKerja = %%s OR unitkerjaParentId = %%s OR '1' = %%s)*/ AND invDetAsetStatusId = 1
	AND (a.invTglUbah BETWEEN '%s' AND DATE_ADD('%s', INTERVAL 1 DAY)) AND (golbrgId = '%s' OR 'all' = '%s')
   GROUP BY DATE(a.invTglUbah),  invBarangId, a.invMerek, invDetTipe, YEAR(a.invTglPembelian), a.invSumberDana, a.invSatuanBarang, pengadaanBAVerifikasi
   ORDER BY a.invTglUbah DESC, YEAR(a.invTglPembelian), invBarangId, a.invSumberDana
";

$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,a.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
";
?>
