<?php ob_start();
	session_start();?>
<!DOCTYPE html>
<html>
    <head>
 <?php include("header-js.php");?>	
		
<!-----hrm-------------->		
<!-- Responsive -->
<link rel="stylesheet" href="css/responsive.css" />


<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/styles.css" />
<!-- Responsive -->
<script language="javascript" src="js/css3-mediaqueries.js" type="text/javascript"></script>



<!-----hrm-------------->

    </head>
	<script type="text/javascript">         
   
$(function(){
		   
	//getAllMonths();
	//getAllYear();	
	setTimeout(function(){  // This timeout for overwrite multiple function colling in this part.					
		$('#month_attendance').getmonths(new Date().getMonth()+1);
		$('#year_attendance').getYears(new Date().getFullYear());
	},200);
	

	
});
var serviceURLHR =localStorage['serviceURLHR'];

	$.getJSON(serviceURLHR + 'branch.php?action=load',function(data){
		branch = data.branch;
		
		$.each(branch, function(index, branch) {
									 
							$('#branch_attendance').append('<option value="'+branch.branch_id+'">'+branch.branch_name	+'</option>');
							
		});	
	});


	
	jQuery.fn.getmonths = function setAllMunths(paymentmonth){
	
	$this = this;
	var currentMonth =paymentmonth;
	$this.html(''); 

var months = ["January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December"];

	$.each(months,function(index,months){
		if( (parseInt(index)+1) == currentMonth){
			$this.append('<option selected="selected" value="'+ (parseInt(index)+1) +'">'+months+'</option>');	
		}else{
			$this.append('<option value="'+ (parseInt(index)+1) +'">'+months+'</option>');	
		}
		
	});


}

////////////////////////////// Year list //////////////////////////////
jQuery.fn.getYears = function setAllYears(paymentyear){
	$this = this;
	var currentYear =paymentyear; 
	$this.html('');


    var dt = new Date();
	var thisyear =dt.getFullYear(); 
	
	for ( i = 2013; i < (parseInt(thisyear)+1); i ++ ) {  
		if( i == currentYear){
			$this.append('<option selected="selected" value="'+ i +'">'+i+'</option>');	
		}else{
			$this.append('<option value="'+ i +'">'+i+'</option>');	
		}
	}



}


		
	$(document).ready( function() { 
	if(!attendance_url_month && !attendance_url_year && !brah_url_id )
	{
	window.location="muster-roll.php#Attendance_show";

	}
	if(brah_url_id)
	{
		
		var brnchsplit=brah_url_id.split("#");
		setTimeout(function(){$('#branch_attendance').val(brnchsplit[0]) },5000);
		
	}
	
	if(attendance_url_year)
	{
		
		$('#year_attendance').val(attendance_url_year);
	}
	
	
	if(attendance_url_month)
	{
		setTimeout(function(){$('#month_attendance').val(attendance_url_month) },500);
		
	}
	
            var cur_year=new Date().getFullYear();
            var obj=document.getElementById("year_attendance");         
            for (var i = 2013; i <= 2030; i++)     {                
                opt = document.createElement("option");
                opt.value = i;
                opt.text=i;
                obj.appendChild(opt);
            }
            document.getElementById("year_attendance").value = cur_year;
			 var month1='';
		 	 var cur_mon=new Date().getMonth() + 1;
            var obj=document.getElementById("month_attendance");         
            for (var i = 1; i <= 12; i++)     {                
                opt = document.createElement("option");
				if(i<10) i='0'+i;	
				//-------------------------------------//
				if(i==1)month1='January';if(i==2)month1='February';;if(i==3)month1='March';if(i==4)month1='April';if(i==5)month1='May';
if(i==6)month1='Jun';if(i==7)month1='July';;if(i==8)month1='August';if(i==9)month1='September';if(i==10)month1='October';
if(i==11)month1='November';if(i==12)month1='December';
				//---------------------------------------//
                opt.value = i;
                opt.text=month1;
                obj.appendChild(opt);
            }
        document.getElementById("month_attendance").value = cur_mon;
        });
		
