<?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);
$catatan = fetch_postget_val("catatan", $_POST, $_GET);
$id_bayarankat = fetch_postget_val("id_bayarankat", $_POST, $_GET);
$tahun = fetch_postget_val("tahun", $_POST, $_GET);
$fasa = fetch_postget_val("fasa", $_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)
	{
		$PDO_stmt = $PDO->prepare("
			SELECT A.*, B.id_daerah
			FROM m_pemohon A
			LEFT JOIN i_dun B ON B.id_dun = A.id_dun
			WHERE A.id_pemohon = ?
		");
		$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', $program_List['jum_bayar'], PDO::PARAM_STR);
		$success_Array[] = array('id_bayaranjenis', 1, PDO::PARAM_STR); //PENGHARGAAN
		//$success_Array[] = array('id_bayarankat', 1, PDO::PARAM_STR);
		$success_Array[] = array('id_bayarankat', $id_bayarankat, PDO::PARAM_STR);
		$success_Array[] = array('id_bayaranfasa', $kawalan_List['id_bayaranfasa_tunai'], 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', $my_details->usr_id, PDO::PARAM_STR);	
		$success_Array[] = array('tkh_bayar', formatDate($tkh_bayar, 'Y-m-d'), PDO::PARAM_STR);
		$success_Array[] = array('tahun_bayar', $kawalan_List['tahun_bayaran_tunai'], PDO::PARAM_STR);
		$success_Array[] = array('id_carabayar', 1, PDO::PARAM_STR);
		$success_Array[] = array('id_byrkepada',1, 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);
		$success_Array[] = array('catatan', $catatan, PDO::PARAM_STR);
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, $success_table, $success_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();
}

?>