<?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_status_barang_old'] = "
	SELECT 
		statusPengBrgId AS `id`,
		statusPengBrgNama AS `name`
	FROM 
		status_peng_brg_ref
";

$sql['get_status_barang'] = "
SELECT statusAsetId AS id, statusAsetNama AS name 
FROM aset_ref_aset_status
WHERE statusAsetId NOT IN(1,4)
";

$sql['get_status_kondisi_barang'] = "
	SELECT 
		kondisiBrgId AS `id`,
		kondisiBrgNama AS `name`
	FROM 
		kondisi_barang_ref
";

$sql['get_prioritas'] = "
SELECT 
    prioritasId AS id,
    prioritasNama AS name
FROM
    prioritas_ref
";

$sql['get_unit_kerja'] = "
	SELECT 
		unitKerjaId AS `id`,
		unitKerjaNama AS `name`
	FROM 
		unit_kerja_ref
        WHERE (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
        [UNIT]
	ORDER BY 
		unitKerjaNama
";

$sql['get_lokasi_barang'] = "
	SELECT 
	   invDetRuanganId AS id,
	   ruangNama AS name 
	FROM
	   inventarisasi_detail 
	   LEFT JOIN ruang 
		  ON ruangId = invDetRuanganId 
	WHERE invDetUnitKerja = '%s' [RUANG]
	GROUP BY invDetRuanganId 
";

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

$sql['get_list_pengendalian_barang_count'] = "
SELECT
   COUNT(id) AS total
FROM
   (
      SELECT 
         pengBrgId AS id,
         CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0')) AS kodeAset,
         barangNama AS namaAset,
         GROUP_CONCAT(DISTINCT pengBrgStatusId SEPARATOR ',') AS status_concat
      FROM 
			pengendalian_barang_mst
			JOIN pengendalian_barang_det ON pengBrgDetMstId = pengBrgId
			JOIN inventarisasi_detail ON invDetId = pengBrgInvDetId
			JOIN inventarisasi_mst ON invMstId = invDetMstId
			JOIN barang_ref ON barangId = invBarangId
			JOIN sub_kelompok_barang_ref ON subkelbrgId = barangSubkelbrgId
			JOIN kelompok_barang_ref ON kelbrgId = subkelbrgKelbrgId
			JOIN bidang_barang_ref ON bidangbrgId = kelbrgBidangbrgId
			JOIN golongan_barang_ref ON golbrgId = bidangbrgGolbrgId
			JOIN status_peng_brg_ref ON statusPengBrgId = pengBrgStatusId
			JOIN unit_kerja_ref ON unitkerjaId = pengBrgUnitId
			JOIN ruang ON ruangId = pengBrgLokasi
			LEFT JOIN gedung ON gedungId = ruangGedungId
			LEFT JOIN kampus_ref ON kampusId = gedungKampusId
      WHERE
         (pengBrgTgl >= %s OR '0000-00-00' = %s) AND
         (pengBrgTgl <= %s OR '0000-00-00' = %s) AND
         (pengBrgUnitId = %s OR 'all' = %s)
      GROUP BY
         pengBrgId
      HAVING
         (LOCATE(CONCAT(',',%s,','),CONCAT(',',status_concat,',')) > 0 OR 'all' = %s) AND
         (kodeAset LIKE %s OR namaAset LIKE %s)
   ) new_table
";

$sql['get_list_pengendalian_barang'] = "
SELECT 
   SQL_CALC_FOUND_ROWS
   pengBrgId AS id, 
   pengBrgTgl, 
   CONCAT(IFNULL(CONCAT(kampusNama, '->'), ''), IFNULL(CONCAT(gedungNama, '->'), ''), ruangNama) AS pengBrgLokasi, 
   CONCAT(LPAD(golbrgKode, 1, '0'), '.', LPAD(bidangbrgKode, 2, '0'), '.', LPAD(kelbrgKode, 2, '0'), '.', LPAD(subkelbrgKode, 2, '0'), '.', LPAD(barangKode, 3, '0')) AS kodeAset, 
   barangNama AS namaAset, 
   unitkerjaNama, 
   pengBAPeng, 
	pengPIC, 
   SUM(pengBrgHrgJual) as pengBrgHrgJual , 
   GROUP_CONCAT(DISTINCT pengBrgStatusId SEPARATOR ',') AS status_concat
FROM 
   pengendalian_barang_mst
   JOIN pengendalian_barang_det ON pengBrgDetMstId = pengBrgId
   JOIN inventarisasi_detail ON invDetId = pengBrgInvDetId
   JOIN inventarisasi_mst ON invMstId = invDetMstId
   JOIN barang_ref ON barangId = invBarangId
   JOIN sub_kelompok_barang_ref ON subkelbrgId = barangSubkelbrgId
   JOIN kelompok_barang_ref ON kelbrgId = subkelbrgKelbrgId
   JOIN bidang_barang_ref ON bidangbrgId = kelbrgBidangbrgId
   JOIN golongan_barang_ref ON golbrgId = bidangbrgGolbrgId
   JOIN aset_ref_aset_status ON statusAsetId = pengBrgStatusId
   JOIN unit_kerja_ref ON unitkerjaId = pengBrgUnitId
   JOIN ruang ON ruangId = pengBrgLokasi
   LEFT JOIN gedung ON gedungId = ruangGedungId
   LEFT JOIN kampus_ref ON kampusId = gedungKampusId
WHERE
   (pengBrgTgl >= %s OR '0000-00-00' = %s) AND
   (pengBrgTgl <= %s OR '0000-00-00' = %s) AND
   (pengBrgUnitId = %s OR 'all' = %s) AND
   (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
GROUP BY
   pengBrgId
HAVING
   (LOCATE(CONCAT(',',%s,','),CONCAT(',',status_concat,',')) > 0 OR 'all' = %s) AND
   (kodeAset LIKE %s OR namaAset LIKE %s)
ORDER BY pengBrgTgl DESC
";

$sql['get_pengendalian_by_id'] = "
SELECT
   *
FROM
   pengendalian_barang_mst
WHERE
   pengBrgId = %s
";

$sql['get_barang_by_pengendalian_id'] = "
SELECT
   pengBrgInvDetId AS barangId,
   invDetKodeBarang AS barangKode, 
   barangNama,
   pengBrgStatusId,
   pengBrgPrioritasId,
   pengBrgHrgJual,
   statusAsetNama AS statusPengBrgNama,
   prioritasNama,
   pengBrgKondisibrgId,
   IF(IFNULL(lelangDetPengendalianBrgDetId,'0') <> '0','1', 
   	  IF(IFNULL(perbaikanDetId,'0') <> '0','1','0')) AS isFixed
   /*IFNULL(perbaikanDetId,'0') AS isFixed*/
FROM
   pengendalian_barang_det
   JOIN inventarisasi_detail ON invDetId = pengBrgInvDetId
   JOIN inventarisasi_mst ON invMstId = invDetMstId
   JOIN barang_ref ON barangId = invBarangId
   JOIN sub_kelompok_barang_ref ON subkelbrgId = barangSubkelbrgId
   JOIN kelompok_barang_ref ON kelbrgId = subkelbrgKelbrgId
   JOIN bidang_barang_ref ON bidangbrgId = kelbrgBidangbrgId
   JOIN golongan_barang_ref ON golbrgId = bidangbrgGolbrgId
   JOIN aset_ref_aset_status ON statusAsetId = pengBrgStatusId
   LEFT JOIN prioritas_ref ON prioritasId = pengBrgPrioritasId
   LEFT JOIN aset_perbaikan_det ON perbaikanPengendalianBrgDetId = pengBrgDetId
   LEFT JOIN aset_lelang_det ON lelangDetPengendalianBrgDetId = pengBrgDetId
WHERE
   pengBrgDetMstId = '%s'
ORDER BY
   barangKode
";

$sql['get_pengendalian_by_id_for_detail'] = "
SELECT
   unitkerjaNama, 
   pengendalian_barang_mst.*,
   CONCAT(IFNULL(CONCAT(kampusNama, '->'), ''), IFNULL(CONCAT(gedungNama, '->'), ''), ruangNama) AS pengBrgLokasi
FROM
   pengendalian_barang_mst
   JOIN unit_kerja_ref ON unitkerjaId = pengBrgUnitId
   JOIN ruang ON ruangId = pengBrgLokasi
   LEFT JOIN gedung ON gedungId = ruangGedungId
   LEFT JOIN kampus_ref ON kampusId = gedungKampusId
WHERE
   pengBrgId = %s
";

$sql['get_log_header'] = "
SELECT
   invDetKodeBarang AS kodeBarang,
   barangNama,
   a.invDetMstLabel,
   a.invDetMerek,
   a.invDetSpesifikasi
FROM
   inventarisasi_detail a
   JOIN inventarisasi_mst b ON b.invMstId = a.invDetMstId
   JOIN barang_ref c ON invBarangId = barangId
   JOIN sub_kelompok_barang_ref d ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref e ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref f ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref g ON bidangbrgGolbrgId = golbrgId
WHERE
   a.invDetId = '%s'
";

$sql['get_log_pengendalian_barang'] = "
SELECT
   pengBrgTgl,
   unitkerjaNama,
   pengBAPeng,
   (SELECT kondisibrgNama FROM inv_kondisi_history LEFT JOIN kondisi_barang_ref ON kondisibrgId = invKondisiBrgKondId WHERE kondisibrgId = pengBrgKondisibrgId AND invKondisiBrgTglKondisi <= pengBrgTgl ORDER BY invKondisiBrgId DESC LIMIT 1) AS kondisibrgNama,
   statusAsetNama AS statusPengBrgNama,
   prioritasNama,
   IFNULL(pengBrgBiaya, pengBrgHrgJual) AS biaya
FROM
   pengendalian_barang_det
   JOIN pengendalian_barang_mst ON pengBrgId = pengBrgDetMstId
   JOIN unit_kerja_ref ON unitkerjaId = pengBrgUnitId
   JOIN aset_ref_aset_status ON statusAsetId = pengBrgStatusId
   LEFT JOIN prioritas_ref ON prioritasId = pengBrgPrioritasId
WHERE
   pengBrgInvDetId = %s
ORDER BY
   pengBrgDetId DESC,
   pengBrgTgl DESC
";

/////////
// Do Query
/////////

$sql['add_pengendalian_barang_mst'] = "
INSERT INTO
   pengendalian_barang_mst
   (
      pengBrgUnitId,
      pengBrgLokasi,
      pengBrgTgl,
      pengBAPeng,
      pengPIC,
      pengKet,
      pengFile,
		pengTindakLanjut
   )
VALUES
   (%s, %s, %s, %s, %s, %s, %s, %s)
";

$sql['add_pengendalian_barang_det_old'] = "
INSERT INTO
   pengendalian_barang_det
   (
      pengBrgDetMstId,
      pengBrgInvDetId,
      pengBrgStatusId,
      pengBrgKondisibrgId,
      pengBrgPrioritasId,
      pengBrgHrgJual
   )
VALUES
   (%s, %s, %s, %s, %s, %s)
";

$sql['add_pengendalian_barang_det'] = "
INSERT INTO
pengendalian_barang_det
(
pengBrgDetMstId,
pengBrgInvDetId,
pengBrgStatusId,
pengBrgKondisibrgId,
pengBrgPrioritasId,
pengBrgHrgJual
)
VALUES ";

$sql['add_inv_konsidi_history_old'] = "
INSERT INTO
	inv_kondisi_history
	(
		invKondisiBrgBrgDetId,
		invKondisiBrgKondId,
		invKondisiBrgTglKondisi,
		invKondisiTglHistory,
		invKondisiUserId
	)
VALUES
	(%s, %s, %s, %s, %s)
";

$sql['add_inv_konsidi_history'] = "
INSERT INTO
inv_kondisi_history
(
invKondisiBrgBrgDetId,
invKondisiBrgKondId,
invKondisiBrgTglKondisi,
invKondisiTglHistory,
invKondisiUserId
)
VALUES ";

$sql['update_inventarisasi_detail']="
UPDATE inventarisasi_detail
SET
    invDetAsetStatusId = %s,
    invDetKondisiId = %s
WHERE invDetId = %s
";

$sql['update_pengendalian_barang_mst'] = "
UPDATE
   pengendalian_barang_mst
SET
   pengBrgUnitId = %s,
   pengBrgLokasi = %s,
   pengBrgTgl = %s,
   pengBAPeng = %s,
   pengPIC = %s,
   pengKet = %s,
   pengFile = IFNULL(%s, pengFile),
   pengTindakLanjut = %s
WHERE
   pengBrgId = %s
";

$sql['delete_pengendalian_barang_det'] = "
DELETE FROM
   pengendalian_barang_det
WHERE
   pengBrgDetMstId = %s AND pengBrgDetId NOT IN (SELECT perbaikanPengendalianBrgDetId FROM aset_perbaikan_det)
";

$sql['delete_pengendalian_barang'] = "
DELETE FROM
   pengendalian_barang_mst
WHERE
   pengBrgId IN (%s)
";
?>
