<?php
date_default_timezone_set('Asia/Singapore');

require_once('Constants.php') ;
require_once(PATH_LIB . 'AutoLoader.php') ;
require_once(PATH_LIB . 'Util.php') ;
require_once(PATH_LIB . 'Log.php') ;
require_once(PATH_EXCEL . 'PHPExcel.php') ;
require_once(PATH_MESSAGE . 'Message.php') ;
require_once(PATH_CODE . "database/MsSqlDb.php") ;
require_once(PATH_CODE . "database/MySqlDb.php") ;
require_once(PATH_CODE . "database/MySqliDb.php") ;
require_once('ConfigDb.php') ;

function myException($e)
{
	die("Sorry, there is an error and the application can not continue and will be terminated.") ;
	Log::write('[Report]' . $e->getMessage(),"") ;
}
set_exception_handler('myException');
register_shutdown_function('fatalErrorShutdownHandler');

function fatalErrorShutdownHandler()
{
	$last_error = error_get_last();
	if ($last_error['type'] > 0)
		Log::write('[Report]' . $last_error['message'] . " - " . $last_error['file'] . " on line " . $last_error['line']);
}

if (session_id() == "") 
	session_start(); 

/* if (!isset($_SESSION[SE_INIT]) || $_SESSION[SE_INIT] == false ){
    session_regenerate_id();
    $_SESSION[SE_INIT] = true;
	$_SESSION[SE_USERID] = "" ;
	$_SESSION[SE_USERGROUP] = "" ;
	$_SESSION[SE_MENU] = "" ;
	$_SESSION[SE_ORGID] = -1 ;
	$_SESSION[SE_USERKEY] = "" ;
	$_SESSION[SE_ORGNAME] = "" ;
	$_SESSION[SE_REMOTE_IP] = Util::getRemoteIP() ;
} */
if (isset($_SESSION[SE_ID]))
	$oldseid = $_SESSION[SE_ID] ;
else
	$oldseid = "" ;
	
$_SESSION[SE_ID] = session_id() ;

if (!isset($_SESSION[SE_THEME])) {
	$_SESSION[SE_THEME] = "redmond" ;
}
/* if (!isset($_SESSION[SE_DB])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbType() == DbType::MySql)
		$_SESSION[SE_DB] = new MySqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	else if ($config->getDbType() == DbType::MySqli)
		$_SESSION[SE_DB] = new MySqliDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	else
		$_SESSION[SE_DB] = new MsSqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	unset($config) ;
}
if (!isset($_SESSION[SE_DB_SUN])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbTypeSun() == DbType::MySql)
		$_SESSION[SE_DB_SUN] = new MySqlDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun(),$config->getPortNoSun()) ;
	else if ($config->getDbTypeSun() == DbType::MySqli)
		$_SESSION[SE_DB_SUN] = new MySqliDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun(),$config->getPortNoSun()) ;
	else
		$_SESSION[SE_DB_SUN] = new MsSqlDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun(),$config->getPortNoSun()) ;
    
    $_SESSION[SE_DB_BU_SUN] = $config->getBusinessUnitSun();
	unset($config) ;
} */

if (isset($_GET['c']))
	$class = $_GET['c'] ;
else
	$class = "" ;
