<?php 
$PageSecurity = 15; include('includes/fun-php.php');
include('Class-EmpShift.php');
$frmpermission= $px->GetFileAccess($modid,$frmid,$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];


if(isset($_REQUEST['attendane_date'])){
$date=$_REQUEST['attendane_date'];
$p=explode('/',$_REQUEST['attendane_date']);
$month=$p[1];
$day=$p[0];
$year=$p[2];
}
else{
$date=date('d/m/Y');
$month=date('m');
$day=date('d');
$year=date('Y');
}	
$time=strftime(($DefaultClock==24) ? '%I:%M %p' : '%H:%M');
if($_REQUEST['Save'])
{

//echo  lastOfMonth();
   	
	   $dd1=$_REQUEST['attendane_date'];
	
 	//$_REQUEST['attendane_date']=$dd1;
 	//echo "<pre>";print_r($_REQUEST);
	 
	
	 $CounterCL=0;
 for($k=1; $k<$_POST['rows']; $k++)
{
	if($_POST['pres_abs'][$k]!='0')
	{
  		$data['site']=$_REQUEST['AttSite'];
		$data['e_code']=$_POST['e_code'][$k];
		$data['card_no']=$_POST['card_no'][$k];
		$data['employee']=$_POST['employee'][$k];
		$data['shift_category']=$_POST['emp_shift'][$k];
		$data['shift_start']=$_POST['shift_start'][$k];
		$data['shiftstart_AMPM']=$_POST['shiftstart_AMPM'][$k];
		$data['shift_end']=$_POST['shift_end'][$k];
		$data['shiftend_AMPM']=$_POST['shiftend_AMPM'][$k];
		$data['time_in']=$_POST['time_in'][$k];
		$data['timein_AMPM']=$_POST['timein_AMPM'][$k];
		$data['time_out']=$_POST['time_out'][$k];
		$data['timeout_AMPM']=$_POST['timeout_AMPM'][$k];
		$data['work_hrs']=$_POST['work_hrs'][$k];
		if($_REQUEST['pres_abs'][$k]=='WL')
		{$data['ot_hrs']=='';}else
		{$data['ot_hrs']=$_POST['ot_hrs'][$k];}
		$data['late_arrival']=$_POST['late_arrival'][$k];
		$data['office_hours']=$_REQUEST['officework_hrs'][$k];
		$data['leave_code']=$_POST['leave_code'][$k];
		$data['attendance_date']=FormatDateForSQL($date);
		
		$ot_min=$OBJ_EMPSHIFT->ConvertTimeINTO_Mins($_REQUEST['ot_hrs'][$k]);
		
		if($_POST['shiftstart_AMPM'][$k]=='PM')
		{$data['out_date']=date('Y-m-d', strtotime("+1 day", strtotime(FormatDateForSQL($date))));}
		else if($_REQUEST['timein_AMPM'][$k]=='PM' && $_REQUEST['timeout_AMPM'][$k]=='PM' && $ot_min>'209')
		{$data['out_date']=date('Y-m-d', strtotime("+1 day", strtotime(FormatDateForSQL($date))));}
		else if($_REQUEST['timein_AMPM'][$k]=='AM' && $_REQUEST['timeout_AMPM'][$k]=='PM' && $ot_min>'389')
		{$data['out_date']=date('Y-m-d', strtotime("+1 day", strtotime(FormatDateForSQL($date))));}
		else if($_REQUEST['timein_AMPM'][$k]=='AM' && $_REQUEST['timeout_AMPM'][$k]=='AM')
		{$data['out_date']=date('Y-m-d', strtotime("+1 day", strtotime(FormatDateForSQL($date))));}
		else
		{$data['out_date']=FormatDateForSQL($date);}
		//echo $_REQUEST['timein_AMPM'][$k]; echo "===="; echo $_REQUEST['timeout_AMPM'][$k]; echo "===="; echo $_REQUEST['ot_hrs'][$k];
		//echo $data['out_date'];
		
		$data['created_date']=FormatDateForSQL($date);
		$data['attendance_day']=$day;
		$data['attendance_month']=$month;
		$data['attendance_year']=$year;
		
	  	$data['pres_abs']=$_REQUEST['pres_abs'][$k];
//		$data['others']=$_POST['other_official_tour'][$k];
		$data['created_by']=$_SESSION['UserID'];
//echo "select * from daily_attendance where attendance_date='".FormatDateForSQL($date)."' and e_code='".$_POST['e_code'][$k]."'"; exit;	 
$chkatt=DB_query("select * from daily_attendance where attendance_date='".FormatDateForSQL($date)."' and e_code='".$_POST['e_code'][$k]."'",$db);
  $n=DB_num_rows($chkatt);
	if($n==0)
	{	 
//************************************************************		 
	/////Check Employee leave history	 
	 $leave_sql="Select * from leavemaster";
   $leave_res=DB_query($leave_sql,$db);  
  
   while($leave_row=DB_fetch_array($leave_res))
   {
     if($_REQUEST['pres_abs'][$k]=='L' && $leave_row['leavecode']==$_REQUEST['leave_code'][$k])	
      {
	   $totemp_leaves=$s->GetFieldValue('emp_leave',"emp_id='".$_REQUEST['employee'][$k]."' and  leave_type='".$leave_row['leavecode']."'",'leave_days');
	    if($totemp_leaves>0)
	    {
		      $chkleavestatus=$s->GetFieldValue('emp_leave_apply',"empid  ='".$_REQUEST['employee'][$k]."' and  leave_type='".$leave_row['leavecode']."' and approve_status='Y' and period_from='".ConvertSQLDate($_REQUEST['created_date'][$k])."'",'id');
			if($chkleavestatus==0)
			{
					$data_leaveapply['empid']=$_REQUEST['employee'][$k];
					$data_leaveapply['period_from']=FormatDateForSQL($_REQUEST['attendane_date']);
					$data_leaveapply['period_to']=FormatDateForSQL($_REQUEST['attendane_date']);
					$data_leaveapply['leave_reason']="Daily Auto Generated";
					$data_leaveapply['total_days']=1;
					$data_leaveapply['leave_type']=$leave_row['leavecode'];
					$data_leaveapply['approve_status']="Y";
					$s->RecordInsert('emp_leave_apply', $data_leaveapply);
					
					$data_update['leave_days']=$totemp_leaves-1;
					$data_update['emp_id']=$_REQUEST['employee'][$k]; 
					$cond_leave=" emp_id='".$_REQUEST['employee'][$k]."' and leave_type ='".$leave_row['leavecode']."'";		
					$s->RecordEdit('emp_leave', $data_update,$cond_leave);
			 } 
         }
	  }

   }  
 		 
		 
//*****************************************************		 
		 
		 $s->RecordInsert('daily_attendance', $data);
		$empshift=$s->GetFieldValue('shift_master',"id='".$_POST['emp_shift'][$k]."'" ,'shift_for');
		if(strtolower($empshift)=='staff'){	
			
			if($_POST['emp_basic_sal'][$k]>10000 && $OBJ_EMPSHIFT->ConvertTimeINTO_Mins($data['ot_hrs'])>=390)
			{
 				$CS=1;
				$data_leave['leave_type']='CS';
				$data_leave['emp_id']=$_POST['employee'][$k];
				$cond="emp_id='".$_POST['employee'][$k]."'";
				$chkempleave=$s->GetRecordById('emp_leave',  $cond);
				if(DB_num_rows($chkempleave)>0)
				{
					$rx=DB_fetch_array($chkempleave);
					
					$data_leave['leave_days']=$rx['leave_days']+$CS;
					$cond="id='".$rx['id']."'";				
					$s->RecordEdit('emp_leave', $data_leave, $cond);
				}else
				{
					$data_leave['leave_days']=$CS;
					$s->RecordInsert('emp_leave', $data_leave);
				}	
				
 				    $datacs['empid']=$_POST['employee'][$k];
					 $datacs['csdate']=FormatDateForSQL($date);
					 $datacs['csdays']=$CS;
  					 $insert=$s->RecordInsert('emp_csleave_updation', $datacs);
			}	
	}		
		}
	
	prnMsg( _('Attendance Saved Successfully'),'success');
	}
}


for($k=1;$k<=count($_REQUEST['employee']);$k++)	
{

	$dd=explode("/",$_REQUEST['attendane_date']);
	//echo strtotime(lastOfMonth($dd[1],$dd[2]))."==".strtotime(FormatDateForSQL($dd1));
		 if(strtotime(lastOfMonth($dd[1],$dd[2]))== strtotime(FormatDateForSQL($dd1))) 
		 {
		// echo " select count(pres_abs) as Tpresent  from daily_attendance where   employee='".$_REQUEST['employee'][$k]."' and (month(attendance_date)=$dd[1] and year(attendance_date)=$dd[2])   and (pres_abs='p' or pres_abs='L')   ";
		 $CLleaveID=DB_query(" select count(pres_abs) as Tpresent  from daily_attendance where employee='".$_REQUEST['employee'][$k]."' and (month(attendance_date)=$dd[1] and year(attendance_date)=$dd[2])   and (pres_abs='p' or pres_abs='L')   ",$db);
		  $TotalCL=DB_fetch_array($CLleaveID);
		
		  if($TotalCL['Tpresent']>20)
		  {
		  //echo $TotalCL['Tpresent'];
			$CLleaveID=$s->GetFieldValue('leavemaster',"  leavecode='CL' ",'id');
			$CLleave_days=$s->GetFieldValue('emp_leave',"emp_id  ='".$_REQUEST['employee'][$k]."' and leave_type='CL' ",'leave_days');
			$qx=DB_query("update emp_leave  set leave_days=$CLleave_days+1 where  emp_id  ='".$_REQUEST['employee'][$k]."' and id=$CLleaveID",$db);
		  }
}	   
	 }
/* echo "<script>location.href=".$target."&attendane_date=".$_REQUEST['attendane_date']."&AttSite=".$_REQUEST['AttSite']."</script>";
*/
  
}
?>
<link rel="stylesheet" type="text/css" href="css/professional/table_frize.css">

