<?PHP

ob_start();

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysql_query("select * from admin where username='$user'");

$gtp1=mysql_fetch_array($gtp);



include 'top.php';

?>







<div class="main-content">

				<div class="main-content-inner">

					<div class="breadcrumbs ace-save-state" id="breadcrumbs">

						<ul class="breadcrumb">

							<li>

								<i class="ace-icon fa fa-home home-icon"></i>

								<a href="#">Home</a>

							</li>

							<li class="active">Attendance</li>

						</ul><!-- /.breadcrumb -->

					</div>



					<div class="page-content">

			



						<div class="page-header">

                        

<?php



$aca=mysql_query("select * from financial where user='$user' order by ID desc");

$aca1=mysql_fetch_array($aca);



$financial=$aca1['financial'];



?>                        

                        

							<table style="width:100%;">

                        

                        <tr><td style="width:50%;">

                        

							<h1>

								Attendance

							</h1>

                            

                            </td>

                            

                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">

                            

                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>

                            

                            </td></tr></table>

                            

						</div><!-- /.page-header -->

<br />



						<div class="row">

							<div class="col-xs-12">

                            

								<!-- PAGE CONTENT BEGINS -->

                                

<script type="text/javascript" src="assets/js/jquery.js"></script>   



<script>

	

jQuery(function($){

		   

	// simple jQuery validation script

	$('#report').submit(function(){

		

		var valid = true;

		var errormsg = 'This field is required!';

		var errorcn = 'error';

		

		$('.' + errorcn, this).remove();			

		

		$('#required', this).each(function(){

			var parent = $(this).parent();

			if( $(this).val() == '' ){

				var msg = $(this).attr('title');

				msg = (msg != '') ? msg : errormsg;

				$('<span style="font-size:11px;color:#ff0000;" class="'+ errorcn +'">'+ msg +'</span>')

					.appendTo(parent)

					.fadeIn('fast')

					.click(function(){ $(this).remove(); });

				valid = false;

			};

		});

		

		return valid;

	});

	

});	



