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


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}

$insert_ntf = false;
$error_message;
$valid = true;

$json = file_get_contents('php://input');

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

$site_cd = $data['site_cd'];
$EmpID = $data['EmpID'];
$EmpName = $data['EmpName'];
$RowID = $data['RowID'];
$dis_Dec = $data['dis_Dec'];
$LOGINID = $data['LOGINID'];



$sql = "SELECT wkr_mst.wkr_mst_wr_status		
		
		FROM 	wkr_mst (NOLOCK),wkr_det (NOLOCK)
		WHERE	wkr_mst.site_cd = '".$site_cd."' 
		AND 	wkr_mst.RowID = '".$RowID."'
		AND		wkr_mst.site_cd = wkr_det.site_cd 
		AND		wkr_mst.RowID = wkr_det.mst_RowID";			
			
		$stmt = sqlsrv_query( $conn, $sql);			
		if( !$stmt ) {
			$error_message = "Error select table (cnt_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
						
				$wkr_mst_wr_status = $row['wkr_mst_wr_status'];						
				 
			}
		} while ( sqlsrv_next_result($stmt));
		sqlsrv_free_stmt( $stmt);	
	
	
	if ($wkr_mst_wr_status === 'W') {
		
		
		$sql = " update wkr_mst SET wkr_mst_wr_status ='D' ,mbl_audit_user =?, mbl_audit_date =getDate() WHERE RowID =?";
		$params_wkr_mst = array($LOGINID,$RowID);		
		$stmt_wkr_mst = sqlsrv_query( $conn, $sql,$params_wkr_mst);	
				
		if( !$stmt_wkr_mst ) {
			$error_message = "Error update table (wkr_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wkr_mst, SQLSRV_FETCH_ASSOC)) {
			 
			 $emp_det_wo_approval_limit = $row['emp_det_wo_approval_limit'];
			
				 
		   }
		} while ( sqlsrv_next_result($stmt_wkr_mst));
		sqlsrv_free_stmt($stmt_wkr_mst);
		
		
		$sql = " update wkr_det SET wkr_det_reject_by =?, wkr_det_reject_date = getDate(), wkr_det_reject_desc =?, mbl_audit_user =?, mbl_audit_date =getDate() WHERE mst_RowID =?";	
		$params_wkr_det = array($EmpID,$dis_Dec,$LOGINID,$RowID);		
		$stmt_wkr_det = sqlsrv_query( $conn, $sql,$params_wkr_det );	
				
		if( !$stmt_wkr_det ) {
			$error_message = "Error update table (wkr_det)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wkr_det, SQLSRV_FETCH_ASSOC)) {
			 
			 $emp_det_wo_approval_limit = $row['emp_det_wo_approval_limit'];
			
				 
		   }
		} while ( sqlsrv_next_result($stmt_wkr_det));
		sqlsrv_free_stmt($stmt_wkr_det);
		
		
    
	} else {
		
		$sql = "SELECT 	wkr_det_wo	
				FROM 	wkr_det (NOLOCK)
				WHERE	wkr_det.site_cd = '".$site_cd."' 
				AND 	wkr_det.mst_RowID = '".$RowID."'";			
			
		$stmt = sqlsrv_query( $conn, $sql);			
		if( !$stmt ) {
			$error_message = "Error select table (cnt_mst)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
			
			$wkr_det_wo = $row['wkr_det_wo'];				
			
			}
		}while ( sqlsrv_next_result($stmt));
		 sqlsrv_free_stmt( $stmt);
		 sqlsrv_rollback( $conn );
		 if ($wkr_mst_wr_status === 'A') {
			$error_message = 'Work Request is already Approved Work Order No: ' .$wkr_det_wo. '';
			returnalert($error_message);
		 }else if ($wkr_mst_wr_status === 'D') {
			 $error_message = 'Work Request is already Disapproved';
			 returnalert($error_message);
		 }
		
	}


if($stmt_wkr_mst && $stmt_wkr_det){
	
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData();
}else{
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}
		
 
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Work Request Disapproved');
	echo json_encode($returnData);
}

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

function returnalert($error_message){	
	
	$returnData = array(
	'status' => 'alert',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}






 
 
?>