<?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'];
$LOGINID = $data['LOGINID'];

$pur_mst_porqnnum = $data['pur_mst_porqnnum'];
$pur_mst_rowID = $data['pur_mst_rowID'];
$remark = $data['remark'];
$header_audit_date = $data['header_audit_date'];
$dft_mst_pur_email_approver = $data['dft_mst_pur_email_approver'];
$dft_mst_pur_email_requestor = $data['dft_mst_pur_email_requestor'];
$dft_mst_generate_po = $data['dft_mst_generate_po'];
$pur_mst_status = $data['pur_mst_status'];


$sql_pur_mst ="Select audit_date from pur_mst (NOLOCK)  Where site_cd = ? and  RowID = ?";
			
	$params_pur_mst = array($site_cd,$pur_mst_rowID);

	$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 PR audit date had been changed. The record already updated by another user. Please cancel and retrieve the record again.";
returnError($error_message);
				
}else{

	  
$sql_pur_det = "UPDATE pur_det SET 
				pur_det_note1 = ?,				
				audit_user=?,
				audit_date=getdate(),
				mbl_audit_user=?,		
				mbl_audit_date=getdate() 
				WHERE site_cd=? 
				and mst_rowid = ?";


$params_pur_det = array($remark ,$LOGINID,$LOGINID,$site_cd,$pur_mst_rowID);
$stmt_update_pur_det = sqlsrv_query( $conn, $sql_pur_det,$params_pur_det);	
		
if( !$stmt_update_pur_det ) {
	$error_message = "Error update table (mtr_det)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt($stmt_update_pur_det);



$sql_pur_app ="SELECT	ll_empl_cnt = Count(pur_app_empl_id), ls_approver = Min(pur_app_empl_id)
				FROM	pur_app (NOLOCK)
				WHERE	site_cd = ?
				AND		mst_rowid = ?
				AND		pur_app_level= ?";

	$params_pur_app = array($site_cd,$pur_mst_rowID,1);

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

	if( !$stmt_select_pur_app ) {

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

	do {

	while ($row = sqlsrv_fetch_array($stmt_select_pur_app, SQLSRV_FETCH_ASSOC)) {		

		$ll_empl_cnt = $row['ll_empl_cnt'];		
		$ls_approver = $row['ls_approver'];

				//echo $ll_empl_cnt ;		
				//echo $ls_approver ;											

	}

	} while ( sqlsrv_next_result($stmt_select_pur_app) );
	sqlsrv_free_stmt( $stmt_select_pur_app);  
					
	if($ll_empl_cnt == 1){
		
		$sql_pur_mst = "UPDATE pur_mst  SET 
						pur_mst_purq_approve = 'W',	
						pur_mst_status = '".$pur_mst_status."',	
						pur_mst_cur_app_level = 0 ,	
						pur_mst_approver ='".$ls_approver."',
						pur_mst_release_for_app = '0',						
						audit_user='".$LOGINID."',
						audit_date=getdate(),
						mbl_audit_user='".$LOGINID."',		
						mbl_audit_date=getdate() 
						WHERE site_cd='".$site_cd."' 
						and RowID ='".$pur_mst_rowID."'";


			
			$stmt_update_pur_mst = sqlsrv_query( $conn, $sql_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 if($ll_empl_cnt > 1){
		
		$sql_pur_mst = "UPDATE pur_mst  SET 
						pur_mst_purq_approve = 'W',	
						pur_mst_status = '".$pur_mst_status."',	
						pur_mst_cur_app_level = 0 ,	
						pur_mst_approver ='".$ls_approver."'+ '...',
						pur_mst_release_for_app = '0',						
						audit_user='".$LOGINID."',
						audit_date=getdate(),
						mbl_audit_user='".$LOGINID."',		
						mbl_audit_date=getdate()  
						WHERE site_cd='".$site_cd."' 
						and RowID ='".$pur_mst_rowID."'";


			
			$stmt_update_pur_mst = sqlsrv_query( $conn, $sql_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 if($ll_empl_cnt == 0){
		
		
		$sql_pur_mst = "UPDATE pur_mst  SET 
						pur_mst_purq_approve = 'W',	
						pur_mst_status = '".$pur_mst_status."',	
						pur_mst_cur_app_level = 0 ,	
						pur_mst_approver =Null,
						pur_mst_release_for_app = '0',						
						audit_user='".$LOGINID."',
						audit_date=getdate(),
						mbl_audit_user='".$LOGINID."',		
						mbl_audit_date=getdate()  
						WHERE site_cd='".$site_cd."' 
						and RowID ='".$pur_mst_rowID."'";


			
			$stmt_update_pur_mst = sqlsrv_query( $conn, $sql_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);
		
		
	}			


		$sql_pur_app = "UPDATE pur_app  SET 
						pur_app_status = 'P',	
						pur_app_desc = Null ,	
						pur_app_date =Null,												
						audit_user='".$LOGINID."',
						audit_date=getdate(),
						mbl_audit_user='".$LOGINID."',		
						mbl_audit_date=getdate()  
						WHERE site_cd='".$site_cd."' 
						and mst_RowID ='".$pur_mst_rowID."'";

		$stmt_update_pur_app = sqlsrv_query( $conn, $sql_pur_app);	
				
		if( !$stmt_update_pur_app ) {
			$error_message = "Error update table (pur_app)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_update_pur_app);
		
		
		
		$sql_pur_ls2 = "UPDATE pur_ls2  SET 
						pur_ls2_datetime2 = GETDATE(),	
						pur_ls2_numeric1 = '0',												
						audit_user='".$LOGINID."',
						audit_date=GETDATE()
						WHERE pur_ls2_datetime2 is NULL 
						and site_cd='".$site_cd."'
						and mst_RowID ='".$pur_mst_rowID."'";

		$stmt_update_pur_ls2 = sqlsrv_query( $conn, $sql_pur_ls2);	
				
		if( !$stmt_update_pur_ls2 ) {
			$error_message = "Error update table (pur_ls2)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_update_pur_ls2);
		
		
		$sql_insert_pur_ls2 = "INSERT pur_ls2 
					(		site_cd,				mst_RowID ,				pur_ls2_varchar1,		pur_ls2_varchar2,		
							pur_ls2_varchar3,		pur_ls2_datetime1, 		pur_ls2_datetime2,		pur_ls2_numeric1,		
							audit_user,				audit_date,				mbl_audit_user,			mbl_audit_date ) 
			VALUES  (		?,						?,						?,						?,					
							?,						GETDATE (), 			NULL,					NULL,					
							?,						GETDATE (),				?,						GETDATE ())";
								
								
	$params_pur_ls2 = array($site_cd,$pur_mst_rowID,$pur_mst_porqnnum,$pur_mst_status,$EmpID,$LOGINID,$LOGINID);				
	$stmt_pur_ls2 = sqlsrv_query( $conn, $sql_insert_pur_ls2,$params_pur_ls2);

	if( !$stmt_pur_ls2 ) {
		$error_message = "Error insert table (INSERT Table pur_ls2)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_pur_ls2);
		
		
	
	


if($stmt_update_pur_det){
	
	if($stmt_select_pur_app){
		
		if($ll_empl_cnt == 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($ll_empl_cnt > 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($ll_empl_cnt == 0){
			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); 
	}	

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

}


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

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


 
?>