<?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_pur_mst ="Select audit_date 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 mtr_mst";
		 returnError($error_message);
		 die( print_r( sqlsrv_errors(), true));
		 
	}	

	do {
		
		while ($row = sqlsrv_fetch_array($stmt_pur_mst, SQLSRV_FETCH_ASSOC)) {		
		
			$audit_date = $row['audit_date']->format('Y-m-d H:i:s');						
			//echo $audit_date;
		}
		
	} while ( sqlsrv_next_result($stmt_pur_mst) );
	  sqlsrv_free_stmt( $stmt_pur_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_pur_app = "UPDATE pur_app SET pur_app_date = getdate(), pur_app_status = ?, pur_app_desc = ?,audit_user=? ,audit_date=getdate() WHERE site_cd=? and RowID = ?";
					
				
					$params_pur_app = array($status,$remark,$LOGINID,$site_cd,$mtr_app_rowID);
					$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);	
						
					if( !$stmt_pur_app ) {
						$error_message = "Error update table (pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt($stmt_pur_app);
						
			}
		}else if($type == "my_limit"){
			
			if($count == "0"){
				
				$sql_insert_pur_app = "	INSERT pur_app 
										(	site_cd ,					mst_RowID ,					pur_app_level ,		pur_app_cost_center , 
											pur_app_empl_id ,			pur_app_pr_limit ,			pur_app_date ,		pur_app_status , 
											pur_app_desc,				pur_app_approval,			audit_user ,	 	audit_date,
											column1,					column2,					column3,			column4,		column5) 
								VALUES 
										(	?,							?,							?,					?,
											?,							?,							GetDate(),			?,
											?,							NUll,						?,					GetDate(),
											NUll,						NUll,						NUll,				NUll,			NUll)";

						$params_pur_app = array($site_cd,$mtr_mst_rowID,$il_level+1,'***',$EmpID,$approval_limit,$status,$remark,$LOGINID);
						$stmt_pur_app = sqlsrv_query( $conn, $sql_insert_pur_app,$params_pur_app);	
						if( !$stmt_pur_app ) {
						$error_message = "Error (INSERT Table pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
							
						}
						sqlsrv_free_stmt($stmt_pur_app);
				
			}
			
		}
		
		
					$sql_pur_mst = "UPDATE pur_mst SET 
									pur_mst_purq_approve = 'D', 
									pur_mst_cur_app_level = ?, 
									pur_mst_approver = Null,
									audit_user=? ,
									audit_date=getdate() 
									WHERE site_cd=? 
									and RowID = ?";
					
					
					$params_pur_mst = array($il_level + 1,$LOGINID,$site_cd,$mtr_mst_rowID);
					$stmt_update_pur_mst = sqlsrv_query( $conn, $sql_pur_mst,$params_pur_mst);	
							
					if( !$stmt_update_pur_mst ) {
						$error_message = "Error update table (pur_mst)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_update_pur_mst);
		
	}else{
		//Loop 0
		
		if($type == "my_level"){
			
			if($count == "1"){
				
				$sql_pur_app = "UPDATE pur_app SET pur_app_date = getdate(), pur_app_status = ?, pur_app_desc = ?,audit_user=? ,audit_date=getdate() WHERE site_cd=? and RowID = ?";
					
				
					$params_pur_app = array($status,$remark,$LOGINID,$site_cd,$mtr_app_rowID);
					$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);	
						
					if( !$stmt_pur_app ) {
						$error_message = "Error update table (pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt($stmt_pur_app);
				
			}
			
		}else if($type == "my_limit"){
			
			if($count == "0"){
				
				$sql_insert_pur_app = "	INSERT pur_app 
										(	site_cd ,					mst_RowID ,					pur_app_level ,		pur_app_cost_center , 
											pur_app_empl_id ,			pur_app_pr_limit ,			pur_app_date ,		pur_app_status , 
											pur_app_desc,				pur_app_approval,			audit_user ,	 	audit_date,
											column1,					column2,					column3,			column4,		column5) 
								VALUES 
										(	?,							?,							?,					?,
											?,							?,							GetDate(),			?,
											?,							NUll,						?,					GetDate(),
											NUll,						NUll,						NUll,				NUll,			NUll)";

						$params_pur_app = array($site_cd,$mtr_mst_rowID,$il_level+1,'***',$EmpID,$approval_limit,$status,$remark,$LOGINID);
						$stmt_pur_app = sqlsrv_query( $conn, $sql_insert_pur_app,$params_pur_app);	
						if( !$stmt_pur_app ) {
						$error_message = "Error (INSERT Table pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
							
						}
						sqlsrv_free_stmt($stmt_pur_app);
				
			}
			
			
		}else if($type == "all_level"){
			
			if($count == "1"){
				
				$sql_pur_app = "UPDATE pur_app SET pur_app_date = getdate(), pur_app_status = ?, pur_app_desc = ?,audit_user=? ,audit_date=getdate() WHERE site_cd=? and RowID = ?";
					
				
					$params_pur_app = array($status,$remark,$LOGINID,$site_cd,$mtr_app_rowID);
					$stmt_pur_app = sqlsrv_query( $conn, $sql_pur_app,$params_pur_app);	
						
					if( !$stmt_pur_app ) {
						$error_message = "Error update table (pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt($stmt_pur_app);
				
			}
			
			
			
			
		}
		
					$sql_pur_mst = "UPDATE pur_mst SET 
									pur_mst_purq_approve = 'D', 
									pur_mst_cur_app_level = ?, 
									pur_mst_approver = Null,
									audit_user=? ,
									audit_date=getdate() 
									WHERE site_cd=? 
									and RowID = ?";
					
					
					$params_pur_mst = array($ll_level ,$LOGINID,$site_cd,$mtr_mst_rowID);
					$stmt_update_pur_mst = sqlsrv_query( $conn, $sql_pur_mst,$params_pur_mst);	
							
					if( !$stmt_update_pur_mst ) {
						$error_message = "Error update table (pur_mst)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_update_pur_mst);
					
					
					
		
	}
	
	
}
	
	
			  
			  



if($stmt_pur_mst){
	if($header_audit_date <> $audit_date){
				 
				
			
	}else{
		
		if($header_loop == "1"){
			
			if($stmt_update_pur_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_pur_mst ){
							
					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 Purchase Request Successfully Disapproved.');
	echo json_encode($returnData);
}

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


 
?>