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

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


$sql= "SELECT	pur_mst_porqnnum, 
				pur_mst_rqn_date, 
				pur_mst_purq_approve, 
				pur_ls1_desc, 
				pur_ls1_ord_qty, 
				pur_ls1_ord_uom,
				pur_ls1_po_no		=COALESCE(pur_ls1_po_no,''),
				pur_ls1_po_lineno	=COALESCE(pur_ls1_po_lineno,'')

	from		pur_mst (NOLOCK)   
	INNER JOIN	pur_det (NOLOCK)
	ON			pur_mst.site_cd = pur_det.site_cd
	AND			pur_mst.rowid = pur_det.mst_RowID
			
			
	INNER JOIN	pur_ls1 (NOLOCK)
	ON			pur_mst.site_cd = pur_ls1.site_cd
	AND			pur_mst.rowid = pur_ls1.mst_RowID
	
	where 		pur_mst.site_cd  =	'".$site_cd."'
	AND 	( 	pur_det_varchar1 = '".$wko_mst_wo_no."'
	OR			pur_det_varchar5 = '".$wko_mst_wo_no."'
	OR 			pur_det_varchar6 = '".$wko_mst_wo_no."'
	OR 			pur_det_varchar7 = '".$wko_mst_wo_no."'
	OR 			pur_det_varchar8 = '".$wko_mst_wo_no."')";
	
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (pur_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$PurchasingInfo = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	

	
		$PurchasingInfo[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


if ($valid) {
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

	//echo $sql;
	returnData($PurchasingInfo);	
	
}

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

function returnError($error, $error_message){
	$json = array();
	
	$returnData = array(
	'status' => $error,
	'message' => $error_message,	
	'data' => '');
	
	echo json_encode($returnData);
	exit();
		
}
		

 
?>