<?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,
   unitkerjaKodeSistem
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
   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,
   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_combo_unit'] = "
SELECT
   unitkerjaId AS id,
   unitkerjaNama AS name
FROM
   unit_kerja_ref
WHERE
   unitkerjaKodeSistem= '%s' OR unitkerjaKodeSistem LIKE '%s'
";

$sql['get_combo_unit_detail'] = "
SELECT
	unitkerjaId AS id,
	unitkerjaNama AS name
FROM do_unit_new
LEFT JOIN unit_kerja_ref ON unitkerjaId = doUnitUnitIdPenerima
WHERE
	doUnitMstId = %s AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
";

$sql['get_combo_ruang'] = "
SELECT
   ruangId AS id,
   CONCAT(IFNULL(CONCAT(kampusNama,' &raquo '),''),gedungNama,' &raquo ',ruangNama) AS name
FROM
   ruang
   LEFT JOIN gedung ON gedungId = ruangGedungId
   LEFT JOIN kampus_ref ON kampusId = gedungKampusId
WHERE
	(ruangUnitId = %s /*OR 1 = %%s*/)
ORDER BY
   kampusNama,
   gedungNama,
   ruangNama
";

$sql['get_item_pengadaan_by_id_old'] = "
	SELECT 
	   invMstId AS idMst,
	   invKodeAset AS kodeBarang,
	   barangNama AS namaBarang,
	   invDetMerek AS pengadaanDetMerk,
	   pengadaanDetJmlAset,
	   unitkerjaNama AS `name`,
	   unitkerjaId AS id 
	FROM
	   pengadaan_mst 
	   JOIN pengadaan_detail_rekap_usulan a 
		  ON a.pengadaanId = pengadaan_mst.pengadaanId 
	   JOIN usulan_brg_rkp 
		  ON pengadaanRkpUsulanId = usulan_brg_rkp.usulanRkpId 
	   JOIN usulan_det_brg_rkp 
		  ON usulan_det_brg_rkp.usulanRkpId = usulan_brg_rkp.usulanRkpId 
	   JOIN usulan_brg_det 
		  ON usulanBrgDetId = usulanKodeBrg 
	   JOIN unit_kerja_ref 
		  ON unitkerjaId = usulanUnitId 
	   JOIN barang_ref 
		  ON barangId = usulanBrgDetBrgId 
	   JOIN pengadaan_detail 
		  ON pengadaanDetBrg = barangId 
	   JOIN inventarisasi_mst 
		  ON barangId = invBarangId 
	   JOIN inventarisasi_detail 
		  ON (
			 invDetMstId = invMstId 
			 AND invDetId NOT IN 
			 (SELECT 
				doInvDetId 
			 FROM
				do_detail)
		  ) 
	WHERE pengadaanDetPengadaanMstId = '%s' 
	   AND (unitkerjaId = '%s' 
		  OR '' = '%s') 
	   AND pengadaanBAVerifikasi IS NOT NULL 
	GROUP BY barangId,
	   unitkerjaId
";

$sql['get_item_pengadaan_by_id']="
SELECT 
   f.invKodeAset AS kodeBarang,
   f.invMstLabel AS namaBarang,
   f.invMerek AS pengadaanDetMerk,
   (e.usulanBrgDetJmlApprove - IFNULL(g.totalDO,0)) AS pengadaanDetJmlAset,
   d.`unitkerjaNama` AS `name`,
   f.invBarangId,
   d.`unitkerjaId` AS id,
   a.pengadaanDetPengadaanMstId AS pengadaanId 
