<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/perbaikan_barang_history/business/PerbaikanBarangHistory.class.php';
class ProcessPerbaikanBarangHistory {

	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 PerbaikanBarangHistory();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['id']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('perbaikan_barang_history', 'perbaikanBarangHistory', 'view', 'html').'&grp='.Dispatcher::Instance()->Encrypt($this->_POST['idPerbaikanDet']);
		$this->pageViewDel = Dispatcher::Instance()->GetUrl('perbaikan_barang_history', 'perbaikanBarangHistory', 'view', 'html').'&grp='.Dispatcher::Instance()->Decrypt($_GET['grp']);
		$this->pageInput = Dispatcher::Instance()->GetUrl('perbaikan_barang_history', 'inputPerbaikanBarangHistory', 'view', 'html');
			
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
	}

	function Check() {
            if (isset($_POST['btnsimpan'])) {
                if(trim($this->_POST['idPerbaikanDet']) == "" || count($this->_POST['brg']) == 0 ) {
                        return "empty";
                } else {
                        return "valid";
                }
            return false;
            }
	}
	
	function Update() {
		$cek = $this->Check();
		if($cek === "valid" ) {
			$this->Obj->StartTrans();		
			# add perbaikan barang history
			$brg = $this->_POST['brg'];
			foreach($brg as $val){
				$dataHistory = array(
					$this->_POST['idPerbaikanDet'], date('Y-m-d', strtotime($val['tgl'])),
					$val['serv'], $val['harga'],
					$val['jml'], $val['satuan_aset'],
					$val['total'], $val['ket'],
					$this->idUser
				);
				$addHistory = $this->Obj->DoAddPerbaikanBarangHistory($dataHistory);
			}
			# -----------------------------
			
			# update status di inventarisasi detail
			$dataInvDet = array(
				$this->_POST['status_aset'],
                                $this->_POST['kondisi_aset'],
				$this->idUser,
				$this->_POST['barangId'],
			);
			$updateInvDet = $this->Obj->DoUpdateStatusInvDet($dataInvDet);
			# -------------------------------------
			
			# add kondisi history n trigger ke inventarisasi detail
			$dataKondisi = array(
				$this->_POST['barangId'],
				$this->_POST['kondisi_aset'],
				$this->idUser
			);
			$addKondisi = $this->Obj->DoAddKondisiHistory($dataKondisi);
			# -----------------------------------------------------

			if ($this->Obj->EndTrans(true)) {
				Messenger::Instance()->Send('perbaikan_barang_history', 'perbaikanBarangHistory', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('perbaikan_barang_history', 'perbaikanBarangHistory', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		}elseif($cek == "empty"){
			Messenger::Instance()->Send('perbaikan_barang_history', 'inputPerbaikanBarangHistory', 'view', 'html', array($this->_POST,'Lengkapi Isian Data, tanda * menunjukkan field tersebut harus diisi atau dipilih'),Messenger::NextRequest);
			return $this->pageInput . "&idPerbaikanBarangHistory=" .$this->_POST['barangId'];
		} return $this->pageView;
	
	}
}
?>
