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

$now = date("Ymdhisa");
$file_name = __DIR__ . "\mobile_log\_get_inventory_count_book_by_params.log";

$error_message;
$valid = true;

$itm_rcf_book_no;

$sql = "SELECT site_cd
	,itm_rcb_book_no
	,itm_rcb_desc
	,itm_rcb_select_criteria
	,itm_rcb_active_date
	,itm_rcb_item_count
	,itm_rcb_completed
	,itm_rcb_total_item
	,rowid
FROM itm_rcb
WHERE site_cd = '".$_REQUEST['site_cd']."' AND itm_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 (itm_rcb)";
	returnError($error_message);
}

$json = array();

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



if ($valid) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json);	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',	
	'message' => 'Select successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
		
	exit();
}

?>