<?PHP

ob_start();

session_start();

include 'connect.php';

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

{

$user=$_SESSION['adminemail'];



$gtp=mysqli_query($conn,"select * from admin where username='$user'");

$gtp1=mysqli_fetch_assoc($gtp);



include 'top.php';

?>






<div id="page-wrapper" >
          <div id="page-inner">
<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=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");

$aca1=mysqli_fetch_assoc($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 src="assets/js/jquery.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css">
 <style>
    /* Custom CSS for Chosen select */
    .chosen-container {
      width: 25ch;
	  margin-left:30px;
    }

    .chosen-container-single .chosen-single {
      border-radius: 4px;
      border: 1px solid #f0f0f0; /* Change the border color here */
      height:34px;
	  margin-bottom:10px;
      background-color: #fff;
      background-image: none;
      box-shadow: 0 0 0 1px #c0c0c0;
    }

    .chosen-container-single .chosen-single span {
      padding: 5px;
      text-transform: uppercase;
    }

    .chosen-container-single .chosen-search input[type="text"] {
      border-radius: 4px;
      border: 1px solid #c0c0c0;
      height: 34px;
    }

    .chosen-container-active .chosen-single {
      border-color: #7f7f7f;
      box-shadow: 0 0 0 1px #7f7f7f;
    }

    .chosen-container-active .chosen-single:hover {
      border-color: #7f7f7f;
      box-shadow: 0 0 0 1px #7f7f7f;
    }

    .chosen-container-active .chosen-search input[type="text"] {
      border-color: #7f7f7f;
    }
  </style>

<script>
$(function() {
  // Initialize the Chosen select element
  $(".chosen-select").chosen({
    width: '15ch',
    disable_search_threshold: 10
  });
});
</script>
<script type="text/javascript">

	//global var

	var maxRowNum = 1;

	var selectedId = 0;

	

function print_today() {

 

  var now = new Date();

  var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

  var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

  function fourdigits(number) {

    return (number < 1000) ? number + 1900 : number;

  }

  var today =  months[now.getMonth()] + " " + date + ", " + (fourdigits(now.getYear()));

  return today;

}







function roundNumber(number,decimals) {

  var newString;// The new rounded number

  decimals = Number(decimals);

  if (decimals < 1) {

    newString = (Math.round(number)).toString();

  } else {

    var numString = number.toString();

    if (numString.lastIndexOf(".") == -1) {// If there is no decimal point

      numString += ".";// give it one at the end

    }

    var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number

    var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with

    var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want

    if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated

      if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point

        while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {

          if (d1 != ".") {

            cutoff -= 1;

            d1 = Number(numString.substring(cutoff,cutoff+1));

          } else {

            cutoff -= 1;

          }

        }

      }

      d1 += 1;

    } 

    if (d1 == 10) {

      numString = numString.substring(0, numString.lastIndexOf("."));

      var roundedNum = Number(numString) + 1;

      newString = roundedNum.toString() + '.';

    } else {

      newString = numString.substring(0,cutoff) + d1.toString();

    }

  }

  if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string

    newString += ".";

  }

  var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;

  for(var i=0;i<decimals-decs;i++) newString += "0";

  //var newNumber = Number(newString);// make it a number if you like

  return newString; // Output the result to the form field (change for your purposes)

}



function update_total() {

  var total = 0;

  $('.price').each(function(i){

    price = $(this).html().replace("Rs.","");

    if (!isNaN(price)) total += Number(price);

  });



  total = roundNumber(total,2);



  $('.amount').html("<img src='images/inr.png' />"+total);

  



}





function update_price() {

  var row = $(this).parents('.item-row');

  var price = row.find('.wages').val() * row.find('.now').val();

  //var vat = price * row.find('.vat').val()/100; 

  //price = price + vat;

  //vat = vat;

  //price = roundNumber(price,2);

  //alert(price);

  isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html(price);

  //isNaN(vat) ? roq.find('.vamt').html("N/A") : row.find('.vamt').html(vat);

  update_total();

}





function bind() {

  $(".wages").blur(update_price);

  $(".now").blur(update_price);  

}



