<?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_usg.site_cd,   
				ast_usg.mst_RowID,   
				ast_usg.ast_usg_meter_id ,
				ast_usg.ast_usg_usage_reading,   
				ast_usg.ast_usg_usage_uom,   
				ast_usg.ast_usg_usage_date,   
				ast_usg.ast_usg_org_ltd_usage,   
				ast_usg.ast_usg_new_ltd_usage,   
				ast_usg.ast_usg_wo_no,
				ast_ls2.ast_ls2_old_ltd_usage,
				ast_ls2.ast_ls2_incr_usage_flag,
				wko_mst.wko_mst_descs,
				wko_mst.wko_mst_status,
				ast_usg.audit_user,   
				ast_usg.audit_date,   
				ast_usg.column1,   
				ast_usg.column2,   
				ast_usg.column3,   
				ast_usg.column4,   
				ast_usg.column5,   
				ast_usg.RowID,
				usage = CASE When ast_ls2_incr_usage_flag = 'N' Then ast_usg.ast_usg_new_ltd_usage
							 When ast_usg.ast_usg_new_ltd_usage - ast_usg.ast_usg_org_ltd_usage <= 0 Then 0
							 Else ast_usg.ast_usg_new_ltd_usage -  ast_usg.ast_usg_org_ltd_usage End
FROM 		ast_usg (NOLOCK)

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

LEFT OUTER JOIN wko_mst (NOLOCK)
ON			( ast_usg.site_cd = wko_mst.site_cd )
AND			( ast_usg.ast_usg_wo_no = wko_mst.wko_mst_wo_no )

WHERE	( ast_usg.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){
	
	$json1 = json_encode($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, $error_message){
	$json = array();
	
	$returnData = array(
	'status' => $error,
	'message' => $error_message,	
	'data' => '');
	
	echo json_encode($returnData);
	exit();
		
}
		

 
?>