<?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
		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_intra_old'] = "
	SELECT 
	   * 
	FROM
	   (SELECT 
		  golbrgKibKode,
		  golbrgCoaId AS kode_akun,
		  CONCAT(
			 LPAD(golbrgKode, 1, '0'),
			 '.',
			 LPAD(bidangbrgKode, 2, '0'),
			 '.',
			 LPAD(kelbrgKode, 2, '0'),
			 '.',
			 LPAD(subkelbrgKode, 2, '0'),
			 '.',
			 LPAD(barangKode, 3, '0')
		  ) AS kode_barang,
		  RIGHT(golbrgNama, LENGTH(golbrgNama) - 7) AS nama_golongan,
		  barangNama AS nama_barang,
		  satuanbrgNama AS satuan,
		  (SELECT 
			 COUNT(DISTINCT t.invDetId) 
		  FROM
			 inventarisasi_detail t,
			 inventarisasi_mst s 
		  WHERE t.invDetmstId = s.invMstId 
			 AND s.invbarangId = a.invBarangId 
			 AND YEAR(s.invTglPembelian) = '%s' 
			 AND invDetAsetStatusId = 1) AS kuantitas_awal,
		  (SELECT 
			 SUM(t.invDetNilaiPerolehanSatuan) 
		  FROM
			 inventarisasi_detail t,
			 inventarisasi_mst s 
		  WHERE t.invDetmstId = s.invMstId 
			 AND s.invbarangId = a.invBarangId 
			 AND YEAR(s.invTglPembelian) = '%s' 
			 AND invDetAsetStatusId = 1) AS nilai_awal,
		  (SELECT 
			 COUNT(DISTINCT t.invDetId) 
		  FROM
			 inventarisasi_detail AS t 
		  WHERE invDetAsetStatusId = 1 
			 AND t.invDetmstId = a.invMstId) AS kuantitas_tambah,
		  (SELECT 
			 COUNT(DISTINCT t.invDetId) * a.invNilaiPerolehanSatuan 
		  FROM
			 inventarisasi_detail AS t 
		  WHERE invDetAsetStatusId = 1 
			 AND t.invDetmstId = a.invMstId) AS nilai_tambah,
		  (SELECT 
			 COUNT(DISTINCT t.invDetId) 
		  FROM
			 inventarisasi_detail AS t 
		  WHERE invDetAsetStatusId != 1 
			 AND t.invDetmstId = a.invMstId) AS kuantitas_kurang,
		  (SELECT 
			 COUNT(DISTINCT t.invDetId) * a.invNilaiPerolehanSatuan 
		  FROM
			 inventarisasi_detail AS t 
		  WHERE invDetAsetStatusId != 1 
			 AND t.invDetmstId = a.invMstId) AS nilai_kurang,
		  (
			 (
				(SELECT 
				   COUNT(DISTINCT t.invDetId) 
				FROM
				   inventarisasi_detail t,
				   inventarisasi_mst s 
				WHERE t.invDetmstId = s.invMstId 
				   AND s.invbarangId = a.invBarangId 
				   AND YEAR(s.invTglPembelian) = '%s' 
				   AND invDetAsetStatusId = 1) + 
				(SELECT 
				   COUNT(DISTINCT t.invDetId) 
				FROM
				   inventarisasi_detail AS t 
				WHERE invDetAsetStatusId = 1 
				   AND t.invDetmstId = a.invMstId)
			 ) - 
			 (SELECT 
				COUNT(t.invDetId) 
			 FROM
				inventarisasi_detail AS t 
			 WHERE invDetAsetStatusId != 1 
				AND t.invDetmstId = a.invMstId)
		  ) AS kuantitas_akhir,
		  (
			 (
				IFNULL(
				   (SELECT 
					  SUM(t.invDetNilaiPerolehanSatuan) 
				   FROM
					  inventarisasi_detail t,
					  inventarisasi_mst s 
				   WHERE t.invDetmstId = s.invMstId 
					  AND s.invbarangId = a.invBarangId 
					  AND YEAR(s.invTglPembelian) = '%s' 
					  AND invDetAsetStatusId = 1),
				   0
				) + 
				(SELECT 
				   COUNT(DISTINCT t.invDetId) * a.invNilaiPerolehanSatuan 
				FROM
				   inventarisasi_detail AS t 
				WHERE invDetAsetStatusId = 1 
				   AND t.invDetmstId = a.invMstId)
			 ) - 
			 (SELECT 
				COUNT(t.invDetId) * a.invNilaiPerolehanSatuan 
			 FROM
				inventarisasi_detail AS t 
			 WHERE invDetAsetStatusId != 1 
				AND t.invDetmstId = a.invMstId)
		  ) AS nilai_akhir,
		  a.invNilaiPerolehanSatuan AS satuan_perolehan 
	   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 
	   WHERE (invDetUnitKerja = '%s' 
			 OR 'all' = '%s') 
		  AND (
			 invDetUnitKerja = '%s' 
			 OR unitkerjaParentId = '%s' 
			 OR '1' = '%s'
		  ) 
		  AND (YEAR(a.invTglPembelian) = '%s') 
		  AND (golbrgId = '%s' 
			 OR 'all' = '%s') 
	   GROUP BY DATE(a.invTglPembelian),
		  invBarangId,
		  YEAR(a.invTglPembelian),
		  a.invSumberDana,
		  a.invMerek,
		  invDetTipe,
		  a.invSatuanBarang,
		  pengadaanBAVerifikasi 
	   ORDER BY invBarangId,
		  a.invTglUbah DESC,
		  YEAR(a.invTglPembelian)) AS TB_INTRA 
	WHERE (
		  golbrgKibKode = 'b' 
		  AND satuan_perolehan >= 300000
	   ) 
	   OR (
		  golbrgKibKode = 'd' 
		  AND satuan_perolehan >= 10000000
	   ) 
	   OR (
		  golbrgKibKode = 'a' 
		  AND satuan_perolehan >= 10000000
	   )
