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

$success_table = "m_bayaran_KH";
$jum_bayaran = 1000;

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_daerah, C.id_waris, C.id_bankwaris, C.no_akaunwaris
						  FROM m_pemohon A
						  LEFT JOIN i_dun B ON B.id_dun = A.id_dun
						  LEFT JOIN m_waris C ON C.id_pemohon = A.id_pemohon
						  WHERE A.id_pemohon = ?
						  AND C.status = 1";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $result, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		$success_Array = array();
		$success_Array[] = array('id_pemohon', $result, PDO::PARAM_STR);
		$success_Array[] = array('jum_bayaran', $jum_bayaran, PDO::PARAM_STR);
		$success_Array[] = array('id_bayaranjenis', 2, PDO::PARAM_STR); //KHAIRAT		
		$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_byrkepada', 2, PDO::PARAM_STR);
		$success_Array[] = array('id_waris', $pemohon_List['id_waris'], PDO::PARAM_STR);
		$success_Array[] = array('id_bank', $pemohon_List['id_bankwaris'], PDO::PARAM_STR);
		$success_Array[] = array('no_akaun', $pemohon_List['no_akaunwaris'], PDO::PARAM_STR);
		$success_Array[] = array('id_dun', $pemohon_List['id_dun'], PDO::PARAM_STR);
		$success_Array[] = array('id_daerah', $pemohon_List['id_daerah'], PDO::PARAM_STR);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, $success_table, $success_Array);
		
		$bayar_Array = array();
		$bayar_Array[] = array('flag_bayarkhairat', 1, PDO::PARAM_STR);		
		$PASS_VALUE += 1;
		$whereCondition = "WHERE id_pemohon = ".$result;
		$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $bayar_Array, $whereCondition);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $result, 34, $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, $result['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();
}

?>