<script src="js/function-ajax.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function check()
{
	var site=document.getElementById("site").value;
	var date=document.getElementById("date").value;
	if(site=='')
	{alert('Please choose a branch'); return false;}
	if(date=='')
	{alert('Please enter a date'); return false;}
}
function hideDetails(str,i)
{
	if(str=='A')	
	{
			document.getElementById("time_in_"+i).value='';
 			document.getElementById("time_out_"+i).value='';
			document.getElementById("work_hrs_"+i).value='';
 			document.getElementById("ot_hrs_"+i).value='';
			document.getElementById("officework_hrs_"+i).value='';
		 

		document.getElementById("card_no_"+i).disabled=true;
		document.getElementById("shift_start_"+i).disabled=true;
		document.getElementById("shift_end_"+i).disabled=true;
		document.getElementById("time_in_"+i).disabled=true;
		document.getElementById("time_out_"+i).disabled=true;
		document.getElementById("work_hrs_"+i).disabled=true;
		document.getElementById("ot_hrs_"+i).disabled=true;
		document.getElementById("leave_code_"+i).disabled=true;
//		document.getElementById("other_official_tour_"+i).disabled=true;
		
	}
	else if(str=='L')
	{
			document.getElementById("time_in_"+i).value='';
 			document.getElementById("time_out_"+i).value='';
			document.getElementById("work_hrs_"+i).value='';
 			document.getElementById("ot_hrs_"+i).value='';
			document.getElementById("officework_hrs_"+i).value='';
			
		document.getElementById("card_no_"+i).disabled=true;
		document.getElementById("shift_start_"+i).disabled=true;
		document.getElementById("shift_end_"+i).disabled=true;
		document.getElementById("time_in_"+i).disabled=true;
		document.getElementById("time_out_"+i).disabled=true;
		document.getElementById("work_hrs_"+i).disabled=true;
		document.getElementById("ot_hrs_"+i).disabled=true;
		document.getElementById("leave_code_"+i).disabled=false;
		
//		document.getElementById("other_official_tour_"+i).disabled=true;
		
	}
	else if(str=='P')
	{
	
	 	document.getElementById("time_in_"+i).value=document.getElementById("shift_start_"+i).value;
 			document.getElementById("time_out_"+i).value=document.getElementById("shift_end_"+i).value;

		document.getElementById("card_no_"+i).disabled=false;
		document.getElementById("shift_start_"+i).disabled=false;
		document.getElementById("shift_end_"+i).disabled=false;
		document.getElementById("time_in_"+i).disabled=false;
		document.getElementById("time_out_"+i).disabled=false;
		document.getElementById("work_hrs_"+i).disabled=false;
		document.getElementById("ot_hrs_"+i).disabled=false;
		document.getElementById("leave_code_"+i).disabled=true;
		document.getElementById("officework_hrs_"+i).disabled=false;
//		document.getElementById("other_official_tour_"+i).disabled=true;
		
	}
	else if(str=='official_tour')
	{
		document.getElementById("card_no_"+i).disabled=true;
		document.getElementById("shift_start_"+i).disabled=true;
		document.getElementById("shift_end_"+i).disabled=true;
		document.getElementById("time_in_"+i).disabled=true;
		document.getElementById("time_out_"+i).disabled=true;
		document.getElementById("work_hrs_"+i).disabled=true;
		document.getElementById("ot_hrs_"+i).disabled=true;
		document.getElementById("leave_code_"+i).disabled=true;
//		document.getElementById("other_official_tour_"+i).disabled=false;
		
	}
	else
	{		document.getElementById("officework_hrs_"+i).value='';
			document.getElementById("time_in_"+i).value='';
 			document.getElementById("time_out_"+i).value='';
			document.getElementById("work_hrs_"+i).value='';
 			document.getElementById("ot_hrs_"+i).value='';

		document.getElementById("card_no_"+i).disabled=false;
		document.getElementById("shift_start_"+i).disabled=false;
		document.getElementById("shift_end_"+i).disabled=false;
		document.getElementById("time_in_"+i).disabled=false;
		document.getElementById("time_out_"+i).disabled=false;
		document.getElementById("work_hrs_"+i).disabled=false;
		document.getElementById("ot_hrs_"+i).disabled=false;
		document.getElementById("leave_code_"+i).disabled=false;
//		document.getElementById("other_official_tour_"+i).disabled=true;
		
	}
}


