<?php
$pgMod="payFee";
$pgAct="view";
?>
<style>.expance {
width: 451px !important;
height: 287px !important;
}</style>
<?php
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
		
if(isset($_GET["id"]) && $_GET["id"]!=""){
	$insTall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and session='".$_SESSION["admin"]["session"]."'");
	if(is_array($insTall) && count($insTall)>0){
	
	}else{
		if(!isset($_GET["dis"]) || $_GET["dis"]=="" || $_GET["dis"]=="NULL"){
		?>
		<script>
			var dis=prompt("Please Enter Discount",0);
			window.location='?src=Hostel&page_id=payFee&action=add&id=<?php echo $_GET["id"] ?>&dis='+dis;
		</script>
        <?php
		exit;	
		}
		$insAry=array(	"idno"=>$_GET["id"],
						"session"=>$_SESSION["admin"]["session"],
						"discount"=>$_GET["dis"]
		);
		$installDetail=$db->getRow("select * from hostel where school_id='".$_SESSION["admin"]["school_id"]."' and name=(select hostel from hostelroom where id=(select roomid from roomallocation where idno='".$_GET["id"]."'))");
		if(is_array($installDetail) && count($installDetail)>0)
		{
			$insAry["hid"]=$installDetail["id"];
			$insAry["total"]=$installDetail["fee"];
			$devidedFee=(int)($installDetail["fee"]-$_GET["dis"])/(int)$installDetail["installment"];
			for($i=1; $i<=$installDetail["installment"]; $i++){
				$insAry["i".$i]=$devidedFee;
			}
			$insAry["school_id"]=$_SESSION["admin"]["school_id"];
$insAry["added_by"]=$_SESSION["admin"]["uid"];
			$db->insertAry("hostelinstallment",$insAry);
		}else{
			echo "Hostel May Be delete or remove From database, Please re allocate room to this student<br />
			<br />
			Click <a href='?src=Hostel&page_id=payFee'>here</a> to ho back<br />
			".$db->getErMsg().$db->getLastQuery();;
			exit;
		}
		//echo $db->getErMsg().$db->getLastQuery();
		//echo "<pre>";print_r($installDetail);echo $db->getErMsg().$db->getLastQuery();exit;
	}
}
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=="add" && isset($_GET['id']) && trim($_GET['id']) !='')
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
	$aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
