
<?php
$pgMod="paysalary";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{
	error_reporting(888);
	//echo sms(mydetail("mobile",$_POST["idno"],"staff"),"Dear ".mydetail("name",$_POST["idno"],"staff").", Your salary amount  ".$_POST["netsalery"]." is successfully credited in your account by  ".$LinksDetails["site_name"]." on ".$udate,"faculty_salery");
	//echo mydetail("mobile",$_POST["idno"],"staff");echo $db->getErMsg().$db->getLastQuery();exit;
	$saleryStructure=$db->getRows("select * from saleryStructure where school_id='".$_SESSION["admin"]["school_id"]."' and status=1");
	//echo"<pre>";//print_r($saleryStructure);
	$key=array();
	for($i=0; $i<count($_POST["key"]); $i++){
		$key[]=array("key"=>$_POST["key"][$i],"value"=>$_POST["value"][$i]);
	}
	$udate=$_POST["bmonth"];
	$NewAry=array(  "idno"=>$_POST["idno"],
					"udate"=>$udate,
					"uyear"=>$_POST["byear"],
					"total"=>$_POST["netsalery"],
					"paytype"=>$_POST["paytype"],
					"cheque"=>$_POST["cheque"],
					"bankname"=>$_POST["bankname"],
					"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					"detail"=>json_encode($key),
					"session"=>$_SESSION["admin"]["session"]
	);
	$check=$db->getVal("select idno from paysalery where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_POST["idno"]."' and udate='".$udate."' and uyear='".$_POST["byear"]."'");
	if($check==""){
		$flgIn=$db->insertAry("paysalery",$NewAry);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			withdraw($_POST["netsalery"],$_POST["idno"],$flgIn,"salery","Give Faculty salery");
			$mon=date('M',strtotime($_REQUEST["byear"]."-".$udate."-1"));
			sms(mydetail("mobile",$_POST["idno"],"staff"),"Dear ".mydetail("name",$_POST["idno"],"staff").", Your salary amount  ".$_POST["netsalery"]." is successfully credited in your account by  ".$LinksDetails["site_name"]." on ".$mon."-".$_POST["byear"],"faculty_salery");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Faculty", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}else{
		array_push($alert_err,"salery already insert for this month");
	}
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	//$flgUp=$db->updateAry("staff",array("status"=>0),"where idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Faculty", "page_id"=>$pgMod)));
}
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
}
if($pgAct=="pay" && isset($_GET['id']) && trim($_GET['id']) !='')
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- Forms --> 