var secondsPerMinute = 60;
var minutesPerHour = 60;

function convertSecondsToHHMMSS(intSecondsToConvert) {
var hours = convertHours(intSecondsToConvert);
var minutes = getRemainingMinutes(intSecondsToConvert);
minutes = (minutes == 60) ? "00" : minutes;
//var seconds = getRemainingSeconds(intSecondsToConvert);
return hours+":"+minutes;
}

function convertHours(intSeconds) {
var minutes = convertMinutes(intSeconds);
var hours = Math.floor(minutes/minutesPerHour);
return hours;
}
function convertMinutes(intSeconds) {
return Math.floor(intSeconds/secondsPerMinute);
}
function getRemainingSeconds(intTotalSeconds) {
return (intTotalSeconds%secondsPerMinute);
}
function getRemainingMinutes(intSeconds) {
var intTotalMinutes = convertMinutes(intSeconds);
return (intTotalMinutes%minutesPerHour);
}

function HMStoSec1(T) { // h:m:s
  var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 +  0 }
  
  
function calculate_OverTime(i,j)
{
//alert(j);
if(j=='WL')
{
	document.getElementById("ot_hrs_"+i).value=0;
}else{
var working=document.getElementById("work_hrs_"+i).value;
var office_work_hrs=document.getElementById("officework_hrs_"+i).value;
//var late_arrival=document.getElementById("late_arrival_"+i).value;
var time1 = HMStoSec1(working);
var time2 = HMStoSec1(office_work_hrs);
if(time1>time2)
{
	var diff = time1 - time2
	var  OTHOUR=convertSecondsToHHMMSS(diff);
	document.getElementById("ot_hrs_"+i).value=OTHOUR;
	}else
	{
	document.getElementById("ot_hrs_"+i).value=0;
	}
 } 
//******************************************
}