$page = "" ;	
if ($class !=""){
	if (isset($_SESSION[SE_USERID]) && $_SESSION[SE_USERID] != "" ){
		$page = Util::revertLink($class,$_SESSION[SE_ID]) ;
	}
	if (class_exists($page)) {
		$params = array() ;
		$params['type'] = 'r' ;
		if (isset($_GET['termType']))
			$params['termType'] = $_GET['termType'] ;
		if (isset($_GET['appType']))
			$params['appType'] = $_GET['appType'] ;
		if (isset($_GET['language']))
			$params['language'] = $_GET['language'] ;
		if (isset($_GET['m']))
			$params['month'] = $_GET['m'] ;
		if (isset($_GET['pr']))
			$params['pr'] = $_GET['pr'] ;
		if (isset($_GET['y']))
			$params['year'] = $_GET['y'] ;
		if (isset($_GET['co']))
			$params['coy'] = $_GET['co'] ;
		if (isset($_GET['dp']))
			$params['dept'] = $_GET['dp'] ;
		if (isset($_GET['dt']))
			$params['date'] = $_GET['dt'] ;
		if (isset($_GET['dtend']))
			$params['dateend'] = $_GET['dtend'] ;
		if (isset($_GET['dtapp']))
			$params['dateapp'] = $_GET['dtapp'] ;
		if (isset($_GET['dtappend']))
			$params['dateappend'] = $_GET['dtappend'] ;
		if (isset($_GET['t']))
			$params['type'] = $_GET['t'] ;
		if (isset($_GET['empIdBegin']))
			$params['empIdBegin'] = $_GET['empIdBegin'] ;
		if (isset($_GET['empIdEnd']))
			$params['empIdEnd'] = $_GET['empIdEnd'] ;
		if (isset($_GET['reporttype']))
			$params['reporttype'] = $_GET['reporttype'] ;
		if (isset($_GET['claim_group']))
			$params['claim_group'] = $_GET['claim_group'] ;
		if (isset($_GET['exptype']))
			$params['exptype'] = $_GET['exptype'];
		if (isset($_GET['fn']))
			$params['fn'] = $_GET['fn'];
		if (isset($_GET['tn']))
			$params['tn'] = $_GET['tn'];
		if (isset($_GET['id']))
			$params['id'] = $_GET['id'];
		if (isset($_GET['report_status']))
			$params['report_status'] = $_GET['report_status'];
		if (isset($_GET['export_type']))
			$params['export_type'] = $_GET['export_type'];
		if (isset($_GET['external_system']))
			$params['external_system'] = $_GET['external_system'];
		if (isset($_GET['claim_group']))
			$params['claim_group'] = $_GET['claim_group'];
		if (isset($_GET['batch_no']))
			$params['batch_no'] = $_GET['batch_no'];
		if (isset($_GET['value_date']))
			$params['value_date'] = $_GET['value_date'];
		if (isset($_GET['status']))
			$params['status'] = $_GET['status'];
		//add siti 20/04/2020
		if (isset($_GET['agentid']))
			$params['agentid'] = $_GET['agentid'];
		if (isset($_GET['custid']))
			$params['custid'] = $_GET['custid'];
		if (isset($_GET['jobno']))
			$params['jobno'] = $_GET['jobno'];
		if (isset($_GET['jobtype']))
			$params['jobtype'] = $_GET['jobtype'];
		if (isset($_GET['fdate']))
			$params['fdate'] = $_GET['fdate'];
		if (isset($_GET['tdate']))
			$params['tdate'] = $_GET['tdate'];
		if (isset($_GET['fmonth']))
			$params['fmonth'] = $_GET['fmonth'];
		if (isset($_GET['fyear']))
			$params['fyear'] = $_GET['fyear'];
		if (isset($_GET['tmonth']))
			$params['tmonth'] = $_GET['tmonth'];
		if (isset($_GET['tyear']))
			$params['tyear'] = $_GET['tyear'];
		if (isset($_GET['sorts']))
			$params['sorts'] = $_GET['sorts'];
		//end siti 20/04/2020
		$called = call_user_func_array(array(new $page,"processRequest"),array($params)) ;
		if ($called == FALSE) {
			echo "<h2>invalid request - call fail</h2>" ;
			Log::write("[Report]Calling class failed : " . $page . " failed.",$page) ;
		}
	} else{
		echo "<h2>invalid request - class/module not found. </h2> ";
		Log::write("[Report]Class not found - " . $class . ", page : " . $page) ;
	}
} else {
	echo "<h2>invalid request - missing class/module info.</hf>" ;
	Log::write("[Report]Missing class name.",$class) ;
}

?>