<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/perbaikan_barang/business/PerbaikanBarang.class.php';
class ProcessPerbaikanBarang {

	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 PerbaikanBarang();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('perbaikan_barang', 'perbaikanBarang', 'view', 'html').'&grp='.Dispatcher::Instance()->Encrypt($this->_POST['id_perbaikan_barang']);
		$this->pageViewDel = Dispatcher::Instance()->GetUrl('perbaikan_barang', 'perbaikanBarang', 'view', 'html').'&grp='.Dispatcher::Instance()->Decrypt($_GET['grp']);
		$this->pageInput = Dispatcher::Instance()->GetUrl('perbaikan_barang', 'inputPerbaikanBarang', 'view', 'html');
		
		$this->perbaikanDate=$_POST['tanggalPerbaikan_year'].'-'.$_POST['tanggalPerbaikan_mon'].'-'.$_POST['tanggalPerbaikan_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['barangPerbaikan'];
		
		if(!empty($detil)){
				 foreach ($detil as $value)
				{
				$totalBiaya += $value['perbaikanBiaya'];
				}			
		}else
		{$totalBiaya=0;}
		
			$addPerbaikanBarang = $this->Obj->DoAddPerbaikanBarang($this->_POST['nomorBa'], $this->perbaikanDate, $this->doDate, $this->_POST['idMitra'],
																									$totalBiaya, $this->_POST['catatan'], $this->idUser);
			
			
			if(!empty($detil)){
				 foreach ($detil as $value)
				{
				$addPerbaikanBarangDet= $this->Obj->DoAddPerbaikanBarangDet($value['barangId'],$value['pengendalianDetId'],$value['perbaikanBiaya'], $this->idUser);
				}			
			}
			
		$this->Obj->EndTrans($addPerbaikanBarangDet);
		
			if ($addPerbaikanBarangDet === true) {
				Messenger::Instance()->Send('perbaikan_barang', 'perbaikanBarang', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('perbaikan_barang', 'perbaikanBarang', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('perbaikan_barang', 'inputPerbaikanBarang', '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_perbaikan_barang']);;
		} return $this->pageView;
	}
	
	function Update() {
		$cek = $this->Check();
		if($cek === true ) {
		$this->Obj->StartTrans();
		
		$detil=$this->_POST['barangPerbaikan'];
		
		if(!empty($detil)){
				 foreach ($detil as $value)
				{
				$totalBiaya += $value['perbaikanBiaya'];
				}			
		}else
		{$totalBiaya=0;}
		
			$updatePerbaikanBarang = $this->Obj->DoUpdatePerbaikanBarang($this->_POST['nomorBa'], $this->perbaikanDate, $this->doDate, $this->_POST['idMitra'],
																									$totalBiaya, $this->_POST['catatan'], $this->idUser, $this->_POST['idPerbaikan']);
			
			$dataBarang = $this->Obj->GetDataPerbaikanBarangById($this->_POST['idPerbaikan']);
			$deleteDetil = $this->Obj->DoDeletePerbaikanBarangDetil($this->_POST['idPerbaikan']);
			if($deleteDetil == ''){
				foreach($dataBarang as $val){
					$this->Obj->DoUpdateInvDet('1', $this->idUser, $val['barangId']);
				}
			}
			
			if(!empty($detil)){
				foreach ($detil as $value){
					$updatePerbaikanBarangDet= $this->Obj->DoUpdatePerbaikanBarangDet($this->_POST['idPerbaikan'], $value['barangId'],$value['pengendalianDetId'],$value['perbaikanBiaya'], $this->idUser);
				}			
			}
			
		$this->Obj->EndTrans($updatePerbaikanBarangDet);
		
			if ($updatePerbaikanBarangDet === true) {
				Messenger::Instance()->Send('perbaikan_barang', 'perbaikanBarang', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('perbaikan_barang', 'perbaikanBarang', 'view', 'html', array($this->_POST,'Gagal Mengubah Data', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('perbaikan_barang', 'inputPerbaikanBarang', '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_perbaikan_barang']);;
		} return $this->pageView;
	
	}
	
	function Delete() {
		$arrId = $this->_POST['idDelete'];
		
		$dataPerbaikanById = $this->Obj->GetDataPerbaikanBarangById($arrId);
		$deleteDetil = $this->Obj->DoDeletePerbaikanBarangDetil($arrId);
		$deleteArrData = $this->Obj->DoDeletePerbaikanBarangByArrayId($arrId);
		
		if($deleteArrData === true) {
			foreach($dataPerbaikanById as $val){
				$this->Obj->DoUpdateInvDet('1', $this->idUser, $val['barangId']);
			}
			
			Messenger::Instance()->Send('perbaikan_barang', 'perbaikanBarang', '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('perbaikan_barang', 'perbaikanBarang', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}
}
?>
