<?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']; 


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

		$params_dvc_mst = array($site_cd,$ntf_log_to);	
		$stmt_dvc_mst = sqlsrv_query( $conn, $sql_dvc_mst,$params_dvc_mst);	
		
	if( !$stmt_dvc_mst ) {
		$error_message = "Error Select table (dvc_mst)";
		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();
}
 
?>