<?php

//===GET===
$sql['get_combo_jenis_kib'] = "
SELECT
   golbrgId AS id,
   golbrgNama AS name
FROM
   golongan_barang_ref
WHERE golbrgId IN('1', '2', '3')
ORDER BY golbrgNama
";

$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_cbx_satuan'] = "
SELECT
   satuanbrgId AS id,
   satuanbrgNama AS name
FROM
   satuan_barang_ref
ORDER BY satuanbrgNama
";

$sql['get_cbx_pagar'] = "
SHOW COLUMNS FROM inventarisasi_detail LIKE 'invDetPagar'
";

$sql['get_cbx_sifat_bangunan'] = "
SHOW COLUMNS FROM pengadaan_detail LIKE 'pengadaanDetSifatBangunan'
";

$sql['get_cbx_imb'] = "
SHOW COLUMNS FROM pengadaan_detail LIKE 'pengadaanDetIMB'
";

$sql['get_cbx_penguasaan_barang'] = "
SELECT
   statuspengbrgId AS id,
   statuspengbrgNama AS name
FROM
   status_penguasaan_barang_ref
ORDER BY statuspengbrgNama
";

$sql['get_kondisi'] = "
SELECT
   kondisibrgId AS id,
   kondisibrgNama AS name
FROM
   kondisi_barang_ref
ORDER BY kondisibrgNama
";

$sql['get_unit_pj'] = "
SELECT
   unitkerjaId AS id,
   unitkerjaNama AS name
FROM
   unit_kerja_ref
ORDER BY unitkerjaKode
";

$sql['get_list_biaya_lain'] = "
SELECT
   biayaId AS id,
   biayaName AS name
FROM
   biaya_ref
";

$sql['get_biaya_lain_by_id'] = "
SELECT
   pengadaanBiayaBiaya as biayaId,
   pengadaanBiayaNilai as biayaNilai
FROM
   pengadaan_biaya
WHERE
   pengadaanBiayaPengadaan = %d
";

$sql['get_pengadaan_detail'] = "
SELECT
   pengadaanNoSPK AS noSPK,
   pengadaanTglSPK AS tglSPK,
   pengadaanTglVerifikasi,
   mitraNama AS mitra,
   pengadaanNominalPengadaan AS nominal,
   jnspengbrgNama AS jenis_pengadaan,
   pengadaanFileSPK
FROM
   pengadaan_mst
   LEFT JOIN mitra_ref ON mitraId = pengadaanMitraId
   LEFT JOIN jenis_pengadaan_barang_ref ON jnspengbrgId = pengadaanJnsPengBrgId
WHERE
   pengadaanId = %s
";

$sql['get_komponen_list_count'] = "
SELECT
   COUNT(*) AS total
FROM
   pengadaan_detail
   LEFT JOIN barang_ref ON barangId = pengadaanDetBrg
   LEFT JOIN sub_kelompok_barang_ref ON subkelbrgId = barangSubkelbrgId
   LEFT JOIN kelompok_barang_ref ON kelbrgId = subkelbrgKelbrgId
   LEFT JOIN bidang_barang_ref ON bidangbrgId = kelbrgBidangbrgId
   LEFT JOIN golongan_barang_ref ON golbrgId = bidangbrgGolbrgId
WHERE
   pengadaanDetPengadaanMstId = %s AND
   (barangNama LIKE %s OR CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) LIKE %s)
";

$sql['get_komponen_list'] = "
SELECT
   pengadaanDetId,
   CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) AS kodeBarang,
   barangNama as namaBarang,
   pengadaanDetJmlAset,
   satuanbrgNama,
   IF(pengadaanDetHargaFaktur != 0,pengadaanDetHargaFaktur,pengadaanDetNilaiPerolehan) as pengadaanDetHargaFaktur,
   biayaLain,
   pengadaanDetTotalPerolehan
FROM
   pengadaan_detail
   LEFT JOIN barang_ref ON barangId = pengadaanDetBrg
   LEFT JOIN sub_kelompok_barang_ref ON subkelbrgId = barangSubkelbrgId
   LEFT JOIN kelompok_barang_ref ON kelbrgId = subkelbrgKelbrgId
   LEFT JOIN bidang_barang_ref ON bidangbrgId = kelbrgBidangbrgId
   LEFT JOIN golongan_barang_ref ON golbrgId = bidangbrgGolbrgId
   LEFT JOIN satuan_barang_ref ON satuanbrgId = pengadaanDetSatuan
   LEFT JOIN (SELECT pengadaanBiayaPengadaan AS id, SUM(pengadaanBiayaNilai) AS biayaLain FROM pengadaan_biaya GROUP BY pengadaanBiayaPengadaan) a ON a.id = pengadaanDetId
WHERE
   pengadaanDetPengadaanMstId = %s
HAVING
   namaBarang LIKE %s OR
   kodeBarang LIKE %s
ORDER BY
   kodeBarang, namaBarang
LIMIT
   %s, %s
";

