<?php

//===GET===
$sql['get_data_periode'] = "
SELECT 
	periodeId AS id,
	periodeNama AS `name`
FROM 
	aset_ref_periode_usulan
";

$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_combo_unit'] = "
SELECT
   unitkerjaId as id,
   unitkerjaNama as name
FROM
   unit_kerja_ref
WHERE
   unitkerjaId = %s OR '1' = %s
";

$sql['get_data_grid_count'] = "
SELECT
   COUNT(DISTINCT usulanBrgMstId) as total
FROM
   usulan_brg_mst
   LEFT JOIN usulan_brg_det ON usulanBrgDetMstId = usulanBrgMstId
   LEFT JOIN barang_ref ON barangId = usulanBrgDetBrgId
   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
	(CASE
      WHEN (SELECT SUM(usulanBrgDetJmlApprove) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId) = 0 THEN 0
      WHEN (SELECT COUNT(*) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId AND usulanBrgDetJmlApprove = 0 ) > 0 THEN 1
      ELSE 2
   END) LIKE %s AND 
   (usulanBrgMstTglUsulan >= %s OR %s = '') AND
   (usulanBrgMstTglUsulan <= (%s + INTERVAL 1 DAY) OR %s = '') AND
   (usulanBrgMstUnitId = %s OR %s = 'all') AND
   (barangNama IS NULL OR 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_data_grid'] = "
SELECT
   usulanBrgMstId as id,
   usulanBrgMstTglUsulan,
   unitkerjaNama,
   (SELECT COUNT(*) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId) as jumlahItem,
   CASE
      WHEN (SELECT SUM(usulanBrgDetJmlApprove) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId) = 0 THEN 0
      WHEN (SELECT COUNT(*) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId AND usulanBrgDetJmlApprove = 0 ) > 0 THEN 1
      ELSE 2
   END AS status,
   CONCAT(RealName,' (',UserName,')') as pengusul
FROM
   usulan_brg_mst
   LEFT JOIN usulan_brg_det ON usulanBrgDetMstId = usulanBrgMstId
   LEFT JOIN barang_ref ON barangId = usulanBrgDetBrgId
   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
   JOIN unit_kerja_ref ON unitkerjaId = usulanBrgMstUnitId
   JOIN gtfw_user ON UserId = usulanBrgMstUserId
WHERE
	(CASE
      WHEN (SELECT SUM(usulanBrgDetJmlApprove) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId) = 0 THEN 0
      WHEN (SELECT COUNT(*) FROM usulan_brg_det WHERE usulanBrgDetMstId = usulanBrgMstId AND usulanBrgDetJmlApprove = 0 ) > 0 THEN 1
      ELSE 2
   END) LIKE %s AND 
   (usulanBrgMstTglUsulan >= %s OR %s = '') AND
   (usulanBrgMstTglUsulan <= (%s + INTERVAL 1 DAY) OR %s = '') AND
   (usulanBrgMstUnitId = %s OR %s = 'all') AND
   (barangNama IS NULL OR 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) 
GROUP BY
   usulanBrgMstId
ORDER BY status, usulanBrgMstTglUsulan DESC
%LIMIT%
";

$sql['get_data_by_id'] = "
SELECT
   usulanBrgMstTglUsulan,
   usulanBrgMstNoUsulan,
   usulanBrgMstUnitId,
   usulanBrgMstPIC,
   usulanBrgMstKet,
   usulanBrgMstFile,
   unitkerjaNama AS usulanBrgMstUnitNama,
   usulanBrgMstPeriodeId AS periode,
   periodeNama
FROM
   usulan_brg_mst
   JOIN unit_kerja_ref ON  unitkerjaId = usulanBrgMstUnitId
   LEFT JOIN aset_ref_periode_usulan ON periodeId = usulanBrgMstPeriodeId
WHERE
   usulanBrgMstId = %s
";

$sql['get_detail_by_id'] = "
SELECT
   usulanBrgMstTglUsulan,
   usulanBrgMstNoUsulan,
   unitkerjaNama,
   usulanBrgMstPIC,
   usulanBrgMstKet,
   usulanBrgMstFile,
   usulanBrgMstPeriodeId AS periode,
   periodeNama
FROM
   usulan_brg_mst
   JOIN unit_kerja_ref ON unitkerjaId = usulanBrgMstUnitId
   LEFT JOIN aset_ref_periode_usulan ON periodeId = usulanBrgMstPeriodeId
WHERE
   usulanBrgMstId = %s
";

$sql['get_detail_barang'] = "
SELECT 
   usulanBrgDetBrgId,
   CONCAT(LPAD(golbrgKode, 1, '0'), '.', LPAD(bidangbrgKode, 2, '0'), '.', LPAD(kelbrgKode, 2, '0'), '.', LPAD(subkelbrgKode, 2, '0'), '.', LPAD(barangKode, 3, '0')) as brgKode,
   barangNama as brgNama,
   (usulanBrgTotal1/usulanBrgDetJml) as brgHargaLama,
   barangHps as brgHps,
   IFNULL(usulanBrgDetJmlApprove, 'belum diproses') AS usulanBrgDetJmlApprove,
   usulanBrgDetJml,
   usulanBrgTotal1,
   usulanBrgTotal2,
   usulanBrgSpesifikasi AS spesifikasi
FROM
   usulan_brg_det
   JOIN barang_ref ON barangId = usulanBrgDetBrgId
   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
   usulanBrgDetMstId = %s
";

$sql['get_last_mst_id'] = "
SELECT
   MAX(usulanBrgMstId) as id
FROM
   usulan_brg_mst
";

$sql['get_list_barang_to_approve'] = "
SELECT
   usulanBrgDetId AS id,
   unitkerjaId AS usulanUnitId,
   unitkerjaNama,
   usulanBrgMstNoUsulan,
   usulanBrgMstTglUsulan,
   usulan_brg_det.*
FROM
   usulan_brg_det
   JOIN usulan_brg_mst ON usulanBrgMstId = usulanBrgDetMstId
   JOIN unit_kerja_ref ON unitkerjaId = usulanBrgMstUnitId
WHERE
   usulanBrgDetId NOT IN (SELECT usulanKodeBrg FROM usulan_det_brg_rkp) AND
   usulanBrgDetBrgId = %s
";

$sql['get_data_grid_for_print'] = "
SELECT
   usulanBrgMstId,
   usulanBrgMstTglUsulan,
   usulanBrgMstNoUsulan,
   unitkerjaNama,
   CONCAT(RealName,' (',UserName,')') as pengusul,
   usulanBrgMstPIC,
   usulanBrgMstKet
FROM
   usulan_brg_mst
   JOIN unit_kerja_ref ON unitkerjaId = usulanBrgMstUnitId
   JOIN gtfw_user ON UserId = usulanBrgMstUserId
WHERE
   usulanBrgMstId IN (%s)
";

$sql['get_data_grid_for_print_rtf'] = "
SELECT
   usulanBrgMstId,
   usulanBrgMstTglUsulan,
   usulanBrgMstNoUsulan,
   unitkerjaNama,
   CONCAT(RealName,' (',UserName,')') as pengusul,
   usulanBrgMstPIC,
   usulanBrgMstKet
FROM
   usulan_brg_mst
   JOIN unit_kerja_ref ON unitkerjaId = usulanBrgMstUnitId
   JOIN gtfw_user ON UserId = usulanBrgMstUserId
";

$sql['get_data_barang_usulan_for_print'] = "
SELECT 
   usulanBrgDetMstId,
   barangNama,
   usulanBrgDetJml,
   usulanBrgDetJmlApprove,
   usulanBrgSpesifikasi AS spesifikasi
FROM
   usulan_brg_det
   JOIN barang_ref ON barangId = usulanBrgDetBrgId
WHERE
   usulanBrgDetMstId IN (%s)
";

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

$sql['add_usulan'] = "
INSERT INTO
   usulan_brg_mst
   (
      usulanBrgMstTglUsulan,
      usulanBrgMstNoUsulan,
      usulanBrgMstUnitId,
      usulanBrgMstPIC,
      usulanBrgMstKet,
      usulanBrgMstFile,
      usulanBrgMstUserId,
      usulanBrgMstTglUbah,
		usulanBrgMstMAK,
		usulanBrgMstPeriodeId
   )
VALUES (%s, %s, %s, %s, %s, %s, (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s'), NOW(),'%s',%s)
";

$sql['add_detail_barang'] = "
INSERT INTO
   usulan_brg_det
   (
      usulanBrgDetMstId,
      usulanBrgDetBrgId,
      usulanBrgDetJml,
      usulanBrgTotal1,
      usulanBrgTotal2,
      usulanBrgSpesifikasi
   )
VALUES (%s, %s, %s, %s, %s, %s)
";

$sql['delete_detail_barang'] = "
DELETE FROM
   usulan_brg_det
WHERE
   usulanBrgDetMstId = %s
";

$sql['update_usulan'] = "
UPDATE
   usulan_brg_mst
SET
   usulanBrgMstTglUsulan = %s,
   usulanBrgMstNoUsulan = %s,
   usulanBrgMstUnitId = %s,
   usulanBrgMstPIC = %s,
   usulanBrgMstKet = %s,
   usulanBrgMstFile = IFNULL(%s, usulanBrgMstFile),
   usulanBrgMstUserId = (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s'),
   usulanBrgMstTglUbah = NOW(),
   usulanBrgMstPeriodeId = %s
WHERE
   usulanBrgMstId = %s
";

$sql['delete_usulan'] = "
DELETE FROM
   usulan_brg_mst
WHERE
   usulanBrgMstId IN (%VALUE%)
";

$sql['add_approval'] = "
INSERT INTO
   usulan_brg_rkp (usulanRkpNoRkp, usulanRkpPicApprove, usulanRkpTglApprove)
VALUES
(
   %s,
   %s,
   %s
)
";

$sql['add_rekap_detail'] = "
INSERT INTO
   usulan_det_brg_rkp (usulanRkpId, usulanKodeBrg, usulanUnitId)
VALUES
(
   (SELECT MAX(usulanRkpId) FROM usulan_brg_rkp),
   %s,
   %s
)
";

$sql['update_detail_barang'] = "
UPDATE
   usulan_brg_det
SET
   usulanBrgDetJmlApprove = %s,
   usulanBrgStjTotal1 = %s,
   usulanBrgStjTotal2 = %s
WHERE
   usulanBrgDetId = %s
";
?>
