<?php
//untuk mitra
$sql['generate_number'] = "
	SELECT
		sewaNoSewa
	FROM aset_sewa
	ORDER BY sewaNoSewa DESC
	LIMIT 0,1
";

$sql['validation_date'] = "
	SELECT
	  CONCAT(sewaTanggalMulai,' ',sewaJamMulai) AS mulai,
	  CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai) AS selesai,
	  IF( ('%s'>=CONCAT(sewaTanggalMulai,' ',sewaJamMulai)AND '%s'<=CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) 
	  OR ('%s'<=CONCAT(sewaTanggalMulai,' ',sewaJamMulai) AND '%s'>=CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) 
	  OR ('%s'<=CONCAT(sewaTanggalMulai,' ',sewaJamMulai) AND '%s'<=CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai) 
	  AND '%s'>=CONCAT(sewaTanggalMulai,' ',sewaJamMulai)) OR ('%s'<=CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai) 
	  AND '%s'>=CONCAT(sewaTanggalMulai,' ',sewaJamMulai)),1,0 ) AS status_isi,
	  invDetMstLabel AS label
	FROM aset_sewa
	  LEFT JOIN aset_sewa_det
		 ON asetSewaDetSewaid = sewaId
	  LEFT JOIN aset_ref_biaya_sewa
		 ON invBiayaSewaId = asetSewaDetBiayaSewaId
	  LEFT JOIN inventarisasi_detail
		 ON invDetId = invBiayaSewaInvId
	  LEFT JOIN unit_kerja_ref
		 ON unitkerjaId = sewaUnitId
	  LEFT JOIN mitra_ref
		 ON mitraId = sewaMitraId
	WHERE asetSewaDetBiayaSewaId = '%s'
	AND sewaId != '%s'
";

$sql['validation_date_by_id'] = "
	SELECT
	  sewaId
	FROM aset_sewa
	  LEFT JOIN aset_sewa_det
		 ON asetSewaDetSewaid = sewaId
	  LEFT JOIN aset_ref_biaya_sewa
		 ON invBiayaSewaId = asetSewaDetBiayaSewaId
	  LEFT JOIN inventarisasi_detail
		 ON invDetId = invBiayaSewaInvId
	  LEFT JOIN unit_kerja_ref
		 ON unitkerjaId = sewaUnitId
	  LEFT JOIN mitra_ref
		 ON mitraId = sewaMitraId
	WHERE 
		sewaTanggalMulai = '%s' AND 
		sewaJamMulai = '%s' AND 
		sewaTanggalSelesai = '%s' AND 
		sewaJamSelesai = '%s' AND 
		asetSewaDetBiayaSewaId = '%s'
";

$sql['get_mitra_view'] ="
   SELECT
   	  SQL_CALC_FOUND_ROWS
      mitraId as mkId,
      mitraNama as mkNama,
      mitraAlamat as mkAlamat,
      mitraTelp as mkTelepon,
      (SELECT unitkerjaKodeSistem FROM unit_kerja_ref WHERE unitkerjaId = (SELECT userunitkerjaUnitkerjaId FROM user_unit_kerja WHERE userunitkerjaUserId = mitraDataPrepared)) AS kodeSistem
   FROM
      mitra_ref
   WHERE
      mitraNama LIKE '%s' AND
      mitraAlamat LIKE '%s'
   GROUP BY mitraId
   HAVING (kodeSistem = '%s' OR kodeSistem LIKE '%s')
   ORDER BY
      mitraNama
   LIMIT
      %s, %s
";

$sql['get_count_mitra_view'] = "
   SELECT
		mitraId as id
   FROM
      mitra_ref
   WHERE
      mitraNama LIKE '%s' AND
      mitraAlamat LIKE '%s'
   GROUP BY
      mitraId
";
//--end

//--untuk unit
$sql['get_data_unit'] = "
	SELECT
		a.unitkerjaId as unitid,
		a.unitkerjaNama as unitnama,
		(SELECT COUNT(b.unitkerjaId) FROM unit_kerja_ref b WHERE b.unitkerjaParentId = a.unitkerjaId) AS isParent
	FROM
		unit_kerja_ref a
	WHERE
		/*(unitkerjaId = %%s OR '1' = %%s) AND*/ a.unitkerjaNama LIKE '%s'
	ORDER BY a.unitkerjaNama
	LIMIT %s, %s
