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

								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>

<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 name="empid[]" class="empid chosen" style="text-transform:uppercase;"><option value="">Select Employee</option><?php $cate=mysql_query("select * from employee where status='' order by ID asc"); while($cate1=mysql_fetch_array($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 type="text" name="empname[]" class="emp" style="width:100px;" readonly="readonly" placeholder="Emp #" /></td>';

        htmlString += '<td id="page" style="padding-top:7px;"><select name="timein[]" class="timein chosen1"><option value="">Select Time</option><?php $tmv=mysql_query("select * from time_vl order by ID asc"); while($tmv1=mysql_fetch_array($tmv)) { echo'<option value="'.$tmv1['time'].'">'.$tmv1['time'].'</option>'; } ?></select></td>';

		htmlString += '<td id="page" style="padding-top:7px;"><select name="timeout[]" class="timeout chosen1"><option value="">Select Time</option><?php $tmv=mysql_query("select * from time_vl order by ID asc"); while($tmv1=mysql_fetch_array($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: "getotattendance.php",

data: dataString,

cache: false,

success: function( resp )

{



//alert(resp);



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

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



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

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



if(war1!='')

{

alert(war1);

}



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



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



}

});

});





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

	  

	  var total=0;

	  

	    for(var j = 0; j < len; j++)

		  {

		  

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

		   

		   if(eid==empid)

		   {		   

		   total = total+1;

		   }

		  

		  }

		  

		  var total=total*1;

		  		  

		  

	 if(total>1)	  

	 {

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

	 valid = false;

	 return false;

	 }		  

		

	  });*/

	  

	  

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

	  

	  //alert(len);  

	  

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

	  

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

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

	  

	  var total=0;

	  

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

		  {

		  		  

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

		   

		   if(eid==emid)

		   {

		   //alert(eid);		   

		   total = total+1;

		   }

		   

		  

		  });

		  

		  total=total*1;

		  

		  //var cbal=row.find('.war5').val()*1;

		  

		  //alert(cbal)

		  

	 if(total>1)	  

	 {

	 alert('same emp name');

	 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();

	  

	  if(empid=='')

	  {

	  alert('select employee');

	  valid = false;

	  }

	  else if(timein=='')

	  {

	  alert('select Time');

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

		$empid=$_POST['empid'];

		$entryno=$_POST['entryno'];

		$timein=$_POST['timein'];

		$timeout=$_POST['timeout'];

		

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

		

		$message='';

		if(is_array_empty($empid))$message.=" Employee,";

		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 =otattendance.php'>";



		}

		

		else

		{

		

		$i=0;

		$total=0;

			foreach($empid as $eid)

			{

				$tin=$timein[$i];

				$tout=$timeout[$i];

				

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

				$sel1=mysql_fetch_array($sel);



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

				{

					

					//timein

					$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

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

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

					if($tlsc=='PM')

					{

					

					if($tlsac!=12)

					{

						$tatw=$tlsac+12;

					}

					else

					{

					$tatw=$tlsac;

					}

					

					$toutt=str_replace($tlsac, $tatw, $tout);

					

					}					

					else

					{

						

						$toutt=$tout;

					

					}

					

					

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

					mysql_query($insert) or die(mysql_error());

                 

				}



				$i++;

			}

			

			if($insert)

			{

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

			}

			

			

		}

		

	}

	else

	{

	

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

		}

	}

	

	    $newnumber=mysql_query("SELECT MAX(entryno) as newid from ot_attendance") or die("Unable to get new inovice number.");

		$newnumber1=mysql_fetch_array($newnumber);

		$newnumber2=$newnumber1['newid']+1;



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



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

<tr><td id="page">Entry No. :</td><td id="page"><input type="text" name="entryno" value="'.$newnumber2.'" readonly="readonly"/></td>

<td id="page"><label>Date <span class="red">*</span> :</label></td> <td id="page"><input type="text" id="required" class="datepicker" title="Select Date" value="'.date('d-m-Y').'" name="date" size="30" autocomplete="off" /></td></tr>

</table>



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



<br />';

		?>

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

<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />



<script>

function suggest(inputString,id){

		selectedId = id;

		

	//	$('#productlist').addClass('load');

		$('#' + id).autocomplete("autosuggest.php", {

		width: 260,

		matchContains: true,

		//mustMatch: true,

		//minChars: 0,

		//multiple: true,

		//highlight: false,

		//multipleSeparator: ",",

		selectFirst: false

		//$('#' + id).removeClass('load');

			});

		}



</script>



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

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

    <th width="10%"></th>

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

	<th width="20%" 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>

    

	<tr class="item-row">

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

		<td class="item-name" id="page" style="padding-top:11px;"><select name="empid[]" class="empid chosen" style="text-transform:uppercase;"><option value="">Select Employee</option>

        <?php

		$cate=mysql_query("select * from employee order by ID asc");

		while($cate1=mysql_fetch_array($cate))

		{

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

		}

		?>        

        </select>

        <input type="hidden" name="rno[]" class="rno" value="1" />

        </td>

		<td id="page" style="padding-top:11px;" align="center"><input type="text" name="empname[]" class="emp" style="width:100px;" readonly="readonly" placeholder="Emp #" /></td>

		<td id="page" style="padding-top:11px;"><select name="timein[]" class="timein chosen1"><option value="">Select Time</option>

		

		<?php

		

		$tmv=mysql_query("select * from time_vl order by ID asc");

		while($tmv1=mysql_fetch_array($tmv))

		{

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

		}

		

		?>

		

		</select></td>

		<td id="page" style="padding-top:11px;"><select name="timeout[]" class="timeout chosen1"><option value="">Select Time</option>

		

		<?php

		

		$tmv=mysql_query("select * from time_vl order by ID asc");

		while($tmv1=mysql_fetch_array($tmv))

		{

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

		}

		

		?>

		

		</select></td></tr>



	<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="2" class="blank"></td>

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

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

		<div class="submit">

		<button 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 -->





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}

ob_flush();

?>