<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/hubungan_biaya/business/AppHubunganBiaya.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/hubungan_biaya/business/AppHubunganBiayaToSia.class.php';
class ProcessHubunganBiaya {

	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 AppHubunganBiaya();
		$this->ObjToSia = new AppHubunganBiayaToSia();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('hubungan_biaya', 'hubunganBiaya', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('hubungan_biaya', 'inputHubunganBiaya', 'view', 'html');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $lastHubunganBiaya= $this->Obj->GetLastIdHubunganBiaya();
         if(empty($lastHubunganBiaya[0]['hubbiayaId']))
            $lastHubunganBiaya[0]['hubbiayaId']=0;
            
         $newKode=($lastHubunganBiaya[0]['hubbiayaId']) + 1;

			$addHubunganBiaya = $this->Obj->DoAddHubunganBiaya($newKode,ucwords($this->_POST['hubunganBiaya']));
			
         $addHubunganBiayaToSia = $this->ObjToSia->DoAddHubunganBiaya($newKode,ucwords($this->_POST['hubunganBiaya']));
			
         $addData=$addHubunganBiaya && $addHubunganBiayaToSia;
         
         $this->Obj->EndTrans($addData);
         $this->ObjToSia->EndTrans($addData);
         
         if ($addData  === true) {
				Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('hubungan_biaya', 'inputHubunganBiaya', '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->ObjToSia->StartTrans();
         
			$updateHubunganBiaya = $this->Obj->DoUpdateHubunganBiaya(ucwords($this->_POST['hubunganBiaya']), $this->decId);
			
         $updateHubunganBiayaToSia = $this->ObjToSia->DoUpdateHubunganBiaya(ucwords($this->_POST['hubunganBiaya']), $this->decId);
         
         $updateData=$updateHubunganBiaya && $updateHubunganBiayaToSia;
         
         $this->Obj->EndTrans($updateData);   
         $this->ObjToSia->EndTrans($updateData);   
         
      if ($updateData === true) {
				Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('hubungan_biaya', 'inputHubunganBiaya', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
		$arrId = $this->_POST['idDelete'];
      
      if($this->ObjToSia->addConn){
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeleteHubunganBiayaById($arrId);
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteHubunganBiayaById($arrId);

         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         
         $this->Obj->EndTrans($deleteDataById); 
         $this->ObjToSia->EndTrans($deleteDataById); 
         
         if($deleteDataById==false){
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans();
            
            $deleteArrDataToSireg = $this->Obj->DoDeleteHubunganBiayaByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeleteHubunganBiayaByArrayId($arr);
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            
            $this->Obj->EndTrans($deleteArrData); 
            $this->ObjToSia->EndTrans($deleteArrData); 
         }

         if($deleteArrData === true) {
            Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', '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++) {
               $deleteData = false;
               $this->Obj->StartTrans();
               $this->ObjToSia->StartTrans();
               
               $deleteDataToSireg = $this->Obj->DoDeleteHubunganBiayaById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeleteHubunganBiayaById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               
               $this->Obj->EndTrans($deleteData); 
               $this->ObjToSia->EndTrans($deleteData); 
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('hubungan_biaya', 'hubunganBiaya', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
