<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/sewa_aset/business/SewaAset.class.php';

class SewaAsetProc {

   var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;

	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";
	var $cssFalse = "notebox-alert";

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

   function __construct() {
		$this->Obj		= new SewaAset();
		$this->_POST 	= $_POST->AsArray();

		$this->decId 	= Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId 	= Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView		= Dispatcher::Instance()->GetUrl('sewa_aset', 'sewaAset', 'view', 'html').'&grp='.Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);
		$this->pageViewDel 	= Dispatcher::Instance()->GetUrl('sewa_aset', 'sewaAset', 'view', 'html').'&grp='.Dispatcher::Instance()->Decrypt($_GET['grp']);
		$this->pageInput 		= Dispatcher::Instance()->GetUrl('sewa_aset', 'inputSewaAset', 'view', 'html');

		$this->sewaTglAwal	= $_POST['tgl_awal_year'].'-'.$_POST['tgl_awal_mon'].'-'.$_POST['tgl_awal_day'];
		$this->sewaTglAkhir	= $_POST['tgl_akhir_year'].'-'.$_POST['tgl_akhir_mon'].'-'.$_POST['tgl_akhir_day'];
		$this->sewaWktAwal	= $_POST['jam_awal'].':'.$_POST['menit_awal'].':'.'00';
		$this->sewaWktAkhir	= $_POST['jam_akhir'].':'.$_POST['menit_akhir'].':'.'00';
		
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
	}

   public function CheckData($id="")
   {
		$id = Dispatcher::Instance()->Decrypt($_GET['id']);
		if (isset($_POST['save']))
		{
			if(/*trim($this->_POST['nomor_sewa']) == "" || */(trim($this->_POST['pengadaanMitraId']=="" && trim($this->_POST['pengadaanUnitkerjaId']==""))))
			{
				return "empty";
			}elseif(trim($this->_POST['pengadaanUnitkerjaId']!="" && trim($this->_POST['pengadaanMitraId']!="")))
			{
				return "pilih";
			}elseif(isset($this->_POST['asetSewaList'])){
				$dataAset = $this->_POST['asetSewaList'];
				$tglAwal = $this->sewaTglAwal.' '.$this->sewaWktAwal;
				$tglAkhir = $this->sewaTglAkhir.' '.$this->sewaWktAkhir;
				foreach($dataAset as $val){
					$dataX = $this->Obj->ValidationDate($val['asetId'],$tglAwal,$tglAkhir,$id);
					$checkId = $this->Obj->ValidationDateById($val['asetId'],$this->sewaTglAwal,$this->sewaWktAwal,$this->sewaTglAkhir,$this->sewaWktAkhir);
					if($id == "" || count($checkId) == ""){
						$checkData = $dataX;
					}

					if(isset($checkData)){
						foreach($checkData as $value){
							if($value['status_isi']==1){
								$arrReturn['status'] = 'ada';
								$arrReturn['message'] = 'Range tanggal sudah digunakan untuk aset '.$value['label'].'';
							}
						}
					}
				}
				if(isset($arrReturn)){
					return $arrReturn;
				} else {
					return true;
				}
			}
		}
			else 
		{
			return false;
		}
   }


	function GenerateNumber()
	{
		$gnumber = $this->Obj->GetGenerateNumber();
		$explode = explode(".", $gnumber[0]['sewaNoSewa']);
		$lenght 	= strlen(intval($explode[2]+1));
		$code		= "SW";
		$year		= $code.".".date('Y');
		switch($lenght){
			case 1: $generate = $year.'.'.'00000'.($explode[2]+1); break;
			case 2: $generate = $year.'.'.'0000'.($explode[2]+1); break;
			case 3: $generate = $year.'.'.'000'.($explode[2]+1); break;
			case 4: $generate = $year.'.'.'00'.($explode[2]+1); break;
			case 5: $generate = $year.'.'.'0'.($explode[2]+1); break;
			case 6: $generate = $year.'.'.($explode[2]+1); break;
		}
		return $generate;
	}

   public function Add()
   {
      $check = $this->CheckData();
		$this->_POST['nomor_sewa'] = $this->GenerateNumber();
      if($check === true)
		{
			$this->Obj->StartTrans();
			$asetSewaList = $this->_POST['asetSewaList'];
			$addSewaAset = $this->Obj->DoAddSewaAset($this->_POST['nomor_sewa'], $this->_POST['penyewa'], $this->_POST['kontak'], $this->_POST['alamat'], $this->_POST['keterangan'], $this->_POST['jumlah_hari'], $this->sewaTglAwal, $this->sewaWktAwal, $this->sewaTglAkhir, $this->sewaWktAkhir, $this->_POST['pengadaanUnitkerjaId'], $this->_POST['pengadaanMitraId'], $this->idUser);

			if(!empty($asetSewaList))
			{
				foreach ($asetSewaList as $value)
				{
					$addSewaAsetDet = $this->Obj->DoAddSewaAsetDet($value['asetId'], $value['asetHargaSewa'], $value['asetPotongan'], $value['statusId']);
				}
			}

			$this->Obj->EndTrans($addSewaAsetDet);
			if ($addSewaAsetDet === true)
			{
				Messenger::Instance()->Send('sewa_aset', 'sewaAset', 'view', 'html', array($this->_POST, 'Penambahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
				return $this->pageView;
			} 
				else 
			{
				Messenger::Instance()->Send('sewa_aset', 'sewaAset', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
				return $this->pageView;
			}
		}
			elseif($check === "empty")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih', $this->cssFalse),Messenger::NextRequest);
			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
			elseif($check === "pilih")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST,'Pilih salah satu, antara unit atau mitra', $this->cssFalse),Messenger::NextRequest);
			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
			elseif($check['status'] === "ada")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST, $check['message'], $this->cssFalse),Messenger::NextRequest);
			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
   }

	public function Delete()
	{
		$arrId = $this->_POST['idDelete'];

		$dataSewaById = $this->Obj->GetDataSewaAsetById($arrId);
		$deleteDetil = $this->Obj->DoDeleteSewaAsetDetil($arrId);
		$deleteArrData = $this->Obj->DoDeleteSewaAsetByArrayId($arrId);
		
		if($deleteArrData === true) {
			foreach($dataSewaById as $val){
				$this->Obj->DoUpdateInvDet('1', $val['statusId']);
			}
			Messenger::Instance()->Send('sewa_aset', 'sewaAset', '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('sewa_aset', 'sewaAset', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}

   public function Update()
   {
      $id = Dispatcher::Instance()->Decrypt($_GET['id']);
		$check = $this->CheckData($id);

      if($check === true)
		{
			$this->Obj->StartTrans();
			$asetSewaList = $this->_POST['asetSewaList'];
			$this->Obj->DoUpdateSewaAset($this->_POST['penyewa'], $this->_POST['kontak'], $this->_POST['alamat'], $this->_POST['keterangan'], $this->_POST['jumlah_hari'], $this->sewaTglAwal, $this->sewaWktAwal, $this->sewaTglAkhir, $this->sewaWktAkhir, $this->_POST['pengadaanUnitkerjaId'], $this->_POST['pengadaanMitraId'], $this->idUser, $this->_POST['sewa_id']);
			$sewaById = $this->Obj->GetDataSewaAsetById($this->_POST['sewa_id']);
			$delData = $this->Obj->DoDeleteSewaAsetDetil($this->_POST['sewa_id']);
			
			if($delData == ''){
				foreach($sewaById as $val){
					$this->Obj->DoUpdateInvDet('1', $val['statusId']);
				}
			}
			
			if(!empty($asetSewaList))
			{
				foreach ($asetSewaList as $value)
				{
					$updateSewaAsetDet = $this->Obj->DoUpdateSewaAsetDet($value['statusId'], $this->_POST['sewa_id'], $value['asetId'], $value['asetHargaSewa'], $value['asetPotongan']);
				}
			}

			$this->Obj->EndTrans($updateSewaAsetDet);

			if ($updateSewaAsetDet === true)
			{
				Messenger::Instance()->Send('sewa_aset', 'sewaAset', 'view', 'html', array($this->_POST, 'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			}
				else
			{
				Messenger::Instance()->Send('sewa_aset', 'sewaAset', 'view', 'html', array($this->_POST,'Gagal Merubah Data', $this->cssFail),Messenger::NextRequest);
			}
		}
			elseif($check === "empty")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih', $this->cssFalse),Messenger::NextRequest);

			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
			elseif($check === "pilih")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST,'Pilih salah satu, antara unit atau mitra', $this->cssFalse),Messenger::NextRequest);
			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
			elseif($check['status'] === "ada")
		{
			Messenger::Instance()->Send('sewa_aset', 'inputSewaAset', 'view', 'html', array($this->_POST, $check['message'], $this->cssFalse),Messenger::NextRequest);
			return $this->pageInput .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['sewa_id']);;
		}
		return $this->pageView;
   }
}
?>