<?php
require_once('config.php');
$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}
 
$site_cd = $_REQUEST['site_cd'];
$itm_mst_stockno = $_REQUEST['itm_mst_stockno'];


$sql = "select  Vadlidation_1 = Count(*) 
			from itm_mst (NOLOCK)
			where site_cd = '".$site_cd."'
			and itm_mst_stockno = '".$itm_mst_stockno."'";			
			
			
			$stmt = sqlsrv_query( $conn, $sql);			
			if( !$stmt ) {
				$error_message = "Error update table (itm_mst)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			do {
				while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
				
				$Vadlidation_1 = $row['Vadlidation_1'];

				
				
					 	 
			}
		} while ( sqlsrv_next_result($stmt));
		
		
		$sql = "select	 Vadlidation_2 = Count(*) 
				from	itm_mst (NOLOCK), itm_det (NOLOCK)
				where	itm_mst.site_cd = itm_det.site_cd
				and		itm_mst.rowid = itm_det.mst_rowid
				and		itm_mst.site_cd = '".$site_cd."' 
				and		itm_mst_stockno = '".$itm_mst_stockno."'
				and		itm_det_part_deac_status = 'DEA'";			
			
			
			$stmt = sqlsrv_query( $conn, $sql);			
			if( !$stmt ) {
				$error_message = "Error update table (itm_mst)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			do {
				while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
				
				$Vadlidation_2 = $row['Vadlidation_2'];

				
				
					 	 
			}
		} while ( sqlsrv_next_result($stmt));
		
		//echo $Vadlidation_1;
		//echo $Vadlidation_2;
		
		if($Vadlidation_1 =='1' && $Vadlidation_2  =='0'){
			$valid =true;
			
		}else{
			$valid =false;
			
		}


returnData($valid);
//sqlsrv_free_stmt( $stmt);

sqlsrv_close($conn);


function returnData($valid){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'data' => $valid);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);	
	
	echo json_encode($returnData);
		
	exit();
}
 
?>