<?php $PageSecurity = 15;
include('Class-EmpShift.php');
//include('includes/session.inc');
$title = _('View Daily Attendance') ;
//include('includes/header.inc');
//include('includes/SQL_CommonFunctions.inc');
//include('includes/function_lib.php');

$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];echo "<br>";

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="index.php?route=view_daily_attendance&modid=".$modid."&frmid=".$frmid."";



$date=date('d/m/Y');
 
$time=strftime(($DefaultClock==12) ? '%I:%M %p' : '%H:%M');
 
if($_REQUEST['SaveAttandence'] && $_REQUEST['action']=='Edit')
{
 
    	$data['site']=$_POST['site'];
		$data['e_code']=$_POST['emp_code'];
		$data['card_no']=$_POST['card_no'];
		 
		$data['time_in']=$_POST['time_in'];
		$data['timein_AMPM']=$_POST['timein_AMPM'];
		$data['time_out']=$_POST['time_out'];
		$data['timeout_AMPM']=$_POST['timeout_AMPM'];
		//$data['work_hrs']=$_POST['work_hrs'];
		$data['ot_hrs']=$_POST['ot_hrs'];
		$data['pres_abs']=$_POST['pres_abs'];
		 
		$cond="id='".$_REQUEST['id']."'";
		$Recordupdation=$s->RecordEdit('daily_attendance', $data, $cond); 
	 $s->pageLocation( $target."&site=".$_POST['site']."&att_date=".$_POST['attendance_date']."&daction=Updated&View=1");
		
		 
	
}
?>
<script type="text/javascript" language="javascript">
function check()
{
	var site=document.getElementById("site").value;
	var date=document.getElementById("att_date").value;
	if(date=='')
	{alert('Please enter a date'); 
	document.getElementById("att_date").focus();
	return false;}
	
	if(site=='000')
	{alert('Please select site'); 
	document.getElementById("site").focus();
	return false;}
	
	
}
</script>
<br>
<h2 align="center"><strong>View Attendance</strong></h2>
<br>
<form action="" method="post">
<table width="40%" border="1" cellspacing="2" cellpadding="2" align="center">
  <tr>
    <th class="tabledata1"><strong>VIEW ATTENDANCE OF - </strong></th>
    <td>
    	<input type="text" name="att_date" id="att_date" class="datepicker" size="15" value="<?php echo $_REQUEST['att_date'] ?>">
    	<input type="text" name="att_from_date" id="att_from_date" class="datepicker" size="15" value="<?php echo $_REQUEST['att_from_date'] ?>"></td>
  </tr>
  
<tr>
<th class="tabledata1"><strong>ATTENDANCE FOR SITE</strong></th>
<td><select name="site" id="site" style="width:135px">
<option value="000">---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 class="tabledata1"><b>EMP CODE/NAME</b></th><td><input type="text" name="Emp_code" value="<?php echo $_REQUEST['Emp_code'] ?>" /></td></tr>
</table>
 
<p align="center"><input type="submit" name="View" value="View" onclick="return check();" ></p>
</form>

