<?php

class AppGedung extends Database {

   protected $mSqlFile= 'module/gedung/business/gedung.sql.php';
   
   function __construct($connectionNumber=0) {
      parent::__construct($connectionNumber);
      #$this->setDebugOn();
   }
	
	public function GetDataGedungLastId(){      
      $result = $this->Open($this->mSqlQueries['get_data_gedung_last_insert'], array());
		return $result[0];
	}
	
	public function GetDataRuangLastId(){
      $result = $this->Open($this->mSqlQueries['get_data_ruang_last_insert'], array());
		return $result[0];
	}
   
   #combo
   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 GetCbxPeruntukanBangunan() {
      $result = $this->Open($this->mSqlQueries['get_peruntukan_bangunan'], array());
      return $result;
   }
   
   function GetCbxPenguasaanBarang() {
      $result = $this->Open($this->mSqlQueries['get_penguasaan_barang'], array());
      return $result;
   }
   
   #count & get data
   function GetCount(){
   		$result = $this->Open($this->mSqlQueries['get_count'],array());
   		return $result[0]['total'];
   }
   
   function GetCountOld($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi, $unitpj, $key) {
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$user1 = (int)$User['unitkerjaId']; 
		$user2 = (int)$User['unitkerjaId']; 
		$user3 = (int)$User['role_id'];
		$unitpj = (int)$unitpj['unitkerjaId'];
      $sql_add = '';
      $post = $_POST->AsArray();
      if(!empty($post['tahun'])) {
         if($tahun != 'all')
            $sql_add .= " AND left(a.gedungThnPerolehan, 4) = ".$tahun;
            
         if($sbr_dana != 'all')
            $sql_add .= " AND a.gedungSumberDanaId = '".$sbr_dana."'";
         
         if($kepemilikan != 'all')
            $sql_add .= " AND a.gedungKepemilikanBrgId = '".$kepemilikan."'";
            
         if($lokasi != 'all')
            $sql_add .= " AND a.gedungKampusId = '".$lokasi."'";
            
         if($kondisi != 'all')
            $sql_add .= " AND a.gedungKondisiBrgId = '".$kondisi."'";
				
			if($unitpj != '')
            $sql_add .= " AND a.gedungUnitkerjaId = '".$unitpj."'";
      }
      
      $sql = sprintf($this->mSqlQueries['get_count_gedung'], "%%$key%%","%%$key%%", $user1, $user2, $user3, $sql_add);
      #echo $sql;
      $result = $this->Open($sql, array($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi,$unitpj, $key));
      return $result[0]['total'];
   }
   
	function GetDataUserByUsername ($name)
   {
      if (isset($this->mUserByUserName[$name])) return $this->mUserByUserName[$name];
      $result = $this->Open($this->mSqlQueries['get_data_user_by_user_name'], array($name));  
      $this->mUserByUserName[$name] = $result[0];
      return $result[0];
   }
	
	/*function GetDataUnitByUnitName ($name)
   {
      if (isset($this->mUnitByUnitName[$name])) return $this->mUnitByUnitName[$name];
      $result = $this->Open($this->mSqlQueries['get_data_unit_by_unit_name'], array($name));
      $this->mUnitByUnitName[$name] = $result[0];
		
      return $result[0];
   }*/
	
   function GetDataGedungOld($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi, $unitpj, $key, $offset, $limit) {
      #echo 'post'.$tahun.$sbr_dana.$kepemilikan.$lokasi.$kondisi;
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$user1 = (int)$User['unitkerjaId']; 
		$user2 = (int)$User['unitkerjaId']; 
		$user3 = (int)$User['role_id'];
		$unitpj = (int)$unitpj['unitkerjaId'];
      $sql_add = '';
		$post = $_POST->AsArray();
      if(!empty($post['tahun'])) {
         if($tahun != 'all')
            $sql_add .= " AND left(a.gedungThnPerolehan, 4) = ".$tahun;
            
         if($sbr_dana != 'all')
            $sql_add .= " AND a.gedungSumberDanaId = '".$sbr_dana."'";
         
         if($kepemilikan != 'all')
            $sql_add .= " AND a.gedungKepemilikanBrgId = '".$kepemilikan."'";
            
         if($lokasi != 'all')
            $sql_add .= " AND a.gedungKampusId = '".$lokasi."'";
            
         if($kondisi != 'all')
            $sql_add .= " AND a.gedungKondisiBrgId = '".$kondisi."'";
				
			if($unitpj != '')
            $sql_add .= " AND a.gedungUnitkerjaId = '".$post['unitkerjaId']."'";
		}
      
      $sql = sprintf($this->mSqlQueries['get_data_gedung'], "%%$key%%","%%$key%%", $user1, $user2, $user3, $sql_add, $offset, $limit);
      #echo $sql;
      $result = $this->Open($sql, array($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi,$unitpj, $key, $offset, $limit));
      
      return $result;
   }
   
