<?php

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

$num_errors = 0;
$pemohon = fetch_postget_val("pemohon", $_POST, $_GET);
$program = fetch_postget_val("program", $_POST, $_GET);
$dun = fetch_postget_val("dun", $_POST, $_GET);

if($dun == 0)
{
	$indicator_Query = "SELECT *
					  FROM m_pemohon
					  WHERE id_pemohon = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $pemohon, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$pemohon_List['m_pemohon'] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	
	$dun = $pemohon_List['m_pemohon']['id_dun'];	
}

$daerah = fetch_postget_val("daerah", $_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);
$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();

	$success_Array = array();
	$success_Array[] = array('id_pemohon', $pemohon, 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); //Pembayaran Secara EFT
	$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', $dateTimeNow, 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', $dun, PDO::PARAM_STR);
	$success_Array[] = array('id_daerah', $daerah, 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();
}

?>