?>
<style>
.st {
	width: 55px;
	display: inline-block;
	color: #021EF0;
}
</style>
<!-- Forms --> 
<div id="printDiv">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Student detail</h3>
                <div style="float:right; margin-top:5px">
                    <button type="button" href="<?php echo URL_ROOT ?>hostel/addexpance.php?id=<?php echo $_GET['id']; ?>" data-class="expance" class="btn btn-primary fancybox">Add Expance</button>
                    <button type="button" href="<?php echo URL_ROOT ?>hostel/addincome.php?id=<?php echo $_GET['id']; ?>" data-class="expance" class="btn btn-primary fancybox">Add income</button>
                    <button type="button" onclick="printDiv('printDiv')" class="btn btn-primary">Print</button>
                </div>
            </div>
            <div class="well">
				<div class="span3">
                	<ul class="unstyled">
                      <li><strong class="st">id no</strong> : <?php echo $aryForm["idno"] ?></li>
                      <li><strong class="st">DOA</strong> : <?php echo $aryForm["doa"] ?></li>
                    </ul>
            	</div>
                <div class="span2">
                	<ul class="unstyled">
                      <li><strong class="st">Class</strong> : <?php echo $aryForm["class"] ?></li>
                      <li><strong class="st">Father</strong> : <?php echo $aryForm["fname"] ?></li>
                    </ul>
            	</div>
                <div class="span2">
                	<ul class="unstyled">
                      <li><strong class="st">Roll no</strong> : <?php echo $aryForm["rollno"] ?></li>
                      <li><strong class="st">Subling</strong> : <?php echo $aryForm["sibling"] ?></li>
                    </ul>
            	</div>
                <div class="span3">
                	<ul class="unstyled">
                      <li><strong class="st">Name</strong> : <?php echo $aryForm["name"] ?></li>
                      <li><strong class="st">DOB</strong> : <?php echo $aryForm["dob"] ?></li>
                    </ul>
            	</div>
                <div class="span2">
					<?php if($aryForm["photo"]!=""){
                        ?><img src="media/photo/<?php echo $aryForm["photo"]; ?>" style="height:70px" /><?php
                    }else{
                        echo "No photo uploaded";
                    }?>
            	</div>
            </div>
        </div>
     </div> 
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Payment detail</h3>
                <div style="float:right; margin-top:5px; display:none">
                    <button type="button" class="btn btn-primary" onclick="if(confirm('Do you want to Pay compleet fee'))competeFee()">Complete fee</button>
                </div>
            </div>
            <div class="well no-padding">
             
            	<table class="table">
                    <thead>
                        <tr>
                            <th>Quater</th>
                            <th>Amount</th>
                            <th>Amount Given</th>
                            <th>Late Fine</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php 
						$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
						
						
						$feeCount=array();$totalclasspayfee=0;$totalbuspayfee=0;
						for($i=1;$i<=12;$i++){
							$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."' and ins_name='".$i."-hostel'");
							//echo $db->getLastQuery();
							$fine=$fineAry["fine"];
							$iis="i".$i;
							$iip="ip".$i;
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
							?>
                            <tr>
                                <td>Hostel fee <?php echo $i ?></td>
                                <td><input readonly="readonly" type="text" id="fee<?php echo $i ?>" class="input-block-level" value="<?php echo $aryInstall[$iis]; ?>"  name="fee[]"></td>
                                <td><input type="text" id="given<?php echo $i ?>" class="input-block-level" value="<?php echo $aryInstall[$iip]; ?>" <?php if($aryInstall[$iip]>0)echo 'readonly="readonly"'; ?> name="given[]"></td>
                                <td>
                                <?php 
								$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and ins_name='".$i."-hostel'");
								echo "<input  readonly='readonly' class='span4' type='text' title='".$fineAry["reasion"]."' rel='tooltip' value='".$fine=$fineAry["fine"]."'/>"; ?>
                                </td>
                                <td><?php if($aryInstall[$iip]>0)echo '<font color="#009900">Paid</font>';else echo '<font color="#FF0000">Due</font>'; ?></td>
                                <td class="classtd">
								<input type="hidden" value="<?php echo $aryInstall[$iis]; ?>" class="instalment" />
                                <input type="hidden" value="<?php echo $i ?>" class="instalment_no" />
								<?php if($aryInstall[$iip]>0)echo '';else{ 
								echo '<a style="float:left" onclick="paynow(\''.$i.'\')" href="javascript:;"><button type="button" class="btn btn-info">Pay Fees</button></a>'; 
									if($fine=="" || $fine==0){
										echo '<a href="add_fine.php?idno='.$aryForm["idno"].'&no='.$i.'&type=hostel" data-class="pradesh" class="fancybox"><button type="button" class="btn btn-warning">Add Fine</button></a>';
									}
								}?></td>
                            </tr>
							<?php
							}
						}
					?>
                    
                  	</tbody>
                 </table>
                 <table class="table" style="border-top:2px solid #F00">
                 	<thead>
                    	<tr>
                        	<th>Hostel Fee Description</th>
                            <th>Total Fee</th>
                            <th>Total Paid</th>
                            <th>Total Left</th>
                            <th>Discount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <th>Total Hostel Fee</th>
                            <th><?php echo $aryInstall["total"]; ?></th>
                            <th><?php echo $totalclasspayfee; ?></th>
                            <th><?php echo (int)($aryInstall["total"]-$totalclasspayfee); ?></th>
                           <td><?php echo $aryInstall["discount"]; ?></td>
                       </tr>
                        
                    </tbody>
                 </table>
                 <?php 
				 $allExpance=$db->getRows("select * from transaction where school_id='".$_SESSION["admin"]["school_id"]."' and tfor='hostel-expance' and rid='".$_GET["id"]."' and session='".$_SESSION["admin"]["session"]."'");
				 if(is_array($allExpance) && count($allExpance)>0){
					 ?>
				 <table class="table" style="border-top:2px solid #060">
                 	<thead>
                    	<tr>
                        	<th colspan="4">Total Expance</th>
                        </tr>
                        <tr>
                        	<th width="10%">Sr. No.</th>
                            <th width="20%">Date</th>
                            <th width="20%">Amount</th>
                            <th>Detail</th>
                        </tr>
                    </thead>
                    <tbody>
					 <?php $i=0;$totalexpanceamount=0;
				 	foreach($allExpance as $Expance){$i++;
						?>
						<tr>
                        	<th><?php echo $i; ?></th>
                            <th><?php echo $Expance["udate"]; ?></th>
                            <th><?php $totalexpanceamount+=$Expance["amount"]; echo $Expance["amount"]; ?></th>
                            <th><?php echo $Expance["detail"]; ?></th>
                        </tr>
						<?php	
					}
					?>
					</tbody>
                    <tfoot>
                    	<tr>
                        	<td></td>
                        	<td>Total</td>
                            <td colspan="2"><?php echo $totalexpanceamount; ?></td>
                        </tr>
                    </tfoot>
                    </table>
					<?php
				 }
				 ?>
                  <?php 
				 $allExpance=$db->getRows("select * from transaction where school_id='".$_SESSION["admin"]["school_id"]."' and tfor='hostel-income' and rid='".$_GET["id"]."' and session='".$_SESSION["admin"]["session"]."'");
				 if(is_array($allExpance) && count($allExpance)>0){
					 ?>
				 <table class="table" style="border-top:2px solid #060">
                 	<thead>
                    	<tr>
                        	<th colspan="4">Total Income</th>
                        </tr>
                        <tr>
                        	<th width="10%">Sr. No.</th>
                            <th width="20%">Date</th>
                            <th width="20%">Amount</th>
                            <th>Detail</th>
                        </tr>
                    </thead>
                    <tbody>
					 <?php $i=0;$totalincomeamount=0;
				 	foreach($allExpance as $Expance){$i++;
						?>
						<tr>
                        	<th><?php echo $i; ?></th>
                            <th><?php echo $Expance["udate"]; ?></th>
                            <th><?php $totalincomeamount+=$Expance["amount"]; echo $Expance["amount"]; ?></th>
                            <th><?php echo $Expance["detail"]; ?></th>
                        </tr>
						<?php	
					}
					?>
					</tbody>
                    <tfoot>
                    	<tr>
                        	<td></td>
                        	<td>Total</td>
                            <td colspan="2"><?php echo $totalincomeamount; ?></td>
                        </tr>
                    </tfoot>
                    </table>
					<?php
				 }
				 ?>
                 <table class="table" style="border-top:2px solid #F00">
                 	<thead>
                    	<tr>
                        	<th>Other Expance/Income</th>
                            <th>Total Expance Fee</th>
                            <th>Total Paid</th>
                            <th>Total Left</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <th>Total Hostel Fee</th>
                            <th><?php echo $totalexpanceamount; ?></th>
                            <th><?php echo $totalincomeamount; ?></th>
                            <th><?php echo (int)($totalexpanceamount-$totalincomeamount); ?></th>
                       </tr>
                        
                    </tbody>
                 </table>
            </div>
         </div>
    </div>
