<?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 ='WKO'";
$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) );


//WorkType
$sql= "	SELECT 	WorkType= wrk_typ.wrk_typ_typ_cd + ' : ' + wrk_typ.wrk_typ_desc, wrk_typ_option,  wrk_typ_pm_option  
		FROM 	wrk_typ  (NOLOCK)     
		WHERE wrk_typ.site_cd = '".$site_cd."' AND wrk_typ.wrk_typ_disable_flag ='0'ORDER BY wrk_typ_typ_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

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

$WorkType = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$WorkType[] = $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) );



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

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

$account = array();

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

//wrk_group
$sql="	select wrk_grp_grp_cd,wrk_grp_desc from wrk_grp where site_cd = '".$site_cd."'";
		
$stmt = sqlsrv_query( $conn, $sql);

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

$wrk_group = array();

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



if ($valid) {

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

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

 
?>