function printDiv(divName) {
	
     var printContents = document.getElementById(divName).innerHTML;
	
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();
	setTimeout(function(){document.body.innerHTML = originalContents;},3000);
     
}

function employeeattendanceroll(pagenum){
	
	var serviceURL = localStorage['serviceURL'];	
	var month=$("#month_attendance").val();
	var year=$("#year_attendance").val();
	var branch=$("#branch_attendance").val();

	location.href=serviceURL+"muster-roll.php?attendance_month="+month+"&attendance_year="+year+"&brah_id="+branch+"#Attendance_show";
		
		}
		
		
function  download_muster_roll(pagenum)
{
	var empid = $('#employees').val();
	
	var month=$("#month_muster").val();

var year=$("#year_muster").val();
var branch=$("#branch_muster").val();

//window.location.href = "timesheet-individual.php?muster_month="+month+"&muster_year="+year+"&branch_id="+branch+"#Muster_Roll_show";
	window.location = serviceURLHR+"downloadmusteroll.php?muster_month="+month+"&muster_year="+year+"&branch_id="+branch;

}

    </script>
	<?php
/*	ob_start();
	session_start();*/
	function firstSunday($DATE)
    {
	// $DATE='2014-04-01';
         $date = date('Y-m',strtotime($DATE));
         $day = trim(strtotime("%e",$DATE));

        for($day; $day <= '7'; $day++){
             $dd = strftime("%A",strtotime($date.'-'.$day));
            if($dd == 'Sunday'){
                return $day;
            }
        }
    }
?>


    <body class="skin-blue"> 
	<?php include("header.php");?>


                    <!-- Small boxes (Stat box) -->
                    

                    <!-- Main row -->
                    <div class="row">
                    
					<div id="wrapper" style="width:1048px;">



<div class="content">

<div class="report-tab">
<div class="report-cell-02">
	<!--<label><input type="button" value="Timesheet" data-page="Timesheet_show"  class="btn-tab-01 Timesheet_show" /></label>
	<label><input type="button" value="Muster Roll" data-page="Muster_Roll_show"  class="btn-tab-01 Muster_Roll_show" /></label>-->
    

<?php
if($_SESSION['usertype'] != 'Subadmin')
{	
?>
<!--<ul class="payroll-menu" style="margin-bottom:0px !important; ">
<li data-page="MusterRollshow" class="Muster_Roll_show" style="padding-bottom: 30px !important;"><translate>Muster Roll</translate></li>
</ul>-->
<!--<ul class="payroll-menu" style="margin-bottom:0px !important; ">
<li data-page="Attendance_show" class="Attendance_show"style="padding-bottom: 30px !important;width: auto !important;" ><translate>Attendance Report</translate></li>
</ul>-->
<!--<ul class="payroll-menu">
<li data-page="timesheet_edit" class="timesheet_edit" >Edit Timesheet</li>
</ul>-->
<?php } ?>

<!--<ul class="payroll-menu">
<li data-page="timesheet_approval" class="timesheet_approval" >Timesheet Approval</li>
</ul>-->
<div class="clear-float"></div>
</div>
<div class="Muster_roll_Grid"> 

<div class="report-left">

<h2><translate>Muster Roll</translate></h2>
</div>
<div class="clear-float"></div>

<div class="report-cell-03">
<label><!-- <input type="text" placeholder="From date" />  -->
<script>selectmonthattfn();</script>
<select name="month_attendance"  id="month_attendance"  class="selectclass"  style="width:100px;">
</select>
 
</label>
<label><!--<input type="text" placeholder="To date" /> -->
<select name="year_muster"  id="year_attendance"  class="selectclass"  style="width:100px;"></select>
</label>

<label>
<select name="branch_muster" id="branch_attendance"  style="width:75px;">
    <option value="">Branch</option>
    </select>
</label>




