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

function get_emp_ls5($site_cd, $empl_id, $table){
	global $conn;
	$sql_get_work_group="Select 	emp_ls5_work_group
			From		emp_mst (NOLOCK), emp_ls5 (NOLOCK)
			Where 	emp_mst.site_cd = emp_ls5.site_cd
			And		emp_mst.rowid = emp_ls5.mst_rowid
			And		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_work_group = sqlsrv_query( $conn, $sql_get_work_group, $params, $options);

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

	do {
		 while ($row = sqlsrv_fetch_array($stmt_get_work_group, 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['emp_ls5_work_group']."')";
			}elseif ($row_count > 1 && $work_group_cnt == 1) {
				$emp_det_work_grp = " AND (".$table." IN ( '";
				$emp_det_work_grp = $emp_det_work_grp.$row['emp_ls5_work_group']."',";
			}elseif($row_count > 1 && $work_group_cnt > 1 && $row_count != $work_group_cnt){
				$emp_det_work_grp = $emp_det_work_grp."'".$row['emp_ls5_work_group']."',";
			}elseif($row_count == $work_group_cnt){
				$emp_det_work_grp = $emp_det_work_grp."'".$row['emp_ls5_work_group']."')";
			}
		 }
	} while ( sqlsrv_next_result($stmt_get_work_group) );

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