<?php
class lapNeracaAset extends Database {
	
	protected $mSqlFile= 'module/lap_neraca_aset/business/lap_neraca_aset.sql.php';
	
	function __construct($connectionNumber=0) {
		parent::__construct($connectionNumber);
		#$this->setDebugOn();
   	}
   	
   	# multiunit
   	function getInventarisOldestDate(){
   		$kodeSistem = $this->getUserKodeSistem();
   		$result = $this->Open($this->mSqlQueries['get_inv_oldest_date'],array($kodeSistem,$kodeSistem.'.%'));
   		return $result[0]['oldest_date'];
   	}
   	
   	function getUserData(){
   		$userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
   		$result = $this->Open($this->mSqlQueries['get_user_data'],array($userId));
   		return $result[0];
   	}
   	
   	function getDataUnitKerjaById($unitId){
   		$result = $this->Open($this->mSqlQueries['get_data_unit_kerja_by_id'],array($unitId));
   		return $result[0];
   	}
   	
   	function getUserKodeSistem(){
   		$userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
   		$result = $this->Open($this->mSqlQueries['get_user_kodesistem'],array($userId));
   		return $result[0]['unitkerjaKodeSistem'];
   	}
   	
   	# multiunit
   	function getDataLaporan($tanggal,$unitId){
   		$kodeSistem = $this->getUserKodeSistem();
   		$sql = $this->mSqlQueries['get_data_laporan'];
   		$unit = $this->getDataUnitKerjaById($unitId);   
   		$result = $this->Open($sql,array($kodeSistem,$kodeSistem.'.%',$tanggal,$unit['unitkerjaKodeSistem'],$unit['unitkerjaKodeSistem'].'.%'));
   		return $result;
   	}
   	
   	# multiunit
   	function GetDataUnitkerja ($offset, $limit, $unitkerja='', $kode='') {
   		$kodeSistem = $this->getUserKodeSistem();
   		$result = $this->Open($this->mSqlQueries['get_data_unitkerja'], array('%'.$kode.'%', '%'.$kode.'%', '%'.$unitkerja.'%', '%'.$unitkerja.'%', $kodeSistem,$kodeSistem.".%", $offset, $limit));
   		return $result;
   	}
   	
   	function GetCount(){
   		$result = $this->Open($this->mSqlQueries['get_count'],array());
   		return $result[0]['total'];
   	}
   	
   	# ======= Invetarisasi Detail
   	# multiunit
   	function GetInventarisasi($key, $offset, $limit) {
   		$kodeSistemDefault = $this->getUserKodeSistem();
   		extract ($key);
   	
   		$sql = $this->mSqlQueries['get_inventarisasi'];
   		if($jenis_kib <> '' && $jenis_kib <> 'all') $sql = str_replace('[CARI_GOL]', 'invDetGolId = '.$jenis_kib.' AND ', $sql);
   		if($unit_pj_id <> '') $sql = str_replace('[CARI_UNIT]', '(unitkerjaKodeSistem = "'.$kodeSistemDefault.'" OR unitkerjaKodeSistem LIKE "'.$kodeSistemDefault.'.%%") AND (unitkerjaId = '.$unit_pj_id.' OR unitkerjaKodeSistem LIKE (SELECT CONCAT(b.unitkerjaKodeSistem,\'.%%\') FROM unit_kerja_ref b WHERE b.unitkerjaId = '.$unit_pj_id.')) AND', $sql);
   		if($tanggal <> '') $sql = str_replace('[CARI_THN]', "invTglPembelian < '$tanggal' AND ", $sql);
   		if($sbr_dana <> '' && $sbr_dana <> 'all') $sql = str_replace('[CARI_SUMBER]', "invSumberDana = $sbr_dana AND ", $sql);
   		else $sql = str_replace('[CARI_SUMBER]', '', $sql);
   		if($kepemilikan <> '' && $kepemilikan <> 'all') $sql = str_replace('[CARI_KEPEMILIKAN]', "invDetKepemilikan = $kepemilikan AND ", $sql);
   		else $sql = str_replace('[CARI_KEPEMILIKAN]', '', $sql);
   		if($lokasi <> '' && $lokasi <> 'all') $sql = str_replace('[CARI_KAMPUS]', "invDetKampus = $lokasi AND ", $sql);
   		else $sql = str_replace('[CARI_KAMPUS]', '', $sql);
   		if($kondisi <> '' && $kondisi <> 'all') $sql = str_replace('[CARI_KONDISI]', "invDetKondisiId = $kondisi AND ", $sql);
   		else $sql = str_replace('[CARI_KONDISI]', '', $sql);
   		if($status <> '' && $status <> 'all') $sql = str_replace('[CARI_STATUS]', "statusAsetId = $status AND ", $sql);
   		else $sql = str_replace('[CARI_STATUS]', '', $sql);
   	
   		$filter = array
   		(
   				"%$key%", "%$key%","%$key%",
   				$offset, $limit
   		);
   		$result = $this->Open($sql, $filter);
   		return $result;
   	}
   	
   	#combobox
	function GetCbxJenisKibAll() {
      $result = $this->Open($this->mSqlQueries['get_combo_jenis_kib'], array());
		foreach (array_keys($result) as $key)
      {
			if($result[$key]['kode'] != 'c' AND $result[$key]['kode'] != 'g') 
			$return[] = $result[$key];
		}
      return $return;
   }
   	
   	function GetCbxStatusAset() {
   		$result = $this->Open($this->mSqlQueries['get_combo_status_aset'], array());
   		return $result;
   	}
   	
   	function GetCbxSumberDana() {
   		$result = $this->Open($this->mSqlQueries['get_sumber_dana'], array());
   		return $result;
   	}
   	
   	function GetCbxKepemilikan() {
   		$result = $this->Open($this->mSqlQueries['get_kepemilikan'], array());
   		return $result;
   	}
   	
   	function GetCbxLokasiKampus() {
   		$result = $this->Open($this->mSqlQueries['get_lokasi_kampus'], array());
   		return $result;
   	}
   	
   	function GetCbxKondisi() {
   		$result = $this->Open($this->mSqlQueries['get_kondisi'], array());
   		return $result;
   	}
   	
   	function ConvertCombo($combo)
   	{
   		foreach ($combo as $value) $return[$value['id']] = $value['name'];
   		return $return;
   	}
}
?>