<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$emp_code = $_REQUEST['emp_code'];
$att_date = $_REQUEST['att_date'];
$hour = $_REQUEST['hour'];
$minute = $_REQUEST['minute'];
$duty_type = $_REQUEST['duty_type'];
$day_count = $_REQUEST['day_count'];
$remarks = $_REQUEST['remarks'];
$flag = $_REQUEST['flag'];
$schedule_intime = $_REQUEST['schedule_intime'];
$grace_time = $_REQUEST['grace_time'];
$late_count = $_REQUEST['late_count'];
		$year = date('Y',strtotime($att_date));
  		$mon = date('m',strtotime($att_date));
		
		
if($flag>0){
$sql_del = 'delete from employee_daily_attendance where emp_code="'.$emp_code.'" and att_date="'.$att_date.'"';
mysqli_query($conn,$sql_del);
}	
		
		
		

$access_datetime=$att_date.' '.$hour.':'.$minute.':'.'00';
$access_time=$hour.':'.$minute.':'.'00';

$tr_no = $year.''.$mon.''.$emp_code;



if ($access_time > $schedule_intime && $access_time <= $grace_time)
{
     $grace_time_no=1;
}
else
{
      $grace_time_no=0;
}


$grace_time_count=find_a_field($conn,'employee_daily_attendance','sum(grace_time_no)','year="'.$year.'" and mon="'.$mon.'" and emp_code="'.$emp_code.'"')+$grace_time_no;


if($grace_time_count>5){
$schedule_time = $schedule_intime;
}else{
$schedule_time = $grace_time;
}

$in_time = strtotime($access_datetime);
$from_time = strtotime($att_date.' '.$schedule_time);
$late_time = ($in_time - $from_time)/60;

if($late_time>0){
$late_min=$late_time;
	if($late_count=='Yes' && $duty_type==1){
		$grace_no=1;
	}
}


$entry_by=$_SESSION['user']['id'];
$entry_at=date('Y-m-d H:i:s');



if($day_count>0){
$ins_sql = 'INSERT INTO  employee_daily_attendance (emp_code, tr_no, att_date, mon, year, schedule_intime, att_hour, att_min, att_intime, att_datetime,  entry_at, entry_by, late_min, grace_no, duty_type, day_count, remarks, grace_time, grace_time_no) VALUES
("'.$emp_code.'", "'.$tr_no.'", "'.$att_date.'", "'.$mon.'", "'.$year.'", "'.$schedule_intime.'",  "'.$hour.'",  "'.$minute.'", "'.$access_time.'", "'.$access_datetime.'", "'.$entry_at.'", "'.$entry_by.'", "'.$late_min.'", "'.$grace_no.'", "'.$duty_type.'", "'.$day_count.'", "'.$remarks.'", "'.$grace_time.'", "'.$grace_time_no.'")';
	
mysqli_query($conn,$ins_sql);
}


echo 'Success!';
?>