<?php 
include('includes/config/config.php');
$query="";
//echo $_REQUEST['flex_appointment_id'];
if(!empty($_REQUEST['flex_appointment_id'])){
	//echo $query = " delete from employee_appointment where appointment_id = '".$_REQUEST['flex_appointment_id']."'";
	 $query = "DELETE FROM guest_appointment,guest_treatment USING guest_appointment left JOIN guest_treatment on (guest_appointment.appointment_id = guest_treatment.appointment_id) WHERE guest_appointment.appointment_id= '".$_REQUEST['flex_appointment_id']."'";
	 

	  // echo $query;
	
	  
	  $query_appointment="select * from guest_appointment_stock_history where  appointment_id='".$_REQUEST['flex_appointment_id']."' ";
	   //echo $query_appointment;
	  $result_stock = @mysqli_query($conn,$query_appointment);
	 while($row_stock=@mysqli_fetch_array($result_stock)){
		 $issued_qty=$row_stock['issued_qty'];
		  //echo "iss".$issued_qty;
		   $current_stock_level=0;
	 $select_query="select * from  tbl_items where item_id='".$row_stock['item_id']."' ";
	  //echo  $select_query;
	 $select_result = @mysqli_query($conn,$select_query);
	
	   while($row_select=@mysqli_fetch_array($select_result)){
		    $current_stock_level=$row_select['current_stock_level'];
	       // echo "cuu".$row_select['current_stock_level'];
	   }
	  
	  
	   $current_stock_qty=$current_stock_level+$issued_qty;
	  // echo "current_stock_qty".$current_stock_qty;
	   $query_stock = "update tbl_items SET  current_stock_level='".$current_stock_qty."' where  item_id='".$row_stock['item_id']."' ";
	    @mysqli_query($conn,$query_stock);
	 }
	  
	  
	   $query1="DELETE from guest_appointment_stock_history where guest_appointment_stock_history.appointment_id='".$_REQUEST['flex_appointment_id']."' ";
	  $result = @mysqli_query($conn,$query);
	  $result1 = @mysqli_query($conn,$query1);
	   
	if (!$result&&!$result1) {
		
				exit(mysqli_error($conn));
				echo json_encode("FAILURE");
	}else{
			echo json_encode("SUCCESS");
	}
	
}

?>