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

$site_cd = $_REQUEST['site_cd'];

//Checking Work Order Number A/M
$sql= "Select cnt_mst_numbering From cnt_mst (NOLOCK) Where site_cd ='".$site_cd."' And cnt_mst_module_cd ='WKR'";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (cnt_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$cnt_mst_numbering = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$cnt_mst_numbering = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//Mydefault values
$sql= "SELECT 
		 dft_mst_wko_sts = dft_mst_wko_sts + ' : ' + wrk_sts_desc, 
		 dft_mst_lab_act , 
		 dft_mst_mat_act , 
		 dft_mst_con_act , 
		 dft_mst_wko_pri = dft_mst_wko_pri + ' : ' + wrk_pri_desc + ' - ' + cast(wrk_pri_due_date_count as varchar), 
		 dft_mst_temp_ast , 
		 dft_mst_wko_asset_no , 
		 dft_mst_wo_default_cc , 
		 dft_mst_time_card_by , 
		 dft_mst_itm_resv , 
		 dft_mst_mr_approval , 
		 dft_mst_pur_email_approver , 
		 dft_mst_mtr_email_approver , 
		 dft_mst_gen_inv
	FROM dft_mst (NOLOCK) ,wrk_pri (NOLOCK), wrk_sts (NOLOCK) 
	WHERE dft_mst.site_cd = wrk_pri.site_cd 
	AND dft_mst.dft_mst_wko_pri = wrk_pri.wrk_pri_pri_cd 
	AND dft_mst.site_cd = wrk_sts.site_cd
	AND dft_mst.dft_mst_wko_sts = wrk_sts.wrk_sts_status 
	AND	dft_mst.site_cd ='".$site_cd."'";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Mydefault values)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$Defaultvalue = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$Defaultvalue[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//Workorder Status
$sql= "SELECT 	wrk_sts.wrk_sts_status,  wrk_sts.wrk_sts_typ_cd,    wrk_sts.wrk_sts_desc,  wrk_sts.wrk_sts_email_flag  
FROM 	wrk_sts  (NOLOCK)
WHERE (wrk_sts.wrk_sts_disable_flag = 0  ) 
AND wrk_sts.site_cd = '".$site_cd."' 
AND wrk_sts_typ_cd NOT IN ('COMPLETE', 'CLOSE', 'CANCEL', 'FORCE-CLOSE')ORDER BY wrk_sts_status ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Workorder Status)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$WorkorderStatus = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$WorkorderStatus[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//Priority
$sql= "SELECT wrk_pri.wrk_pri_pri_cd,  wrk_pri.wrk_pri_desc,   wrk_pri.wrk_pri_due_date_count  
FROM wrk_pri   (NOLOCK)
WHERE wrk_pri.site_cd = '".$site_cd."' AND wrk_pri.wrk_pri_disable_flag = '0'ORDER BY wrk_pri_pri_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Priority)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$Priority = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$Priority[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );



//FaultCode
$sql= "SELECT wrk_flt.wrk_flt_fault_cd,wrk_flt.wrk_flt_desc  
FROM wrk_flt (NOLOCK) 
WHERE wrk_flt.site_cd = '".$site_cd."' 
AND wrk_flt.wrk_flt_disable_flag = '0' ORDER BY wrk_flt_fault_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Fault Code)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$FaultCode = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$FaultCode[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );



//CostCenter
$sql= "SELECT 	cf_cost_center.costcenter,    cf_cost_center.descs    
FROM 	cf_cost_center (NOLOCK)  
WHERE (	cf_cost_center.disable_flag = 0  ) 
AND cf_cost_center.site_cd = '".$site_cd."' 
AND disable_flag = '0'ORDER BY costcenter ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Cost Center)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$CostCenter = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$CostCenter[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );




//WorkGroupCode
$sql= "SELECT 	wrk_grp.wrk_grp_grp_cd,     wrk_grp.wrk_grp_desc     
FROM 	wrk_grp (NOLOCK)  
WHERE 	wrk_grp.site_cd = '".$site_cd."' 
AND 	wrk_grp.wrk_grp_disable_flag ='0' ORDER BY wrk_grp_grp_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Cost Center)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$WorkGroupCode = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$WorkGroupCode[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


if ($valid) {

	//echo $sql;
	returnData($Defaultvalue,$cnt_mst_numbering,$WorkorderStatus,$Priority,$FaultCode,$CostCenter,$WorkGroupCode);	
	
}

function returnData($Defaultvalue,$cnt_mst_numbering,$WorkorderStatus,$Priority,$FaultCode,$CostCenter,$WorkGroupCode){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'Defaultvalue' =>$Defaultvalue,	
	'Auto_Manual' => $cnt_mst_numbering,
	'WorkorderStatus' =>$WorkorderStatus,
	'CostCenter'=>$CostCenter,
	'WorkGroupCode'=>$WorkGroupCode,
	'Priority'=>$Priority,
	'FaultCode'=>$FaultCode);
	
	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);	

 
?>