<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/penghasilan/business/AppPenghasilan.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/penghasilan/business/AppPenghasilanToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/penghasilan/business/AppPenghasilanToUm.class.php';
class ProcessPenghasilan {

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

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

	function __construct() {
		$this->Obj = new AppPenghasilan();
		$this->ObjToSia = new AppPenghasilanToSia();
		$this->umObj = new AppPenghasilanToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('penghasilan', 'penghasilan', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('penghasilan', 'inputPenghasilan', 'view', 'html');
		$this->useUm= GTFWConfiguration::GetValue('application','use_gtum');
	}

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

	function Add() {
		$cek = $this->Check();
      
		if($cek === true) {
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         

			$addPenghasilanToSireg = $this->Obj->DoAddPenghasilan($this->_POST['penghasilan_besar'],$this->_POST['penghasilan_urutan']);
			$id_peng = $this->Obj->GetMaxId();
			$addPenghasilanToSia = $this->ObjToSia->DoAddPenghasilan($id_peng[0]['max_id'],$this->_POST['penghasilan_besar'],$this->_POST['penghasilan_urutan']);
			
         $addPenghasilan=$addPenghasilanToSireg && $addPenghasilanToSia;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $phsln=explode('-',str_replace(array('.','<','>'),'', $this->_POST['penghasilan_besar']));
            
            $addPengasilanToUm = $this->umObj->DoAddPenghasilan($id_peng[0]['max_id'],$phsln[0],$phsln[1],$this->_POST['penghasilan_besar']);
            $addPenghasilan = $addPenghasilan && $addPengasilanToUm;
            $this->umObj->EndTrans($addPenghasilan);
         }
         $this->Obj->EndTrans($addPenghasilan);
         $this->ObjToSia->EndTrans($addPenghasilan);
         
         if ($addPenghasilan === true) {
				Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('penghasilan', 'inputPenghasilan', '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();
         
			$updatePenghasilanToSireg = $this->Obj->DoUpdatePenghasilan($this->_POST['penghasilan_besar'], $this->_POST['penghasilan_urutan'], $this->decId);
			$updatePenghasilanToSia = $this->Obj->DoUpdatePenghasilan($this->_POST['penghasilan_besar'], $this->_POST['penghasilan_urutan'], $this->decId);
		//	print(str_replace('.','', $this->_POST['penghasilan_besar']));exit;
         $updatePenghasilan=$updatePenghasilanToSireg && $updatePenghasilanToSia;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $phsln=explode('-',str_replace(array('.','<','>'),'', $this->_POST['penghasilan_besar']));
            //print_r($phsln);
            $updatePenghasilanToUm = $this->umObj->DoUpdatePenghasilan($phsln[0],$phsln[1],$this->_POST['penghasilan_besar'], $this->decId);
            $updatePenghasilan = $updatePenghasilan && $updatePenghasilanToUm;
            $this->umObj->EndTrans($updatePenghasilan);
         }
         $this->Obj->EndTrans($updatePenghasilan);
         $this->ObjToSia->EndTrans($updatePenghasilan);         
         
         if ($updatePenghasilan === true) {
				Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('penghasilan', 'inputPenghasilan', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
	
      if($this->ObjToSia->addConn==false){
         Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif($this->umObj->addConn==false && $this->useUm==true){
         Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Admisi', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;  
      }else{
   		$arrId = $this->_POST['idDelete'];
         #print_r($arrId);
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans(); 
         
         
   		$deleteDataByIdToSireg=$this->Obj->DoDeletePenghasilanById($arrId);
   		$deleteDataByIdToSia=$this->ObjToSia->DoDeletePenghasilanById($arrId);
   		
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteDataByIdToUm = $this->umObj->DoDeletePenghasilanById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdToUm;
            $this->umObj->EndTrans($updatePenghasilan);
         }
         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById); 	
   
         if($deleteDataById==false){
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans(); 
   		    if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
               
            $deleteArrDataToSireg = $this->Obj->DoDeletePenghasilanByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeletePenghasilanByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            if($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrDataToUm = $this->umObj->DoDeletePenghasilanByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataToUm;
               $this->umObj->EndTrans($deleteArrData);
            }
            $this->Obj->EndTrans($deleteArrData);
            $this->ObjToSia->EndTrans($deleteArrData);
         }
   
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('penghasilan', 'penghasilan', '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->DoDeletePenghasilanById($arr[$i]);
   				$deleteDataToSia = $this->Obj->DoDeletePenghasilanById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               if($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteDataToUm = $this->umObj->DoDeletePenghasilanById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataToUm;
                  $this->umObj->EndTrans($deleteData);
               }
               $this->Obj->EndTrans($deleteData);
               $this->ObjToSia->EndTrans($deleteData);       
               
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('penghasilan', 'penghasilan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
