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

class ProcessPengadaanInput
{
	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 Pengadaan;
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('pengadaan', 'listPengadaan', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('pengadaan', 'pengadaanInput', 'view', 'html');
	}

   function CheckUploadedImage($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 ||
         strpos($_FILES[$name]['type'], 'image') !== 0 ||
         !in_array($fileExt, array('gif','jpg','jpeg','png','bmp')) AND
         $fileName !== ''
      )
         return false;
      else return true;
   }

   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      unset($this->_POST['btnsimpan']);

      $msg = array();
		$dataCheck = $this->_POST['rekapUsulan'];
		if(!empty($dataCheck)){
			foreach($dataCheck as $val){
				$vals = number_format($val['total2'], 2, ',', '.');
				$step0 = str_replace(',00','',$vals);
				$step1[] = str_replace('.','',$step0);
				//$step1[] = $val['total2'];
			}
			$max = array_sum($step1);
		}#else{$msg[] = 'Data rekap tidak boleh kosong!';}

      $MustExists = array
      (
         'pengadaanNama',
         // 'pengadaanMAK',
         'pengadaanNoSPK',
         'pengadaanUnitkerjaId',
         'pengadaanNominalPengadaan',
      	 'rekapUsulan'
      );
      
      foreach ($MustExists as $val) if ($this->_POST[$val] == '')
         {$msg[] = 'Isian bertanda * (bintang) tidak boleh kosong!'; break;}

      // cuma ngecek validitas file, proses move_uploaded_file ada di Pengadaan.class.php
      if (!$this->CheckUploadedImage('pengadaanFileSPK'))
         $msg[] = 'File SPK tidak valid atau proses upload gagal.';

#       $input = $this->_POST['pengadaanNominalPengadaan'];
#		if($input != $max){
#			$msg[] = 'Jumlah Nominal Pengadaan (<b>Rp. '.number_format($input, 2, ",", ".").'</b>) tidak sama dengan nominal rekap usulan yang diadakan yaitu (<b>Rp. '.number_format($max, 2, ",", ".").'</b>)! ';
#		}
	  
//       if($input > $max){
//       		$msg[] = 'Jumlah Nominal Pengadaan (<b>Rp '.number_format($input,2,",",".").'</b>) tidak boleh lebih besar dari total rekap usulan yang diadakan yaitu (<b>Rp '.number_format($max,2,",",".").'</b>) ! ';
//       }

      if (count($msg) > 0)
      {
         $msg = array($this->_POST, $msg, '');
         Messenger::Instance()->Send('pengadaan', 'pengadaanInput', '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;

      $error = array
      (
         'Penambahan data berhasil dilakukan.',
         'Tidak bisa mengubah tabel pengadaan!',
         'Tidak bisa mengubah tabel panitia pengadaan!',
         'Upload file SPK gagal!',
         'Tidak bisa menambah data Rekap!'
      );

      $result = $this->Obj->AddPengadaan($this->_POST);
      if ($result === 0)
         $msg = array(1=>$error[$result], $this->cssDone);
      else $msg = array(1=>$error[$result], $this->cssFail);
      Messenger::Instance()->Send('pengadaan', 'listPengadaan', 'view', 'html', $msg, Messenger::NextRequest);

      return $this->pageView;
   }

   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;

      $error = array
      (
         'Proses update data berhasil dilakukan.',
         'Tidak bisa mengubah tabel pengadaan!',
         'Tidak bisa mengubah tabel panitia pengadaan!',
         'Upload file SPK gagal!',
         'Tidak bisa menambah data Rekap!',
         'Tidak bisa menghapus data Rekap!'
      );

      $result = $this->Obj->UpdatePengadaan($this->_POST);
      if ($result === 0)
         $msg = array(1=>$error[$result], $this->cssDone);
      else $msg = array(1=>$error[$result], $this->cssFail);
      Messenger::Instance()->Send('pengadaan', 'listPengadaan', '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('pengadaan', 'listPengadaan', 'view', 'html', $msg, Messenger::NextRequest);

      return $this->pageView;
   }
}
?>