<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/'.Dispatcher::Instance()->mModule.'/business/DeliveryOrder.class.php';

class ProcessDO
{
	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 DeliveryOrder;
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DOInput', '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;
      if (empty($this->_POST)) return $this->pageInput;
      $msg = array();
      
      if (!$this->_POST['doNoDO']) $msg[] = "Field Nomor DO harus diisi.";
      if (!$this->_POST['doPICPengirim']) $msg[] = "Data PIC Pengirim tidak boleh kosong.";
      
      // cuma ngecek validitas file, proses move_uploaded_file ada di Pengadaan.class.php
      if (!$this->CheckUploadedImage('doFile')) 
         $msg[] = 'File DO tidak valid atau proses upload gagal.';
      
      if (count($msg) > 0 OR isset($_GET['changePengadaan']))
      {
         if (isset($_GET['changePengadaan']))
         {
            $msg = array();
            unset($this->_POST['jumlahDO']);
         }
         $msg = array($this->_POST, $msg, 'notebox-alert');
         Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'DOInput', '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 Delivery Order Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses Penambahan Data Gagal!", $this->cssFail);
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
      
      $result = $this->Obj->Update($_GET['id']->Integer()->Raw(), $this->_POST);
      if ($result === 0)
         $msg = array(1=>'Pengubahan Data Delivery Order Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses pengubahan data gagal!", $this->cssFail);
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Penerimaan ()
   {
   	$this->pageReturn = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DODetail', 'view', 'html')."&id=".$_GET['id']->Integer()->Raw();
      
      foreach(array('doPICPenerima','tglTerima') as $key) if ($this->_POST[$key] == '')
         {$msg[] = 'Isian bertanda * tidak boleh kosong!'; break;}
      
      foreach ($this->_POST['ruang'] as $value) if ($value > 0) $temp = $value;
      if (!isset($temp)) $msg[] = 'Tidak ada item DO yang diproses!';
      
      if (isset($msg))
      {
         $msg = array($this->_POST, $msg, 'notebox-alert');
         Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'DODetail', 'view', 'html', $msg, Messenger::NextRequest);
         return $this->pageReturn;
      }
      
      $result = $this->Obj->Penerimaan($_GET['id']->Integer()->Raw(), $this->_POST);
      if ($result === 0)
         $msg = array(1=>'Proses Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses pengubahan data gagal!", $this->cssFail);
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'DODetail', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageReturn;
	}
   
   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(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
}
?>