<?php
// get these values from your DB.

require_once('config.php');

$error_message;
$valid = true;
$site_cd = $_REQUEST['site_cd'];
$mtr_no = $_REQUEST['mtr_no'];
$stockno = $_REQUEST['stockno'];


	
$sql= "SELECT 	DISTINCT	itm_loc.itm_loc_stk_loc,   
							itm_loc_oh_qty,  	
							Ava_Qty = 	CASE WHEN ( itm_loc.itm_loc_oh_qty - itm_loc_hard_resrv ) >= 0
   								THEN ( itm_loc.itm_loc_oh_qty - itm_loc_hard_resrv )   								
   								ELSE 0 END + COALESCE(itm_rsv_rsv_qty, 0) 											  
   								FROM 		mtr_mst    INNER JOIN 	mtr_ls1  ON				
   								mtr_mst.site_cd = mtr_ls1.site_cd  AND 			
   								mtr_mst.rowid = mtr_ls1.mst_rowid  INNER JOIN 	
   								itm_mst  ON				
   								itm_mst.site_cd = mtr_ls1.site_cd  AND 			
   								itm_mst.itm_mst_stockno = mtr_ls1.mtr_ls1_stockno    INNER JOIN	itm_loc  ON				
   								itm_mst.site_cd = itm_loc.site_cd  AND 			
   								itm_mst.RowID = itm_loc.mst_RowID  LEFT OUTER JOIN itm_rsv  ON 			
   								itm_rsv.site_cd = mtr_mst.site_cd  AND			
   								itm_rsv.itm_rsv_mtr_no = mtr_mst.mtr_mst_mtr_no  AND			
   								itm_rsv.itm_rsv_mtr_lineno = mtr_ls1.mtr_ls1_mtr_lineno   AND			
   								itm_rsv.itm_rsv_itm_loc = itm_loc.itm_loc_stk_loc     
   								WHERE (		itm_loc_inc_ttloh = '1'  AND	itm_loc_lockout4count = '0') 
   								AND itm_mst.site_cd = '".$site_cd."' 
   								AND itm_mst.itm_mst_stockno = '".$stockno."' 
   								AND mtr_mst.mtr_mst_mtr_no = '".$mtr_no."' ";

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

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

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

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

 
?>