<?php
	ob_start();
	session_start();
?>
<?php 
include_once("../class/class.MySQL.php");
include_once("../class/userdefinedfunctions.php");
$db	=new MySQL();
$userfunction	=new userdefinefunctions();

//////////////////////////////// GET Branch list /////////////////////////
if(isset($_GET['act']) && $_GET['act']=='getbranch'){
	$empid = $_GET['eid'];
	$getBranch = $db->ExecuteSQL("SELECT branchid FROM employee WHERE e_id='$empid'");
	echo '{"emp":'.json_encode($getBranch).'}';
}else{




//----------------------------------------retrieving get values--------------------//
$limit=$page_emp;
$page=1; $employees='';
$basicnt=0;
$sumtotaltime =0;
$month=date("m");
$year=date("Y");
if(isset($_GET['page']))
$page=$_GET['page'];
else
$page=1;
if($page) 
		$start = ($page - 1) * $limit;  //first item to display on this page
	else
		$start = 0;			
if(isset($_GET['month']) && ($_GET['month']!='undefined' || $_GET['month']!=''))
{

$month=$_GET['month'];
}
else
{
$month=date("m");
}
if(isset($_GET['year'])  && ($_GET['year']!='undefined'  || $_GET['year']!='' ))
{
$year=$_GET['year'];
}
else
{
$year=date("Y");
}
if(isset($_GET['employees']))
{
 $_GET['employees'];
 $employees=$_GET['employees'];
}
//---------------------------------------------------------------------------------------------//
$start=0;
$view="";

if($month=='null')$month=date('m');if($year=='null')$year=date("Y");
if($employees=='null'){
$qqry="select  e_id from employee limit 0,1";
$ssql=mysql_query($qqry);
$oobj=mysql_fetch_object($ssql);
$employees=$oobj->e_id;
}

$nummonth=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$check=$year.'-'.$month;

$todaysday=date('Y-m-d');

$endOfLastmth=$year.'-'.$month.'-31';
$firstOfLastmth=$year.'-01-01';
    $lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$month' and YEAR(date)='$year' and is_blocked='0'");
   
   $holidays=array();
   foreach($lastMtnHolidays as $lastMtnHolidays){
   $holidays[]=$lastMtnHolidays['date'];
   }
   //echo "SELECT count(id) FROM time WHERE e_date>='$firstOfLastmth' and date<='$endOfLastmth'";
   
   
    
   $workingDays=  $db->getWorkingDays($firstOfLastmth,$endOfLastmth,$holidays);

	 $lastyrattendance = $db->fetchQuery("SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<='$endOfLastmth' and e_id='$employees'");
	 $yrattendance=$lastyrattendance[0]['count(distinct(e_date))'];
	
	$availedleaves=$workingDays-$yrattendance;
	 
	
	 	$nolossleaveapplied = $db->fetchQuery("Select count(*) as nolossleave from payroll_user_leave_requests where employee_id=$employees and is_cancelled=0 and imd_status=1 and day_from>='$firstOfLastmth' and day_from<='$endOfLastmth' and (leave_id='3' or leave_id='1') ");
	 
	   $nolossleaves=$nolossleaveapplied[0]['nolossleave'];
	
	$leavesentitledqry = $db->fetchQuery("SELECT sum(`number_of_days`) as entitled FROM `payroll_leave` WHERE `lid` IN  (select `leave_id` from `payroll_user_leave` where `employee_id`='$employees')");
	$leavesentitled=$leavesentitledqry[0]['entitled'];
	
	
	$balanceleaves=$leavesentitled-$nolossleaves;
	
	if($balanceleaves<0)
	{
	$balanceleaves=0;
	}
/* $sql=" select a.* from time as a,employee as b where a.e_id=b.e_id and a.e_id=".$employees." and    DATE_FORMAT(a.e_date,'%m-%Y') = '".$month."-".$year."' limit ".$start.",".$limit;
$employees=$db->fetchQuery($sql);

*/ $view='
<div class="container">
  <!-- /top-wrap -->
<div class="content-wrap">
<div class="cart-back-title">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10%">&nbsp;</td>
    <td width="11%">&nbsp;</td>
    <td width="10%">&nbsp;</td>
    <td width="35%"><p>FORM VI <br />[ Rule 29 (5) ]
    </p></td>
    <td width="17%">&nbsp;</td>
    <td width="17%">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="2"><h2>Muster Roll</h2></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>


<div class="attendance-cell-1">
<table border="0" align="center" cellpadding="0" cellspacing="5">
  <tr>
    <td width="135" align="left" valign="bottom"><strong>Name of Establishment</strong></td>
    <td width="219" align="left" valign="bottom"  class="display-border-1">Group Mukkadan</td>
    <td width="34" align="left" valign="bottom"><strong>Place</strong></td>
    <td width="187" align="left" valign="bottom"  class="display-border-1">&nbsp;</td>
  </tr>
</table>
</div><!-- /attendance-cell-1 -->
</div>
</div><!-- /content-wrap --> 


<div class="display-cell-1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
 
  <tr>
  
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="4%" align="left" valign="bottom" class="display-border-1"><strong><img src="images/a-head-1.jpg" alt="" width="21" height="87" /></strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Name</strong></td>
        <td width="9%" align="left" valign="bottom" class="display-border-1"><strong>Father/Husband Name</strong></td>
        <td width="4%" align="left" valign="bottom" class="display-border-1"><strong>Sex</strong></td>
        <td width="8%" align="left" valign="bottom" class="display-border-1"><strong>Date of entry into service</strong></td>
        <td width="10%" align="left" valign="bottom" class="display-border-1"><strong>Nature of Work</strong></td>
        <td width="46%" align="left" valign="bottom" class="display-border-1"><table border="0" align="center" cellpadding="0" cellspacing="5">
          <tr>
            <td width="135" align="left" valign="bottom"><strong>For the period ending</strong></td>
            <td width="219" align="left" valign="bottom"  class="display-border-1">&nbsp;</td>
            </tr>
        </table>
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>';
           
            $numdays=cal_days_in_month(CAL_GREGORIAN,4,2014);
			for($k=1;$k<=$numdays;$k++)
			{
            
               $view.='<td width="40" align="left" valign="middle"><strong>'.$k.'</strong></td>';
               } 
              
               $view.='</tr>
          </table></td>
        <td width="7%" align="left" valign="bottom" class="display-border-1"><img src="images/a-head-5.jpg" alt="" width="21" height="76" /></td>
      </tr>';
      
	    $i=1;
  		$sql = mysql_query("select * from employee join designation on employee.e_post = designation.desig_id");
		while($rw = mysql_fetch_array($sql))
		{
			if($rw['e_gender'] == 'Female')
			{
				$gender = 'F';
			}
			if($rw['e_gender'] == 'Male')
			{
				$gender = 'M';
			}
  		
     	  $view.='<tr>
        <td align="left" valign="top" class="display-border-1">'.$i.'</td>
        <td align="left" valign="top" class="display-border-1">'.$rw['e_fname'].'  '.$rw['e_lname'].'</td>
        <td align="left" valign="top" class="display-border-1">'.$rw['e_mname'] .'</td>
        <td align="left" valign="top" class="display-border-1">'.$gender.'</td>
        <td align="left" valign="top" class="display-border-1">'.$rw['date_join'].'</td>
        <td align="left" valign="top" class="display-border-1">'.$rw['desig_name'].'</td>
        <td align="left" valign="top" class="display-border-1"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>';
		
		$userid = $rw['e_id'];
		$dateno = array();
		
		$attsql = $db->fetchQuery("select distinct(e_date) as edate from time where e_id = '$userid' and  MONTH(e_date) = '04' and YEAR(e_date) = '2014' limit 0,$numdays");
		
		$status;
		$j=1;
		foreach(range(1,$numdays)as $c){
		//$c=1;
				if($attsql==NULL){
				$day=0;
				}else{
				 $day = date('j',strtotime($attsql[$j-1]['edate']));
				}
				if($day == $c){
					$dts = 	'P';
				}else{
					$dts = 	'A';
					$j--;
				}
				
			
		
		
		
          
            $view.='<td width="40" align="left" valign="middle">'.$dts.'</td>';
         
      
	  $j++;
	    }
		$view.='</tr>
        </table></td>
        
      </tr>';
       
	   $i++;
	   } 
	  
      
    $view.='</table></td>
  </tr>
  
  <tr>
    <td height="70" class="display-border-1">&nbsp;</td>
  </tr>
  <tr>
    <td height="34" valign="top">I . Inserted by Notn. No. 15447 E I / 73 / LBR dt . 19-11-1947 in K.G.No. 53 dt. 31-12-1947.</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>



</div><!-- /display-cell-1 -->


</div>';


$_SESSION['download_musteroll_view']=$view;
echo $view;
}