FROM pengadaan_detail a
LEFT JOIN pengadaan_detail_rekap_usulan b ON b.`pengadaanRkpId` = a.pengadaanRekapId
LEFT JOIN usulan_det_brg_rkp c ON c.`usulanRkpId` = b.pengadaanRkpUsulanId
LEFT JOIN unit_kerja_ref d ON d.unitkerjaId = c.`usulanUnitId`
LEFT JOIN usulan_brg_det e ON e.usulanBrgDetId = c.`usulanKodeBrg`
LEFT JOIN 
   (SELECT invMstPengadaanId,invBarangId,invKodeAset,invMstLabel,invMerek
    FROM inventarisasi_mst
    GROUP BY invMstPengadaanId,invBarangId) f 
ON f.invMstPengadaanId = a.`pengadaanDetPengadaanMstId` AND f.invBarangId = a.`pengadaanDetBrg`
LEFT JOIN
   (SELECT COUNT(doDetId) AS totalDO, doDetUnitIdPenerima, doDetMstPengadaanId, invDetBarangId
    FROM do_detail_new
    LEFT JOIN inventarisasi_detail ON invDetId = doDetInvDetId
    GROUP BY doDetUnitIdPenerima,doDetMstPengadaanId,invDetBarangId) g
ON 
	g.doDetMstPengadaanId = a.pengadaanDetPengadaanMstId AND 
	g.doDetUnitIdPenerima = d.unitkerjaId AND
	g.invDetBarangId = a.pengadaanDetBrg
WHERE a.pengadaanDetPengadaanMstId = %s
GROUP BY d.unitkerjaId,f.invBarangId
[SHOW_AVAILABLE]
";

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

$sql['get_data_grid_count'] = "
SELECT
   COUNT(*) AS total
FROM
   do_mst
   JOIN pengadaan_mst ON pengadaanId = doPengadaanId
   LEFT JOIN do_detail ON doDoId = doId
   LEFT JOIN inventarisasi_detail ON invDetId = doInvDetId
   LEFT JOIN inventarisasi_mst ON invMstId = invDetMstId
   LEFT JOIN barang_ref ON barangId = invBarangId
WHERE
   pengadaanNoSPK LIKE %s AND
   doNoDO LIKE %s AND
   (doTglDO >= %s OR %s = '') AND
   (doTglDO <= (%s + INTERVAL 1 DAY) OR %s = '') AND
   (doUnitPenerima = %s OR %s = 'all') AND
   (barangNama LIKE %s OR invKodeAset LIKE %s)
";

$sql['get_data_grid']="
SELECT 
	SQL_CALC_FOUND_ROWS
	doMstId AS id,
	pengadaanNoSpk,
	doMstNoDO AS doNoDo,
	doMstTglDO,
	CONCAT(RealName,' (',UserName,')') AS penanggungJawab,
	g.unitkerjaNama,
	g.doPICPenerima,
	g.doTglTerima,
	g.jumlah,
	g.unitkerjaKodeSistem,
	g.unitkerjaId,
	g.isDelivered
FROM do_mst_new
LEFT JOIN pengadaan_mst ON pengadaanId = doMstPengadaanId
LEFT JOIN
    (SELECT 
    	b.doUnitMstId,
     	GROUP_CONCAT(unitkerjaNama SEPARATOR ', ') AS unitkerjaNama,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',IFNULL(b.doUnitPICPenerima,'-')) SEPARATOR '<br>') AS doPICPenerima,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',
     						IFNULL((SELECT DATE_FORMAT(doDetTglTerima, '%%d %%M %%Y') AS doDetTglTerima 
        							FROM do_detail_new 
        							WHERE doDetDoUnitId = b.doUnitId AND doDetUnitIdPenerima = unitkerjaId
        							ORDER BY doDetTglTerima DESC
        							LIMIT 1),'-')) SEPARATOR '<br>') AS doTglTerima,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',IFNULL(b.doUnitJumlah,'-')) SEPARATOR '<br>') AS jumlah,
     	GROUP_CONCAT(CONCAT('#',unitkerjaKodeSistem,'#') SEPARATOR ',') AS unitkerjaKodeSistem,
     	GROUP_CONCAT(CONCAT('|',unitkerjaId,'|') SEPARATOR ',') AS unitkerjaId,
     	GROUP_CONCAT(barangNama SEPARATOR ',') AS barangNama,
     	GROUP_CONCAT(CONCAT('|',barangId,'|') SEPARATOR ',') AS barangId,
     	GROUP_CONCAT((SELECT invKodeAset FROM inventarisasi_mst WHERE invBarangId = barangId LIMIT 1) SEPARATOR ',') AS barangKode,
     	(SELECT COUNT(a.doUnitPICPenerima) FROM do_unit_new a WHERE a.doUnitPICPenerima IS NOT NULL AND a.doUnitMstId = b.doUnitMstId) AS isDelivered 
     FROM do_unit_new b
     LEFT JOIN unit_kerja_ref ON unitkerjaId = b.doUnitUnitIdPenerima 
     LEFT JOIN barang_ref ON barangId = b.doUnitBarangId
     [UNIT_FILTER]
     GROUP BY b.doUnitMstId) g  
