<?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"];$status="";
if($ch=="PreCheckExist"){
	$detail=$db->getRow("select * from student where status=1 and  school_id='".$_SESSION["admin"]["school_id"]."' and name='".$_REQUEST["name"]."' and fname='".$_REQUEST["fname"]."' and dob='".$_REQUEST["dob"]."'");
	if(is_array($detail) && count($detail)>0){
		echo "user already register on ".$detail["session"]." session in class ".$detail["class"];
	}else{
		echo 0;
	}
}
elseif($ch=="getsibling"){
	$detail=$db->getRow("select * from student where status=1 and  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 id='".$_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_bus_installment"){
	$newUpdateFee=$_REQUEST["fee"]/$_REQUEST["id"];
	$NewAry==array();
	for($i=0;$i<$_REQUEST["id"];$i++){
		$busins="b".(int)($_REQUEST["instalment_no"]+$i);
		$NewAry[$busins]=$newUpdateFee;
	}
	$inup=$db->updateAry("installment",$NewAry,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_REQUEST["student_id"]);
	if($inup!="")	echo 0;	else echo $db->getErMsg();
}
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("installment",$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 installment 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;
		$bpay="bp".$i;$bp="b".$i;$feePayment=0;$busPayment=0;
		if($installDeatail[$ipay]=="" && trim($installDeatail[$ip])!="" && trim($installDeatail[$ip])!="0" ){
			$completeAry[$ipay]=$installDeatail[$ip];
			$feePayment=(int)$feePayment+(int)$installDeatail[$ip];
		}
		if($installDeatail[$bpay]=="" && trim($installDeatail[$bp])!="" && trim($installDeatail[$bp])!="0" ){
			$completeAry[$bpay]=$installDeatail[$bp];
			$busPayment=(int)$busPayment+(int)$installDeatail[$bp];
		}
	}
	
	$inup=$db->updateAry("installment",$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,"fee","Direct Complete student fee Payment in single time");
		deposit($busPayment,$_REQUEST["student_id"],$inup,"bus","Direct Complete student Bus Payment in single time");echo 0;	
		sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Complete Fee successfully submitted as Bus & Tution fee in ".$LinksDetails["site_name"],"fee_submit");
	}
	else echo $db->getErMsg();
}
elseif($ch=="feepayment"){
	$aryInstall=$db->getRow("select * from installment 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;
		$reff_receipt=array("amount"=>$_REQUEST["given"],
							"type"=>"fee",
							"idno"=>$_REQUEST["student_id"],
							"session"=>$_SESSION["admin"]["session"]
							);
		$inst_id=array();
		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("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($given,$_REQUEST["student_id"],json_encode($insAry),"fee","Student fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"ip".$requestid);
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Tution fee in ".$LinksDetails["site_name"],"fee_submit");
					$inst_id[]=$requestid;
				}elseif($given==$total){
					for($j=0;$j<=$i;$j++){	
						$insAry=array( "ip".($requestid+$j)	=>	$aryInstall["i".($requestid+$j)]);
						$inup=$db->updateAry("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["i".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"fee","Student fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
						$inst_id[]=$requestid+$j;
					}
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Tution 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("installment",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
						deposit($aryInstall["i".($requestid+$j)],$_REQUEST["student_id"],json_encode($insAry),"fee","Student fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
						$currentTotal+=$aryInstall["i".($requestid+$j)];
						$inst_id[]=$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("installment",$insAry1," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
					deposit($newInstallment,$_REQUEST["student_id"],json_encode($insAry1),"fee","Student fee Payment in installment type");
					$inst_id[]=$requestid+$j;
					installment_time($_REQUEST["student_id"],"ip".($requestid+$j));
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Tution fee in ".$LinksDetails["site_name"],"fee_submit");
				}
				
			}
		}
		if(is_array($inst_id) && count($inst_id)>0){
			$reff_receipt["inst_id"]=implode(",",$inst_id);
			$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
			$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			$db->insertAry("reff_receipt",$reff_receipt);
		}
		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;
		$reff_receipt=array("amount"=>$_REQUEST["busgiven"],
							"type"=>"busfee",
							"idno"=>$_REQUEST["student_id"],
							"session"=>$_SESSION["admin"]["session"]
							);
		$inst_id=array();
		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");
					$inst_id[]=$requestid;
					installment_time($_REQUEST["student_id"],"bp".$requestid);
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Bus fee in ".$LinksDetails["site_name"],"fee_submit");
				}
				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));
						sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Bus fee in ".$LinksDetails["site_name"],"fee_submit");
						$inst_id[]=$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");
						$inst_id[]=$requestid+$j;
						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));
					$inst_id[]=$requestid+$j;
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as Bus fee in ".$LinksDetails["site_name"],"fee_submit");
				}
				
			}
		}
		if(is_array($inst_id) && count($inst_id)>0){
			$reff_receipt["inst_id"]=implode(",",$inst_id);
			$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
            $reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			$db->insertAry("reff_receipt",$reff_receipt);//echo $db->getErMsg();exit;
		}
		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 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(  "ins_name"	=>	$_REQUEST["id"]."-otherfee",
						"fine"	=>	$_REQUEST["given"],
						"reasion"	=>	$_REQUEST["reasion"],
						"status"				=>	"1",
						"idno"	=>		$_REQUEST["student_id"]
						
					);
					$insAry["school_id"]=$_SESSION["admin"]["school_id"];
                    $insAry["added_by"]=$_SESSION["admin"]["uid"];
		$inup=$db->insertAry("installment_fine",$insAry);
		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"]);
		sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of rs.".$_REQUEST["given"]." is successfully deposited as ".$_REQUEST["fee"]." in ".$LinksDetails["site_name"],"fee_submit");
			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"]."' order by id");
	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"]."' order by id");
						?>
                        <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
}
elseif($ch=="sendsmsexam"){
	$dataAry=array();
	$data=explode("<br/>",$_REQUEST["AllData"]);
	foreach($data as $d){
		$exData=explode("~",$d);
		$dataAry[$exData[0]][$exData[1]]=$exData[2]."/".$exData[3];
	}
	foreach($dataAry as $key=>$value){
		$mobile=mydetail("mobile",$key,"student");
		$data="";
		foreach($value as $subject=>$detail){
			$data.=$subject." : ".$detail."\n";
		}
		sms($mobile,"Dear Parents, result of ".$_REQUEST["examtype"]." Declare \n".$data,"send_markshhet_accourding");
		
	}
	echo "sms Send successfully";
}
elseif($ch=="smsBirthday"){
	sms($_REQUEST["no"],"Dear ".mydetail("name",$_REQUEST["id"],"student").", ".$LinksDetails["site_name"]." wishes Happy birthday to you may God Blesh you and fullfill All your Wishes","birth_day_sms");
	echo "send successfully";
}

