<?php

require_once('config.php');

$error_message;
$valid = true;

// Takes raw data from the request
$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);

$itemcount = $data['count'];
$rowid = $data['rowid'];
$site_cd = $data['site_cd'];
$count_book = $data['count_book'];
$remark = $data['remark'];
$itm_rcb_completed_by = $data['itm_rcb_completed_by'];
$LOGINID = $data['LOGINID'];
//echo $itemcount;
//echo $rowid;
//echo $site_cd;
//echo $count_book;

$sql = "UPDATE itm_rcf SET itm_rcf_item_count = ".$itemcount." ,itm_rcf_remark = '".$remark."' WHERE RowID = ".$rowid." And itm_rcf.site_cd = '".$site_cd."'";

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

if( !$stmt ) {
     $error_message = "Error updating 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 = "update 	itm_rcb 
		SET 	itm_rcb_item_count = (Select Count(*) From itm_rcf Where itm_rcf.itm_rcf_book_no = '".$count_book."' 
				And itm_rcf_item_count IS NOT NULL And itm_rcf.site_cd = '".$site_cd."'), 
				itm_rcb_count_flag ='1' , 
				itm_rcb_count_date = GetDate ( ),
				mbl_audit_user='".$LOGINID."',		
				mbl_audit_date=getdate()				
				WHERE itm_rcb.site_cd = '".$site_cd."' 
				AND itm_rcb.itm_rcb_book_no = '".$count_book."'";

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

if( !$stmt ) {
     $error_message = "Error updating table (itm_rcb)";
	 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 = "UPDATE itm_rcb 
		SET itm_rcb_completed = 1,
		itm_rcb_completed_by = '".$itm_rcb_completed_by."' WHERE itm_rcb.site_cd = '".$site_cd."' AND itm_rcb.itm_rcb_book_no = '".$count_book."' AND itm_rcb_total_item = (	SELECT COUNT(*) FROM itm_rcf WHERE itm_rcb.site_cd = itm_rcf.site_cd AND itm_rcb.itm_rcb_book_no = itm_rcf.itm_rcf_book_no AND itm_rcf_item_count IS NOT NULL )";

//echo $itm_rcb_completed_by;
//echo $sql;
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error updating table (itm_rcb)";
	 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' => 'Update 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);		
	exit();
}

?>