<?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_mst.site_cd, 
					wko_mst_originator 			= COALESCE(CASE WHEN emp_mst1.emp_mst_name IS NULL THEN wko_mst_originator WHEN emp_mst1.emp_mst_name IS NOT NULL THEN wko_mst_originator+':'+emp_mst1.emp_mst_name END,'') ,       			
					wko_mst_phone				= COALESCE(wko_mst_phone,'') ,
					wko_det_cause_code			= COALESCE(wko_det_cause_code +':'+wrk_ccd.wrk_ccd_desc,''),
					wko_det_act_code			= COALESCE(wko_det_act_code+':'+wrk_act.wrk_act_desc,''),
					wko_det_work_type			= COALESCE(wko_det_work_type+':'+wrk_typ.wrk_typ_desc,''),
					wko_det_work_class			= COALESCE(wko_det_work_class+':'+wrk_cls.wrk_cls_desc,''),
					wko_det_work_grp			= COALESCE(wko_det_work_grp+':'+ wrk_grp.wrk_grp_desc,''),
					wrk_sts_status				= COALESCE(wrk_sts_status +':'+ wrk_sts.wrk_sts_desc,''),
					work_complete 				= COALESCE(CASE wrk_sts_typ_cd WHEN 'OPEN' THEN '0' ELSE '1' END,''), 
					wko_det_corr_action			= COALESCE(wko_det_corr_action,''),
					wko_det.mst_RowID,
					wko_det.RowID	,
					wko_mst_wo_no				= COALESCE(wko_mst_wo_no,''),  
					wko_mst_assetno 			= COALESCE(wko_mst_assetno,''), 
					wko_mst_work_area 			= COALESCE(wko_mst_work_area +':'+mst_war_desc,''), 
					wko_mst_asset_location 		= COALESCE(wko_mst_asset_location+':'+ast_loc_desc,''),
					wko_mst_asset_level			= COALESCE(wko_mst_asset_level+':'+ast_lvl_desc,'') ,     
					wko_mst_org_date			= COALESCE(wko_mst_org_date,''),  
					wko_mst_due_date			= COALESCE(wko_mst_due_date,''),  
					wko_mst_descs				= COALESCE(wko_mst_descs,''),
					wko_det_note1				= COALESCE(wko_det_note1,''),
					ast_mst_asset_shortdesc		= COALESCE(ast_mst_asset_shortdesc,'')					
																
									
					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
							
							LEFT 
							OUTER 
							JOIN		emp_mst emp_mst1(NOLOCK)						
							ON			wko_mst.site_cd = emp_mst1.site_cd
							AND			wko_mst.wko_mst_originator = emp_mst1.emp_mst_empl_id
							
							LEFT 
							OUTER 
							JOIN		ast_mst (NOLOCK)
							ON			wko_mst.site_cd = ast_mst.site_cd
							AND			wko_mst.wko_mst_assetno = ast_mst.ast_mst_asset_no
									
							
							
							LEFT 
							OUTER 
							JOIN		mst_war (NOLOCK)
							ON			wko_mst.site_cd = mst_war.site_cd
							AND			wko_mst.wko_mst_work_area = mst_war.mst_war_work_area
							
							
							LEFT 
							OUTER 
							JOIN		ast_lvl (NOLOCK)
							ON			wko_mst.site_cd = ast_lvl.site_cd
							AND			wko_mst.wko_mst_asset_level = ast_lvl.ast_lvl_ast_lvl
							
							
							LEFT 
							OUTER 
							JOIN		ast_loc (NOLOCK)
							ON			wko_mst.site_cd = ast_loc.site_cd
							AND			wko_mst.wko_mst_asset_location = ast_loc.ast_loc_ast_loc
							
							LEFT 
							OUTER 
							JOIN		wrk_ccd (NOLOCK)
							ON			wko_det.site_cd = wrk_ccd.site_cd
							AND			wko_det.wko_det_cause_code = wrk_ccd.wrk_ccd_cause_cd
							
							LEFT 
							OUTER 
							JOIN		wrk_act (NOLOCK)
							ON			wko_det.site_cd = wrk_act.site_cd
							AND			wko_det.wko_det_act_code = wrk_act.wrk_act_action_cd
							
							LEFT 
							OUTER 
							JOIN		wrk_grp (NOLOCK)
							ON			wko_det.site_cd = wrk_grp.site_cd
							AND			wko_det.wko_det_work_grp = wrk_grp.wrk_grp_grp_cd
							
							
							LEFT 
							OUTER 
							JOIN		wrk_cls (NOLOCK)
							ON			wko_det.site_cd = wrk_cls.site_cd
							AND			wko_det.wko_det_work_class = wrk_cls.wrk_cls_cls_cd
							
							LEFT 
							OUTER 
							JOIN		wrk_typ (NOLOCK)
							ON			wko_mst.site_cd = wrk_typ.site_cd
							AND			wko_det.wko_det_work_type = wrk_typ.wrk_typ_typ_cd	
												
							
					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_mst.site_cd, 
					wko_mst_originator 			= COALESCE(CASE WHEN emp_mst1.emp_mst_name IS NULL THEN wko_mst_originator WHEN emp_mst1.emp_mst_name IS NOT NULL THEN wko_mst_originator+':'+emp_mst1.emp_mst_name END,'') ,       			
					wko_mst_phone				= COALESCE(wko_mst_phone,'') ,
					wko_det_cause_code			= COALESCE(wko_det_cause_code +':'+wrk_ccd.wrk_ccd_desc,''),
					wko_det_act_code			= COALESCE(wko_det_act_code+':'+wrk_act.wrk_act_desc,''),
					wko_det_work_type			= COALESCE(wko_det_work_type+':'+wrk_typ.wrk_typ_desc,''),
					wko_det_work_class			= COALESCE(wko_det_work_class+':'+wrk_cls.wrk_cls_desc,''),
					wko_det_work_grp			= COALESCE(wko_det_work_grp+':'+ wrk_grp.wrk_grp_desc,''),
					wrk_sts_status				= COALESCE(wrk_sts_status +':'+ wrk_sts.wrk_sts_desc,''),
					work_complete 				= COALESCE(CASE wrk_sts_typ_cd WHEN 'OPEN' THEN '0' ELSE '1' END,''), 
					wko_det_corr_action			= COALESCE(wko_det_corr_action,''),				
					wko_det.mst_RowID,
					wko_det.RowID	,
					wko_mst_wo_no				= COALESCE(wko_mst_wo_no,''),  
					wko_mst_assetno 			= COALESCE(wko_mst_assetno,''), 
					wko_mst_work_area 			= COALESCE(wko_mst_work_area +':'+mst_war_desc,''), 
					wko_mst_asset_location 		= COALESCE(wko_mst_asset_location+':'+ast_loc_desc,''),
					wko_mst_asset_level			= COALESCE(wko_mst_asset_level+':'+ast_lvl_desc,'') ,     
					wko_mst_org_date			= COALESCE(wko_mst_org_date,''),  
					wko_mst_due_date			= COALESCE(wko_mst_due_date,''),  
					wko_mst_descs				= COALESCE(wko_mst_descs,''),
					wko_det_note1				= COALESCE(wko_det_note1,''),					
					ast_mst_asset_shortdesc		= COALESCE(ast_mst_asset_shortdesc,'')
					
															
												
				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	
							
							LEFT 
							OUTER 
							JOIN		emp_mst emp_mst1(NOLOCK)						
							ON			wko_mst.site_cd = emp_mst1.site_cd
							AND			wko_mst.wko_mst_originator = emp_mst1.emp_mst_empl_id
							
							LEFT 
							OUTER 
							JOIN		ast_mst (NOLOCK)
							ON			wko_mst.site_cd = ast_mst.site_cd
							AND			wko_mst.wko_mst_assetno = ast_mst.ast_mst_asset_no
							
							LEFT 
							OUTER 
							JOIN		wrk_ccd (NOLOCK)
							ON			wko_det.site_cd = wrk_ccd.site_cd
							AND			wko_det.wko_det_cause_code = wrk_ccd.wrk_ccd_cause_cd
							
							
							LEFT 
							OUTER 
							JOIN		mst_war (NOLOCK)
							ON			wko_mst.site_cd = mst_war.site_cd
							AND			wko_mst.wko_mst_work_area = mst_war.mst_war_work_area
							
							
							LEFT 
							OUTER 
							JOIN		ast_lvl (NOLOCK)
							ON			wko_mst.site_cd = ast_lvl.site_cd
							AND			wko_mst.wko_mst_asset_level = ast_lvl.ast_lvl_ast_lvl
							
							LEFT 
							OUTER 
							JOIN		ast_loc (NOLOCK)
							ON			wko_mst.site_cd = ast_loc.site_cd
							AND			wko_mst.wko_mst_asset_location = ast_loc.ast_loc_ast_loc
							
							
							LEFT 
							OUTER 
							JOIN		wrk_act (NOLOCK)
							ON			wko_det.site_cd = wrk_act.site_cd
							AND			wko_det.wko_det_act_code = wrk_act.wrk_act_action_cd
							
							
							LEFT 
							OUTER 
							JOIN		wrk_grp (NOLOCK)
							ON			wko_det.site_cd = wrk_grp.site_cd
							AND			wko_det.wko_det_work_grp = wrk_grp.wrk_grp_grp_cd
							
							
							LEFT 
							OUTER 
							JOIN		wrk_cls (NOLOCK)
							ON			wko_det.site_cd = wrk_cls.site_cd
							AND			wko_det.wko_det_work_class = wrk_cls.wrk_cls_cls_cd
							
							LEFT 
							OUTER 
							JOIN		wrk_typ (NOLOCK)
							ON			wko_mst.site_cd = wrk_typ.site_cd
							AND			wko_det.wko_det_work_type = wrk_typ.wrk_typ_typ_cd
																	
							
							

					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 (Asset Group Code)";
	 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' => 'Completed 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);

?>