<?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">Payslip</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>

								Payslip

							</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 type='text/javascript'>

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_adjbalance() {

  

  //var row = $(this).parents('.wages_row');

  

  var bal =($(".salary").val().replace("Rs.","")*1)+($(".overtime").val().replace("Rs.","")*1)-($(".loss").val().replace("Rs.","")*1)-($(".advance").val().replace("Rs.","")*1);

  //bal = roundNumber(bal,2);

  

  bal = bal.toFixed(2);

  

  $('.amnt').val(bal);

  

  var net= ($(".incentive").val().replace("Rs.","")*1)+($(".allowance").val().replace("Rs.","")*1)+(bal*1);

  

  net = net.toFixed(2);

  

  //alert(net);

  

  $('.amnt').val(net);

}



$(document).ready(function() {



$('.loss').blur(update_adjbalance);

$('.overtime').blur(update_adjbalance);

$('.incentive').blur(update_adjbalance);

$('.allowance').blur(update_adjbalance);

	

 });

 

 </script>                

                

                

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

<script>

	

jQuery(function($){

		   

	// simple jQuery validation script

	$('#payslip').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 alo=$('.allowance').val();

		var desc=$('.desc').val();

		

		if(alo!='' || alo!=0)

		{

		if(desc=='')

		{

		alert('Enter Description');

		valid=false;

		return false;

		}

		}

		

		return valid;

		

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

		

	});

	

})	





</script>      

        

<?php        



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

{ 

$id=$_POST['id'];

$date=$_POST['date'];

$name=$_POST['name'];

$empid=$_POST['empid'];

$month=$_POST['month'];

$year=$_POST['year'];

$amount=$_POST['amount'];

$salary=$_POST['salary'];

$description=$_POST['description'];

$advance=$_POST['advance'];

$loss=$_POST['loss'];

$incentive=$_POST['incentive'];

$allowance=$_POST['allowance'];

$balance=$_POST['balance'];

$overtime=$_POST['overtime'];

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

$nmonth = date('m',strtotime($month));



if($date && $empid && $month && $year && $amount)

{



$mx=mysql_query("select MAX(ID) as id from payslip");

$mx1=mysql_fetch_array($mx);



$nid=$mx1['id']+1;



$insert="insert into payslip(ID,date,month,year,empid,salary,lop,netsalary,amount,incentive,allowance,overtime) values('$nid','$date','$nmonth','$year','$empid','$salary','$loss','$balance','$amount','$incentive','$allowance','$overtime')";

$insert1=mysql_query($insert);



if($insert1){



header("location:viewpayslip.php?id=".$nid." & empid=".$empid." & s=1" . urlencode($Message));



}

}



else{



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



}



}







elseif($_GET['id'])

