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

class BiayaSewaAsetProc {

   var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";
	var $cssFalse = "notebox-alert";

   function __construct() {
		$this->Obj		= new BiayaSewa;
		$this->_POST 	= $_POST->AsArray();
			
		$this->pageView		= Dispatcher::Instance()->GetUrl('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html').'&id='.Dispatcher::Instance()->Encrypt($this->_POST['biaya_sewa_id']);
		$this->pageViewDel 	= Dispatcher::Instance()->GetUrl('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html').'&id='.Dispatcher::Instance()->Decrypt($_GET['id']);
		$this->pageInput 		= Dispatcher::Instance()->GetUrl('biaya_sewa_aset', 'inputBiayaSewaAset', 'view', 'html');
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
	}

   public function CheckData()
   {
		if (isset($_POST['save'])) 
		{					
			if(trim($this->_POST['biaya_sewa_kode']) == "" || (trim($this->_POST['biaya_sewa_label_id']=="" || trim($this->_POST['biaya_sewa']) == 0)))
			{
				return "empty";
			} 			
				return true;
		}
			return false;
   }
	
   public function Add()
   {
      $check = $this->CheckData();
      if($check === true)
		{
			$addBiayaSewaAset = $this->Obj->DoAddBiayaSewaAset($this->_POST['biaya_sewa_label_id'], $this->_POST['biaya_sewa_kode'], $this->_POST['biaya_sewa'], $this->_POST['biaya_sewa_terlambat'], $this->_POST['biaya_sewa_rusak'], $this->_POST['biaya_sewa_denda'], $this->idUser);
			
			if ($addBiayaSewaAset === true) 
			{
				Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html', array($this->_POST, 'Penambahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} 
				else 
			{
				Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		}
			elseif($check === "empty") 
		{
			Messenger::Instance()->Send('biaya_sewa_aset', 'inputBiayaSewaAset', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih', $this->cssFalse),Messenger::NextRequest);

			return $this->pageInput .'&id='. Dispatcher::Instance()->Encrypt($this->_POST['biaya_sewa_id']);;
		}
		return $this->pageView;
   }

   public function Update()
   {
      $check = $this->CheckData();
		
      if($check === true)
		{
			$updateBiayaSewaAset = $this->Obj->DoUpdateBiayaSewaAset($this->_POST['biaya_sewa_label_id'], $this->_POST['biaya_sewa_kode'], $this->_POST['biaya_sewa'], $this->_POST['biaya_sewa_terlambat'],$this->_POST['biaya_sewa_rusak'], $this->_POST['biaya_sewa_denda'], $this->idUser, $this->_POST['biaya_sewa_id']);
			
			if ($updateBiayaSewaAset === true) 
			{
				Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html', array($this->_POST, 'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} 
				else 
			{
				Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html', array($this->_POST,'Gagal Mengubah Data', $this->cssFail),Messenger::NextRequest);
			}
		}
			elseif($check === "empty") 
		{
			Messenger::Instance()->Send('biaya_sewa_aset', 'inputBiayaSewaAset', 'view', 'html', array($this->_POST, 'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih', $this->cssFalse),Messenger::NextRequest);

			return $this->pageInput .'&id='.Dispatcher::Instance()->Encrypt($this->_POST['biaya_sewa_id']);;
		}
		return $this->pageView;
   }
	
	public function Delete() 
	{
		$arrId = $this->_POST['idDelete'];

		$deleteArrData = $this->Obj->DoDeleteBiayaSewaAsetByArrayId($arrId);
		if($deleteArrData === true) {
			Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', '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->DoDeleteBiayaSewaAsetById($arrId[$i]);
				if($deleteData === true) $sukses += 1;
				else $gagal += 1;
			}
			Messenger::Instance()->Send('biaya_sewa_aset', 'biayaSewaAset', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}


}
?>