<?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">Overtime 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>

								Overtime 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/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
  <script>
    $(function() {
      // Initialize the datepicker
      $("#datepicker").datepicker({
        format: "mm/dd/yyyy",
        autoclose: true,
        todayHighlight: true
      });

      // Adjust the positioning of the datepicker dropdown
      $("#datepicker").on("show", function(e) {
        var inputOffset = $(this).offset();
        var inputHeight = $(this).outerHeight();
        var datepickerDropdown = $(".datepicker-dropdown");

        datepickerDropdown.css({
          top: inputOffset.top + inputHeight + 10,
          left: inputOffset.left
        });
      });
    });
  </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(){

		

		//$('.select2').select2('destroy');

		

		$('.chosen').css('width','220px').select2('destroy');

		$('.chosen1').css('width','150px').select2('destroy');

		

    	maxRowNum++;

        var htmlString = "";

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

        htmlString += '<td id="page" style="padding-top:7px;" align="center"><a class="delete" href="javascript:;" title="Remove row">X</a></td>';

        

		htmlString += '<td id="page" style="padding-top:7px;" class="description"><select style="width: 30ch; margin-bottom:10px;height:34px;border-radius: 5px;border: 1px solid #c0c0c0;background-color: rgb(240, 240, 240);" name="empid[]" class="empid " style="text-transform:uppercase;"><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" style="padding-top:7px;" align="center"><input style="width: 30ch; margin-bottom:10px;height:34px;border-radius: 5px;border: 1px solid #c0c0c0;background-color: rgb(240, 240, 240);" type="text" name="empname[]" class="emp" style="width:100px;" readonly="readonly" placeholder="Emp #" /></td>';

		

        htmlString += '<td id="page" style="padding-top:7px;"><select  style="width:15ch; margin-bottom:10px;margin-left:30px;margin-top:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="timein[]" class="timein "><option value="">Select Time</option><?php $tmv=mysqli_query($conn,"select * from time_vl order by ID asc"); while($tmv1=mysqli_fetch_assoc($tmv)) { echo'<option value="'.$tmv1['time'].'">'.$tmv1['time'].'</option>'; } ?></select></td>';

		

		htmlString += '<td id="page" style="padding-top:7px;"><select style="width:15ch; margin-bottom:10px;margin-left:20px;margin-top:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="timeout[]" class="timeout "><option value="">Select Time</option><?php $tmv=mysqli_query($conn,"select * from time_vl order by ID asc"); while($tmv1=mysqli_fetch_assoc($tmv)) { echo'<option value="'.$tmv1['time'].'">'.$tmv1['time'].'</option>'; } ?></select></td>';

		

        htmlString += '</tr>';



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

	

	 $('.chosen').css('width','220px').select2();

	 $('.chosen1').css('width','150px').select2();

	

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

    bind();

	

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

  

  //alert("ds");

  

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

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

  

  row.find('.emp').val(empid);

});

	

  });



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

  

  //alert("ds");

  

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

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

  

  row.find('.emp').val(empid);

});

  

  bind(); 

  

    

});



$(".delete").live('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;

		

		$('button:submit').prop('disabled', true);

		

	});

	

});	