";

$sql['get_count_data_unit'] = "
	SELECT
		unitkerjaId as id
	FROM
		unit_kerja_ref
	WHERE
		(unitkerjaId = %s OR '1' = %s) AND unitkerjaNama LIKE '%s'
";
//--end unit


$sql['get_data_aset_sewa_count'] ="
	SELECT
		COUNT(DISTINCT(invBiayaSewaId)) AS total
	FROM 
		aset_ref_biaya_sewa
   LEFT JOIN 
		inventarisasi_detail ON invDetId = invBiayaSewaInvId
	WHERE 
		invBiayaSewaKode LIKE '%s' AND invDetMstLabel LIKE '%s' AND invDetAsetStatusId = 1
	ORDER BY invDetMstLabel ASC 
";

$sql['get_data_aset_sewa'] = "
	SELECT 
	   SQL_CALC_FOUND_ROWS
	   invBiayaSewaId AS aset_id,
	   invBiayaSewaKode AS aset_kode,
	   invDetMstLabel AS aset_label,
	   invBiayaSewaNilai AS aset_harga_sewa,
	   invDetId AS status_id 
	FROM aset_ref_biaya_sewa 
	LEFT JOIN inventarisasi_detail ON invDetId = invBiayaSewaInvId 
	LEFT JOIN unit_kerja_ref ON unitkerjaId = invDetUnitKerja
	WHERE invBiayaSewaKode LIKE '%s' 
	   AND invDetMstLabel LIKE '%s' 
	   AND invDetAsetStatusId = 1 
	   AND (unitkerjaKodeSistem = '%s' OR unitkerjaKodeSistem LIKE '%s')
	ORDER BY invDetMstLabel  ASC LIMIT %s, %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
	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['do_add_sewa_aset'] = "
	INSERT INTO aset_sewa (
		sewaNoSewa,
		sewaNama,
		sewaNomorContact,
		sewaAlamatPenyewa,
		sewaKeterangan,
		sewaJumlahHari,
		sewaTanggalSewa,
		sewaTanggalMulai,
		sewaJamMulai,
		sewaTanggalSelesai,
		sewaJamSelesai,
		sewaUnitId,
		sewaMitraId,
		sewaTanggal,
		sewaUserId
	) VALUES (
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		DATE(NOW()),
		'%s',
		'%s',
		'%s',
		'%s',
		NULLIF(%s, 0),
		NULLIF(%s, 0),
		NOW(),
		'%s'
	)
";

$sql['do_add_sewa_aset_det'] = "
	INSERT INTO aset_sewa_det (
		asetSewaDetSewaid,
		asetSewaDetBiayaSewaId,
		asetSewaDetValue,
		asetSewaDetPotongan
	) VALUES (
		(SELECT MAX(sewaId) FROM aset_sewa),
		'%s',
		'%s',
		'%s'
	)
";

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

$sql['get_count_data_sewa_aset'] = "
	SELECT
		count(sewaId) AS total
	FROM aset_sewa
	WHERE sewaNoSewa LIKE '%s' AND (IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) = '%s'
				 OR 'all' = '%s')
";

$sql['get_data_sewa_aset'] = "
	SELECT
	  SQL_CALC_FOUND_ROWS
	  sewaId             AS sewa_id,
	  sewaNoSewa         AS sewa_nomor,
	  sewaTanggalMulai   AS sewa_tanggal_awal,
	  sewaJamMulai       AS sewa_waktu_awal,
	  sewaTanggalSelesai AS sewa_tanggal_akhir,
	  sewaJamSelesai     AS sewa_waktu_akhir,
	  sewaKeterangan     AS keterangan,
	  uk.unitkerjaNama   AS unitkerja,
	  mitraNama          AS mitra,
	  IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) AS status_isi,
	  IFNULL(pengembSewaId,0) 		AS pengembalian
	FROM aset_sewa
	  LEFT JOIN aset_sewa_det
		 ON asetSewaDetSewaid = sewaId
	  LEFT JOIN aset_ref_biaya_sewa
		 ON invBiayaSewaId = asetSewaDetBiayaSewaId
	  LEFT JOIN inventarisasi_detail
		 ON invDetId = invBiayaSewaInvId
	  LEFT JOIN unit_kerja_ref uk
		 ON uk.unitkerjaId = sewaUnitId
	  LEFT JOIN mitra_ref
		 ON mitraId = sewaMitraId
	  LEFT JOIN aset_sewa_pengembalian
		 ON pengembSewaSewaId = sewaId
	  LEFT JOIN unit_kerja_ref u ON u.unitkerjaId = invDetUnitKerja
	WHERE sewaNoSewa LIKE '%s'
		AND (u.unitkerjaKodeSistem = '%s' OR u.unitkerjaKodeSistem LIKE '%s')
		 AND (IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) = '%s'
				 OR 'all' = '%s')
	GROUP BY sewaId
	ORDER BY status_isi DESC
	LIMIT %d, %d
