<?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'];



$approval_limit = $data['approval_limit'];
$status = $data['status'];
$remark = $data['remark'];
$mtr_app_rowID = $data['mtr_app_rowID'];
$mtr_mst_rowID = $data['mtr_mst_rowID'];


$header_loop = $data['loop'];
$type = $data['type'];
$count = $data['count'];
$LOGINID = $data['LOGINID'];
$header_audit_date = $data['header_audit_date'];
$Mr_tot_cost = $data['Mr_tot_cost'];
$mtr_mst_mtr_no = $data['mtr_mst_mtr_no'];
$mst_costcenter = $data['mst_costcenter'];
$il_level = $data['cur_app_level'];//il_level
$ll_level = $data['level'];//ll_level
$total_level = $data['app_level'];



$sql_mtr_mst ="Select audit_date 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)) {		
		
			$audit_date = $row['audit_date']->format('Y-m-d H:i:s');						
			//echo $audit_date;
		}
		
	} while ( sqlsrv_next_result($stmt_mtr_mst) );
	  sqlsrv_free_stmt( $stmt_mtr_mst);
			  
if($header_audit_date <> $audit_date){
	 //echo $header_audit_date;
	 //echo $audit_date;
				 
	sqlsrv_rollback( $conn );
	$error_message = "The MR audit date had been changed. The record already updated by another user. Please cancel and retrieve the record again.";
	returnError($error_message);
				
}
else{
	
	
	if($header_loop == "1"){
		
		if($type == "my_level"){
			
			if($count == "1"){
				
				$sql_mtr_app = "UPDATE mtr_app 
								SET 	mtr_app_date = getdate(), 
										mtr_app_status = ?, 
										mtr_app_desc = ?,
										audit_user=? ,
										audit_date=getdate(),
										mbl_audit_user=?,		
										mbl_audit_date=getdate() 
								WHERE site_cd=? 
								and RowID = ?";
			
				$params_mtr_app = array($status,$remark,$LOGINID,$LOGINID,$site_cd,$mtr_app_rowID);
				$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);	
					
				if( !$stmt_mtr_app ) {
					$error_message = "Error update table (wko_sts)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}sqlsrv_free_stmt( $stmt_mtr_app);
						
			}
		}else if($type == "my_limit"){
			
			if($count == "0"){
				
				$sql_insert_mtr_app = "	INSERT mtr_app 
										(	site_cd ,					mst_RowID ,					mtr_app_level ,		mtr_app_cost_center , 
											mtr_app_empl_id ,			mtr_app_mr_limit ,			mtr_app_date ,		mtr_app_status , 
											mtr_app_desc,				mtr_app_approval,			audit_user ,	 	audit_date,
											column1,					column2,					column3,			column4,		column5,mbl_audit_user,mbl_audit_date) 
								VALUES 
										(	?,							?,							?,					?,
											?,							?,							GetDate(),			?,
											?,							NUll,						?,					GetDate(),
											NUll,						NUll,						NUll,				NUll,			NUll,?,					GetDate())";

						$params_mtr_app = array($site_cd,$mtr_mst_rowID,$il_level,'***',$EmpID,$approval_limit,$status,$remark,$LOGINID,$LOGINID);
						$stmt_mtr_app = sqlsrv_query( $conn, $sql_insert_mtr_app,$params_mtr_app);	
						if( !$stmt_mtr_app ) {
						$error_message = "Error (INSERT Table mtr_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
							
						}
						sqlsrv_free_stmt($stmt_mtr_app);
				
			}
			
		}
		
		
		$sql_mtr_mst = "UPDATE mtr_mst SET 
									mtr_mst_status = 'D', 
									mtr_mst_cur_app_level = ?, 
									mtr_mst_approver = Null,
									audit_user=? ,
									audit_date=getdate(),
									mbl_audit_user=?,		
									mbl_audit_date=getdate() 
									WHERE site_cd=? 
									and RowID = ?";
					
					
					$params_mtr_mst = array($il_level + 1,$LOGINID,$LOGINID,$site_cd,$mtr_mst_rowID);
					$stmt_update_mtr_mst = sqlsrv_query( $conn, $sql_mtr_mst,$params_mtr_mst);	
							
					if( !$stmt_update_mtr_mst ) {
						$error_message = "Error update table (mtr_mst)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_update_mtr_mst);
		
	}else{
		//Loop 0
		
		if($type == "my_level"){
			
			if($count == "1"){
				
				$sql_mtr_app = "UPDATE mtr_app 
								SET		mtr_app_date = getdate(), 
										mtr_app_status = ?, 
										mtr_app_desc = ?,
										audit_user=? ,
										audit_date=getdate(),
										mbl_audit_user=?,		
										mbl_audit_date=getdate() 
								WHERE site_cd=? 
								and RowID = ?";
					
				
					$params_mtr_app = array($status,$remark,$LOGINID,$LOGINID,$site_cd,$mtr_app_rowID);
					$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);	
						
					if( !$stmt_mtr_app ) {
						$error_message = "Error update table (wko_sts)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_mtr_app);
				
			}
			
		}else if($type == "my_limit"){
			
			if($count == "0"){
				
				$sql_insert_mtr_app = "	INSERT mtr_app 
										(	site_cd ,					mst_RowID ,					mtr_app_level ,		mtr_app_cost_center , 
											mtr_app_empl_id ,			mtr_app_mr_limit ,			mtr_app_date ,		mtr_app_status , 
											mtr_app_desc,				mtr_app_approval,			audit_user ,	 	audit_date,
											column1,					column2,					column3,			column4,		column5,mbl_audit_user,mbl_audit_date) 
								VALUES 
										(	?,							?,							?,					?,
											?,							?,							GetDate(),			?,
											?,							NUll,						?,					GetDate(),
											NUll,						NUll,						NUll,				NUll,			NUll,?,					GetDate())";

						$params_mtr_app = array($site_cd,$mtr_mst_rowID,$ll_level+1,'***',$EmpID,$approval_limit,$status,$remark,$LOGINID,$LOGINID);
						$stmt_mtr_app = sqlsrv_query( $conn, $sql_insert_mtr_app,$params_mtr_app);	
						if( !$stmt_mtr_app ) {
						$error_message = "Error (INSERT Table mtr_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
							
						}
						sqlsrv_free_stmt($stmt_mtr_app);
				
			}
			
			
		}else if($type == "all_level"){
			
			if($count == "1"){
				
				$sql_mtr_app = "UPDATE 	mtr_app 
								SET 	mtr_app_date = getdate(), 
										mtr_app_status = ?, 
										mtr_app_desc = ?,
										audit_user=? ,
										audit_date=getdate(),
										mbl_audit_user=?,		
										mbl_audit_date=getdate() 
								WHERE site_cd=? 
								and RowID = ?";
					
				
					$params_mtr_app = array($status,$remark,$LOGINID,$LOGINID,$site_cd,$mtr_app_rowID);
					$stmt_mtr_app = sqlsrv_query( $conn, $sql_mtr_app,$params_mtr_app);	
						
					if( !$stmt_mtr_app ) {
						$error_message = "Error update table (wko_sts)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_mtr_app);
				
			}
			
			
			
			
		}
		
		$sql_mtr_mst = "UPDATE mtr_mst SET 
									mtr_mst_status = 'D', 
									mtr_mst_cur_app_level = ?, 
									mtr_mst_approver = Null,
									audit_user=? ,
									audit_date=getdate(),
									mbl_audit_user=?,		
									mbl_audit_date=getdate() 
									WHERE site_cd=? 
									and RowID = ?";
					
					
					$params_mtr_mst = array($ll_level ,$LOGINID,$LOGINID,$site_cd,$mtr_mst_rowID);
					$stmt_update_mtr_mst = sqlsrv_query( $conn, $sql_mtr_mst,$params_mtr_mst);	
							
					if( !$stmt_update_mtr_mst ) {
						$error_message = "Error update table (mtr_mst)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_update_mtr_mst);
					
					
					$sql_mtr_ls1 = "UPDATE mtr_ls1 SET 
									mtr_ls1_status = 'D',									
									audit_user=? ,
									audit_date=getdate(),
									mbl_audit_user=?,		
									mbl_audit_date=getdate() 
									WHERE site_cd=? 
									and mst_RowID = ?";
					
					
					$params_mtr_ls1 = array($LOGINID,$LOGINID,$site_cd,$mtr_mst_rowID);
					$stmt_update_mtr_ls1 = sqlsrv_query( $conn, $sql_mtr_ls1,$params_mtr_ls1);	
							
					if( !$stmt_update_mtr_ls1 ) {
						$error_message = "Error update table (mtr_ls1)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_update_mtr_ls1);
		
	}
	
	
}
	
	
			  
			  



if($stmt_mtr_mst){
	if($header_audit_date <> $audit_date){
				 
				
			
	}else{
		
		if($header_loop == "1"){
			
			if($stmt_update_mtr_mst){
					sqlsrv_commit( $conn );
					sqlsrv_close( $conn);	
					returnData();
			}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
			}
						
		}else{
			if($stmt_update_mtr_mst && $stmt_update_mtr_ls1){
							
					sqlsrv_commit( $conn );
					sqlsrv_close( $conn);	
					returnData();
			
			}else{
					sqlsrv_rollback( $conn );
					$error_message = "Transaction rolled back.<br />";
					returnError($error_message); 
			}
		}

		
	}
	
}else{
	sqlsrv_rollback( $conn );
	$error_message = "Transaction rolled back.<br />";
	returnError($error_message);
	
}

function returnData(){
	$returnData = array(
		'status' => 'SUCCESS',
		'message' => 'The material request successfully disapproved.');
	echo json_encode($returnData);
}

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


 
?>