$sql['get_pengadaan_det_by_id'] = "
SELECT
   pengadaan_detail.*,
	barangSatuanbrgId,
   golbrgId,
   golbrgNama,
   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
FROM
   pengadaan_detail
   JOIN barang_ref ON barangId = pengadaanDetBrg
   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
WHERE
   pengadaanDetId = %s
";

$sql['get_last_komponen_id'] = "
SELECT
   MAX(pengadaanDetId) as id
FROM
   pengadaan_detail
";

$sql['get_data_by_barcode'] = "
SELECT
   golbrgId,
   a.invDetBarcode AS pengadaanDetBarcode,
   CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) AS kodeAset,
   invBarangId AS pengadaanDetBrg,
   barangNama AS namaAset,
   a.invDetMstLabel AS pengadaanDetLabel,
   a.invDetMerek AS pengadaanDetMerk,
   invDetTipe AS pengadaanDetTipe,
   a.invDetSpesifikasi AS pengadaanDetSpesifikasi,
   invDetUmurEkonomis AS pengadaanDetUmrEko,
   invJumlahAset AS pengadaanDetJmlAset,
   a.invDetSatuanBarang AS pengadaanDetSatuan,
   a.invDetNilaiPerolehanSatuan AS pengadaanDetNilaiPerolehan,
   invNilaiFaktur AS pengadaanDetHargaFaktur,
   invTotalPerolehan AS pengadaanDetTotalPerolehan,
   invDetNilaiResidu AS pengadaanDetNilaiResidu,
   invDetBPKB AS pengadaanDetBPKB,
   invDetSTNK AS pengadaanDetSTNK,
   invDetNoMesin AS pengadaanDetNoMesin,
   invDetNoRangka AS pengadaanDetNoRangka,
   invDetNoPolisi AS pengadaanDetNoPolisi,
   invDetNoLain AS pengadaanDetNoLain,
   invDetWarna AS pengadaanDetWarna,
   invDetKepemilikan AS pengadaanDetPmlkBrg,
   invDetPenguasaanBarang AS pengadaanDetPenguasaan,
   invDetKeteranganLain AS pengadaanDetKetLain
FROM
   inventarisasi_detail a
   JOIN inventarisasi_mst ON invMstId = invDetMstId
   JOIN barang_ref ON invBarangId = barangId
   JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
WHERE
   a.invDetBarcode = '%s'
LIMIT 1
";

$sql['get_data_by_barcode_for_atk'] = "
SELECT
   invAtkMstId AS id,
   golbrgId,
   invAtkDetBarcode AS pengadaanDetBarcode,
   CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) AS kodeAset,
   barangId AS pengadaanDetBrg,
   barangNama AS namaAset,
   invAtkDetLabel AS pengadaanDetLabel,
   invAtkDetMerk AS pengadaanDetMerk,
   '' AS pengadaanDetTipe,
   invAtkDetSpesifikasi AS pengadaanDetSpesifikasi,
   '' AS pengadaanDetUmrEko,
   '' AS pengadaanDetJmlAset,
   invAtkMstSatuanBarang AS pengadaanDetSatuan,
   invAtkMstNilaiPerolehan AS pengadaanDetNilaiPerolehan,
   '' AS pengadaanDetHargaFaktur,
   '' AS pengadaanDetTotalPerolehan,
   '' AS pengadaanDetNilaiResidu,
   '' AS pengadaanDetBPKB,
   '' AS pengadaanDetSTNK,
   '' AS pengadaanDetNoMesin,
   '' AS pengadaanDetNoRangka,
   '' AS pengadaanDetNoPolisi,
   '' AS pengadaanDetNoLain,
   '' AS pengadaanDetWarna,
   '' AS pengadaanDetPmlkBrg,
   '' AS pengadaanDetPenguasaan,
   invAtkMstKeteranganLain AS pengadaanDetKetLain
FROM
   inv_atk_det
   JOIN inv_atk_master ON invAtkDetMstId = invAtkMstId
   JOIN barang_ref ON invAtkMstBarangId = barangId
   JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
WHERE
   invAtkDetBarcode = %s
LIMIT 1
";

$sql['get_biaya_lain_inventarisasi_by_id'] = "
SELECT
   invDetBiayaBiayaId as biayaId,
   invDetBiayaNilai as biayaNilai
FROM
   inv_det_biaya
WHERE
   invDetBiayaInvId = %d
";

$sql['get_biaya_lain_atk_by_id'] = "
SELECT
   invAtkBiayaBiayaId as biayaId,
   invAtkBiayaNominal as biayaNilai
FROM
   inv_atk_biaya
WHERE
   invAtkBiayaAktId = %d
";

$sql['get_sisa_dana_pengadaan'] = "
SELECT
   (pengadaanNominalPengadaan - IFNULL(SUM(pengadaanDetTotalPerolehan),0)) AS sisa
FROM
   pengadaan_mst
   LEFT JOIN pengadaan_detail ON pengadaanDetPengadaanMstId = pengadaanId
WHERE
   pengadaanId = %s