$(document).ready(function() {



$(function() {

$('.datepicker').datepicker({dateFormat: 'dd-mm-yy'});

});



  $('input').click(function(){

    $(this).select();

  });



  

    $("#addrow").click(function(){

    	maxRowNum++;

        var htmlString = "";

        htmlString += '<tr class="item-row" align="center">';

        htmlString += '<td id="page"><a class="delete" href="javascript:;" title="Remove row">X</a></td>';

        

		htmlString += '<td id="page" class="description"><select name="empid[]" class="empid"><option value="">Select Employee</option><?php $cate=mysqli_query($conn,"select * from employee where status='' order by ID asc"); while($cate1=mysqli_fetch_assoc($cate)) { echo'<option value="'.$cate1['empid'].'">'.$cate1['name'].'</option>'; } ?></select> <input type="hidden" name="rno[]" value="'+ maxRowNum +'" class="rno"></td>';

		

        htmlString += '<td id="page"><input type="text" name="empname[]" class="emp" style="width:100px;" placeholder="Emp #" /></td>';

        htmlString += '<td id="page"><select name="timein[]" class="timein"><option value="">Select Time</option><option value="01:00:00">01:00 AM</option><option value="01:15:00">01:15 AM</option><option value="01:30:00">01:30 AM</option><option value="01:45:00">01:45 AM</option><option value="02:00:00">02:00 AM</option><option value="02:15:00">02:15 AM</option><option value="02:30:00">02:30 AM</option><option value="02:45:00">02:45 AM</option><option value="03:00:00">03:00 AM</option><option value="03:15:00">03:15 AM</option><option value="03:30:00">03:30 AM</option><option value="03:45:00">03:45 AM</option><option value="04:00:00">04:00 AM</option><option value="04:15:00">04:15 AM</option><option value="04:30:00">04:30 AM</option><option value="04:45:00">04:45 AM</option><option value="05:00:00">05:00 AM</option><option value="05:15:00">05:15 AM</option><option value="05:30:00">05:30 AM</option><option value="05:45:00">05:45 AM</option><option value="06:00:00">06:00 AM</option><option value="06:15:00">06:15 AM</option><option value="06:30:00">06:30 AM</option><option value="06:45:00">06:45 AM</option><option value="07:00:00">07:00 AM</option><option value="07:15:00">07:15 AM</option><option value="07:30:00">07:30 AM</option><option value="07:45:00">07:45 AM</option><option value="08:00:00">08:00 AM</option><option value="08:15:00">08:15 AM</option><option value="08:30:00">08:30 AM</option><option value="08:45:00">08:45 AM</option><option value="09:00:00">09:00 AM</option><option value="09:15:00">09:15 AM</option><option value="09:30:00">09:30 AM</option><option value="09:45:00">09:45 AM</option><option value="10:00:00">10:00 AM</option><option value="10:15:00">10:15 AM</option><option value="10:30:00">10:30 AM</option><option value="10:45:00">10:45 AM</option><option value="11:00:00">11:00 AM</option><option value="11:15:00">11:15 AM</option><option value="11:30:00">11:30 AM</option><option value="11:45:00">11:45 AM</option><option value="12:00:00">12:00 PM</option><option value="12:15:00">12:15 PM</option><option value="12:30:00">12:30 PM</option><option value="12:45:00">12:45 PM</option><option value="13:00:00">01:00 PM</option><option value="13:15:00">01:15 PM</option><option value="13:30:00">01:30 PM</option><option value="13:45:00">01:45 PM</option><option value="14:00:00">02:00 PM</option><option value="14:15:00">02:15 PM</option><option value="14:30:00">02:30 PM</option><option value="14:45:00">02:45 PM</option><option value="15:00:00">03:00 PM</option><option value="15:15:00">03:15 PM</option><option value="15:30:00">03:30 PM</option><option value="15:45:00">03:45 PM</option><option value="16:00:00">04:00 PM</option><option value="16:15:00">04:15 PM</option><option value="16:30:00">04:30 PM</option><option value="16:45:00">04:45 PM</option><option value="17:00:00">05:00 PM</option><option value="17:15:00">05:15 PM</option><option value="17:30:00">05:30 PM</option><option value="17:45:00">05:45 PM</option><option value="18:00:00">06:00 PM</option><option value="18:15:00">06:15 PM</option><option value="18:30:00">06:30 PM</option><option value="18:45:00">06:45 PM</option><option value="19:00:00">07:00 PM</option><option value="19:15:00">07:15 PM</option><option value="19:30:00">07:30 PM</option><option value="19:45:00">07:45 PM</option><option value="20:00:00">08:00 PM</option><option value="20:15:00">08:15 PM</option><option value="20:30:00">08:30 PM</option><option value="20:45:00">08:45 PM</option><option value="21:00:00">09:00 PM</option><option value="21:15:00">09:15 PM</option><option value="21:30:00">09:30 PM</option><option value="21:45:00">09:45 PM</option><option value="22:00:00">10:00 PM</option><option value="22:15:00">10:15 PM</option><option value="22:30:00">10:30 PM</option><option value="22:45:00">10:45 PM</option><option value="23:00:00">11:00 PM</option><option value="23:15:00">11:15 PM</option><option value="23:30:00">11:30 PM</option><option value="23:45:00">11:45 PM</option><option value="24:00:00">12:00 AM</option></select></td>';

        htmlString += '</tr>';



     $(".item-row:last").after(htmlString);

	

    if ($(".delete").length > 0) $(".delete").show();

    bind();

	

	$(".empid").change(function(){

  

  var row = $(this).parents('.item-row');

  var empid=row.find('.empid').val();

  

  $('.emp').val(empid);

});



	

  });



  

  

  bind();

    

	$(".empid").change(function(){

  

  var row = $(this).parents('.item-row');

  var empid=row.find('.empid').val();

  

  $('.emp').val(empid);

});





  $(".delete").click(function(){

    $(this).parents('.item-row').remove();

    update_total();

    if ($(".delete").length < 2) $(".delete").hide();

  });

      

});