ON g.doUnitMstId = doMstId
LEFT JOIN gtfw_user ON UserId = doMstUserId
WHERE
	pengadaanNoSPK LIKE %s AND
	doMstNoDO LIKE %s 
	[SEARCH_TGLDO]
	[SEARCH_BARANG]
HAVING 
	[SEARCH_UNIT] 
	(unitkerjaKodeSistem LIKE '%s' OR unitkerjaKodeSistem LIKE '%s') 	
ORDER BY doMstTglDO DESC
%LIMIT%
";

$sql['get_do_list_for_print'] = "
SELECT
	doMstId AS id,
	pengadaanNoSpk,
	doMstNoDO AS doNoDo,
	doMstTglDO AS doTglDo,
	CONCAT(RealName,' (',UserName,')') AS penanggungJawab,
	g.unitkerjaNama,
	g.doPICPenerima,
	g.doTglTerima,
	g.jumlah,
	g.unitkerjaKodeSistem,
	g.unitkerjaId,
	g.isDelivered
FROM do_mst_new
JOIN pengadaan_mst ON pengadaanId = doMstPengadaanId
LEFT JOIN
    (SELECT 
    	b.doUnitMstId,
     	GROUP_CONCAT(unitkerjaNama SEPARATOR ', ') AS unitkerjaNama,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',IFNULL(b.doUnitPICPenerima,'-')) SEPARATOR '<br>') AS doPICPenerima,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',
     						IFNULL((SELECT DATE_FORMAT(doDetTglTerima, '%%d %%M %%Y') AS doDetTglTerima 
        							FROM do_detail_new 
        							WHERE doDetDoUnitId = b.doUnitId AND doDetUnitIdPenerima = unitkerjaId
        							ORDER BY doDetTglTerima DESC
        							LIMIT 1),'-')) SEPARATOR '<br>') AS doTglTerima,
     	GROUP_CONCAT(CONCAT(unitkerjaNama,' : ',IFNULL(b.doUnitJumlah,'-')) SEPARATOR '<br>') AS jumlah,
     	GROUP_CONCAT(CONCAT('#',unitkerjaKodeSistem,'#') SEPARATOR ',') AS unitkerjaKodeSistem,
     	GROUP_CONCAT(CONCAT('|',unitkerjaId,'|') SEPARATOR ',') AS unitkerjaId,
     	GROUP_CONCAT(barangNama SEPARATOR ',') AS barangNama,
     	GROUP_CONCAT(CONCAT('|',barangId,'|') SEPARATOR ',') AS barangId,
     	(SELECT COUNT(a.doUnitPICPenerima) FROM do_unit_new a WHERE a.doUnitPICPenerima IS NOT NULL AND a.doUnitMstId = b.doUnitMstId) AS isDelivered 
     FROM do_unit_new b
     LEFT JOIN unit_kerja_ref ON unitkerjaId = b.doUnitUnitIdPenerima 
     LEFT JOIN barang_ref ON barangId = b.doUnitBarangId
     [UNIT_FILTER]
     GROUP BY b.doUnitMstId) g  
