
<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="salary_report";
$pgAct="view"; 
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

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>Salary Month</label>
   
    <select class="span6" name="bmonth" id="bmonth">
     <?php
     $monthAry = ["April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March"];
     $i = 0;
     foreach($monthAry as $m){$i++; ?>
         <option value='<?php echo $i; ?>' <?php if($i==$_POST['bmonth'])echo "selected"; ?> ><?php echo $m ?></option>
         <?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
    }*/
    ?>
    <label>Department</label>
    <select class="span6" name="department" id="department" onchange="getfaculty(this.value)">
		<option value=""> All</option>
		<?php 
			$class=$db->getRows("select * from department where school_id='".$_SESSION["admin"]["school_id"]."' ");
			foreach($class as $cla){
				?>
				<option <?php if($cla["name"]==$aryForm["department"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
					<?php echo $cla["name"] ?>
                </option>
				<?php 
			}
		?>
	</select>
    
    <div style="display:inline-block;width:30%" id="facultydiv">
      <select class="" name="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($_POST["teacher_id"])!=""){
	    $teacherid = " and idno ='$_POST[teacher_id]' ";
	}
	
	$dept = "";
	if(isset($_POST['department']) && trim($_POST["department"])!=""){
	    $dept = " and department ='$_POST[department]' ";
	}

	$month = $_POST["bmonth"];
// 	$year=$_POST["byear"];

	if($month=="")$month=date("m", (time()));
 
    $years=explode("-",$_SESSION["admin"]["session"]);
	if($month > 9){
	    $year = $years[1];
	    if($_POST["bmonth"]!=""){
	        $month = $month-9;
	    }
	} else {
	    $year = $years[0];
	    if($_POST["bmonth"]!=""){
	        $month = $month+3;
	    }
	}


	echo "<input type='hidden' id='totalDay' value='".cal_days_in_month(CAL_GREGORIAN, $month, $year)."' />";

	$aryPgAct=array("src"=>"Payroll", "page_id"=>$pgMod,"action"=>"add");
 
    $sql="select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $teacherid $dept and status=1 order by name";

	$aryList=$db->getRows($sql);

    $additioncomponents = $db->getRows("select * from salary_components where type='Addition' ");
    $deductioncomponents = $db->getRows("select * from salary_components where type='Deduction' ");

	if(is_array($aryList) && count($aryList)>0)
		{	$totpaybranch=0;
			?><hr style="clear:both">
            <h4 style="text-align:center;">Salary Report : <?php echo date("M",strtotime($year."-".$month."-1"))." ".$year;?> </h4>
		<form  method="post">
		    <div style="width:"> 
			<table class="table">
				<thead> 
					<tr>
						<th>S. No</th>
						<th>Name</th>
						<th>Account No.</th>
						<th>Basic salary</th>
						<?php foreach($additioncomponents as $comp){ ?>
                        <th><?php echo $comp['sname']; ?></th>
                        <?php } ?>
                        <?php foreach($deductioncomponents as $comp){ ?>
                        <th><?php echo $comp['sname']; ?></th>
                        <?php } ?>
                        <th>OneDay Payment</th>
                        <th>CL</th>
                        <th>Day Absent</th>
                        <th>Absent Total</th>
                        <th>Gross Total</th>
					</tr>
				</thead>
				<tbody>
				<?php 
				$total = 0;
				foreach($aryList as $clist){ $z++;

                $paydetail = $db->getRow("select * from salery where month ='".$month."' and idno='".$clist['idno']."' and session='".$_SESSION["admin"]["session"]."' ");

                $detail = json_decode($paydetail['detail'], true);
				?>
					<tr class="grade<?php if($z%2==0)echo "C";else "U";?>">
						<td><?php echo $z; ?><script>$(document).ready(function(){getFacultySaleryTotal('<?php echo $clist['idno'] ?>')})</script></td>
						<td><?php echo $clist["name"]; ?></td>
						<td><?php echo $clist["accountno"] ?></td>

						<td><?php echo $clist["salery"] ?></td>

                            <?php $k=0;
                                foreach($additioncomponents as $comp){$k++; 
                                $val = "";
                                foreach($detail as $key => $value)
                                {
                                    if($comp['id'] == $key)
                                    {
                                        if($comp['calculate_mode'] == "Amount"){
                                            $val = $value;
                                        } else {
                                            $amount = (($value*$paydetail['basic_salary'])/100);
                                            $val = $amount;
                                        }
                                    }
                                }
                                ?>

                                        <?php echo "<td>$val</td>"; ?>
                                    <?php
                                }
                            ?>

                            <?php
                                foreach($deductioncomponents as $comp){ $k++;
                                $val = "";
                                foreach($detail as $key => $value)
                                {
                                    if($comp['id'] == $key)
                                    {
                                        if($comp['calculate_mode'] == "Amount"){
                                            $val = $value;
                                        } else {
                                            $amount = (($value*$paydetail['basic_salary'])/100);
                                            $val = $amount;
                                        }
                                    }
                                }
                                ?>
                                <?php echo "<td>$val</td>"; ?>
                                    <?php
                                }
                            ?>
                        <td><?php echo $paydetail["one_day_payment"] ?></td>
                        <td><?php echo $paydetail["cl"] ?></td>
                        <td><?php echo $paydetail['total_absent']; ?></td>
                        <td><?php echo $paydetail['absent_amount']; ?></td>
                        <td style="text-align:right;"><?php echo number_format($paydetail["gross_total"],2); $total+=$paydetail["gross_total"]; ?>

                            <?php
                            // if(count($paydetail) > 1){
                            //     $recordfound = 1;
                            //     	echo "<a href='print/?page_id=salery&idno=$clist[idno]&month=$month&session=".$_SESSION["admin"]["session"]."' target='_blank' class='btn btn-info' style='width:50px;font-size:12px;'>Print</a>"; 
                            // }
                            ?>
                        </td>

					</tr>
				<?php } ?>
				</tbody>
				<tfoot>
				    <tr>
				        <th colspan="<?php echo 8+count($additioncomponents)+count($deductioncomponents);?>">TOTAL</th>
				        <th style="text-align:right;"><?php echo number_format($total,2); ?></th>
				    </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>

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

function getfaculty(id){
	$.post( "Payroll/ajax.php", {type: "departmentfaculty", department:id	}, 
		function( data ) {
	    document.getElementById("facultydiv").innerHTML = data;
	}); 

}

    function getFacultySaleryTotal(id){
        var totalDays=$("#totalDay").val();
    	var SchoolSalary=$("#SchoolSalary"+id).val();
    	var OneDayPayment=SchoolSalary/totalDays;
    	
        var nu = OneDayPayment.toFixed(2);
        // alert(SchoolSalary);
        $("#OneDayPayment"+id).val(nu);
        
        var record = '<?php echo $recordfound; ?>';
        if(record != 1){
        $("#GrossTotal"+id).val(SchoolSalary);
        }
    }
 
var totalcomponents = '<?php echo json_encode($totalcomp); ?>';
var temp1 = JSON.parse(totalcomponents);

function addup(tid){

    var basic = $("#SchoolSalary"+tid).val();
    var total = basic;
    var comp  = 0;
    var absenttotal = 0;

    var onedaypayment = $("#OneDayPayment"+tid).val();
    var absent  =   $("#DayAbsent"+tid).val();
    if(isNaN(absent) || absent == ""){

    } else {
        absenttotal = parseFloat(onedaypayment * absent);
    }

    var temptotal = basic;

    for(var i = 0; i<temp1.length; i++){
        var temp = $("#"+tid+'_'+temp1[i]).val();

        if(isNaN(temp) || temp == ""){
            temp = 0;
        }

        var type = $("#"+tid+'_'+temp1[i]).attr("data-type");
        var mode = $("#"+tid+'_'+temp1[i]).attr("data-mode");

        if(type == "Addition"){ 
            if(mode == "Amount"){
                temptotal = parseInt(temptotal)+parseInt(temp);
            }
            if(mode == "Percent"){
                var per = (parseInt(temp)*parseInt(basic))/100;
                temptotal = parseInt(temptotal)+parseInt(per);
            }
        }
        if(type == "Deduction"){
            if(mode == "Amount"){
                temptotal = parseInt(temptotal)-parseInt(temp);
            }
            if(mode == "Percent"){
                var per = (parseInt(temp)*parseInt(basic))/100;
                temptotal = parseInt(temptotal)-parseInt(per);
            }
        }
    }

        total = temptotal - absenttotal;

    $("#GrossTotal"+tid).val(total);
}

</script>
</div>
	</div></div></div>
	<?php

?>