$(".empid").live('change', function(){

  

  var row = $(this).parents('.item-row');

  var date=$('.datepicker').val();

  var empid=row.find('.empid').val();

 

 var dataString = 'empid='+ empid +'&date='+ date;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getattendance.php",

data: dataString,

cache: false,

success: function( resp )

{



//alert(resp);



var war=$(resp).filter("#war").text();



row.find('.warning').val(war);



var war='<span style="color:red;">'+ war +'</span>';



$('.war').html(war);



}

});

});





  $(".timeout").live('change',function(i){

  

  var date=$('.datepicker').val();

 

 var dataString = 'date='+ date;

 //alert(dataString);

    $.ajax

({

type: "GET",

url: "getattendance1.php",

data: dataString,

cache: false,

success: function( resp )

{



//alert(resp);



var war1=$(resp).filter("#war1").text();



$('.warning1').val(war1);



if(war1!='')

{

alert(war1);

}



}

});

});



</script>

<script>

	

jQuery(function($){

		   

	// simple jQuery validation script

	$('#invoice').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;

			};

		});

		

		

		var len=$('.item-row').length;

		

	  $('.rno').each(function(i){

	  

	  var row = $(this).parents('.item-row');

	  

	  var empid=row.find('.empid').val();

	  

	  //alert(empid);

	  

	  var total=0;

	  

	    $('.empid').each(function(i)

		  {

		  		  

		   var eid=$(this).val();

		   

		   //alert(eid);

		   

		   if(eid==empid)

		   {		   

		   total = total+1;

		   }

		  

		  });

		  

		  //alert(total);

		  

		  var total=total*1;

		  		  

		  

	 if(total>1)	  

	 {

	 alert('Same employee name selected in two rows.');

	 valid = false;

	 return false;

	 }		  

		

	  });

	  

	  

	  //all rows empty

	  

	  $('.rno').each(function(i){

	  

	  var row = $(this).parents('.item-row');

	  

	  var empid=row.find('.empid').val();

	  var timein=row.find('.timein').val();

	  var timeout=row.find('.timeout').val();

	  

	  if(empid=='')

	  {

	  alert('select employee');

	  valid = false;

	  }

	  else if(timein=='')

	  {

	  alert('select Time In');

	  valid = false;

	  }

	  else if(timeout=='')

	  {

	  alert('select Time Out');

	  valid = false;

	  }



	  

	  

	  });

		

		

		

		$('.warning').each(function(i){

	  

	  var row = $(this).parents('.item-row');

	  

	  var w = row.find('.warning').val();

	  

	  //alert(w);

	 

	 if(w!='')

		{

		alert(w);

		

		valid = false;

		

		return false;

		}

	 	  

	  });

	  

	  