ON g.doUnitMstId = doMstId
JOIN gtfw_user ON UserId = doMstUserId
WHERE doMstId IN (%ID%) 
HAVING (unitkerjaKodeSistem LIKE '%s' OR unitkerjaKodeSistem LIKE '%s')
ORDER BY doMstTglDO DESC
";

$sql['get_data_grid_old'] = "
SELECT
   SQL_CALC_FOUND_ROWS
   doId AS id,
   pengadaanNoSpk,
   doNoDo,
   doTglDo,
   unitkerjaNama,
   CONCAT(RealName,' (',UserName,')') AS penanggungJawab,
   doPICPenerima,
   doDetailTglTerima AS doTglTerima,
	COUNT(doId)       AS jumlah
FROM
   do_mst
   JOIN pengadaan_mst ON pengadaanId = doPengadaanId
   LEFT JOIN (SELECT * FROM do_detail ORDER BY doDetailTglTerima DESC) AS a ON doDoId = doId
   LEFT JOIN inventarisasi_detail ON invDetId = doInvDetId
   LEFT JOIN inventarisasi_mst ON invMstId = invDetMstId
   LEFT JOIN barang_ref ON barangId = invBarangId
   JOIN unit_kerja_ref ON unitkerjaId = doUnitPenerima
   JOIN gtfw_user ON UserId = doUserId
WHERE
   pengadaanNoSPK LIKE %s AND
   doNoDO LIKE %s AND
   (doTglDO >= %s OR %s = '') AND
   (doTglDO <= (%s + INTERVAL 1 DAY) OR %s = '') AND
   (doUnitPenerima = %s OR %s = 'all') AND
   (barangNama LIKE %s OR invKodeAset LIKE %s)
GROUP BY
   doId
ORDER BY
   doTglDo DESC
%LIMIT%
";

$sql['get_DO_by_id']="
SELECT 
   pengadaanNoSPK,
   pengadaanId,
   pengadaanTglSPK,
   pengadaanNama,
   jlrpengbrgNama,
   mitraNama,
   pengadaanNominalPengadaan,
   doMstTglDO AS doTglDO,
   doMstNoDO AS doNoDO,
   doMstPICPengirim AS doPICPengirim,
   doMstNIP AS doNIP,
   doMstPengguna AS doPengguna,
   doMstKeterangan AS doKeterangan,
   (SELECT GROUP_CONCAT(CONCAT(doUnitUnitIdPenerima,'|',doUnitBarangId,'|',doUnitJumlah) SEPARATOR ',') FROM do_unit_new WHERE doUnitMstId = doMstId) AS doUnit
FROM do_mst_new 
LEFT JOIN pengadaan_mst ON pengadaanId = doMstPengadaanId
LEFT JOIN jalur_pengadaan_barang_ref ON jlrpengbrgId = pengadaanJlrPengBrgId
LEFT JOIN mitra_ref ON mitraId = pengadaanMitraId
WHERE doMstId = %s
";

$sql['get_DO_by_id_old'] = "
SELECT
   pengadaanId,
   pengadaanNoSPK,
   pengadaanTglSPK,
   pengadaanNama,
   mitraNama,
   jlrpengbrgNama,
   pengadaanNominalPengadaan,
   doTglDO,
   doNoDO,
	doNIP,
   doPengguna,
   doUnitPenerima,
   doPICPengirim,
   doKeterangan,
   invMstId,
   (SELECT COUNT(invDetId) FROM inventarisasi_detail WHERE invDetMstId = invMstId AND invDetId IN (SELECT doInvDetId FROM do_detail WHERE doDoId = doId)) AS jmlDO
FROM
   do_mst
   JOIN pengadaan_mst ON pengadaanId = doPengadaanId
   JOIN jalur_pengadaan_barang_ref ON jlrpengbrgId = pengadaanJlrPengBrgId
   JOIN mitra_ref ON mitraId = pengadaanMitraId
   JOIN inventarisasi_mst ON invMstPengadaanId = pengadaanId
