<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/nim_formula/business/AppNimFormula.class.php';
class ProcessNimFormula {
	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
   	
   var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

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

   function __construct() {
		$this->Obj = new AppNimFormula();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('nim_formula', 'nimFormula', 'view', 'html');
		$this->pageSetFormula = Dispatcher::Instance()->GetUrl('nim_formula', 'settingNimFormula', 'view', 'html');
		$this->pageSetFormatFormula = Dispatcher::Instance()->GetUrl('nim_formula', 'settingNimFormulaFormat', 'view', 'html');
	}
   
   function Check(){
   	if (isset($_POST['btnsimpan'])) {
      
         $Check=array_values($this->_POST);
         
         for($i=1;$i<sizeOf($Check)-1;$i++){
            if($Check[$i]==""){
               if($strCheck=="")
                  $strCheck.="empty";
               else
                  $strCheck.="-empty";
            }else{
               if($strCheck=="")
                  $strCheck.="not_empty";
               else
                  $strCheck.="-not_empty";
            }
         }
         
         $strCheck=explode('-',$strCheck);
         $empty=0;
         
         for($i=0;$i<sizeOf($strCheck);$i++){
            if($strCheck[$i]=="empty"){
               $empty++;
            }
         }

         if($empty>0){
            return "empty";
         }else{
            return true;
         }
      }
   }
   
   function SettingFormula(){
      $formula=$this->_POST['formula'];
      if(!empty($formula)){
         $_SESSION['formula']=$formula;
         Messenger::Instance()->Send('nim_formula', 'settingNimFormulaFormat', 'view', 'html', array($this->_POST),
            Messenger::NextRequest);
         return $this->pageSetFormatFormula;
      }else{
         Messenger::Instance()->Send('nim_formula', 'settingNimFormula', 'view', 'html', array($this->_POST,'NIM Komponen Belum Dipilih'),
            Messenger::NextRequest);
         return $this->pageSetFormula;
      }
   }
   
