<?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">Salary Sheet</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>

								Salary Sheet

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



						<div class="row">

							<div class="col-xs-12">

                            

								<!-- PAGE CONTENT BEGINS -->



<script src="assets/js/jquery-2.1.4.min.js"></script>



<script>

	

jQuery(function($){

		   

	// simple jQuery validation script

	$('#course').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:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')

					.appendTo(parent)

					.fadeIn('fast')

					.click(function(){ $(this).remove(); });

				valid = false;

			};

		});

		

		return valid;

		

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

		

	});

	

});	



</script>







<?php  



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

{

	

	$month=$_GET['month'];

	$year=$_GET['year'];



echo"<center><a href='salarysheetexcel.php?month=".$month."&year=".$year."'><button type='submit' name='submit' value='submit' class='btn btn-sm btn-primary'>Excel</button></a></center>



<table style='margin:auto;'>



<tr><td>Month</td><td style='padding:0 7px;'>:</td><td>".date("F",mktime($month))."</td></tr>



<tr><td>Year</td><td style='padding:0 7px;'>:</td><td>".$year."</td></tr>



</table>



<table class='table table-bordered table-condensed' style='font-size:12px;'>



<thead><tr><th>Emp ID</th><th>Name</th><th>Designation</th>

<th>DOJ</th><th>No.Of Payable Days</th><th>OT Days</th>

<th>Salary/M</th><th>Ex-Gratia</th><th>Special Allowance</th><th>KPI Ince</th>

<th>Advance / Loan</th><th>Fine</th><th>Room Rent</th>

<th>Total Deduction</th><th>Grand Total</th><th>Net Payable</th><th>Mode of Payment</th></tr></thead>";



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

    );

}



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

				);

			}



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

while($em1=mysqli_fetch_assoc($em))

{



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

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

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







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

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



$datePeriod = returnDates($date1, $date2);



$tote=0;

foreach($datePeriod as $date) {



    $tdate = $date->format('d-m-Y');

	

	$cdate = $date->format('Y-m-d');



	$kp=mysqli_query($conn,"select * from kpi where date='$cdate' and employee='$em1[ID]'");

	$kp1=mysqli_fetch_assoc($kp);

	

	$sum=$kp1['attendance']+$kp1['socialmedia']+$kp1['crmupdate']+$kp1['businessconversation']+$kp1['deliveryfeedback']+$kp1['productfeedback']+$kp1['nocalls']+$kp1['paymentcollection']+$kp1['tds']+$td1['customerretention'];

	

	if($sum>0)

	{

	$kpii=1400/30/$sum*$sum;

	}

	

	$kpii=number_format($kpii,0,'.','');

	

	$tote=$tote+$kpii;



}





		//



			$nm=0;

			

			

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

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

			

			$datePeriod = returnDays($date1, $date2);

			foreach($datePeriod as $dte) {

				$tdate = $dte->format('d-m-Y');

				

				$nm++;		

				

			}

			

			$lastday=$nm;

			

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

			

			$interval = DateInterval::createFromDateString('1 day');

			$period = new DatePeriod($start, $interval, $end);

			foreach ($period as $dt)

			{

				if ($dt->format('N') == 7)

				{

					$no++;

				}

			}

			$sundays = $no;



	//

	

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

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



$ph=mysqli_query($conn,"select * from holiday where date between '$date1' and '$date2'");

$ph1=mysqli_num_rows($ph);

	

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



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



$sal=mysqli_query($conn,"select * from salary where empid='$em1[empid]' and incrementfrom<='$sm' order by ID desc limit 1");

$sal1=mysqli_fetch_assoc($sal);



$desig=mysqli_query($conn,"select * from employee_designation where ID='$em1[designation]'");

$desig1=mysqli_fetch_assoc($desig);



$spd=$sal1['salary']/$balancedays;



$otat=mysqli_query($conn,"select * from ot_attendance where empid='$em1[empid]' and month(date)='$month' and year(date)='$year'");

$otat1=mysqli_num_rows($otat);



$otvl=$otat1*$spd;



$grand=$sal1['salary']+$tote+$otvl;



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





echo"<tr><td>".$em1['empid']."</td><td>".$em1['name']."</td>

<td>".$desig1['designation']."</td>

<td>".date('d-m-Y',strtotime($em1['doj']))."</td>

<td>".$balancedays."</td><td>".$otat1."</td>

<td>".$sal1['salary']."</td>

<td></td><td></td>

<td>".$tote."</td>

<td></td><td></td><td></td><td></td>

<td>".$grand."</td><td>".$grand."</td><td>Bank, A/C No. ".$em1['accountno']."</td></tr>";



}



echo"</table>



<center><a href='salarysheet.php'>Back</a></center>";



}

else

{



$mn=date("m");

$yr=date("Y");



echo'<br /><form action="salarysheet.php" method="get">



<table style="margin:auto;">



<tr><td id="page">Month  <select class="form-control" name="month" id="month">



<option value="">Select Month</option>';



$i=1;



while($i<=12)



{



if($i<10)

{

$i='0'.$i;

}

else

{

$i=$i;

}



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



if($i==$mn)

{

echo"<option value='".$i."' selected='selected'>".$monthname."</option>";

}

else

{

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

}



$i=$i+1;



}



echo'</select></td>



<td id="page" style="padding:0 7px;">Year  <select class="form-control" name="year" id="year"><option value="">Select Year</option>';



$y="2019";

$cy=date('Y');



while($y<=$cy)

{



if($y==$yr)

{

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

}

else

{

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

}



$y=$y+1;



$j++;



}



echo'</select></td></tr>



<tr><td id="page" colspan="2" style="text-align:center; padding-top:11px;"><button type="submit" name="submit" value="submit" class="btn btn btn-primary">Go</button></td></tr>



</table>



</form>



</center>';



}



?>









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



?>