<?php

class Pengelolaan extends Database {

	protected $mSqlFile= 'module/pengelolaan_bhp/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']; 
		if(empty($setting) || $setting == '') $setting = '';
		$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;
	}
	
	//---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 GetGdgByMstId($mstId){
   		return $this->Open($this->mSqlQueries['get_gdg_by_mst_id'], array($mstId));
   }
	
	function UpdateStokGedung($data,$rthNumber)
   {		
		foreach($data as $val){
			if($val['jumlah'] <= $val['stok']){
				$mstId = $this->GetMstByBrgId($val['mstId']);
				$arrGudang = $this->GetGdgByMstId($mstId);
				
				$sisaRth = $val['jumlah'];

				foreach($arrGudang as $value){
					if($sisaRth == 0) break;
					$jmlRth = ($value['jmlBrg']>=$sisaRth)?$sisaRth:$value['jmlBrg'];
					$sisaStokGdg = $value['jmlBrg']-$jmlRth;
					$sisaRth -= $jmlRth;
					$update = $this->UpdateGudangMax(array($sisaStokGdg,$value['ruangId'],$value['invBrgAtkId']));
					$log = $this->AddLogUpdate(array($value['ruangId'],$value['invBrgAtkId'],$jmlRth,$value['hrgSatuan'],$rthNumber,$val['barangDiskripsi']));
				    $result = $update&&$log;
				    if(!$result) break;
				}	
			}
		}
		return $result;
	}
	
}
?>