elseif($ch=="completeFeeShowSMS"){
	
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that complete acadmic fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that complete acadmic fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that complete acadmic fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}elseif($ch=="SchoolFeeShowSMS"){
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Student fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Student fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Student fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}elseif($ch=="BusFeeShowSMS"){
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Bus fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Bus fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that  Bus fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}elseif($ch=="SchoolFineFeeShowSMS"){
	//print_r($_REQUEST);exit;
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Student Fine fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Student fine fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Sctudent Fine fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}elseif($ch=="BusFineFeeShowSMS"){
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Bus Fine fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Bus Fine fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Bus Fine fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}elseif($ch=="OtherFineFeeShowSMS"){
	if($_REQUEST["class"]!="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Other fee of class ".$_REQUEST["class"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else
	if($_REQUEST["fdate"]=="0"){
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Other fee of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}else{
		$status=sms($LinksDetails["DirectorNumber"],"Dear Sir, It is to inform you that Other fee from ".$_REQUEST["fdate"]."-".$_REQUEST["fmonth"]."-".$_REQUEST["fyear"]." to ".$_REQUEST["tdate"]."-".$_REQUEST["tmonth"]."-".$_REQUEST["tyear"]." of your school is RS".$_REQUEST["fee"],"fee_submit");	
	}
	echo $status;
}

elseif($ch=="school_outstanding"){
	$numbers=explode(",",$_REQUEST["fee"]);
	if(is_array($numbers) && count($numbers)>0){
		foreach($numbers as $num){
			$Alldetail=explode("-",$num);
			$page=URL_ADMIN_HOME."?page_id=fee_summery&school_outstanding=1&mobile=".$Alldetail[0];
			$ch=$db->getVal("select id from sms where school_id='".$_SESSION["admin"]["school_id"]."' and MONTH(dtime) = MONTH(CURDATE()) and page='".$page."'");
			if($ch==""){
				//$status.="Send sms to ".$Alldetail[0]." Success \n".$db->getLastQuery();
				$SMSdetail="Dear Parent, Total School fee is Rs ".$Alldetail[1]." and Total Paid amount is Rs ".$Alldetail[2]." so Remaining Due Amount to be paid is Rs ".$Alldetail[3].". Thanks Regard ".$LinksDetails["site_name"]."\n";
				$status.=sms($Alldetail[0],$SMSdetail,"fee_submit",$page);	
			}else{
				$status.="Send sms to ".$Alldetail[0]." on next month";
			}			
		}
	}
	echo $status;
}elseif($ch=="bus_outstanding"){
	$numbers=explode(",",$_REQUEST["fee"]);
	if(is_array($numbers) && count($numbers)>0){
		foreach($numbers as $num){
			$Alldetail=explode("-",$num);
			$page=URL_ADMIN_HOME."?page_id=fee_summery&bus_outstanding=1&mobile=".$Alldetail[0];
			$ch=$db->getVal("select id from sms where school_id='".$_SESSION["admin"]["school_id"]."' and MONTH(dtime) = MONTH(CURDATE()) and page='".$page."'");
			if($ch==""){
				//$status.="Send sms to ".$Alldetail[0]." Success \n".$db->getLastQuery();
				$SMSdetail="Dear Parent, Total bus fee is Rs ".$Alldetail[1]." and Total Paid amount is Rs ".$Alldetail[2]." so Remaining Due Amount to be paid is Rs ".$Alldetail[3].". Thanks Regard ".$LinksDetails["site_name"]."\n";
				$status.=sms($Alldetail[0],$SMSdetail,"fee_submit",$page);	
			}else{
				$status.="Send sms to ".$Alldetail[0]." on next month";
			}			
		}
	}
	echo $status;
}
elseif($ch=="getStudentlist"){
	$sList=$db->getRows("select * from student where status=1 and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["class"]."' and issue_TC=0 order by name");
	?>
	<select name="otype" id="otype">
        <option <?php if(""==$_REQUEST["otype"])echo 'selected';?> value="">All Student</option>
        <option <?php if("new"==$_REQUEST["otype"])echo 'selected';?> value="new">New Student</option>
        <option <?php if("old"==$_REQUEST["otype"])echo 'selected';?> value="old">Old Student</option>
        <optgroup label="Student list" style="color:#F00">
        <?php 
		if(is_array($sList) && count($sList)>0){
			foreach($sList as $List){
				?>
				<option <?php if($List["idno"]==$_REQUEST["otype"])echo 'selected';?> value="<?php echo $List["idno"] ?>">
                <?php echo $List["name"] ?>
                </option>
				<?php
			}
		}
		?>
        </optgroup>
    </select>
	<?php
}elseif($ch=="uploadToProceed"){
	if($LinksDetails["website"]==""){
		echo "please enter website name from setting";	
	}else{
		$tableData=backup_tables();
		$filename="database/sql.sql";
		if(!file_exists($filename)){
			add($filename);
			Write($filename , $tableData);
		}else{
			Write($filename , $tableData);
		}
		
		
		set_time_limit(300);
		$server_path = "/database/";
		$file_tmp_name = $filename;
		$ftp_server = "in15.hostgator.in";
		$ftp_username = "mss@technosofttechnology.com";
		$ftp_password = "Mragank@123";
		$file_name = "sql.sql";
		
		// Create FTP stream, a connection to the FTP server
		$connection = ftp_connect($ftp_server);
		
		// Login to the server
		$ftp_conn = ftp_login($connection, $ftp_username, $ftp_password);
		
		// Login check
		if ((!$connection) || (!$ftp_conn)) {
			echo "<p>Connection to <b>$ftp_server</b> for user<b>$ftp_username</b> FAILED</p>"; exit;
		} else {
			// Upload file to specified path
			$ftp_upload = ftp_put($connection,
			str_replace("//","/",$server_path."/".$file_name),
			$file_tmp_name, FTP_BINARY);					
			// Close FTP connection
			ftp_close($ftp_conn);
		}
		
		
		
		$url=$LinksDetails["website"]."/localuplaod.php";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,
					http_build_query(array('query' => $file_name,"status"=>"1")));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$server_output = curl_exec ($ch);	
		curl_close ($ch);
		echo $server_output;
	}
}elseif($ch=="getOtherStudentlist"){
	$sList=$db->getRows("select * from student where status=1 and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["class"]."' and issue_TC=0 order by name");
	?>
	<select name="idno" id="idno">
        <option <?php if(""==$_REQUEST["idno"])echo 'selected';?> value="">Select Student</option>
        <?php 
		if(is_array($sList) && count($sList)>0){
			foreach($sList as $List){
				?>
				<option <?php if($List["idno"]==$_REQUEST["idno"])echo 'selected';?> value="<?php echo $List["idno"] ?>"><?php echo $List["name"] ?>
                </option>
				<?php
			}
		}
		?>
    </select>
	<?php
}elseif($ch=="getOtherStudentFee"){
	?>
	<select name="other_id" id="other_id">
        <option <?php if(""==$_REQUEST["other_id"])echo 'selected';?> value="">select Other Fee</option>
       <?php 
			$class=$db->getRows("select * from otherfee_structure where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and (otype='' or otype='old' or otype='new')");
			foreach($class as $cla){
				?>
				<option <?php if($cla["id"]==$_REQUEST["other_id"])echo 'selected';?> value="<?php echo $cla["id"] ?>">
					<?php echo $cla["name"] ?>
				</option>
				<?php
			}
		?>
    </select>
	<?php
}elseif($ch=="makeadmission_data"){
	$where="";
	if(isset($_REQUEST["class"]))$where=" AND class='".$_REQUEST["class"]."'";
	$sql_limit="select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where and status=1 $where and issue_TC=0 order by name";
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("page_id"=>"make_admission","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
		$aryData='{ "aaData": [
		';
	    $i=0;
		foreach($aryList as $iList)
		{
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			$aryData.='["'.$i.'",';
			$aryData.='"'.$iList['name'].'",';
			$aryData.='"'.$iList['class'].'",';
			$aryData.='"'.$iList['medium'].'",';
			$aryData.='"'.$iList['gender'].'",';
			$aryData.='"'. $db->getVal("select root from bus where id=".$clist["bus"]).'",';
			$aryData.='"'.$iList["cast"].'",';
			$aryData.='"'.$iList['fname'].'",';
			$aryData.='"'.$iList['mobile'].'",';
			$aryData.='"';
			$aryPgAct['action']="edit";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Edit</button></a>';
			$aryPgAct['action']="delete";
			$aryData.='<a href=\'javascript:;\' onclick=\'del(\"'.URL_ADMIN_HOME.getQueryString($aryPgAct).'\")\' class=\'confirmation\'><button type=\'button\' class=\'btn btn-danger\'>Delete</button></a>';
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }
		 $aryData.="] }";	
		 echo $aryData;	
	}else{
		echo $db->getLastQuery().$db->getErMsg();
	}
	
		
}
elseif($ch=="payfee_data"){
		$where="";
	if(isset($_REQUEST["class"]))$where=" AND class='".$_REQUEST["class"]."'";
	$sql_limit="select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where and status=1 $where and issue_TC=0 order by name ";
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("page_id"=>"pay_fee","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
	   $i=0;
	   $aryData='{ "aaData": [
		';
		foreach($aryList as $iList)
		{   
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			$aryData.='["'.$i.'",';
			$aryData.='"'.$iList['name'].'",';
			$aryData.='"'.$iList['class'].'",';
			$aryData.='"'.$iList['gender'].'",';
			$aryData.='"'.$iList["cast"].'",';
			$aryData.='"'.$iList['fname'].'",';
			$aryData.='"'.$iList['mobile'].'",';
			$aryData.='"';
			$aryPgAct['action']="add";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>PayFee</button></a>';
			
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }		
	
		 $aryData.="] }";	
		 echo $aryData;	
	}
	else{
		echo $db->getLastQuery().$db->getErMsg();
	}
	
		
}
elseif($ch=="feeRecord_data"){
	$where="";
	if(isset($_REQUEST["class"]))$where=" AND class='".$_REQUEST["class"]."'";
	$sql_limit="select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' $where and status=1 $where and issue_TC=0 order by name";
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("page_id"=>"fee_record","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
	   $i=0;
	   $aryData='{ "aaData": [
		';
		foreach($aryList as $iList)
		{   
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			$aryData.='["'.$i.'",';
			$aryData.='"'.$iList['name'].'",';
			$aryData.='"'.$iList['class'].'",';
			$aryData.='"'.$iList['gender'].'",';
			$aryData.='"'.$iList["cast"].'",';
			$aryData.='"'.$iList['fname'].'",';
			$aryData.='"'.$iList['mobile'].'",';
			$aryData.='"';
			$aryPgAct['action']="view_fees";
			$aryData.='<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>View Fees</button></a>';
			
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }		
	
		 $aryData.="] }";	
		 echo $aryData;	
	}
	else{
		echo $db->getLastQuery().$db->getErMsg();
	}
	
		
}

elseif($ch=='DATE_SEARCH')
{
	
	$aryList=$db->getRows("SELECT * FROM sms where school_id='".$_SESSION["admin"]["school_id"]."' and YEAR(dtime)=".date('Y',strtotime($_REQUEST['date']))." and  MONTH(dtime)=".date('m',strtotime($_REQUEST['date']))." and DAY(dtime)=".date('d',strtotime($_REQUEST['date']))."");
	
 
	if(is_array($aryList) && count($aryList)>0)
	{ 
		
		$i=0;
		
			 foreach($aryList as $clist){ $i++;
			 ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $clist["mobile"] ?></td>
                    <td><?php echo $clist["dtime"] ?></td>
                    <td><?php echo $clist["msg"] ?></td>
                    <td><div style="max-width:150px; overflow:hidden"><?php echo "<span class='label'>".strip_tags($clist["status"])."</span>";?></div></td>
                </tr>
        <?php } 
	}
	else
	{
	    echo "No record found";
	}
}