<?php

//===GET===
$sql['get_cbx_status_verifikasi'] = "
SHOW COLUMNS FROM pengadaan_detail LIKE 'pengadaanDetVerifikasi'
";

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

$sql['get_komponen_list'] = "
SELECT
   golbrgId,
   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,
   pengadaanDetVerifikasi
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
   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
";

$sql['get_data_for_duplication'] = "
SELECT
   golbrgId,
   pengadaanDetId,
   pengadaanDetBarcode AS invBarcode,
   CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0')) AS invKodeAset,
   pengadaanDetBrg AS invBarangId,
   pengadaanDetLabel AS invMstLabel,
   pengadaanDetMerk AS invMerek,
   NULL AS invTipe,
   pengadaanDetSpesifikasi AS invSpesifikasi,
   pengadaanTglSPK AS invTglPembelian,
   pengadaanSumberdanaId AS invSumberDana,
   pengadaanDetUmrEko AS invUmurEkonomis,
   pengadaanDetJmlAset AS invJumlahAset,
   pengadaanDetSatuan AS invSatuanBarang,
   pengadaanDetLuasTanah AS invLuasTanah,
   pengadaanDetNilaiPerolehan AS invNilaiPerolehanSatuan,
   pengadaanDetHargaFaktur AS invNilaiFaktur,
   pengadaanDetTotalPerolehan AS invTotalPerolehan,
   pengadaanDetNilaiResidu AS invNilaiResidu,
   pengadaanDetNJOP AS invNJOP,
   pengadaanDetLokasi AS invLokasi,
   pengadaanDetKegunaan AS invKegunaan,
   pengadaanDetSertifikat AS invSertifikat,
   pengadaanDetAkta AS invAkta,
   pengadaanDetBPKB AS invBPKB,
   pengadaanDetSTNK AS invSTNK,
   pengadaanDetNoMesin AS invNoMesin,
   pengadaanDetNoRangka AS invNoRangka,
   pengadaanDetNoPolisi AS invNoPolisi,
   pengadaanDetNoLain AS invNoLain,
   pengadaanDetWarna AS invWarna,
   pengadaanDetPagar AS invPagar,
   pengadaanDetBtsUtara AS invBatasUtara,
   pengadaanDetBtsSelatan AS invBatasSelatan,
   pengadaanDetBtsTimur AS invBatasTimur,
   pengadaanDetBtsBarat AS invBatasBarat,
   pengadaanDetPmlkBrg AS invKepemilikan,
   pengadaanDetPenguasaan AS invPenguasaanBarang,
   pengadaanDetKondisi AS invKondisiBrgKondId,
   gedungKampusId AS invKampus,
   gedungId AS invGedungId,
   NULL AS invRuanganId,
   pengadaanDetKetLain AS invKeteranganLain,
   %s AS invUnitKerja
FROM
   pengadaan_detail
   JOIN pengadaan_mst ON pengadaanId = pengadaanDetPengadaanMstId
   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
   JOIN setting ON settingNama = 'Gudang_Default'
   JOIN ruang ON ruangId = settingValue
   JOIN gedung ON gedungId = ruangGedungId
WHERE
   pengadaanDetId = %s
LIMIT 1
";

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

$sql['get_foto_pengadaan_by_id'] = "
SELECT
   pengadaanDetDenah AS denah,
   pengadaanDetFoto AS foto
FROM
   pengadaan_detail
WHERE
   pengadaanDetId = %s
";

$sql['get_atk_mirip_pengadaan_detail'] = "
SELECT
   invAtkGudangId