/*function calculate_late_arrival(i)
{
 	var InTime=document.getElementById("time_in_"+i).value;
 	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
  	var InTime_AMPM=document.getElementById("timein_AMPM_"+i).value;

 	var shiftTime=document.getElementById("shift_start_"+i).value;
 	var shiftTimeBrk=shiftTime.split(':');
	var shiftTime_hour=shiftTimeBrk[0];
	var shiftTime_min=shiftTimeBrk[1];
	var shiftstart_AMPM=document.getElementById("shiftstart_AMPM_"+i).value;

		if(InTime_AMPM=='PM' && InTime_hour!='12')	
		{intimevalue=parseInt(InTime_hour)+12;}
		else if(InTime_AMPM=='AM' && InTime_hour=='12')
		{intimevalue=0;}
		else
		{intimevalue=parseInt(InTime_hour);}
		
		if(shiftstart_AMPM=='PM' && shiftTime_hour!='12')	
		{shifttimevalue=parseInt(shiftTime_hour)+12;}
		else if(shiftstart_AMPM=='AM' && shiftTime_hour=='12')
		{shifttimevalue=0;}
		else
		{shifttimevalue=parseInt(shiftTime_hour);}
		
		if(shiftstart_AMPM=='PM' && InTime_AMPM=='AM')
		{
			shifttimevalue_1=12-parseInt(shiftTime_hour);
			shifttimevalue=-(shifttimevalue_1);
			intimevalue=intimevalue;
		}
		
		if(intimevalue>shifttimevalue || intimevalue==shifttimevalue)
		{
			if(parseInt(shiftTime_min)>parseInt(InTime_min))
			{
				
				var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue)-1;
				var intermediate_min=parseInt(shiftTime_min)-parseInt(InTime_min);
				var Final_outMin=60-parseInt(intermediate_min);
			}
			else if(parseInt(InTime_min)>parseInt(shiftTime_min))
			{
				
				var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue);
				var Final_outMin=parseInt(InTime_min)-parseInt(shiftTime_min);
			}
			else
			if(parseInt(InTime_min)==parseInt(shiftTime_min))
			{
				var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue);
				var Final_outMin="00";
			}
		}
		else
		{
			var Final_outHours='0';
			var Final_outMin='0';
		}
	
	var late_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
	document.getElementById("late_arrival_"+i).value=late_Hours; }*/
	
function calculate_workhours(i,j)
{
	var out_date=document.getElementById("out_date_"+i).value;
	var current_date=document.getElementById("current_date_"+i).value;

	var InTime=document.getElementById("time_in_"+i).value;
	var InTime_AMPM=document.getElementById("timein_AMPM_"+i).value;
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
	
	var OutTime=document.getElementById("time_out_"+i).value;
	var OutTime_AMPM=document.getElementById("timeout_AMPM_"+i).value;
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
	
		if(InTime_AMPM=='PM' && InTime_hour!='12')	
		{intimevalue=parseInt(InTime_hour)+12;}
		else if(InTime_AMPM=='AM' && InTime_hour=='12')
		{intimevalue=0;}
		else
		{intimevalue=parseInt(InTime_hour);}
		
		if(OutTime_AMPM=='PM' && OutTime_hour!='12')	
		{outtimevalue=parseInt(OutTime_hour)+12;}
		else if(OutTime_AMPM=='AM' && OutTime_hour=='12')
		{outtimevalue=0;}
		else
		{outtimevalue=parseInt(OutTime_hour);}
		
		if(InTime_AMPM=='PM' && OutTime_AMPM=='AM')
		{
			intimevalue_1=12-parseInt(InTime_hour);
			intimevalue=-(intimevalue_1)
			outtimevalue=outtimevalue; 
		}
		 
		
		if(out_date>current_date)
		{
			if(InTime_AMPM=='PM' && OutTime_AMPM=='PM')
			{
				var outtimevalue=parseInt(InTime_hour)-parseInt(OutTime_hour);
				var intimevalue=24;
			}
			 if(InTime_AMPM=='AM' && OutTime_AMPM=='AM' )
			{
		 		if((parseInt(InTime_hour)>parseInt(OutTime_hour))){
				var outtimevalue=  parseInt(InTime_hour)-parseInt(OutTime_hour);
				}else{
				var outtimevalue= parseInt(OutTime_hour)-parseInt(InTime_hour);
				}
			var intimevalue=24;
		 	}
		}
		
			if(parseInt(OutTime_min)>parseInt(InTime_min))
			{
			 
			  if(parseInt(intimevalue)>parseInt(outtimevalue))
 				  Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue) ;			  
			   else			  
 				  Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue); 
				 Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);	
			}
			else if(parseInt(InTime_min)>parseInt(OutTime_min))
			{
				if(parseInt(intimevalue)>parseInt(outtimevalue))
 				  Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue)-1;			  
			   else			  
 				  Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
				  
			//	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
				var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
				var Final_outMin=60-parseInt(intermediate_min);
			}
			else if(parseInt(InTime_min)==parseInt(OutTime_min))
			{
				 if(parseInt(outtimevalue)>parseInt(intimevalue))
				 {var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);}
				 else
				 {var Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue);}
				var Final_outMin="00"; 
			}	
			  
			  
		var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
		
		if(Working_Hours!='NaN:NaN')		
		document.getElementById("work_hrs_"+i).value=Working_Hours; 
		else
		document.getElementById("work_hrs_"+i).value=0; 
		if(j=='WL') 
		{
			document.getElementById("ot_hrs_"+i).value=''; 
		}
	
}

