<?php
	$server	= "192.168.254.228:5020";
	$user		= "alauddin";
	$pass		= "alauddinPASS";
	$db		= "alauddin_devel_gtaset";
	$port		= "5020";
	$connect = mysql_connect($server, $user, $pass, $port) or die ("Not Connected");

	$selectDb=mysql_select_db($db) or die ("Database Not Found");

	if ($selectDb){

	$id =(int)$_GET['id'];

	//-- start class
	class AppInventarisasi{
		function GetListBiayaLain(){
			$sql="
				SELECT biayaId AS id, biayaName AS name FROM biaya_ref
			";
			$result = mysql_query($sql);
			$i=-1;
			while($data = mysql_fetch_assoc($result)){
			$i++;
				$return[$i] = $data;
			}
			mysql_free_result($result);
			return $return;
		}

		function GetInventarisasiDetailById($id){
			$sql="
				SELECT
				  mst.invMstId,
				  mst.invMstPengadaanId,
				  invBarangId,				  CONCAT(LPAD(golbrgKode,1,'0'),'.',LPAD(bidangbrgKode,2,'0'),'.',LPAD(kelbrgKode,2,'0'),'.',LPAD(subkelbrgKode,2,'0'),'.',LPAD(barangKode,3,'0'),'.',LPAD(invKodeBarang,4,'0')) AS invKodeAset,
				  invdet.invBarcode,
				  invdet.invMstLabel,
				  invdet.invMerek,
				  invdet.invSpesifikasi,
				  invdet.invTglPembelian,
				  sumberdanaNama                 AS invSumberDana,
				  mst.invJumlahAset,
				  satuanbrgNama                  AS invSatuanBarang,
				  invLuasTanah,
				  invdet.invNilaiPerolehanSatuan,
				  invNilaiFaktur,
				  invTotalPerolehan,
				  invSPMNumber,
				  invdet.invTglUbah,
				  invdet.invUserId,
				  invDetId,
				  invDetMstId,
				  invKodeBarang,
				  invKodeAsli,
				  invNilaiResidu,
				  invTipe,
				  invNJOP,
				  invUmurEkonomis,
				  invLokasi,
				  invKegunaan,
				  invSertifikat,
				  invAkta,
				  invBPKB,
				  invSTNK,
				  invNoMesin,
				  invNoRangka,
				  invNoPolisi,
				  invNoLain,
				  invWarna,
				  invPagar,
				  invBatasUtara,
				  invBatasTimur,
				  invBatasSelatan,
				  invBatasBarat,
				  kepemilikanbrgNama             AS invKepemilikan,
				  statuspengbrgNama              AS invPenguasaanBarang,
				  invKeteranganLain,
				  kampusNama 							AS invKampus,
				  gedungNama,
				  ruangNama,
				  invUnitKerja,
				  invDetTglPerkiraanPerbaikan,
				  golbrgNama,
				  barangNama                     AS namaAset,
				  kondisibrgNama                 AS invKondisi,
				  CONCAT(gedungNama,IF(ruangNama IS NULL,'',CONCAT(' � ',ruangNama))) AS lokasiAset,
				  unitkerjaNama,
				  barangFoto
				FROM inventarisasi_mst mst
				  LEFT JOIN inventarisasi_detail invdet ON invDetMstId = invMstId
				  LEFT JOIN barang_ref ON invBarangId = barangId
				  LEFT JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
				  LEFT JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
				  LEFT JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
				  LEFT JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
				  LEFT JOIN gedung ON invGedungId = gedungId
				  LEFT JOIN ruang ON invRuanganId = ruangId
				  LEFT JOIN inv_kondisi_history ON invDetId = invKondisiBrgBrgDetId
				  LEFT JOIN unit_kerja_ref ON invUnitKerja = unitkerjaId
				  LEFT JOIN kondisi_barang_ref ON kondisibrgId = invDetKondisiId
				  LEFT JOIN `status_penguasaan_barang_ref` ON `statuspengbrgId` = invPenguasaanBarang
				  LEFT JOIN `kepemilikan_barang_ref` ON `kepemilikanbrgId` = invKepemilikan
				  LEFT JOIN satuan_barang_ref ON satuanbrgId = mst.invSatuanBarang
				  LEFT JOIN sumber_dana_ref ON sumberdanaId = mst.invSumberDana
				  LEFT JOIN kampus_ref ON kampusId = invKampus
				WHERE
					invdet.invDetId NOT IN(SELECT
                          lelangDetInvDetId
                        FROM aset_lelang_det
                        WHERE lelangDetInvDetId = invdet.invDetId) AND
					invDetId = '$id'
				ORDER BY
					invKondisiBrgTglKondisi DESC
				LIMIT 1
			";

			$result = mysql_query($sql);
			$i=-1;
			while($data = mysql_fetch_assoc($result)){
			$i++;
				$res[$i] = $data;
			}
			if (empty($res)) return false;

			$return = $res[0];

			// nambah data biaya lain
			$sql2="
				SELECT
					invDetBiayaBiayaId as biayaId,
					invDetBiayaNilai as biayaNilai
				FROM
					inv_det_biaya
				WHERE
					invDetBiayaInvId = '$return[invMstId]'
			";
			$result2 = mysql_query($sql2);
			$i=-1;
			while($data2 = mysql_fetch_assoc($result2)){
			$i++;
				$res2[$i] = $data2;
			}
			$return['biaya_lain'] = array();

			if($res2=="")$res2=array();
			foreach ($res2 as $val){
				$return['biaya_lain'][$val['biayaId']] = $val['biayaNilai'];

			}

			$return['invTglPembelian'] = date ('Y', strtotime($return['invTglPembelian']));
			mysql_free_result($result);
			mysql_free_result($result2);
			return $return;
		}

	}
	//-- end of class

	//-- start view
		$Obj = new AppInventarisasi;

      $return['list_biaya_lain'] = $Obj->GetListBiayaLain();
		$return['data'] = $Obj->GetInventarisasiDetailById($id);
		if(empty($return['data'])){
			echo "Data Tidak Ditemukan";
			exit;
		}
		$return['data']['link_foto'] = "document/barang/".$return['data']['barangFoto'];

		//------------------------------------------------------------------------------------------
		$exp	= explode(":", $return['data']['golbrgNama']);
		$index = $exp[0];

		$biayaLain = $return['data']['biaya_lain'];
      unset($return['data']['biaya_lain']);

		$return['data']['invNilaiPerolehanSatuan']=number_format($return['data']['invNilaiPerolehanSatuan'], 2, ',', '.');
		$return['data']['invNilaiFaktur']=number_format($return['data']['invNilaiFaktur'], 2, ',', '.');
		$return['data']['invTotalPerolehan']=number_format($return['data']['invTotalPerolehan'], 2, ',', '.');
		$return['data']['invNJOP']=number_format($return['data']['invNJOP'], 2, ',', '.');
		$return['data']['invNilaiResidu']=number_format($return['data']['invNilaiResidu'], 2, ',', '.');

			echo $return['data']['golbrgNama'],'|';
			echo $return['data']['invBarcode'],'|';
			echo $return['data']['invKodeAset'],'|';
			echo $return['data']['namaAset'],'|';
			echo $return['data']['invMstLabel'],'|';
			echo $return['data']['invMerek'],'|';
			echo $return['data']['invSpesifikasi'],'|';
         echo $return['data']['invTglPembelian'],'|';
			echo $return['data']['invSumberDana'],'|';
			echo $return['data']['invUmurEkonomis'],' bulan|';
			echo $return['data']['invJumlahAset'],' Unit|';
         echo $return['data']['invLuasTanah'],' m�|';
         echo $return['data']['invNilaiPerolehanSatuan'],'|';
         echo $return['data']['invNilaiFaktur'],'|';
			if (empty($return['list_biaya_lain']))
				echo "Data Tidak Ditemukan";
			else
			{
				foreach ($return['list_biaya_lain'] as $value)
				{
					echo $value['name'],' : ';
					$biaya = number_format($biayaLain[$value['id']], 2, ',', '.');
					echo $biaya,'|';
				}
			}

			echo $return['data']['invTotalPerolehan'].'|';
			echo $return['data']['invNilaiResidu'],'|';
         echo $return['data']['invNJOP'],'|';
         echo $return['data']['invLokasi'],'|';
         echo $return['data']['invKegunaan'],'|';

         //echo $return['data']['landasanLegal'].'landasanlegal|';
         echo $return['data']['invSertifikat'],'|';
         echo $return['data']['invAkta'],'|';
			echo $return['data']['invBPKB'],'|';
         echo $return['data']['invSTNK'],'|';
			// bukti kendaraan
			echo $return['data']['invNoMesin'],'|';
         echo $return['data']['invNoRangka'],'|';
         echo $return['data']['invNoPolisi'],'|';
         echo $return['data']['invNoLain'],'|';
         echo $return['data']['invWarna'],'|';
         echo $return['data']['invPagar'],'|';
         //echo $return['data']['batas'].'batas;';
         echo $return['data']['invBatasUtara'],'|';
         echo $return['data']['invBatasSelatan'],'|';
         echo $return['data']['invBatasTimur'],'|';
         echo $return['data']['invBatasBarat'],'|';
			echo $return['data']['invKepemilikan'],'|';
         echo $return['data']['invPenguasaanBarang'],'|';
			echo $return['data']['invKondisi'],'|';
         echo $return['data']['invKampus'],'|';
         echo $return['data']['lokasiAset'],'|';
         echo $return['data']['invKeteranganLain'],'|';
         echo $return['data']['unitkerjaNama'],'';
			mysql_close($connect);
	}else{
		echo "No database Found";
		exit;
	}
?>