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

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

	var $return;

	function __construct() {
		$this->Obj = new Usulan;
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanList', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanInput', 'view', 'html');
	}
   
   function CheckUploadedFile($name)
   {
      $fileName = strrev(basename($_FILES[$name]['name']));
      $fileExtPos = strpos($fileName, '.');
      $fileExt = strtolower(strrev(substr($fileName, 0, $fileExtPos)));
      $_FILES[$name]['name']= strrev(substr($fileName, $fileExtPos + 1));
      $_FILES[$name]['ext']= $fileExt;
      if
      (
         $_FILES[$name]['error'] !== 0 ||
         in_array($fileExt, array('pl','py','php','asp','cgi')) AND
         $fileName !== ''
      )
         return false;
      else return true;
   }
   
   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      unset($this->_POST['btnsimpan']);
      
      $msg = array();
      
      if (!empty($this->_POST['detail_barang'])) foreach ($this->_POST['detail_barang'] as $key=>$value)
      {
         if (intval($value['usulanBrgDetJml']) != $value['usulanBrgDetJml'] OR $value['usulanBrgDetJml'] == 0)
         {
            $msg[] = "Jumlah DO harus numeris dan nilainya lebih dari 0.";
            break;
         }
         
         $this->_POST['detail_barang'][$key]['usulanBrgDetJml'] = intval($value['usulanBrgDetJml']);
      }
      
      if ($this->_POST['usulanBrgMstPIC'] == '')
         $msg[] = "Data PIC harap diisi.";
      if (!$this->CheckUploadedFile('fileUsulan'))
         $msg[] = "Proses upload file usulan gagal!";
      
      if (count($msg) > 0)
      {
         $msg = array($this->_POST, $msg, 'notebox-alert');
         Messenger::Instance()->Send('approval_usulan_barang', 'usulanInput', 'view', 'html', $msg, Messenger::NextRequest);
         
         $return = $this->pageInput;
         if (isset($_GET['id'])) $return .= "&id=".$_GET['id'];
         return $return;
      }
      
      return true;
   }
   
   function Add()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
      
      $result = $this->Obj->Add($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Penambahan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses Penambahan Data Gagal!", $this->cssFail);
      Messenger::Instance()->Send('approval_usulan_barang', 'usulanList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
		
      $result = $this->Obj->Update($this->_POST, $_GET['id']->Integer()->Raw());
      if ($result === 0)
         $msg = array(1=>'Perubahan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Tidak Berhasil Mengubah Data!", $this->cssFail);
      Messenger::Instance()->Send('approval_usulan_barang', 'usulanList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Delete()
   {
      $result = $this->Obj->Delete($this->_POST['idDelete']);
      if ($result === 0)
         $msg = array(1=>'Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Ada $result data yang tidak bisa dihapus.", $this->cssFail);
      Messenger::Instance()->Send('approval_usulan_barang', 'usulanList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Approve ()
   {
      $this->pageInput = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanApproval', 'view', 'html');
      
      $get = $_GET->AsArray();
      
      //if (isset($_GET['changeId']))
      if(!empty($get['changeApproval']) || isset($get['changeApproval']) || $get['changeApproval'] == '1')
      {
         $msg = array($this->_POST);
         Messenger::Instance()->Send('approval_usulan_barang', 'usulanApproval', 'view', 'html', $msg, Messenger::NextRequest);
         return $this->pageInput;
      }
      
      // validasi
      $msg = array();
      if ($this->_POST['usulanRkpNoRkp'] == '') $msg[] = 'Kolom "No Rekap Usulan" harus diisi!';
      if ($this->_POST['usulanRkpPicApprove'] == '') $msg[] = 'Kolom "PIC Approval" harus diisi!';
      // ---------
      
      if (!empty($msg))
      {
         $msg = array($this->_POST, $msg, 'notebox-alert');
         Messenger::Instance()->Send('approval_usulan_barang', 'usulanApproval', 'view', 'html', $msg, Messenger::NextRequest);
         return $this->pageInput;
      }
      
      $result = $this->Obj->Approve($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Proses Approval Pengadaan Barang Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Proses Approval gagal!', $this->cssFail);
      Messenger::Instance()->Send('approval_usulan_barang', 'usulanList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
}
?>