<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/sumber_dana/business/AppSumberDana.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/sumber_dana/business/AppSumberDanaToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/sumber_dana/business/AppSumberDanaToUm.class.php';
class ProcessSumberDana {

	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 AppSumberDana();
		$this->siaObj = new AppSumberDanaToSia();
		$this->umObj = new AppSumberDanaToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('sumber_dana', 'sumberDana', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('sumber_dana', 'inputSumberDana', 'view', 'html');
		$this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['sumber_dana_nama']) == "") {
				return "empty";
			} else {
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         $dataIdSumberDana = $this->Obj->GetIdSumberDana();
         $id = $dataIdSumberDana[0]['id'];
			$addSumberDana = $this->Obj->DoAddSumberDana($id+1,strtoupper($this->_POST['sumber_dana_nama']));
			

            

			$addSumberDanaToSia = $this->siaObj->DoAddSumberDanaToSia($id+1, strtoupper($this->_POST['sumber_dana_nama']));
			$addData = $addSumberDana && $dataIdSumberDana && $addSumberDanaToSia;
			if($this->useUm==true){
            $this->umObj->StartTrans();
            $addSumberDanaToUm=$this->umObj->DoAddSumberDanaToUm($id+1, strtoupper($this->_POST['sumber_dana_nama']));
            $addData = $addData && $addSumberDanaToUm;
            $this->umObj->EndTrans($addData);  
         }	
         $this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('sumber_dana', 'inputSumberDana', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView;
	}

	function Update() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$updateSumberDana = $this->Obj->DoUpdateSumberDana(strtoupper($this->_POST['sumber_dana_nama']), $this->decId);
			$updateSumberDanaSia = $this->siaObj->DoUpdateSumberDanaSia(strtoupper($this->_POST['sumber_dana_nama']), $this->decId);
			$updateData = $updateSumberDana && $updateSumberDanaSia;
			if($this->useUm==true){
            $this->umObj->StartTrans();
            $updateSumberDanaUm=$this->umObj->DoUpdateSumberDanaSia(strtoupper($this->_POST['sumber_dana_nama']), $this->decId);
            $updateData = $updateData && $updateSumberDanaUm;
            $this->umObj->EndTrans($updateData);  
         }	
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
			if ($updateData === true) {
				Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('sumber_dana', 'inputSumberDana', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
      if(!$this->siaObj->addConn){
         Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif(!$this->umObj->addConn && $this->useUm==true){
         Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Admisi', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
   		$arrId = $this->_POST['idDelete'];
   		$this->Obj->StartTrans();
   		$this->siaObj->StartTrans();
   		
   		$deleteSumberDanaById = $this->Obj->DoDeleteSumberDanaById($arrId);
   		$deleteSumberDanaSiaById = $this->siaObj->DoDeleteSumberDanaSiaById($arrId);
   		$deleteDataById = $deleteSumberDanaById && $deleteSumberDanaSiaById;
			if($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteSumberDanaUmById=$this->umObj->DoDeleteSumberDanaUmById($arrId);
            $deleteDataById = $deleteDataById && $deleteSumberDanaUmById;
            $this->umObj->EndTrans($deleteDataById);  
         }	
   		$this->Obj->EndTrans($deleteDataById);
   		$this->siaObj->EndTrans($deleteDataById);
   		
   		if($deleteDataById==false) {
            if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->siaObj->StartTrans();
            
            $deleteArrSumberDana = $this->Obj->DoDeleteSumberDanaByArrayId($arr);
            $deleteArrSumberDanaSia = $this->siaObj->DoDeleteSumberDanaSiaByArrayId($arr);
            $deleteArrData = $deleteArrSumberDana && $deleteArrSumberDanaSia;
   			if($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrSumberDanaUm=$this->umObj->DoDeleteSumberDanaUmByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrSumberDanaUm;
               $this->umObj->EndTrans($deleteArrData);  
            }	
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
   		}
         if($deleteArrData === true) {
   			Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('sumber_dana', 'sumberDana', '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($arr);$i++) {
   			   $this->Obj->StartTrans();
   			   $this->siaObj->StartTrans();
   			   
   				$deleteData = false;
   				$deleteSumberDana = $this->Obj->DoDeleteSumberDanaById($arr[$i]);
   				$deleteSumberDanaSia = $this->siaObj->DoDeleteSumberDanaSiaById($arr[$i]);
   				$deleteData = $deleteSumberDana && $deleteSumberDanaSia;
      			if($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteSumberDanaUm=$this->umObj->DoDeleteSumberDanaUmById($arr[$i]);
                  $deleteData = $deleteData && $deleteSumberDanaUm;
                  $this->umObj->EndTrans($deleteData);  
               }	
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('sumber_dana', 'sumberDana', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
