<?php

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

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

try
{
	$indicator_Query = "SELECT A.*
					  FROM m_bayaran_AE A
					  WHERE A.id_anakemas = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$anakemas_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

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

	$indicator_Array = array();
	$indicator_Array[] = array('id_bank', fetch_postget_val("id_bank", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('no_akaun', fetch_postget_val("no_akaun", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_carabayar', fetch_postget_val("id_carabayar", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_bayarankat', fetch_postget_val("id_bayarankat", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_bayaranfasa', fetch_postget_val("id_bayaranfasa", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_bayar', formatDate(fetch_postget_val("tkh_bayar", $_POST, $_GET), "Y-m-d"), PDO::PARAM_STR);
	$indicator_Array[] = array('catatan', $catatan, PDO::PARAM_STR,1);
	$indicator_Array[] = array('flag_gantian', 1, PDO::PARAM_STR);
	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_anakemas = $id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_bayaran_AE', $indicator_Array, $whereCondition);

	$gantian_Array = array();
	$gantian_Array[] = array('id_bankanakemas', fetch_postget_val("id_bank", $_POST, $_GET), PDO::PARAM_STR);
	$gantian_Array[] = array('no_akaunanakemas', fetch_postget_val("no_akaun", $_POST, $_GET), PDO::PARAM_STR);
	$gantian_Array[] = array('flag_gantian', 1, PDO::PARAM_STR);
	$gantian_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$gantian_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_anakemas = $id";
	//$PASS_COUNTER += PDO_UPDATE($PDO, 'm_anakemas', $gantian_Array, $whereCondition);
	$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, 'm_anakemas', $gantian_Array, $whereCondition, $anakemas_List['id_pemohon'], $id, $my_details->usr_id, $dateTimeNow, 0, $id);
		
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $anakemas_List['id_pemohon'], 39, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, $catatan, $id, '', $my_details->user_parlimen, 5);

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

?>