<?php
  session_start();
  $PageSecurity = 15;
 $title = _('Monthly Attendance') ;
  include('includes/fun-php.php');
  
function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}



function dateDiff1($d1, $d2)
	{
		return round(abs(strtotime($d1)-strtotime($d2))/86400)+1;
	}  

$currentyear=date('Y');
?>

 <script type="text/javascript" language="javascript">
 
function open_print(url)
{
window.open(url,'popup','height=500,width=750,left=200,scrollbars=yes');
}
 
function check()
{
	 var month=document.getElementById("month").value;	  
	 if(month=='000')
	 {
	 	alert('Please select month'); 
		document.getElementById("month").focus();
		return false;
	 }	
}

</script>
<link rel="stylesheet" type="text/css" href="css/table_frize_1.css">
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Monthly Attendance</h1>
<h3>*Please select a month to view attendance</h3>

<form action="" method="post">
 <table width="100%" align="center" class="" border="0" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
  	<tr class="row1">
	    <td width="13%"><strong>For Month</strong></td>  
	    <td width="35%">
			<select name="month" id="month">
	  			<option value="000">Select</option>
				<option value='01'<?php echo ($_REQUEST['month']=='01'?'selected':'')?>>January</option>
				<option value='02'<?php echo ($_REQUEST['month']=='02'?'selected':'')?>>February</option>
				<option value='03'<?php echo ($_REQUEST['month']=='03'?'selected':'')?>>March</option>
				<option value='04'<?php echo ($_REQUEST['month']=='04'?'selected':'')?>>April</option>
				<option value='05'<?php echo ($_REQUEST['month']=='05'?'selected':'')?>>May</option>
				<option value='06'<?php echo ($_REQUEST['month']=='06'?'selected':'')?>>June</option>
				<option value='07'<?php echo ($_REQUEST['month']=='07'?'selected':'')?>>July</option>
				<option value='08'<?php echo ($_REQUEST['month']=='08'?'selected':'')?>>August</option>
				<option value='09'<?php echo ($_REQUEST['month']=='09'?'selected':'')?>>September</option>
				<option value='10'<?php echo ($_REQUEST['month']=='10'?'selected':'')?>>October</option>
				<option value='11'<?php echo ($_REQUEST['month']=='11'?'selected':'')?>>November</option>
				<option value='12'<?php echo ($_REQUEST['month']=='12'?'selected':'')?>>December</option>
	  		</select>	
	  	</td>
	    <td width="13%"><strong>Year</strong></td>
	    <td>
			<select name="year" id="year">
	        <?php 
				for($year=date('Y');$year>1999;$year--){
			?>
	        	<option value="<?php echo $year;?>"<?php echo ($_REQUEST['year']==$year?'selected':'')?>><?php echo $year;?></option>
	        <?php }	?>
	    	</select>
		</td>
	</tr>
	<tr class="row1">
	  	<td><strong>Branch</strong></td>
	  	<td>
	  	<select name="site" id="site">
			<option value="0">Select</option>
			<?php 		
			
			$site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' 
				and loccode in('".$_SESSION['AssignedBranch']."')",$db);
				
				
				while($site_row=DB_fetch_array($site_sql))
				{
					$site1='';
					if($_REQUEST['site']==$site_row['loccode'])
						$site1="selected";
			?>
			<option value="<?php echo $site_row['loccode'];?>"<?php echo $site1; ?> ><?php echo $site_row['locationname'];?>
			</option>
			<?php }?>
		</select>
	  	</td>
	    <td><strong>Employee</strong></td>
		<td>
			<select name="employee_code" id="employee_code">
				<option value="">select</option>
				<?php 
					$emp_query = "select * from empmaster where CompanyId='".$_SESSION['ACGCompanyId']."' ORDER BY emp_name ASC";
					$emp_res = DB_query($emp_query,$db) or die(DB_error());
					while($emp_result = DB_fetch_array($emp_res))
					{
				?>
				 <option value="<?php echo $emp_result['id'];?>" <?php if($_REQUEST['employee_code']==$emp_result['id']) { ?> selected="selected" <?php } ?>><?php echo $emp_result['emp_name'];?></option>
				<?php } ?>
			</select>
		</td>
  	</tr>
  	<tr class="row1">
	  	<td colspan="4" style="text-align:center;"><input type="submit" name="View" value="View"  onclick="return check();" class="search"/>
		<a href="#" onclick="open_print('Print-monthly_attendance.php')" id="print"><input type="button" value="Print" id="print" class="search"/></a></td>
  	</tr>