";

$sql['get_list_intra']="
SELECT 
  TB1.*, 
  IFNULL(TB2.kuantitas_awal,0) AS kuantitas_awal, 
  IFNULL(TB2.nilai_awal,0) AS nilai_awal,
  IFNULL(TB3.kuantitas_tambah,0) AS kuantitas_tambah,
  (IFNULL(TB3.kuantitas_tambah,0)*TB1.invNilaiPerolehanSatuan) AS nilai_tambah,
  IFNULL(TB4.kuantitas_kurang,0) AS kuantitas_kurang,
  (IFNULL(TB4.kuantitas_kurang,0)*TB1.invNilaiPerolehanSatuan) AS nilai_kurang,
  (IFNULL(TB2.kuantitas_awal,0)+IFNULL(TB3.kuantitas_tambah,0)-IFNULL(TB4.kuantitas_kurang,0)) AS kuantitas_akhir,
  (IFNULL(TB2.nilai_awal,0)+(IFNULL(TB3.kuantitas_tambah,0)*TB1.invNilaiPerolehanSatuan)-(IFNULL(TB4.kuantitas_kurang,0)*TB1.invNilaiPerolehanSatuan)) AS nilai_akhir 
FROM
  (SELECT 
    a.`invBarangId`,
    a.`invMstId`,
    a.invNilaiPerolehanSatuan,
    IFNULL(TRIM(a.invMerek),'') AS invMerek,
    golbrgKibKode,
    golbrgCoaId AS kode_akun,
    CONCAT(LPAD(golbrgKode, 1, '0'),'.',LPAD(bidangbrgKode, 2, '0'),'.',LPAD(kelbrgKode, 2, '0'),'.',LPAD(subkelbrgKode, 2, '0'),'.',LPAD(barangKode, 3, '0')) AS kode_barang,
    RIGHT(golbrgNama, LENGTH(golbrgNama) - 7) AS nama_golongan,
    barangNama AS nama_barang,
    satuanbrgNama AS satuan,
    a.invNilaiPerolehanSatuan AS satuan_perolehan
  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 
  WHERE 
  	[SEARCH_UNIT]
  	[SEARCH_GOLBRG]
  	(unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s') AND 
  	(YEAR(a.`invTglPembelian`) >= '[THN_AWAL]' AND YEAR(a.invTglPembelian) <= '[THN_AKHIR]') 
  GROUP BY DATE(a.invTglPembelian),invBarangId,YEAR(a.invTglPembelian),a.invSumberDana,
    a.invMerek,invDetTipe,a.invSatuanBarang,pengadaanBAVerifikasi 
  ORDER BY invBarangId,a.invTglUbah DESC,YEAR(a.invTglPembelian)
  ) AS TB1
/* get kuantitas awal dan nilai awal */
LEFT JOIN 
	(SELECT TRIM(t.invDetMerek) AS invDetMerek,t.invDetBarangId,COUNT(t.invDetId) AS kuantitas_awal, SUM(t.invDetNilaiPerolehanSatuan) AS nilai_awal
     FROM inventarisasi_detail t,inventarisasi_mst s 
     WHERE t.invDetmstId = s.invMstId  
     AND YEAR(s.invTglPembelian) < '[THN_AWAL]' 
     AND invDetAsetStatusId = 1
     GROUP BY t.invDetBarangId) TB2 
ON TB2.invDetBarangId = TB1.invBarangId AND TB2.invDetMerek = TB1.invMerek
/* get kuantitas tambah dan nilai tambah */
LEFT JOIN (SELECT 
      invDetMstId,TRIM(invDetMerek) AS invDetMerek,COUNT(invDetMstId) AS kuantitas_tambah
    FROM inventarisasi_detail
    WHERE invDetAsetStatusId = 1
    GROUP BY invDetMstId) TB3 
ON TB3.invDetMstId = TB1.invMstId AND TB3.invDetMerek = TB1.invMerek
/* get kuantitas kurang dan nilai kurang */
LEFT JOIN (SELECT 
      invDetMstId,TRIM(invDetMerek) AS invDetMerek,COUNT(invDetMstId) AS kuantitas_kurang
    FROM inventarisasi_detail
    WHERE invDetAsetStatusId != 1
    GROUP BY invDetMstId) TB4 
ON TB4.invDetMstId = TB1.invMstId AND TB4.invDetMerek = TB1.invMerek
WHERE (TB1.golbrgKibKode = 'b' AND TB1.satuan_perolehan >= 300000) 
  OR (TB1.golbrgKibKode = 'd' AND TB1.satuan_perolehan >= 10000000)
  OR (TB1.golbrgKibKode = 'a' AND TB1.satuan_perolehan >= 10000000)
";


$sql['get_count_intra'] = "
   SELECT 
	   COUNT(*) AS `total` 
	FROM
	   (SELECT 
		  golbrgKibKode,
		  golbrgCoaId AS kode_akun,
		  barangNama AS nama_barang,
		  a.invNilaiPerolehanSatuan AS satuan_perolehan 
	   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 
	   WHERE (invDetUnitKerja = '%s' 
			 OR 'all' = '%s') 
		  AND (
			 invDetUnitKerja = '%s' 
			 OR unitkerjaParentId = '%s' 
			 OR '1' = '%s'
		  ) 
		  AND (YEAR(a.invTglPembelian) = '%s') 
		  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) AS TB_INTRA 
	WHERE (
		  golbrgKibKode = 'b' 
		  AND satuan_perolehan >= 300000
	   ) 
	   OR (
		  golbrgKibKode = 'd' 
		  AND satuan_perolehan >= 10000000
	   ) 
	   OR (
		  golbrgKibKode = 'a' 
		  AND satuan_perolehan >= 10000000
	   )
";

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

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