<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id = fetch_postget_val("id", $_POST, $_GET);
$id_program = fetch_postget_val("id_program", $_POST, $_GET);
$type = fetch_postget_val('type',$_POST, $_GET);
$id_bayaran_KH = fetch_postget_val('id_bayaran_KH',$_POST, $_GET);
$id_khairatkat = fetch_postget_val("id_khairatkat", $_POST, $_GET);
$tkh_mati = formatDate(fetch_postget_val("tkh_mati", $_POST, $_GET), "Y-m-d");
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT A.*
				  FROM m_pemohon A
				  WHERE A.id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$tkh_daftar = $pemohon_List['tkh_daftar'];

if ($id_khairatkat == 1 && $tkh_daftar > $tkh_mati)
{
	echo -999;
	exit(0);	
}

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 0; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();

	$log = '19';
	
	$indicator_Array = array();
	$indicator_Array[] = array('id_khairatkat', fetch_postget_val("id_khairatkat", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('no_sijilmati', fetch_postget_val("no_sijilmati", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_mati', formatDate(fetch_postget_val("tkh_mati", $_POST, $_GET), "Y-m-d"), PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_mati', fetch_postget_val("ulasan_mati", $_POST, $_GET), PDO::PARAM_STR,1);
	
	if($type == 0)
	{
		$indicator_Array[] = array('id_statushidup', 2, PDO::PARAM_STR);
		$indicator_Array[] = array('id_sahmati', 1, PDO::PARAM_STR);
		$indicator_Array[] = array('flag_querymati', 0, PDO::PARAM_STR);
		$indicator_Array[] = array('ulasan_sahmati', '', PDO::PARAM_STR);
		$indicator_Array[] = array('id_keyinmati', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_keyinmati', $dateTimeNow, PDO::PARAM_STR);
	}
	
	if($type == 1)
	{
		$indicator_Array[] = array('id_kemaskinimati', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_kemaskinimati', $dateTimeNow, PDO::PARAM_STR);	
	
		if($pemohon_List['id_sahmati'] == 2 && $id_bayaran_KH == 0)
		{
			$log = '42';
				
			$indicator_Array[] = array('id_sahmati', 1, PDO::PARAM_STR);
		}	
	}
	
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_pemohon = $id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $indicator_Array, $whereCondition);
	
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id, $log, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, fetch_postget_val("ulasan_mati", $_POST, $_GET), '', '', $my_details->user_parlimen, $id_program);

	if($type == 1 && $id_bayaran_KH > 0)
	{
		$khairat_Array = array();
		$khairat_Array[] = array('id_bayarankat', fetch_postget_val("id_bayarankat", $_POST, $_GET), PDO::PARAM_STR);
		$khairat_Array[] = array('id_bayaranfasa', fetch_postget_val("id_bayaranfasa", $_POST, $_GET), PDO::PARAM_STR);	
		$khairat_Array[] = array('id_carabayar', fetch_postget_val("id_carabayarKH", $_POST, $_GET), PDO::PARAM_STR);	
		$khairat_Array[] = array('id_bank', fetch_postget_val("id_bankKH", $_POST, $_GET), PDO::PARAM_STR);	
		$khairat_Array[] = array('no_akaun', fetch_postget_val("no_akaunKH", $_POST, $_GET), PDO::PARAM_STR);	
		$khairat_Array[] = array('tkh_bayar', formatDate(fetch_postget_val("tkh_bayar", $_POST, $_GET), "Y-m-d"), PDO::PARAM_STR);
		$khairat_Array[] = array('no_eft', fetch_postget_val("no_eft", $_POST, $_GET), PDO::PARAM_STR);	
		$khairat_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
		$khairat_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);	
		$PASS_VALUE += 1;
		$whereCondition = "WHERE id_bayaran_KH = $id_bayaran_KH";
		$PASS_COUNTER += PDO_UPDATE($PDO, 'm_bayaran_KH', $khairat_Array, $whereCondition);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id, 38, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, '', '', '', $my_details->user_parlimen, $id_program);
	}	
	
	$kpbaru_waris = fetch_postget_val("kpbaru_waris", $_POST, $_GET);
	
	if($kpbaru_waris != '')
	{	
		$indicator_Query = "SELECT *
							FROM m_waris
							WHERE id_pemohon = ?
							AND status = 1";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$waris_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		$id_waris = $waris_List['id_waris'];
		
		$waris_Array = array();
		$waris_Array[] = array('nama_waris', fetch_postget_val("nama_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('kpbaru_waris', fetch_postget_val("kpbaru_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris', fetch_postget_val("alamat_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris2', fetch_postget_val("alamat_waris2", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris3', fetch_postget_val("alamat_waris3", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('id_poskodwaris', fetch_postget_val("id_poskodwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('bandar_waris', fetch_postget_val("bandar_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('id_negeriwaris', fetch_postget_val("id_negeriwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('tel_hpwaris', fetch_postget_val("tel_hpwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('tel_rumahwaris', fetch_postget_val("tel_rumahwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('emel_waris', fetch_postget_val("emel_waris", $_POST, $_GET), PDO::PARAM_STR,1);
		$waris_Array[] = array('id_bankwaris', fetch_postget_val("id_bankwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('no_akaunwaris', fetch_postget_val("no_akaunwaris", $_POST, $_GET), PDO::PARAM_STR);
		
		if($id_waris > 0)
		{	
			if($waris_List['kpbaru_waris'] == $kpbaru_waris)
			{
				$PASS_VALUE += 1;
				$whereCondition = "WHERE id_waris = $id_waris";
				$PASS_COUNTER += PDO_UPDATE($PDO, 'm_waris', $waris_Array, $whereCondition);	
			}
			else
			{			
				$updatewaris_Array = array();
				$updatewaris_Array[] = array('status', 0, PDO::PARAM_STR);
				$PASS_VALUE += 1;
				$whereCondition = "WHERE id_waris = $id_waris";
				$PASS_COUNTER += PDO_UPDATE($PDO, 'm_waris', $updatewaris_Array, $whereCondition);	
				
				$waris_Array[] = array('status', 1, PDO::PARAM_STR);
				$waris_Array[] = array('id_pemohon', $id, PDO::PARAM_STR);
				$PASS_VALUE += 1;
				$PASS_COUNTER += PDO_INSERT($PDO, 'm_waris', $waris_Array);			
			}
		}
		else
		{
			$waris_Array[] = array('status', 1, PDO::PARAM_STR);
			$waris_Array[] = array('id_pemohon', $id, PDO::PARAM_STR);
			$PASS_VALUE += 1;
			$PASS_COUNTER += PDO_INSERT($PDO, 'm_waris', $waris_Array);
		}
	}

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>