</table>

</form>
<form action="" method="post">
<p align="left"><font color="#0000FF"><strong>P : Present &nbsp;|&nbsp; A : Absent &nbsp;|&nbsp; L : On Leave &nbsp;|&nbsp; HO : Holiday &nbsp;|&nbsp; WO : weekly Off</strong></font></p>

<div style="overflow:auto; width:1250px ; height:auto">
<?php
if($_REQUEST['View']!='')
{
  $month=trim($_REQUEST['month']);
  $year=trim($_REQUEST['year']);
  $employee_code=trim($_REQUEST['employee_code']);
  $site=trim($_REQUEST['site']);
  
  if(date('m')==$month)
  	$workingdays=date('d');
  else
  	$workingdays= cal_days_in_month(CAL_GREGORIAN, $month, $year);
 ?> 

<table width="7500"  border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid;">
  <tr>
  	<th>Code</th>
  	<th width="2%">Name</th>
   	<?php 
	  for($k=1;$k<=$workingdays;$k++) {
	   $startdate=$k.'-'.$month.'-'.$year;
	 	?>
    <th width="4%" style="padding:0;" valign="top">
    	<div id="amc-tab">
    	<table width="100%" border="0" cellspacing="0" cellpadding="3">
    		<tr>
    			<th colspan="4"><strong><?php  echo $k.'/'.$month.'/'.$year; ?> 
	</strong>
 </th>
</tr>
<tr>
	<th width="80">In Time</th>
	<th width="80">Out Time</th>
	<th width="80">Hours Spend</th>
	<th width="80">Type</th>
</tr>

</table>

</div>

</th>
	<?php } ?>	     
  </tr>
  <?php 
	$filter=array();
	if($employee_code!='')
		$filter[]=" id='".$employee_code."'";
	if ($site!='')
		$filter[]=" emp_site='".$site."'";
	if(count($filter)>0)
	
		$filter =implode(' and ', $filter);

		if($month==12){
			$month1=1;
			$year1=$year+1;
		} else{
			$month1=$month+1;
			$year1=$year;
		}	
	$searchval=$month1."/01/".$year1;
   	  $att_sql="select * from empmaster where CompanyId='".$_SESSION['ACGCompanyId']."' AND $filter ORDER BY emp_name ASC";
	$att_result=DB_query($att_sql,$db);
	$i=0;$j=1;
	$numrow1=DB_num_rows($att_result);
	if($numrow1>0){
	while($att_row=DB_fetch_array($att_result)) {

?>
  <tr>
  	<td><?php echo $att_row['emp_code'];?></td>
    <td><?php echo $att_row['emp_name'];?></td>
 	<?php for($k=1;$k<=$workingdays;$k++) { 
 		$startdate1=$year.'-'.$month.'-'.$k; ?>
<td style="padding:0;" valign="top">
    	<?php 
 
   // echo "SELECT time_in,time_out,work_hrs,pres_abs FROM daily_attendance WHERE attendance_month='".$month."' AND attendance_date='".$startdate1."' AND employee='".$att_row['id']."'";

    $atndcRec=DB_query("SELECT time_in,time_out,work_hrs,pres_abs FROM daily_attendance WHERE attendance_month='".$month."' AND attendance_date='".$startdate1."' AND employee='".$att_row['id']."'",$db);
    	if(DB_num_rows($atndcRec)>0){ 
    			$recrd=DB_fetch_assoc($atndcRec);
    		?>

    	
    	<div id="amc-tab">
    	<table width="100%" border="0" cellspacing="0" cellpadding="3">
    		<tr>
    			<td width="59"><?php echo $recrd['time_in'];?></td>
    			<td width="59"><?php echo $recrd['time_out'];?></td>
    			<td width="62"><?php echo convertToHoursMins($recrd['work_hrs'], '%02d:%02d');?></td>
    			<td width=""><?php echo $recrd['pres_abs'];?></td>
    		</tr>
    	</table>
       </div>
       <?php } else{?>
		<div id="amc-tab">
    	<table width="100%" border="0" cellspacing="0" cellpadding="3">
    		<tr>
    			<td width="59">--</td>
    			<td width="59">--</td>
    			<td width="62">--</td>
    			<td width="">--</td>
    		</tr>
    	</table>
       </div>

       <?php } ?>
    
</td>
<? }?>
  </tr>

   <?php $i++;} } } ?>
  

</table>
 <?php

 ?>
</div>
</form>

</div>
</div>