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

require_once('config.php');

$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$type = $_REQUEST['type'];
$from_date = $_REQUEST['from_date'];
$to_date = $_REQUEST['to_date'];
	
//$sql= "select emp_mst.emp_mst_empl_id,emp_mst.emp_mst_name from emp_mst where site_cd ='".$site_cd."'";

$sql= "SELECT	itm_trx.itm_trx_doc_no,					itm_trx.itm_trx_trx_date,			itm_trx.itm_trx_mtlrqnnum, 	itm_trx.itm_trx_pkg_slip_no,
				itm_trx.itm_trx_mtlrqn_lineno,			itm_trx.itm_trx_chg_costcenter,		itm_trx.itm_trx_stockno, 	itm_trx.itm_trx_rcv_qty,itm_trx.itm_trx_rtn_qty,	
				itm_trx.itm_trx_desc,					itm_trx.itm_trx_isu_qty,			itm_trx.itm_trx_item_cost, 
				itm_trx.itm_trx_isu_empl_id,			emp_mst.emp_mst_name,				itm_trx.itm_trx_remark 
		
FROM itm_trx, emp_mst 

WHERE (itm_trx_trx_type = '".$type."') -- Issue
AND  itm_trx.site_cd  = '".$site_cd."' 
AND itm_trx_trx_date >= '".$from_date."' 
AND itm_trx_trx_date < DATEADD(day,1,'".$to_date."')  
AND  itm_trx.site_cd  = emp_mst.site_cd
AND  itm_trx.itm_trx_isu_empl_id  = emp_mst.emp_mst_empl_id ORDER BY itm_trx.itm_trx_trx_date DESC";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (dft_mst)";
	 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) );

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

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => '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();
}

 
?>