<?php
/**
* @package UsulanBarangProc
* @copyright Copyright (c) PT Gamatechno Indonesia
* @author Dyan Galih <galih@gamatechno.com>
* @version 0.1
* @startDate 2011-01-21
* @lastUpdate 2011-01-21
* @description Usulan Barang
*/

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

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


   function __construct() {
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('pb_usulan', 'usulanBarang', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('pb_usulan', 'inputUsulanBarang', 'view', 'html');
   }
	
	public 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;
   }
	
	public function CheckData ()
   {
      $arrReturn['status']=true;
      
		if (!empty($this->_POST['detail_barang'])) foreach ($this->_POST['detail_barang'] as $key=>$value)
      {
         if (intval($value['usulanBrgDetJml']) != $value['usulanBrgDetJml'] OR $value['usulanBrgDetJml'] == 0)
         {
            $arrReturn['status'] = false;
				$arrReturn['message'] = 'Jumlah DO harus numeris dan nilainya lebih dari 0.';return $arrReturn;
            break;
         }
         $this->_POST['detail_barang'][$key]['usulanBrgDetJml'] = intval($value['usulanBrgDetJml']);
		}
		
      if ($this->_POST['pic'] == ''){
			$arrReturn['status'] = false;
         $arrReturn['message'] = "Data PIC harap diisi.";return $arrReturn;}
		if (!$this->CheckUploadedFile('file')){
         $arrReturn['status'] = false;
         $arrReturn['message'] = "Proses upload file usulan gagal!";return $arrReturn;}
   }
	
	public function GenerateNumber()
	{
		$Obj = new Usulan();
		$gnumber = $Obj->GenerateNumber();
		$explode = explode(".", $gnumber[0]['usulanBrgNoUsulan']);
		$lenght 	= strlen(intval($explode[1]+1));
		$year		= date('Y');
		switch($lenght){
			case 1: $generate = 'usl'.$year.'.'.'00000'.($explode[1]+1); break;
			case 2: $generate = 'usl'.$year.'.'.'0000'.($explode[1]+1); break;
			case 3: $generate = 'usl'.$year.'.'.'000'.($explode[1]+1); break;
			case 4: $generate = 'usl'.$year.'.'.'00'.($explode[1]+1); break;
			case 5: $generate = 'usl'.$year.'.'.'0'.($explode[1]+1); break;
			case 6: $generate = 'usl'.$year.'.'.($explode[1]+1); break;
		}
		return $generate;
	}
	
	public function Add()
   {
      $result = $this->CheckData();

		if($result['status']===false){
         return $result;
		}

      $Obj = new Usulan();
		$this->_POST['no_usul'] = $this->GenerateNumber();
      $result =  $Obj->Add($this->_POST);
		$arrReturn['status'] = $result;
      if($result)
         $arrReturn['message'] = 'Penambahan Data Sukses';
      else
         $arrReturn['message'] = 'Penambahan Data Gagal';
      return $arrReturn;
   }
	
	public function Update()
   {
      $result = $this->CheckData();

		if($result['status']===false){
         return $result;
		}
		
      $Obj = new Usulan();
		$result =  $Obj->Update($this->_POST, $this->_POST['usulanId']);
      $arrReturn['status'] = $result;
      if($result)
         $arrReturn['message'] = 'Perubahan Data Sukses';
      else
         $arrReturn['message'] = 'Perubahan Data Gagal';
      return $arrReturn;
   }
	
	public function Delete($id)
   {
      $Obj = new Usulan();
      $result =  $Obj->Delete($id);
      $urlReturn = Dispatcher::Instance()->GetUrl('pb_usulan', 'usulanBarang', 'view', 'html');
      if($result){
         Messenger::Instance()->Send('pb_usulan', 'usulanBarang', 'view', 'html', array($_POST,"Penghapusan Data Sukses",$this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('pb_usulan', 'usulanBarang', 'view', 'html', array($_POST,"Penghapusan Data Gagal",$this->cssFail),Messenger::NextRequest); 
      }
      return $urlReturn;
   }
}
?>