WHERE
   doId = %s
";

$sql['get_DO_header_by_id'] = "
SELECT
   pengadaanNoSPK,
   pengadaanNama,
   doMstTglDO,
   doMstNoDO
FROM
   do_mst_new
   JOIN pengadaan_mst ON pengadaanId = doMstPengadaanId
WHERE
   doMstId = %s 
";

$sql['get_DO_item_by_id_old'] = "
SELECT
   invMstId,
   invDetId AS id,
   invKodeAset AS kodeAset,
   invDetKodeBarang AS kodeBarang,
   barangNama AS namaBarang,
   a.invDetMerek AS merekBarang,
   a.invDetSpesifikasi AS spesifikasi,
   satuanbrgNama AS satuanBarang,
   a.invDetNilaiPerolehanSatuan AS hargaSatuan,
   doDetailRuang,
   doDetailTglTerima
FROM
   do_mst
   JOIN do_detail ON doDoId = doId
   JOIN inventarisasi_mst ON invMstPengadaanId = doPengadaanId
   JOIN barang_ref ON barangId = invBarangId
   JOIN inventarisasi_detail a ON (invDetMstId = invMstId AND invDetId = doInvDetId)
   LEFT JOIN satuan_barang_ref ON satuanbrgId = a.invDetSatuanBarang
WHERE
   doId = '%s'
";

$sql['get_DO_item_by_id']="
SELECT 
	f.invKodeAset AS kodeAset,
	f.invMstLabel AS namaBarang,
	f.invMerek AS merekBarang,
	f.invSpesifikasi AS spesifikasi,
	satuanbrgNama AS satuanBarang,
	f.invNilaiPerolehanSatuan AS hargaSatuan,
	doUnitJumlah AS jumlah,
	g.doDetTglTerima AS doUnitTglTerima,
	IFNULL(doUnitPICPenerima,'<em>Belum di ACC</em>') AS doUnitPICPenerima,
	unitkerjaNama 
FROM do_unit_new
LEFT JOIN 
   (SELECT invMstPengadaanId,invBarangId,invKodeAset,invMstLabel,invMerek,invSpesifikasi,invSatuanBarang,invNilaiPerolehanSatuan
    FROM inventarisasi_mst
    GROUP BY invMstPengadaanId,invBarangId) f 
ON f.invMstPengadaanId = doUnitPengadaanId AND f.invBarangId = doUnitBarangId
LEFT JOIN unit_kerja_ref ON unitkerjaId = doUnitUnitIdPenerima
LEFT JOIN satuan_barang_ref ON satuanbrgId = f.invSatuanBarang
LEFT JOIN
	(SELECT *
	 FROM 
	 	(SELECT doDetDoUnitId,doDetUnitIdPenerima,doDetTglTerima
	 	FROM do_detail_new
	 	ORDER BY doDetTglTerima DESC) a
	 GROUP BY a.doDetDoUnitId,a.doDetUnitIdPenerima) g
ON g.doDetDoUnitId = doUnitId AND g.doDetUnitIdPenerima = doUnitUnitIdPenerima
WHERE doUnitMstId = %s
";

$sql['get_DO_item_by_id_unit']="
SELECT
   doDetInvDetId AS id,
   invDetKodeBarang AS kodeBarang,
   invDetMstLabel AS namaBarang,
   invDetMerek AS merekBarang,
   invDetSpesifikasi AS spesifikasi,
   satuanbrgNama AS satuanBarang,
   invDetNilaiPerolehanSatuan AS hargaSatuan,
   doDetRuangId,
   doDetTglTerima AS doUnitTglTerima,
   unitkerjaNama,
   unitkerjaId,
   (SELECT doUnitPICPenerima FROM do_unit_new WHERE doUnitId = doDetDoUnitId) AS doUnitPICPenerima 
