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

class ProcessJenisPembayaran {

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

   function __construct() {
      $this->Obj = new AppJenisPembayaran();
      $this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('jenis_pembayaran', 'jenisPembayaran', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html');      
      
   }
   
   function Check() {
		if (isset($_POST['btnsimpan'])) {
			$dataJenisBayar=$this->Obj->GetDataJenisPembayaranById($this->_POST['jns_bayar_kode']);
			if((trim($this->_POST['jns_bayar_kode']) == "") or (trim($this->_POST['jns_bayar_nama']) == "") ){
				return "empty";
			} elseif(is_numeric($this->_POST['jns_bayar_kode'])) {
            return "not_string";
         }elseif((empty($this->decId)) and($dataJenisBayar[0]['kode_jns_bayar']===$this->_POST['jns_bayar_kode'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataJenisBayar[0]['kode_jns_bayar']!=$this->decId) and ($dataJenisBayar[0]['kode_jns_bayar']===$this->_POST['jns_bayar_kode'])){           
            return "already_use";           
         }else{
				return true;
			}
		}
		return false;	
	}

   function Add() {
		$cek = $this->Check();
		if($cek === true) {
			$add = $this->Obj->DoAddJenisPembayaran($this->_POST['jns_bayar_kode'], $this->_POST['jns_bayar_nama']);
         if ($add  === true) {
				Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);             
			}
		}elseif($cek=="already_use"){
         Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Kode Jenis Pembayaran Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "not_string") {
			Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Kode Jenis Pembayaran Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView;
	}
  
   function Update() {
      $cek = $this->Check();
		if($cek === true) {
            $proc = $this->Obj->DoUpdateJenisPembayaran($this->_POST['jns_bayar_kode'], $this->_POST['jns_bayar_nama'], $this->decId);
            if ($proc === true) {
               Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST,'Pengupdatean data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST,'Gagal Mengupdate Data', $this->cssFail),Messenger::NextRequest);
            }
            $urlRedirect = $this->pageView;
      }elseif($cek=="already_use"){
         Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Kode Jenis Pembayaran Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "not_string") {
			Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Kode Jenis Pembayaran Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jenis_pembayaran', 'inputJenisPembayaran', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}
		 return $urlRedirect;
   }
   
   function Delete(){
		$arrId = $this->_POST['idDelete'];
         
      $deleteDataById=$this->Obj->DoDeleteJenisPembayaranById($arrId);
      
		if($deleteDataById==false){
   		if(is_array($arrId)==false)
            $arr[0]=$arrId;
         else
            $arr=$arrId;           
            $deleteArrData = $this->Obj-> DoDeleteJenisPembayaranByArrayId($arr);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', '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;
            $deleteData = $this->Obj->DoDeleteJenisPembayaranById($arr[$i]);
                              
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('jenis_pembayaran', 'jenisPembayaran', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
	
	

}
?>
