<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/penggunaan_bhp_maintenance/business/Pengelolaan.class.php';
class ProcessPengelolaan {

	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	//css hanya dipake di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;
	var $decId;
	var $encId;

	function __construct() 
	{	
		$this->Obj = new Pengelolaan();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html').'&grp='.Dispatcher::Instance()->Encrypt($this->_POST['idtrans']);
		$this->pageViewDel = Dispatcher::Instance()->GetUrl('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html').'&grp='.Dispatcher::Instance()->Decrypt($_GET['grp']);
		$this->pageInput = Dispatcher::Instance()->GetUrl('penggunaan_bhp_maintenance', 'inputPengelolaan', 'view', 'html');
		
		$this->lelangDate = $_POST['tanggalLelang_year'].'-'.$_POST['tanggalLelang_mon'].'-'.$_POST['tanggalLelang_day'];
				
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();	
	}

	function Check()
	{
		if (isset($_POST['btnsimpan'])) 
		{
			if(trim($this->_POST['penerima']) == "") 
			{
				return "empty";
			} 
				else 
			{
				$detil = $this->_POST['barangLelang'];
				foreach ($detil as $value)
				{
					if($value['jumlah'] > $value['stok']){
						$return[] = 1;
					}
				}
				if(count($return) != 0){
					return "over";
				} else {
					return true;
				}
			}
			return false;
		}
	}
	
	function GenerateNumber()
	{
		$gnumber = $this->Obj->GetAtkMstNomor();
		$explode = explode(".", $gnumber[0]['transAtkMstNomor']);
		$lenght 	= strlen(intval($explode[1]+1));
		$year		= date('Y');
		switch($lenght){
			case 1: $generate = $year.'.'.'00000'.($explode[1]+1); break;
			case 2: $generate = $year.'.'.'0000'.($explode[1]+1); break;
			case 3: $generate = $year.'.'.'000'.($explode[1]+1); break;
			case 4: $generate = $year.'.'.'00'.($explode[1]+1); break;
			case 5: $generate = $year.'.'.'0'.($explode[1]+1); break;
			case 6: $generate = $year.'.'.($explode[1]+1); break;
		}
		return $generate;
	}

	function Add() 
	{
		$cek = $this->Check();
		$gnumber = $this->GenerateNumber();

		if($cek === true ) 
		{
			$this->Obj->StartTrans();
			$detil = $this->_POST['barangLelang'];
			
			/*if(!empty($detil))
			{
				foreach ($detil as $value)
				{
					$totalBiaya += $value['total'];
				}			
			} 
				else 
			{ 
				$totalBiaya=0; 
			}*/

			$addPengelolaan = $this->Obj->DoAddPengelolaan($this->lelangDate, $gnumber, $this->_POST['catatan'], $this->_POST['penerima'], $this->_POST['unitkerjaId'], $this->idUser);

			if(!empty($detil))
			{
				foreach ($detil as $value)
				{
				//print_r($value);exit;
					$addPengelolaanDet = $this->Obj->DoAddPengelolaanDet($value['barangId'], $value['barangDiskripsi'], $value['jumlah'], $value['lelangBiaya']);
				}			
			}
		$result = $this->Obj->UpdateStokGedung($detil);
		
		$this->Obj->EndTrans($result);
		
			if ($result == true) 
			{
				Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST,'Penambahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} 
				else 
			{
				Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		}
			elseif($cek == "empty") 
		{
			Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'inputPengelolaan', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['idtrans']);
		} 
			elseif($cek == "over")
		{
			Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'inputPengelolaan', 'view', 'html', array($this->_POST,'Isian jumlah stok melebihi batas maksimal'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['idtrans']);;
		} 
		return $this->pageView;
	}

	function Update()
	{
		$cek = $this->Check();
		if($cek === true ) 
		{
			$this->Obj->StartTrans();	
			$detil=$this->_POST['barangLelang'];
		
			/* if(!empty($detil))
			{
				foreach ($detil as $value)
				{
					$totalBiaya += $value['lelangBiaya'];
				}			
			}
				else
			{
				$totalBiaya=0;
			} */
			
			$updatePengelolaan = $this->Obj->DoUpdatePengelolaan($this->lelangDate, $this->_POST['catatan'], $this->_POST['penerima'], $this->_POST['unitkerjaId'], $this->idUser, $this->_POST['idtrans']);
			$delete = $this->Obj->DoDeletePengelolaanDetil($this->_POST['idtrans']);
				
			if(!empty($detil))
			{
				foreach($detil as $value)
				{
					$updatePengelolaanDet = $this->Obj->DoUpdatePengelolaanDet($this->_POST['idtrans'], $value['barangId'], $value['barangDiskripsi'], $value['jumlah'], $value['lelangBiaya']);
				}			
			}
				
			$this->Obj->EndTrans($updatePengelolaanDet);
			
			if($updatePengelolaanDet === true) 
			{
				Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} 
				else 
			{
				Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST,'Gagal Mengubah Data', $this->cssFail),Messenger::NextRequest);
			}
		}
			elseif($cek == "empty") 
		{
			Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'inputPengelolaan', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['idtrans']);;
		} 
		return $this->pageView;	
	}
	
	function Delete()
	{
		$arrId = $this->_POST['idDelete'];
		$deleteDetil = $this->Obj->DoDeletePengelolaanDetil($arrId);
		$deleteArrData = $this->Obj->DoDeletePengelolaanByArrayId($arrId);
		if($deleteArrData === true) 
		{
			Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} 
			else 
		{
			//jika masuk disini, berarti PASTI ada salah satu atau lebih data yang gagal dihapus
			for($i=0;$i<sizeof($arrId);$i++)
			{
				$deleteData = false;
				$deleteData = $this->Obj->DoDeleteBgDepartemenById($arrId[$i]);
				if($deleteData === true)
				{
					$sukses += 1;
				}
					else 
				{
					$gagal += 1;
				}
			}
			Messenger::Instance()->Send('penggunaan_bhp_maintenance', 'pengelolaan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}
}
?>
