
<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} 
.recepit {
	width: 717px !important;
	height: 344px !important;
}
</style><?php
$pgMod="staff";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);



if(isset($_POST['saveme']))
{
$resulr1=$_POST['idno'];
foreach($resulr1 as $x => $x_value) {

	$db->delete("salery","where  idno='".$x_value."' and month='".$_POST["month"]."'");

    $saveAry=array(
                "idno"              =>  $x_value,
                "one_day_payment"   =>  $_POST["one_day_payment"][$x],
                "total_absent"      =>  $_POST["total_absent"][$x],
                "total_cl"          =>  $_POST["total_cl"][$x],
                "advance_payment"   =>  $_POST["advance_payment"][$x],
                "grass_total"       =>  $_POST["grasstotal"][$x],
                "month"             =>  $_POST["month"]
			);
    $flgIn=$db->insertAry("salery",$saveAry);
}



if($flgIn>0)
{
?>
	echo "<script>window.location = 'print/?page_id=salery&action=print&month=<?php echo $_POST["month"] ?>';</script>"; 
<?php
}

}

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}

	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <!-- <button type="button" style="float:right" onclick="printDiv('printDiv')" class="btn btn-danger">Print</button> !-->
    </div>
    <div class="well no-padding">
    <form method="post" style=" margin:20px">
    <label class="control-label" for="rollno">Salery Month</label>
   
    <select class="span6" name="bmonth" id="bmonth">
     <?php
        for($m = 1;$m <= 12; $m++){ 
            $month =  date("F", mktime(0, 0, 0, $m));
            $selected="";
			if(isset($_POST["bmonth"]) &&  $_POST["bmonth"] == $m) $selected="selected";
			elseif($m == date("m", (time()))) $selected="selected"  ;
			
            echo "<option $selected value='$m'>$month</option>"; 
        }
     ?> 
    </select>
    <?php
    $years=explode("-",$_SESSION["admin"]["session"]);
    if(is_array($years) && count($years)>0){
        ?><select class="span6" name="byear" id="byear"><?php 
		$selected="";
        foreach($years as $y){
			if(isset($_POST["byear"]) && $_POST["byear"] == $y) $selected="selected";
            echo "<option $selected value='$y'>$y</option>"; 
        }
        ?></select><?php
    }
    ?>
    Branch 
    <select class="span6" name="shift" id="shift">
        <option value="">All</option>
        <?php 
            $location=$db->getRows("select * from branch where school_id='".$_SESSION["admin"]["school_id"]."'");
            if(is_array($location) && count($location)>0){
                foreach($location as $loc){
                    ?>
                    <option <?php if($loc["id"]==$_POST["shift"])echo 'selected';?> value="<?php echo $loc["id"] ?>">
                        <?php echo $loc["name"] ?>
                    </option>
                    <?php
                }
            }
        ?>
    </select>
    
          <!--<div style="display:inline-block;"><span>Select Teacher</span><br>-->
      <select name="teacher_id" id="teacher_id">
      <option value="">--Select Teacher--</option>
      <?php $staff = $db->getRows("select * from staff where session='".$_SESSION["admin"]["session"]."' and status=1 order by name");
            foreach($staff as $teacher){
                echo "<option value='$teacher[idno]'>$teacher[name]</option>";
                }
            ?>
        </select>
      <!--</div>-->
    
    <button class="btn btn-default" type="submit">Search</button>
   </form>
