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

$error_message;
$valid = true;

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

$sql = "SELECT itm_loc.rowid, itm_loc.mst_rowid,  itm_loc.site_cd, itm_loc_stk_loc, itm_loc_part_no, itm_loc_oh_qty,itm_loc_prim_locn_flg
			FROM itm_mst, itm_loc 
			WHERE itm_mst.site_cd = itm_loc.site_cd 
			AND itm_mst.rowid = itm_loc.mst_rowid
			AND itm_mst.site_cd = '".$_REQUEST['site_cd']."' 
			AND itm_mst_stockno = '".$_REQUEST['itm_mst_stockno']."'";
$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_mst)";
	returnError($error_message);
}

$json = array();

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

// $returnData = array(
    // 'status' => 'OK',
	// 'rowid' => $rowid,
    // 'message' => 'Select OK',
	// 'data' => json_encode($json));
	

if ($valid) {
	
	
	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();
}

	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

?>