{



$id=$_GET['id'];

$month=$_GET['month'];

$year=$_GET['year'];



$monthname = date("F", mktime(0, 0, 0, $month, 10));



$sql=mysql_query("select * from employee where ID='$id'");

$sql1=mysql_fetch_array($sql);



$get=mysql_query("select MAX(ID) as ID from advance");

$get1=mysql_fetch_array($get);

$newid=$get1['ID']+1;



$sm=date($year.'-'.$month.'-01');



$sl=mysql_query("select * from salary where empid='".$sql1['empid']."' and incrementfrom<='$sm' order by ID desc limit 1");

$sl1=mysql_fetch_array($sl);



if($month==01)

{

$lm=12;

}	

else

{

$lm=$month-1;

}



$date1 = $year.'-'.$month.'-01';

$dte=date("t",strtotime($date1));

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



echo'<form action="payslip.php" method="post" id="payslip">



<center><table width="85%" style="line-height:37px;">



<input type="hidden" name="id" value="'.$newid.'">



<tr><td id="page">Employee Name </td><td>:</td> <td id="page"><input type="text" name="name" value="'.$sql1['name'].'" readonly="true"  /></td>

<td>Date </td><td>:</td><td> <input type="text" name="date" class="datepicker" value="'.date('d-m-Y').'" id="required" title="Enter Date" autocomplete="off" /></td></tr>



<tr><td id="page">Employee ID </td><td>:</td> <td id="page"><input type="text" name="empid" value="'.$sql1['empid'].'" readonly="true"  /></td><td>Salary </td><td>:</td><td> <input type="text" name="salary" value="'.$sl1['salary'].'" readonly="true" class="salary"  /></td></tr>



<tr><td id="page">Month </td><td>:</td> <td id="page"><input type="text" name="month" value="'.$monthname.'" readonly="true"  /></td><td>Year </td><td>:</td><td> <input type="text" name="year" value="'.$year.'" readonly="true"  /></td></tr>';



$amt=mysql_query("select sum(amount) as amnt from advance where month='$month' and year='$year' and empid='$sql1[empid]'");

$amt1=mysql_fetch_array($amt);



echo'<tr style="visibility:hidden;"><td id="page">Advance </td><td>:</td> <td id="page"><input type="text" name="advance" value="'.$amt1['amnt'].'" readonly="true" class="advance" /></td><td></td><td></td><td></td></tr></table>';





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

		$emp1=mysql_fetch_array($emp);

		

		

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

				

			$date1 = $year.'-'.$month.'-01';			

			$dte=date("t",strtotime($date1));

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

			

				

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

	

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

	

	$spd=$sl1['salary']/$balance;//salary per day

		

		echo"</center><b style='margin-left:7%;'>Attendance Entries : </b>

		<center><table border='1' style='width:87%; margin-top:0.5%;' align='center'><tr>";

		

$date1 = $year.'-'.$month.'-01';

$dte=date("t",strtotime($date1));

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

		

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

		

		$att=0;

		

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

		{

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

		*/

		

		$date1 = $year.'-'.$month.'-01';

		$dte=date("t",strtotime($date1));

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

		

	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 attendance where empid='".$sql1['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;

		}



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



		$l=$balance-$att;

		

		$loss=$l*$spd;

		

		$otat=mysql_query("select * from ot_attendance where empid='$sql1[empid]' and month(date)='$month' and year(date)='$year'");

		$otat1=mysql_num_rows($otat);

		

		echo"<br /><table style='width:87%; margin:auto;' border='1'>

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

		<table style='margin-left:2%; width:100%;'>

		<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>Number of working days : <b style='color:green;'>".$balance."</b></td></tr>

		<tr><td>Salary per day : <b style='color:green;'>".number_format($spd,2,'.','')."</b></td></tr>

		</table></td><td sytle='width:50%;'>";

		

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

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

		<tr><td>Loss of days : <b style='color:#FF0000;'> ".$l."</b></td></tr>

		<tr><td>Loss of pay : <b style='color:green;'>".number_format($loss,0,'.','').".00</b></td></tr>

		<tr><td>OT Days : <b style='color:green;'>".$otat1."</b></td></tr>

		</table></td></tr></table><br /><br />";

		

		$otvl=$otat1*$spd;

		

		$bal=$sl1['salary']-$loss-$amt1['amnt']+$otvl;





echo'<br /><table width="55%">

<tr><td>OT </td><td>:</td><td><input type="text" name="overtime" class="overtime" onKeyPress="return numbersonly(event, false)" value="'.number_format($otvl,0,'.','').'.00" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Loss of pay <span class="red">*</span> </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" name="loss" class="loss" id="required" title="Enter Amount" onKeyPress="return numbersonly(event, false)" value="'.number_format($loss,0,'.','').'.00" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Advance <span class="red">*</span> </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" name="advance" readonly="true" id="required" title="Enter Amount" onKeyPress="return numbersonly(event, false)" value="'.number_format($amt1['amnt'],0,'.','').'.00" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Balance <span class="red">*</span> </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" name="balance" readonly="true" onKeyPress="return numbersonly(event, false)" value="'.number_format($bal,0,'.','').'.00" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Incentive </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" class="incentive" name="incentive" onKeyPress="return numbersonly(event, false)" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Allowance </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" class="allowance" name="allowance" onKeyPress="return numbersonly(event, false)" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;">Amount Pay <span class="red">*</span> </td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><input type="text" name="amount" class="amnt" id="required" title="Enter Amount" onKeyPress="return numbersonly(event, false)" value="'.number_format($bal,0,'.','').'.00" autocomplete="off" /></td></tr>

<tr><td style="padding-top:7px;"></td><td style="padding-top:7px;"></td><td style="padding-top:7px;"><button id="save" type="submit" name="submit" value="Add" class="btn btn-sm btn-primary" style="height:31px;">Create</button></td></tr>



</table></center>



</form>';



}



?>





 <link rel="stylesheet" href="bootstrap/dataTables.tableTools.css">

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

 <script type="text/javascript" language="javascript" src="bootstrap/dataTables.tableTools.js"></script>

    <script type="text/javascript" language="javascript" src="bootstrap/js/jquery.dataTables.js"></script>

 

    <script>

    $(document).ready(function() {

        $('#dataTables-example').dataTable({ 

		

		/*dom: 'T<"clear">lfrtip',*/

		

		/*"sSwfPath" : "bootstrap/swf/copy_cvs_xls.swf",*/

		

		"bAutoWidth": false

			

	  });	  

	  

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

    $('#dataTables-example').dataTable().fnSetColumnVis([-1], false);

	$("#dataTables-example").css({"font-size": "10px"});



});  



});

	

</script>

 

 

<link rel="stylesheet" href="css/datepicker.css">



<script src="bootstrap/jquery-1.7.2.min.js"></script>

<script src="bootstrap/bootstrap-datepicker.js"></script>



<script>

    

	$('.datepicker').datepicker();

	

</script>





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

?>