<?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_ls4.site_cd,   
				wko_ls4.wko_ls4_assetno,   
				wko_ls4.wko_ls4_activity_code,   
				wko_ls4.wko_ls4_supplier,   
				wko_ls4.wko_ls4_contract_id,   
				wko_ls4.wko_ls4_po_no,    
				wko_ls4.wko_ls4_po_lineno,   
				wko_ls4.wko_ls4_cmpl_date,    
				wko_ls4.wko_ls4_est_cost,    
				wko_ls4.wko_ls4_act_cost,   
				wko_ls4.wko_ls4_descr,   
				wko_ls4.wko_ls4_qty_needed,   
				wko_ls4.wko_ls4_svc_uom,   
				wko_ls4.wko_ls4_pr_no,   
				wko_ls4.wko_ls4_pr_lineno,   
				wko_ls4.wko_ls4_nonrefpono,   
				wko_ls4.wko_ls4_com_code,   
				wko_ls4.wko_ls4_taxable,    
				wko_ls4.wko_ls4_chg_costcenter,   
				wko_ls4.wko_ls4_chg_account,
				wko_ls4.wko_ls4_tax_cd,   
				wko_ls4.wko_ls4_tax_rate,   
				wko_ls4.wko_ls4_tax_value,   
				pur_mst.pur_mst_purq_approve,
				pur_ls1_po_no = COALESCE(pur_ls1.pur_ls1_po_no,''),   
				pur_ls1_po_lineno = COALESCE(pur_ls1.pur_ls1_po_lineno,''),
				puo_ls1.puo_ls1_rcv_qty,
				wko_ls4.audit_user,   
				wko_ls4.audit_date,  				  
				wko_ls4.mst_RowID,   
				wko_ls4.RowID  	
			
			FROM 		wko_ls4

			LEFT OUTER JOIN pur_mst
			ON			pur_mst.site_cd = wko_ls4.site_cd
			AND			pur_mst.pur_mst_porqnnum =  wko_ls4.wko_ls4_pr_no


			LEFT OUTER JOIN pur_ls1
			ON			pur_mst.site_cd = pur_ls1.site_cd		
			AND			pur_mst.RowID =  pur_ls1.mst_RowID
			AND			pur_ls1.pur_ls1_pr_lineno =  wko_ls4.wko_ls4_pr_lineno
			
			LEFT OUTER JOIN puo_ls1
			ON			wko_ls4.site_cd = puo_ls1.site_cd		
			AND			wko_ls4.wko_ls4_pr_no =  puo_ls1.puo_ls1_pr_no


			WHERE 	wko_ls4.site_cd = '".$site_cd."'
			AND 	wko_ls4.mst_RowID = '".$mst_RowID."'";
 
 


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

 
?>