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


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

//Mydefault values
$sql= "SELECT   ast_mst_asset_no,
				ast_mst_asset_type,
				ast_mst_asset_grpcode,
				ast_mst_asset_status 		= COALESCE(ast_mst.ast_mst_asset_status + ' : ' + ast_sts_desc,''),
				ast_mst_asset_shortdesc,
				ast_mst_asset_locn 			= COALESCE(ast_mst.ast_mst_asset_locn + ' : ' + ast_loc_desc,''),		
				ast_mst_rowid = ast_mst.RowID, 
				ast_ls2_meter_id,
				ast_ls2_meter_desc,
				ast_ls2_meter_point,
				ast_ls2_usage_uom,  
				ast_ls2_meter_install_date,
				ast_ls2_usage_date,
				ast_ls2_usage_reading,
				ast_ls2_ltd_usage,
				ast_ls2_avg_usage,
				ast_ls2_max_avg_usage,
				ast_ls2_incr_usage_flag,
				ast_ls2_meter_maximum,
				ast_ls2.audit_user,
				ast_ls2.audit_date,
				ast_ls2.RowID
				
		From 	ast_mst(NOLOCK)


		INNER JOIN		ast_ls2 (NOLOCK)
		ON				ast_mst.site_cd = ast_ls2.site_cd
		AND				ast_mst.RowID = ast_ls2.mst_RowID

		LEFT 
		OUTER
		JOIN	ast_sts (NOLOCK)
		ON		ast_mst.site_cd = ast_sts.site_cd
		AND		ast_mst.ast_mst_asset_status = ast_sts.ast_sts_status

		LEFT 
		OUTER 
		JOIN	ast_loc (NOLOCK)
		ON		ast_mst.site_cd = ast_loc.site_cd
		AND		ast_mst.ast_mst_asset_locn = ast_loc.ast_loc_ast_loc

Where			ast_ls2.site_cd = '".$site_cd."'
And		ast_ls2.RowID = ".$RowID."";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Mydefault values)";
	 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 ($valid) {
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

	//echo $sql;
	returnData($json);	
	
}

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

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

 
?>