<?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);
	
$site_cd = $_GET['site_cd'];
$sync_step = $_GET['sync_step'];
$sync_time = $_GET['sync_time'];
$sync_status = $_GET['sync_status'];
$sync_url = $_GET['sync_url'];
$LOGINID = $_GET['LOGINID'];
$dvc_id = $_GET['dvc_id'];

f_logging($json,"off_update_response",$sync_url,$LOGINID,$sync_step,$sync_time,$site_cd);

foreach ($result->Header as $Header)
{
	
	$mst_RowID = $Header->mst_RowID;
	$wko_det_tech_resp_date = $Header->wko_det_tech_resp_date;
	$wko_det_tech_resp_id = $Header->wko_det_tech_resp_id;
	$wko_det_resp_contact = $Header->wko_det_resp_contact;
	$wko_det_resp_id = $Header->wko_det_resp_id;
	$wko_det_resp_date = $Header->wko_det_resp_date;
	$wko_det_resp_name = $Header->wko_det_resp_name;
	$wko_det_varchar9 = $Header->wko_det_varchar9;
	
	
	$sql="UPDATE wko_det SET 							
			wko_det_tech_resp_date =?,		
			wko_det_tech_resp_id=?,		
			wko_det_resp_contact=?,			
			wko_det_resp_id=?,		
			wko_det_varchar9=?,		
			wko_det_resp_name=?,			
			wko_det_resp_date=?,
			mbl_audit_user=?,
			mbl_audit_date=GETDATE()	
			WHERE site_cd = ? 
			AND mst_RowID = ? ;";	
				
	//echo $sql;
	$params = array($wko_det_tech_resp_date,$wko_det_tech_resp_id,$wko_det_resp_contact,$wko_det_resp_id,$wko_det_varchar9,$wko_det_resp_name,$wko_det_resp_date,$LOGINID,$site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql,$params);

	if( !$stmt ) {
		$error_message = "Error SELECT Table (wko_det)";
		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($json);		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}


	




function returnData(){
	
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Response updated successfully ');
	
	echo json_encode($returnData);
}

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


 
?>