<?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_ls2.emp_ls2_level  
						from 	cf_cost_center ( NOLOCK ) , emp_ls2 ( NOLOCK )  
						Where 	cf_cost_center.site_cd = emp_ls2.site_cd 
						AND 	cf_cost_center.costcenter =emp_ls2.emp_ls2_costcenter 
						AND 	emp_ls2.emp_ls2_approver ='1' 
						AND 	cf_cost_center.site_cd =? 
						AND 	cf_cost_center.costcenter =? 
						AND 	emp_ls2.emp_ls2_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_ls2_level= $row['emp_ls2_level'];				
			
			}
			
		} while ( sqlsrv_next_result($stmt) );
		  sqlsrv_free_stmt( $stmt);
		
		
		
		$sql_pur_app ="	Select * from pur_app (NOLOCK)
						Where pur_app.site_cd = ?
						And mst_RowID = ?
						And pur_app.pur_app_level = ?
						And pur_app.pur_app_empl_id = ?";
			
		$params_pur_app = array($site_cd,$RowID,$emp_ls2_level,$EmpID);

		$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);

		if( !$stmt_pur_app ) {
			
			 $error_message = "Error selecting pur_app";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_pur_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_pur_app) );
		  sqlsrv_free_stmt( $stmt_pur_app);
		  
		  
	
	}else{
	
	$sql_pur_mst ="Select RowID, pur_mst_cur_app_level from pur_mst (NOLOCK)  Where site_cd = ? and  pur_mst_porqnnum = ?";
		
		$params_pur_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_pur_mst = sqlsrv_query( $conn, $sql_pur_mst,$params_pur_mst);

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

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


		$sql_pur_app ="	Select * from pur_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And pur_app_level = ?
						And pur_app_empl_id = ?";
			
		$params_pur_app = array($site_cd,$mtr_mst_RowID,$pur_mst_cur_app_level + 1,$EmpID);

		$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);

		if( !$stmt_pur_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_pur_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_pur_app) );
		  sqlsrv_free_stmt( $stmt_pur_app);
	
		
		
	}
	
	
}else if ($type == "my_limit")
{
	
	
	if($loop == "1"){
		$sql_pur_mst ="Select RowID, pur_mst_cur_app_level from pur_mst (NOLOCK)  Where site_cd = ? and  pur_mst_porqnnum = ?";
		
		$params_pur_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_pur_mst = sqlsrv_query( $conn, $sql_pur_mst,$params_pur_mst);

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

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


		$sql_pur_app ="	Select * from pur_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And pur_app_level = ?
						And pur_app_empl_id = ?";
			
		$params_pur_app = array($site_cd,$mtr_mst_RowID,$pur_mst_cur_app_level + 1,$EmpID);

		$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);

		if( !$stmt_pur_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_pur_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_pur_app) );
		  sqlsrv_free_stmt( $stmt_pur_app);
	}else{
		$json_array = array();
	}
	
	
}
else if ($type == "all_level")
{
	
			
		if($loop == "0"){
		
		$sql ="Select 	emp_ls2.emp_ls2_level  
						from 	cf_cost_center ( NOLOCK ) , emp_ls2 ( NOLOCK )  
						Where 	cf_cost_center.site_cd = emp_ls2.site_cd 
						AND 	cf_cost_center.costcenter =emp_ls2.emp_ls2_costcenter 
						AND 	emp_ls2.emp_ls2_approver ='1' 
						AND 	cf_cost_center.site_cd =? 
						AND 	cf_cost_center.costcenter =? 
						AND 	emp_ls2.emp_ls2_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_ls2_level= $row['emp_ls2_level'];				
			
			}
			
		} while ( sqlsrv_next_result($stmt) );
		  sqlsrv_free_stmt( $stmt);
		
		
		
		$sql_pur_app ="	Select * from pur_app (NOLOCK)
						Where pur_app.site_cd = ?
						And mst_RowID = ?
						And pur_app.pur_app_level = ?
						And pur_app.pur_app_empl_id = ?";
			
		$params_pur_app = array($site_cd,$RowID,$emp_ls2_level,$EmpID);

		$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);

		if( !$stmt_pur_app ) {
			
			 $error_message = "Error selecting pur_app";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
			 
		}	
				$json_array = array();
		do {
			
			while ($row = sqlsrv_fetch_array($stmt_pur_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_pur_app) );
		  sqlsrv_free_stmt( $stmt_pur_app);
		  
		  
	
	}else{
	
	$sql_pur_mst ="Select RowID, pur_mst_cur_app_level from pur_mst (NOLOCK)  Where site_cd = ? and  pur_mst_porqnnum = ?";
		
		$params_pur_mst = array($site_cd,$mtr_mst_mtr_no);

		$stmt_pur_mst = sqlsrv_query( $conn, $sql_pur_mst,$params_pur_mst);

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

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


		$sql_pur_app ="	Select * from pur_app (NOLOCK)
						Where site_cd = ?
						And mst_RowID = ?
						And pur_app_level = ?
						And pur_app_empl_id = ?";
			
		$params_pur_app = array($site_cd,$mtr_mst_RowID,$pur_mst_cur_app_level + 1,$EmpID);

		$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);

		if( !$stmt_pur_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_pur_app, SQLSRV_FETCH_ASSOC)) {		
			
					$json_array[]= $row;
			
			}
			
		} while ( sqlsrv_next_result($stmt_pur_app) );
		  sqlsrv_free_stmt( $stmt_pur_app);
	
		
		
	}	
			
	
}




if($type == "my_level")
{
	
	if($loop == "0"){
		
		if($stmt && $stmt_pur_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
	
	}else{
	
	
		if($stmt_pur_mst && $stmt_pur_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_pur_mst && $stmt_pur_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_pur_app){
					
			sqlsrv_commit( $conn );
			returnData($json_array);
		}else{
			
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		}
	
	}else{
	
	
		if($stmt_pur_mst && $stmt_pur_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);
		
}


 
?>