<?php
require_once('config.php');
//require_once('f_update_budget.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$site_cd = $_REQUEST['site_cd'];
$ntf_log_to =$_REQUEST['ntf_log_to']; 
$RowID =$_REQUEST['RowID']; 


$sql_ntf_log = "UPDATE ntf_log SET ntf_log_read_flag = ? WHERE RowID = ?";
$params_ntf_log = array('1',$RowID);
$stmt_update_ntf_log = sqlsrv_query( $conn, $sql_ntf_log,$params_ntf_log);	
		
if( !$stmt_update_ntf_log ) {
	$error_message = "Error update table (ntf_log)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt( $stmt_update_ntf_log);


$sql_dvc_mst = "SELECT * FROM ntf_log (NOLOCK)  WHERE site_cd = ? and ntf_log_to = ? and RowID = ? order by ntf_log_send_date DESC";

		$params_dvc_mst = array($site_cd,$ntf_log_to,$RowID);	
		$stmt_dvc_mst = sqlsrv_query( $conn, $sql_dvc_mst,$params_dvc_mst);	
		
	if( !$stmt_dvc_mst ) {
		$error_message = "Error Select table (ntf_log)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	$json= array();
	
	do {
		 while ($row = sqlsrv_fetch_array($stmt_dvc_mst, SQLSRV_FETCH_ASSOC)) {
		 
		 $json[] = $row;		
			
			 
	   }
	} while ( sqlsrv_next_result($stmt_dvc_mst));
	
	
	if( $stmt_dvc_mst) {
		 sqlsrv_commit( $conn );
		 sqlsrv_close( $conn);	
		 returnData($json);	
	} else {
		 sqlsrv_rollback( $conn );
		 $error_message = "Transaction rolled back.<br />";
		 returnError($error_message);
	} 


function returnData($json){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'Alert',
	'message' => $error_message,
	'data' => $json);	
	
	echo json_encode($returnData);
		
	exit();
}
 
?>