<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;



/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), 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'];
$mtr_mst_mtr_no = $data['mtr_mst_mtr_no'];
$loop = $data['loop']; 	
$type = $data['type'];
$mst_costcenter = $data['mst_costcenter'];
$RowID = $data['RowID'];


if($type == "my_level")
{
	
	if($loop == "0"){
		
		$sql ="Select 	emp_ls3_level 
						from 	cf_cost_center ( NOLOCK ) , emp_ls3 ( NOLOCK )  
						Where 	cf_cost_center.site_cd = emp_ls3.site_cd 
						AND 	cf_cost_center.costcenter =emp_ls3.emp_ls3_costcenter 
						AND 	emp_ls3.emp_ls3_approver_flag ='1' 
						AND 	cf_cost_center.site_cd =? 
						AND 	cf_cost_center.costcenter =? 
						AND 	emp_ls3.emp_ls3_empl_id =?";
		
		$params = array($site_cd,$mst_costcenter,$EmpID);

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

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

		do {
			
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
			
				$emp_ls3_level= $row['emp_ls3_level'];				
			
			}
			
		} while ( sqlsrv_next_result($stmt) );
		  sqlsrv_free_stmt( $stmt);
		
		
		
		$sql_mtr_app ="	Select * from mtr_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And mtr_app_level = ?
						And mtr_app_empl_id = ?";
			
		$params_mtr_app = array($site_cd,$RowID,$emp_ls3_level,$EmpID);

		$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);

		if( !$stmt_mtr_app ) {
			
			 $error_message = "Error selecting mtr_app";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_app) );
		  sqlsrv_free_stmt( $stmt_mtr_app);
		  
		  
	
	}else{
	
	$sql_mtr_mst ="Select RowID, mtr_mst_cur_app_level from mtr_mst (NOLOCK)  Where site_cd = ? and  mtr_mst_mtr_no = ?";
		
		$params_mtr_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_mtr_mst = sqlsrv_query( $conn, $sql_mtr_mst,$params_mtr_mst);

		if( !$stmt_mtr_mst ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	

		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_mst, SQLSRV_FETCH_ASSOC)) {		
			
				$mtr_mst_RowID= $row['RowID'];	
				$mtr_mst_cur_app_level= $row['mtr_mst_cur_app_level'];		
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_mst) );
		  sqlsrv_free_stmt( $stmt_mtr_mst);


		$sql_mtr_app ="	Select * from mtr_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And mtr_app_level = ?
						And mtr_app_empl_id = ?";
			
		$params_mtr_app = array($site_cd,$mtr_mst_RowID,$mtr_mst_cur_app_level + 1,$EmpID);

		$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);

		if( !$stmt_mtr_app ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_app) );
		  sqlsrv_free_stmt( $stmt_mtr_app);
	
		
		
	}
	
	
}else if ($type == "my_limit")
{
	
	
	if($loop == "1"){
		$sql_mtr_mst ="Select RowID, mtr_mst_cur_app_level from mtr_mst (NOLOCK)  Where site_cd = ? and  mtr_mst_mtr_no = ?";
		
		$params_mtr_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_mtr_mst = sqlsrv_query( $conn, $sql_mtr_mst,$params_mtr_mst);

		if( !$stmt_mtr_mst ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	

		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_mst, SQLSRV_FETCH_ASSOC)) {		
			
				$mtr_mst_RowID= $row['RowID'];	
				$mtr_mst_cur_app_level= $row['mtr_mst_cur_app_level'];		
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_mst) );
		  sqlsrv_free_stmt( $stmt_mtr_mst);


		$sql_mtr_app ="	Select * from mtr_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And mtr_app_level = ?
						And mtr_app_empl_id = ?";
			
		$params_mtr_app = array($site_cd,$mtr_mst_RowID,$mtr_mst_cur_app_level + 1,$EmpID);

		$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);

		if( !$stmt_mtr_app ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_app) );
		  sqlsrv_free_stmt( $stmt_mtr_app);
	}else{
		$json_array = array();
	}
	
	
}
else if ($type == "all_level")
{
	
			
		if($loop == "0"){
		
		$sql ="Select 	emp_ls3_level 
						from 	cf_cost_center ( NOLOCK ) , emp_ls3 ( NOLOCK )  
						Where 	cf_cost_center.site_cd = emp_ls3.site_cd 
						AND 	cf_cost_center.costcenter =emp_ls3.emp_ls3_costcenter 
						AND 	emp_ls3.emp_ls3_approver_flag ='1' 
						AND 	cf_cost_center.site_cd =? 
						AND 	cf_cost_center.costcenter =? 
						AND 	emp_ls3.emp_ls3_empl_id =?";
		
		$params = array($site_cd,$mst_costcenter,$EmpID);

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

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

		do {
			
			while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
			
				$emp_ls3_level= $row['emp_ls3_level'];				
			
			}
			
		} while ( sqlsrv_next_result($stmt) );
		  sqlsrv_free_stmt( $stmt);
		
		
		
		$sql_mtr_app ="	Select * from mtr_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And mtr_app_level = ?
						And mtr_app_empl_id = ?";
			
		$params_mtr_app = array($site_cd,$RowID,$emp_ls3_level,$EmpID);

		$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);

		if( !$stmt_mtr_app ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_app) );
		  sqlsrv_free_stmt( $stmt_mtr_app);
		  
		  
	
	}else{
	
	$sql_mtr_mst ="Select RowID, mtr_mst_cur_app_level from mtr_mst (NOLOCK)  Where site_cd = ? and  mtr_mst_mtr_no = ?";
		
		$params_mtr_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_mtr_mst = sqlsrv_query( $conn, $sql_mtr_mst,$params_mtr_mst);

		if( !$stmt_mtr_mst ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	

		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_mst, SQLSRV_FETCH_ASSOC)) {		
			
				$mtr_mst_RowID= $row['RowID'];	
				$mtr_mst_cur_app_level= $row['mtr_mst_cur_app_level'];		
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_mst) );
		  sqlsrv_free_stmt( $stmt_mtr_mst);


		$sql_mtr_app ="	Select * from mtr_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And mtr_app_level = ?
						And mtr_app_empl_id = ?";
			
		$params_mtr_app = array($site_cd,$mtr_mst_RowID,$mtr_mst_cur_app_level + 1,$EmpID);

		$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);

		if( !$stmt_mtr_app ) {
			
			 $error_message = "Error selecting mtr_mst";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_mtr_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_mtr_app) );
		  sqlsrv_free_stmt( $stmt_mtr_app);
	
		
		
	}	
			
	
}




if($type == "my_level")
{
	
	if($loop == "0"){
		
		if($stmt && $stmt_mtr_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
	
	}else{
	
	
		if($stmt_mtr_mst && $stmt_mtr_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
		
	}
	
	
}else if ($type == "my_limit")
{
	if($loop == "1"){
		if($stmt_mtr_mst && $stmt_mtr_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
	}else{
		sqlsrv_commit( $conn );
		returnData($json_array);
	}
	
}
else if ($type == "all_level")
{
	
			
		if($loop == "0"){
		
		if($stmt && $stmt_mtr_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
	
	}else{
	
	
		if($stmt_mtr_mst && $stmt_mtr_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
		
	}	
			
	
}



function returnData($json_array){
	$json1 = json_encode($json_array);
	//echo$json;
	 
	if(empty(json_decode($json1,1))) {
		 $returnData = array(
		'status' => 'SUCCESS',
		'message' => 'No Records found',		
		'data' => $json_array);
	}else{
		$returnData = array(
		'status' => 'SUCCESS',
		'message' => 'Successfully',			
		'data' => $json_array);
	}

	echo json_encode($returnData);
}

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


 
?>