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


$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$mst_RowID = $_REQUEST['mst_RowID'];

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


$sql="Select Total = Sum(Case when wko_isp_datetime3 IS NULL Then 1 Else 0 End)
		From	wko_isp (NOLOCK)
		WHere	wko_isp.mst_rowid = '".$mst_RowID."'
		And		wko_isp.site_cd = '".$site_cd."'";	
									
						
	$stmt = sqlsrv_query( $conn, $sql);

			if( !$stmt ) {
				$error_message = "Error select Table (wko_isp)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			

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


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

function returnData($Total){
		
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'Total' => $Total);

	echo json_encode($returnData);
}

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

 
?>