<?php

class Pengelolaan extends Database {

	protected $mSqlFile= 'module/rth_bhp_usulan/business/pengelolaan.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 GetDataPengelolaan ($offset, $limit, $notrans, $nama, $setting='') {
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$kodeSistem = $User['unitkerjaKodeSistem']; 
		$result = $this->Open($this->mSqlQueries['get_data_lelang_barang'], array('%'.$notrans.'%', '%'.$nama.'%', $setting, $kodeSistem,$kodeSistem.'.%',$offset, $limit));		
		return $result;
	}
	
	function GetCount(){
		$result = $this->Open($this->mSqlQueries['get_count'],array());
		return $result[0]['total'];
	}
	
	function GetCountDataPengelolaan($notrans, $nama, $setting='') {
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$user1 = $User['unitkerjaId']; 
		$user2 = $User['unitkerjaId']; 
		$user3 = $User['role_id'];
		$result = $this->Open($this->mSqlQueries['get_count_data_lelang_barang'], array('%'.$notrans.'%', '%'.$nama.'%', $setting, $setting, $user1, $user2, $user3));
		
		if (!$result) 
		{
			return 0;
		} 
			else 
		{
			return $result[0]['total'];
		}
	}
	
	function GetDataPengelolaanById ($id) {
		$result = $this->Open($this->mSqlQueries['get_data_pengelolaan_by_id'], array($id));
		return $result;
	}
	
	function GetAtkMstNomor() {
		$result = $this->Open($this->mSqlQueries['get_atk_mst_nomor'], array());
		return $result;
	}
	
	# FOR POPUP USULAN BHP
	
	# multiunit
	function GetDataUsulanBhp($startRec, $itemViewed, $unitKerja, $noUsulanBhp, $namaBarang){
		$kodeSistem = $this->GetDataUserByUsername($_SESSION['username']);
		$sql = $this->mSqlQueries['get_data_usulan_bhp'];
		$sql = ($unitKerja <> '' && $unitKerja <> 'all') ? str_replace('[SEARCH_UNIT]', ' AND usulanBrgUnitId = '.$unitKerja, $sql) : str_replace('[SEARCH_UNIT]', '', $sql);
		$result = $this->Open($sql,array('%'.$noUsulanBhp.'%','%'.$namaBarang.'%',$kodeSistem['unitkerjaKodeSistem'],$kodeSistem['unitkerjaKodeSistem'].'.%',$startRec, $itemViewed));
		return $result;
	}
	
	# multiunit
	function GetComboUnitKerja(){
		$kodeSistem = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_combo_unit_kerja'],array($kodeSistem['unitkerjaKodeSistem'],$kodeSistem['unitkerjaKodeSistem'].'.%'));
		return $result;
	}
	
	//---DO--
	function DoAddPengelolaan($tanggal, $gnumber, $deskripsi, $nama, $unitId, $userId)
	{
		$result = $this->Execute($this->mSqlQueries['do_add_lelang_barang'], array($tanggal, $gnumber, $deskripsi, $nama, $userId, $unitId));
		return $result;
	}
	
	function DoAddPengelolaanDet($barangId, $diskripsi, $jumlah, $amount) {
		$result = $this->Execute($this->mSqlQueries['do_add_pengelolaan_det'], array($barangId, $diskripsi, $jumlah, $amount));
		return $result;
	}
	
	function DoUpdatePengelolaan($tanggal, $catatan, $penerima, $unitId, $iduser, $idtrans)
	{
		$result = $this->Execute($this->mSqlQueries['do_update_lelang_barang'], array($tanggal, $catatan, $penerima, $unitId, $iduser, $idtrans));
		return $result;
	}
	
	function DoUpdatePengelolaanDet($idtrans, $barangId, $diskripsi, $jumlah, $amount) {
		$result = $this->Execute($this->mSqlQueries['do_update_pengelolaan_det'], array($idtrans, $barangId, $diskripsi, $jumlah, $amount));
		return $result;
	}
	
	function DoDeletePengelolaanDetil($idTrans){
		$result = $this->Execute($this->mSqlQueries['do_delete_pengelolaan_det'], array($idTrans));
	}
	
	function DoDeletePengelolaanByArrayId($arrTransid) {
		$transId = implode("', '", $arrTransid);
		$result=$this->Execute($this->mSqlQueries['do_delete_pengelolaan_by_array_id'], array($transId));
		return $result;
	}
	
	//-- update stok di gedung
	function ab($nilai,$data,$i,$akhir,$habis){
		if ($i < $akhir){
			if ($habis==1){
				$sisa = $data[$i];
			}	else	{
				$sisa = $nilai-$data[$i];	
				$sisa2 = $sisa;
				if($sisa<0){
					$sisa = $sisa*(-1);
					$habis = 1;
				} elseif($sisa){
					$sisa=0;
				}
			}
			
			$x = $i+1;
			
			$this->ab($sisa2,$data,$x,$akhir,$habis);
			$_SESSION['hasil'][] = $sisa;
		}
	}
	
	function GetMstByBrgId($barangId)
   {
      $result = $this->Open($this->mSqlQueries['get_mst_id'], array($barangId));
      return $result['0']['invAtkMstId'];
   }
	
	function GetBrgByMstId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_brg_by_mst_id'], array($id));
      return $result;
   }
	
	function GetMaxGdgByDetId($detId, $ruangId)
   {
      $result = $this->Open($this->mSqlQueries['get_max_gdg_by_det_id'], array($detId, $ruangId));
		return $result[0]['jumlah'];
   }
	
	function UpdateGudangMax($data)
   {
      $result = $this->Execute($this->mSqlQueries['update_gudang_max'], $data);
      return $this->AffectedRows();
   }
	
	function AddLogUpdate($data){
      $result = $this->Execute($this->mSqlQueries['add_log_update'], $data);
      return $this->AffectedRows();
   }
	
	function UpdateStokGedung($data,$nousulanbhp)
   {		
		foreach($data as $val){
			if($val['jumlah'] <= $val['stok']){
				$mstId = $this->GetMstByBrgId($val['mstId']);
				$detId = $this->GetBrgByMstId($mstId);
				$keymax = array_keys($detId, max($detId));
				$detIdMax = $detId[$keymax[0]]['invBrgAtkId'];
				
				foreach($detId as $value){
					$jumlah[] = (int)$this->GetMaxGdgByDetId($value['invBrgAtkId'], $val['ruangId']);
				}
				
				$x = 0;
				$habis = 2;
				$akhir = sizeof($jumlah);
				$this->ab($val['jumlah'], $jumlah, $x, $akhir, $habis);
				$resultx = array_reverse($_SESSION['hasil']);
				
				foreach($detId as $values){
					$dataDet = array($resultx[$x], $val['ruangId'], $values['invBrgAtkId']);
					$result = $this->UpdateGudangMax($dataDet);
					$x++;
				}
				
				$val['barangDiskripsi'] = ($val['barangDiskripsi'] == '') ? $nousulanbhp : $nousulanbhp.' - '.$val['barangDiskripsi'];
				
				$dataDet = array($val['ruangId'], $detIdMax, $val['jumlah'], $val['barangDiskripsi']);
				$result = $this->AddLogUpdate($dataDet);
			}
			unset($jumlah);
			unset($_SESSION['hasil']);
			//session_unregister('hasil');
		}
		return $result;
	}
	
	function DoUpdateStokUsulanBhp($jumlah,$usulanId){
		$result = $this->Execute($this->mSqlQueries['do_update_stok_usulan_bhp'], array($jumlah,$usulanId));
		return $this->AffectedRows();
	}
	
}
?>