<?php

if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id_pemohon = fetch_postget_val("id", $_POST, $_GET);
$id_perakuan = fetch_postget_val("id_perakuan", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

//get program from pemohon
$indicator_Query = "SELECT *
					FROM m_pemohon
					WHERE 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);

$id_statusmohon = 4;
$ulasan_saheft = '';
$flag_saheft = 1;
$log = 9;
if($id_perakuan == 2)
{
	$id_statusmohon = $pemohon_List['id_statusmohon'];
	$ulasan_saheft = fetch_postget_val("ulasan_saheft", $_POST, $_GET);
	$flag_saheft = 0;
	$log = 12;	
}

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

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

	$indicator_Array = array();
	$indicator_Array[] = array('id_statusmohon', $id_statusmohon, PDO::PARAM_INT);
	$indicator_Array[] = array('ulasan_saheft', $ulasan_saheft, PDO::PARAM_STR);
	$indicator_Array[] = array('flag_saheft', $flag_saheft, PDO::PARAM_INT);
	if($id_perakuan == 1)
	{
		$indicator_Array[] = array('id_saheft', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_saheft', $dateTimeNow, PDO::PARAM_STR);
	}
	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$whereCondition = "WHERE id_pemohon = $id_pemohon";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $indicator_Array, $whereCondition);

	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, $log, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, $ulasan_saheft, '', '', $my_details->user_parlimen, $pemohon_List['id_program']);

	//*****************************************
	//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();
}

?> 

	





