<?php

//===GET===
$sql['get_data_user_by_id'] = "
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
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
WHERE
   UserId = %s
";

$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
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
WHERE
   UserName = %s
";

$sql['get_setting'] = "
SELECT
   settingValue AS value
FROM
   setting
WHERE
   settingId = %s
";

$sql['combo_sumber_dana'] = "
SELECT
   sumberdanaId AS id,
   sumberdanaNama AS name
FROM
   sumber_dana_ref
ORDER BY sumberdanaNama
";

$sql['combo_jenis_pengadaan'] = "
SELECT
   jnspengbrgId AS id,
   jnspengbrgNama AS name
FROM
   jenis_pengadaan_barang_ref
";

$sql['combo_jalur_pengadaan'] = "
SELECT
   jlrpengbrgId AS id,
   jlrpengbrgNama AS name
FROM
   jalur_pengadaan_barang_ref
";

$sql['combo_mitra_kerja'] = "
SELECT
   mitraId AS id,
   mitraNama AS name
FROM
   mitra_ref
ORDER BY
   mitraNama
";

$sql['combo_jabatan_ref'] = "
SELECT
   jabatanId AS id,
   jabatanNama AS name
FROM
   jabatan_ref
";

$sql['get_combo_unit'] = "
SELECT
   unitkerjaId as id,
   unitkerjaNama as name
FROM
   unit_kerja_ref
WHERE
   unitkerjaId = %s OR '1' = %s
";

$sql['get_list_pengadaan_count'] = "
SELECT
   COUNT(*) as total
FROM
   pengadaan_mst
WHERE
   (pengadaanTahun = %s OR %s) AND
   pengadaanMAK LIKE %s AND
   (pengadaanSumberDanaId = %s OR %s) AND
   pengadaanNama LIKE %s AND
   pengadaanNoSPK LIKE %s
";

$sql['get_list_pengadaan'] = "
SELECT 
   pengadaanId, pengadaanBAVerifikasi, pengadaanTglVerifikasi, pengadaanMAK, pengadaanNominalPengadaan, pengadaanNoSPK, pengadaanNama, mitraNama, jlrpengbrgNama, pengadaanFileSPK,
   CASE WHEN sum(status)/count(status)!= 1 AND sum(status)/count(status) != 3 THEN 2
   WHEN sum(status)/count(status)= 1 THEN 1 
   WHEN sum(status)/count(status)= 3 THEN 3 END AS `status`
FROM
   (
      SELECT 
         pengadaanTglSPK, pengadaanSumberDanaId, pengadaanTahun, pengadaanId, pengadaanBAVerifikasi, pengadaanTglVerifikasi, pengadaanMAK, pengadaanNominalPengadaan, pengadaanFileSPK, pengadaanNoSPK, pengadaanNama, mitraNama, jlrpengbrgNama,
         CASE WHEN pengadaanDetVerifikasi = 'Belum diperiksa' OR pengadaanDetVerifikasi IS NULL THEN 1
         WHEN pengadaanDetVerifikasi = 'sesuai' THEN 3
         WHEN pengadaanDetVerifikasi = 'tidak sesuai' THEN 2 END AS status
      FROM
         pengadaan_mst 
         LEFT JOIN pengadaan_detail ON pengadaanId = pengadaanDetPengadaanMstId 
         LEFT JOIN jalur_pengadaan_barang_ref ON jlrpengbrgId = pengadaanJlrPengBrgId 
         LEFT JOIN mitra_ref ON mitraId = pengadaanMitraId 
   ) a 
WHERE
   (pengadaanTahun = %s OR %s) AND
   pengadaanMAK LIKE %s AND
   (pengadaanSumberDanaId = %s OR %s) AND
   pengadaanNama LIKE %s AND
   pengadaanNoSPK LIKE %s
GROUP BY pengadaanId
ORDER BY
   status ASC,
   pengadaanTglSPK DESC
LIMIT %s, %s
";

$sql['get_pengadaan_mst_by_id'] = "
SELECT
   *
FROM
   pengadaan_mst
WHERE
   pengadaanId = %s
";

$sql['get_panitia_pengadaan_by_pengadaan_id'] = "
SELECT
   pengadaanPanitiaNIP as NIP,
   pengadaanPanitiaNama as nama,
   pengadaanPanitiaJabatanId as status
FROM
   pengadaan_panitia
WHERE
   pengadaanPanitiaPengadaanId = %s
";

$sql['get_rekap_pengadaan_by_pengadaan_id'] = "
SELECT
   a.usulanRkpId AS rekapId,
   usulanRkpNoRkp AS rekapNomor,
   IFNULL(SUM(usulanBrgStjTotal1),0) AS total1,
   REPLACE(IFNULL(SUM(usulanBrgStjTotal2),0), '.', ',') AS total2
FROM
   pengadaan_detail_rekap_usulan
   LEFT JOIN usulan_brg_rkp a ON usulanRkpId = pengadaanRkpUsulanId
   LEFT JOIN usulan_det_brg_rkp b ON b.usulanRkpId = a.usulanRkpId
   LEFT JOIN usulan_brg_det ON usulanBrgDetId = usulanKodeBrg
   LEFT JOIN barang_ref ON barangId = usulanBrgDetBrgId
WHERE
   pengadaanId = %s
GROUP BY
   a.usulanRkpId
";