FROM do_detail_new
LEFT JOIN inventarisasi_detail ON invDetId = doDetInvDetId
LEFT JOIN satuan_barang_ref ON satuanbrgId = invDetSatuanBarang
LEFT JOIN unit_kerja_ref ON unitkerjaId = doDetUnitIdPenerima
WHERE doDetDoUnitId = (SELECT doUnitId FROM do_unit_new WHERE doUnitMstId = %s AND doUnitUnitIdPenerima = %s)
";

$sql['get_do_list_for_print_old'] = "
SELECT
   doId AS id,
   pengadaanNoSpk,
   doNoDo,
   doTglDo,
   unitkerjaNama,
   CONCAT(RealName,' (',UserName,')') AS penanggungJawab,
   doPICPenerima,
   doDetailTglTerima AS doTglTerima
FROM
   do_mst
   JOIN pengadaan_mst ON pengadaanId = doPengadaanId
   LEFT JOIN (SELECT * FROM do_detail ORDER BY doDetailTglTerima DESC) AS a ON doDoId = doId
   JOIN unit_kerja_ref ON unitkerjaId = doUnitPenerima
   JOIN gtfw_user ON UserId = doUserId
WHERE
   doId IN (%ID%) AND
   (doUnitPenerima = %s OR %s = 'all')
GROUP BY
   doId
ORDER BY
   doTglDo DESC
";

$sql['get_DO_item_by_id_for_print_old'] = "
SELECT
   invMstId,
   invDetId AS id,
   invKodeAset AS kodeAset,
   invDetKodeBarang AS kodeBarang,
   barangNama AS namaBarang,
   a.invDetMerek AS merekBarang,
   a.invDetSpesifikasi AS spesifikasi,
   satuanbrgNama AS satuanBarang,
   a.invDetNilaiPerolehanSatuan AS hargaSatuan,
   doDetailRuang,
   doDetailTglTerima,
   CONCAT(IFNULL(CONCAT(kampusNama,' &raquo '),''),IFNULL(CONCAT(gedungNama,' &raquo '),''),ruangNama) AS ruangNama
FROM
   do_mst
   JOIN do_detail ON doDoId = doId
   JOIN inventarisasi_mst ON invMstPengadaanId = doPengadaanId
   JOIN barang_ref ON barangId = invBarangId
   JOIN inventarisasi_detail a ON (invDetMstId = invMstId AND invDetId = doInvDetId)
   LEFT JOIN satuan_barang_ref ON satuanbrgId = a.invDetSatuanBarang
   LEFT JOIN ruang ON ruangId = doDetailRuang
   LEFT JOIN gedung ON gedungId = ruangGedungId
   LEFT JOIN kampus_ref ON kampusId = gedungKampusId
WHERE
   doId = '%s'
";

$sql['get_DO_item_by_id_for_print']="
SELECT 
	f.invKodeAset AS kodeAset,
	f.invMstLabel AS namaBarang,
	f.invMerek AS merekBarang,
	f.invSpesifikasi AS spesifikasi,
	satuanbrgNama AS satuanBarang,
	f.invNilaiPerolehanSatuan AS hargaSatuan,
	doUnitJumlah AS jumlah,
	g.doDetTglTerima AS doUnitTglTerima,
	IFNULL(doUnitPICPenerima,'<em>Belum di ACC</em>') AS doUnitPICPenerima,
	unitkerjaNama 
FROM do_unit_new
LEFT JOIN 
   (SELECT invMstPengadaanId,invBarangId,invKodeAset,invMstLabel,invMerek,invSpesifikasi,invSatuanBarang,invNilaiPerolehanSatuan
    FROM inventarisasi_mst
    GROUP BY invMstPengadaanId,invBarangId) f 