var warn = $('.warning1').val();

	  

	  if(warn!='')

	  {

	  alert(warn);

	  

	  valid = false;

	  

	  return false;

	  }

	  

		

		

		return valid;

	});

	

});	



</script>



	<?php

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

	{

		function is_array_empty($InputVariable)

		{

			$Result = true;



			if (is_array($InputVariable) && count($InputVariable) > 0)

			{

				foreach ($InputVariable as $Value)

				{

					$Result = $Result && is_array_empty($Value);

				}

			}

			else

			{

				$Result = empty($InputVariable);

			}



			return $Result;

		}

		$date=$_POST['date'];

		$empid=$_POST['empid'];

		$empname=$_POST['empname'];

		$entryno=$_POST['entryno'];

		$timein=$_POST['timein'];

		$timeout=$_POST['timeout'];

		

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

		

		$message='';

		if(is_array_empty($timeout))$message.=" Select Timeout,";

		if(is_array_empty($timein))$message.=" Select Time in,";



		if(is_array_empty($empid) || is_array_empty($timein))

		{



			$alert="Please provide".$message;

			$alert=substr($alert,0,-1);

			echo "<script type='text/javascript'>alert('".$alert."');</script><META HTTP-EQUIV = 'Refresh' Content = '1; URL =viewattendance.php'>";



		}

		

		else

		{

		

		$del=mysqli_query($conn,"delete from attendance where entryno='$entryno'");

		

		$i=0;

		$total=0;

			foreach($empid as $eid)

			{

				$tin=$timein[$i];

				$tout=$timeout[$i];

				$ename=$empname[$i];

				

				if(!empty($eid) &&  !empty($tin) && $tout)

				{

					

					

					$lsc=substr($tin, -2);

					

					$lsac=substr($tin, 0, 2);

					

					if($lsc=='PM')

					{

					

					if($lsac!=12)

					{

					$atw=$lsac+12;

					}

					else

					{

					$atw=$lsac;

					}

					

					$tinn=str_replace($lsac, $atw, $tin);

					

					}

					else

					{

					$tinn=$tin;

					}

					

					

					//timeout					

					

					$tsc=substr($tout, -2);

					

					$tsac=substr($tout, 0, 2);

					

					if($tsc=='PM')

					{



					if($tsac!=12)

					{

					$btw=$tsac+12;

					}

					else

					{

					$btw=$tsac;

					}

					

					$toutt=str_replace($tsac, $btw, $tout);

					

					}

					else

					{

						$toutt=$tout;

					}

					

					

					$insert="INSERT into attendance(date,empid,name,timein,timeout,entryno) values('$date','$eid','$ename','$tinn','$toutt','$entryno')";

					mysqli_query($conn,$insert) or die(mysqli_error($conn));

                 

				 

				}



				$i++;

				

			}

			

			if($insert)

			{

			header("location:viewattendance.php?s=2");

			}			

			

		}

		

	}

	else

	{

	

	$id=$_GET['id'];

	

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

	{

	 $status=$_GET['s'];

		 if($status=='1')

		 {

           echo'<div class="alert alert-success alert-dismissable">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

                <span style="text-align:left;">Atendance Added Successfully.</span>

                </div>';

		}

	}

	

	    $chk=mysqli_query($conn,"SELECT * from attendance where entryno='$id'") or die("Unable to get new inovice number.");

		$chk1=mysqli_fetch_assoc($chk);

		

echo'<form id="invoice" action="editattendance.php" method="post"><center>



<table width="85%" style="border:none;">

<tr><td id="page">Entry No. </td><td id="page"><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="entryno" value="'.$id.'" readonly="readonly"/></td>

<td id="page"><label>Date <span class="red"></span> </label></td> <td id="page"><input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" id="required" class="datepicker" title="Select Date" value="'.date('d-m-Y',strtotime($chk1['date'])).'" name="date" size="30" autocomplete="off" /></td></tr>

</table>



<input type="hidden" name="warning1" class="warning1" />



<br />';

		?>



<table id="items" width="85%" style="border:1px solid #ddd;">



	<tr bgcolor="#EAF5F8" height="25">	

	<th width="5%" style="text-align:center;"></th>

	<th width="30%" style="text-align:center;">Employee</th>

	<th width="25%" style="text-align:center;">Emp #</th>

	<th width="20%" style="text-align:center;">Time In</th>

    <th width="20%" style="text-align:center;">Time Out</th></tr>

    

	<?php



 $pur_details=mysqli_query($conn,"SELECT * from attendance where entryno='$id' order by ID ASC") or die("Unable to get purchase details.");

$tmpid=0;

$rno=0;

while($details1=mysqli_fetch_assoc($pur_details))

{





$tin=date('g',strtotime($details1['timein']));



if($tin<10)

{

	$pref='0';

}

else

{

	$pref='';

}



$tmin=$pref.date('g:i A',strtotime($details1['timein']));





$tout=date('g',strtotime($details1['timeout']));



if($tout<10)

{

	$prefo='0';

}

else

{

	$prefo='';

}



$tmout=$prefo.date('g:i A',strtotime($details1['timeout']));





$rno=$rno+1;

    

		echo'<tr class="item-row">

		

		<td id="page" height="50" align="center"><a class="delete" href="javascript:;" title="Remove row">X</a></td>

		

		<td class="item-name" id="page" align="center"><input style="width: 25ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;background-color: rgb(240, 240, 240);"  type="text" readonly="true" class="emp" name="empname[]" value="'.$details1['name'].'">';

		echo"

		

		<input type='hidden' name='rno[]' class='rno' value='".$rno."' />

		

		<input type='hidden' name='row[]' class='row' value='".$details1['ID']."' />

		

		</td>";		

		

		echo'<td id="page" align="center"><input style="width: 25ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;background-color: rgb(240, 240, 240);" type="text" name="empid[]" readonly="true" class="empid" style="width:100px;" value="'.$details1['empid'].'" /></td>

		

		<td id="page"><select style="width: 10ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;" name="timein[]" class="timein chosen-select" style="width:150px;"><option value="">Select Time</option>';

		

		$tm=mysqli_query($conn,"select * from time_vl order by ID asc");

		while($tm1=mysqli_fetch_assoc($tm))

		{

		$in="<option value='".$tm1['time']."'>".$tm1['time']."</option>";

		$in=str_replace("<option value='".$tmin."'","<option value='".$tmin."' selected",$in);

		echo $in;

		}

		

		echo'</td>

		

		<td id="page"><select style="width: 10ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;" name="timeout[]" class="timeout chosen-select" style="width:150px;"><option value="">Select Time</option>';

		

		$tn=mysqli_query($conn,"select * from time_vl order by ID asc");

		while($tn1=mysqli_fetch_assoc($tn))

		{

		echo"<option value='".$tn1['time']."'>".$tn1['time']."</option>";

		}

		

		echo'</td>

		

	</tr>';

	

	?>

	

          <script>

		  document.getElementById("productlist_"+"<?PHP echo $tmpid; ?>").value = "<?PHP echo $details1['empid']; ?>";

		  </script>

          <?PHP

		  $tmpid++;

		  }

		  ?>

		  

          <script>

		  maxRowNum ="<?PHP echo ($tmpid-1); ?>";

		  </script>

          

    <tr>

		<td id="page" colspan="3" class="blank"></td>

	    <td class="blank"  id="page"></td>

		<td id="page" style="text-align:center;">

		<div class="submit">

		<button class="btn btn-sm btn-primary" id="save" type="submit" name="submit" value="Submit">Save</button>

		</div>

		</td>

	</tr>

</table>

</center>

</form>

		<?php

	}

	

?>

								<!-- PAGE CONTENT ENDS -->

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

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

					



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

				</div>

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





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}

ob_flush();

?>