<?php

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

$sql['get_data_pengembalian_count'] = "
	SELECT
	  COUNT(pengembSewaId)	     AS total
	FROM aset_sewa_pengembalian
	WHERE pengembSewaNomor LIKE '%s'
";

$sql['get_data_pengembalian'] = "
	SELECT
	  SQL_CALC_FOUND_ROWS
	  pengembSewaId              AS pengembalian_id,
	  pengembSewaNomor           AS nomor,
	  pengembSewaTglPengembalian AS tanggal_kembali,
	  pengembSewaKeterangan      AS keterangan
	FROM aset_sewa_pengembalian
	WHERE pengembSewaNomor LIKE '%s'
	ORDER BY tanggal_kembali DESC
	LIMIT %d, %d
";

$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
   FROM
      mitra_ref
   WHERE
      mitraNama LIKE '%s' AND
      mitraAlamat LIKE '%s'
   GROUP BY
      mitraId
   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
		unitkerjaId as unitid,
		unitkerjaNama as unitnama
	FROM
		unit_kerja_ref
	WHERE
		(unitkerjaId = %s OR '1' = %s) AND unitkerjaNama LIKE '%s'
	ORDER BY
		unitkerjaNama
	LIMIT %s, %s
";

$sql['get_count_data_unit'] = "
	SELECT
		SQL_CALC_FOUND_ROWS
		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 
	   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 
	WHERE invBiayaSewaKode LIKE '%s' 
	   AND invDetMstLabel LIKE '%s' 
	   AND invDetAsetStatusId = 1 
	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_data_sewa_aset'] = "
	SELECT
		count(sewaId) AS total
	FROM aset_sewa
	LEFT JOIN aset_sewa_pengembalian
		 ON pengembSewaSewaId = sewaId
	WHERE sewaNoSewa LIKE '%s' AND (IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) = '1')
";

$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,
	  a.unitkerjaNama      AS unitkerja,
	  mitraNama          AS mitra,
	  IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) AS status_isi,
	  pengembSewaId 		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 b ON b.unitkerjaId = invDetUnitKerja
	  LEFT JOIN unit_kerja_ref a
		 ON a.unitkerjaId = sewaUnitId
	  LEFT JOIN mitra_ref
		 ON mitraId = sewaMitraId
	  LEFT JOIN aset_sewa_pengembalian
		 ON pengembSewaSewaId = sewaId
	WHERE sewaNoSewa LIKE '%s'
		 /*AND (IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) = '1')*/
		 AND (b.unitkerjaKodeSistem = '%s' OR b.unitkerjaKodeSistem LIKE '%s')
	GROUP BY sewaId
	ORDER BY status_isi DESC, pengembalian ASC
	LIMIT %d, %d
";

$sql['get_data_pengembalian_aset_by_id'] = "
	SELECT 
	   sewaId AS sewa_id,
	   sewaKeterangan AS keterangan,
	   invBiayaSewaId AS asetId,
	   invBiayaSewaKode AS asetKode,
	   invDetMstLabel AS asetLabel,
	   invBiayaSewaTerlambat AS terlambat,
	   invBiayaSewaRusak AS rusak,
	   invBiayaSewaGanti AS ganti,
	   '' AS subKeterangan,
	   invDetId AS statusId,
	   IF((CONCAT(sewaTanggalSelesai,' ',sewaJamSelesai)) < NOW(), 1, 0) AS statusTerlambat 
	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 
	WHERE sewaId IN ('%s')
";

$sql['get_data_pengembalian_aset_detil_by_id'] = "
	SELECT
	  pengembSewaNomor           AS nomor,
	  pengembSewaKeterangan      AS keterangan,
	  pengembSewaTglPengembalian AS tgl_kembali,
	  invBiayaSewaKode           AS kode_sewa,
	  invDetMstLabel                AS label,
	  pengembSewaDetBiayaTerlambat AS terlambat,
	  pengembSewaDetBiayaRusak   AS rusak,
	  pengembSewaDetBiayaGanti   AS ganti,
	  pengembSewaDetKeterangan   AS subketerangan
	FROM aset_sewa_pengembalian
	  JOIN aset_sewa_pengembalian_detil
		 ON pengembSewaDetMstId = pengembSewaId
	  JOIN aset_ref_biaya_sewa
		 ON invBiayaSewaId = pengembSewaDetBiayaSewaId
	  JOIN inventarisasi_detail
		 ON invDetId = invBiayaSewaInvId
	WHERE pengembSewaSewaId = '%d'
";

$sql['do_update_investaris_detil'] = "
	UPDATE inventarisasi_detail
	SET invDetAsetStatusId = '1',
	  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_add_pengembalian_aset'] = "
	INSERT INTO aset_sewa_pengembalian
	SET pengembSewaSewaId = '%s',
	  pengembSewaNomor = '%s',
	  pengembSewaKeterangan = '%s',
	  pengembSewaTglPengembalian = '%s',
	  pengembSewaTgl = NOW(),
	  pengembSewaUserId = '%s'
";

$sql['do_add_pengembalian_aset_det'] = "
	INSERT INTO aset_sewa_pengembalian_detil
	SET pengembSewaDetMstId = (SELECT MAX(pengembSewaId) FROM aset_sewa_pengembalian),
	  pengembSewaDetBiayaSewaId = '%s',
	  pengembSewaDetBiayaTerlambat = '%s',
	  pengembSewaDetBiayaRusak = '%s',
	  pengembSewaDetBiayaGanti = '%s',
	  pengembSewaDetKeterangan = '%s'
";
?>
