<?php
// get these values from your DB.

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

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

$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];

	

$sql= "SELECT 	loc_mst.loc_mst_stk_loc, loc_mst.loc_mst_mst_loc_cd, 
		loc_mst.loc_mst_area_loc_cd, loc_mst.loc_mst_bin_id, 
		loc_mst.loc_mst_storage_type, loc_mst.loc_mst_desc FROM 	loc_mst WHERE loc_mst.site_cd = '".$site_cd."' ";

$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 (loc_mst)";
	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',	
	'data' => $json);
	
	echo json_encode($returnData);
}

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

 
?>