   # multiunit
   function GetDataGedung($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi, $unitpj, $key, $offset, $limit) {
   	$User = $this->GetDataUserByUsername($_SESSION['username']);
   	$kodeSistem = $User['unitkerjaKodeSistem'];
   	$sql_add = '';
   	if($tahun <> '') {
   		if($tahun != 'all')			$sql_add .= sprintf(" AND left(a.gedungThnPerolehan, 4) = '%s'",$tahun);
   		if($sbr_dana != 'all') 		$sql_add .= sprintf(" AND a.gedungSumberDanaId = '%s'",$sbr_dana);
   		if($kepemilikan != 'all')	$sql_add .= sprintf(" AND a.gedungKepemilikanBrgId = '%s'",$kepemilikan);
   		if($lokasi != 'all')		$sql_add .= sprintf(" AND a.gedungKampusId = '%s'",$lokasi); 
   		if($kondisi != 'all')		$sql_add .= sprintf(" AND a.gedungKondisiBrgId = '%s'",$kondisi);
   		if($unitpj != '') 			$sql_add .= sprintf(" AND a.gedungUnitkerjaId = '%s'", $unitpj);
   	}
   	 
   	$sql = $this->mSqlQueries['get_data_gedung'];
   	 
   	$sql_data_master = sprintf("
   	(SELECT g1.* FROM gedung g1
   	LEFT JOIN unit_kerja_ref u1 ON u1.unitkerjaId = g1.gedungUnitkerjaId
   	WHERE (u1.unitkerjaKodeSistem = '%s' OR u1.unitkerjaKodeSistem LIKE '%s.%%')
   	UNION
   	SELECT g2.* FROM gedung g2
   	LEFT JOIN ruang ON ruangGedungId = g2.gedungId
   	LEFT JOIN unit_kerja_ref u3 ON u3.unitkerjaId = g2.gedungUnitKerjaId
   	LEFT JOIN unit_kerja_ref u2 ON u2.unitkerjaId = ruangUnitId
   	WHERE 
   		(u3.unitkerjaKodeSistem != '%s' AND u3.unitkerjaKodeSistem NOT LIKE '%s.%%') 
   		AND (u2.unitkerjaKodeSistem = '%s' OR u2.unitkerjaKodeSistem LIKE '%s.%%'))",$kodeSistem,$kodeSistem,$kodeSistem,$kodeSistem,$kodeSistem,$kodeSistem); 
   
   	$sql = sprintf($sql, "%$key%","%$key%", $sql_add, $offset, $limit);
   	$sql = str_replace('[DATA_MASTER]', $sql_data_master, $sql);
   	
   	//$result = $this->Open($sql, array($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi,$unitpj, $key, $offset, $limit));
   	$result = $this->Open($sql,array());
   	
   	return $result;
   }

   function GetGedungById($id) {
      $result = $this->Open($this->mSqlQueries['get_gedung_by_id'], array($id, $id));
		
      return $result[0];
   }
   
   //do
   function DoAdd($arr_data) {    
   	
      $ret = $this->Execute($this->mSqlQueries['insert_gedung'], $arr_data);  
      return $ret;
   }  
   
   function DoUpdate($arr_data) {
   	
      $ret = $this->Execute($this->mSqlQueries['update_gedung'], $arr_data); 
      return $ret;
   }   
   
   function DoDelete($id) {
      $ret = $this->Execute($this->mSqlQueries['delete_gedung'], array($id));
      #$sql = sprintf($this->mSqlQueries['delete_gedung'], $arr_data);
      #echo $sql; exit; 
      return $ret;
   }
   
   #gedung dan biaya
   function GetBiaya() {
      $result = $this->Open($this->mSqlQueries['get_biaya_ref'], array());
      return $result;
   }
   
   function GetBiayaGedung($id_gedung) {
      $result = $this->Open($this->mSqlQueries['get_biaya_bedung'], array($id_gedung));
      return $result;
   }
   
   function DoAddBiayaGedung($biaya_id, $gedung_id, $nilai) {    
      $ret = $this->Execute($this->mSqlQueries['insert_biaya_gedung'], array($biaya_id, $gedung_id, $nilai));   
      return $ret;
   }  
   
   function DoUpdateBiayaGedung($nilai, $biaya_gedung_id) {
      $ret = $this->Execute($this->mSqlQueries['update_biaya_gedung'], array($nilai, $biaya_gedung_id));  
      #$sql = sprintf($this->mSqlQueries['update_gedung'], $arr_data);
      #echo $sql; exit;
      return $ret;
   }
   
   #gedung dan fasilitas
   function GetFasilitasGedungRef() {
      $result = $this->Open($this->mSqlQueries['get_fasilitas_gedung_ref'], array());
      return $result;
   }
   
   function GetFasilitasGedungList($id_gedung) {
      $result = $this->Open($this->mSqlQueries['get_fasilitas_gedung'], array($id_gedung, $id_gedung, $id_gedung));
      return $result;
   }
   
   function GetMaxIdGedung() {
      $res = $this->Open($this->mSqlQueries['get_max_id_gedung'], array());
      return $res[0]['id'];
   }
   
   //untuk print rtf ruang
   # multiunit
   function GetDataPrintGedung($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi, $key)
   {
		 if($tahun != 'all')
            $sql_add .= " AND left(a.gedungThnPerolehan, 4) = ".$tahun;
            
         if($sbr_dana != 'all')
            $sql_add .= " AND a.gedungSumberDanaId = '".$sbr_dana."'";
         
         if($kepemilikan != 'all')
            $sql_add .= " AND a.gedungKepemilikanBrgId = '".$kepemilikan."'";
            
         if($lokasi != 'all')
            $sql_add .= " AND a.gedungKampusId = '".$lokasi."'";
            
         if($kondisi != 'all')
            $sql_add .= " AND a.gedungKondisiBrgId = '".$kondisi."'";
         
         $kodeSistem = $this->GetDataUserByUsername($_SESSION['username']);
         $sql_add .= " AND (unitkerjaKodeSistem = '".$kodeSistem['unitkerjaKodeSistem']."'OR unitkerjaKodeSistem LIKE '".$kodeSistem['unitkerjaKodeSistem'].".%%')";
      
      $sql = sprintf($this->mSqlQueries['get_data_print_gedung'], "%%$key%%","%%$key%%", $sql_add);
      $result = $this->Open($sql, array($tahun, $sbr_dana, $kepemilikan, $lokasi, $kondisi, $key));

      return $result;
   }
   
   function DoAddFasilitasGedung($gedung_id, $fas_id, $jml) {    
      $ret = $this->Execute($this->mSqlQueries['insert_gedung_fasilitas'], array($gedung_id, $fas_id, $jml));   
      return $ret;
   }
   
   function DoDeleteFasilitasGedung($id) {
      $ret = $this->Execute($this->mSqlQueries['delete_gedung_fasilitas'], array($id));
      return $ret;
   }
   
   function CekGedung($kode) {
      $result = $this->Open($this->mSqlQueries['cek_gedung'], array($kode));
      return $result[0]['id_gedung'];
   }
   
   function GetPenanggungjawabGedung() {
      $result = $this->Open($this->mSqlQueries['get_penanggungjawab'], array());
      return $result[0]['nama'];
   }
   
   #insert penyusutan_brg_mst
   function DoAddPenyusutan($arr_data) {
      #$sql = sprintf($this->mSqlQueries['insert_penyusutan'], $arr_data[0],$arr_data[1],$arr_data[2],$arr_data[3],$arr_data[4],$arr_data[5]);
      #echo $sql; exit; 
      $ret = $this->Execute($this->mSqlQueries['insert_penyusutan'], $arr_data);   
      return $ret;
   } 
   
   function DoUpdatePenyusutan($arr_data) {
      $ret = $this->Execute($this->mSqlQueries['update_penyusutan'], $arr_data);  
      return $ret;
   }
   
   function GetDataPenyusutanGedung($gd_id) {
      $result = $this->Open($this->mSqlQueries['get_data_penyusutan_gedung'], array($gd_id,$gd_id));
      return $result[0];
   }
   
   function DoPenyesuaian($arr_data) {
      $ret = $this->Execute($this->mSqlQueries['penyesuaian_gedung'], $arr_data);  
      return $ret;
   } 
}
?>
