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

$site_cd = $_REQUEST['site_cd'];
$EmpID = $_REQUEST['EmpID'];
$LoginID = $_REQUEST['LoginID'];
$wko_group = $_REQUEST['wko_group'];


	
//$sql= "select emp_mst.emp_mst_empl_id,emp_mst.emp_mst_name from emp_mst where site_cd ='".$site_cd."'";

$sql= "SELECT  emp_mst.emp_mst_empl_id,     		
				emp_mst_title = COALESCE(emp_mst_title,''), 
				emp_mst.emp_mst_name,emp_mst_emg_phone = COALESCE(emp_mst_emg_phone,''),
				emp_det_craft = COALESCE(emp_det_craft+':'+crf_mst_desc,''),
				emp_ls1_charge_rate = COALESCE(emp_ls1.emp_ls1_charge_rate,0),

				WOASSIGN = (SELECT count(*) FROM wko_mst(NOLOCK) , wko_det(NOLOCK), wrk_sts(NOLOCK)
						WHERE	wko_mst.site_cd = wko_det.site_cd
						AND		wko_mst.rowid = wko_det.mst_rowid
						AND		wko_mst.site_cd = wrk_sts.site_cd
						AND		wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
						ANd		wko_det.site_cd = emp_mst.site_cd
						AND		wko_det.wko_det_assign_to = emp_mst.emp_mst_empl_id
						AND		wrk_sts.wrk_sts_typ_cd ='OPEN')		
							
		From 	emp_mst (NOLOCK)

				LEFT 
				OUTER 
				JOIN	emp_sts (NOLOCK) 
				ON		emp_mst.site_cd = emp_sts.site_cd  
				and     emp_mst.emp_mst_status = emp_sts.emp_sts_status  	
				And		emp_sts.emp_sts_typ_cd = 'ACTIVE'

				LEFT 
				OUTER 
				JOIN	emp_det (NOLOCK) 
				ON		emp_mst.site_cd = emp_det.site_cd		
				And		emp_mst.rowid = emp_det.mst_rowid
				
				LEFT 
				OUTER 
				JOIN	emp_ls1 (NOLOCK) 
				ON		emp_det.mst_rowid = emp_ls1.mst_rowid	
				And		emp_det.site_cd = emp_ls1.site_cd	
				AND		emp_det.emp_det_craft = emp_ls1.emp_ls1_craft
				
				LEFT 
				OUTER 
				JOIN	crf_mst (NOLOCK) 
				ON		emp_det.site_cd = crf_mst.site_cd		
				AND		emp_det.emp_det_craft = crf_mst.crf_mst_crf_cd	
					
					
		WHERE	emp_mst.site_cd = '".$site_cd."' 
		And 	emp_sts.emp_sts_typ_cd = 'ACTIVE' 
		AND 	emp_det.emp_det_foreman = '1'
		AND 	( emp_det.emp_det_work_grp = '".$wko_group."' OR emp_det.emp_det_ira_idno = '".$wko_group."'  OR emp_det.emp_det_work_area = '".$wko_group."'  OR emp_det.emp_det_liability_acc = '".$wko_group."'  ) 
		ORDER BY emp_mst.emp_mst_empl_id ASC";

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


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

function returnData($json){
	//echo $json;
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'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);	

 
?>