function calculate_officeworkhours(i)
{
    var InTime=document.getElementById("shift_start_"+i).value;
	var InTime_AMPM=document.getElementById("shiftstart_AMPM_"+i).value;
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];

	var OutTime=document.getElementById("shift_end_"+i).value;
	var OutTime_AMPM=document.getElementById("shiftend_AMPM_"+i).value;
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
		
		if(InTime_AMPM=='PM' && InTime_hour!='12')	
		{intimevalue=parseInt(InTime_hour)+12;}
		else if(InTime_AMPM=='AM' && InTime_hour=='12')
		{intimevalue=0;}
		else
		{intimevalue=parseInt(InTime_hour);}
		
		if(OutTime_AMPM=='PM' && OutTime_hour!='12')	
		{outtimevalue=parseInt(OutTime_hour)+12;}
		else if(OutTime_AMPM=='AM' && OutTime_hour=='12')
		{outtimevalue=0;}
		else
		{outtimevalue=parseInt(OutTime_hour);}
		
		if(InTime_AMPM=='PM' && OutTime_AMPM=='AM')
		{
			intimevalue_1=12-parseInt(InTime_hour);
			intimevalue=-(intimevalue_1)
			outtimevalue=outtimevalue;
		}
		
			if(parseInt(OutTime_min)>parseInt(InTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
				var Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);
			}
			else if(parseInt(InTime_min)>parseInt(OutTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
				var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
				var Final_outMin=60-parseInt(intermediate_min);
			}
			else
			if(parseInt(InTime_min)==parseInt(OutTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
				var Final_outMin="00";
			}
				 
		var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
		document.getElementById("officework_hrs_"+i).value=Working_Hours; 
		
	
}
</script>
<strong><h2 align="center">DAILY ATTENDANCE</h2></strong>
<form action="" method="post">
<table width="30%" align="center" style="border: solid 1px #999999; background-color:#FFFFFF">
<tr>
<th>Branch</th>
<td><select name="site" id="site" style="width: 100%; height:20px">
<option value="">---Select Site---</option>
<?php 
if($_SESSION['AssignedBranch']!=''){

$site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);}
else
{$site_sql=DB_query("Select loccode, locationname from locations",$db);}
while($site_row=DB_fetch_array($site_sql)){
?>
<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'');?>><?php echo $site_row['locationname'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<th>Attendance Date</th>
<td><input type="text"  name="attendane_date" id="date" class="datepicker" value="<?php echo $_REQUEST['attendane_date'];?>" style="width: 100%; height:20px"/></td>	
</tr>
</table>
<p align="center"><input type="submit" name="proceed" value="Proceed" class="search" onclick="return check();" /></p>
</form>

<p align="left"><strong>P:Present &nbsp; A:Absent &nbsp; L:Leave &nbsp; O:Official tour</strong></p>
<form action="" method="post">
<input type="hidden" name="AttSite" value="<?php echo $_REQUEST['site'];?>" />
<input type="hidden" name="attendane_date" value="<?php echo $_REQUEST['attendane_date'];?>" />
<div style="overflow:auto;  width:auto; height:auto"> 
<table width="100%" border="0" cellspacing="1" cellpadding="4" style="border: solid 1px #999999; background:#666666">
<tr>
<th width="3%"><div align="center"><strong>Sr.	</strong></div></th>
<th width="6%"><div align="center"></div></th>
<th colspan="4"><div align="center"><strong>Employee</strong></div></th>
<th colspan="2"><div align="center"><strong>Time</strong></div></th>
<th width="15%"><div align="center"></div></th>
<th width="6%"><div align="center"><strong>Official</strong></div></th>
<th width="6%"><div align="center"><strong>Working</strong></div></th>
<th width="4%"><div align="center"><strong>OT</strong></div></th>
<th width="6%"><div align="center"></div></th>
</tr>
<tr>
<th><div align="center"><strong>No.</strong></div></th>
<th><div align="center"><strong>Status</strong></div></th>
<th width="4%"><div align="center"><strong>Code</strong></div></th>
<th width="5%"><div align="center"><strong>Name</strong></div></th>
<th width="9%"><div align="center"><strong>Designation</strong></div></th>
<th width="6%"><div align="center"><strong>Shift</strong></div></th>
<th width="17%"><div align="center"><strong>In</strong></div></th>
<th width="18%"><div align="center"><strong>Out</strong></div></th>
<th><div align="center"><strong>Out Date</strong></div></th>
<th><div align="center"><strong>Hrs</strong></div></th>
<th><div align="center"><strong>Hrs</strong></div></th>
<th><div align="center"><strong>Hrs	</strong></div></th>
<th><div align="center"><strong>Leave</strong></div></th>
</tr>
<?php 
if($_REQUEST['proceed']!=''){
$datefilter=explode("/",$_REQUEST['attendane_date']);
$holiday_status=GET_Holidays($datefilter[0],$datefilter[1],$datefilter[2]);
$condition="day(start_date)='".$datefilter[0]."' and  month(start_date)='".$datefilter[0]."' and published='Y' and  year(start_date)='".$datefilter[0]."'";
$holidaytitle=$s->GetFieldValue('holidays',$condition ,'holiday_title');
if($holiday_status!=1)
{
	$emp_daily_attendance_id='';
	$chkemp_dailydate=DB_query("Select employee from daily_attendance where attendance_date='".ConvertSQLDate($_REQUEST['attendane_date'])."'",$db);
	if(DB_num_rows($chkemp_dailydate)>0)
	{
		while($emp_daily_attendance=DB_fetch_array($chkemp_dailydate))
 		 {
 		 	 $emp_daily_attendance_id[]=$emp_daily_attendance['employee'];
		 }
		  $emp_daily_attendance_id=implode(',',array_unique($emp_daily_attendance_id));
	}
	
if($emp_daily_attendance_id!='')
{$emp_sql="Select * from empmaster where emp_site='".$_REQUEST['site']."' and id not in($emp_daily_attendance_id)";}
else 
{$emp_sql="Select * from empmaster where emp_site='".$_REQUEST['site']."'";}

$emp_result=DB_query($emp_sql,$db);
$chkDay=DayOfDate(ConvertSQLDate($date));

if(DB_num_rows($emp_result)>0)
{$i=1;
	while($emp_row=DB_fetch_array($emp_result))
	{
		$_REQUEST['e_code']=$emp_row['emp_code'];
      	$emp_site=$emp_row['emp_site'];
	   	$emp_shift=$emp_row['employee_type'];
	  	$GETEMPTYPE=   $s->getField('shift_master','id', $emp_shift,'shift_for');
     	$GetEmpShift= GetDay($emp_row['emp_weekly_off']);
		
		
		
		$get_shift=DB_query("select * from shift_master where id='".$emp_row['employee_type']."' and site='".$_REQUEST['site']."' and shift_for='".$GETEMPTYPE."'",$db);											
		//echo "select * from shift_master where id='".$emp_row['employee_type']."' and site='".$_REQUEST['site']."' and shift_for='".$GETEMPTYPE."'";
	  	$get_res=DB_fetch_array($get_shift);
   	  	$_REQUEST['shift_start']=$get_res['shift_start'];
	  	$_REQUEST['shiftstart_AMPM']=$get_res['start_AMPM'];
	  	$_REQUEST['shift_end']=$get_res['shift_end'];
	   	$_REQUEST['shiftend_AMPM']=$get_res['end_AMPM'];
		if($chkDay==$GetEmpShift)
		{
		
			$_REQUEST['time_in'][$i]='';
			$_REQUEST['timein_AMPM'][$i]='AM';
			$_REQUEST['time_out'][$i]='';
			$_REQUEST['timeout_AMPM'][$i]='PM';
		}
 		else{
	  	$_REQUEST['time_in'][$i]=$get_res['shift_start'];
	  	$_REQUEST['timein_AMPM'][$i]=$get_res['start_AMPM'];
	  	$_REQUEST['time_out'][$i]=$get_res['shift_end'];
	  	if($get_res['end_AMPM']!='')
	  	$_REQUEST['timeout_AMPM'][$i]=$get_res['end_AMPM'];
	  	else
	  	$_REQUEST['timeout_AMPM'][$i]='PM';
		
		}
		$_REQUEST['work_hrs'][$i]=0;
		$_REQUEST['ot_hrs'][$i]=0;
		?>
		<tr bgcolor="#FFFFFF">
		<td><?php echo $i;?><input name="emp_basic_sal[<?php echo $i;?>]" type="hidden" value="<?php echo $emp_row['gross_sal']; ?>" /></td>
		<td>
		<?php 
		if($holiday_status==1) {echo "<br><b>Holiday</b>";}
		elseif($GetEmpShift==$chkDay)
	 	{echo "<br><b>WeeklyOff</b>";}
		//else
		{?> <input name="present_status[<?php echo $i;?>]"  id="present_status_<?php echo $i;?>"  type="hidden" value="<?php echo($GetEmpShift==$chkDay?'WL':'');?>" /><br/>
			<select name="pres_abs[<?php echo $i;?>]" id="pres_abs_<?php echo $i;?>" style="width:auto" onchange="hideDetails(this.value,<?php echo $i;?>)">
			<option value="0" selected="selected"<?php echo ($present_status=='0'?'selected':'')?>>--</option>
			<option value="P"  <?php echo ($present_status=='P'?'selected':'')?>  >P</option>
			<option value="A" <?php echo ($present_status=='A'?'selected':'')?> >A</option>
			<option value="L" <?php echo ($present_status=='L'?'selected':'')?> >L</option>
			<option value="official_tour" <?php echo ($present_status=='official_tour'?'selected':'')?>>O</option>
			<option value="WL" <?php echo ($GetEmpShift==$chkDay?'selected':'')?>>WL</option>
			<option value="HO" <?php echo ($present_status=='HO'?'selected':'')?>>HO</option>
			</select>
		<?php 	
		}
		?>
		</td>
		<td><?php echo $_REQUEST['e_code'];?>
		<input name="e_code[<?php echo $i;?>]" type="hidden" id="e_code_<?php echo $i;?>" value="<?php echo $_REQUEST['e_code'];?>" size="15" /></td>
		<td><?php echo $emp_row['emp_name'];?>
		<input type="hidden" value="<?php echo $emp_row['id'];?>" name="employee[<?php echo $i;?>]" id="employee_<?php echo $i;?>" /></td>
		<td><?php echo $s->getField('desigmaster','desigcode',$emp_row['designation'],'designame');?>
		<input type="hidden" value="<?php echo $emp_row['id'];?>" name="employee[<?php echo $i;?>]" id="employee_<?php echo $i;?>" /></td>
		<td>
		<?php 
		$emp_shift_sql=DB_query("Select id ,shift_for,shift_category  from shift_master where site='".$emp_site."' and shift_for='".$GETEMPTYPE."'",$db);?>
		<select name="emp_shift[<?php echo $i;?>]" id="emp_shift_<?php echo $i;?>" onchange="ShowDiv(this.value,'hid-shift_<?php echo $i;?>','emp_shifttime.php?AttSite=<?php echo $_REQUEST['site'].'&shift_for='.$GETEMPTYPE.'&i='.$i?>')" style="width:auto;">
		<?php while($emp_shift_row=DB_fetch_array($emp_shift_sql))
		{?>
		<option value="<?php echo $emp_shift_row['id'];?>"<?php echo ($emp_shift==$emp_shift_row['id']?'selected':'')?>><?php echo $emp_shift_row['shift_for'].'('.$emp_shift_row['shift_category'].')'?></option>
		<?php 
		}
		?>
		</select>
		</td>
		<td colspan="2">
		<table width="100%" id="hid-shift_<?php echo $i; ?>" border="0" align="center">
		<tr>
		<td width="50%">
	<?php 
	$disabled='';
	if($GetEmpShift==$chkDay){$disabled="disabled";}?>
	  	<input name="" type="hidden" id="shift_start_<?php echo $i;?>" value="<?php echo $_REQUEST['shift_start'];?>" style=""/>
	  	<select name="shiftstart_AMPM[<?php echo $i;?>]" id="shiftstart_AMPM_<?php echo $i;?>" style="width:40px;display:none" >
	  	<option value="AM"<?php echo ($_REQUEST['shiftstart_AMPM']=='AM'?'selected':'')?>>AM</option>
	  	<option value="PM"<?php echo ($_REQUEST['shiftstart_AMPM']=='PM'?'selected':'')?>>PM</option>
	  	</select>
  	  	<input name="" type="hidden" id="shift_end_<?php echo $i;?>" value="<?php echo $_REQUEST['shift_end'];?>" size="3" />
<?php  //echo $setAMPM= $_REQUEST['shiftend_AMPM'];?>
	   	<select name="shiftend_AMPM[<?php echo $i;?>]" id="shiftend_AMPM_<?php echo $i;?>" style="width:40px;display:none"  >
		<option value="AM"<?php echo ($_REQUEST['shiftend_AMPM']=='AM'?'selected':'')?>>AM</option>
	  	<option value="PM"<?php echo ($_REQUEST['shiftend_AMPM']=='PM'?'selected':'')?>>PM</option>
	  	</select>
	  	<input name="time_in[<?php echo $i;?>]" type="text" id="time_in_<?php echo $i;?>" value="<?php echo $_REQUEST['time_in'][$i] ;?>"  style="width:40px" <?php //echo $disabled;?>/>
	  	<select name="timein_AMPM[<?php echo $i;?>]" id="timein_AMPM_<?php echo $i;?>" style="width:40px">
	  	<option value="AM"<?php echo ($_REQUEST['timein_AMPM'][$i]=='AM'?'selected':'')?>>AM</option>
	  	<option value="PM"<?php echo ($_REQUEST['timein_AMPM'][$i]=='PM'?'selected':'')?>>PM</option>
	  	</select>	
		</td>
    	<td width="50%">
	 	<input name="time_out[<?php echo $i;?>]" type="text" id="time_out_<?php echo $i;?>" value="<?php echo $_REQUEST['time_out'][$i];?>" style="width:40px" <?php //echo $disabled;?>/>
	  	<select name="timeout_AMPM[<?php echo $i;?>]" id="timeout_AMPM_<?php echo $i;?>" style="width:40px" >
	  	<option value="AM"<?php echo ($_REQUEST['timeout_AMPM'][$i]=='AM'?'selected':'')?>>AM</option>
	  	<option value="PM"<?php echo ($_REQUEST['timeout_AMPM'][$i]=='PM'?'selected':'')?>>PM</option>
	  	</select>
	 	</td>
		</tr>
		</table>
		</td>
		<td><input type="text" name="" id="out_date_<?php echo $i;?>" value="<?php echo $_REQUEST['attendane_date'];?>" style="width:90%" class="datepicker"/><input type="hidden" id="current_date_<?php echo $i?>" value="<?php echo $_REQUEST['attendane_date'];?>" /></td>
		<td><input name="officework_hrs[<?php echo $i;?>]" id="officework_hrs_<?php echo $i;?>" type="text" onfocus="calculate_officeworkhours(<?php echo $i;?>)" value="" size="3" <?php //echo $disabled;?>/></td>
		<td><input name="work_hrs[<?php echo $i;?>]" type="text" id="work_hrs_<?php echo $i;?>"  size="3" onfocus="calculate_workhours(<?php echo $i;?>,document.getElementById('present_status_<?php echo $i?>').value)" <?php //echo $disabled;?>/></td>
		<td><input name="ot_hrs[<?php echo $i;?>]" type="text" id="ot_hrs_<?php echo $i;?>"  size="3"  onfocus="calculate_OverTime(<?php echo $i;?>,document.getElementById('present_status_<?php echo $i?>').value)" <?php // echo $disabled;?>/></td>
		<td><select name="leave_code[<?php echo $i;?>]" id="leave_code_<?php echo $i;?>" style="width:50px" <?php echo $disabled;?>>
        <option value="000">---</option>
        <?php
	$leave_sql=DB_query("select * from leavemaster",$db); 
	while($leave_row=DB_fetch_array($leave_sql)){
	?>
        <option value="<?php echo $leave_row['leavecode'];?>"><?php echo $leave_row['leavename'];?></option>
        <?php } ?>
      </select></td>
		</tr>
		<?php  $i++;
	}
}
else
{
	echo "<tr><td colspan='13'><h3 align='center'>No employee at this branch</h3></td></tr>";
}
?>
<?php } 
else
{echo "<div align='center'> <h3>Today is HoliDay</h3></div>";}
?>
<input type="hidden" name="rows" id="rows" value="<?php echo $i;?>" />
<?php }?>
</table>
</div>
<p><input type="submit" name="Save" value="Save" /></p>
</form>
<?php 
//if($_REQUEST['site']!='')
{
$att_sql="Select * from daily_attendance where attendance_date='".ConvertSQLDate($date)."'";
$att_res=DB_query($att_sql,$db);
if(DB_num_rows($att_res)>0)
{
?>
<h3 align="center"><strong>View Attendance For Today (<?php echo $date;?>)</strong></h3>

<table width="100%" border="0" cellspacing="1" cellpadding="2"  style="border: solid 1px #999999; background:#666666">
  <tr>
    <th width="6%"><div align="center"><strong>Sr.</strong></div></th>
    <th width="5%"><div align="center"></div></th>
	<th width="14%"><div align="center"></div></th>
	<th width="8%"><div align="center"></div></th>
    <th width="15%"><div align="center"></div></th>
    <th width="12%"><div align="center"></div></th>  <th  ><div align="center"></div></th> 
    <th colspan="2"><div align="center"><strong>Shift</strong></div></th>
    <th colspan="2"><div align="center"><strong>Time</strong></div></th>
    <th width="6%"><div align="center"><strong>OT</strong></div></th>
    <th width="8%"> <div align="center"><strong>Leave</strong></div></th>
  </tr>
  <tr>
    <th><div align="center"><strong>No.</strong></div></th>
	<th><div align="center"><strong>Site</strong></div></th>
	<th><div align="center"><strong>Status</strong></div></th>
    <th><div align="center"><strong>E Code</strong></div></th>
    <th><div align="center"><strong>Employee Name </strong></div></th>
	<th><div align="center"><strong>Employee Designation </strong></div></th>
	<th><div align="center"><strong>Shift </strong></div></th>
	<th width="8%"><div align="center"><strong>Start</strong></div></th>
    <th width="7%"><div align="center"><strong>End</strong></div></th>
    <th width="6%"><div align="center"><strong>In</strong></div></th>
    <th width="5%"><div align="center"><strong>Out</strong></div></th>
    <th><div align="center"><strong>HRS</strong></div></th>
    <th><div align="center"><strong>Code</strong></div></th>
  </tr>
<?php 
$j=1;
while($att_row=DB_fetch_array($att_res))
{
?>  
  <tr bgcolor="#FFFFFF">
    <td><div align="center"><?php echo $j; ?></div></td>
	<td><div align="center">
	  <?php 
	$site_query=DB_query("Select loccode,locationname from locations where loccode='".$att_row['site']."'",$db);
	$site_res=DB_fetch_array($site_query);
	echo $site_res['locationname']; ?>
    </div></td>
	<td><div align="center">
	  <?php 
	if($att_row['pres_abs']=='WL')echo "Weekly Off"; 
	else if($att_row['pres_abs']=='P')echo "Present"; 
	else if($att_row['pres_abs']=='HL')echo "Holiday"; 
	else if($att_row['pres_abs']=='A')echo "Absent"; 
	else if($att_row['pres_abs']=='L')echo "Leave";
	else echo "Official Tour";
	//echo $att_row['pres_abs']; ?>
    </div></td>
    <td><div align="center"><?php echo $att_row['e_code']; ?></div></td>
    <td><div align="center">
      <?php 
	$empl_sql=DB_query("Select emp_name,designation from empmaster where id='".$att_row['employee']."'",$db);
	$empl_res=DB_fetch_array($empl_sql);
	echo $empl_res['emp_name']; ?>
    </div></td>
    <td><div align="center">
        <?php 
      echo $s->getField('desigmaster','desigcode',$empl_res['designation'],'designame');
    //echo $empl_res['designation'];?>
    &nbsp;</div></td>
     <td><div align="center">
        <?php //echo  $att_row['shift_category'];
      echo $s->getField('shift_master','id',$att_row['shift_category'],'shift_category');
  ?>
     &nbsp;</div></td>
  <?php 
  $shift_timings=DB_query("Select shift_start,start_AMPM,shift_end,end_AMPM from shift_master where id='".$att_row['shift_category']."'",$db);
  $shift_res=DB_fetch_array($shift_timings);
  ?>
    <td><div align="center">
      <?php  echo $att_row['pres_abs']!='A'? $shift_res['shift_start']." ".$shift_res['start_AMPM'] :''; ?>
    </div></td>
    <td><div align="center">
      <?php  echo $att_row['pres_abs']!='A'?$shift_res['shift_end']." ".$shift_res['end_AMPM'] :''; ?>
    </div></td>
    <td><div align="center">
      <?php  echo $att_row['pres_abs']!='A'?$att_row['time_in']." ".$att_row['timein_AMPM'] :''; ?>
    </div></td>
    <td><div align="center">
      <?php  echo $att_row['pres_abs']!='A'?$att_row['time_out']." ".$att_row['timeout_AMPM'] :''; ?>
    </div></td>
    <td><div align="center">
      <?php  echo $att_row['pres_abs']!='A'?$att_row['ot_hrs'] :''; ?>
    </div></td>
    <td><div align="center">
      <?php 
	if(($att_row['leave_code']=='000' || $att_row['leave_code']=='') && $att_row['others']=='')
	{echo "N/A";}
	else
	{
		$query=DB_query("Select leavename from leavemaster where leavecode='".$att_row['leave_code']."'",$db);
		$res=DB_fetch_array($query);
		echo $res['leavename']; 
	}
	?>
    </div></td>
  </tr>
<?php $j++;
} ?>
</table>
<?php  
   }
}
?>