<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;

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



$json = file_get_contents('php://input');
//to debug the json values;
//file_put_contents("data.json", $json)
// Converts it into a PHP object
$data = json_decode($json, true);

$site_cd = $data['site_cd'];
$prm_ast_grp_cd = $data['prm_ast_grp_cd'];
$prm_ast_wo_no = $data['prm_ast_wo_no'];



$sql="SELECT	prm_ast.site_cd,
				prm_ast.prm_ast_grp_cd,
				ast_mst.ast_mst_asset_no,
				ast_mst.ast_mst_asset_shortdesc,
				ast_mst.ast_mst_asset_type,
				ast_mst.ast_mst_asset_status,
				ast_mst.ast_mst_work_area,    
				ast_mst.ast_mst_asset_locn,   
				ast_mst.ast_mst_cost_center
		FROM 	prm_ast (NOLOCK), 
				ast_mst (NOLOCK)   
		WHERE	prm_ast.site_cd = ast_mst.site_cd
		AND		prm_ast.prm_ast_asset_no = ast_mst.ast_mst_asset_no
		AND		prm_ast.site_cd = '".$site_cd."'
		AND		prm_ast.prm_ast_grp_cd = '".$prm_ast_grp_cd."'
		AND		prm_ast.prm_ast_wo_no = '".$prm_ast_wo_no."'";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (prm_ast)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}
$json = array();
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
	 		
		$json[] = $row;
	
     }
} while ( sqlsrv_next_result($stmt) );







if( $stmt ) {
		 sqlsrv_commit( $conn );
		 sqlsrv_close( $conn);	
		 returnData($json);	
	} else {
		 sqlsrv_rollback( $conn );
		 $error_message = "Transaction rolled back.<br />";
		 returnError($error_message);
	} 



function returnData($json){
	 $json1 = json_encode($json);
	 //echo$json;	 
	 if(empty(json_decode($json1,1))) {
     $returnData = array(	 
	'status' => 'SUCCESS',
	'message' => 'No Records found ',	
	'data' => $json);
}else{
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);
} 	
	
	echo json_encode($returnData);
}

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

 
?>