<?php

class AppLaporanInventaris extends Database {

   protected $mSqlFile= 'module/laporan_inventaris/business/laporan_inventaris.sql.php';
   
   function __construct($connectionNumber=0) {
      parent::__construct($connectionNumber);
      #$this->setDebugOn();
   }
   
	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];
   }
	
    # multiunit
	function GetComboUnit(){
		$User = $this->GetDataUserByUsername($_SESSION['username']);		
		$array = array(
			$User['unitkerjaKodeSistem'],
			$User['unitkerjaKodeSistem'].".%"
		);
		return $this->Open($this->mSqlQueries['get_combo_unit'], $array);		

	
	}
	
	function GetComboPic($id){
		return $this->Open($this->mSqlQueries['get_combo_pic'],array($id));
	}
	
	function GetPenguasaanBarang(){
		return $this->Open($this->mSqlQueries['get_penguasaan_barang'],array());
	}
	
	function GetKondisiBarang(){
		return $this->Open($this->mSqlQueries['get_kondisi_barang'],array());
	}
	
	# multiunit
	function GetDataInventaris($unitId,$barangId,$tahun){
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		/*$user1 = $User['unitkerjaId']; 
		$user2 = $User['unitkerjaId']; 
		$user3 = $User['role_id'];*/
	   $sql = $this->Open($this->mSqlQueries['get_dyn_query'],array());
		
		if($barangId=='')
		   $barangId='all';
		
		$newSql = "";
		
		for($i=0;$i<count($sql);$i++){
		   $newSql .= $sql[$i]['DYN_QUERY'];
      }
      
      $newSql .= $this->mSqlQueries['get_dyn_query_additional'];
      
      if($unitId <> '' && $unitId <> 'all') $newSql = str_replace('[SEARCH_UNIT]', 'invDetUnitKerja = '.$unitId.' AND ', $newSql);
      else $newSql = str_replace('[SEARCH_UNIT]', '', $newSql);
      if($tahun <> '' && $tahun <> 'all') $newSql = str_replace('[SEARCH_TAHUN]', 'AND YEAR(a.invTglPembelian) = "'.$tahun.'"', $newSql);
      else $newSql = str_replace('[SEARCH_TAHUN]', '', $newSql);
		
		
	   return $this->Open($newSql,array($barangId,$barangId,$User['unitkerjaKodeSistem'],$User['unitkerjaKodeSistem'].".%" /*$user1,$user2, $user3*/));
		// echo $this->GetLastError();
	}
	
	# multiunit
	function GetDataUnitkerja ($offset, $limit, $unitkerja='', $kode='') {
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$kodeSistem = $User['unitkerjaKodeSistem'];
		$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'];
	}
}
?>
