<?php

//==========GET DATA ======================================
$sql['get_data_user_by_user_name'] = "
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,
   unitkerjaId,
   unitkerjaNama,
   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
   UserName = %s
";

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

$sql['get_count_data_lelang_barang'] = "
	SELECT
		COUNT(DISTINCT(transAtkMstId)) AS total
	FROM aset_transaksi_atk
		LEFT JOIN aset_transaksi_atk_detil
		  ON transAtkDetTransMstId = transAtkMstId
		LEFT JOIN inv_atk_det
		  ON invAtkDetId = transAtkDetAtkId
		LEFT JOIN inv_atk_brg
		  ON invDet = invAtkDetId
		LEFT JOIN inv_atk_gudang
		  ON invAtkGudangInvDtlbrgId = invBrgAtkId
		LEFT JOIN ruang
		  ON ruangId = invAtkGudangRuangId
		LEFT JOIN unit_kerja_ref
		  ON unitkerjaId = transAtkMstUnitKerjaId
	WHERE transAtkMstNomor LIKE '%s'
		AND transAtkNamaPenerima LIKE '%s' AND ruangId != '%s' AND (transAtkMstUnitKerjaId =  %s
        OR unitkerjaParentId =  %s
        OR '1' =  %s)
";
		
$sql['get_data_lelang_barang']="
	SELECT
		SQL_CALC_FOUND_ROWS
		transAtkMstId         AS trans_id,
		transAtkMstTglEntry   AS trans_tanggal,
		transAtkMstNomor      AS trans_nomor,
		transAtkMstKeterangan AS trans_keterangan,
		transAtkNamaPenerima  AS trans_nama,
		unitkerjaNama         AS trans_unit
	FROM aset_transaksi_atk
		LEFT JOIN aset_transaksi_atk_detil
		  ON transAtkDetTransMstId = transAtkMstId
		LEFT JOIN inv_atk_det
		  ON invAtkDetId = transAtkDetAtkId
		LEFT JOIN inv_atk_brg
		  ON invDet = invAtkDetId
		LEFT JOIN inv_atk_gudang
		  ON invAtkGudangInvDtlbrgId = invBrgAtkId
		LEFT JOIN ruang
		  ON ruangId = invAtkGudangRuangId
		LEFT JOIN unit_kerja_ref
		  ON unitkerjaId = transAtkMstUnitKerjaId
	WHERE transAtkMstNomor LIKE '%s'
		AND transAtkNamaPenerima LIKE '%s' AND ruangId != '%s' AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s') AND transAtkMstKeterangan LIKE 'No Usul%%'
	GROUP BY transAtkMstId
	ORDER BY transAtkMstId DESC
	LIMIT %s, %s
";
 
$sql['get_data_pengelolaan_by_id']=" 	
   SELECT
     transAtkMstId              AS trans_id,
     transAtkMstTglEntry        AS trans_tanggal,
     transAtkMstNomor           AS trans_nomor,
     transAtkMstKeterangan      AS trans_keterangan,
     transAtkNamaPenerima       AS trans_nama,
     transAtkDetAtkId           AS barangId,
     CONCAT(LPAD(golbrgKode, 1, '0'), '.', LPAD(bidangbrgKode, 2, '0'), '.', LPAD(kelbrgKode, 2, '0'), '.', LPAD(subkelbrgKode, 2, '0'), '.', LPAD(barangKode, 3, '0'),'.',invAtkKode) AS barangKode,
     invAtkNama                 AS barangNama,
     transAtkDetKeteranganDetil AS barangDiskripsi,
     transAtkDetJmlAtk          AS jumlah,
     transAtkDetNilaiSatuan     AS lelangBiaya,
     (SELECT
        lelangBiaya*jumlah) AS total,
     unitkerjaNama              AS nama_unit
   FROM aset_transaksi_atk
     LEFT JOIN aset_transaksi_atk_detil
       ON transAtkDetTransMstId = transAtkMstId
     LEFT JOIN inv_atk_det
       ON invAtkDetId = transAtkDetAtkId
     LEFT JOIN inv_atk_master
       ON invAtkMstId = invAtkDetMstId
     LEFT JOIN inv_atk_jenis_ref
       ON invAtkMstJenisPersediaanId = invAtkJenisRefId
     LEFT JOIN barang_ref
       ON invAtkJenisBarangRefId = barangId
     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 unit_kerja_ref
       ON unitkerjaId = transAtkMstUnitKerjaId
	WHERE 
		transAtkMstId = '%s'	
	
";

$sql['get_atk_mst_nomor'] = "
	SELECT
		transAtkMstId,
		transAtkMstNomor
	FROM aset_transaksi_atk
	ORDER BY transAtkMstNomor DESC LIMIT 0,1
"; 

# FOR POPUP USULAN BHP

