<?PHP

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>

	

jQuery(function($){

		   

	// simple jQuery validation script

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





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

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

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





//sundays



$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.'-0'.$lm.'-27';

			$date2 = $year.'-'.$month.'-26';*/

			

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

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



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



	

	$balancedays=$lastday-$sundays-$ph1;

	



		

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

    );

	}

	



////////////



echo"<center><b>Month of ".date("F",mktime(0, 0, 0, $month)).", ".$year."</b></center><br />



<table class='table table-striped table-bordered table-condensed' id='dataTables-example' style='font-size:11px;'>

<thead><tr><th width='7%'>Emp.ID</th><th width='15%'>Employee Name</th><th width='9%'>Salary (Rs.)</th>

<th width='9%'>OT (Rs.)</th>

<th width='9%'>LOP (Rs.)</th><th width='11%'>Allowances (Rs.)</th><th width='11%'>Net Pay (Rs.)</th>

<th width='9%'>Paid (Rs.)</th><th width='11%'>Balance (Rs.)</th><th width='10%'>Actions</th></tr></thead><tbody>";



$g=mysql_query("select * from employee ORDER BY ID ASC");

while($g1=mysql_fetch_array($g))

{



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



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

$rs1=mysql_fetch_array($rs);



$sum=mysql_query("select *,sum(amount) as amount from advance where empid='$g1[empid]' and month='$month' and year='$year'");

$sum1=mysql_fetch_array($sum);



$spd=$rs1['salary']/$balancedays;//salary per day





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



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



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



$spd=$rs1['salary']/$balancedays;//salary per day



//attendance



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

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



$att=0;

	

	$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='".$g1['ID']."' 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)

		{

		$attendance1=1;

		}

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

		{

		$attendance1=0.5;

		}

		elseif($day=='Sunday')

		{

		$attendance1=0;

		}

		elseif($p1>0)

		{

		$attendance1=0;

		}

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

		{

		$attendance1=0;

		}



		$att=$att+$attendance1;

		}

		

		$l=$balancedays-$att;

		

		$ccl=$l;

		

		/*if($l>=1)

		{

		$ccl=$l-1;

		}

		else

		{

		$ccl=0;

		}*/

		

		$loss=$ccl*$spd;

		



//





$lo=mysql_query("select * from payslip where empid='$g1[empid]' and month='$month' and year='$year'");

$nw=mysql_num_rows($lo);

$lo1=mysql_fetch_array($lo);







if($nw>0)

{

$l=$lo1['lop'];

}



else

{

$l=$loss;

}





//allowances



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

$otat1=mysql_num_rows($otat);



$otvl=$otat1*$spd;



$allowance=$lo1['allowance']+$lo1['incentive'];



$net=$rs1['salary']+$otvl+$allowance-$l;



$net=number_format($net,0,'.','').'.00';



$paid=$sum1['amount']+$lo1['amount'];



$balance=$net-$paid;



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



if($g1['status']!='')

{



if($salmon<$g1['dor'])

{



echo"<tr style='color:red;'><td>".$g1['empid']."</td><td>".$g1['name']."</td>

<td style='text-align:right;'>".number_format($rs1['salary'],2,'.','')."</td>

<td style='text-align:right;'>".number_format($otvl,0,'.','').".00</td>

<td style='text-align:right;'>".number_format($l,2,'.','')."</td>

<td style='text-align:right;'>".number_format($allowance,2,'.','')."</td>

<td style='text-align:right;'>".number_format($net,2,'.','')."</td>

<td style='text-align:right;'>".number_format($paid,2,'.','')."</td>

<td style='text-align:right;'>".number_format($balance,2,'.','')."</td>

<td style='padding-left:10px;'>";



if($balance!=0 && $nw==0)

{

echo"<a href='payslip.php?id=".$g1['ID']."&month=".$month."&year=".$year."'>Pay</a>";

}

elseif($balance!=0 && $nw>0)

{

echo"<a href='del.php?id=".$lo1['ID']."&type=payslip&month=".$month."&year=".$year."&empid=".$g1['ID']."' title='Delete'><i class='fa fa-trash-o'></i></a>";

}

elseif($balance==0)

{

echo"<a href='viewpayslip1.php?id=".$g1['ID']."&month=".$month."&year=".$year."'><i class='fa fa-search-plus'></i></a> &nbsp; <a href='del.php?id=".$lo1['ID']."&type=payslip&month=".$month."&year=".$year."&empid=".$g1['empid']."' title='Delete'><i class='fa fa-trash-o'></i></a>";

}



echo"</td></tr>";



}



}



else

{



echo"<tr><td>".$g1['empid']."</td><td>".$g1['name']."</td><td style='text-align:right;'>".number_format($rs1['salary'],2,'.','')."</td>

<td style='text-align:right;'>".number_format($otvl,0,'.','').".00</td><td style='text-align:right;'>".number_format($l,2,'.','')."</td><td style='text-align:right;'>".number_format($allowance,2,'.','')."</td><td style='text-align:right;'>".number_format($net,2,'.','')."</td><td style='text-align:right;'>".number_format($paid,2,'.','')."</td><td style='text-align:right;'>".number_format($balance,2,'.','')."</td><td style='padding-left:10px;'>";



if($balance!=0 && $nw==0)

{

echo"<a href='payslip.php?id=".$g1['ID']."&month=".$month."&year=".$year."'>Pay</a>";

}

elseif($balance!=0 && $nw>0)

{

echo"<a href='del.php?id=".$lo1['ID']."&type=payslip&month=".$month."&year=".$year."&empid=".$g1['ID']."' title='Delete'><i class='fa fa-trash-o'></i></a>";

}

elseif($balance==0 && $paid>0)

{

echo"<a href='viewpayslip1.php?id=".$g1['ID']."&month=".$month."&year=".$year."'><i class='fa fa-search-plus'></i></a> &nbsp; <a href='del.php?id=".$lo1['ID']."&type=payslip&month=".$month."&year=".$year."&empid=".$g1['ID']."' title='Delete'><i class='fa fa-trash-o'></i></a>";

}



echo"</td></tr>";



}



}



echo"</tbody></table>";



echo"<br /><br />



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



}







else

{



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

	{

	 $status=$_GET['s'];

	 

		if($status=='2')

		 {

           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;">Values Deleted Successfully.</span>

                </div>';

		}

		if($status=='3')

		 {

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

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

                <span style="text-align:left;">Value not deleted.</span>

                </div>';

		}

		

	}



?>





<form id="register" action="salary.php" method="get">



<center>



<table>



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



<?php



$sty=2019;



$cy=date("Y");



while($sty<=$cy)

{

	echo"<option value='".$sty."'>".$sty."</option>";

	

	$sty=$sty+1;

}



?>



</select>



</td></tr>



<tr><td></td><td></td><td style="padding-top:11px;"><button type="submit" name="submit" value="submit" class="btn btn-sm btn-primary">Submit</button></td></tr>



</table>



</center>



</form>



 <?php



 }



 ?> 

 

 

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

}

?>