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

$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$itm_rcf_book_no = $_REQUEST['itm_rcf_book_no'];
$counted = $_REQUEST['counted'];

$total_count;
$pending_count;
$completed_count;

if ($counted=="ALL"){
	$sql = "select	itm_rcf.site_cd,
					itm_rcf.rowid,
					itm_rcf_book_no,
					itm_rcf_stockno,
					itm_rcf_stk_loc,
					itm_rcf_oh_qty,
					itm_rcf_item_count,
					itm_mst.itm_mst_desc,
					itm_mst_partno= COALESCE(itm_mst.itm_mst_partno,'' ),
					itm_mst_ext_desc= COALESCE(itm_mst.itm_mst_ext_desc,'' ),
					itm_rcf_remark 
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."' ORDER BY itm_mst_stockno ASC";
					
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 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) );
					
}else if($counted=="PENDING"){
	$sql = "select	itm_rcf.site_cd,
					itm_rcf.rowid,
					itm_rcf_book_no,
					itm_rcf_stockno,
					itm_rcf_stk_loc, 
					itm_rcf_oh_qty,
					itm_rcf_item_count,
					itm_mst.itm_mst_desc,
					itm_mst_partno= COALESCE(itm_mst.itm_mst_partno,'' ),
					itm_mst_ext_desc= COALESCE(itm_mst.itm_mst_ext_desc,'' ),
					itm_rcf_remark
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."' and itm_rcf_item_count IS NULL ORDER BY itm_mst_stockno ASC";


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

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 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) );					
}else if($counted=="COMPLETED"){
	$sql = "select	itm_rcf.site_cd,
					itm_rcf.rowid,
					itm_rcf_book_no,
					itm_rcf_stockno, 
					itm_rcf_stk_loc, 
					itm_rcf_oh_qty, 
					itm_rcf_item_count,
					itm_mst.itm_mst_desc,
					itm_mst_partno= COALESCE(itm_mst.itm_mst_partno,'' ),
					itm_mst_ext_desc= COALESCE(itm_mst.itm_mst_ext_desc,'' ),
					itm_rcf_remark 
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."' and itm_rcf_item_count IS NOT NULL ORDER BY itm_mst_stockno ASC";
					
					
					$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 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) );
}$sql = "select	COUNT(*)AS TotalCount
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."'";



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

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		$total_count = $row['TotalCount'];	
		
     }
} while ( sqlsrv_next_result($stmt) );

$sql = "select	COUNT(*)AS TotalCount
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."' and itm_rcf_item_count IS NULL";



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

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}
do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		$pending_count = $row['TotalCount'];	
		
     }
} while ( sqlsrv_next_result($stmt) );


$sql = "select	COUNT(*)AS TotalCount
					from 	itm_rcf,itm_mst
					Where	itm_rcf.site_cd = itm_mst.site_cd 
					and		itm_rcf.itm_rcf_stockno = itm_mst.itm_mst_stockno 
					and 	itm_rcf_book_no = '".$itm_rcf_book_no."' And itm_rcf.site_cd = '".$site_cd."' and itm_rcf_item_count IS NOT NULL";



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

if( !$stmt ) {
     $error_message = "Error selecting table (itm_rcf)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

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





if ($valid) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json,$total_count,$pending_count,$completed_count);	
}

function returnData($json,$total_count,$pending_count,$completed_count){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Select successfully',
	'TotalCount' => $total_count,
	'PendingCount' => $pending_count,
	'CompletedCount' => $completed_count,
	'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();
}

?>