<?php

//==========GET DATA ===============================
$sql['get_status_aset']=" 	
	SELECT 
		statusAsetId AS id,
		statusAsetNama AS `name` 
	FROM
		aset_ref_aset_status 
";

$sql['get_kondisi_aset']=" 	
	SELECT 
		kondisibrgId AS id,
		kondisibrgNama AS `name` 
	FROM
		kondisi_barang_ref 
";

$sql['get_satuan_aset']=" 	
	SELECT 
		`satuanbrgId` AS id,
		`satuanbrgNama` AS `name` 
	FROM
		`satuan_barang_ref` 
";

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

$sql['get_count_data_perbaikan_barang_history']=" 	
	SELECT 
		COUNT(*) AS total 
	FROM
		aset_perbaikan 
		LEFT JOIN mitra_ref 
			ON mitraId = perbaikanMitraId 
		LEFT JOIN aset_perbaikan_det 
			ON perbaikanPerbaikanId = perbaikanId 
		LEFT JOIN inventarisasi_detail 
			ON invDetId = perbaikanInvDetId 
		LEFT JOIN inventarisasi_mst 
			ON invDetMstId = invMstId 
		LEFT JOIN barang_ref c 
			ON invBarangId = barangId 
		LEFT JOIN inv_kondisi_history d 
			ON invDetId = invKondisiBrgBrgDetId 
		LEFT JOIN kondisi_barang_ref e 
			ON invKondisiBrgKondId = kondisibrgId 
	WHERE invKondisiBrgId = 
		(SELECT 
			MAX(invKondisiBrgId) 
		FROM
			inv_kondisi_history 
		WHERE invKondisiBrgBrgDetId = invDetId)
";

$sql['get_data_perbaikan_barang_history']=" 	
	SELECT 
           SQL_CALC_FOUND_ROWS
	   perbaikanId AS id,
	   perbaikanBA AS nomorba,
	   perbaikanTgl AS tanggal_perbaikan,
	   perbaikanDoDate AS do_date,
	   mitraId AS idMitra,
	   mitraNama AS namaMitra,
	   perbaikanCatatan AS catatan,
	   perbaikanPerkiraanHarga AS perbaikanBiaya,
	   invDetKodeBarang AS barangKode,
	   barangNama AS barangNama,
	   invDetId AS barangId,
	   kondisibrgNama AS barangKondisi,
           (SELECT COUNT(*) FROM aset_perbaikan_det_history WHERE perbaikanPerbaikanDetId = perbaikanDetId) AS isDone
	FROM
	   aset_perbaikan 
	   LEFT JOIN mitra_ref 
		  ON mitraId = perbaikanMitraId 
	   LEFT JOIN aset_perbaikan_det 
		  ON perbaikanPerbaikanId = perbaikanId 
	   LEFT JOIN inventarisasi_detail 
		  ON invDetId = perbaikanInvDetId 
	   LEFT JOIN inventarisasi_mst 
		  ON invDetMstId = invMstId 
	   LEFT JOIN barang_ref c 
		  ON invBarangId = barangId 
	   LEFT JOIN inv_kondisi_history d 
		  ON invDetId = invKondisiBrgBrgDetId 
	   LEFT JOIN kondisi_barang_ref e 
		  ON invKondisiBrgKondId = kondisibrgId 
	WHERE invKondisiBrgId = 
	   (SELECT 
		  MAX(invKondisiBrgId) 
	   FROM
		  inv_kondisi_history 
	   WHERE invKondisiBrgBrgDetId = invDetId) 
	   AND (
		  barangNama LIKE '%s' 
		  OR invDetKodeBarang LIKE '%s'
	   )
	ORDER BY perbaikanId DESC LIMIT %s, %s
";