";

$sql['get_data_sewa_aset_by_id'] = "
	SELECT
		sewaId                 AS sewa_id,
		sewaNoSewa             AS nomor_sewa,
		sewaKeterangan         AS keterangan,
		sewaJumlahHari			  AS jumlah_hari,	
		sewaTanggalMulai       AS sewa_tanggal_awal,
		sewaJamMulai           AS sewa_waktu_awal,
		sewaTanggalSelesai     AS sewa_tanggal_akhir,
		sewaJamSelesai         AS sewa_waktu_akhir,
		sewaUnitId             AS pengadaanUnitkerjaId,
		sewaMitraId            AS pengadaanMitraId,
		asetSewaDetBiayaSewaId AS asetId,
		invBiayaSewaKode       AS asetKode,
		unitkerjaNama          AS unitkerja,
		mitraNama              AS mitra,
		invDetMstLabel            AS asetLabel,
		asetSewaDetValue       AS asetHargaSewa,
		asetSewaDetPotongan    AS asetPotongan,
		sewaNama               AS penyewa,
		sewaNomorContact       AS kontak,
		sewaAlamatPenyewa      AS alamat,
		invDetId 	  AS statusId
	FROM aset_sewa
		LEFT JOIN aset_sewa_det
			ON asetSewaDetSewaid = sewaId
		LEFT JOIN aset_ref_biaya_sewa
			ON invBiayaSewaId = asetSewaDetBiayaSewaId
		LEFT JOIN inventarisasi_detail
			ON invDetId = invBiayaSewaInvId
		LEFT JOIN unit_kerja_ref
			ON unitkerjaId = sewaUnitId
		LEFT JOIN mitra_ref
			ON mitraId = sewaMitraId
	WHERE sewaId IN ('%s')
";

$sql['do_update_investaris_detil'] = "
	UPDATE inventarisasi_detail
	SET invDetAsetStatusId = '%s',
	  invDetTglUbah = NOW(),
	  invDetUserId = '%s'
	WHERE invDetId = '%s'
";

$sql['do_delete_sewa_aset_det'] = "
	DELETE FROM aset_sewa_det
	WHERE asetSewaDetSewaid IN ('%s')
";

$sql['do_delete_sewa_aset_by_array_id'] = "
	DELETE from aset_sewa
   WHERE 
     sewaId IN ('%s')
";

$sql['do_update_sewa_aset'] = "
	UPDATE 
		aset_sewa 
	SET  
		sewaNama = '%s',
		sewaNomorContact = '%s',
		sewaAlamatPenyewa = '%s',
		sewaKeterangan = '%s',
		sewaJumlahHari = '%s',
		sewaTanggalSewa = DATE(NOW()),
		sewaTanggalMulai = '%s',
		sewaJamMulai = '%s',
		sewaTanggalSelesai = '%s',
		sewaJamSelesai = '%s',
		sewaUnitId = NULLIF(%s, 0),
		sewaMitraId = NULLIF(%s, 0),
		sewaTanggal = NOW(),
		sewaUserId = '%s'
	WHERE
		sewaId = '%s'
";

$sql['do_update_sewa_aset_det'] = "
	INSERT INTO aset_sewa_det (
		asetSewaDetSewaid,
		asetSewaDetBiayaSewaId,
		asetSewaDetValue,
		asetSewaDetPotongan
	) VALUES (
		'%s',
		'%s',
		'%s',
		'%s'
	)
";
?>