</script>   

        

        <?php

		if(isset($_GET['submit']))

		{

		$month=$_GET['month'];

		$year=$_GET['year'];

		$empid=$_GET['employee'];

		

		if($month=='' || $year=='' || $empid=='')

		{

		echo"<center>Select Month and Year. <a href='report.php'>Back</a></center>";

		}

		

		else

		{

		

		$monthn=date('F', mktime(0, 0, 0, $month, 10));

		

		$emp=mysql_query("select * from employee where empid='$empid'");

		$emp1=mysql_fetch_array($emp);



if($month==01)

{

$lm=12;



$year1=$year-1;



}	

else

{

$lm=$month-1;



$year1=$year;

}



$ld=date('t',strtotime($month));



if($lm>9)

{

$date1 = $year1.'-'.$lm.'-01';

$date2 = $year.'-'.$month.'-'.$ld;	

}

else

{

$date1 = $year1.'-0'.$lm.'-01';

$date2 = $year.'-'.$month.'-'.$ld;

}

	



$nm=0;

function returnDays($fromdate, $todate) {

    $fromdate = DateTime::createFromFormat('d-m-Y', $fromdate);

    $todate = DateTime::createFromFormat('d-m-Y', $todate);

    return new DatePeriod(

        $fromdate,

        new DateInterval('P1D'),

        $todate->modify('+1 day')

    );

}



$date1 = date('d-m-Y',strtotime($date1));

$date2 = date('d-m-Y',strtotime($date2));



$datePeriod = returnDays($date1, $date2);

foreach($datePeriod as $date) {

    $tdate = $date->format('d-m-Y');

	

	$nm++;

		

}



$lastday=$nm;	

	



if($lm>9)

{

$date1 = $year1.'-'.$lm.'-01';

$date2 = $year.'-'.$month.'-'.$ld;	

}

else

{

$date1 = $year1.'-0'.$lm.'-27';

$date2 = $year.'-'.$month.'-'.$ld;	

}

	

	

$no = 0;

$start = new DateTime($date1);

$end   = new DateTime($date2);

$end   = $end->modify('+1 day');

/*$todate = $end -> format('Y-m-d');



echo"$todate";*/

$interval = DateInterval::createFromDateString('1 day');

$period = new DatePeriod($start, $interval, $end);

foreach ($period as $dt)

{

    if ($dt->format('N') == 7)

    {

        $no++;

    }

}

$sundays = $no;



$ph=mysql_query("select COUNT(ID) from holiday where date between '$date1' and '$date2'");

$ph1=mysql_result($ph,0);

	

		echo"<br /><center><p style='color:green; font-size:21px;'>Attendance Report for ".$monthn.", ".$year."</p></center><br /><br />";

		

		$balance=$lastday-$sundays-$ph1;

		

		echo"<table style='width:93%; margin:auto;'>

		<tr><td width='50%'>		

		<table style='margin-left:37px;'>

		<tr><td>Number of days: <b style='color:#FF7200;'> ".$lastday."</b></td></tr>

		<tr><td>Number of sundays: <b style='color:#FF0000;'>".$sundays."</b></td></tr>

		<tr><td>Public Holidays: <b style='color:#FF0000;'>".$ph1."</b></td></tr>

		<tr><td>Number of working days: <b style='color:green;'>".$balance."</b></td></tr>

		

		<tr><td>Period: <b style='color:red;'>".date('d-m-Y',strtotime($date1))."</b> to <b style='color:red;'>".date('d-m-Y',strtotime($date2))."</b></td></tr>

		

		</table>

		</td>

		

		<td>

		

		<table style='width:75%; border:1px solid #ccc;'>

		

		<tr><td style='padding-left:10px; width:30px;'><b style='color:green;'>H </b></td><td width='5%'>-</td><td> Sunday</td></tr>

		<tr><td style='padding-left:10px;'><b style='color:green;'>P </b></td><td width='5%'>-</td><td> Public Holiday</td></tr>

		<tr><td style='padding-left:10px;'><img src='assets/images/w.png'></td><td width='5%'>-</td><td> Absent</td></tr>

		<tr><td style='padding-left:10px;'><img src='assets/images/full.png'></td><td width='5%'>-</td><td> Full Day Present</td></tr>

		<tr><td style='padding-left:10px;'><img src='assets/images/tick.png'></td><td width='5%'>-</td><td> Half Day Present</td></tr>

				

		</table>

		

		</td>

		

		</tr></table>

		

		<br />";

        

		echo"<table border='1' style='width:93%;' align='center'><tr><th style='width:9%; padding:0 3px;'>Emp #</th><th style='width:17%; padding:0 3px;'>Name</th>";

		

		

function returnDates($fromdate, $todate) {

    $fromdate = DateTime::createFromFormat('d-m-Y', $fromdate);

    $todate = DateTime::createFromFormat('d-m-Y', $todate);

    return new DatePeriod(

        $fromdate,

        new DateInterval('P1D'),

        $todate->modify('+1 day')

    );

}



$date1 = date('d-m-Y',strtotime($date1));

$date2 = date('d-m-Y',strtotime($date2));



$datePeriod = returnDates($date1, $date2);

foreach($datePeriod as $date) {

    $tdate = $date->format('d-m-Y');

	

	echo"<th style='width:2%;'>".date("d",strtotime($tdate))."</th>";

	

}

		

		/*for($i=1; $i<=$lastday; $i++)

		{

		echo"<th style='width:2%;'>".$i."</th>";

		}

		*/

		

		echo"<th style='width:8%;'>Total</th></tr>";

		

		echo"<tr><td style='padding:0 3px;'>".$emp1['empid']."</td><td style='padding:0 3px;'>".$emp1['name']."</td>";

		

		$att=0;

		

		/*for($i=1; $i<=$lastday; $i++)

		{

		$nowdate=$year.'-'.$month.'-'.$i;

		*/

	

	function returnDate($fromdate, $todate) {

    $fromdate = DateTime::createFromFormat('d-m-Y', $fromdate);

    $todate = DateTime::createFromFormat('d-m-Y', $todate);

    return new DatePeriod(

        $fromdate,

        new DateInterval('P1D'),

        $todate->modify('+1 day')

    );

	}

	

	$date1 = date('d-m-Y',strtotime($date1));

	$date2 = date('d-m-Y',strtotime($date2));

	

	$datePeriod = returnDate($date1, $date2);

	foreach($datePeriod as $date) {

	

	$nowdate = $date->format('d-m-Y');

	

	$nowdate=date('Y-m-d',strtotime($nowdate));

	

		$present=mysql_query("select * from new_attendance where empid='".$empid."' and date='$nowdate'");

		$present1=mysql_fetch_array($present);

		

		$timein=strtotime($present1['timein']);

		$timeout=strtotime($present1['timeout']);

		

		$acct= round(abs($timein - $timeout) / 60,2);

		

		$acc=$acct/60;

		

		$ac = round($acc);

		

		$day=date("l",strtotime($nowdate));

		

		$p=mysql_query("select COUNT(ID) from holiday where date='$nowdate'");

		$p1=mysql_result($p,0);

		

		if($ac>=5)

		{

		echo"<td style='text-align:center;'><img src='assets/images/full.png'></td>";

		$attendance1=1;

		}

		elseif($ac>=2 && $ac<5)

		{

		echo"<td style='text-align:center;'><img src='assets/images/tick.png'></td>";

		$attendance1=0.5;

		}

		elseif($day=='Sunday')

		{

		echo"<td style='text-align:center; font-weight:bold; color:green;'>H</td>";

		$attendance1=0;

		}

		elseif($p1>0)

		{

		echo"<td style='text-align:center; font-weight:bold; color:green;'>P</td>";

		$attendance1=0;

		}

		elseif($ac<2 || $ac==0)

		{

		echo"<td style='text-align:center;'><img src='assets/images/w.png'></td>";

		$attendance1=0;

		}

				



		$att=$att+$attendance1;

		}

			

		$loss=$balance-$att;	

			

		echo"<td style='text-align:right;'>".$att."</td></tr></table><br /><br />

		

		

		<table style='margin-left:37px;'>

		<tr><td style='color:green;'>Days Present : ".$att."</td></tr>



		<tr><td style='color:red;'>Days Absent &nbsp;  : ".$loss."</td></tr>

		</table><br />";

		}

				

		echo"<br /><br /><center><a href='javascript:history.back();'>Back</a></center>";

		

		}

		

		else

		{

		?>

        

        <form action="attendancereport.php" method="get" id="report">

        <table width="75%" align="center">

        <tr><td>Month <span class="red">*</span></td><td width="2%">:</td><td> <select name="month" style="width:220px;" id="required" title="Select Month"><option value="">Select Month</option>

        <option value="01">January</option>

		<option value="02">February</option>

		<option value="03">March</option>

		<option value="04">April</option>

		<option value="05">May</option>

		<option value="06">June</option>

		<option value="07">July</option>

		<option value="08">August</option>

		<option value="09">September</option>

		<option value="10">October</option>

		<option value="11">November</option>

		<option value="12">December</option></select>

        </td></tr>

        <tr><td style="padding-top:7px;">Year <span class="red">*</span></td><td width="2%" style="padding-top:7px;">:</td><td style="padding-top:7px;"> <select name="year" style="width:220px;" id="required" title="Select Year"><option value="">Select Year</option>

        <option value="2014">2014</option><option value="2015">2015</option><option value="2016">2016</option><option value="2017">2017</option><option value="2018">2018</option>

		<option value="2019">2019</option><option value="2020">2020</option></select>

        </td></tr>

        

        <tr><td style="padding-top:7px;">Employee <span class="red">*</span></td><td width="2%" style="padding-top:7px;">:</td><td style="padding-top:7px;"> <select name="employee" style="width:220px;" id="required" title="Select Employee"><option value=""> Select Employee </option>

        

        <?php

		

		$ret=mysql_query("select * from employee where status='' order by ID asc");

		while($ret1=mysql_fetch_array($ret))

		{

		echo"<option value='".$ret1['empid']."'>".$ret1['name']."</option>";

		}

		

		?>

        

        </select>

        

        </td></tr>

        

        </table><br />

        

		<center>

        

        <button type="submit" name="submit" value="submit">Submit</button></center>

		

        </form>

        <?php

		}

		?>        



								<!-- PAGE CONTENT ENDS -->

							</div><!-- /.col -->

						</div><!-- /.row -->

					



</div><!-- /.page-content -->

				</div>

			</div><!-- /.main-content -->





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}

ob_flush();

?>