<?php
require_once('config.php');
require_once('f_logging.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}

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

foreach($result->Header as $Heade){
	
	$site_cd= $Heade->site_cd;
	$ast_rcb_mst_war= $Heade->ast_rcb_mst_war;
	$ast_rcb_ast_lvl= $Heade->ast_rcb_ast_lvl;
	$ast_rcb_ast_loc= $Heade->ast_rcb_ast_loc;
	
	$ast_rcb_ast_total= $Heade->ast_rcb_ast_total;
	$ast_rcb_ast_scan= $Heade->ast_rcb_ast_scan;
	$ast_rcb_ast_in_loc= $Heade->ast_rcb_ast_in_loc;
	$ast_rcb_ast_not_loc= $Heade->ast_rcb_ast_not_loc;
	$ast_rcb_ast_not_system= $Heade->ast_rcb_ast_not_system;
	
	$LogingID= $Heade->LoginID;
	
	$sql = "SELECT 	RowID 
			FROM 	ast_rcb 
			where 	site_cd = '".$site_cd."' 
			and 	ast_rcb_mst_war ='".$ast_rcb_mst_war."' 
			and 	ast_rcb_ast_lvl='".$ast_rcb_ast_lvl."' 
			and 	ast_rcb_ast_loc ='".$ast_rcb_ast_loc."'
			and 	ast_rcb_active_flag ='1'";

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

	if ($stmt === false)
	{
		if (($errors = sqlsrv_errors()) != null)
		{
			foreach ($errors as $error)
			{

				file_put_contents($file_name, $now . "\r\n" . $sql . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
			}
		}
		sqlsrv_rollback($conn);
		sqlsrv_close($conn);

		$error_message = "(".$now.") Error selecting and updating table (cnt_mst)";
		returnError($error_message);
	}
	do
	{
		while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
		{

			$RowID = $row['RowID'];
			
		}
	}
	while (sqlsrv_next_result($stmt));
	
	if (!empty($RowID)) {
		
		$sql="UPDATE ast_rcb SET 						
			ast_rcb_active_flag =?,		
			ast_rcb_completed_by=?,
			audit_user=?,
			audit_date = GETDATE(),			
			mbl_audit_user=?,
			mbl_audit_date=GETDATE()
			WHERE site_cd = ? 
			AND RowID = ? ;";	
				
		//echo $sql;
		$params = array('0',$LogingID,$LogingID,$LogingID,$site_cd,$RowID);					
							
		$stmt = sqlsrv_query( $conn, $sql,$params);

		if( !$stmt ) {
			$error_message = "Error SELECT Table (ast_rcb)";
			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) );
		sqlsrv_free_stmt( $stmt);

	}
			
	
		
	
}



if( $stmt) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData();		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData(){
	
	 
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Update completed successfully.');
	
	echo json_encode($returnData);
}

function returnError($error_message){	
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}


 
?>