<label><input type="button" value="Search"  onclick="employeeattendanceroll(1)" /></label>
<label><input name="" type="button" value="Download Muster Roll" class="button-style-05" onClick="download_muster_roll(1)" style="width:250px;" /></label>
<div class="clear-float"></div>
</div><!-- /report-cell-03 -->


<div class="clear-float"></div>


<form method="post" enctype="multipart/form-data" id="muster_roll_form" name="muster_roll_form" >
<!-- Recruitment ////////////////////////////////////////////////////////////////////// -->		
                 
				 <?php
				 
				 $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>'; 
			/*	   $view='
<div class="container">
  <!-- /top-wrap -->
<div class="content-wrap">
<div class="cart-back-title">';*/

if(isset($_GET['attendance_month']) && $_GET['attendance_month']!='null')
	   {
	   $month=$_GET['attendance_month'];
	   }
	   else
	   {
	   $month = date('m');
	   }
	   if(isset($_GET['attendance_year']) && $_GET['attendance_year']!='null')
	   {
	   $year=$_GET['attendance_year'];
	   }
	   else
	   {
	   $year = date('Y');
	   }
	   if(isset($_GET['brah_id']) && $_GET['brah_id']!='null')
	   {
	   
	   
	   $branch_id=$_GET['brah_id'];
	  
	   $branchqry =$db->fetchQuery("select branch_name from branches where branch_id ='$branch_id'");
	  
	   $branchname=$branchqry[0]['branch_name'];
	   
	   }
	   else
	   {
	   $branch_id = ''; $branchname='';
	   }
	   $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' group by date");
   
   $holidays=array();
   foreach($lastMtnHolidays as $lastMtnHolidays){
   $holidays[]=$lastMtnHolidays['date'];
   }
  
   $noofholidays=count($holidays);
 
	    
$monthname=date("F", mktime(0, 0, 0, $month, 10));

	$cmpname = $db->fetchQuery("SELECT * FROM addhrm_company_master ");

/* <td width="4%" align="left" valign="bottom" class="display-border-1"><strong>No</strong></td>*/
$view .='<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"> '.$cmpname[0]['company_name'].'</td>
    <td width="34" align="left" valign="bottom"><strong>Place</strong></td>
    <td width="187" align="left" valign="bottom"  class="display-border-1"> &nbsp;&nbsp;'.$branchname.'</td>
	<td width="34" align="left" valign="bottom"><strong>Month</strong></td>
    <td width="187" align="left" valign="bottom"  class="display-border-1"> &nbsp;&nbsp;'.$monthname.'</td>
	<td width="34" align="left" valign="bottom"><strong>Year</strong></td>
    <td width="187" align="left" valign="bottom"  class="display-border-1"> &nbsp;&nbsp;'.$year.'</td>
  </tr>
</table>  <br/>  <br/>
</div><!-- /attendance-cell-1 -->
</div>
</div><!-- /content-wrap --> 