</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'];

		$entryno=$_POST['entryno'];

		$employee=$_POST['employee'];

		$timein=$_POST['timein'];

		$timeout=$_POST['timeout'];

		$row=$_POST['row'];

		$rno=$_POST['rno'];

		$check=$_POST['check'];

		

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

		

		$message='';

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



		if(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 =viewotattendance.php'>";



		}

		

		else

		{

			

			$array=array();

			

			foreach($rno as $rnn)

			{				

			$dbrn=$row[$rnn];

				

			array_push($array,$dbrn);

			}

			

			

				$gv=mysqli_query($conn,"select * from ot_attendance where entryno='$entryno' order by ID asc");

				while($gv1=mysqli_fetch_assoc($gv))

				{

					

					if(in_array($gv1['ID'], $array)) {



					//echo"Found";



					}

					

					else

					{



					$del=mysqli_query($conn,"delete from ot_attendance where ID='$gv1[ID]'");



					}

					

				}

		

			foreach($rno as $rn)

			{

				$eid=$employee[$rn];

				$tin=$timein[$rn];

				$tout=$timeout[$rn];

				$dbrno=$row[$rn];

				

				//echo"$eid";

				

				if($tin)

				{

					

					

					$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;

					}

					

				}

					

				if($tout)

				{

					

					//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;

					}

					

				}

					

					

					if($dbrno!='')

					{

					$insert="update ot_attendance set date='$date',timein='$tinn',timeout='$toutt' where ID='$dbrno'";

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

					}

					else

					{

					

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

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

					

					}

            

			}

			

			if($insert)

			{

			header("location:viewotattendance.php?s=4");

			}



		}	

		

	}

	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 ot_attendance where entryno='$id'") or die("Unable to get new inovice number.");

		$chk1=mysqli_fetch_assoc($chk);



		echo'<form id="invoice" action="editotattendance1.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; " 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; " type="text" id="datepicker" class="datepicker" title="Select Date" value="'.date('d-m-Y',strtotime($chk1['date'])).'" name="date" size="30" autocomplete="off" /required></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="7%"></th>

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

	<th width="15%" 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 ot_attendance where entryno='$id' order by ID asc") or die("Unable to get purchase details.");

$tmpid=1;

$rno=0;$sn=0;

while($details1=mysqli_fetch_assoc($pur_details))

{



$rno=$rno+1;

$sn=$sn+1;



$det=mysqli_query($conn,"SELECT * from ot_attendance where empid='$details1[empid]' and entryno='$id' order by ID ASC");

$detn=mysqli_num_rows($det);

$det1=mysqli_fetch_assoc($det);



if($detn>0)

{

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



if($tin<10)

{

	$pref='0';

}

else

{

	$pref='';

}



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





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



if($tout<10)

{

	$prefo='0';

}

else

{

	$prefo='';

}



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

}



$empn=mysqli_query($conn,"select * from employee where empid='$details1[empid]'");

$empn1=mysqli_fetch_assoc($empn);



$depn=mysqli_query($conn,"select * from department where ID='$empn1[department]'");

$depn1=mysqli_fetch_assoc($depn);



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

		

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

		

		<td style="padding-top:7px;"><input style="width: 30ch; margin-bottom:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;background-color: rgb(240, 240, 240);" type="text" readonly="true" class="emp" value="'.$empn1['name'].'">';		

		echo"	

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

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

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

		</td>";

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

		

		<td><select style="width:15ch; margin-bottom:10px;margin-left:30px;margin-top:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="timein['.$rno.']" class="" style="width:100px;"><option value="">-- Select --</option>';	

		

		if($detn>0)

		{

	

		if($det1['timein']!='00:00:00')

		{

	

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

		while($tmv1=mysqli_fetch_assoc($tmv))		

		{						

			$sop="<option value='".$tmv1['time']."'>".$tmv1['time']."</option>";			

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

			echo $sop;					

		}		

		

		}

		

		}

		else

		{

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

		while($tmv1=mysqli_fetch_assoc($tmv))

		{

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

		}

		}

		

		echo'</select></td>

		

		<td><select style="width: 15ch; margin-bottom:10px;margin-left:20px;margin-top:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="timeout['.$rno.']" class="" style="width:100px;"><option value="">-- Select --</option>';						

		

		if($detn>0)

		{

			

		if($det1['timeout']!='00:00:00')

		{

			

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

		while($tmv1=mysqli_fetch_assoc($tmv))		

		{						

			$sop="<option value='".$tmv1['time']."'>".$tmv1['time']."</option>";			

			$sop=str_replace("<option value='".$tmout."'","<option value='".$tmout."' selected",$sop);			

			echo $sop;					

		}

		

		}

		

		}

		else

		{

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

		while($tmv1=mysqli_fetch_assoc($tmv))		

		{			

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

		}

		}

		

		echo'</select></td>

		

	</tr>';

	

		  $tmpid++;

		  }

		  ?>

          <script>

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

		  </script>

		  

	<tr id="hiderow">

		<td colspan="2" id="page"><a id="addrow" href="javascript:;"

			title="Add a row">Add Row</a></td>

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

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

	</tr>

          

    <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 id="save"  class="btn btn btn-primary" 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();







?>