FROM
   inv_atk_gudang
   JOIN inv_atk_brg ON
      invBrgAtkId = invAtkGudangInvDtlbrgId AND
      jumlahSatuan = invAtkGudangJumlah
   JOIN inv_atk_det ON invAtkDetId = invDet
   JOIN inv_atk_master ON invAtkMstId = invAtkDetMstId
   JOIN pengadaan_detail ON
      IFNULL(pengadaanDetBrg,'') = IFNULL(invAtkMstBarangId,'') AND
      IFNULL(pengadaanDetSatuan,'') = IFNULL(invAtkMstSatuanBarang,'') AND
      IFNULL(pengadaanDetKetLain,'') = IFNULL(invAtkMstKeteranganLain,'') AND
      IFNULL(pengadaanDetBarcode,'') = IFNULL(invAtkDetBarcode,'') AND
      IFNULL(pengadaanDetLabel,'') = IFNULL(invAtkDetLabel,'') AND
      IFNULL(pengadaanDetMerk,'') = IFNULL(invAtkDetMerk,'') AND
      IFNULL(pengadaanDetSpesifikasi,'') = IFNULL(invAtkDetSpesifikasi,'') AND
      IFNULL(FLOOR(pengadaanDetTotalPerolehan),'') = IFNULL(FLOOR(biaya),'') AND
      IFNULL(pengadaanDetJmlAset,'') = IFNULL(jumlahSatuan,'')
   JOIN pengadaan_mst ON pengadaanId = pengadaanDetPengadaanMstId
      IFNULL(DATE(pengadaanTglVerifikasi),'') = IFNULL(DATE(tglPengadaan),'')
WHERE
   pengadaanDetId = %s
";

// DO Query

$sql['update_pengadaan_mst'] = "
UPDATE
   pengadaan_mst
SET
   pengadaanBAVerifikasi = NULL,
   pengadaanVerifikator = NULL,
   pengadaanNoBukti = NULL,
   pengadaanTglVerifikasi = NULL
WHERE
   pengadaanId = %s
";

$sql['update_pengadaan_detail'] = "
UPDATE
   pengadaan_detail
SET
   pengadaanDetVerifikasi = %s
WHERE
   pengadaanDetId = %s
";

$sql['delete_atk_gudang'] = "
DELETE
   inv_atk_gudang
FROM
   inv_atk_gudang
   JOIN inv_atk_brg ON invBrgAtkId = invAtkGudangInvDtlbrgId
WHERE
   jumlahSatuan = invAtkGudangJumlah AND
   invBrgPengadaanDetId = %s
";

$sql['delete_atk_barang_dan_biaya'] = "
DELETE
   inv_atk_brg, inv_atk_biaya
FROM
   inv_atk_brg
   LEFT JOIN inv_atk_biaya ON invAtkBiayaPengadaanDetId = invBrgPengadaanDetId
WHERE
   invBrgPengadaanDetId = %s
";

$sql['insert_into_inv_atk_mst_from_pengadaan'] = "
INSERT INTO
   inv_atk_master
   (
      invAtkMstBarangId,
      invAtkMstSatuanBarang,
      invAtkMstKeteranganLain,
      invAtkMstUserId
   )
SELECT
   pengadaanDetBrg,
   pengadaanDetSatuan,
   pengadaanDetKetLain,
   %s
FROM
   pengadaan_detail
WHERE
   pengadaanDetId = %s AND
   (SELECT COUNT(*) FROM inv_atk_master WHERE invAtkMstBarangId = pengadaanDetBrg) = 0
";

$sql['insert_into_inv_atk_detail_from_pengadaan'] = "
INSERT INTO
   inv_atk_det
   (
      invAtkDetMstId,
      invAtkDetBarcode,
      invAtkDetLabel,
      invAtkDetMerk,
      invAtkDetSpesifikasi,
      invAtkDetUserId
   )
SELECT
   (SELECT invAtkMstId FROM inv_atk_master WHERE invAtkMstBarangId = pengadaanDetBrg),
   pengadaanDetBarcode,
   pengadaanDetLabel,
   pengadaanDetMerk,
   pengadaanDetSpesifikasi,
   %s
FROM
   pengadaan_detail
WHERE
   pengadaanDetId = %s AND
   (SELECT COUNT(*) FROM inv_atk_det WHERE invAtkDetBarcode = pengadaanDetBarcode) = 0
";

$sql['insert_into_inv_atk_brg_from_pengadaan'] = "
INSERT INTO
   inv_atk_brg
   (
      invDet,
      tglPengadaan,
      biaya,
      jumlahSatuan
   )