ON f.invMstPengadaanId = doUnitPengadaanId AND f.invBarangId = doUnitBarangId
LEFT JOIN unit_kerja_ref ON unitkerjaId = doUnitUnitIdPenerima
LEFT JOIN satuan_barang_ref ON satuanbrgId = f.invSatuanBarang
LEFT JOIN
	(SELECT *
	 FROM 
	 	(SELECT doDetDoUnitId,doDetUnitIdPenerima,doDetTglTerima
	 	FROM do_detail_new
	 	ORDER BY doDetTglTerima DESC) a
	 GROUP BY a.doDetDoUnitId,a.doDetUnitIdPenerima) g
ON g.doDetDoUnitId = doUnitId AND g.doDetUnitIdPenerima = doUnitUnitIdPenerima
WHERE doUnitMstId = %s AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
";

$sql['get_last_id'] = "
SELECT LAST_INSERT_ID() AS id;
";

$sql['get_module_access'] = "
SELECT COUNT(a.ModuleId) AS akses
FROM gtfw_group_module a
LEFT JOIN gtfw_module b ON b.ModuleId = a.ModuleId
WHERE 
	a.GroupId = (SELECT GroupId FROM gtfw_user WHERE UserId = '%s') AND 
	b.Module = '%s' AND
	b.SubModule = '%s'
";

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

$sql['add_do_mst'] = "
INSERT INTO do_mst_new
SET
	doMstPengadaanId = %s,
	doMstTglDO = %s,
	doMstNoDO = %s,
	doMstUnitId = %s,
	doMstNIP = %s,
	doMstPengguna = %s,
	doMstPICPengirim = %s,
	doMstKeterangan = %s,
	doMstFile = %s,
	doMstTglUpdate = NOW(),
	doMstUserId = %s
";

$sql['add_do_unit'] = "
INSERT INTO do_unit_new
SET
	doUnitMstId = (SELECT MAX(doMstId) FROM do_mst_new),
	doUnitUnitIdPenerima = %s,
	doUnitJumlah = %s,
	doUnitBarangId = %s,
	doUnitPengadaanId = %s
";

$sql['add_do_detail']="
INSERT INTO do_detail_new(doDetDoUnitId,doDetInvDetId,doDetUnitIdPenerima,doDetMstPengadaanId)
SELECT
   (SELECT MAX(doUnitId) FROM do_unit_new),
   invDetId,'%s',invMstPengadaanId
FROM inventarisasi_detail
LEFT JOIN inventarisasi_mst
ON invDetMstId = invMstId
WHERE
   invMstPengadaanId = %s AND
   invDetBarangId = %s AND
   invDetId NOT IN (SELECT doDetInvDetId FROM do_detail_new)
LIMIT %s
";

$sql['add_do_detail_old'] = "
INSERT INTO do_detail(doDoId,doInvDetId)
SELECT
   (SELECT MAX(doId) FROM do_mst),
   invDetId
FROM
   inventarisasi_detail
WHERE
   invDetMstId = %s AND
   invDetId NOT IN (SELECT doInvDetId FROM do_detail)
LIMIT %s
";

$sql['update_do_detail'] = "
INSERT INTO
   do_detail
   (
		doDoId,
		doInvDetId
   )
SELECT
   %s,
   invDetId
FROM
   inventarisasi_detail
WHERE
   invDetMstId = %s AND
   invDetId NOT IN (SELECT doInvDetId FROM do_detail)
LIMIT %s
";

$sql['update_do_mst'] = "
UPDATE
   do_mst_new
SET
   doMstTglDO = %s,
   doMstNoDO = %s,
   doMstUnitId = %s,
   doMstNIP = %s,
   doMstPengguna = %s,
   doMstPICPengirim = %s,
   doMstKeterangan = %s,
   doMstFile = IFNULL(%s, doMstFile),
   doMstTglUpdate = NOW(),
   doMstUserId = %s
WHERE
   doMstId = %s
";

