<?php
 
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}
//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);
$pemohon = fetch_postget_val('pemohon',$_POST, $_GET);

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_program
	WHERE id_program = ?
");
$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$bayaran_table = "m_bayaran_".$program_List['kod_program'];
$id_bayaran_table = "id_bayaran_".$program_List['kod_program'];
//*****************************************************
// MAIN TABLE AND JOIN TABLE
// main = main table name in the database,
// sub['name'] = join table name
// sub['sub_id'] = join table identifier
// sub['main_id'] = main table identifier
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
$tableArray = array(
	'main' => $bayaran_table,
	'primary_key' => $id_bayaran_table,
	'sub' => array(
		array('name' => 'i_bayaranfasa', 'sub_id' => 'id_bayaranfasa', 'main_id' => 'id_bayaranfasa', 'print'=>1 ),
		array('name' => 'i_bayaranjenis', 'sub_id' => 'id_bayaranjenis', 'main_id' => 'id_bayaranjenis', 'print'=>1 ),
		array('name' => 'i_bayarankat', 'sub_id' => 'id_bayarankat', 'main_id' => 'id_bayarankat', 'print'=>1 ),
		array('name' => 't_pengguna', 'sub_id' => 'id_pengguna', 'main_id' => 'id_sah', 'aka'=>'sah_pengguna', 'print'=>1 ),
		array('name' => 't_pengguna', 'sub_id' => 'id_pengguna', 'main_id' => 'id_daftar', 'aka'=>'daftar_pengguna', 'print'=>1 ),
		array('name' => 'i_daerah', 'sub_id' => 'id_daerah', 'main_id' => 'id_daerah', 'print'=>1 ),
		array('name' => 'i_dun', 'sub_id' => 'id_dun', 'main_id' => 'id_dun', 'print'=>1 ),
		array('name' => 'i_bank', 'sub_id' => 'id_bank', 'main_id' => 'id_bank', 'print'=>1 ),
	),
);
												
//*****************************************************
// DATATABLE COLUMN
// db = column name in the database,
// dt = DataTables column identifier
// table = database table name
// akaFrom = use if and only if there is aka name for table
//*****************************************************
$columns = array(
    array( 'user_defined' => '', 'dt' => 0),
    array( 'db' => 'jum_bayaran', 'dt' => 1, 'table' => $bayaran_table, 'print' => 1 ),
    array( 'db' => 'tahun_bayar', 'dt' => 2, 'table' => $bayaran_table, 'print' => 1 ),
    array( 'db' => 'desk_bayaranfasa', 'dt' => 3, 'table' => 'i_bayaranfasa', 'print' => 1 ),
    array( 'db' => 'tkh_bayar', 'dt' => 4, 'table' => $bayaran_table, 'print' => 1,
			'formatter' => function( $d, $row ) {
				
				return formatDate($d, 'd M Y');
			}
	),
    array( 'db' => 'tkh_daftar', 'dt' => 5, 'table' => $bayaran_table,
			'formatter' => function( $d, $row ) {
				
				return formatDate($d, 'd M Y');
			}
	),
    array( 'db' => 'tkh_sah', 'dt' => 6, 'table' => $bayaran_table ),
    array( 'db' => 'namasah', 'dt' => 7, 'table' => 'sah_pengguna', 'akaFrom' => 'nama'),
    array( 'db' => 'desk_daerah', 'dt' => 8, 'table' => 'i_daerah', 'print' => 1 ),
    array( 'db' => 'desk_bayarankat', 'dt' => 9, 'table' => 'i_bayarankat', 'print' => 1 ),
    array( 'db' => 'desk_bank', 'dt' => 10, 'table' => 'i_bank', 'print' => 1 ),
    array( 'db' => 'no_akaun', 'dt' => 11, 'table' => $bayaran_table, 'print' => 1 ),
    array( 'db' => 'catatan', 'dt' => 12, 'table' => $bayaran_table, 'print' => 1),
    array( 'db' => 'id_pemohon', 'dt' => 13, 'table' => $bayaran_table ),
    array( 'db' => 'id_bank', 'dt' => 14, 'table' => $bayaran_table ),
    array( 'db' => 'id_carabayar', 'dt' => 15, 'table' => $bayaran_table ),
    array( 'db' => 'id_byrkepada', 'dt' => 16, 'table' => $bayaran_table ),
    array( 'db' => 'id_waris', 'dt' => 17, 'table' => $bayaran_table ),
);

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$whereArray = array(
	array($bayaran_table, 'id_pemohon', $pemohon),
);	

//echo SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 1, buildWhereQuery($whereArray));
//echo "whereArray = ".count($whereArray);
//exit(0);
//*****************************************************
// SSP GET JSON
// No need to modify below
//*****************************************************
if($sqlOnly == 1)
{
	//LEAVE EMPTY FOR FUTURE USAGE
}
else
{
	$temp_result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	for($num = 0; $num < count($temp_result['data']); $num++)
	{
		$id_pemohon = $temp_result['data'][$num][13];		
		$id_bank = $temp_result['data'][$num][14];	
		$id_carabayar = $temp_result['data'][$num][15];	
		$id_byrkepada = $temp_result['data'][$num][16];	
		$id_waris = $temp_result['data'][$num][17];	

		if($id_bank == 0 && $id_carabayar == 2)
		{			
			$indicator_Query = "SELECT A.*, B.desk_bank
							  FROM m_pemohon A
							  LEFT JOIN i_bank B ON B.id_bank = A.id_bank
							  WHERE A.id_pemohon = ?";
			$PDO_stmt = $PDO->prepare($indicator_Query);
			$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
			$PDO_stmt->execute();
			$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);	
			
			$temp_result['data'][$num][10] = $pemohon_List['desk_bank'];
			$temp_result['data'][$num][11] = $pemohon_List['no_akaun'];			
		}		
		
		if($id_byrkepada == 2 && $id_waris > 0)
		{			
			$indicator_Query = "SELECT A.nama_waris, A.kpbaru_waris
							  FROM m_waris A
							  WHERE A.id_waris = ?";
			$PDO_stmt = $PDO->prepare($indicator_Query);
			$PDO_stmt->bindValue(1, $id_waris, PDO::PARAM_INT);
			$PDO_stmt->execute();
			$waris_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);	
			
			$temp_result['data'][$num][9] = $temp_result['data'][$num][9].' kepada waris<br>('.$waris_List['nama_waris'].' - '.$waris_List['kpbaru_waris'].')';		
		}
		
	}
	echo json_encode(
		$temp_result
	);
}