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


$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);

$site_cd = $data['site_cd'];
$XML = $data['XML'];	


/* 
//XML validation
$sql= "select column_name,default_label,cf_label_required from cf_label where language_cd ='DEFAULT' and table_name = 'ast_mst' and column_name in ".$XML." ";
		
		
$stmt_validation = sqlsrv_query( $conn, $sql);

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

$XML_validation = array();

do {
     while ($row = sqlsrv_fetch_array($stmt_validation, SQLSRV_FETCH_ASSOC)) {		
		$XML_validation[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt_validation) );
 */


//AssetStatus
$sql= "SELECT ast_sts_status,ast_sts_desc		
		FROM 	ast_sts (NOLOCK)
		WHERE	ast_sts.site_cd = '".$site_cd."'
		AND		ast_sts.ast_sts_typ_cd IN ( 'IN-SERVICE', 'OUT-OF-SERVICE') ORDER BY ast_sts_status";


$stmt_ast_sts = sqlsrv_query( $conn, $sql);

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

$AssetStatus = array();

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



//Critical_Factor 
$sql= "SELECT 	ast_cri.ast_cri_cri_factor, ast_cri.ast_cri_desc  
		FROM 	ast_cri      
		WHERE ast_cri.site_cd = '".$site_cd."'";


$stmt_ast_cri = sqlsrv_query( $conn, $sql);

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

$Critical_Factor  = array();

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




//Manufacturer 
$sql= "SELECT	mfg_mst.mfg_mst_mfg_cd,  mfg_mst.mfg_mst_company  FROM 	mfg_mst WHERE mfg_mst.site_cd = '".$site_cd."'";


$stmt_mfg_mst = sqlsrv_query( $conn, $sql);

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

$Manufacturer  = array();

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


//User Area 
$sql= "SELECT 		ulc_mst_loc_cd,  ulc_mst_desc  FROM ulc_mst    
WHERE (		ulc_mst.ulc_mst_disable_flag = 0  AND	ulc_mst.ulc_mst_cls = '1'  ) 
AND ulc_mst.site_cd = '".$site_cd."' ";


$stmt_ulc_mst = sqlsrv_query( $conn, $sql);

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

$UserArea  = array();

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


//Location Type 
$sql= "	SELECT loc_typ_cod,loc_typ_desc  
		FROM   loc_typ     WHERE ( loc_typ.loc_typ_disable_flag = 0  ) 
		AND    loc_typ.site_cd ='".$site_cd."' ";


$stmt_loc_typ = sqlsrv_query( $conn, $sql);

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

$LocationType  = array();

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


//Campus 
$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'";


$stmt_cf_cost_center = sqlsrv_query( $conn, $sql);

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

$Campus  = array();

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



//Building  
$sql= "	SELECT mst_war.mst_war_work_area, mst_war.mst_war_desc        
		FROM mst_war      
		WHERE (mst_war.mst_war_disable_flag = 0     ) 
		AND mst_war.site_cd = '".$site_cd."'";


$stmt_mst_war = sqlsrv_query( $conn, $sql);

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

$Building   = array();

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



//Infra   
$sql= "	SELECT blk_mst.blk_mst_blk_cod, blk_mst.blk_mst_description   
		FROM  blk_mst     
		WHERE ( blk_mst.blk_mst_disable_flag = 0  ) 
		AND blk_mst.site_cd = '".$site_cd."'";


$stmt_blk_mst = sqlsrv_query( $conn, $sql);

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

$Infra   = array();

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




//umdns_mst   
$sql= "	SELECT umdns_mst_umdns_cod, umdns_mst_description, umdns_mst_lif_cyc  
		FROM	umdns_mst     
		WHERE (	umdns_mst.umdns_mst_disable_flag = 0  )
		AND umdns_mst.site_cd = '".$site_cd."'";


$stmt_umdns_mst = sqlsrv_query( $conn, $sql);

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

$umdns_mst   = array();

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



//Discipline Code 
$sql= "SELECT ast_type.ast_type_cd,ast_type.ast_type_descs        
		FROM ast_type     
		WHERE (ast_type.ast_type_disable_flag = 0  ) 
		AND ast_type.site_cd = '".$site_cd."'";


$stmt_ast_type = sqlsrv_query( $conn, $sql);

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

$Discipline_Code   = array();

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



//System Code 
$sql= "SELECT wrk_grp.wrk_grp_grp_cd,wrk_grp.wrk_grp_desc       
		FROM wrk_grp     
		WHERE (wrk_grp_disable_flag = '0'  ) 
		AND wrk_grp.site_cd = '".$site_cd."'";


$stmt_wrk_grp = sqlsrv_query( $conn, $sql);

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

$System_Code   = array();

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


//Sub System Code 
$sql= "SELECT ast_cod.ast_cod_ast_cd,ast_cod.ast_cod_desc        
		FROM ast_cod     
		WHERE (ast_cod.ast_cod_disable_flag = '0'  ) 
		AND ast_cod.site_cd = '".$site_cd."'";


$stmt_ast_cod = sqlsrv_query( $conn, $sql);

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

$Sub_System_Code   = array();

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






if($stmt_ast_sts && $stmt_ast_cri && $stmt_mfg_mst && $stmt_ulc_mst && 
	$stmt_loc_typ && $stmt_cf_cost_center && $stmt_mst_war && $stmt_blk_mst && $stmt_umdns_mst && $stmt_ast_type && $stmt_wrk_grp && $stmt_ast_cod) {
		 sqlsrv_commit( $conn );
		 sqlsrv_close( $conn);	
		 returnData($AssetStatus,$Critical_Factor,$Manufacturer,$UserArea,$LocationType,
		 $Campus,$Building,$Infra,$umdns_mst,$Discipline_Code,$System_Code,$Sub_System_Code);	
	} else {
		 sqlsrv_rollback( $conn );
		 $error_message = "Transaction rolled back.<br />";
		 returnError($error_message);
	} 
	

function returnData($AssetStatus,$Critical_Factor,$Manufacturer,$UserArea,$LocationType,
		 $Campus,$Building,$Infra,$umdns_mst,$Discipline_Code,$System_Code,$Sub_System_Code){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'AssetStatus'=>$AssetStatus,
	'CriticalFactor' => $Critical_Factor,
	'Manufacturer' => $Manufacturer,
	'UserArea' => $UserArea,
	'LocationType' => $LocationType,
	'Campus' => $Campus,
	'Building' => $Building,
	'Discipline_Code' => $Discipline_Code,
	'System_Code' => $System_Code,
	'Sub_System_Code' => $Sub_System_Code,
	'umdns_mst' => $umdns_mst,
	'Infra' => $Infra);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(


	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
	exit();
		
}
	

 
?>