<?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_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') ;

require '../vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

function myException($e)
{
	Log::write('[Report]' . $e->getMessage(),"") ;
	die("Sorry, there is an error and the application can not continue and will be terminated.") ;
}
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_regenerate_id();
    $_SESSION[SE_INIT] = true;
	$_SESSION[SE_USERID] = "" ;
	$_SESSION[SE_USERGROUP] = "" ;
	$_SESSION[SE_MENU] = "" ;
	$_SESSION[SE_ORGID] = -1 ;
	$_SESSION[SE_ORGNAME] = "" ;
	$_SESSION[SE_USERKEY] = "";
}
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()) ;
//else
//$_SESSION[SE_DB] = new MsSqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword()) ;
//unset($config) ;
//}

//Log::write($_GET);
if (isset($_GET['c']))
	$class = $_GET['c'] ;
else
	$class = "" ;
$page = "" ;

if ($class !=""){
	if (isset($_SESSION[SE_USERID]) && $_SESSION[SE_USERID] != "" ){
		$page = Util::revertLink($class) ;

	}

	if (class_exists($page)) {
		$params = array() ;
		$params['type'] = 'r' ;

		if (isset($_GET['t']))
			$params['type'] = $_GET['t'];

		if (isset($_GET['bu']))
			$params['bu'] = $_GET['bu'];

		if (isset($_GET['att']))
			$params['account_type_text'] = $_GET['att'];

		if (isset($_GET['it']))
			$params['invoice_type'] = $_GET['it'];

		if (isset($_GET['ad']))
			$params['aging_date'] = $_GET['ad'];

		if (isset($_GET['pe']))
			$params['period_end'] = $_GET['pe'];

		if (isset($_GET['at']))
			$params['account_type'] = $_GET['at'];

		if (isset($_GET['aci']))
			$params['account_id'] = $_GET['aci'];

		if (isset($_GET['acc']))
			$params['account_code'] = $_GET['acc'];

		if (isset($_GET['acf']))
			$params['account_code_from'] = $_GET['act'];

		if (isset($_GET['act']))
			$params['account_code_to'] = $_GET['act'];

		if (isset($_GET['jt']))
			$params['journal_type'] = $_GET['jt'];

		if (isset($_GET['pf']))
			$params['period_from'] = $_GET['pf'];

		if (isset($_GET['pt']))
			$params['period_to'] = $_GET['pt'];

		if (isset($_GET['jn']))
			$params['journal_no'] = $_GET['jn'];

		if (isset($_GET['jnt']))
			$params['journal_no_to'] = $_GET['jnt'];

		if (isset($_GET['held_jn']))
			$params['held_journal'] = $_GET['held_jn'];

		if (isset($_GET['exception']))
			$params['exception'] = $_GET['exception'] ;

		if (isset($_GET['wo_attachment']))
			$params['wo_attachment'] = $_GET['wo_attachment'] ;

		if (isset($_GET['dty']))
			$params['date_type'] = $_GET['dty'];

		if (isset($_GET['df']))
			$params['date_from'] = $_GET['df'];

		if (isset($_GET['dt']))
			$params['date_to'] = $_GET['dt'];

		if (isset($_GET['rt']))
			$params['rate_type'] = $_GET['rt'];

		if (isset($_GET['cf']))
			$params['currency_from'] = $_GET['cf'];

		if (isset($_GET['ct']))
			$params['currency_to'] = $_GET['ct'];

		if (isset($_GET['spf']))
			$params['supplier_from'] = $_GET['spf'];

		if (isset($_GET['spt']))
			$params['supplier_to'] = $_GET['spt'];

		if (isset($_GET['csf']))
			$params['customer_from'] = $_GET['csf'];

		if (isset($_GET['cst']))
			$params['customer_to'] = $_GET['cst'];

		if (isset($_GET['anf']))
			$params['anlcode_from'] = $_GET['anf'];

		if (isset($_GET['ant']))
			$params['anlcode_to'] = $_GET['ant'];

		if (isset($_GET['tbl']))
			$params['table_name'] = $_GET['tbl'];

		if (isset($_GET['col']))
			$params['table_column'] = $_GET['col'];

		if (isset($_GET['row']))
			$params['table_row'] = $_GET['row'];

		if (isset($_GET['trf']))
			$params['trans_ref'] = $_GET['trf'];

		if (isset($_GET['piv']))
			$params['proforma_invoice_detail'] = $_GET['piv'];

		if (isset($_GET['select']))
			$params['select'] = $_GET['select'];

		if (isset($_GET['cat']))
			$params['category'] = $_GET['cat'];

		if (isset($_GET['cat_to']))
			$params['category_to'] = $_GET['cat_to'];

		if (isset($_GET['loc']))
			$params['location'] = $_GET['loc'];

		if (isset($_GET['loc_to']))
			$params['location_to'] = $_GET['loc_to'];

		if (isset($_GET['cdf']))
			$params['code_from'] = $_GET['cdf'];

		if (isset($_GET['cdt']))
			$params['code_to'] = $_GET['cdt'];

		//PNL STATEMENT REPORT
		if (isset($_GET['period']))
			$params['period'] = $_GET['period'];

		if (isset($_GET['department']))
			$params['department'] = $_GET['department'];

		if (isset($_GET['anlcoderange_from']))
			$params['anlcoderange_from'] = $_GET['anlcoderange_from'];

		if (isset($_GET['anlcoderange_to']))
			$params['anlcoderange_to'] = $_GET['anlcoderange_to'];

		if (isset($_GET['fa']))
			$params['fixed_assets'] = $_GET['fa'];

		// if (isset($_GET['cate']))
		// 	$params['cate'] = $_GET['cate'];

		if (isset($_GET['cost_fr']))
			$params['cost_from'] = $_GET['cost_fr'];

		if (isset($_GET['cost_to']))
			$params['cost_to'] = $_GET['cost_to'];

		if (isset($_GET['detail_id']))
			$params['detail_id'] = $_GET['detail_id'];

		if (isset($_GET['line_no']))
			$params['line_no'] = $_GET['line_no'];
		//========
			//$params['trans_ref'] = $_GET['trf'];

		if (isset($_GET['stt']))
			$params['status'] = $_GET['stt'];

		if (isset($_GET['user']))
			$params['user'] = $_GET['user'];

		if (isset($_GET['user_to']))
			$params['user_to'] = $_GET['user_to'];

		if (isset($_GET['val']))
			$params['val'] = $_GET['val'];

		if (isset($_GET['cfr']))
			$params['credit_from'] = $_GET['cfr'];

		if (isset($_GET['cto']))
			$params['credit_to'] = $_GET['cto'];

		$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) ;
}

?>