<div id="printDiv">
	<?php
	$teacherid  = "";
	
	if(isset($_POST['teacher_id']) && trim($_GET["teacher_id"])!=""){
	    $teacherid = " and idno ='$_POST[teacher_id]' ";
	}
	
	$month= $_POST["bmonth"];
	$year=$_POST["byear"];
	if($month=="")$month=date("m", (time()));
	if($year=="")$year=$years[0];
	echo "<input type='hidden' id='totalDay' value='".cal_days_in_month(CAL_GREGORIAN, $month, $year)."' />";
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	 $sql="select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $teacherid and status=1 order by name";
	if(isset($_POST["shift"]) && $_POST["shift"]!="")
		$sql="select * from staff where shift='".$_POST["shift"]."' and school_id='".$_SESSION["admin"]["school_id"]."' $teacherid and session='".$_SESSION["admin"]["session"]."' and status=1 order by name";
	$aryList=$db->getRows($sql);
	//echo $year." ".$month;
	if(is_array($aryList) && count($aryList)>0)
		{	$totpaybranch=0;
			?><hr style="clear:both">
            <h4>Teacher Salery for <?php echo date("M",strtotime($year."-".$month."-1"))." ".$year;?> <p style="float:right">Branch : <?php if($_POST["shift"]=="")echo "ALL"; else $db->getVal("select name from branch where id='".$_POST["shift"]."' and school_id='".$_SESSION["admin"]["school_id"]."'"); ?></p></h4>
		<form  method="post">
		    <div style="width:100%;overflow-x:scroll;">
			<table class="table">
				<thead>
					<tr>
						<th>Serial No</th>
						<th>Name</th>
						<th>Contcat no</th>
						<th>Designation</th>
						<th>Bank Salary</th>
						<th>School salary</th>
                        <!--<th>Diffrence</th>  !-->

                        <th>Medical Allowance</th>
                        <th>Transport Allowance</th>
                        <th>PF - Employer</th>
                        <th>Grade Pay</th>
                        <th>Dearness Allowance</th>
                        <th>Special Allowance</th>
                        <th>House Rent Allowance</th>

                        <th>Uniform</th>
                        <th>TDS</th>
                        <th>PF</th>
                        <th>ESI</th>
                        <th>House Rent</th>
                        <th>Electricity Bill</th>

                        <th>OneDay Payment</th>
                        <th>Day Absent</th>
                        <th>CL Day</th>
                        <th>Advance Payment</th>
                        <th>Gross Total</th>
                        <th></th>
                        <th></th>
					</tr>
				</thead>
				<tbody>
				<?php foreach($aryList as $clist){ $i++;
				$paydetail      =   $db->getRow("select * from paybybranch where dmonth='".$month."' and dyear='".$year."' and faculty_id='".$clist['idno']."' and status=1 and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
				$aryPgAct["id"] =   $clist['idno'];$totpaybranch+=$paydetail["GrossTotal"];
				$grass_total    =   $db->getVal("select grass_total from salery where month ='".$month."' and idno='".$clist['idno']."' ");
				$total_absent   =   $db->getVal("select total_absent from salery where month ='".$month."' and idno='".$clist['idno']."' ");
                $total_cl       =   $db->getVal("select total_cl from salery where month ='".$month."' and idno='".$clist['idno']."' ");
    			$advance_payment    =   $db->getVal("select advance_payment from salery where month ='".$month."' and idno='".$clist['idno']."' ");

				?>
					<tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
						<td><?php echo $i; ?><script>$(document).ready(function(){getFacultySaleryTotal('<?php echo $clist['idno'] ?>')})</script></td>
						<td><?php echo $clist["name"] ?></td>
						<td><?php echo $clist["mobile"] ?></td>
						<td><?php echo $clist["designation"] ?></td>
						<td><?php echo $clist["email"] ?>
                        <input class="py" type="hidden" data-id="<?php echo $clist['idno'] ?>" value="<?php echo $clist["email"] ?>" id="BankSalary<?php echo $clist['idno'] ?>" />
                        </td>
						<td><input class="py" type="text" readonly="readonly" data-id="<?php echo $clist['idno'] ?>" id="SchoolSalary<?php echo $clist['idno'] ?>" value="<?php echo $clist["salery"] ?>" /></td>
						<!--<td><input class="py" type="text" name="diffrence[]" data-id="<?php echo $clist['idno'] ?>" readonly="readonly" id="Diffrence<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["Diffrence"] ?>" /></td>  !-->
	
	                    <td><input class="py" type="text" id="medicalallowance<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["medical"] ?>" /></td>
	                    <td><input class="py" type="text" id="transportallowance<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["transport"] ?>" /></td>
	                    <td><input class="py" type="text" id="pfEmployer<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["pfemployer"] ?>"  /></td>
	                    <td><input class="py" type="text" id="gradepay<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["gradepay"] ?>"  /></td>
	                    <td><input class="py" type="text" id="dearnessallowance<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["dearnessallowance"] ?>"  /></td>
	                    <td><input class="py" type="text" id="specialallowance<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["specialallowance"] ?>"  /></td>
	                    <td><input class="py" type="text" id="hrallowance<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["hrallowance"] ?>" /></td>

	                    <td><input class="py" type="text" id="uniform<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["uniform"] ?>" /></td>
	                    <td><input class="py" type="text" id="tds<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["tds"] ?>" /></td>
	                    <td><input class="py" type="text" id="pf<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["pf"] ?>" /></td>
	                    <td><input class="py" type="text" id="esi<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["esi"] ?>" /></td>
	                    <td><input class="py" type="text" id="houserent<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["houserent"] ?>" /></td>
	                    <td><input class="py" type="text" id="electricitybill<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["electricitybill"] ?>" /></td>

						<td><input class="py" type="text" name="one_day_payment[]" data-id="<?php echo $clist['idno'] ?>" readonly="readonly" id="OneDayPayment<?php echo $clist['idno'] ?>" value="<?php echo $paydetail["OneDayPayment"] ?>" /></td>
                        <td><input class="py" type="text" name="total_absent[]" data-id="<?php echo $clist['idno'] ?>" id="DayAbsent<?php echo $clist['idno'] ?>"  value="<?php echo $total_absent; ?>" /></td>
						<td><input class="py" type="text" name="total_cl[]" data-id="<?php echo $clist['idno'] ?>" id="CLAmount<?php echo $clist['idno'] ?>" value="<?php echo $total_cl; ?>" /></td>
                        <td><input class="py" type="text" name="advance_payment[]" data-id="<?php echo $clist['idno'] ?>" id="AdvancePayment<?php echo $clist['idno'] ?>"  value="<?php echo $advance_payment; ?>" /></td>
                        <td><input class="py" name="grasstotal[]" type="text" readonly="readonly" data-id="<?php echo $clist['idno'] ?>" id="GrossTotal<?php echo $clist['idno'] ?>"  value="<?php echo (int)$paydetail["GrossTotal"] ?>" /></td>
                        <td><input class="py" name="idno[]" type="hidden" readonly="readonly" data-id="<?php echo $clist['idno'] ?>" id="GrossTotal<?php echo $clist['idno'] ?>"  value="<?php echo $clist['idno']; ?>" /></td>
                        <input class="py" name="month" type="hidden" readonly="readonly" data-id="<?php echo $clist['idno'] ?>" value="<?php echo $month ?>" />

                        <td>
                        <?php if($paydetail["id"]!=""){?>
                        <button class="btn btn-danger" onclick="deletepaynow('<?php echo $clist['idno'] ?>','<?php echo $paydetail['id'] ?>','<?php echo $paydetail['tid'] ?>')" data-id="<?php echo $clist['idno'] ?>" type="button" id="btn<?php echo $clist['idno'] ?>">delete</button>
                        <a href="<?php echo URL_ROOT; ?>/print?page_id=payslip&idno=<?php echo $clist['idno'] ?>&month=<?php echo $month; ?>&year=<?php echo $year; ?>" target="_blank" class="btn btn-info">PaySlip</a>
                        </td>
                        <?php
						}else{?>
                        <button class="btn btn-default" onclick="paynow('<?php echo $clist['idno'] ?>')" data-id="<?php echo $clist['idno'] ?>" type="button" id="btn<?php echo $clist['idno'] ?>">Pay</button></td>
                        <?php } ?>
					</tr>
				<?php } ?>
				</tbody>
               <!-- <tfoot>
                	<tr>
                    	<td colspan="11">Gross Total</td>
                        <td><?php echo $total_amt=$db->getVal("select sum(salery)  from staff where session='".$_SESSION["admin"]["session"]."'  and status=1") ?></td>
                        <td></td>
                    </tr>
                </tfoot>  !-->
			</table>
            </div>
			  <center><button type="submit" id="saveme" name="saveme"  class="btn btn-primary"> Save & Print</button></center>
			</form>
			<?php
		}
		else{
			?>
			<div class="alert alert-info">
			  <a class="close" data-dismiss="alert">×</a>
			  <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
			</div>
			<?php
		}
	?>
	<style>
input.py {
  width: 60px;
}
</style>
<script>
$(".py").each(function(index, element) {
    var id=$(this).attr("data-id");
	$("#SchoolSalary"+id).on("keyup",function(){
		getFacultySaleryTotal(id);
	})
	$("#DayAbsent"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#CLAmount"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#AdvancePayment"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});

	$("#medicalallowance"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#transportallowance"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#pfEmployer"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#gradepay"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#dearnessallowance"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#specialallowance"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#hrallowance"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});


	$("#uniform"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#tds"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#pf"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#esi"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#houserent"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});
	$("#electricitybill"+id).on("keyup",function(){
		getFacultySaleryTotal(id)
	});

});
function getFacultySaleryTotal(id){
	var totalDays=$("#totalDay").val();
	var SchoolSalary=$("#SchoolSalary"+id).val();
	var OneDayPayment=SchoolSalary/totalDays;
	
		 var nu = OneDayPayment.toFixed(2);
	$("#OneDayPayment"+id).val(nu);
	
	var BankSalary=$("#BankSalary"+id).val();
	var Diffrence=BankSalary-SchoolSalary;
	$("#Diffrence"+id).val(Diffrence);
	
	var OneDayPayment=$("#OneDayPayment"+id).val();
	var DayAbsent=$("#DayAbsent"+id).val();
	var SchoolSalary=$("#SchoolSalary"+id).val();
	var CLAmount=$("#CLAmount"+id).val();
	var AdvancePayment=$("#AdvancePayment"+id).val();
	
	var medical         =   $("#medicalallowance"+id).val();
    var transport       =   $("#transportallowance"+id).val();
    var pfemployer      =   $("#pfEmployer"+id).val();
    var gradepay        =   $("#gradepay"+id).val();
    var dearnessallowance=  $("#dearnessallowance"+id).val();
    var specialallowance=   $("#specialallowance"+id).val();
    var hrallowance     =   $("#hrallowance"+id).val();
    
    var uniform         =   $("#uniform"+id).val();
    var tds             =   $("#tds"+id).val();
    var pf              =   $("#pf"+id).val();
    var esi             =   $("#esi"+id).val();
    var houserent       =   $("#houserent"+id).val();
    var electricitybill =   $("#electricitybill"+id).val();
	
// 	FinalSalery=parseInt(SchoolSalary)-(OneDayPayment*DayAbsent)+(CLAmount*OneDayPayment)-AdvancePayment + parseInt(medical+transport+pfemployer+gradepay+dearnessallowance+specialallowance+hrallowance) -parseInt(uniform+tds+pf+esi+houserent+electricitybill);
    FinalSalery=parseInt(SchoolSalary)-(OneDayPayment*DayAbsent)+(CLAmount*OneDayPayment)-AdvancePayment + parseInt(medical) + parseInt(transport)+ 
    parseInt(pfemployer)+parseInt(gradepay)+parseInt(dearnessallowance)+parseInt(specialallowance)+parseInt(hrallowance) 
    - parseInt(uniform) - parseInt(tds) - parseInt(pf) - parseInt(esi) - parseInt(houserent) - parseInt(electricitybill);
    var gt = FinalSalery.toFixed(2);
	$("#GrossTotal"+id).val(gt)
}
function paynow(id){

	$("#btn"+id).hide();

	$.post( "Faculty/ajax.php", { 	type: "paybybranch" ,
							faculty_id      :   id,
							totalDays       :   $("#totalDay").val(),
							SchoolSalary    :   $("#SchoolSalary"+id).val(),
							OneDayPayment   :   $("#OneDayPayment"+id).val(),
							Diffrence       :   $("#Diffrence"+id).val(),
							DayAbsent       :   $("#DayAbsent"+id).val(),
							CLAmount        :   $("#CLAmount"+id).val(),
							AdvancePayment  :   $("#AdvancePayment"+id).val(),
							GrossTotal      :   $("#GrossTotal"+id).val(),
							dmonth          :   '<?php echo $month?>',
							dyear           :   '<?php echo $year?>',

							medical         :   $("#medicalallowance"+id).val(),
                            transport       :   $("#transportallowance"+id).val(),
                            pfemployer      :   $("#pfEmployer"+id).val(),
                            gradepay        :   $("#gradepay"+id).val(),
                            dearnessallowance:  $("#dearnessallowance"+id).val(),
                            specialallowance:   $("#specialallowance"+id).val(),
                            hrallowance     :   $("#hrallowance"+id).val(),

                            uniform         :   $("#uniform"+id).val(),
                            tds             :   $("#tds"+id).val(),
                            pf              :   $("#pf"+id).val(),
                            esi             :   $("#esi"+id).val(),
                            houserent       :   $("#houserent"+id).val(),
                            electricitybill :   $("#electricitybill"+id).val()

						} , function( data ) {

		alert("Salery pay successfully");
	});
}
function deletepaynow(id,payid,tid){
	$("#btn"+id).hide();
	$.post( "Faculty/ajax.php", { 	type: "deletepaybybranch" ,
									payid:payid,
									tid:tid
								} , function( data ) {
		alert("Salery removed successfully")	
		$("#DayAbsent"+id).val(0);
		$("#CLAmount"+id).val(0);
		$("#AdvancePayment"+id).val(0);
		getFacultySaleryTotal(id);
	});
}
</script>
</div>
	</div></div></div>
	<?php

?>
