<?php
session_start();
	
include_once("../class/class.MySQL.php");
require '../class/resizeImage.php';
$db=new MySQL();


$data="";
$data1="";
$data2="";
$data3="";
$data4="";
$data5="";
$data6="";


if(isset($_POST['recdate']))
{
	$data=array();
	$dts = date(('Y-m-d'),strtotime($_POST['recdate']));
	$chk_date=date('Y-m-d',strtotime($_POST['recdate']));
	
	$timerin = $dts.' '.$_POST['timepicker_in'];	
	
	$timerout = $dts.' '.$_POST['timepicker_out'];
	$eid = $_POST['empid'];
	$reason = $_POST['reason'];
	
	$time1 = strtotime($timerin);
	$time2 = strtotime($timerout);
	
	$diff = $time2 - $time1;
	
	$totalWorkTime = date('H:i:s',strtotime("-6 hours 30 minutes",$diff));
	
	$LogTimeIn =  date("H:i",strtotime($timerin));  
	$dt_frtIn =  date('A', strtotime($timerin));
	
	$LogTimeOut =  date("H:i",strtotime($timerout));  
	$dt_frtOut =  date('A', strtotime($timerout));
	
	$result = mysql_query("select device_id from employee where e_id = '$eid'");
	$rws  = mysql_fetch_array($result);
	$device_ids = $rws['device_id'];
	
	if($_SESSION['usertype']=='Superadmin' || $_SESSION['branchadmin'] =='Y') 
	{
		mysql_query("DELETE FROM time where e_id = '$eid' and e_date ='$dts'");
		mysql_query("INSERT INTO time (e_date,e_id,deviceids,e_in,e_out,e_work,time_in,time_inloc,time_out,time_outloc,total_worked_hours) VALUES ('$dts','$eid','$device_ids','$timerin','$timerout','$totalWorkTime','$LogTimeIn','$dt_frtIn','$LogTimeOut','$dt_frtOut','$totalWorkTime')");
		
		mysql_query("delete from time_approval where edate = '$dts' and eid = '$eid'");
		mysql_query("INSERT INTO time_approval (edate,eid,ein,eout,ework,timein,timeinloc,timeout,timeoutloc,totalworked_hours,reason_desc,is_blocked) VALUES ('$dts','$eid','$timerin','$timerout','$totalWorkTime','$LogTimeIn','$dt_frtIn','$LogTimeOut','$dt_frtOut','$totalWorkTime','$reason','0')");
	}
	else
	{
		
		mysql_query("delete from time_approval where edate = '$dts' and eid = '$eid'");
		mysql_query("INSERT INTO time_approval (edate,eid,ein,eout,ework,timein,timeinloc,timeout,timeoutloc,totalworked_hours,reason_desc) VALUES ('$dts','$eid','$timerin','$timerout','$totalWorkTime','$LogTimeIn','$dt_frtIn','$LogTimeOut','$dt_frtOut','$totalWorkTime','$reason')");
		
		$sel_qry		="SELECT * FROM employee where e_id ='$eid'";
		$sel_res = $db->fetchQuery($sel_qry);
		
		$e_fname=$sel_res[0]['e_fname'];
		$imd_supervisor_id=$sel_res[0]['imd_supervisor_id'];
		$fromemail=$sel_res[0]['e_mail'];
		//$fromemail='arunsab82@gmail.com';
		
		$sel_qry_imm		="SELECT * FROM employee where e_id ='$imd_supervisor_id'";
		$sel_res_imm = $db->fetchQuery($sel_qry_imm);
		
		$email=$sel_res_imm[0]['e_mail'];	
		//$email='arunsab82@gmail.com';			
	
		$message=$e_fname.' has made changes in his time sheet for the date '.date('d-m-Y',strtotime($dts)).' please do check the same <br/><br/>www.groupmukkadan.com/HRMS';	
		
		$headers  	= "MIME-Version: 1.0\r\n";
		$headers 	.= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
		$headers 	.= "From:  ".$fromemail."\r\n";
		
		mail($email,'Leave Request',$message,$headers,parameters);
	}
	//echo "INSERT INTO time_approval (edate,eid,ein,eout,ework,timein,timeinloc,timeout,timeoutloc,totalworked_hours) VALUES ('$dts','$eid','$timerin','$timerout','$totalWorkTime','$LogTimeIn','$dt_frtIn','$LogTimeOut','$dt_frtOut','$totalWorkTime')";
	
	

	
} 
?>