SELECT
   IF(pengadaanDetBarcode != '', (SELECT invAtkDetId FROM inv_atk_det WHERE invAtkDetBarcode = pengadaanDetBarcode), (SELECT MAX(invAtkDetId) FROM inv_atk_det)),
   pengadaanTglVerifikasi,
   pengadaanDetTotalPerolehan,
   pengadaanDetJmlAset
FROM
   pengadaan_detail
   JOIN pengadaan_mst ON pengadaanId = pengadaanDetPengadaanMstId
WHERE
   pengadaanDetId = %s
";

$sql['insert_into_inv_atk_gudang_from_pengadaan'] = "
INSERT INTO
   inv_atk_gudang
   (
      invAtkGudangRuangId,
      invAtkGudangInvDtlbrgId,
      invAtkGudangJumlah
   )
SELECT
   settingValue,
   invBrgAtkId,
   jumlahSatuan
FROM
   inv_atk_brg
   LEFT JOIN setting ON settingNama = 'Gudang_Default'
ORDER BY
   invBrgAtkId DESC
LIMIT 1
";

$sql['insert_into_inv_atk_biaya_from_pengadaan_biaya'] = "
INSERT INTO
   inv_atk_biaya
   (
      invAtkBiayaAktId,
      invAtkBiayaBiayaId,
      invAtkBiayaNominal
   )
SELECT
   (SELECT invAtkMstId FROM inv_atk_master WHERE invAtkMstBarangId = pengadaanDetBrg),
   pengadaanBiayaBiaya,
   pengadaanBiayaNilai
FROM
   pengadaan_biaya
   JOIN pengadaan_detail ON pengadaanDetId = pengadaanBiayaPengadaan
WHERE
   pengadaanBiayaPengadaan = %s
";

$sql['insert_into_gedung_from_pengadaan'] = "
INSERT INTO
   gedung
   (
      gedungBarangId,
      gedungKode,
      gedungNama,
      gedungThnPerolehan,
      gedungUmurEkonomis,
      gedungUmurTeknis,
      gedungSumberDanaId,
      gedungNilaiPerolehan,
      gedungNilaiResidu,
      gedungLuasTanah,
      gedungLuasBangunan,
      gedungSifatBangunan,
      gedungIMB,
      gedungKepemilikanBrgId,
      gedungKondisiBrgId,
      gedungKeterangan,
      gedungDenahBangunan,
      gedungFoto,
      gedungUnitkerjaId,
      gedungTglUbah,
      gedungUserId
   )
SELECT
   pengadaanDetBrg,
   pengadaanDetKdGedung,
   pengadaanDetNmGedung,
   pengadaanTahun,
   pengadaanDetUmrEko,
   pengadaanDetUmrTeknis,
   pengadaanSumberdanaId,
   pengadaanDetNilaiPerolehan,
   pengadaanDetNilaiResidu,
   pengadaanDetLuasTanah,
   pengadaanDetLuasGedung,
   pengadaanDetSifatBangunan,
   pengadaanDetIMB,
   pengadaanDetPmlkBrg,
   pengadaanDetKondisi,
   pengadaanDetKetLain,
   pengadaanDetDenah,
   pengadaanDetFoto,
   pengadaanUnitkerjaId,
   NOW(),
   %s
FROM
   pengadaan_detail
   JOIN pengadaan_mst ON pengadaanId = pengadaanDetPengadaanMstId
WHERE
   pengadaanDetId = %s
";

$sql['insert_into_gedung_biaya_from_pengadaan_biaya'] = "
INSERT INTO
   gedung_biaya (gedungBiayaGedungId, gedungBiayaBiayaId, gedungBiayaNilai)
SELECT
   (SELECT MAX(gedungId) FROM gedung),
   pengadaanBiayaBiaya,
   pengadaanBiayaNilai
FROM
   pengadaan_biaya
WHERE
   pengadaanBiayaPengadaan = %s
";

$sql['delete_inventarisasi_mst'] = "
DELETE FROM
   inventarisasi_mst
WHERE
   invMstPengadaanId = %s
";
?>