<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pelelangan_barang/business/LelangBarang.class.php';
class ProcessLelangBarang {

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

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

	function __construct() {
	
		$this->Obj = new LelangBarang();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('pelelangan_barang', 'lelangBarang', 'view', 'html').'&grp='.Dispatcher::Instance()->Encrypt($this->_POST['id_pelelangan_barang']);
		$this->pageViewDel = Dispatcher::Instance()->GetUrl('pelelangan_barang', 'lelangBarang', 'view', 'html').'&grp='.Dispatcher::Instance()->Decrypt($_GET['grp']);
		$this->pageInput = Dispatcher::Instance()->GetUrl('pelelangan_barang', 'inputLelangBarang', 'view', 'html');
		
		$this->lelangDate=$_POST['tanggalLelang_year'].'-'.$_POST['tanggalLelang_mon'].'-'.$_POST['tanggalLelang_day'];
		//$this->doDate=$_POST['doDate_year'].'-'.$_POST['doDate_mon'].'-'.$_POST['doDate_day'];
		
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
		
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['nomorBa']) == "") {
				return "empty";
			} else {
				return true;
			}
		return false;
		}
	}
	
	function Add() {
		$cek = $this->Check();
		if($cek === true ) {
		$this->Obj->StartTrans();
		
		$detil=$this->_POST['barangLelang'];
		
		if(!empty($detil)){
			foreach ($detil as $value)
			{
				$totalBiaya += $value['lelangBiaya'];
			}			
		} else { $totalBiaya=0; }

			$addLelangBarang = $this->Obj->DoAddLelangBarang($this->_POST['nomorBa'], $this->_POST['idMitra'], $this->lelangDate, $totalBiaya, $this->_POST['catatan'], $this->idUser);

			if(!empty($detil)){
				 foreach ($detil as $value)
				{
				$addLelangBarangDet= $this->Obj->DoAddLelangBarangDet($value['barangId'],$value['pengBrgDetId'],$value['lelangBiaya'], $this->idUser);
				}			
			}
	
			$this->Obj->EndTrans($addLelangBarangDet);
		
			if ($addLelangBarangDet === true) {
				Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('pelelangan_barang', 'inputLelangBarang', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['id_pelelangan_barang']);;
		} return $this->pageView;
	}

	function Update() {
		$cek = $this->Check();
		if($cek === true ) {
		$this->Obj->StartTrans();
		
		$detil=$this->_POST['barangLelang'];
		
		if(!empty($detil)){
				 foreach ($detil as $value)
				{
				$totalBiaya += $value['lelangBiaya'];
				}			
		}else
		{$totalBiaya=0;}
		
			$updateLelangBarang = $this->Obj->DoUpdateLelangBarang($this->_POST['nomorBa'], $this->_POST['idMitra'], $this->lelangDate, $totalBiaya, $this->_POST['catatan'], $this->idUser, $this->_POST['idLelang']);
			
			$dataLelang = $this->Obj->GetDataLelangBarangById($this->_POST['idLelang']);
			$deleteDetil = $this->Obj->DoDeleteLelangBarangDetil($this->_POST['idLelang']);
			
			if($deleteDetil == 1){
				foreach($dataLelang as $val){
					$this->Obj->DoUpdateInvDet('1', $this->idUser, $val['barangId']);
				}
			}
			
			if(!empty($detil)){
				foreach ($detil as $value)
				{
					$updateLelangBarangDet = $this->Obj->DoUpdateLelangBarangDet($this->_POST['idLelang'], $value['barangId'],$value['pengBrgDetId'],$value['lelangBiaya'], $this->idUser);
				}			
			}
			
		$this->Obj->EndTrans($updateLelangBarangDet);
		
			if ($updateLelangBarangDet === true) {
				Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', 'view', 'html', array($this->_POST,'Gagal Mengubah Data', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('pelelangan_barang', 'inputLelangBarang', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, Tanda * menunjukkan field tersebut harus diisi atau dipilih'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId .'&grp='. Dispatcher::Instance()->Encrypt($this->_POST['id_pelelangan_barang']);;
		} return $this->pageView;	
	}
	
	function Delete() {
		$arrId = $this->_POST['idDelete'];
		
		$dataLelangId = $this->Obj->GetDataLelangBarangById($arrId);
		$deleteDetil = $this->Obj->DoDeleteLelangBarangDetil($arrId);
		$deleteArrData = $this->Obj->DoDeleteLelangBarangByArrayId($arrId);
		
		if($deleteArrData === true) {
			foreach($dataLelangId as $val){
				$this->Obj->DoUpdateInvDet('1', $this->idUser, $val['barangId']);
			}
			
			Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', '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($arrId);$i++) {
				$deleteData = false;
				$deleteData = $this->Obj->DoDeleteBgDepartemenById($arrId[$i]);
				if($deleteData === true) $sukses += 1;
				else $gagal += 1;
			}
			Messenger::Instance()->Send('pelelangan_barang', 'lelangBarang', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}
}
?>