$sql['get_data_usulan_bhp'] = "
SELECT
usulanBrgNoUsulan,
usulanBrgDetId,
unitkerjaNama,
unitkerjaId,
a.invAtkNama,
usulanBrgDetJml,
usulanBrgDetJmlPengadaan AS pengadaan,
usulanBrgDetJmlDipakai AS jmlTerpakai,
IF(usulanBrgDetTglPakai = '0000-00-00','-',usulanBrgDetTglPakai) AS usulanBrgDetTglPakai,
c.Kode,
a.invAtkJenisRefId,
IF(usulanBrgDetTglPakai = '0000-00-00',999999,DATEDIFF(usulanBrgDetTglPakai,NOW())) AS counter
/* cek apakah sudah dipindah gudang*/
FROM bhp_usulan_brg_det
LEFT JOIN bhp_usulan_brg ON usulanBrgId = usulanBrgDetUsulanBrgId
LEFT JOIN inv_atk_jenis_ref a ON a.invAtkJenisRefId = usulanBrgDetBrgId
LEFT JOIN unit_kerja_ref ON unitkerjaId = usulanBrgUnitId
LEFT JOIN (
SELECT invAtkJenisRefId, CONCAT(LPAD(`golbrgKode`,1,0),'.',LPAD(`bidangbrgKode`,2,0),'.',LPAD(`kelbrgKode`,2,0),'.', LPAD(`subkelbrgKode`,2,0),'.',LPAD(`barangKode`,3,0),'.',`invAtkKode`) AS `kode`
FROM `golongan_barang_ref`
JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
JOIN `inv_atk_jenis_ref` ON (invAtkJenisBarangRefId = barangId)
) c ON c.invAtkJenisRefId = a.invAtkJenisRefId
WHERE
usulanBrgNoUsulan LIKE '%s'AND
invAtkNama LIKE '%s'
[SEARCH_UNIT]
AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
AND usulanBrgIsApprove = 'Ya'
AND (usulanBrgDetJmlPengadaan IS NOT NULL AND usulanBrgDetJmlPengadaan > 0)
/*AND (usulanBrgDetJmlDipakai < usulanBrgDetJmlPengadaan)*/
ORDER BY counter ASC
LIMIT %s,%s
";

$sql['get_combo_unit_kerja'] = "
SELECT unitkerjaId AS id, unitkerjaNama AS name
FROM unit_kerja_ref
WHERE unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s'
";

//do-----------------------------------------------------
$sql['do_add_lelang_barang'] = "
	INSERT INTO 
		aset_transaksi_atk(
			transAtkMstTglEntry, 
			transAtkMstNomor, 
			transAtkMstKeterangan, 
			transAtkNamaPenerima, 
			transAtkMstTgl, 
			transAtkMstUserid,
			transAtkMstUnitKerjaId
		)
			VALUES 
      (
			'%s','%s','%s','%s', NOW(), '%s', '%s'
		)
";

$sql['do_add_pengelolaan_det'] = "
	INSERT INTO 
		aset_transaksi_atk_detil(
			transAtkDetTransMstId,
         transAtkDetAtkId,
         transAtkDetKeteranganDetil,
         transAtkDetJmlAtk,
         transAtkDetNilaiSatuan
		)
			VALUES 
		(
			(SELECT MAX(transAtkMstId) FROM aset_transaksi_atk),
			'%s',			
			'%s',
			'%s',
			'%s'
      )
";

//update
$sql['do_update_lelang_barang'] = "
	UPDATE 
		aset_transaksi_atk
	SET 
		transAtkMstTglEntry = '%s',
		transAtkMstKeterangan = '%s',
		transAtkNamaPenerima = '%s',
		transAtkMstTgl = NOW(),
		transAtkMstUnitKerjaId = '%s',
		transAtkMstUserid = '%s'
	WHERE 
		transAtkMstId = '%s'
";

$sql['do_update_pengelolaan_det'] = "
	INSERT INTO 
		aset_transaksi_atk_detil(
			transAtkDetTransMstId,
         transAtkDetAtkId,
         transAtkDetKeteranganDetil,
         transAtkDetJmlAtk,
         transAtkDetNilaiSatuan
		)
			VALUES 
		(
			'%s',
			'%s',			
			'%s',
			'%s',
			'%s'
      )
";

//delete
$sql['do_delete_pengelolaan_det'] = "
	DELETE
	FROM aset_transaksi_atk_detil
	WHERE transAtkDetTransMstId IN ('%s')
";

$sql['do_delete_lelang_barang'] = "
	DELETE
	FROM aset_transaksi_atk
	WHERE transAtkMstId = '%s'
";

$sql['do_delete_pengelolaan_by_array_id'] = "
	DELETE from aset_transaksi_atk
   WHERE 
     transAtkMstId  IN ('%s')
";

$sql['get_mst_id'] = "
	SELECT invAtkMstId FROM inv_atk_master WHERE invAtkMstJenisPersediaanId = '%s'
";

$sql['get_brg_by_mst_id'] = "
	SELECT
		invBrgAtkId
	FROM inv_atk_brg
	LEFT JOIN inv_atk_det
		ON invAtkDetId = invdet
	WHERE invAtkDetMstId = '%s'
";

$sql['get_max_gdg_by_det_id']="
	SELECT
	invAtkGudangJumlah AS jumlah
	FROM inv_atk_gudang
	WHERE invAtkGudangInvDtlbrgId = %s
		AND invAtkGudangRuangId = %s
";

$sql['update_gudang_max'] = "
	UPDATE inv_atk_gudang
		SET invAtkGudangJumlah = '%s'
		WHERE invAtkGudangRuangId = '%s'
			AND invAtkGudangInvDtlbrgId = '%s'
";

$sql['add_log_update'] = "
	INSERT INTO `log_atk`
	SET
		`logAtkRuangId` = '%s' ,
		`logAtkAtkId` = '%s' ,
		`logAtkJmlBrg` = '%s' ,
		`logAtkStatus` = 'Register Transaksi Harian',
		`logAtkTgl` = NOW(),
		`logAtkKeterangan` = '%s'
";

$sql['do_update_stok_usulan_bhp']="
UPDATE bhp_usulan_brg_det
SET usulanBrgDetJmlDipakai = IFNULL(usulanBrgDetJmlDipakai,0) + %d
WHERE usulanBrgDetId = %s
";
?>