$sql['update_do_unit']="
UPDATE do_unit_new
SET doUnitJumlah = %s
WHERE 
	doUnitBarangId = %s AND 
	doUnitUnitIdPenerima = %s AND 
	doUnitPengadaanId = %s AND 
	doUnitMstId = %s AND 
	doUnitPICPenerima IS NULL
";

$sql['update_do_detail']="
INSERT INTO do_detail_new(doDetDoUnitId,doDetInvDetId,doDetUnitIdPenerima,doDetMstPengadaanId)
SELECT
	[UNIT_ID],
	invDetId,
	'%s',
	invMstPengadaanId
FROM inventarisasi_detail
LEFT JOIN inventarisasi_mst
ON invDetMstId = invMstId
WHERE
invMstPengadaanId = %s AND
invDetBarangId = %s AND
invDetId NOT IN (SELECT doDetInvDetId FROM do_detail_new)
LIMIT %s
";

$sql['delete_do_detail'] = "
DELETE FROM
   do_detail_new
WHERE
   doDetDoUnitId = (SELECT doUnitId 
   					FROM do_unit_new 
   					WHERE 
   						doUnitBarangId = %s AND 
						doUnitUnitIdPenerima = %s AND 
						doUnitPengadaanId = %s AND 
						doUnitMstId = %s AND 
						doUnitPICPenerima IS NULL)
";

$sql['update_pic_penerimaan'] = "
UPDATE
   do_unit_new
SET
   doUnitPICPenerima = %s,
   doUnitTgl = NOW(),
   doUnitUserIdPenerima = %s
WHERE
   doUnitMstId = %s AND doUnitUnitIdPenerima = %s
";

$sql['update_data_inventarisasi'] = "
UPDATE
   do_detail_new a,
   /*JOIN do_unit_new g ON doUnitId = doDetDoUnitId,*/
   inventarisasi_detail b,
   ruang c,
   gedung d,
   kampus_ref e,
   gtfw_user f
SET
   a.doDetRuangId = c.ruangId,
   a.doDetTglTerima = '%s',
   b.invDetRuanganId = c.ruangId,
   b.invDetGedungId = d.gedungId,
   b.invDetKampus = e.kampusId,
   /*a.doDetailTglUbah = NOW(),
   a.doDetailUserId = f.UserId,*/
   b.invDetTglUbah = NOW(),
   b.invDetUserId = f.UserId,
   b.invDetUnitKerja = %s /*g.doUnitUnitIdPenerima*/
WHERE
   a.doDetInvDetId = '%s' AND
   a.doDetInvDetId = b.invDetId AND
   c.ruangId = '%s' AND
   d.gedungId = c.ruangGedungId AND
   e.kampusId = d.gedungKampusId AND
   f.UserId = '%s'
";

$sql['add_history_lokasi'] = "
INSERT INTO
   inventarisasi_lokasi_history
   (
      invHisInvDetId,
      InvHisKampusId,
      invHisGedungId,
      InvHisRuangId,
      InvHisTanggal,
      InvHisUserId
   )
SELECT
   invDetId,
   invDetKampus,
   invDetGedungId,
   invDetRuanganId,
   '%s',
   '%s'
FROM
   inventarisasi_detail
WHERE
   invDetId = '%s'
";

$sql['delete_delivery_order_old'] = "
DELETE do_mst_new FROM
   do_mst_new
JOIN do_unit_new ON doUnitMstId = doMstId
WHERE
   doMstId IN (%s) AND
   doUnitPICPenerima IS NULL
";

$sql['delete_delivery_order']="
DELETE do_mst_new 
FROM do_mst_new
LEFT JOIN 
 (SELECT doUnitMstId,COUNT(doUnitId) AS diterima 
  FROM do_unit_new 
  WHERE doUnitPICPenerima IS NOT NULL 
  GROUP BY doUnitMstId) b 
ON b.doUnitMstId = doMstId
WHERE
   doMstId IN (%s) AND
   b.diterima IS NULL
";
?>
