<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;

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

 
 $sql="SELECT 	
				wko_ls2.site_cd,   
				wko_ls2.wko_ls2_assetno,
				wko_ls2.wko_ls2_activity_code,   
				wko_ls2.wko_ls2_stockno,   
				wko_ls2.wko_ls2_desc,    
				wko_ls2_qty_needed				= COALESCE(wko_ls2.wko_ls2_qty_needed,0),    
				wko_ls2.wko_ls2_item_cost,   
				wko_ls2.wko_ls2_mr_no, 
				wko_ls2_mr_lineno, 
				mtr_mst.mtr_mst_status ,
				mtr_mst.mtr_mst_requester,
				requester_name = emp_mst_name,
				mtr_ls1_rcv_qty 				= COALESCE(mtr_ls1.mtr_ls1_rcv_qty,0),
				wko_ls2.wko_ls2_rec_supplier,   
				wko_ls2.wko_ls2_act_qty,   
				wko_ls2.wko_ls2_act_cost,   
				wko_ls2.wko_ls2_mtl_uom,  				
				wko_ls2.wko_ls2_taxable,   
				wko_ls2.wko_ls2_chg_costcenter,   
				wko_ls2.wko_ls2_chg_account,   
				wko_ls2.wko_ls2_stk_locn,			
				wko_ls2.audit_user,   
				wko_ls2.audit_date,  				
				wko_ls2.mst_RowID,				
				TotalAvailable = ( 	SELECT 	itm_mst_ttl_oh 
									FROM 	itm_mst 
									WHERE 	itm_mst.site_cd = wko_ls2.site_cd 
									AND 	itm_mst.itm_mst_stockno = wko_ls2.wko_ls2_stockno ) ,
				mtr_ls1_clear_qty	= COALESCE(mtr_ls1.mtr_ls1_clear_qty,0),    
				wko_ls2.RowID
			FROM 		wko_ls2

			LEFT OUTER JOIN mtr_mst
			ON				mtr_mst.site_cd = wko_ls2.site_cd
			AND			mtr_mst.mtr_mst_mtr_no =  wko_ls2.wko_ls2_mr_no


			LEFT OUTER JOIN mtr_ls1
			ON				mtr_mst.site_cd = mtr_ls1.site_cd
			AND			mtr_mst.RowID =  mtr_ls1.mst_RowID
			AND			mtr_ls1.mtr_ls1_mtr_lineno =  wko_ls2.wko_ls2_mr_lineno	
			
			LEFT OUTER JOIN emp_mst
			ON				mtr_mst.site_cd = emp_mst.site_cd
			AND			mtr_mst.mtr_mst_requester = emp_mst.emp_mst_empl_id


			WHERE 	wko_ls2.site_cd = '".$site_cd."'
			AND 	wko_ls2.mst_RowID = '".$mst_RowID."' order by wko_ls2_mr_no, wko_ls2_mr_lineno";
 
 


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

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

 
?>