<?php
include("../db.php");
/*?>
<script>
$(document).ready(function(){
	$('.onlyZero').blur(function(){
		if($(this).val()==''){
			$(this).val('0');
		}
		//alert($(this).val());
	});
});
</script><?php */
$ch=$_REQUEST["type"];
if($ch=="getsibling"){
	$detail=$db->getRow("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["id"]."'");
	echo "Name : ".$detail["name"]."\n";
	echo "Mobile no : ".$detail["mobile"]."\n";
	echo "father name : ".$detail["fname"]."\n";
	echo "Address : ".$detail["address"]."\n";
	echo "DOB : ".$detail["dob"]."\n";
	echo "class : ".$detail["class"]."\n";
}
elseif($ch=="classinstallment"){
	$clasDetail=$db->getRow("select fee,installment from class where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."'");
	$totalFee=$clasDetail["fee"];
	$installment=$clasDetail["installment"];
	$perFee=$totalFee/$installment;
	for($i=1;$i<=$installment;$i++){
		?>
		<div class="control-group">
			<label class="control-label" for="fee">Class Fee <?php echo $i; ?></label>
			<div class="controls">
				<input type="text" readonly="readonly" id="fee<?php echo $i; ?>" name="fee[]" class="input-block-level feeInsCharge" value="<?php echo number_format($perFee, 2, '.', ''); ?>">
			</div>
		</div>
		<?php
	}
	?>
	<div class="control-group">
		<label class="control-label" for="feetotal">Total</label>
		<div class="controls">
			<input readonly type="text" id="feetotal" name="feetotal" class="input-block-level" value="">
		</div>
	</div>
	<div class="control-group" style="border-bottom:2px solid #F00">
		<label class="control-label" for="discount">Discount</label>
		<div class="controls">
			<input type="text" onkeyup="updateIns()" id="discount" name="discount" class="input-block-level onlyZero" value="0">
			<input type="hidden" id="oldtotal" />
		</div>
	</div>
	<?php
	
}
elseif($ch=="businstallment"){ 
		$busDetail=$db->getRow("select fee,installment from bus where school_id='".$_SESSION["admin"]["school_id"]."' and bus='".$_REQUEST["bus"]."'");
		$totalFee=$busDetail["fee"];
		$installment=$busDetail["installment"];
		if($totalFee>0 && $installment>0)
		{
			$perFee=$totalFee/$installment;
			for($i=1;$i<=$installment;$i++){
				?>
				<div class="control-group">
					<label class="control-label" for="fee">Bus Fee <?php echo $i; ?></label>
					<div class="controls">
						<input type="text" readonly="readonly" id="busfee<?php echo $i; ?>" name="busfee[]" class="input-block-level busfeeInsCharge" value="<?php echo number_format($perFee, 2, '.', ''); ?>">
					</div>
				</div>
				<?php
			}
		}
		?>
		<div class="control-group">
            <label class="control-label" for="busfeetotal">Total</label>
            <div class="controls">
                <input readonly type="text" id="busfeetotal" name="busfeetotal" class="input-block-level" value="">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="busdiscount">Discount</label>
            <div class="controls">
                <input type="text" onkeyup="busupdateIns()" id="busdiscount" name="busdiscount" class="input-block-level" value="0">
                <input type="hidden" id="busoldtotal" class="onlyZero" />
            </div>
        </div>
		<?php
	
}
elseif($ch=="update_fee_installment"){
	$newUpdateFee=$_REQUEST["fee"]/$_REQUEST["id"];
	$NewAry==array();
	for($i=0;$i<$_REQUEST["id"];$i++){
		$busins="i".(int)($_REQUEST["instalment_no"]+$i);
		$NewAry[$busins]=$newUpdateFee;
	}
	$inup=$db->updateAry("hostelinstallment",$NewAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_REQUEST["student_id"]);
	if($inup!="")	echo 0;	else echo $db->getErMsg();
}
elseif($ch=="competeFee"){
	$busPayment=0;$feePayment=0;
	$installDeatail=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
	$completeAry=array("status"=>2);
	for($i=1;$i<=12;$i++){
		$ipay="ip".$i;$ip="i".$i;
		if($installDeatail[$ipay]=="" && trim($installDeatail[$ip])!="" && trim($installDeatail[$ip])!="0" ){
			$completeAry[$ipay]=$installDeatail[$ip];
			$feePayment=(int)$feePayment+(int)$installDeatail[$ip];
		}
	}
	
	$inup=$db->updateAry("hostelinstallment",$completeAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_REQUEST["student_id"]);//echo $db->getLastQuery();exit;
	if($inup!=""){	
		foreach($completeAry as $key=>$value){
			installment_time($_REQUEST["student_id"],$key);
		}
		deposit($feePayment,$_REQUEST["student_id"],$inup,"Hostel fee","Direct Complete Hostel fee Payment in single time");
	}
	else echo $db->getErMsg();
}
elseif($ch=="feepayment"){
	$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
	if($_REQUEST["given"]==""){
		echo "please enter fee";
	}elseif(!is_numeric($_REQUEST["given"])){
		echo "fee must in numeric formet";
	}elseif($_REQUEST["given"]==0){
		echo "fee must greater then 0";
	}else{
		$nextin="i".($_REQUEST["id"]+1);//echo $db->getLastQuery();
		$requestid=$_REQUEST["id"];
		$given=$_REQUEST["given"];
		$total=0;$givenTotal=0;$k=0;
		for($i=0;$i<=14;$i++){
			if($aryInstall["i".($requestid+$i)]!=""){
				$k++;
				$total=$total+$aryInstall["i".($requestid+$i)];
				$nexttotal=$total+$aryInstall["i".($requestid+$i+1)];$currentTotal=0;$balanceTotal=0;
				//echo $total." ".$nexttotal." ".$i."\n";
				if($given<$total && $i==0 && $nexttotal!=$total){
					$newInstallment=$total-$_REQUEST["given"];//." ".$aryInstall["i".($requestid+$j)]." ";
					$newNextInstallment=$newInstallment+$aryInstall["i".($requestid+1)];
					$insAry=array( "ip".($requestid)	=>	$given,
								"i".($requestid+1)	=>	$newNextInstallment
					);
					$inup=$db->updateAry("hostelinstallment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($given,$_REQUEST["student_id"],json_encode($insAry),"Hostel fee","Hostel fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"ip".$requestid);
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of Rs.".$given." successfully deposited as Hostel fee in ".$LinksDetails["site_name"],"hostel_add_fee");
				}elseif($given==$total){
					for($j=0;$j<=$i;$j++){	
						$insAry=array( "ip".($requestid+$j)	=>	$aryInstall["i".($requestid+$j)]);
						$inup=$db->updateAry("hostelinstallment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["i".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"Hostel fee","Hostel fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
						sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of Rs.".$given." successfully deposited as Hostel fee in ".$LinksDetails["site_name"],"fee_submit");
					}
				}else
				if($given>$total && $given<$nexttotal){
					for($j=0;$j<=$i;$j++){
						$insAry=array( "ip".($requestid+$j)	=>	$aryInstall["i".($requestid+$j)]);
						$inup=$db->updateAry("hostelinstallment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["i".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"Hostel fee","Hostel fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
						$currentTotal+=$aryInstall["i".($requestid+$j)];
					}
					$currentTotal." ";
					$newInstallment=$_REQUEST["given"]-$currentTotal;//." ".$aryInstall["i".($requestid+$j)]." ";
					$newNextInstallment=$aryInstall["i".($requestid+$i+1)]-$newInstallment+$aryInstall["i".($requestid+$i+2)];
					$insAry1=array( "ip".($requestid+$i+1)	=>	$newInstallment,
								   /*"i".($requestid+$i+1)	=>	$newInstallment,*/
								   "i".($requestid+$i+2)	=>	$newNextInstallment
					);
					//print_r($insAry1);
					$inup=$db->updateAry("hostelinstallment",$insAry1," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($newInstallment,$_REQUEST["student_id"],json_encode($insAry1),"Hostel fee","Hostel fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of Rs.".$given." successfully deposited as Hostel fee in ".$LinksDetails["site_name"],"fee_submit");
				}
				
			}
		}
		if($given<$total && $k==1 && $nexttotal==$total){
			echo "Please make intallment to add fee.";exit;
		}
		if($given>$total){
			echo "You can not make installment morethen ".$total;
		}
		//echo $given."/".$Payment."/".$total;
	}
}
elseif($ch=="busfeepayment"){
	$aryInstall=$db->getRow("select * from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
	if($_REQUEST["busgiven"]==""){
		echo "please enter fee";
	}elseif(!is_numeric($_REQUEST["busgiven"])){
		echo "fee must in numeric formet";
	}elseif($_REQUEST["busgiven"]==0){
		echo "fee must greater then 0";
	}else{
		$nextin="b".($_REQUEST["id"]+1);
		$requestid=$_REQUEST["id"];
		$given=$_REQUEST["busgiven"];//busgiven
		$total=0;$givenTotal=0;$k=0;
		for($i=0;$i<=14;$i++){
			if($aryInstall["b".($requestid+$i)]!=""){
				$k++;
				$total=$total+$aryInstall["b".($requestid+$i)];
				$nexttotal=$total+$aryInstall["b".($requestid+$i+1)];$currentTotal=0;$balanceTotal=0;
				//echo "$total && $given<$nexttotal";exit;
				if($given<$total && $i==0 && $nexttotal!=$total){
					$newInstallment=$total-$_REQUEST["busgiven"];
					$newNextInstallment=$newInstallment+$aryInstall["b".($requestid+1)];
					$insAry=array( "bp".($requestid)	=>	$given,
								"b".($requestid+1)	=>	$newNextInstallment
					);
					$inup=$db->updateAry("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($given,$_REQUEST["student_id"],json_encode($insAry),"bus","Student bus fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"bp".$requestid);
				}
				elseif($given==$total){
					for($j=0;$j<=$i;$j++){	
						$insAry=array( "bp".($requestid+$j)	=>	$aryInstall["b".($requestid+$j)]);
						$inup=$db->updateAry("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["b".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"bus","Student bus fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"bp".($requestid+$j));
					}
				}
				elseif($given>$total && $given<$nexttotal){
					
					for($j=0;$j<=$i;$j++){
						$insAry=array( "bp".($requestid+$j)	=>	$aryInstall["b".($requestid+$j)]);
						$inup=$db->updateAry("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["b".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"bus","Student bus fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"bp".($requestid+$j));
						$currentTotal+=$aryInstall["b".($requestid+$j)];
					}
					$currentTotal." ";
					$newInstallment=$_REQUEST["busgiven"]-$currentTotal;
					$newNextInstallment=$aryInstall["b".($requestid+$i+1)]-$newInstallment+$aryInstall["b".($requestid+$i+2)];
					$insAry1=array( "bp".($requestid+$i+1)	=>	$newInstallment,
								   "b".($requestid+$i+2)	=>	$newNextInstallment
					);
					$inup=$db->updateAry("installment",$insAry1," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($newInstallment,$_REQUEST["student_id"],json_encode($insAry1),"bus","Student fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"bp".($requestid+$j));
				}
				
			}
		}
		if($given<$total && $k==1 && $nexttotal==$total){
			echo "Please make intallment to add fee.";exit;
		}
		if($given>$total){
			echo "You can not make installment morethen ".$total;
		}
	}
}
elseif($ch=="feeother"){
	if($_REQUEST["fee"]==""){
		echo "please enter detail of other fee";
	}elseif($_REQUEST["given"]==""){
		echo "please enter fee";
	}elseif(!is_numeric($_REQUEST["given"])){
		echo "fee must in numeric formet";
	}elseif($_REQUEST["given"]==0){
		echo "fee must greater then 0";
	}else{
		$insAry=array(  "other".$_REQUEST["id"]."_detail"	=>	$_REQUEST["fee"],
						"other".$_REQUEST["id"]."_fee"	=>	$_REQUEST["given"],
						"status"				=>	"2");
		$inup=$db->updateAry("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
		if($inup>0){
		installment_time($_REQUEST["student_id"],"other".$_REQUEST["id"]."_detail");
		deposit($_REQUEST["given"],$_REQUEST["student_id"],$inup,"other".$_REQUEST["id"]."_fee","Student Other fee ".$_REQUEST["fee"]);
			echo "0";
		}else{
			echo $db->getErMsg().$db->getLastQuery();
		}
	}
}
elseif($ch=="CreateMarksheet"){
	$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
	if(count($detail)==0){
		echo '<div class="alert alert-error">
			  <a class="close" data-dismiss="alert">×</a>
			  <i class="icon icon-warning-sign"></i> <b>Notice</b> Please create Subject for this class</i>
			</div>';
	}else{
		$exam=$db->getRows("select * from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
		if(count($exam)==0){
			echo '<div class="alert alert-error">
			  <a class="close" data-dismiss="alert">×</a>
			  <i class="icon icon-warning-sign"></i> <b>Notice</b> Please create Exams for this class</i>
			</div>';
		}
		else{
	?>
    
	<div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Payment detail</h3>
               
            </div>
            <div class="well no-padding">
            <table class="table">
            	<thead>
                	<tr>
                    	<td>Subject</td>
                        <td>Total</td>
                        <td>Obtain</td>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                	<?php $overAllTotal=0;
					foreach($detail as $d){$overAllTotal=$overAllTotal+$d["total"];
					$feeDetail=$db->getRow("select * from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and subject='".$d["subject"]."' and type='".$_REQUEST["extype"]."' and session='".$_SESSION["admin"]["session"]."'");
						?>
                        <tr>
							<td><?php echo $d["subject"] ?></td>
                            <td><input type="text" name="total<?php echo $d["id"] ?>" class="total" value="<?php echo $feeDetail["total"] ?>" /></td>
                            <td><input type="text" name="obtail<?php echo $d["id"] ?>" class="obtain onlyZero" value="0" /></td>
                            <td></td>
                        </tr>
						<?php
					}
					?>
                </tbody>
                <tfoot>
                	<tr>
                        <td>Total</td>
                        <td><input type="text" name="alltotal" id="alltotal" value="<?php echo $overAllTotal; ?>" /></td>
                        <td><input type="text" name="allobtail" class="onlyZero" id="allobtain" value="0" /></td>
                        <td><input type="text" name="percetage" class="onlyZero" id="percetage" value="0" /></td>
                    </tr>
                </tfoot>
            </table>
            </div>
        </div>
   </div>
    
	<?php
		}
	}
}
elseif($ch=="NoOfExam"){
	$exam=$db->getVal("select id from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."' limit 0,1");
	if($_REQUEST["utype"]=="edit" || $exam!=""){
		$examtype=$db->getRows("select distinct(type) from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
		?>
		<form method="post" action="">
			<input type="hidden" name="class" value="<?php echo $_REQUEST["class"] ?>" />
			<div class="row-fluid">
				<div class="span12">
					<div class="top-bar">
						<h3><i class="icon-cog"></i> Exam type detail</h3>
						<div>
								<button type="submit" name="fee" onclick="createExam()" class="btn btn-primary"> Save</button>
						</div>
					</div>
					<div class="well no-padding">
					<table class="table">
					<tr>
						<td></td>
						<?php 
						
						if(is_array($examtype) && count($examtype)>0){
							foreach($examtype as $eytpe){ //echo "<pre>";print_r($eytpe);echo "</pre>";?>
							<td><input required onkeyup="SpaceReplace(this)" type="text" name="type[]" style="width:80px" class="total" value="<?php echo $eytpe[0] ?>" placeholder="Exam type" /></td>
						<?php }
						}?>
					</tr>
					<?php
					$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
					foreach($detail as $d){
					?><tr>
							<td><?php echo $d["subject"] ?><input type="hidden" name="subject[]" value="<?php echo $d["subject"] ?>" /></td>
							<?php 
							for($i=1; $i<=count($examtype);$i++){ ?>
							<td><input required type="text" style="width:80px" name="<?php echo $d["subject"];?>[]" class="total" value="<?php
							echo $db->getVal("select total from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."' and subject='".$d["subject"]."' and type='".$examtype[$i-1][0]."'")
							 ?>" placeholder="Total Maks" /><?php 
							 //print_r($examtype[$i-1][0]); echo $db->getLastQuery() ?></td>
							<?php } ?>
						</tr>
					<?php
					}
					?>
					</table>
				</div>
			</div>
			</div>
		</form>
		<?php
	}else{
		$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
		if(count($detail)==0){
			echo '<div class="alert alert-error">
				  <a class="close" data-dismiss="alert">×</a>
				  <i class="icon icon-warning-sign"></i> <b>Notice</b> Please create Subject for this class</i>
				</div>';
		}
		else{
			?><form method="post" action="">
			<input type="hidden" name="class" value="<?php echo $_REQUEST["class"] ?>" />
			<div class="row-fluid">
				<div class="span12">
					<div class="top-bar">
						<h3><i class="icon-cog"></i> Exam type detail</h3>
						<div>
								<button type="submit" name="fee" onclick="createExam()" class="btn btn-primary"> Save</button>
						</div>
					</div>
					<div class="well no-padding">
					<table class="table">
					<tr>
						<td></td>
						<?php for($i=1; $i<=(int)$_REQUEST["noexam"];$i++){ ?>
							<td><input required onkeyup="SpaceReplace(this)" type="text" name="type[]" style="width:80px" class="total" value="" placeholder="Exam type" /></td>
						<?php } ?>
					</tr>
					<?php
					foreach($detail as $d){
						$subj=str_replace(" ","_",str_replace("+","~",str_replace("&","and",$d["subject"])));
						?>
						<tr>
							<td><?php echo $d["subject"] ?><input type="hidden" name="subject[]" value="<?php echo $d["subject"] ?>" /></td>
							<?php for($i=1; $i<=(int)$_REQUEST["noexam"];$i++){ ?>
							<td><input required type="text" style="width:80px" name="<?php echo $subj;?>[]" class="total onlyZero" value="0" placeholder="Total Maks" /></td>
							<?php } ?>
						</tr>
						<?php
					}
					?>
					</table>
				</div>
			</div>
			</div>
			</form>
		<?php
		}
	}
}
elseif($ch=="exmtype"){
	$detail=$db->getRows("select DISTINCT type from examtype where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
	?><select name="type" class="span6" id="type" style="float: right;">
    <option value="">Exam type</option><?php
	if(is_array($detail) && count($detail)>0){
		foreach($detail as $ds){
			?><option><?php echo $ds["type"] ?></option><?php	
		}
	}
	?></select><?php
}
?>