</div>        
<!-- / Forms -->
<?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>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>"add");
	//$aryList=$db->getRows("select * from student where session='".$_SESSION["admin"]["session"]."' and (status=1 or status=2)");
		$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 and idno in (select idno from roomallocation) order by name");
	//echo $db->getErMsg()." <hr>".$db->getLastQuery();
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S.No</th>
                    <th>Name</th>
                    <th>Class</th>
                    <th>Father</th>
                    <th>Mobile</th>
                    <th>Action</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 echo $clist["class"] ?></td>
                    <td><?php echo $clist["fname"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Pay Fees</button></a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th colspan="2"></th>
                </tr>
            </tfoot>
        </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
}

?>
<!-- / Forms -->
<script type="text/javascript">
function changeIns(){
	var cla=$("#class").val();
	var id=$("#noInstallment").val();
	var bus=$("#bus").val();
	$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { type: "installment" ,class: cla, bus: bus , nos: id } , function( data ) {
	  	$("#ins").html(data);
		getTotal();
	});
	
}
function getTotal(){
	var sum=0;var classfee=0;var busfee=0;
	var cost = document.getElementsByName('fee[]');
	classfee=$('#classfee').val();
	for(var i=0;i<cost.length;i++)
	{
		sum = parseInt(sum) + parseInt(cost[i].value);
	}
	$('#feetotal').val(sum);
	$('#discount').val(parseInt(classfee)-parseInt(sum));
}
function paynow(id){
	var given=$("#given"+id).val();
	var fee=$("#fee"+id).val();
	$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { 	type: "feepayment" ,	
							fee: fee, 
							given: given, 
							student_id:'<?php echo $_GET["id"] ?>',
							id: id } , function( data ) {
	  	if(data==0)
		{
			window.location='<?php echo curPageURL(); ?>';
		}
		else
		{
			alert(data);
		}
	});
}
function paybusnow(id){
	var busgiven=$("#busgiven"+id).val();
	var busfee=$("#busfee"+id).val();
	$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { 	type: "busfeepayment" ,	
							busfee: busfee, 
							busgiven: busgiven, 
							student_id:'<?php echo $_GET["id"] ?>',
							id: id } , function( data ) {
	  	if(data==0)
		{
			window.location='<?php echo curPageURL(); ?>';
		}
		else
		{
			alert(data);
		}
	});
}
function payother(id){
	var given=$("#givenother"+id).val();
	var fee=$("#feeother"+id).val();
	$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { 	type: "feeother" ,	
							fee: fee, 
							given: given, 
							student_id:'<?php echo $_GET["id"] ?>',
							id: id } , function( data ) {
	  	if(data==0)
		{
			window.location='<?php echo curPageURL(); ?>';
		}
		else
		{
			alert(data);
		}
	});
}
<?php if($aryInstall["status"]==1){?>
$(document).ready(function(){
	var givenCost = document.getElementsByName('given[]');
	var j=0;
	for(var i=0;i<givenCost.length;i++)
	{
		if(givenCost[i].value=="")j++;
	}
	if(j==1){
		var lasttd=$(".classtd:last").html();
		var add_ins='<a style="float:left" onclick="addinstallment()" href="javascript:;"><button type="button" class="btn btn-info">add install</button></a>';
		$(".classtd:last").html(lasttd+' '+add_ins);
	}
});
<?php } ?>
function addinstallment(){
	var ins = prompt ('How musch installment you want','2');
	var instalment_no=$(".classtd:last .instalment_no").val();
	var rested_instalment=12-instalment_no;
	if(ins>rested_instalment){
		alert("You maximun no of installment");
	}else
	if(ins!="" && ins!=0){
		make_installment(ins,instalment_no);
	}
}
function make_installment(id,instalment_no){
	var fee=$(".classtd:last .instalment").val();
	update_installment(id,fee,instalment_no,"update_fee_installment");
}
function update_installment(id,fee,instalment_no,type){
	$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { 	type: type ,	
							id: id, 
							fee: fee,
							instalment_no:instalment_no, 
							student_id:'<?php echo $_GET["id"] ?>'
						} , function( data ) {
	  	if(data==0)
		{
			window.location='<?php echo curPageURL(); ?>';
		}
		else
		{
			alert(data);
		}
	});
}
function competeFee(){
	if(confirm('Are you sure want to pay?')){
		$.post( "<?php echo URL_ROOT ?>Hostel/ajax.php", { 	type: "competeFee" ,
								student_id:'<?php echo $_GET["id"] ?>'
							} , function( data ) {
			if(data==0)
			{
				window.location='<?php echo curPageURL(); ?>';
			}
			else
			{
				alert(data);
			}
		});
	}
}
</script>