<script>
function hide_underof(valu)
{
	if(valu=='1'){
		$('#paymode').hide('slow');
		$("#chque").val('');$("#backname").val('');
	}else 
	if(valu=='2'){
		$('#paymode').show('slow');
	}
}
function getTotal(){
	var sum=0;var classfee=0;var busfee=0;
	$('.sal').each(function(){
		var opp=$(this).attr("rel");
		if(opp=="-")
		sum = parseInt(sum) - parseInt($(this).val());
		else
		sum = parseInt(sum) + parseInt($(this).val());
	});
	$('#netsalery').val(sum);
}
$(document).ready(function(){
	getSalery()
	getTotal();
	$('.sal').keyup(function(){
		getTotal();
	});
	
});
var root='<?php echo URL_ROOT; ?>';
function getSalery(){
	$("#saleryDetail").html('<center><img src="'+root+'assets/icon/load.gif" /></enter>');
	var month=$("#bmonth").val();
	var year=$("#byear").val();
	$.post( "<?php echo URL_ROOT ?>Faculty/ajax.php", { type: "saleryDetail", month: month, year: year, id: "<?php echo $_GET["id"] ?>" } , function( data ) {
	  	$("#saleryDetail").html(data);
		getTotal();
		getCalender();
	});
}
function getCalender(){
	var month=$("#bmonth").val();
	var year=$("#byear").val();
	$.post( "<?php echo URL_ROOT ?>Faculty/ajax.php", { type: "CalenderDetail", month: month, year: year, id: "<?php echo $_GET["id"] ?>" } , function( data ) {
	  	$("#CalenderDetail").html(data);
	});
}
</script>
<form method="post" action="" id="printableDiv" enctype="multipart/form-data">
<?php  $aryForm=$db->getRow("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");//echo $db->getLastQuery(); echo $db->getErMsg();print_r($aryForm);?>

     <div class="row-fluid">
        <div class="span12">
            <div >
                        <div class="span3">
                            <ul class="unstyled">
                              <li><strong class="st">Name</strong> : <?php echo $aryForm["name"] ?></li>
                              <li><strong class="st">Father Name</strong> : <?php echo $aryForm["fname"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <ul class="unstyled">
                              <li><strong class="st">Department</strong> : <?php echo $aryForm["department"] ?></li>
                              <li><strong class="st">Designation</strong> : <?php echo $aryForm["designation"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <ul class="unstyled">
                              <li><strong class="st">Idno</strong> : <?php echo $aryForm["idno"] ?></li>
                              <li><strong class="st">DOB</strong> : <?php echo $aryForm["dob"] ?></li>
                            </ul>
                        </div>
                        <div class="span3">
                            <ul class="unstyled">
                              <li><strong class="st">Shift</strong> : <?php echo $aryForm["shift"] ?></li>
                              <li><strong class="st">Contact</strong> : <?php echo $aryForm["phone"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <?php if($aryForm["photo"]!=""){
                                ?><img src="media/photo/<?php echo $aryForm["photo"]; ?>" style="height:70px" /><?php
                            }?>
                        </div>
                </div>
                <div style="clear:both"></div>
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Pay salery</h3>
                <div style="float:right; margin-top:5px">
                	<button type="button" onclick="printDiv('printableDiv')" class="btn btn-primary">Print</button>
                    <button type="submit" class="btn btn-primary" id="paynowBTN"> Pay Now</button>
                </div>
            </div>
            <div class="well">
			<!-- Forms: Box -->
                <div class="span6">
                	<div class="control-group">
                        <label class="control-label" for="idno">Faculty idno</label>
                        <div class="controls">
                            <input type="text" readonly id="idno" name="idno" class="input-block-level" value="<?php echo $_GET["id"] ?>" >
                        </div>
					</div>
                    <div class="control-group">
                        <label class="control-label" for="rollno">Salery Month</label>
                        <div class="controls">
                            <select onchange="getSalery()" class="span6" name="bmonth" id="bmonth">
                             <?php
                                for($m = 1;$m <= 12; $m++){ 
                                    $month =  date("F", mktime(0, 0, 0, $m));
									$selected="";if($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 onchange="getSalery()" class="span6" name="byear" id="byear"><?php
								foreach($years as $y){
									echo "<option value='$y'>$y</option>"; 
								}
								?></select><?php
							}
							?>
                         </div>
                    </div>
                   
                    <div id="saleryDetail">
                    
                    </div>
                </div>
                
                <div class="span3">
                <table class="table">
                <th>Salery</th><th>Month</th><th>Year</th>
                <?php
					for($m = 1;$m <= 12; $m++){ 
						$mon[$m] =  date("M", mktime(0, 0, 0, $m));
					}
                    $saleryRecord=$db->getRows("select * from paysalery where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and session='".$_SESSION["admin"]["session"]."'");
					if(is_array($saleryRecord) && count($saleryRecord)>0){
						foreach($saleryRecord as $sr){
							?><tr><td><?php echo $sr["total"] ?></td><td><?php $ud=$sr["udate"];echo $mon[$ud]?></td><td><?php echo $sr["uyear"];?></td></tr><?php
						}
					}else{
						echo "<tr><td>No Salery Record Found</td></tr>";
					}
				?>
                </table>
                </div>
                
                <div id="CalenderDetail" class="printHide span3"> 
                
                </div>
                </div>
            </div>
        </div>
	</div>
</form>
<!-- / Forms -->
<?php
}elseif($pgAct=="viewall"){
	?>
    <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 style="float:right; margin-top:5px" onclick="printDiv('printdiv')" class="btn btn-primary" type="button">Print</button>
    </div>
    <div class="well no-padding" id="printdiv">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from staff  where school_id='".$_SESSION["admin"]["school_id"]."' order by name");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" >
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Name</th>
                    <th>Pay fee detail</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td>
                    <?php
					for($m = 1;$m <= 12; $m++){ 
						$mon[$m] =  date("M", mktime(0, 0, 0, $m));
					}
                    $saleryRecord=$db->getRows("select * from paysalery where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$clist["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
					if(is_array($saleryRecord) && count($saleryRecord)>0){
						foreach($saleryRecord as $sr){
							?><span title="<?php echo $sr["total"] ?>" class="label label-success" rel="tooltip"><?php $ud=$sr["udate"]; echo $mon[$ud] ?> (Rs<?php echo $sr["total"] ?>)</span> <?php
						}
					}else{
						echo "No Salary Record Found";
					}
					?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		<?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
	}
	?>
	</div></div></div>
	<?php
}
else{
	?>
	<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 style="float:right; margin-top:5px" onclick="window.location='./?src=Faculty&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Faculty", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 order by name");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Name</th>
                    <th>Shift</th>
                    <th>Department</th>
                    <th>Designation</th>
                    <th>Contact</th>
                    <th>Pay Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th colspan="3"></th>
                </tr>
            </tfoot>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["shift"] ?></td>
                    <td><?php echo $clist["department"] ?></td>
                    <td><?php echo $clist["designation"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                    <?php
					for($m = 1;$m <= 12; $m++){ 
						$mon[$m] =  date("M", mktime(0, 0, 0, $m));
					}
                    $saleryRecord=$db->getRows("select * from paysalery where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$clist["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
					if(is_array($saleryRecord) && count($saleryRecord)>0){
						foreach($saleryRecord as $sr){
							 
                               
							?><span title="<?php echo $sr["total"] ?>" class="label label-success" rel="tooltip"><?php $ud=$sr["udate"]; echo $mon[$ud] ?></span><?php
						}
					}else{
						echo "No Salery Record Found";
					}
					?>
                    </td>
                    <td>
                     <?php $aryPgAct['action']="pay"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Pay Now</button></a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		<?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
	}
	?>
	</div></div></div>
	<?php
}
?>