<?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);
$tkh_bayar = fetch_postget_val("tkh_bayar", $_POST, $_GET);
$tahun = fetch_postget_val("tahun", $_POST, $_GET);
$fasa = fetch_postget_val("fasa", $_POST, $_GET);
$id_bayaranfasa = fetch_postget_val("id_bayaranfasa", $_POST, $_GET);
$program = fetch_postget_val("program", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

$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);
$eft_table = "m_bayaran_".$program_List['kod_program']."_EFT";
$success_table = "m_bayaran_".$program_List['kod_program'];

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

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

	$allIds = explode(',', $id);
	foreach($allIds as $result)
	{
		$indicator_Query = "SELECT A.*,B.id_pemohon,B.id_dun,C.id_daerah
						  FROM m_anakemas A
						  LEFT JOIN m_pemohon B ON B.id_pemohon = A.id_pemohon
						  LEFT JOIN i_dun C ON C.id_dun = B.id_dun
						  WHERE A.id_anakemas = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $result, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$anakemas_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		$success_Array = array();
		$success_Array[] = array('id_pemohon', $anakemas_List['id_pemohon'], PDO::PARAM_STR);
		$success_Array[] = array('id_anakemas', $result, PDO::PARAM_STR);
		$success_Array[] = array('jum_bayaran', $program_List['jum_bayar'], PDO::PARAM_STR);
		$success_Array[] = array('id_bayaranjenis', 1, PDO::PARAM_STR); //PENGHARGAAN		
		$success_Array[] = array('id_bayarankat', 6, PDO::PARAM_STR); //Pembayaran Secara EFT
		$success_Array[] = array('id_bayaranfasa', $id_bayaranfasa, PDO::PARAM_STR);
		$success_Array[] = array('id_daftar', $my_details->usr_id, PDO::PARAM_STR);
		$success_Array[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR);
		$success_Array[] = array('id_sah', $my_details->usr_id, PDO::PARAM_STR);
		$success_Array[] = array('tkh_sah', $dateTimeNow, PDO::PARAM_STR);
		$success_Array[] = array('id_sahbayar', 2, PDO::PARAM_STR);		
		$success_Array[] = array('tkh_bayar', formatDate($tkh_bayar, 'Y-m-d'), PDO::PARAM_STR);
		$success_Array[] = array('id_carabayar', 2, PDO::PARAM_STR);
		$success_Array[] = array('tahun_bayar', $tahun, PDO::PARAM_STR);
		$success_Array[] = array('id_bank', $anakemas_List['id_bankanakemas'], PDO::PARAM_STR);
		$success_Array[] = array('no_akaun', $anakemas_List['no_akaunanakemas'], PDO::PARAM_STR);
		$success_Array[] = array('id_dun', $anakemas_List['id_dun'], PDO::PARAM_STR);
		$success_Array[] = array('id_daerah', $anakemas_List['id_daerah'], PDO::PARAM_STR);		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, $success_table, $success_Array);		
		
		$indicator_Array = array();
		$indicator_Array[] = array('flag_bayaran', 1, PDO::PARAM_INT);
		$PASS_VALUE += 1;
		$whereCondition = "WHERE id_anakemas = $result";
		$PASS_COUNTER += PDO_UPDATE($PDO, 'm_anakemas', $indicator_Array, $whereCondition);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $anakemas_List['id_pemohon'], 33, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, '', $result, '', $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();
}

?>