<?php
if($_REQUEST['View']!=''  )
{?>
	
	
	  <?php 
  if(isset($_REQUEST['daction']) )
   {?>
     
    	<div class="success"><?php	echo  "Record ".$_REQUEST['daction']." Successfully";  ?></div> 
   
  
   <?php }?>
  
<fieldset>
<div style="overflow:scroll ;width:800px;height:400px;" >
<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <th scope="col"><strong>Sr.</strong></th>
    <th scope="col">&nbsp;</th>
	<th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
    <th scope="col" colspan="2"><div align="center"><strong>Shift</strong></div></th>
    <th scope="col" colspan="2"><div align="center"><strong>Time</strong></div></th>
    <th scope="col"><div align="center"><strong>OT</strong></div></th>
	<th scope="col"><div align="center"><strong>Leave</strong></div></th>
	 <th scope="col">&nbsp;</th> <th scope="col">&nbsp;</th> <th scope="col">&nbsp;</th> <th scope="col">&nbsp;</th>
    
	<th scope="col"><div align="center"><strong>Action</strong></div></th>
  </tr>
  <tr>
    <th><strong>No.</strong></th>
	<th><strong>Attendance Date</strong></th>
    <th><strong>E Code</strong></th>
    <th><strong>Card No. </strong></th>
    <th><strong>Employee Name </strong></th>
	<th><strong>Employee Designation </strong></th>
    <th><strong>Start</strong></th>
    <th><strong>End</strong></th>
    <th><strong>In</strong></th>
    <th><strong>Out</strong></th>
    <th><strong>HRS</strong></th>
	<th><strong>Code</strong></th>
	 <th scope="col">Early Arr.</th> 
	 <th scope="col"> Late Arr.</th> 
	 <th scope="col">Early  Dep.</th> 
	 <th scope="col">Late Dep.</th>
    
	 <th><strong>Edit</strong></th>
  </tr>
<?php 
 
 
	$filter=array();
	// if($_REQUEST['att_date']!='')
	// $filter[]="B.attendance_date='".FormatDateForSQL($_REQUEST['att_date'])."'";
	 if($_REQUEST['att_from_date']!='' && $_REQUEST['att_date']!='')
	 $filter[]="B.attendance_date BETWEEN '".FormatDateForSQL($_REQUEST['att_date'])."' AND '".FormatDateForSQL($_REQUEST['att_from_date'])."'";
	 if($_REQUEST['site']!='000')
	 $filter[]="B.site = '".$_REQUEST['site']."' ";
	if($_REQUEST['Emp_code']!='')
	$filter[]="(B.e_code = '".$_REQUEST['Emp_code']."' or A.emp_name like '%".$_REQUEST['Emp_code']."%')  ";
	
	
if(count($filter)>0)
{	
   $search=implode(" and ",$filter);
	//$search=$filter1; 
}else{
	$search='';
	
}
 
 
  $sql="select B.*,A.emp_code ,A.emp_name,A.designation from daily_attendance B, empmaster A where A.id=B.employee and $search ";
//echo $sql;
$result=DB_query($sql,$db);
$j=1;
$l=0;
while($myrow=DB_fetch_array($result))
{
	  if($l++%2==0)
	  	{
	  		$bg="cccccc" ;
		}
	  else
	  	{ 	$bg="ffffff" ;
	  	}
	  
?>  
  <tr bgcolor='#<?php echo $bg; ?>' >
    <td><?php echo $j; ?></td>
	<td><?php echo ConvertSQLDate($myrow['attendance_date']); ?></td>
    <td><?php echo $myrow['emp_code']; ?></td>
    <td><?php echo $myrow['card_no']; ?></td>
    <td><?php echo $myrow['emp_name']; ?>	 </td>
    <td><?php 
     
   echo $s->getField('desigmaster','desigcode',$myrow['designation'],'designame');
    //echo $empl_res['designation'];?></td>
	<td><?php echo $myrow['shift_start'].' '.$myrow['shiftstart_AMPM']; ?></td>
    <td><?php echo $myrow['shift_end'].' '.$myrow['shiftend_AMPM'];; ?></td>
    <td><?php echo $myrow['time_in'].' '.$myrow['timein_AMPM'];; ?></td>
    <td><?php echo $myrow['time_out'].' '.$myrow['timeout_AMPM']; ?></td>
    <td><?php echo $myrow['ot_hrs']; ?></td>
    <td><?php 
	if($myrow['leave_code']=='000')
	{echo "N/A";}
	else
	{
		$query=DB_query("Select leavename from leavemaster where leavecode='".$myrow['leave_code']."'",$db);
		$res=DB_fetch_array($query);
		echo $res['leavename']; 
	}?></td>
	
	<td><?php $stime[0]=$myrow['shift_start'].' '.$myrow['shiftstart_AMPM'];
	$stime[1]=$myrow['shift_end'].' '.$myrow['shiftend_AMPM'];
	$stime[2]=$myrow['time_in'].' '.$myrow['timein_AMPM'];
	$stime[3]=$myrow['time_out'].' '.$myrow['timeout_AMPM'];	
	$stime[4]=$myrow['office_hours'];
 	$stime[5]=$myrow['work_hrs'];			
 		echo ($myrow['pres_abs']=='P')  ?$OBJ_EMPSHIFT->EmpShift_Calculation($myrow['emp_code'],$stime,'early'):'-'  ;?></td>
	<td><?php echo $myrow['pres_abs']=='P' ?$OBJ_EMPSHIFT->EmpShift_Calculation($myrow['emp_code'],$stime,'latearr'):'-' ;?></td>
	 <td><?php echo $myrow['pres_abs']=='P' ?$OBJ_EMPSHIFT->EmpShift_Calculation($myrow['emp_code'],$stime,'earlydept'):'-' ;;?></td>
	<td><?php  echo $myrow['pres_abs']=='P' ?$OBJ_EMPSHIFT->EmpShift_Calculation($myrow['emp_code'],$stime,'latedept'):'-' ;;?></td>
	
	<td>
	<?php if($_SESSION['frmEdit']=='1'){?>
	<a href="<?php echo  $target; ?>&id=<?php echo $myrow['id'];?>&action=Edit&att_date=<?php echo ConvertSQLDate($myrow['attendance_date'])?>&site=<?php echo $_REQUEST['site'];?>">Edit</a>
	<?php }else {echo "No permission";}?>	</td>
  </tr>
<?php $j++;} 

?>
</table>
</div>
</fieldset>
<?php }else if($_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$query=$s->GetRecord('daily_attendance',$condition);
	$result=DB_query($query,$db);
	echo DB_num_rows($result);
	$row=DB_fetch_array($result);
	 	 $emp_id = $row['employee'];
		 $site= $row['site'];
		 $emp_name =$s->getField('empmaster','id',$emp_id,'emp_name');
		 $emp_code =$s->getField('empmaster','id',$emp_id,'emp_code');  
		 $card_no=$row['card_no'];
		 $time_in= ($row['time_in']!=''?$row['time_in']:'00:00');
		 $time_out=($row['time_out']!=''?$row['time_out']:'00:00');
		 $pres_abs=$row['pres_abs'];
		 
		 $ot_hrs=$row['ot_hrs'];
		 $timein_AMPM=$row['timein_AMPM'];
		  $timeout_AMPM=$row['timeout_AMPM'];
		  $attendance_date  =ConvertSQLDate($row['attendance_date']);
	?>
	<center><h2>EDIT DAILY ATTENDANCE</h2></center>
	<form action="" method="post"  >
	<table border="1" cellpadding="0" cellspacing="0" width='50%'>
		<tr>
		<td>Attendance Status</td>
		 <td>
<select name="pres_abs" id="pres_abs" style="width:auto">
 <option value="0">--</option>
	  <option value="P" <?php if($pres_abs=='P') echo "selected";?>>P</option>
	  <option value="A" <?php if($pres_abs=='A') echo "selected";?>>A</option>
	  <option value="L" <?php if($pres_abs=='L') echo "selected";?>>L</option>
	   <option value="official_tour" <?php if($pres_abs=='official_tour') echo "selected";?>>O</option>
	  </select>	  </td>
		</tr>
		
		<tr><td>Employee Site </td>
			<td><input type="text" name='site' value="<?php echo $site ?>" readonly></td>			 
		</tr>
		
		<tr><td>Empcode</td>
			<td><input type="text" name='emp_code' value="<?php echo $emp_code ?>" readonly></td>
			 
		</tr>
		<tr><td>Employee Name </td>
			<td><input type="text" name='emp_name' value="<?php echo $emp_name ?>" readonly></td>
			 
		</tr>
		<tr><td>Card No. </td>
			<td><input type="text" name='card_no' value="<?php echo $card_no ?>"></td>
			 
		</tr>
		<tr><td>ATTENDANCE Date </td>
			<td><input type="text" name='attendance_date' value="<?php echo $attendance_date ; ?>" readonly></td>
			 
		</tr>
		 
		<tr><td>Time In</td>
			<td><input type="text" name='time_in' value="<?php echo $time_in ?>" size=5>
		<select name="timein_AMPM"   style="width:40px">
	  <option value="AM"<?php echo ($timein_AMPM=='AM'?'selected':'')?>>AM</option>
	  <option value="PM"<?php echo ($timein_AMPM=='PM'?'selected':'')?>>PM</option>
	  </select>
				
			</td>
			 
		</tr>
		<tr><td>Time Out</td>
			<td><input type="text" name='time_out' value="<?php echo $time_out ?>"  size=5>
				<select name="timeout_AMPM"   style="width:40px">
	  <option value="AM"<?php echo ($timeout_AMPM=='AM'?'selected':'')?>>AM</option>
	  <option value="PM"<?php echo ($timeout_AMPM=='PM'?'selected':'')?>>PM</option>
	  </select>
				
			</td>
			 
		</tr>
		<tr><td>OT</td>
			<td><input type="text" name='ot_hrs'  value="<?php echo $ot_hrs ?>"></td>
			 
		</tr>
		<tr>
			<td colspan="2"><input type="submit" name='SaveAttandence' value="Save"></td>
			 
		</tr>
		
	</table>
	</form>
	
	
	
	
	
	
	
	
	<?php }?>
 