<?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');
// Converts it into a PHP object
$data = json_decode($json, true);


$site_cd = $data['site_cd'];
$sync_step = $data['sync_step'];
$sync_time = $data['sync_time'];
$sync_status = $data['sync_status'];
$sync_url = $data['sync_url'];
$LOGINID = $data['LOGINID'];
$dvc_id = $data['dvc_id'];

if($sync_status =="online"){
	
}else{
	f_logging($json,"update_acknowledgement.php",$sync_url,$LOGINID,$sync_step,$sync_time,$site_cd);
}


$mst_RowID = $data['mst_RowID'];
$rating1 = $data['rating1'];
$rating2 = $data['rating2'];
$rating3 = $data['rating3'];
$ack_name = $data['ack_name'];
$ack_contact = $data['ack_contact'];
$ack_id = $data['ack_id'];

$sql="UPDATE wko_det SET 						
			wko_det_rating1 =?,		
			wko_det_rating2=?,		
			wko_det_rating3=?,			
			wko_det_ack_name=?,		
			wko_det_ack_contact=?,			
			wko_det_ack_id=?,
			wko_det_ack_date = getdate(),
			mbl_audit_user=?,
			mbl_audit_date=GETDATE()
			WHERE site_cd = ? 
			AND mst_RowID = ? ;";	
				
	//echo $sql;
	$params = array($rating1,$rating2,$rating3,$ack_name,$ack_contact,$ack_id,$LOGINID,$site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql,$params);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (wko_ls8)";
				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' => 'Acknowledgement updated successfully ');
	
	echo json_encode($returnData);
}

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


 
?>