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


$error_message;
$valid = true;

//$site_cd = $_REQUEST['site_cd'];
$mst_RowID = $_REQUEST['mst_RowID'];

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


$sql="SELECT 		ast_ls1.site_cd, 
					ast_ls1.ast_ls1_stock_no, 
					ast_ls1.mst_RowID,			 
					ast_ls1.ast_ls1_qty_needed,   
					ast_ls1.ast_ls1_desc,   				
					ast_ls1.ast_ls1_varchar1,	
					itm_mst.itm_mst_desc,    
					itm_mst.itm_mst_ttl_oh,
					ast_ls1.audit_user,   
					ast_ls1.audit_date, 				  
					ast_ls1.RowID
		FROM 		ast_ls1 (NOLOCK)
		
		LEFT 
		OUTER 
		JOIN 		itm_mst (NOLOCK)
		ON			itm_mst.site_cd = ast_ls1.site_cd
		AND			itm_mst.itm_mst_stockno = ast_ls1.ast_ls1_stock_no
		WHERE 	( ast_ls1.mst_RowID = ? )";	
				
	//echo $sql;
	$params = array($mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql,$params);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (itm_mst)";
				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);
	  
/* if(empty(json_decode($json1,1))) {
     $returnData = array(
	'status' => 'Alert',
	'message' => 'No Records found',
	'data' => $json);
}else{
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);
} */
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);

	echo json_encode($returnData);
}

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

 
?>