<?php
/*
	@ClassName : Proc Mutasi Barang
	@Copyright : PT Gamatechno Indonesia
	@Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
	@Designed By : Rosyid <rosyid@gamatechno.com>
	@Author By : Dyan Galih <galih@gamatechno.com>
	@Version : 1.0
	@StartDate : Okt 20, 2008
	@LastUpdate : Okt 20, 2008
	@Description :
*/

require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/mutasi_barang/business/AppMutasi.class.php';

class ProcMutasiBarang
{
	var $viewPageJson;
	var $viewPage;
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";
	
	function __construct(){
		$_POST = $_POST->AsArray();
		$this->viewPageJson = Dispatcher::Instance()->GetUrl('mutasi_barang', 'mutasiBarang', 'view', 'html',true);
		$this->viewPage = Dispatcher::Instance()->GetUrl('mutasi_barang', 'mutasiBarang', 'view', 'html');
	}
	
	function DeleteMutasiBarang(){
		$obj =  new AppMutasiBarang();
		
		$result = $obj->DeleteMutasi($_POST['idDelete']);
		
		if ($result != 0){
         $msg = array('Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
      }else{ $msg = array('Proses Penghapusan Data Gagal!', $this->cssFail);}
      Messenger::Instance()->Send('mutasi_barang', 'MutasiBarang', 'view', 'html', $msg, Messenger::NextRequest);
		
		return $this->viewPage;
	}
	
	function CheckUploadedImage($image)
   {
      $fileName = strrev($_FILES[$image]['name']);
      $fileExtPos = strpos($fileName, '.');
      $fileExt = strtolower(strrev(substr($fileName, 0, $fileExtPos)));
      $_FILES[$image]['name']= strrev(substr($fileName, $fileExtPos + 1));
      $_FILES[$image]['ext']= $fileExt;
      if
      (
         $_FILES[$image]['error'] !== 0 ||
         strpos($_FILES[$image]['type'], 'image') !== 0 ||
         !in_array($fileExt, array('gif','jpg','jpeg','png','bmp')) AND
         $fileName !== ''
      )
         return false;
      else return true;
   }
	
	function UploadImage($image)
   {
      if (!$this->CheckUploadedImage($image)) return false;
      $fileName = $_FILES[$image]['name'];
      if ($fileName === '') return '';
      
      $fileExt = $_FILES[$image]['ext'];
      $fileName .= "-".dechex(mt_rand(65536,1048575)).".$fileExt";
      if (move_uploaded_file($_FILES[$image]['tmp_name'], GTFW_APP_DIR."/images/mutasi/$fileName")) // Note: dirname tempat upload ini dipake sama verifikasi_pengadaan/business/Verifikasi.class.php :: mImageGedungPath
         return $fileName;
      else return false;
   }
	
	function EditMutasiBarang($page='html'){
		
		if(!isset($_POST['batal'])){
			
			$unit = $_POST['unit_asal'];
			$lokasiBarang = $_POST['lokasi_barang'];
			$unitTujuan = $_POST['unit_tujuan'];
			$lokasiBarangTujuan = $_POST['lokasi_tujuan'];
			$tglMutasi = $_POST['tanggal_mutasi_year'].'-'.$_POST['tanggal_mutasi_mon'].'-'.$_POST['tanggal_mutasi_day'];
			$pic = $_POST['pic'];
			$bamutasi = $_POST['bamutasi'];
			$keterangan = $_POST['keterangan'];
			$itemList = $_POST['data'];
			$barangId = $_POST['barang_id'];
			$mutasiId = $_POST['mutasiId'];
			$file = $this->UploadImage('uploadFile');
         if ($file == false) $file = $_POST['oldFIle'];
			
			$userName = $_SESSION['username'];
			$arrMutasi = array(
				$unit,
				$lokasiBarang,
				$unitTujuan,
				$lokasiBarangTujuan,
				$tglMutasi,
				$pic,
				$bamutasi,
				$file,
				$keterangan,
				$userName,
				$mutasiId
			);
			
			$obj =  new AppMutasiBarang();
			
			$return = $obj->EditMutasi($arrMutasi,$itemList);
			
			if($return){
				$msg='update mutasi barang berhasil';
				$css = 'notebox-done';
			}else{
				$msg='update mutasi barang gagal';
				$css = 'notebox-warning';
			}
			
			$msg = array($msg,$css);
			Messenger::Instance()->Send('mutasi_barang', 'mutasiBarang', 'view', 'html', $msg, Messenger::NextRequest);
			
		}
		if($page=='json')
			return $this->viewPageJson;
		else
			return $this->viewPage;
	}
	
	function AddMutasiBarang($page='html'){
	if(!isset($_POST['batal'])){
			$unit = $_POST['unit_asal'];
			$lokasiBarang = $_POST['lokasi_barang'];
			$unitTujuan = $_POST['unit_tujuan'];
			$lokasiBarangTujuan = $_POST['lokasi_tujuan'];
			$tglMutasi = $_POST['tanggal_mutasi_year'].'-'.$_POST['tanggal_mutasi_mon'].'-'.$_POST['tanggal_mutasi_day'];
			$pic = $_POST['pic'];
			$bamutasi = $_POST['bamutasi'];
			$keterangan = $_POST['keterangan'];
			$itemList = $_POST['data'];
			$barangId = $_POST['barang_id'];
			
			$file = $this->UploadImage('uploadFile');
         if ($file == false) $file = $_POST['oldFIle'];
			
			$userName = $_SESSION['username'];
			$arrMutasi = array(
				$unit,
				$lokasiBarang,
				$unitTujuan,
				$lokasiBarangTujuan,
				$tglMutasi,
				$pic,
				$bamutasi,
				$file,
				$keterangan,
				$userName
			);
			$obj =  new AppMutasiBarang();
			
			$return = $obj->AddMutasi($arrMutasi,$itemList);
			
			if($return){
				$msg='mutasi barang berhasil';
				$css = 'notebox-done';
			}else{
				$msg='mutasi barang gagal';
				$css = 'notebox-warning';
			}
			
			$msg = array($msg,$css);
			Messenger::Instance()->Send('mutasi_barang', 'mutasiBarang', 'view', 'html', $msg, Messenger::NextRequest);
		}
		if($page=='json')
			return $this->viewPageJson;
		else
			return $this->viewPage;
	}
}
?>
