<?php
session_start();

include("dbConfig.php");
include("../Function/level_salf.php");
$id = $_POST["id"]; 
$employy_id = $_POST["employy_id"];
$amount = $_POST["amount"];
$company_id = $_POST["company_id"];

$add_user_id = $_POST["add_user_id"];

date_default_timezone_set("Asia/Riyadh");
  $created_date = date("Y-m-d");
  $createdDate_time = date("Y-m-d h:i:sa");
  
  $lang = $_POST["lang"];
 $messag_add = $_POST["messag_add"];
 
 // حساب رقم المستوى
 $sql_num_level="SELECT * FROM hr_salf_steps_tbl where salf_id='$id' ";
$result_num_level = $conn->query($sql_num_level);
$rowcount=mysqli_num_rows($result_num_level);
 $rowcount1 = $rowcount+1;   
 //
 $salf_level = salf_level($id);
 // get the last record
 $lastidofsalfstat = getthelastidofsalfstat($id);
 // end get the last record
 $promotionrefuselevel = promotionrefuselevel($employy_id);
 $level_refusedetailsar = level_refusedetailsar($employy_id);
 $level_refusedetailsen = level_refusedetailsen($employy_id);
 
if($promotionrefuselevel=="final_refuse"){
  $sql_request = "UPDATE hr_salf_tbl SET stat='5',detailsar='".$level_refusedetailsar."',detailsen='".$level_refusedetailsen."',employ_approval_id='$add_user_id' WHERE id='$id'";
  $update_request_steps = "UPDATE hr_salf_steps_tbl SET createdDate_time_to='$createdDate_time' WHERE id='$lastidofsalfstat' ";
  $add_request_steps = "INSERT INTO hr_salf_steps_tbl(salf_id,maden,daen,detailsar,detailsen,employ_approval_id,level,stat,type,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$level_refusedetailsar."','".$level_refusedetailsen."','$add_user_id','".$salf_level."','1','salf','$created_date','$createdDate_time','$add_user_id')" ;
    
}else{
  $sql_request = "UPDATE hr_salf_tbl SET stat='0',detailsar='".$level_refusedetailsar."',detailsen='".$level_refusedetailsen."',employ_approval_id='$add_user_id',level='$promotionrefuselevel' WHERE id='$id'";
  $update_request_steps = "UPDATE hr_salf_steps_tbl SET createdDate_time_to='$createdDate_time' WHERE id='$lastidofsalfstat' ";
  $add_request_steps = "INSERT INTO hr_salf_steps_tbl(salf_id,maden,daen,detailsar,detailsen,employ_approval_id,level,stat,type,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$level_refusedetailsar."','".$level_refusedetailsen."','$add_user_id','".$promotionrefuselevel."','0','salf','$created_date','$createdDate_time','$add_user_id')" ;
    
}
 

//==================العملايات==================================
/*
$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$_SESSION['user_id'];
 $system_type='hr_marsa_system';
  $proccess_type='add_solf_request';
  $proccess_details_en='add solf request'.$add_user_id;
  $proccess_details='طلب سلفه الموظف رقم '.$add_user_id;
 $table_name='hr_salf_tbl';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql_req = "INSERT INTO process_systems_tbl(system_type,user_id,user_ip,proccess_type,proccess_details,proccess_details_en,table_name,proccess_id,proccess_date,process_time,process_datetime,browser)values('$system_type','$add_user_id','$user_ip','$proccess_type','$proccess_details','$proccess_details_en','$table_name','$user_id',NOW(),NOW(),'$createdDate','$browserAgent')";
 //==================العملايات==================================
  */
  
if ($conn->query($sql_request) === TRUE && $conn->query($update_request_steps) === TRUE && $conn->query($add_request_steps) === TRUE ) { 
  $_SESSION['message'] ="تمت عملية الرفض بنجاح";
                   
  header("Refresh:0; url=Employee_Advance.php?lang=$lang&company_id=$company_id");
} else {
  echo "Error: " . $sql_request . "<br>" . $conn->error;
}


?>

