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

function get_usg_wrk_change($site_cd, $empl_id, $table){
	global $conn;
	$sql_get_sts_change="select	usg_wrk_status
						From	usg_wrk (NOLOCK)
						Where	usg_wrk.site_cd = '".$site_cd."'
						And		usg_wrk_change = '1'
						And		usg_wrk_usr_grp = (Select 	emp_mst_usr_grp 
													From 	emp_mst (NOLOCK)
													Where 	emp_mst.site_cd = '".$site_cd."'
													And		emp_mst.emp_mst_empl_id = '".$empl_id."')";

	$params = array();
	$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
	$stmt_get_sts_change = sqlsrv_query( $conn, $sql_get_sts_change, $params, $options);

	if( !$stmt_get_sts_change ) {
		 $error_message = "Error selecting table (usg_wrk)";
		 returnError($error_message);
		 die( print_r( sqlsrv_errors(), true));
		 
	}else{
		$row_count = sqlsrv_num_rows( $stmt_get_sts_change );
	}
	$work_group_cnt = 0;
	$emp_det_work_grp = '';

	do {
		 while ($row = sqlsrv_fetch_array($stmt_get_sts_change, SQLSRV_FETCH_ASSOC)) {	
			$work_group_cnt = $work_group_cnt + 1;
			If ($row_count == 1 && $work_group_cnt == 1) {
				$emp_det_work_grp = " AND (".$table." IN ( '".$row['usg_wrk_status']."')";
			}elseif ($row_count > 1 && $work_group_cnt == 1) {
				$emp_det_work_grp = " AND (".$table." IN ( '";
				$emp_det_work_grp = $emp_det_work_grp.$row['usg_wrk_status']."',";
			}elseif($row_count > 1 && $work_group_cnt > 1 && $row_count != $work_group_cnt){
				$emp_det_work_grp = $emp_det_work_grp."'".$row['usg_wrk_status']."',";
			}elseif($row_count == $work_group_cnt){
				$emp_det_work_grp = $emp_det_work_grp."'".$row['usg_wrk_status']."')";
			}
		 }
	} while ( sqlsrv_next_result($stmt_get_sts_change) );

	if($row_count > 0){
		$emp_det_work_grp = $emp_det_work_grp . ' OR ' . $table . ' IS NULL) ';
	}
	
return $emp_det_work_grp;
}
?>