   function Add(){

      if(isset($_POST['btnbalik'])){
          unset($_SESSION['formula']);
         return $this->pageView ;
      }
      $cek = $this->Check();

      if($cek === true){
         $formula=$_SESSION['formula'];
   
         $formulaKomp=explode('&',$formula);
    
         for($i=0;$i<sizeOf($formulaKomp);$i++){
            
            if($formulaKomp[$i]=='[001]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k001'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k001'];
               }          
            }elseif($formulaKomp[$i]=='[002]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i] ;
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i]  ;
               }    
            }elseif($formulaKomp[$i]=='[003]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k003'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k003'];
               }    
            }elseif($formulaKomp[$i]=='[004]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k004'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k004'];
               }    
            }elseif($formulaKomp[$i]=='[005]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k005'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k005'];
               }    
            }elseif($formulaKomp[$i]=='[006]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k006'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k006'];
               }    
            }elseif($formulaKomp[$i]=='[007]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k007'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k007'];
               }    
            }elseif($formulaKomp[$i]=='[008]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . 
                                    $this->_POST['k008_jml_digit'].'|'.
                                    $this->_POST['k008_tipe'].'|'.
                                    $this->_POST['k008_thn_mulai'].'|'.
                                    $this->_POST['k008_nim_mulai'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' .  
                                    $this->_POST['k008_jml_digit'].'|'.
                                    $this->_POST['k008_tipe'].'|'.
                                    $this->_POST['k008_thn_mulai'].'|'.
                                    $this->_POST['k008_nim_mulai'];
               }    
               
            }elseif($formulaKomp[$i]=='[009]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k009'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k009'];
               }    
            }elseif($formulaKomp[$i]=='[010]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i] ;
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i]  ;
               }    
            }elseif($formulaKomp[$i]=='.'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }elseif($formulaKomp[$i]=='-'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }elseif($formulaKomp[$i]=='/'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }
           // print($formatFormula);
         }

         $addData=$this->Obj->DoAddNimFormula($formula,$formatFormula);
         unset($_SESSION['formula']);
         if($addData==true){
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Penambahan Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            return $this->pageView;
         }else{
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Penambahan Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            return $this->pageView; 
         }
      }elseif($cek == "empty"){
         Messenger::Instance()->Send('nim_formula', 'settingNimFormulaFormat', 'view', 'html', array($this->_POST,'Lengkapi Isian'),Messenger::NextRequest);
         return $this->pageSetFormatFormula;
      }
   }
   
   function Update(){
      if(isset($_POST['btnbalik'])){
         return $this->pageView ;
         unset($_SESSION['formula']);
      }
      
      $cek = $this->Check();
      
      if($cek === true){
         $formula=$_SESSION['formula'];
         $formulaKomp=explode('&',$formula);

           for($i=0;$i<sizeOf($formulaKomp);$i++){
            
            if($formulaKomp[$i]=='[001]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k001'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k001'];
               }          
            }elseif($formulaKomp[$i]=='[002]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i] ;
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i]  ;
               }    
            }elseif($formulaKomp[$i]=='[003]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k003'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k003'];
               }    
            }elseif($formulaKomp[$i]=='[004]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k004'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k004'];
               }    
            }elseif($formulaKomp[$i]=='[005]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k005'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k005'];
               }    
            }elseif($formulaKomp[$i]=='[006]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k006'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k006'];
               }    
            }elseif($formulaKomp[$i]=='[007]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k007'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k007'];
               }    
            }elseif($formulaKomp[$i]=='[008]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . 
                                    $this->_POST['k008_jml_digit'].'|'.
                                    $this->_POST['k008_tipe'].'|'.
                                    $this->_POST['k008_thn_mulai'].'|'.
                                    $this->_POST['k008_nim_mulai'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' .  
                                    $this->_POST['k008_jml_digit'].'|'.
                                    $this->_POST['k008_tipe'].'|'.
                                    $this->_POST['k008_thn_mulai'].'|'.
                                    $this->_POST['k008_nim_mulai'];
               }    
               
            }elseif($formulaKomp[$i]=='[009]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i]. ':' . $this->_POST['k009'];
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i] . ':' . $this->_POST['k009'];
               }    
            }elseif($formulaKomp[$i]=='[010]'){
               if(empty($formatFormula)){
                  $formatFormula .= $formulaKomp[$i] ;
               }else{
                  $formatFormula .= '&' . $formulaKomp[$i]  ;
               }    
            }elseif($formulaKomp[$i]=='.'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }elseif($formulaKomp[$i]=='-'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }elseif($formulaKomp[$i]=='/'){
               $formatFormula .= '&' . $formulaKomp[$i];
            }
         }
         
         $updateData=$this->Obj->DoUpdateNimFormula($formula,$formatFormula,$this->decId);
         
         unset($_SESSION['formula']);
          
         if($updateData==true){
         
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Perubahan Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            return $this->pageView;
         }else{
        
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Perubahan Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            return $this->pageView; 
         }
      }elseif($cek == "empty"){
         Messenger::Instance()->Send('nim_formula', 'settingNimFormulaFormat', 'view', 'html', array($this->_POST,'Lengkapi Isian'),Messenger::NextRequest);
         return $this->pageSetFormatFormula;
      }
   }
   
   function Aktifasi(){
      $this->Obj->StartTrans();
      
      $nonAktifAll=$this->Obj->DoNonAktifAllNimFormula();
      $aktifById=$this->Obj->DoAktifNimFormulaById($this->decId);
      
      $aktifasi=$nonAktifAll && $aktifById;
      
      $this->Obj->EndTrans($aktifasi);
      if($aktifasi==true){
         Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'NIM Formula Berhasil Diaktifkan', $this->cssDone),Messenger::NextRequest);
         return $this->pageView;
      }else{
         Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'NIM Formula Gagal Diaktifkan', $this->cssFail),Messenger::NextRequest);
         return $this->pageView; 
      }
   }
   
   function Delete(){
   
   	$arrId = $this->_POST['idDelete'];
      
      $deleteDataById=$this->Obj->DoDeleteNimFormulaById($arrId);
      
      if($deleteDataById == false){
		   if(is_array($arrId)==false)
            $arr[0]=$arrId;
         else
            $arr=$arrId;
            
         $deleteArrData = $this->Obj-> DoDeleteNimFormulaByArrayId($arr);
      }else{      
         if($deleteArrData == true) {
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById == true){ 
            Messenger::Instance()->Send('nim_formula', 'nimFormula', '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->DoDeleteNimFormulaById($arr[$i]);
               if($deleteData == true) $sukses += 1;
               else $gagal += 1;				
            }
            Messenger::Instance()->Send('nim_formula', 'nimFormula', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }
      }  
      return $this->pageView;
   }
}
?>