GROUP BY
   pengadaanId
";

// DO Query

$sql['add'] = "
INSERT INTO
   pengadaan_detail
   (
      pengadaanDetId,
      pengadaanDetPengadaanMstId,
      pengadaanDetRuangId,
      pengadaanDetBarcode,
      pengadaanDetBrg,
      pengadaanDetLabel,
      pengadaanDetMerk,
      pengadaanDetSpesifikasi,
      pengadaanDetJmlAset,
      pengadaanDetSatuan,
      pengadaanDetKdGedung,
      pengadaanDetNmGedung,
      pengadaanDetLuasTanah,
      pengadaanDetLuasGedung,
      pengadaanDetUmrTeknis,
      pengadaanDetNilaiPerolehan,
      pengadaanDetHargaFaktur,
      pengadaanDetNilaiResidu,
      pengadaanDetTotalPerolehan,
      pengadaanDetNJOP,
      pengadaanDetSifatBangunan,
      pengadaanDetIMB,
      pengadaanDetPeruntukan,
      pengadaanDetUmrEko,
      pengadaanDetLokasi,
      pengadaanDetKegunaan,
      pengadaanDetSertifikat,
      pengadaanDetBPKB,
      pengadaanDetSTNK,
      pengadaanDetAkta,
      pengadaanDetNoMesin,
      pengadaanDetNoRangka,
      pengadaanDetNoPolisi,
      pengadaanDetNoLain,
      pengadaanDetWarna,
      pengadaanDetPagar,
      pengadaanDetBtsUtara,
      pengadaanDetBtsSelatan,
      pengadaanDetBtsTimur,
      pengadaanDetBtsBarat,
      pengadaanDetPmlkBrg,
      pengadaanDetPenguasaan,
      pengadaanDetKondisi,
      pengadaanDetKetLain,
      pengadaanDetDenah,
      pengadaanDetFoto,
      pengadaanDetTglUbah,
      pengadaanDetUserId
   )
VALUES
   (
      NULL,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      %s,
      NOW(),
      (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
   )
";

$sql['add_biaya_lain'] = "
INSERT INTO
   pengadaan_biaya
VALUES
   %VALUES%
";

$sql['delete_biaya_lain'] = "
DELETE FROM
   pengadaan_biaya
WHERE
   pengadaanBiayaPengadaan = %s
";

$sql['update'] = "
UPDATE
   pengadaan_detail
SET
   pengadaanDetPengadaanMstId = %s,
   pengadaanDetRuangId = %s,
   pengadaanDetBarcode = %s,
   pengadaanDetBrg = %s,
   pengadaanDetLabel = %s,
   pengadaanDetMerk = %s,
   pengadaanDetSpesifikasi = %s,
   pengadaanDetJmlAset = %s,
   pengadaanDetSatuan = %s,
   pengadaanDetKdGedung = %s,
   pengadaanDetNmGedung = %s,
   pengadaanDetLuasTanah = %s,
   pengadaanDetLuasGedung = %s,
   pengadaanDetUmrTeknis = %s,
   pengadaanDetNilaiPerolehan = %s,
   pengadaanDetHargaFaktur = %s,
   pengadaanDetNilaiResidu = %s,
   pengadaanDetTotalPerolehan = %s,
   pengadaanDetNJOP = %s,
   pengadaanDetSifatBangunan = %s,
   pengadaanDetIMB = %s,
   pengadaanDetPeruntukan = %s,
   pengadaanDetUmrEko = %s,
   pengadaanDetLokasi = %s,
   pengadaanDetKegunaan = %s,
   pengadaanDetSertifikat = %s,
   pengadaanDetBPKB = %s,
   pengadaanDetSTNK = %s,
   pengadaanDetAkta = %s,
   pengadaanDetNoMesin = %s,
   pengadaanDetNoRangka = %s,
   pengadaanDetNoPolisi = %s,
   pengadaanDetNoLain = %s,
   pengadaanDetWarna = %s,
   pengadaanDetPagar = %s,
   pengadaanDetBtsUtara = %s,
   pengadaanDetBtsSelatan = %s,
   pengadaanDetBtsTimur = %s,
   pengadaanDetBtsBarat = %s,
   pengadaanDetPmlkBrg = %s,
   pengadaanDetPenguasaan = %s,
   pengadaanDetKondisi = %s,
   pengadaanDetKetLain = %s,
   pengadaanDetDenah = IFNULL(%s, pengadaanDetDenah),
   pengadaanDetFoto = IFNULL(%s, pengadaanDetFoto),
   pengadaanDetTglUbah = NOW(),
   pengadaanDetUserId = (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
WHERE
   pengadaanDetId = %s
";

$sql['delete_pengadaan_detail'] = "
DELETE FROM
   pengadaan_detail
WHERE
   pengadaanDetId IN (%VALUE%) AND
   pengadaanRekapId IS NULL AND
   (pengadaanDetVerifikasi IS NULL OR pengadaanDetVerifikasi = 'Belum diperiksa')
";

?>