$sql['get_data_perbaikan_barang_history_by_id']=" 	
	SELECT 
		perbaikanDetId AS id,
		perbaikanBA AS nomorba,
		perbaikanTgl AS tanggal_perbaikan,
		perbaikanDoDate AS do_date,
		mitraId AS idMitra,
		mitraNama AS namaMitra,
		perbaikanCatatan AS catatan,
		perbaikanPerkiraanHarga AS perbaikanBiaya,
		CONCAT(invKodeAset,SUBSTRING(invDetKodeBarang, LENGTH(invKodeAset)+1,LENGTH(invDetKodeBarang))) AS barangKode,
		barangNama AS barangNama,
		invDetId AS barangId,
		kondisibrgNama AS barangKondisi,
		invDetNoPolisi,
		invDetWarna,
		invDetAsetStatusId,
		kondisibrgId
	FROM
		aset_perbaikan 
		LEFT JOIN mitra_ref 
			ON mitraId = perbaikanMitraId 
		LEFT JOIN aset_perbaikan_det 
			ON perbaikanPerbaikanId = perbaikanId 
		LEFT JOIN inventarisasi_detail 
			ON invDetId = perbaikanInvDetId 
		LEFT JOIN inventarisasi_mst 
			ON invDetMstId = invMstId 
		LEFT JOIN barang_ref c 
			ON invBarangId = barangId 
		LEFT JOIN inv_kondisi_history d 
			ON invDetId = invKondisiBrgBrgDetId 
		LEFT JOIN kondisi_barang_ref e 
			ON invKondisiBrgKondId = kondisibrgId 
	WHERE invKondisiBrgId = 
		(SELECT 
			MAX(invKondisiBrgId) 
		FROM
			inv_kondisi_history 
		WHERE invKondisiBrgBrgDetId = invDetId)
		AND invDetId = %d
";

$sql['get_list_perbaikan_barang_history_by_id']=" 	
	SELECT 
	   invDetKodeBarang AS barangKode,
	   barangNama AS barangNama,
	   invDetNoPolisi,
	   invDetWarna,
	   DATE_FORMAT(
		  `invDetTglPembelian`,
		  '%%Y'
	   ) AS tahun,
	   `perbaikanDetHisTgl` AS tanggal,
	   `perbaikanDetHisJenis` AS jenis,
	   `perbaikanDetHisHargaSatuan` AS harga_satuan,
	   `perbaikanDetHisJumlah` AS jumlah,
	   `perbaikanDetHisSatuanId` AS satuan,
	   `perbaikanDetHisTotal` AS total,
	   `perbaikanDetHisKeterangan` AS keterangan,
	   satuanbrgNama 
	FROM
	   aset_perbaikan 
	   LEFT JOIN aset_perbaikan_det 
		  ON perbaikanPerbaikanId = perbaikanId 
	   LEFT JOIN aset_perbaikan_det_history 
		  ON perbaikanPerbaikanDetId = perbaikanDetId 
	   LEFT JOIN satuan_barang_ref 
		  ON satuanbrgId = perbaikanDetHisSatuanId 
	   LEFT JOIN inventarisasi_detail 
		  ON invDetId = perbaikanInvDetId 
	   LEFT JOIN inventarisasi_mst 
		  ON invDetMstId = invMstId 
	   LEFT JOIN barang_ref c 
		  ON invBarangId = barangId 
	   LEFT JOIN inv_kondisi_history d 
		  ON invDetId = invKondisiBrgBrgDetId 
	WHERE invKondisiBrgId = 
	   (SELECT 
		  MAX(invKondisiBrgId) 
	   FROM
		  inv_kondisi_history 
	   WHERE invKondisiBrgBrgDetId = invDetId)
		AND invDetId = %d
";

# ---- lakukan do disini ---- #
$sql['do_add_perbaikan_barang_history'] = "
	INSERT INTO 
		`aset_perbaikan_det_history` 
	SET 
		`perbaikanPerbaikanDetId` = '%s',
		`perbaikanDetHisTgl` = '%s',
		`perbaikanDetHisJenis` = '%s',
		`perbaikanDetHisHargaSatuan` = '%s',
		`perbaikanDetHisJumlah` = '%s',
		`perbaikanDetHisSatuanId` = '%s',
		`perbaikanDetHisTotal` = '%s',
		`perbaikanDetHisKeterangan` = '%s',
		`perbaikanDetHisTglUbah` = NOW(),
		`perbaikanDetHisUserId` = '%s' 
";

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

$sql['do_add_kondisi_history'] = "
	INSERT INTO 
		`inv_kondisi_history` 
	SET 
		`invKondisiBrgBrgDetId` = '%s',
		`invKondisiBrgKondId` = '%s',
		`invKondisiBrgTglKondisi` = CURRENT_DATE,
		`invKondisiTglHistory` = NOW(),
		`invKondisiUserId` = '%s'
";
?>
