<?php
session_start();

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

$add_user_id = $_POST["add_user_id"];
$reason_of_refuse = $_POST["reason_of_refuse"];
$approval_time = $_POST["approval_time"];

$op_type = "refuse";
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"];
 
 // حساب رقم المستوى
 $rowcount = salf_level($id); 
 //
 //
 $refuselevel = refuselevel($rowcount,$company_id);
 $refusedetailsAr = refusedetailsAr($rowcount,$company_id);
 $refusedetailsEn = refusedetailsEn($rowcount,$company_id);
 //
  // get the last record
$lastidofsalfstat = getthelastidofsalfstat($id);
  // end get the last record

if($refuselevel=="final_reject"){

    $sql_request = "UPDATE hr_salf_tbl SET stat='5',detailsar='$refusedetailsAr',detailsen='$refusedetailsEn',approval_time='$createdDate_time' WHERE id='$id'";
    $update_request_steps = "UPDATE hr_salf_steps_tbl SET employ_approval_id='$add_user_id',stat='1',op_type='$op_type',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,op_type,type,reason_of_refuse,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$refusedetailsAr."','".$refusedetailsEn."','".$add_user_id."','".$rowcount."','5','$op_type','salf','$reason_of_refuse','$created_date','$createdDate_time','$add_user_id')" ;
    
    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;
      } 
}
elseif($refuselevel=="returnto_employee"){
    $sql_request = "UPDATE hr_salf_tbl SET stat='3',detailsar='$refusedetailsAr',detailsen='$refusedetailsEn',approval_time='$createdDate_time' WHERE id='$id'";
    $update_request_steps = "UPDATE hr_salf_steps_tbl SET employ_approval_id='$add_user_id',stat='1',op_type='$op_type',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,op_type,type,reason_of_refuse,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$refusedetailsAr."','".$refusedetailsEn."','".$add_user_id."','".$rowcount."','3','$op_type','salf','$reason_of_refuse','$created_date','$createdDate_time','$add_user_id')" ;
    
    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;
      } 

}elseif($refuselevel>countadvancelevel($company_id)){
//echo "same";
$sql_request = "UPDATE hr_salf_tbl SET stat='5',detailsar='$refusedetailsAr',detailsen='$refusedetailsEn',level='$refuselevel',approval_time='$createdDate_time' WHERE id='$id'";
$update_request_steps = "UPDATE hr_salf_steps_tbl SET employ_approval_id='$add_user_id',stat='1',op_type='$op_type',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,reason_of_refuse,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$refusedetailsAr."','".$refusedetailsEn."','".$add_user_id."','".$refuselevel."','5','salf','$reason_of_refuse','$created_date','$createdDate_time','$add_user_id')" ;

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;
  }
}else{
$sql_request = "UPDATE hr_salf_tbl SET stat='2',detailsar='".$refusedetailsAr."',detailsen='".$refusedetailsEn."',level='".$refuselevel."' WHERE id='$id'";
$update_request_steps = "UPDATE hr_salf_steps_tbl SET employ_approval_id='$add_user_id',stat='1',op_type='$op_type',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,reason_of_refuse,createdDate,createdDate_time,add_user_id)VALUES('$id','$amount','0','".$refusedetailsAr."','".$refusedetailsEn."','".$add_user_id."','".$refuselevel."','2','salf','$reason_of_refuse','$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;
}

 }
}
?>