// DO QUERY ---------
$sql['add_pengadaan_mst'] = "
INSERT INTO
   pengadaan_mst
   (
      pengadaanJnsPengBrgId,
      pengadaanTahun,
      pengadaanMAK,
      pengadaanNominalPengadaan,
      pengadaanNoSPK,
      pengadaanNama,
      pengadaanTglSPK,
      pengadaanJlrPengBrgId,
      pengadaanSumberdanaId,
      pengadaanMitraId,
      pengadaanUnitkerjaId,
      pengadaanNominal,
      pengadaanFileSPK,
      pengadaanTglUbah,
      pengadaanUserId
   )
VALUES
(
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['add_pengadaan_panitia'] = "
INSERT INTO
   pengadaan_panitia
   (
      pengadaanPanitiaPengadaanId, 
      pengadaanPanitiaNIP,
      pengadaanPanitiaNama, 
      pengadaanPanitiaJabatanId
   )
VALUES
   %VALUE%
";

$sql['add_pengadaan_rekap'] = "
INSERT INTO
   pengadaan_detail_rekap_usulan
   (
      pengadaanId,
      pengadaanRkpUsulanId
   )
VALUES
   %VALUE%
";

$sql['add_pengadaan_detail_from_rekap'] = "
INSERT INTO
   pengadaan_detail
   (
      pengadaanDetPengadaanMstId,
      pengadaanDetBrg,
      pengadaanDetJmlAset,
      pengadaanDetSatuan,
      pengadaanDetNilaiPerolehan,
      pengadaanDetTotalPerolehan,
      pengadaanRekapId,
      pengadaanDetSpesifikasi
   )
SELECT
   pengadaanId,
   usulanBrgDetBrgId,
   usulanBrgDetJmlApprove,
   barangSatuanbrgId,
   IF(usulanBrgDetJmlApprove = 0, 0, usulanBrgStjTotal2/usulanBrgDetJmlApprove) AS nilaiPerolehanSatuan,
   IF(usulanBrgDetJmlApprove = 0, 0, usulanBrgStjTotal2) AS nilaiPerolehanSatuan,
   pengadaanRkpId,
   usulanBrgSpesifikasi
FROM
   pengadaan_detail_rekap_usulan
   JOIN usulan_det_brg_rkp ON usulanRkpId = pengadaanRkpUsulanId
   JOIN usulan_brg_det ON usulanBrgDetId = usulanKodeBrg
   JOIN barang_ref ON barangId = usulanBrgDetBrgId
WHERE
	pengadaanId = %s
";

$sql['update_pengadaan_mst'] = "
UPDATE
   pengadaan_mst
SET
   pengadaanJnsPengBrgId = %s,
   pengadaanTahun = %s,
   pengadaanMAK = %s,
   pengadaanNominalPengadaan = %s,
   pengadaanNoSPK = %s,
   pengadaanNama = %s,
   pengadaanTglSPK = %s,
   pengadaanJlrPengBrgId = %s,
   pengadaanSumberdanaId = %s,
   pengadaanMitraId = %s,
   pengadaanUnitkerjaId = %s,
   pengadaanNominal = %s,
   pengadaanFileSPK = %s,
   pengadaanTglUbah = NOW(),
   pengadaanUserId = (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
WHERE
   pengadaanId = %s;
";

$sql['delete_pengadaan_panitia'] = "
DELETE FROM
   pengadaan_panitia
WHERE
   pengadaanPanitiaPengadaanId = %s
";

$sql['delete_pengadaan_detail_by_rekap'] = "
DELETE
   pengadaan_detail
FROM
   pengadaan_detail_rekap_usulan
   LEFT JOIN pengadaan_detail ON pengadaanRekapId = pengadaanRkpId
WHERE
   pengadaanRkpUsulanId IN (%s)
";

$sql['delete_pengadaan_rekap'] = "
DELETE FROM
   pengadaan_detail_rekap_usulan
WHERE
   pengadaanRkpUsulanId IN (%s)
";

$sql['add_pengadaan_detail_from_rekap_by_pengadaan_id'] = "
INSERT INTO
   pengadaan_detail
   (
      pengadaanDetPengadaanMstId,
      pengadaanDetBrg,
      pengadaanDetJmlAset,
      pengadaanDetSatuan,
      pengadaanDetNilaiPerolehan,
      pengadaanDetTotalPerolehan
   )
SELECT
   pengadaanId,
   usulanBrgDetBrgId,
   usulanBrgDetJmlApprove,
   barangSatuanbrgId,
   IF(usulanBrgDetJmlApprove = 0, 0, usulanBrgStjTotal2/usulanBrgDetJmlApprove) AS nilaiPerolehanSatuan,
   IF(usulanBrgDetJmlApprove = 0, 0, usulanBrgStjTotal2) AS nilaiPerolehanSatuan
FROM
   pengadaan_detail_rekap_usulan
   JOIN usulan_det_brg_rkp ON usulanRkpId = pengadaanRkpUsulanId
   JOIN usulan_brg_det ON usulanBrgDetId = usulanKodeBrg
   JOIN barang_ref ON barangId = usulanBrgDetBrgId
WHERE
	pengadaanId = %s
";

$sql['delete_pengadaan_mst'] = "
DELETE FROM
   pengadaan_mst
WHERE
   pengadaanId IN (%VALUE%)
";

?>
