<?php
include("dbConfig.php");

$peroid_dates = array();
$system_id=$_POST["system_id"];
$company_id_sel=$_POST["company_id_sel"];
$company_id=$_POST["company_id"];
$id=$_POST["id"];

$shift_id = $_POST["shift_id"];
$date_from = $_POST["date_from"];
$date_to = $_POST["date_to"];

$add_hours_hodoor = $_POST["add_hours_hodoor"];
$add_hours_shift = $_POST["add_hours_shift"];
$add_hours_khasm = $_POST["add_hours_khasm"];

$sum_salary = $_POST["sum_salary"];
$khasem_from_salary = $_POST["khasem_from_salary"];
$rest_sallary = $_POST["rest_sallary"];
$add_user_id = $_POST["add_user_id"];

$employee_number_fingerprint = $_POST["employee_number_fingerprint"];

//--------------------------------------------
date_default_timezone_set("Asia/Riyadh");
$createdDate_Time = date("Y-m-d h:i:sa");
$createdDate = date("Y-m-d");
$createdTime = date("h:i:sa"); 
//--------------------------------------------
// array of dates
$dates_t = array();
$taked_dates = array();
$beginn = new DateTime($date_from);
$endd = new DateTime($date_to);
$endd->modify('+1 day');
$daterangee = new DatePeriod($beginn, new DateInterval('P1D'), $endd);
        foreach($daterangee as $date){
                array_push($dates_t,$date->format("Y-m-d")); 
        }
//end array of dates
// array of takked dates
for($i=0;$i<count($id);$i++){
    $sql_taked="SELECT * FROM hr_takeed_hodor_ensraf_tbl where employ_id='".$_POST["id"][$i]."'";
                     $result_taked = $conn->query($sql_taked);
                    if ($result_taked->num_rows > 0) {
                                  while($row_taked = $result_taked->fetch_assoc()) {
                                    $begin = new DateTime($row_taked["date_from"]);
                                    $end = new DateTime($row_taked["date_to"]);
                                    $end->modify('+1 day');
                                    $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
                                     foreach($daterange as $date){
                                         //echo $date->format("Y-m-d") . "<br>";
                                    array_push($taked_dates,$date->format("Y-m-d"));
                                                             
                                     }
                                                        
                                  }}
}
$commonElements = array_intersect($dates_t, $taked_dates);
                                $count = count($commonElements);
// end array
if($count>0){
    echo "2";
}else{
for($i=0;$i<count($id);$i++){
   
     
 $sql_w = "UPDATE hr_add_hodor_ensraf_tbl SET takeed_stat='1' WHERE employee_number_fingerprint='".$_POST["employee_number_fingerprint"][$i]."'";
 $run_w = $conn->query($sql_w) or die("Error in saving ".$conn->error);
  
$sql_w2 = "INSERT INTO hr_takeed_hodor_ensraf_tbl (system_id,company_id,employ_id,shift_id,date_from,date_to,add_hours_hodoor,add_hours_shift,add_hours_khasm,sum_salary,khasem_from_salary,rest_sallary,createdDate_Time,add_user_id) VALUES ('$system_id','".$_POST["company_id"][$i]."','".$_POST["id"][$i]."','".$_POST["shift_id"][$i]."','".$_POST["date_from"]."','".$_POST["date_to"]."','".$_POST["add_hours_hodoor"][$i]."','".$_POST["add_hours_shift"][$i]."','".$_POST["add_hours_khasm"][$i]."','".$_POST["sum_salary"][$i]."','".$_POST["khasem_from_salary"][$i]."','".$_POST["rest_sallary"][$i]."','$createdDate_Time','$add_user_id') ";
 $run_w2 = $conn->query($sql_w2) or die("Error in saving ".$conn->error);
 

$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$add_user_id;
 $system_type='hr_marsa_system';
  $proccess_type='takeed_hodoor_ensraf';
  $proccess_details_en='Confirmation of attendance and departure from date'.$_POST["date_from"].' to date'.$_POST["date_to"];
$proccess_details_en='تاكيد كشف الحضور والإنصراف من تاريخ'.$_POST["date_from"].'الى تاريخ'.$_POST["date_to"];
 $table_name='hr_takeed_hodor_ensraf_tbl';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql2 = "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','".$_POST["employee_number_fingerprint"][$i]."',NOW(),NOW(),'$createdDate','$browserAgent')";
// $run2 = $conn->query($sql2) or die("Error in saving image".$conn->error);    
 //==================العملايات==================================
 

if ($conn->query($sql2) === TRUE) {
} else {
  echo "Error: " . $sql2 . "<br>" . $conn->error;
}
         }
         echo "1";
        }


?>



