<?php
require_once('config.php');

$error_message;
$valid = true;

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


$sql= "	Select 		wko_det.site_cd,
					wko_det.RowID,
					wko_det.mst_RowID,
					wko_det_rating1		= COALESCE(wko_det.wko_det_rating1,'0') ,
					wko_det_rating2		= COALESCE(wko_det.wko_det_rating2,'0'),
					wko_det_rating3		= COALESCE(wko_det.wko_det_rating3,'0'),
					wko_det_ack_name	= COALESCE(wko_det_ack_name,''),
					wko_det_ack_contact	= COALESCE(wko_det_ack_contact	,''),
					wko_det_ack_id		= COALESCE(wko_det_ack_id,'')
					
												
									
					FROM 	wko_mst (NOLOCK)     			
			
							INNER JOIN wko_det (NOLOCK)
							ON		wko_mst.site_cd = wko_det.site_cd
							AND		wko_mst.rowid = wko_det.mst_rowid
							AND 	wko_mst_type  IN ('C', 'P')
							
							INNER JOIN	wrk_sts (NOLOCK)
							ON			wko_mst.site_cd = wrk_sts.site_cd
							AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
														
												
							

					WHERE	( wko_mst.site_cd = '".$site_cd."' ) 
					AND wko_det.wko_det_assign_to LIKE '".$wko_det_assign_to."'  
					AND (wrk_sts_typ_cd NOT IN ('CLOSE', 'COMPLETE', 'FORCE-CLOSE', 'CANCEL'))
			UNION ALL
			
			Select 	
					wko_det.site_cd,
					wko_det.RowID,
					wko_det.mst_RowID,
					wko_det_rating1		= COALESCE(wko_det.wko_det_rating1,'0') ,
					wko_det_rating2		= COALESCE(wko_det.wko_det_rating2,'0'),
					wko_det_rating3		= COALESCE(wko_det.wko_det_rating3,'0'),
					wko_det_ack_name	= COALESCE(wko_det_ack_name,''),
					wko_det_ack_contact	= COALESCE(wko_det_ack_contact	,''),
					wko_det_ack_id		= COALESCE(wko_det_ack_id,'')					
					
															
												
			FROM 	wko_mst (NOLOCK)     			
			
					INNER JOIN wko_det (NOLOCK)
					ON		wko_mst.site_cd = wko_det.site_cd
					AND		wko_mst.rowid = wko_det.mst_rowid
					AND 	wko_mst_type  IN ('G')
					
					INNER JOIN	wrk_sts (NOLOCK)
					ON			wko_mst.site_cd = wrk_sts.site_cd
					AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
							
							

			WHERE	( wko_mst.site_cd = '".$site_cd."' ) 
			AND 	wko_det.wko_det_assign_to LIKE '".$wko_det_assign_to."' 
			AND 	(wrk_sts_typ_cd NOT IN ('CLOSE', 'COMPLETE', 'FORCE-CLOSE', 'CANCEL'))";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Acknowledgement List)";
	 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) {

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

function returnData($json){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully Retrieve Acknowledgement List ',	
	'data'=>$json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
		
}
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

?>