<?php

class AppMutasiBarang extends Database {

   protected $mSqlFile= 'module/mutasi_barang/business/mutasi.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_username'], array($name));
   	$this->mUserByUserName[$name] = $result[0];
   	return $result[0];
   }
   
   function GetMutasiDetail($id){
   	$arrResult = $this->Open($this->mSqlQueries['get_detail_mutasi'],array($id));
   	
   	return $arrResult;
	}
   
	# multiunit
   function GetListMutasi($golBarang,$tglAwal,$tglAkhir,$kdBrg,$nmBrg,$unitTuj,$lokTuj){
   	
   	$user = $this->GetDataUserByUsername($_SESSION['username']);
   	$sql = $this->mSqlQueries['get_list_mutasi'];
   	if($golBarang <> 'all' && $golBarang <> '') $sql = str_replace('[SEARCH_GOLBRG]', "AND b.golId LIKE '%%|$golBarang|%%", $sql);
   	else $sql = str_replace('[SEARCH_GOLBRG]', "", $sql);
   	if($kdBrg <> '')  $sql = str_replace('[SEARCH_KODEBRG_NAMABRG]', " AND (b.kodeBarang LIKE '%%$kdBrg%%' OR b.namaBarang LIKE '%%$nmBrg%%')", $sql);
   	else $sql = str_replace('[SEARCH_KODEBRG_NAMABRG]', "", $sql);
   	if($unitTuj <> 'all' && $unitTuj <> '')  $sql = str_replace('[SEARCH_UNITTUJUAN]', " AND mtsAsetMstUnitIdTuj = $unitTuj", $sql);
   	else $sql = str_replace('[SEARCH_UNITTUJUAN]', "", $sql);	
   	if($lokTuj <> 'all' && $lokTuj <> '') $sql = str_replace('[SEARCH_LOKASI]', " AND mtsAsetMstLokasiIdTuj = $lokTuj", $sql);
   	else $sql = str_replace('[SEARCH_LOKASI]', "", $sql);	
   	
	return $this->Open($sql,array($user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].'.%',$tglAwal,$tglAkhir));
   }
   
   #combo
   function GetJenisKIB() {
      return $this->Open($this->mSqlQueries['get_combo_jenis_kib'], array());
   }
   
   function GetLokasi() {
      return $this->Open($this->mSqlQueries['get_combo_lokasi'], array());
   }
   
   # multiunit
   function GetUnitKerja() {
      $user = $this->GetDataUserByUsername($_SESSION['username']);
      return $this->Open($this->mSqlQueries['get_combo_unit'], array($user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].".%"));
   }
   
   function GetUnitKerjaTujuan($id) {
      return $this->Open($this->mSqlQueries['get_combo_unit_tujuan'], array($id));
   }
   
   function GetLokasiBarang($idUnit,$comboTujuan=false){
   		$sql = $this->mSqlQueries['get_lokasi_barang'];
   		$vars = array($idUnit);
   		if($comboTujuan) $sql = str_replace('[UNION]', '', $sql);
   		else{
   			$union = "
   			UNION
   			SELECT
   				ruangId AS id,
   				ruangNama AS name
   			FROM inventarisasi_detail
   			LEFT JOIN ruang ON ruangId = invDetRuanganId
   			WHERE invDetUnitKerja = '%s' AND ruangUnitId != '%s'";
   			$sql = str_replace('[UNION]', $union, $sql);
   			array_push($vars, $idUnit,$idUnit);
   		}
		$result = $this->Open($sql,$vars);
		return $result;
	}
	
	function GetBarangLokasi($id){
		return $this->Open($this->mSqlQueries['barang_lokasi'],array($id));
	}
	
	function GetDataGedung($idRuang){
		return $this->Open($this->mSqlQueries['get_data_gedung'],array($idRuang));
	}
	
	function GetHistoryLokasiBarangId($barangId,$gedungId,$ruangId){
		$result = $this->Open($this->mSqlQueries['get_history_lokasi_barang_id'],array($barangId,$barangId,$gedungId,$ruangId));
		return $result[0];
	}
	
	//do	
	function AddMutasi($arrMutasiMst, $arrMutasiDet){
		//start transaction
		$result = true;
		$arrMutasiDet = array_values($arrMutasiDet);
		$gedungId = $this->GetDataGedung($arrMutasiMst[3]);
		$userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
		if($gedungId[0]==''){
			$gedungId = null;
		} else {
			$gedungId = $gedungId[0];
		}
	
		$this->StartTrans();
		
 		$arrResult = $this->Execute($this->mSqlQueries['add_mutasi_master'],$arrMutasiMst);
		if(!$arrResult)
			$result=false;

		for($i=0;$i<count($arrMutasiDet);$i++){
			$arrResult = $this->Execute($this->mSqlQueries['add_mutasi_detail'],array($arrMutasiDet[$i]));
			if(!$arrResult)
				$result=false;
		}		

		for($i=0;$i<count($arrMutasiDet);$i++){
			$arrResult = $this->Execute($this->mSqlQueries['update_data_inventarisasi'],array($gedungId, $arrMutasiMst[3], $arrMutasiMst[2], $arrMutasiDet[$i]));

			$arrResultHistory = $this->Execute($this->mSqlQueries['add_inventarisasi_lokasi_history'],array($arrMutasiDet[$i], $gedungId, $arrMutasiDet[$i], $arrMutasiMst[3], $userId));

			if(!$arrResult or !$arrResultHistory)
				$result=false;
		}
		
		if(!$result)
			$this->EndTrans(false);
		else
			$this->EndTrans(true);
		
		return $result;
	}
	
	function EditMutasi($arrMutasiMst, $arrMutasiDet){
		//start transaction
		
		$result = true;
		$arrMutasiDet = array_values($arrMutasiDet);
		$gedungId = $this->GetDataGedung($arrMutasiMst[3]);
		$userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
		if($gedungId[0]==''){
			$gedungId = 'null';
		} else {
			$gedungId = $gedungId[0];
		}
		
		$this->StartTrans();
		
		$arrResult = $this->Execute($this->mSqlQueries['update_mutasi'],$arrMutasiMst);
		if(!$arrResult)
			$result=false;

		$arrResult = $this->Execute($this->mSqlQueries['delete_mutasi_detail'],array($arrMutasiMst['10']));
		
		if(!$arrResult)
			$result=false;

		for($i=0;$i<count($arrMutasiDet);$i++){
			$arrResult = $this->Execute($this->mSqlQueries['add_mutasi_detail_update'],array($arrMutasiMst['10'],$arrMutasiDet[$i]));
			if(!$arrResult)
				$result=false;
		}
		
		for($i=0;$i<count($arrMutasiDet);$i++){
			$arrResult = $this->Execute($this->mSqlQueries['update_data_inventarisasi'],array($gedungId, $arrMutasiMst[3], $arrMutasiMst[2], $arrMutasiDet[$i]));

			$arrResultHistory = $this->Execute($this->mSqlQueries['add_inventarisasi_lokasi_history'],array($arrMutasiDet[$i], $gedungId, $arrMutasiDet[$i], $arrMutasiMst[3], $userId));

			if(!$arrResult or !$arrResultHistory)
				$result=false;
		}
		
		if(!$result)
			$this->EndTrans(false);
		else
			$this->EndTrans(true);
		
		return $result;
	}
	
	function DeleteMutasi($id){
		$this->StartTrans();
		
		$listBrgMutasi = $this->GetMutasiDetail($id);
		$arrGedung = $this->GetDataGedung($listBrgMutasi[0]['lokasi_asal_id']);
		$gedungId = ($arrGedung[0]=='') ? 'null' : $arrGedung[0];
		$rollback = true;
		foreach ($listBrgMutasi as $brg){
			# rollback ke posisi terakhir sebelum BA dan hapus history lokasi setelah BA
			$lokasiHistId = $this->GetHistoryLokasiBarangId($brg['barangId'],$gedungId,$brg['lokasi_asal_id']);
			$updateInvDet = $this->Execute($this->mSqlQueries['update_data_inventarisasi'],array($gedungId, $brg['lokasi_asal_id'], $brg['unit_asal_id'], $brg['barangId']));
			$deleteNewHistory = $this->Execute($this->mSqlQueries['delete_new_history'],array($brg['barangId'],$lokasiHistId));
			if(!$updateInvDet || !$deleteNewHistory){
				$rollback = false;
				break;
			} 
		}
		
		if($rollback) $result = $this->Execute($this->mSqlQueries['delete_mutasi'],array($id));
		if(!$result) $this->EndTrans(false);
		else $this->EndTrans(true);
		
		return $result;
	}
}
?>
