<?php

CLASS Basic extends RasioProject
{
	protected $set;
	protected $em;
	protected $login;
	protected $limit = 50;

	public function set()
	{
	
	}
	
	public function Defdate()
	{
		$date = new Date;
		$date = $date->serialDate($_GET['idDate']);
			
		$login = SESSION;
		$_SESSION[$login]['login']['date'] = date("Y-m-d", strtotime($date));

		echo $_GET['idDate'];
		$this->layout = "null";
	}
	
	public function Defdate2()
	{
		$date = new Date;
		$awl = $date->serialDate($_GET['tglaw']);
		$akr = $date->serialDate($_GET['tglak']);
			
		$login = SESSION;
		$_SESSION[$login]['login']['date_aw'] = date("Y-m-d", strtotime($awl));
		$_SESSION[$login]['login']['date_ak'] = date("Y-m-d", strtotime($akr));

		echo $_GET['idDate'];
		$this->layout = "null";
	}
	
	public function kunciNotDb($set, $tgl)
	{
		$this->em = $set->em;
		$date	= new Date;
		$cek 	= $set->em->getFind("setting", "value", array("id_setting = '1'"));
		$kunci 	= $date->serialDate($cek->value);
		
		if($kunci >= $tgl){
			echo "<script>alert('Periode sudah di kunci ".$cek->value."')</script>";
			return false;
		}
		
		return true;
	}
	
	protected function serialePk($pk)
	{
		$res = '';
		foreach($pk as $row){
			if(!empty($res)){
				$res.= ", ";
			}
			$res.= $row;
		}
		return $res;
	}
	
	protected function setPage($get, $set)
	{
		$get['jenis'] = strtolower($get['jenis']);
		$no = (int)$get['no'];
		$res->sorting 	= $set->dSort;
		$res->awal		= $no;
		
		$fil = array();
		if(!empty($set->where)){
			foreach($set->where as $row){
				$fil[] = "$row";
			}
		}
		
		
		$data = $set->em->getFind($set->dBaseH, "count(*) as jum", $fil);
		$max = (int)$data->jum - 1;
		if($max < 0){
			$max = 0;
		}
		
		if($get['jenis'] != 'first'){
			if($max < 0){
				$no = 0;
			}
			if($no > $max){
				$no = $max;
			}
			$res->awal = $no;
		}
		if($get['jenis'] == 'last'){
			$res->awal = $max;
		}

		if($res->awal <= 0){
			$res->prev = 0;
			$res->next = 1;
		}elseif($res->awal >= $max){
			$res->prev = $max - 1;
			$res->next = $max;
		}else{
			$res->prev = $res->awal - 1;
			$res->next = $res->awal + 1;
		}
		return $res;
	}
	
	public function display($set, $awal = 0, $akhir = 1, $fil)
	{
		$pk 	= $this->serialePk($set->dBasePk);
		$sort	= $set->dSort;
		
		$sel = "*";
		if(!empty($set->selFind)){
			$sel = $set->selFind;
		}
		
		$sql = $this->em->addSelect($sel);
		$sql = $this->em->getFrom($set->dBaseH, 'a');
		$sql = $this->em->getWhere("1=1");
		
		foreach($fil as $row){
			$sql = $this->em->andWhere($row);
		}
		
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $this->em->andWhere($row);
			}
		}
		
		if(!empty($set->multiSort)){
			$sql = $this->em->getShortBy($set->multiSort, '');
		}else{
			$sql = $this->em->getShortBy($pk, $sort);
		}

		$sql = $this->em->getLimit($awal, $akhir);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);

		if(!empty($res[0])){
			$res = $res[0];
		}
		
		if(!empty($res->chusr)){
			$user = $this->em->getFind("sub", "username", array("kd_sub = '".$res->chusr."'"));
			$res->chusr = $user->username;
		}
		
		return $res;
	}
	
	public function setFirst($set)
	{
		$sql = $this->em->addSelect("_no");
		$sql = $this->em->getFrom($set->dBaseH, 'a');
		$sql = $this->em->getWhere("1=1");
		if(count($set->where) > 0){
			foreach($set->where as $row){
				$sql = $this->em->andWhere($row);
			}
		}
		$sql = $this->em->getShortBy('_no', $set->dSort);
		$sql = $this->em->getLimit(0, 1);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		return $res[0]->_no; 
	}
	
	public function setLast($set)
	{
		$sql = $this->em->addSelect("_no");
		$sql = $this->em->getFrom($set->dBaseH, 'a');
		$sql = $this->em->getWhere('1=1');
		if(count($set->where) > 0){
			foreach($set->where as $row){
				$sql = $this->em->andWhere($row);
			}
		}
		if(strtoupper($set->dSort) == 'ASC'){
			$sql = $this->em->getShortBy('_no', 'desc');
		}else{
			$sql = $this->em->getShortBy('_no', 'asc');
		}
		$sql = $this->em->getLimit(0, 1);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);

		return $res[0]->_no; 
	}
	
	public function setNext($set, $get)
	{
		$nom = $get['idRef'];
		if(strtoupper($set->dSort) == 'ASC'){
			$data = $this->getFindNo($set, $get, $nom, 'asc');
		}else{
			$no = $this->getFindNo($set, $get, $nom, 'desc');
		}
		return $res; 
	}
	
	public function getFindNo($set, $get, $nom, $act = false)
	{
		$pk = $this->serialePk($set->dBasePk);
		$fil = array();
		$fil[] = "CONCAT($pk) > '$nom'";
		if(!empty($set->where)){
			foreach($set->where as $row){
				$fil[] = $row;
			}
		}
	}
	
	public function setPref($set, $get)
	{
		$nom = $get['idRef'];
		if(strtoupper($set->dSort) == 'ASC'){
			$no = $this->getFindNo($set, $get, $nom, '-');
		}else{
			$no = $this->getFindNo($set, $get, $nom, '+');
		}
		$res = (int)$no;
		return $res; 
	}
	
	public function dataDisFind($set, $jen, $has = true)
	{
		$sel = "*";
		if(!empty($set->selFind)){
			$sel = $set->selFind;
		}
		
		$pk  = $this->serialePk($set->dBasePk);		
		if(count($set->dBasePk) > 1){
			$pk = "CONCAT($pk)";
		}
		if(!empty($pk)){
			$sel.= ", $pk as pk";
		}
		
		$sort = "$pk asc";
			
		$fil = array();
		if(strtoupper($_GET['jenis']) == 'FIRST'){

		}
		if(strtoupper($_GET['jenis']) == 'NEXT'){
			$fil[] = "$pk > '".$_GET['idRef']."'";
			$sort = "$pk asc";
		}
		if(strtoupper($_GET['jenis']) == 'LAST'){
			$sort = "$pk desc";
		}
		if(strtoupper($_GET['jenis']) == 'PREV'){
			$fil[] = "$pk < '".$_GET['idRef']."'";
			$sort = "$pk desc";
		}
		if($has == false){
			$fil = array();
			$fil[] = "$pk = '".$_GET['idRef']."'";
		}
		
		if(!empty($_GET['nojnl'])){
			$fil = array();
			$fil[] = "$pk = '".$_GET['nojnl']."'";
		}
		
		$sql = $this->em->addSelect($sel);
		$sql = $this->em->getFrom($set->dBaseH, 'a');
		$sql = $this->em->getWhere('1=1');
		$sql = $this->em->andWhere("$pk is not null");
		$sql = $this->em->andWhere("$pk != ''");
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $this->em->andWhere($row);
			}
		}
		foreach($fil as $row){
			$sql = $this->em->andWhere($row);
		}
		$sql = $this->em->getMultiSort($sort);
		$sql = $this->em->getLimit(0, 1);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);

		return $res[0]; 
	}
	
	public function Find($get, $set, $fil = array(), $load = false)
	{
	
		$this->em = $set->em;
		$data = $this->dataDisFind($set, $get['jenis']);
		if(empty($data)){
			$data = $this->dataDisFind($set, $get['jenis'], false);
		}
		
		if($load == false){
			if(!empty($set->detail)){
				if(!empty($set->detail->query)){
					
					$sel = $set->detail->query;
					$sql = $this->em->addSelect("*");
					$sql = $this->em->getFrom("($sel)", 'a');
					$sql = $this->em->getWhere($set->detail->pk." = '".$data->pk."'");
					$sql = $this->em->andWhere($set->detail->pk." is not null");
					$sql = $this->em->andWhere($set->detail->pk." != ''");
					$eks = $this->em->queryEks($sql);
					$detail = $this->em->getFetchObject($eks);
					
				}else{
					$fil = array();
					$fil[] = $set->detail->pk." is not null";
					$fil[] = $set->detail->pk." != ''";
					$fil[] = $set->detail->pk." = '".$data->pk."'";
					if(!empty($set->detail->where)){
						foreach($set->detail->where as $r){
							if($r['mode'] == 'string'){
								$fil[] = $r['val'];
							}else{
								$idAtr = $r['val'];
								$fil[] = $r['val']." = '".$data->$idAtr."'";
							}
						}
					}else{
						if(!empty($set->detail->where)){
							$fil[] = $set->detail->where;
						}
					}
					$selD = '*';
					if(!empty($set->detail->select)){
						$selD = $set->detail->select;
					}
					
					
					$detail = $set->em->getFindBy($set->detail->data, $selD, $fil, $set->detail->sort, false);
				}
			}
		}
		
		$this->set = $set;
		$this->set->data = $data;
		$this->set->dataDetail = $detail;
		$this->set->db 	 = new DB;
		$this->set->data->funct = $set->funcFind;
		$this->data->data->_no 	= $set->nomUrut;
		return $this->set;
	}
	
	public function serialGet($get)
	{
		foreach($get as $k => $r){
			$get[$k] = str_ireplace("___", "/", $get[$k]);
			$get[$k] = str_ireplace("***", "+", $get[$k]);
			$get[$k] = str_ireplace("---", "%", $get[$k]);
		}
		return $get;
	}
	
	public function data($get = array(), $set, $filter = array(), $jenis = false)
	{
		$this->em = $set->em;
		$this->set = $set;
		$data = $this->dataGrid($get, $set, $filter, $jenis);
		$this->set->set  = $set;
		$this->set->data = $data;
		$this->set->mode = 'browse';
		if(!empty($_GET['pkBrowse'])){
			$this->set->awal = $this->getCountData($get, $set, $filter, $_GET['pkBrowse']);
		}
		
		if(count($set->dBasePk) == 1){
			$_pk = $set->dBasePk[0];
			$_filPk = 'filtera'.str_ireplace(".", "", $_pk);
			$this->set->def_filter = $_filPk;
		}
		
		return $this->set;
	}
	
	public function cariData($jen = false)
	{
		$_GET = $this->serialGet($_GET);
		$ctrl = $_GET['_ctrl'];
		$this->set = $_SESSION['dataSetting'][$ctrl];
		$this->set->em = new $_SESSION['dataSetting'][$ctrl]->database;

		unset($_GET['_ctrl']);
		unset($_GET['idRef']);
		$this->data = $this->data($_GET, $this->set, $_GET, $jen);
		$this->layout = "default/dataManualGrid";
		return $this->data;
	}
	
	public function manGridFirst()
	{
		$this->cariData();
		$this->data->awal = 1;
	}
	
	public function manGridlast()
	{
		$data = $this->cariData('last');
		
		$awal = $data->data->total - $this->limit;
		if($awal <= 0){
			$this->data->awal = 1;
		}else{
			$this->data->awal = $awal+1;
		}
	}
	
	public function Loaddatamanualgrid()
	{
		$_GET = $this->serialGet($_GET);
		$ctrl = $_GET['_ctrl'];
		$_nom = $_GET['Lno'];
		$type = $_GET['type'];
		$_pk = $_GET['pk'];
		$this->set = $_SESSION['dataSetting'][$ctrl];
		$this->set->em = new $_SESSION['dataSetting'][$ctrl]->database;
		$this->em = $this->set->em;

		
		unset($_GET['_ctrl']);
		unset($_GET['Lno']);
		unset($_GET['type']);
		unset($_GET['pk']);
		unset($_GET['idRef']);
		$fil = $_GET;
		
		$_GET['start'] = 0;
		$_GET['limit'] = $this->limit;
		$_GET['nomor'] = $_nom + 1;
		$_GET['next']  = $type;
		$_GET['_pk']   = $_pk;
		
		$data  = $this->getDataList($_GET, $this->set, $fil);
		
		$this->data->no   = $_nom;
		$this->data->nom  = $_nom + 1;
		$this->data->data = $data;
		$this->data->set  = $this->set;
		$this->data->type = $type;
		
		if($type == 'back'){
			$this->data->nom  = ($this->data->nom - count($data)) - 1;
		}
		
		$this->layout = "default/dataManualGridLoad";
	}
	
	public function dataGrid($get, $set, $filter = array(), $jenis = false)
	{
		$limit = $this->limit;
		$page = $_GET['page'];
		if(empty($page)){
			$page = 1;
		}
		$_GET['start'] = ($page * $limit) - $limit;
		$_GET['limit'] = $limit;
		$_GET['nomor'] = $_GET['start'] + 1;
		
		$total = $this->getCountData($_GET, $set, $filter);
		$data  = $this->getDataList($_GET, $set, $filter, $jenis);
		
		$akhir = $_GET['start'] + $limit;
		if($akhir > $total){
			$akhir = $total;
		}
		$_GET['finish'] = $akhir;
		
		$res->total = $total;
		$res->data 	= $data;
		$res->get 	= $_GET;
		return $res;
	}
	
	protected function getCountData($param, $set, $filter = array(), $idRef = false)
	{
		$pk = new Kode;
		$pk = $pk->getPk($set->dBasePk);

		$sql = $this->getQueryGrid($pk, $param, $set, $filter);
		if($idRef != false){
			$sql = $this->em->andWhere("CONCAT($pk) <= '".$idRef."'");
		}

		$eks = $this->em->queryEks($sql);
		return $this->em->getNumRows($eks);
	}
	
	public function lastPkCek($get, $set, $filter = array(), $jenis = false)
	{
		$pk   = '';		
		foreach($set->dBasePk as $row){
			if(!empty($pk)){
				$pk.= ', ';
			}
			$pk.= $row;
		}
		if(count($set->dBasePk) > 1){
			$pk = "CONCAT($pk)";
		}
		
		$sql = $this->getQueryGrid("$pk as pk", $get, $set, $get);
		$sql = $this->em->getMultiSort("$pk desc");
		$sql = $this->em->getLimit(0, 50);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		
		$jum = count($res) - 1;
		if($jum < 0){
			$jum = 0;
		}
		return $res[$jum];
	}
	
	protected function getDataList($get, $set, $filter = array(), $jenis = false)
	{
		$lastPk = false;
		if($jenis == 'last'){
			$lastPk = $this->lastPkCek($get, $set, $filter = array(), $jenis = false);
		}
		$pk   = '';		
		foreach($set->dBasePk as $row){
			if(!empty($pk)){
				$pk.= ', ';
			}
			$pk.= $row;
		}
		if(count($set->dBasePk) > 1){
			$pk = "CONCAT($pk)";
		}
		$pkAttr = '';
		if(!empty($pk)){
			$pkAttr = ", $pk as pk";
		}
		
		$sql = $this->getQueryGrid("* $pkAttr", $get, $set, $get);
		
		if($get['next'] == 'next'){
			$sql = $this->em->andWhere("$pk > '".$get['_pk']."'");
		}
		if($get['next'] == 'back'){
			$sql = $this->em->andWhere("$pk < '".$get['_pk']."'");
		}
		if(!empty($_GET['pkBrowse'])){
			$sql = $this->em->andWhere("$pk >= '".$_GET['pkBrowse']."'");
		}
		if(!empty($lastPk->pk)){
			$sql = $this->em->andWhere("$pk >= '".$lastPk->pk."'");
		}
		
		if(!empty($set->multisort)){
			$sql = $this->em->getMultiShort($set->multisort);
		}else{
			if(!empty($pk)){
				$sort = "$pk asc";
				if($get['next'] == 'back'){
					$sort = "$pk desc";
				}
				$sql = $this->em->getMultiShort($sort);
			}
		}
		
		$sql = $this->em->getLimit($get['start'], $get['limit']);

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	protected function getQueryGrid($sel, $param, $set, $filter = array())
	{
		$pk = new Kode;
		
		if(!empty($set->sqlGrid)){
			$sel = $this->em->setSql($set->sqlGrid);
			$sql = $this->em->addSelect("*");
			$sql = $this->em->getFrom("($sel)", 'a');
			$sql = $this->em->getWhere("1=1");
		}else{
			$sql = $this->em->addSelect($sel);
			$sql = $this->em->getFrom($set->dBaseH, 'a');
			if(count($set->grid->join) > 0){
				foreach($set->grid->join as $tab => $row){
					$sql = $this->em->addLeftJoin($tab, $row['alias'], $row['on']);
				}
			}
			$sql = $this->em->getWhere("1=1");
			if(!empty($set->where)){
				foreach($set->where as $row){
					$sql = $this->em->andWhere($row);
				}
			}
		}
		
		if(!empty($_GET['get_def'])){
			if(count($set->dBasePk) == 1){
				$_idPk = $set->dBasePk[0];
				$sql = $this->em->andWhere("upper($_idPk) like '".strtoupper($_GET['get_def'])."%'");
			}
		}
		
		foreach($set->grid->attr as $row){
			$key = 'filter'.str_ireplace(".", "", $row['attr']);
			$fil = strtoupper($filter[$key]);
			if(!empty($fil)){
			
				$strPors = '%';
				$pos = strpos($fil, $strPors);
				if($pos == true){
					$jenLike = 'like';
				}else{
					$jenLike = '=';
				}
				$jenLike = 'like';

				if(!empty($row['model'])){
					$row['attr'] = 'CONCAT('.$pk->getConcat($row['model']).')';
				}
				if(strtoupper($row['type']) == strtoupper('date')){
					$sql = $this->em->andWhere("upper(to_char(".$row['attr'].", 'dd/mm/yyyy')) $jenLike '%$fil%'");
				}elseif(strtoupper($row['type']) == strtoupper('numeric')){
				
				}else{
					$sql = $this->em->andWhere("upper(".$row['attr'].") $jenLike '%$fil%'");
				}
			}
		}
		$sql = $this->getWherePopUp($sql);

		return $sql;
	}
	
	protected function getWherePopUp($sql)
	{
		if(!empty($_GET['param']) and $_GET['param'] != 'undefined'){
			$param = $_GET['param'];
			$param = str_ireplace("_-_", "(", $param);
			$param = str_ireplace("-_-", ")", $param);
			$param = str_ireplace("***", "'", $param);
			$param = str_ireplace("___", "=", $param);
			$param = str_ireplace(";;;", " ", $param);
			$sql = $this->em->andWhere("($param)");
		}
	
		return $sql;
	}

	protected function setIdAttr($id)
	{
		$jum = explode('.', $id);
		if(count($jum) <= 1){
			return $id;
		}else{
			return $jum[1];
		}
	}
	
	public function Caridatapopup()
	{
		$mod = $_GET['modul'];
		$_SESSION['filterPopUp']->filterPopUp = $_POST;
		$set = $_SESSION['dataSetting'][$mod];
		$this->data = $this->data($_GET, $set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function Pilih()
	{
		$mod = $_GET['modul'];
		$set = $_SESSION['dataSetting'][$mod];
		$_GET['id'] = str_ireplace("__--__", "/", $_GET['id']);
		
		$sel = "*";
		if(!empty($set->selFind)){
			$sel = $set->selFind;
		}
		$pk  = $this->serialePk($set->dBasePk);		
		if(count($set->dBasePk) > 1){
			$pk = "CONCAT($pk)";
		}
		if(!empty($pk)){
			$sel.= ", $pk as pk";
		}
		
		$this->em  = new $set->database;
		$set->data = $this->em->getFind($set->dBaseH, $sel, array("$pk = '".$_GET['id']."'"));
		
		if(!empty($set->data->chusr)){
			$user = $this->em->getFind("sub", "username", array("kd_sub = '".$set->data->chusr."'"));
			$set->data->chusr = $user->username;
		}
		if(!empty($set->detail)){
			if(!empty($set->detail->query)){
				$sel = $set->detail->query;
				$sql = $this->em->addSelect("*");
				$sql = $this->em->getFrom("($sel)", 'a');
				$sql = $this->em->getWhere($set->detail->pk." = '".$set->data->pk."'");
				$eks = $this->em->queryEks($sql);
				$detail = $this->em->getFetchObject($eks);
			}else{
				$fil = array();
				if(!empty($set->detail->where)){
					foreach($set->detail->where as $r){
						if($r['mode'] == 'string'){
							$fil[] = $r['val'];
						}else{
							$idAtr = $r['val'];
							$fil[] = $r['val']." = '".$set->data->$idAtr."'";
						}
					}
				}else{
					$fil[] = $set->detail->pk." = '".$set->data->$pk."'";
					if(!empty($set->detail->where)){
						$fil[] = $set->detail->where;
					}
				}
				$selD = '*';
				if(!empty($set->detail->select)){
					$selD = $set->detail->select;
				}
				$detail = $this->em->getFindBy($set->detail->data, $selD, $fil, $set->detail->sort);
			}
		}		
		$set->pageIn->prev = $_GET['p'];
		$set->pageIn->next = $_GET['n'];
		
		$this->data = $set;
		$this->data->settingPage = 'false';
		$this->data->controller  = $mod;
		$this->data->dataDetail  = $detail;
		$this->data->db 		 = new DB;
		$this->data->funct 		 = $set->funct;
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	public function findNo()
	{
		$mod = $_GET['idRef'];
		$idN = $_GET['id'];
		$set = $_SESSION['findNo'.$idN];

		$this->em  = new PgSql;

		$sel = "*";
		if(!empty($set->selFind)){
			$sel = $set->selFind;
		}
		$pk  = $this->serialePk($set->dBasePk);		
		if(count($set->dBasePk) > 1){
			$pk = "CONCAT($pk)";
		}
		if(!empty($pk)){
			$sel.= ", $pk as pk";
		}
		
		$set->data = $this->em->getFind($set->dBaseH, $sel, array("_no = '".$_GET['id']."'"));
		
		if(!empty($set->data->chusr)){
			$user = $this->em->getFind("sub", "username", array("kd_sub = '".$set->data->chusr."'"));
			$set->data->chusr = $user->username;
		}
		
		if(!empty($set->detail)){
			$fil = array();
			$fil[] = $set->detail->pk." = '".$set->data->$pk."'";
			if(!empty($set->detail->where)){
				$fil[] = $set->detail->where;
			}
			$selD = '*';
			if(!empty($set->detail->select)){
				$selD = $set->detail->select;
			}
			$detail = $this->em->getFindBy($set->detail->data, $selD, $fil, $set->detail->sort);
		}
		
		
		$set->pageIn->prev = $_GET['p'];
		$set->pageIn->next = $_GET['n'];
		
		$this->data = $set;
		$this->data->settingPage = 'false';
		$this->data->controller  = $mod;
		$this->data->dataDetail  = $detail;
		$this->data->db 		 = new DB;
		$this->data->funct 		 = $set->funct;
		$this->view = "default/displayForm";
		$this->layout = "view";
	}

	public function getLastNum($table, $attr, $awal = '', $akhir = '', $panjang = 4, $set)
	{
		$param = array(
						'table' => $table,
						'field' => $attr,
						'kd_awal' => $awal,
						'kd_akhir' => $akhir,
						'panjang' => $panjang,
					);
		return $this->dbLastNum($param, $set);
	}
	
	
	public function dbLastNum($param, $set)
	{
		$data = $this->getInDb($param, $set);
		if(count($data) > 0){
			$field = $param['field'];
			$kode = $data[0]->$field;
			$kode = str_ireplace($param['kd_awal'], '', str_ireplace($param['kd_akhir'], '', $kode));
			$kode = explode(".", $kode);
			$urut = (int)$kode[0] + 1;
		}else{
			$urut = 1;
		}
		
		return $urut;
	}
	
	protected function getInDb($param, $set)
	{
		$sql = $set->em->addSelect($param['field']);
		$sql = $set->em->getFrom($param['table'], 'a');
		$sql = $set->em->getWhere("1=1");
		
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $set->em->andWhere($row);
			}
		}
		
		if($param['tgl'] != false){
			$sql = $set->em->andWhere($param['tgl']." >= '".date('Y-m-d', strtotime($param['tgl_awal']))."'");
			$sql = $set->em->andWhere($param['tgl']." <= '".date('Y-m-d', strtotime($param['tgl_akhir']))."'");
		}
		
		$sql = $set->em->andWhere($param['field']." like '".$param['kd_awal']."%".$param['kd_akhir']."'");
		$sql = $set->em->getShortBy($param['field'], 'desc');
		$sql = $set->em->getLimit(0, 1);

		$eks = $set->em->queryEks($sql);
		return $set->em->getFetchObject($eks);
	}
	
	public function setAttr($post, $set)
	{
		$sess = SESSION;
		$this->login = $_SESSION[$sess]['login'];

		$attr = array();
		foreach($set->form as $row){
			$key = $row['input']['attr']['name'];
			
			if($set->notUpper == true){
				$attr[$key] = $post[$key];
			}else{
				$attr[$key] = strtoupper($post[$key]);
			}
		}
		$attr['chtime'] = date("Y-m-d H:i:s");
		$attr['chusr'] = $this->login['kd_user'];

		if(count($set->clearForm) > 0){
			foreach($set->clearForm as $row){
				unset($attr[$row]);
			}
		}
		unset($attr['']);
		
		return $attr;
	}
	
	protected function simpan($post, $set, $attrTambahan)
	{
		$attr = $this->setAttr($post, $set);
		foreach($attrTambahan as $key => $row){
			$attr[$key] = $row;
		}
		
		if(!empty($set->nKode)){
			foreach($set->nKode as $key => $row){
				$attr[$key] = $row;
			}
		}
		
		$sql = $set->em->addInsert($set->dBaseH);
		$sql = $set->em->addAttrIns($attr);
		$qb[] = $sql;

		return $qb;
	}

	protected function update($post, $set, $attrTambahan = array())
	{
		$idRef = "";
		foreach($set->dBasePk as $key => $row){
			$idRef.= $post[$row];
		}
		$kode = str_ireplace(" ", "", $idRef);
		$Code = new Kode;
		$filter = 'upper(trim(replace(CONCAT('.$Code->getPk($set->dBasePk)."), ' ', ''))) = upper('".str_ireplace(" ", "", $kode)."')";
		
		$attr = $this->setAttr($post, $set);
		if(!empty($attrTambahan)){
			foreach($attrTambahan as $key => $row){
				$attr[$key] = $row;
			}
		}
		$sql = $set->em->addUpdate($set->dBaseH);
		$sql = $set->em->addAttrUp($attr);
		
		$pk = trim($_POST['_nodb']);
		if(!empty($filter)){
			$sql = $set->em->getWhere($filter);
			
			if(!empty($set->where)){
				foreach($set->where as $row){
					$sql = $set->em->andWhere($row);
				}
			}
		}else{
			$sql = $set->em->getWhere("_no = '$pk'");
		}
		
		$qb[] = $sql;
		
		return $qb;
	}
	
	public function cekNom($set)
	{
		$sql = $set->em->addSelect("_no");
		$sql = $set->em->getFrom($set->dBaseH, 'a');
		$sql = $set->em->getShortBy("_no", 'desc');
		$sql = $set->em->getLimit(0, 1);
		$eks = $set->em->queryEks($sql);
		$res = $set->em->fetchObject($eks);
		return (int)$res->_no;
	}
	
	public function cekNomUp($post, $set)
	{
		if(count($set->dBasePk) > 1){
			$idRef = "";
			foreach($set->dBasePk as $key => $row){
				$idRef.= $post[$row];
			}
			$kode = str_ireplace(" ", "", $idRef);
			$Code = new Kode;
			$filter = 'upper(trim(replace(CONCAT('.$Code->getPk($set->dBasePk)."), ' ', ''))) = upper('".str_ireplace(" ", "", $kode)."')";
		}else{
			$idRef = $set->dBasePk[0];
			$filter = "upper($idRef) = upper('".$post[$idRef]."')";
		}
		
		$attr = $this->setAttr($post, $set);
		
		$sql = $set->em->addSelect("_no");
		$sql = $set->em->getFrom($set->dBaseH, 'a');
		$sql = $set->em->getWhere($filter);
		
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $set->em->andWhere($row);
			}
		}
		$sql = $set->em->getShortBy("_no", 'desc');
		$sql = $set->em->getLimit(0, 1);
		$eks = $set->em->queryEks($sql);
		$res = $set->em->fetchObject($eks);

		return (int)$res->_no;
	}
	
	public function getRecord($post, $set, $attrTambahan = array(), $qb = array())
	{
		$idRef = "";
		foreach($set->dBasePk as $key => $row){
			$idRef.= $post[$row];
		}
		
		$qb = str_ireplace("'", "*_kutip_*", $qb);
		$qb = str_ireplace("concat", "CONCAT", $qb);
		$qb = str_ireplace("CONCAT", "CONCATENATION", $qb);
		$qb = str_ireplace("(", "*_bk_*", $qb);
		$qb = str_ireplace(")", "*_tk_*", $qb);
		
		$a = rand(1000, 9999);
		$data = array();
		$data['id_record'] 	= date('Ymd/His').'.'.$a;
		$data['kode_modul'] = $idRef;
		$data['modul'] 		= $set->modul;
		$data['parent'] 	= serialize($qb);
		$data['child'] 		= serialize($attrTambahan);
		$data['post'] 		= serialize($post);
		$data['_post'] 		= serialize($_POST);
		$data['no_induk'] 	= $this->login['kd_user'];
		$data['waktu'] 		= date('Y-m-d H:i:s');
		$data['action'] 	= $post['_prosesAction'];

		$sql = $set->em->addInsert('record');
		$sql = $set->em->addAttrIns($data);

		return $sql;
	}
	
	public function proses($post, $set, $attrTambahan = array(), $qbT = array(), $notFlush = false)
	{
		$act = $post['_prosesAction'];
		$qb = $this->$act($post, $set, $attrTambahan);
		
		foreach($qbT as $row){
			$qb[] = $row;
		}
		
		$qb[] = $this->getRecord($post, $set, $attrTambahan, $qb);

		if($notFlush == true){
			return $qb;
		}else{
			$save = $set->em->getFlush($qb);
		}
		
		if($save != false){
			
			if($post['_prosesAction'] == 'input'){
				$nom = $this->cekNom($set);
			}else{
				$nom = $this->cekNomUp($post, $set);
			}

			$_SESSION['findNo'.$nom] = $set;
			echo "<script>makeSuccesNewForm('$nom')</script>";
		}else{
			echo "<script>$('#_displayErrorPopUp').html('Gagal Ulangi Beberapa Saat Lagi')</script>";
			
			echo "<pre>";
			var_dump($qb);
		}
	}
	

	protected function update2($post, $set, $qb)
	{
		$idRef = "";
		foreach($set->dBasePk as $key => $row){
			$idRef.= $post[$row];
		}
		$kode = str_ireplace(" ", "", $idRef);

		if(!empty($set->setLastKode)){
			$kode = $set->setLastKode;
		}
		if(count($set->dBasePk) > 1){
			$Code = new Kode;
			$filter = 'upper(trim(replace(CONCAT('.$Code->getPk($set->dBasePk)."), ' ', ''))) = upper('".str_ireplace(" ", "", $kode)."')";
		}else{
			$idPk = '';
			foreach($set->dBasePk as $key => $row){
				$idPk = $row;
			}
			$filter = "$idPk = '$kode'";
		}

		$attr = array();
		$attr['control'] = 3;
		
		$sql = $set->em->addUpdate($set->dBaseH);
		$sql = $set->em->addAttrUp($attr);
		$sql = $set->em->getWhere($filter);
		
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $set->em->andWhere($row);
			}
		}
		
		return $sql;
	}
	
	protected function simpan2($post, $set, $attrTambahan)
	{
		$attr = $this->setAttr($post, $set);
		foreach($attrTambahan as $key => $row){
			$attr[$key] = $row;
		}
		
		if(!empty($set->nKode)){
			foreach($set->nKode as $key => $row){
				$attr[$key] = $row;
			}
		}
		
		$sql = $set->em->addInsert($set->dBaseH);
		$sql = $set->em->addAttrIns($attr, $set->dBaseH);

		return $sql;
	}
	
	public function cekControl($set, $post)
	{
		$idRef = "";
		foreach($set->dBasePk as $key => $row){
			$idRef.= $post[$row];
		}
		$kode = str_ireplace(" ", "", $idRef);
		if(!empty($set->setLastKode)){
			$kode = $set->setLastKode;
		}
		
		if(count($set->dBasePk) > 1){
			$Code = new Kode;
			$filter = 'upper(trim(replace(CONCAT('.$Code->getPk($set->dBasePk)."), ' ', ''))) = upper('".str_ireplace(" ", "", $kode)."')";
		}else{
			$idPk = '';
			foreach($set->dBasePk as $key => $row){
				$idPk = $row;
			}
			$filter = "$idPk = '$kode'";
		}
		
		$db = $set->em->getFind($set->dBaseH, "control", array($filter));

		$res = (int)$db->control;
		if($res == 0){
			$res = 1;
		}
		
		return $res;
	}
	
	public function proses2($post, $set, $attrTambahan = array(), $qbT = array(), $after = true, $notFlush = false, $exKode = false)
	{
		$ctrl = $this->cekControl($set, $post);

		if($ctrl > 1 and $ctrl != 12){
			echo "<script>$('#_displayErrorPopUp').html('Data tidak bisa di proses')</script>";
		}else{
			$eks = true;
			if($post['_prosesAction'] == 'update'){
				if($ctrl == 1){
					$qb = array();
					$qb[] = $this->update2($post, $set, $attrTambahan);
					$qb[] = $this->simpan2($post, $set, $attrTambahan);
				}elseif($ctrl == 0){
					$qb = $this->update($post, $set, $attrTambahan);
				}else{
					$eks = false;
				}
			}else{
				$qb = $this->simpan($post, $set, $attrTambahan);
			}
			
				
			if($eks == true){
				foreach($qbT as $row){
					$qb[] = $row;
				}
				$qb[] = $this->getRecord($post, $set, $attrTambahan, $qb);
				if($notFlush == false){
					$save = $set->em->getFlush($qb);
				}else{
					return $qb;
				}
				
			}else{
				$save = false;
			}
			
			if($after == false){
				return $save;
			}else{
				if($save != false){
					if($post['_prosesAction'] == 'input'){
						$nom = $this->cekNom($set);
					}else{
						$nom = $this->cekNomUp($post, $set);
					}
					$_SESSION['findNo'.$nom] = $set;
					echo "<script>makeSuccesNewForm('$nom')</script>";
				}else{
					echo "<script>$('#_displayErrorPopUp').html('Gagal Ulangi Beberapa Saat Lagi')</script>";
				}
			}
		}
	}
	
	public function proses3($post, $set, $attrTambahan = array(), $qbT = array(), $after = true, $notFlush = false, $exKode = false)
	{
		$pk = $set->dBasePk[0];
		if($post['_prosesAction'] == 'simpan'){
			$post[$pk] 	= $set->nojnl['nojnl'];
			$_POST[$pk] = $set->nojnl['nojnl'];
			//$attrTambahan['control'] = 0;
			return $this->proses2($post, $set, $attrTambahan, $qbT, $after, $notFlush, $exKode);
		}else{
			$this->em = $set->em;
			$cek = $this->em->getFind($set->dBaseH, 'control', array("$pk = '".$_POST[$pk]."'"));
			if($cek->control == '0'){
				//$attrTambahan['control'] = 0;
				return $this->proses($post, $set, $attrTambahan, $qbT, $notFlush);
			}else{
				$set->setLastKode = $post[$pk];
				$post[$pk] 	= $set->nojnl['nojnl'];
				$_POST[$pk] = $set->nojnl['nojnl'];
				//$attrTambahan['control'] = 1;
				return $this->proses2($post, $set, $attrTambahan, $qbT, $after, $notFlush, $exKode);
			}
		}
	}
	
	public function getError($error)
	{
		foreach($error as $row){
			if($row == false){
				return false;
			}
		}
		return true;
	}
	
	public function validasiBasic($post, $set, $cekData = true)
	{
		if($_POST['_prosesAction'] == 'update'){
			$pk = $set->dBasePk[0];
			if(empty($_POST[$pk])){
				$_POST['_prosesAction'] = 'simpan';
			}
		}
		$detForm = array();
		foreach($set->form as $row){
			$key = $row['input']['attr']['name'];
			$detForm[$key] = $row['label']['name'];
		}
	
		echo "<script>$('#_displayErrorPopUp').html('')</script>";
		echo "<script>
			$('.text').after(function(){
				_class = $(this).attr('class');
				if(_class === 'undefined'){
					_class = '';
				}
				_class = _class.replace('_errorEmpty', '');
				_class = _class.replace('_errorUtf8', '');
				_class = _class.trim();

				_fixTitle = $('#'+this.id).attr('fix_title');
				if(_fixTitle === undefined){
					$('#'+this.id).removeAttr('title');
				}

				$('#'+this.id).removeAttr('class');
				$('#'+this.id).attr('class', _class);
			})
		</script>";
					
		$error = new Error;
		$errEmpty = true;
		foreach($set->notNull as $row){
			if(!empty($row)){
				$isiPost = strtoupper($post[$row]);
				if(empty($post[$row]) or $isiPost == strtoupper('Load...')){
					echo "<script>
							_class = $('#$row').attr('class');
							if(_class === 'undefined'){
								_class = '';
							}
							_class = _class.trim();
							_class = _class+' _errorEmpty';

							$('#$row').removeAttr('class');
							$('#$row').attr('class', _class);
						</script>";
					echo "<script>$('#_displayErrorPopUp').html('ada inputan wajib yang kosong')</script>";
					$errEmpty = false;
				}
			}
		}
		if($errEmpty == false){
			return false;
		}
		
		//-------------Cek ada UTF-8----------//
		$errUtf8 = array();
		foreach($detForm as $id => $k){
			$type = mb_detect_encoding($_POST[$id]);
			if($type == 'UTF-8'){
				echo "<script>
						_class = $('#$id').attr('class');
						if(_class === 'undefined'){
							_class = '';
						}
						_class = _class.trim();
						_class = _class+' _errorUtf8';

						_fixTitle = $('#$id').attr('fix_title');
						if(_fixTitle === undefined){
							$('#$id').attr('title', '".$_POST[$id]."');
						}
				
						$('#$id').removeAttr('class');
						$('#$id').attr('class', _class);
					</script>";
				echo "<script>$('#_displayErrorPopUp').html('Ada inputan Character Aneh (Kolom Warna Pink)')</script>";
				$errUtf8[$id] = false;
			}
		}

		if(count($errUtf8) > 0){
			return false;
		}
		//-------------End Cek----------//
		
		

		if($cekData == true){
			$filter = "";
			foreach($set->dBasePk as $key => $row){
				if(!empty($filter)){
					$filter.= " and ";
				}
				if(!empty($post[$row])){
					$filter.= "(upper(replace($row, ' ', '')) = '".str_ireplace(" ", "", strtoupper($post[$row]))."')";
				}else{
					$filter.= "($row = '' or $row is null)";
				}
			}
			$this->em  = new $set->em;
			
			$fill = array();
			$fill[] = $filter;
			if(!empty($_POST['_nodb'])){
				$fill[] = "_no != '".str_ireplace(" ", "", strtoupper($_POST['_nodb']))."'";
			}
			
			$data = $this->em->getFind($set->dBaseH, "*", $fill);
			if(!empty($data)){
				echo "<script>$('#_displayErrorPopUp').html('Data sudah ada')</script>";
				return false;
			}
		}
		
		if($control == true){
			$filter = "";
			foreach($set->dBasePk as $key => $row){
				if(!empty($filter)){
					$filter.= " and ";
				}
				if(!empty($post[$row])){
					$filter.= "($row = '".$post[$row]."')";
				}else{
					$filter.= "($row = '' or $row is null)";
				}
			}
			$this->em  = new $set->em;
			$data = $this->em->getFind($set->dBaseH, "control", array($filter));
			if((int)$data->control > 1){
				echo "<script>$('#_displayErrorPopUp').html('Data tidak bisa di proses')</script>";
				return false;
			}
		}
		
		$errUtf8 = true;
		foreach($detForm as $id => $k){
			$type = mb_detect_encoding($_POST[$id]);
			if($type == 'UTF-8'){
				echo "<script>
						_class = $('#$row').attr('class');
						if(_class === 'undefined'){
							_class = '';
						}
						_class = _class.trim();
						_class = _class+' _errorUtf8';

						$('#$row').removeAttr('class');
						$('#$row').attr('class', _class);
					</script>";
				echo "<script>$('#_displayErrorPopUp').html('Ada inputan Character Aneh (Warna Pink)')</script>";
				$errUtf8 = false;
			}
		}
		if($errEmpty == false){
			return false;
		}
		
		return true;
	}
	
	public function delete($get, $set)
	{
		$filter = "";
		foreach($set->dBasePk as $key => $row){
			if(!empty($filter)){
				$filter.= " and ";
			}
			if(!empty($get[$row])){
				$get[$row] = str_ireplace("___", "+", $get[$row]);
				$filter.= "($row = '".str_ireplace("__--__", "/", $get[$row])."')";
			}else{
				$filter.= "($row = '' or $row is null)";
			}
		}

		$sql = $set->em->addDelete($set->dBaseH);
		$sql = $set->em->getWhere($filter);
		if(!empty($set->where)){
			foreach($set->where as $row){
				$sql = $set->em->andWhere($row);
			}
		}

		$sess = SESSION;
		$this->login = $_SESSION[$sess]['login'];
		$get['_prosesAction'] = 'Delete';
		
		$qb[] = $sql;
		$qb[] = $this->getRecord($get, $set, $get, $qb);

		$save = $set->em->getFlush($qb);
		if($save != false){
			echo "<script>makeSuccesDel()</script>";
		}
	}
	
	public function delTable($set, $table, $where = array())
	{
		$sql = $set->em->addDelete($table);
		$sql = $set->em->getWhere("1=1");
		foreach($where as $row){
			$sql = $set->em->andWhere("$row");
		}
		return $sql;
	}
	
	public function delete2($get, $set, $sqlT = array())
	{
		$ctrl = $this->cekControl($set, $get);
		if($ctrl > 1){
			echo "<script>alert('Data tidak bisa di hapus')</script>";
		}else{
			
			$pk = $set->dBasePk[0];

			$idPk = $get[$pk];
			$cekT = explode("/", $idPk);
			$cekT = $cekT[0];
			
			if($cekT == 'TMP'){
				$qb[] = $this->delTable($set, $set->dBaseH, array("$pk = '$idPk'"));
				if(!empty($set->detail->data)){
					$qb[] = $this->delTable($set, $set->detail->data, array("$pk = '$idPk'"));
				}
			}else{
				$sql = $set->em->addUpdate($set->dBaseH);
				$sql = $set->em->addAttrUp(array("control" => 2));
				$sql = $set->em->getWhere("$pk = '$idPk'");
				$qb[] = $sql;
			}

			foreach($sqlT as $row){
				$qb[] = $row;
			}
			
			if(!empty($set->posting)){
				$noG = explode(".", $idPk);
				$noG = $noG[0];
				$posting = new Posting;
				$qb[] = $posting->deleteAcdG($set, $noG);
			}
			
			$save = $set->em->getFlush($qb);
			if($save != false){
				if($cekT == 'TMP'){
					$nom = $set->em->getFind($set->dBaseH, '_no', array());
				}else{
					$nom = $set->em->getFind($set->dBaseH, '_no', array("$pk = '$idPk'"));
				}
				$nom = $nom->_no;
				$_SESSION['findNo'.$nom] = $set;
				echo "<script>makeSuccesNewForm('$nom')</script>";
			}else{
				echo "<script>alert('Proses Gagal');</script>";
			}
		}
	}
	
	public function pending($get, $set, $sqlT = array())
	{
		$ctrl = $this->cekControl($set, $get);
		if($ctrl > 1){
			echo "<script>alert('Data tidak bisa di hapus')</script>";
		}else{
			
			$pk = $set->dBasePk[0];

			$idPk = $get[$pk];
			$cekT = explode("/", $idPk);
			$cekT = $cekT[0];
			
			if($cekT == 'TMP'){
				$qb[] = $this->delTable($set, $set->dBaseH, array("$pk = '$idPk'"));
				if(!empty($set->detail->data)){
					$qb[] = $this->delTable($set, $set->detail->data, array("$pk = '$idPk'"));
				}
			}else{
				$sql = $set->em->addUpdate($set->dBaseH);
				$sql = $set->em->addAttrUp(array("control" => 12));
				$sql = $set->em->getWhere("$pk = '$idPk'");
				$qb[] = $sql;
			}

			foreach($sqlT as $row){
				$qb[] = $row;
			}
			
			if(!empty($set->posting)){
				$noG = explode(".", $idPk);
				$noG = $noG[0];
				$posting = new Posting;
				$qb[] = $posting->deleteAcdG($set, $noG);
			}
			
			// echo "<pre>";
			// var_dump($qb);
			
			$save = $set->em->getFlush($qb);
			if($save != false){
				if($cekT == 'TMP'){
					$nom = $set->em->getFind($set->dBaseH, '_no', array());
				}else{
					$nom = $set->em->getFind($set->dBaseH, '_no', array("$pk = '$idPk'"));
				}
				$nom = $nom->_no;
				$_SESSION['findNo'.$nom] = $set;
				echo "<script>makeSuccesNewForm('$nom')</script>";
			}else{
				echo "<script>alert('Proses Gagal');</script>";
			}
		}
	}
	
	public function printer($get, $set)
	{
		$Code = new Kode;
		$Func = new DisplayFunc;
		$Select = new Select;
		$filDisplay = "";
		$filter = array();
		foreach($set->cetak->attr as $row){
			if($row['filter'] == true){
				$key = 'filPrint'.str_ireplace(".", "", $row['attr']);
				$val = str_ireplace("||", "%", $get[$key]);
				$val = str_ireplace("___", "+", $val);
				if(!empty($val)){
					if(!empty($filDisplay)){
						$filDisplay.= ", ";
					}
					$filDisplay.= $row['label'].' : '.$val;
					
					$kk = $row['attr'];
					if(!empty($row['model'])){
						$kk = 'CONCAT('.$Code->getConcat($row['model']).')';
					}
					$filter[] = "upper($kk) like upper('$val')";
				}
			}
		}

		if(!empty($set->where)){
			foreach($set->where as $row){
				if(!empty($row)){
					$filter[] = $row;
				}
			}
		}
		
		$sort = "";
		foreach($set->dBasePk as $key => $row){
			if(!empty($sort)){
				$sort.= ", ";
			}
			$sort.= "$row";
		}
		
		$db = $set->em->getFindBy($set->dBaseH, "*", $filter, $sort);
		
		$data = '<div id="_listCetakDef">';
		$data.= '<p class="_judul">'.$set->modul.'</p>';
		$data.= '<p class="_filter">'.$filDisplay.'</p>';
		$data.= '<table class="_defTable" cellspacing=0 cellpadding=0>';
		$data.= '<thead>';
		$data.= '<tr>';
		$data.= '<th width="30px">No.</td>';
		foreach($set->cetak->attr as $row){
			$data.= '<th width="'.$row['width'].'">'.$row['label'].'</td>';
		}
		$data.= '</tr>';
		$data.= '</thead>';
		foreach($db as $no => $i){
		
			if($no % 2 == 0){
				$backColor = '';
			}else{
				$backColor = '#f7f7f7';
			}
		
			$data.= '<tr>';
			$data.= '<td align="right" style="background-color: '.$backColor.'">'.($no+1).'</td>';
			foreach($set->cetak->attr as $row){
				$key = explode(".", $row['attr']);
				$key = $key[1];
				if(strtoupper($row['type']) == strtoupper('number')){
					$i->$key = number_format($i->$key, (int)$row['dec']);
				}
				if(strtoupper($row['type']) == strtoupper('date')){
					$i->$key = date("d/m/Y", strtotime($i->$key));
					if($i->$key === '01/01/1970'){
						$i->$key = '';
					}
				}
				if(!empty($row['model'])){
					$val = '';
					foreach($row['model'] as $k => $r){
						$val.= $Code->getLeft($i->$k, $r, ' ');
					}
					$i->$key = $val;
				}
				if(!empty($row['DisplayFunc'])){
					$funct = $row['DisplayFunc'];
					$i->$key = $Func->$funct($i->$key);
				}
				if(!empty($row['select'])){
					$select = $row['select'];
					$select = $Select->$select();
					$i->$key = $select[$i->$key];
				}
				$data.= '<td width="'.$row['width'].'" align="'.$row['align'].'" style="background-color: '.$backColor.'">'.$i->$key.'</td>';
			}
			$data.= '</tr>';
		}
		$data.= '</table>';
		$data.= '</div>';
		
		return $data;
	}
	
	public function load($get, $set, $data = false)
	{
		$get['idRef'] = trim($get['idRef']);
		if(!empty($get['idRef'])){
			$filter = array();
			if(!empty($set->where)){
				foreach($set->where as $row){
					$filter[] = $row;
				}
			}
			
			$get['idRef'] = str_ireplace("___", "+", $get['idRef']);
			$kode = new Kode;
			
			if(count($set->dBasePk) > 1){
				$filter[] = 'upper(trim(replace(CONCAT('.$kode->getPk($set->dBasePk)."), ' ', ''))) = upper('".str_ireplace(" ", "", $get['idRef'])."')";
			}else{
				$filter[] = 'replace('.$set->dBasePk[0].", ' ', '') = '".strtoupper(str_ireplace(" ", "", $get['idRef']))."'";
			}
			
			if($data == false){
				$data = $set->em->getFind($set->dBaseH, $kode->getPk($set->dBasePk), $filter);
			}
			
			$res = '';
			foreach($set->dBasePk as $row){
				$res.= $data->$row;
			}

			if(count($set->dBaseLn)){
				$res = '';
				foreach($set->dBaseLn as $key => $row){
					$res.= $kode->getLeft($data->$key, $row, ' ');
				}
			}
			
			$trm = trim($res);
			if(empty($trm)){
				$res = $trm;
			}
		}else{
			$res = false;
		}
		
		$get = "";
		foreach($_GET as $k => $r){
			if($k != 'ref' and $k != 'idRef'){
				$get.= "&$k=$r";
			}
		}
		
		$model->getUrl = $get;
		$model->idtext = $_GET['idtext'];
		$model->m = false;
		if(!empty($res)){
			$model->hasil = $res;
		}
		echo json_encode($model);
		return;
	}
	
	public function detail($get, $set)
	{
		$filter = array();
		foreach($set->detail->idRef as $row){
			$filter[] = "$row = '".$get[$row]."'";
		}
	
		$this->em = $set->em;
		$this->set = $set;
		$this->set->kode = new Kode;
		$this->set->data = $this->em->getFindBy($set->detail->dbaseH, "*", $filter, $set->detail->idInduk." asc");
		$this->set->jenis = $get['jenis'];
		return $this->set;
	}
	
	public function Urut()
	{
		$cls = $_GET['idRef'];
		$set = $_SESSION['setMyCtrl'][$cls];
		$pk  = $this->serialePk($set->dBasePk);
		
		$data = $set->em->getFindBy($set->dBaseH, $pk, array(), "$pk asc");
		$no = 1;
		$qb = array();
		foreach($data as $row){
			$sql = $set->em->addUpdate($set->dBaseH);
			$sql = $set->em->addAttrUp(array("_no" => $no++));
			$sql = $set->em->getWhere("1=1");
			foreach($row as $k => $r){
				$sql = $set->em->andWhere("trim($k) = '".trim($r)."'");
			}
			$qb[] = $sql;
		}
		
		$save = $set->em->getFlush($qb);
		if($save != false){
			echo "Proses Sukses";
		}else{
			echo "Proses Gagal";
		}
		$this->layout = "null";
	}
	
	public function Loadlistdetail()
	{
		$myfile = fopen("det/".$_GET['ctrl'].".txt", "r") or die("Unable to open file!");
		$data = fread($myfile,filesize("det/".$_GET['ctrl'].".txt"));
		fclose($myfile);
		
		$data = unserialize(urldecode($data));
		
		$this->data->detail = $data->set;
		$this->data->dataDetail = $data->data;
		$this->data->funct 	= $_GET['funct'];
		
		$this->layout = "default/displayDetail";
	}
	
	public function loadDisplay($data, $get)
	{
		$target = str_ireplace(" ", "", $get['target']);
		
		$atr = explode(",", $target);
		
		$col = (int)$_GET['col'];
		if($col <= 0){
			$col = false;
		}
		
		$det = '';
		if($col != false){
			$det = '_'.$col;
		}
		
		$res = array();
		$no = 0;
		foreach($atr as $no => $row){
			if(!empty($row)){
				$r = explode("=>", $row);
				$id = $r[1];
				$res[$no]->atr = $r[0].$det;
				$res[$no]->var = $data->$id;
			}
		}
		
		if(empty($_GET['_loadAf'])){
			$_GET['_loadAf'] = 'undefined';
		}
		
		$result->id  = $_GET['_idIn'];
		$result->res = $res;
		$result->jum = count($res);
		$result->after = $_GET['_loadAf'];
		
		return $result;
	}
	
	public function genKode($set, $post, $db = false)
	{
		if($db == false){
			//$post['alias'] = 'TMP';
		}
		$prefik = "";
		foreach($set->setKode['var'] as $r){
			if(!empty($prefik)){
				$prefik.= $set->setKode['del'];
			}
			$prefik.= $post[$r];
		}
		$prefik.= $set->setKode['del'];
		
		$length = (int)$set->setKode['length'];
		if($length <= 0){
			$length = 4;
		}
		
		$pk = $set->dBasePk[0];

		$sql = $set->em->addSelect($pk);
		$sql = $set->em->getFrom($set->dBaseH, 'a');
		$sql = $set->em->getWhere("1=1");
		$sql = $set->em->andWhere("$pk like '".$prefik."%'");
		$sql = $set->em->getShortBy($pk, 'desc');
		$sql = $set->em->getLimit(0, 1);

		$eks = $set->em->queryEks($sql);
		$res = $set->em->fetchObject($eks);
		
		$nom = $res->$pk;
		$nom = str_ireplace($prefik, '', $nom);
		$nom = (int)$nom;
		$nom = $nom + 1;
		
		
		$kode = new Kode;
		$nojnl = $prefik.$kode->getRight($nom, $length, 0);
		
		$res = array();
		$res['nojnl'] = $nojnl;
		return $res;
	}
	
	public function kode($set, $post, $_post, $data = false)
	{
		$this->em = $set->em;
		$pk = $set->dBasePk[0];
		if($data != false){
			return $this->genKode($set, $post, $data);
		}else{
			if($_post['_prosesAction'] == 'simpan'){
				return $this->genKode($set, $post);
			}else{
				$cek = $this->em->getFind($set->dBaseH, 'control', array("$pk = '".$_post[$pk]."'"));
				if($cek->control == '0'){
					$res['nojnl'] = $_post[$pk];
					return $res;
				}else{
					$kode = new Kode;
					$ex = explode(".", $_post[$pk]);
					$asli = $ex[0];
					$tamb = $ex[1];
					$next = $kode->setAbj($tamb, 1);
					$res['nojnl'] 	= $asli.'.'.$next;
					$res['asli'] 	= $asli;
					return $res;
				}
			}
		}
	}
	
	public function cekTmp($set, $data, $post = false, $table = false)
	{
		$load = false;
		if($data->control == '0'){
			
			if($post == false){
				$post = array();
				foreach($data as $k => $r){
					$post[$k] = $r;
				}
			}
			if($table == false){
				$pk = $set->dBasePk[0];
				$table = array(
							$set->dBaseH => $pk,
							$set->detail->data => $pk,
							);
			}

			$nojnl = $this->kode($set, $post, $post, $data);
			$save = $this->setTmpToData($set, $table, $nojnl['nojnl'], $post['nojnl']);
			if($save == false){
				echo "<script>alert('Render Cetakan Gagal, Ulangi lagi');</script>";
			}else{
				$nojnl = $nojnl['nojnl'];
				$nom = $set->em->getFind($set->dBaseH, "_no", array("$pk = '$nojnl'"), true);
				$nom = $nom->_no;
				$_SESSION['findNo'.$nom] = $set;
				echo "<script>makeSuccesNewForm('$nom')</script>";
				$load = true;
			}
		}else
		if($data->control == '1'){
			$nojnl = $data->nojnl;
			$load = true;
		}else{
			echo "<script>alert('Not Found!');</script>";
			return;
		}
		
		if($load == true){
			$requestUri = $_SERVER['REQUEST_URI'];
			$basePath = parse_url($set->url, PHP_URL_PATH);
			$requestUri = preg_replace('#^' . preg_quote($basePath, '#') . '#', '', $requestUri);
			$requestUri = ltrim($requestUri, '/');

			$url = $set->url.$requestUri;
			$url = str_ireplace($post['nojnl'], $nojnl, $url);
			$url = str_ireplace('push', 'cetak', $url);
			echo "<script>window.open('$url');</script>";
		}
		
		return true;
	}
	
	public function setTmpToData($set, $table, $baru, $lama)
	{
		$this->em = $set->em;
		$qb = array();
		foreach($table as $k => $r){
			$attr = array();
			$attr[$r] = $baru;
			
			if($set->dBaseH == $k){
				$attr['control'] = 1;
			}
			
			$sql = $this->em->addUpdate($k);
			$sql = $this->em->addAttrUp($attr);
			$sql = $this->em->getWhere("$r = '$lama'");
			$qb[] = $sql;
		}
		
		if(!empty($set->posting)){
			$set->nojnl['nojnl'] = $baru;
			return $this->posting($set, $qb, $set->posting);
		}else{
			return $this->em->getFlush($qb);
		}
	}
	
	public function posting($set, $qb, $post)
	{
		$this->em = $set->em;
		$this->em->getBeginTransaction();
		$this->em->getRoolbackFlush($qb);
		
		$posting = new Posting;
		$save = $posting->$post($set, $set->nojnl['nojnl']);
		
		if (!$this->em->getRollCek($save) ) { 
			$this->em->getRollBack();
		}else{
			$this->em->getCommit();
		}
		return $save;
	}

	public function acl($set, $modul, $action, $user)
	{
		$modul = strtoupper($modul.'/'.$action);
		
		$em = $set->em;
		$sql = $em->addSelect('*');
		$sql = $em->getFrom('acl', ' a');
		$sql = $em->addLeftJoin('group', 'b', array('(a.kodegrup = b.kodegrup)'));
		$sql = $em->addLeftJoin('grup_user', 'c', array('(b.kodegrup = c.kodegrup)'));
		$sql = $em->getWhere("upper(a.modul) = '$modul'");
		$sql = $em->andWhere("c.kd_user = '$user'");
		$eks = $em->queryEks($sql);
		$res = $em->getFetchObject($eks);
		$sts = false;
		foreach($res as $r){
			if($r->status == '1'){
				$sts = true;
			}
		}
		
		$sql = $em->addSelect('status');
		$sql = $em->getFrom('acl', 'a');
		$sql = $em->getWhere("upper(a.modul) = '$modul'");
		$sql = $em->andWhere("a.kd_user = '$user'");
		$eks = $em->queryEks($sql);
		$res = $em->fetchObject($eks);
		if($res->status == '1'){
			$sts = true;
		}

		return $sts;
	}
	
	
	public function defReg()
	{
		$date = new Date;
		$_SESSION['RasiojayaPaket2016']['login']['reg'] = $_GET['reg'];
		$this->layout = "null";
	}
	
	public function req_print()
	{
		$this->layout = "null";
		$em = new PgSql;
		
		$cek = $em->getFind("cetak", "*", array("modul = '".$_GET['modul']."'", "control = '1'", "nojnl = '".$_GET['nojnl']."'"));
		if(!empty($cek->nocetak)){
			echo "<script>alert('Transaksi sudah di ajakan Print Ulang');</script>";
			return;
		}
		
		
		$sess  = SESSION;
		$login = $_SESSION[$sess]['login'];
		
		$attr = array();
		$attr['nocetak'] 	= $_GET['modul'].'.'.date("ymdHis").rand(100,999);
		$attr['modul'] 		= $_GET['modul'];
		$attr['nojnl'] 		= $_GET['nojnl'];
		$attr['ket'] 		= $_GET['ket'];
		$attr['control'] 	= 1;
		$attr['chusr'] 		= $login['kd_user'];
		$attr['chtime'] 	= date("Y-m-d H:i:s");
		
		$sql = $em->addInsert('cetak');
		$sql = $em->addAttrIns($attr);
		$qb[] = $sql;
		
		$save = $em->getFlush($qb);
		if($save != false){
			echo "<script>alert('Pengajuan Print Ulang Sukses, #ref ".$attr['nocetak']."');</script>";
		}else{
			echo "<script>alert('Pengajuan Print Ulang Gagal, Ulangi lagi');</script>";
		}
		
	}
	
	public function getAutoNumber($set, $jum)
	{
		$kd = new Kode;
		$pk = $set->dBasePk[0];
		$this->em = $set->em;
		$sql = $this->em->addSelect($pk);
		$sql = $this->em->getFrom($set->dBaseH, 'a');
		$sql = $this->em->getWhere("$pk like '".$set->prefik."%'");
		$sql = $this->em->getMultiSort("$pk desc");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->fetchObject($eks);

		if(!empty($res->$pk)){
			$nom = str_ireplace($set->prefik, "", $res->$pk);
			$nom = (int)$nom;
			$nom = $nom + 1;
			return $set->prefik.$kd->getRight($nom, $jum, 0);
		}else{
			return $set->prefik.$kd->getRight(1, $jum, 0);
		}
	}
	
	public function grid2020()
	{
		$grid = new Grid;
		$ctrl = $_GET['ctrl'];
		$set  = $_SESSION['dataSetting'][$ctrl];
		
		$pk = $set->dBasePk[0];
		$db = new $set->database;
		$this->em = $db;

		if(!empty($set->sqlGrid)){
			$sql = $set->sqlGrid;
		}else{
			$sel = '*';
			if(!empty($set->selGrid)){
				$sel = $set->selGrid;
			}
			$sql = $this->em->addSelect($sel);
			$sql = $this->em->getFrom($set->dBaseH, 'a');
			$sql = $this->em->getWhere("1=1");
			if(!empty($set->where)){
				foreach($set->where as $r){
					$sql = $this->em->andWhere($r);
				}
			}
		}
		$sel = $sql;
		
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		if (!$page) $page = $this->start;
		if (!$rp) $rp = 50;
		$no = (( $page * $rp ) - $rp );
		
		$awal  = $no;
		$akhir = $rp;
		
		$filType = array();
		foreach($set->grid->attr as $rx){
			$idTp = explode(".", $rx['attr']);
			$idTp = $idTp[1];
			$filType[$idTp] = $rx['type'];
		}					
		
		$filDis = array();
		$filSql = array();
		if(!empty($_POST['filter'])){
			$expFil = explode(";", $_POST['filter']);
			foreach($expFil as $k => $r){
				trim($r);
				if(!empty($r)){
						
					$expFil2 = explode("=>", $r);
					$idF = $expFil2[0];
					$vlF = $expFil2[1];

					$type = $filType[$idF];
					if($type == 'date'){
						$filSql[] = "".$idF."::character varying like '%".strtoupper($vlF)."%'";
					}else{
						$filSql[] = "upper(a.".$idF.") like '%".strtoupper($vlF)."%'";
					}
					
					$filDis[$idF] = $vlF;
				}
			}
		}
		
		$sql = $this->em->addSelect($pk);
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->getWhere("1=1");
		foreach($filSql as $r){
			$sql = $this->em->andWhere($r);
		}
		$eks = $this->em->queryEks($sql);
		$tot = $this->em->getNumRows($eks);

		$selTambah = "";
		if(!empty($set->joinDisplay)){
			$selTambah = ', '.$set->joinDisplay['select'];
		}

		$sql = $this->em->addSelect("a.*, a.".$pk." as pk $selTambah");
		$sql = $this->em->getFrom("($sel)", 'a');
		
		foreach($set->joinDisplay['table'] as $idT => $rJT){
			$sql = $this->em->addLeftJoin("(".$rJT['query'].")", $idT, $rJT['fil']);
		}
		
		$sql = $this->em->getWhere("1=1");
		foreach($filSql as $r){
			$sql = $this->em->andWhere($r);
		}
		if(!empty($set->multiSort)){
			$sql = $this->em->getMultiSort($set->multiSort);
		}
		else{
		    $sql = $this->em->getMultiSort($_POST['sortname']." ".$_POST['sortorder']);
		}
		$sql = $this->em->getLimit($awal, $akhir);
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		
		if(!empty($set->joinDisplay)){
			// var_dump($sql);
		}
		
		if($_GET['ctrl'] == 'Rhp'){
			// echo $sql;
		}
		
		$xml = $grid->getGridStartXml();
		$xml.= $grid->getGridPage($page);
		$xml.= $grid->getGridTotal($tot+1);

		$xml.= $grid->getGridStartRows($kode);
		$xml.= $grid->getGridCell("");
		$xml.= $grid->getGridCell("");
		foreach($set->grid->attr as $r){  
			$idAtr = explode(".", $r['attr']);
			$idAtr = $idAtr[1];
			$xml.= $grid->getGridCell("<input type='text' class='_filterGrid2020' id='filter_".$idAtr."' name='filre[".$idAtr."]' style='width: ".($r['width']-10)."px' value='".$filDis[$idAtr]."'>");
		}
		$xml.= $grid->getGridEndRows();
			
		$Func = new DisplayFunc;
		foreach($tmp as $row){
			$no++;
			
			$xml.= $grid->getGridStartRows($kode);
			$xml.= $grid->getGridCell("<input _no='".$no."' type='button' class=_app _iconAct' style='border: none; font-size: 15px' alt='Pilih' _pk='".$row->pk."'>");
			$xml.= $grid->getGridCell($no);
			foreach($set->grid->attr as $r){  
				$idAtr = explode(".", $r['attr']);
				$idAtr = $idAtr[1];
				if(empty($idAtr)){
					$idAtr = $r['attr'];
				}
				
				if($r['type'] == 'date'){
					$row->$idAtr = date("d/m/Y", strtotime($row->$idAtr));
				}
				if($r['type'] == 'numeric'){
					$row->$idAtr = number_format($row->$idAtr, (int)$r['dec']);
				}
				if(!empty($r['DisplayFunc'])){
					$funct = $r['DisplayFunc'];
					$row->$idAtr = $Func->$funct($row->$idAtr);
				}
				
				$xml.= $grid->getGridCell($row->$idAtr);
			}
			$xml.= $grid->getGridEndRows();
		}
		$xml .= $grid->getGridEndXml();

		echo $xml;
		$this->layout = "null";
	}
	
	public function sortGridDet($set, $post)
	{
		$cek = false;
		$sort = array();
		$asal = array();
		$null = array();
		$nom  = 1;
		$nom2 = 1;
		foreach($post['det_sorting'] as $k => $r){
			if(!empty($r)){
				$cek = true;
				$sort[$r] = $k;
			}else{
				$null[$nom2++] = $k;
			}
			$asal[$nom++] = $k;
		}
		
		if($cek == false){
			return;
		}
		
		$jum = count($post['det_sorting']);
		$tmp = array();
		$urt = 1;
		for($i = 1; $i <= $jum; $i++){
			if(!empty($sort[$i])){
				$tmp[$i] = $sort[$i];
			}else{
				$tmp[$i] = $null[$urt];
				$urt++;
			}
		}
		
		$res = array();
		foreach($tmp as $k => $r){
			$id = $asal[$k];
			$res[$id] = $r;
		}
		
		unset($_POST['det_sorting']);
		echo "<script type='text/javascript'>";
		foreach($res as $no => $i){
			foreach($set->detail->attr as $k => $r){
				$id = $r['attr'];
				echo "$('#".$id."_".$no."').val('".$_POST[$id][$i]."');";
			}
		}
		echo "</script>";
		
	}
}