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

require_once('config.php');

$error_message;
$valid = true;

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

	
$sql= "	SELECT  itm_mtu.itm_mtu_stockno,		
				itm_mtu.itm_mtu_desc,								
				itm_mtu.itm_mtu_stk_locn,											
				itm_mtu.itm_mtu_used_uom,
				itm_mtu.itm_mtu_used_qty ,			
				itm_mtu_serial_no= COALESCE(itm_mtu_serial_no,''),	
				itm_mtu.itm_mtu_mtr_no,		
				itm_mtu.itm_mtu_mtr_lineno,			
				itm_mtu_wo= COALESCE(itm_mtu_wo,''),
				itm_mtu_assetno= COALESCE(itm_mtu_assetno,''),
				itm_mtu.itm_mtu_used_uom,	
				itm_mtu.itm_mtu_chg_costcenter,		
				itm_mtu.itm_mtu_chg_account,
				itm_mtu.itm_mtu_crd_costcenter,
				itm_mtu.itm_mtu_crd_account,							
				itm_mtu_po_costcenter= COALESCE(itm_mtu_po_costcenter,''),	
				itm_mtu_po_account= COALESCE(itm_mtu_po_account,''),
				itm_mtu_po_crd_costcenter= COALESCE(itm_mtu_po_crd_costcenter,''),	
				itm_mtu_po_crd_account= COALESCE(itm_mtu_po_crd_account,''),
				itm_mtu.itm_mtu_item_cost, 
				itm_mtu.itm_mtu_mtc_id,			
				itm_mtu.RowID,
				return_qty = COALESCE('','')				
				FROM itm_mtu where 
				itm_mtu.itm_mtu_used_qty > 0  
				AND		
				itm_mtu.itm_mtu_stockno 
				IS NOT NULL AND  ( site_cd = '".$site_cd."' )  
				AND  ( itm_mtu_mtr_no = '".$itm_mtu_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();
}

 
?>