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

require_once('config.php');

$error_message;
$valid = true;

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

	
$sql= "select mtr_ls1.mst_RowID, 
		mtr_ls1.mtr_ls1_stockno,
		mtr_ls1.mtr_ls1_stk_locn,
		mtr_ls1.mtr_ls1_desc,
		mtr_ls1.mtr_ls1_req_qty,
		mtr_ls1.mtr_ls1_rcv_qty,
		mtr_ls1.mtr_ls1_mtr_lineno,
		mtr_ls1.mtr_ls1_chg_account,
		mtr_ls1.mtr_ls1_chg_costcenter,
		mtr_ls1.mtr_ls1_mtl_uom,
		mtr_ls1.mtr_ls1_varchar1,
		mtr_mst.mtr_mst_requester,
		mtr_ls1.RowID,
		itm_loc.itm_loc_oh_qty,
		issue_qty = COALESCE('','')
		
		
from	mtr_mst (NOLOCK),
		mtr_ls1 (NOLOCK),
		itm_mst (NOLOCK),		
		itm_loc (NOLOCK)
		
WHERE	mtr_mst.site_cd = mtr_ls1.site_cd
AND		mtr_mst.RowID = mtr_ls1.mst_RowID
AND		mtr_ls1.site_cd = itm_mst.site_cd
AND		mtr_ls1.mtr_ls1_stockno = itm_mst.itm_mst_stockno
AND		itm_mst.site_cd = itm_loc.site_cd
AND		itm_mst.RowID = itm_loc.mst_RowID
AND		itm_loc.itm_loc_stk_loc = mtr_ls1.mtr_ls1_stk_locn
AND		mtr_mst.mtr_mst_mtr_no = '".$mtr_no."'
AND		mtr_mst.site_cd = '".$site_cd."'";

$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();
}

 
?>