<div class="display-cell-1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  
  <tr>
  
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Sl.No</strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Name</strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Father/Husband Name</strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Sex</strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Doj</strong></td>
        <td width="12%" align="left" valign="bottom" class="display-border-1"><strong>Nature of Work</strong></td>
        
        ';
		
            $numdays=cal_days_in_month(CAL_GREGORIAN,$month,$year);
			for($k=1;$k<=$numdays;$k++)
			{
            	if($k < 10)
				{
					$k= '0'.$k;
				}
               $view.='<td align="center" valign="middle" style="font-size:12px;border:solid 1px #999;"><strong>'.$k.'</strong></td>';
               } 
              
          
      
	    $i=1;
		$cond="where employee.promoters!='1' and  employee.e_id NOT IN (1)"  ;
		if($branch_id == '')
		  {
		  
		 	$cond.=" and(( month(STR_TO_DATE(employee.resigned_date, '%d-%m-%Y')) >='".$month."' AND year(STR_TO_DATE(employee.resigned_date, '%d-%m-%Y')) >= '".$year."') OR employee.resigned_date='')";
			$sql = mysql_query("select * from employee join designation on employee.e_post = designation.desig_id   LEFT JOIN branches  ON employee.branchid = branches.branch_id ".$cond." order by e_fname ASC");
		  }
		  else
		  {
		  	$cond.=" and employee.branchid='$branch_id'  and(( month(STR_TO_DATE(employee.resigned_date, '%d-%m-%Y')) >='".$month."' AND year(STR_TO_DATE(employee.resigned_date, '%d-%m-%Y')) >= '".$year."') OR employee.resigned_date='')";
		  
		
		  $sql = mysql_query("select * from employee join designation on employee.e_post = designation.desig_id   LEFT JOIN branches  ON employee.branchid = branches.branch_id ".$cond." order by e_fname ASC");
		  }
		 
		 $emp_cnt=1;
		   $s=0;
		while($rw = mysql_fetch_array($sql))
		{
			$s++;
		
			if($rw['e_gender'] == 'Female')
			{
				$gender = 'F';
			}
			if($rw['e_gender'] == 'Male')
			{
				$gender = 'M';
			}
  		/* <td align="left" valign="top" class="display-border-1">'.$i.'</td> */
     	$view.='<tr>
        <td align="left" valign="top" class="display-border-1">'.$s.'</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">'.$rw['e_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>
        
        ';
		
		$userid = $rw['e_id'];
		$dateno = array();
		
		$attsql = $db->fetchQuery("select distinct(e_date) as edate,e_in,total_worked_hours from time where e_id = '$userid' and  MONTH(e_date) = '$month' and YEAR(e_date) = '$year' GROUP BY e_date ORDER BY edate ASC limit 0,$numdays");
		
		$status;
		$j=1;
	$firstsunday = firstSunday('01'.'-'.$month.'-'.$year);
   
    $secondsunday=$firstsunday+7;
  	$thirdsunday=$secondsunday+7;
	$fourthsunday=$thirdsunday+7;
	if(($fourthsunday+7)<=$numdays)
	$fivthsunday=$fourthsunday+7;
$cntpresent=0;	
$cntleave=0;	
foreach(range(1,$numdays)as $c){
	$flag=0;	
	
	for($num=0;$num<$noofholidays;$num++)
 {
 
 	$day=date('d',strtotime($holidays[$num]));
	if($day==$c)
	{
	
	$flag=1;
	break;
	}
 }
 
  $datecheck = date('d',strtotime($attsql[$j-1]['edate']));

 if($c!=$firstsunday && $c!=$secondsunday && $c!=$thirdsunday && $c!=$fourthsunday && $c!=$fivthsunday && ($flag==0 || $datecheck==$c))
 {
				if($attsql==NULL){
				$day=0;
				}else{
				$date = date('d',strtotime($attsql[$j-1]['edate']));
				 $day = date('j',strtotime($attsql[$j-1]['edate']));
				}
				if($day == $c){
					$dts = 	'P';
				}else{
					$dts = 	'A';
					$j--;
				}
				
			
		if($dts=='P')
		{

			$rtqry=$db->fetchQuery("select * from report_time  where brch_id = '$branch_id'  ");

			$reportime=$rtqry[0]['late_time'];
			$reportfullday=$rtqry[0]['full_day'];
 			$reporthalfday=$rtqry[0]['half_day'];
			//echo "<pre>";
			//print_r($attsql);
			$tot_time= $attsql[$j-1]['total_worked_hours'];
	$checksatdate = $year.'-'.$month.'-'.$c;
			
			if($attsql[$j-1]['e_in'] <= $reportime)
			{
				if(($tot_time <= '0'.$reportfullday) && ($tot_time >= '0'.$reporthalfday))
				{
				if($brch_half==0 && date('w', strtotime($checksatdate)) == 6)
				{
				$dts1 = 'R';
				$colr = '#B44781';
				}
				else
				{
				$dts1 = 'P';
				$colr = '#009900';
				}}
				else
				{
					$dts1 = 'P';
					$colr = '#009900';
				}
				
			}
			elseif($attsql[$j-1]['e_in'] > $reportime)
			{
				if(($tot_time <= '0'.$reportfullday)  && ($tot_time >= '0'.$reporthalfday))
				{
					$dts1 = 'D';
					$colr = '#0a8cc1';
				}
				else
				{
					$dts1 = 'D';
					$colr = '#0a8cc1';
				}
				
			}
		        $view.='<td width="" align="center" valign="middle" style="border:solid 1px #999; font-weight:bold; font-size:13px;color:'.$colr.'" >'.$dts1.'</td>';
				$cntpresent++; 
		}
		if($dts=='A')
		{
		
				$dtsnw = 'A';
				//echo $c;
				if($c < 10)
				{
					$days_1= '0'.$c;
				}
				else
				{
					$days_1= $c;
				}
				$chkdate =  $year.'-'.$month.'-'.$days_1;
			$presentdate = date('Y-m-d');	
			$resut =$db->fetchQuery("select * from payroll_user_leave_requests where employee_id='$userid' and day_from='$chkdate' and imd_status = '1' and is_cancelled = '0'");
				if(!empty($resut))
				{
					$dtsnw = 'L';
					$colrS = '#FF6666';
				}
				else
				{
					//$dtsnw = 'A';
					//$colrS = '#000';
					
					$chkdate=strtotime($chkdate);
					
					$presentdate=strtotime($presentdate);
					
					if($chkdate<=$presentdate)
					{
						$dtsnw = 'A';
					}else{
					 	$dtsnw = '-';
						//$dtsnw = 'A';
					} 
					$colrS = '#000';
					
					
				}
	            $view.='<td width="" align="center" valign="middle" style="font-size:12px;border:solid 1px #999;color:'.$colrS.';">'.$dtsnw.'</td>';
				$cntleave++;
		}
          
 }
 else
 {
 
				if($attsql==NULL){
				$day=0;
				}else{
				$date = date('d',strtotime($attsql[$j-1]['edate']));
				 $day = date('j',strtotime($attsql[$j-1]['edate']));
				}
				if($day == $c){
					$dts = 	'P';
				}else{
		
					$dts = 	'A';
					$j--;
				}
		
		if($dts=='P')
		{
		        $view.='<td width="" align="center" valign="middle" style="font-size:12px;border:solid 1px #999;color:#FF0000" >H</td>';
		}
		if($dts=='A')
		{
	            $view.='<td width="" align="center" valign="middle" style="font-size:12px;border:solid 1px #999;color:#FF0000" >H</td>';	
		}
        
 }     
	  $j++;
	    }
	
       
	   $i++;
	   } 
	   /*  <td align="left" valign="top" class="display-border-1"></td> */
	  $view.='<tr>';
		$num_cnt=1;
	
		
		$view.='
		
				
       </tr>';
      
    $view.='</table></td>
  </tr>
 
</table>




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


</div>';


$_SESSION['download_musteroll_view']=$view;
echo $view;
?>
						
                    </form>
                    
                </div>



</div>



<!-- /report-tab -->

<div class="clear-float"></div>
</div><!-- /content -->

<div class="footer">
<strong>? add-HRM</strong><br />
<span>Developed by <a href="http://www.aadhocc.com" target="_blank">Aadhocc</a></span></div><!-- /footer -->

</div>
					
					
                    </div><!-- /.row (main row) -->

                </section><!-- /.content -->
            </aside><!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->


        <!-- jQuery 2.0.2 -->
              <?php 
	include("footer-js.php");
?>
	
		<!--------------scripts for employee list-------------->

<?php /*?><script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script><?php */?>


<script src="js/common.js"></script>
<script src="js/employee.js"></script>
<script language="javascript" src="js/css3-mediaqueries.js" type="text/javascript"></script>
<script src="js/employee_sg.js"></script>
	<!--------------scripts for employee list-------------->
    </body>
</html>