<?php
include("./db.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" style="border-bottom:2px solid #F00">
            <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=="hostelinstallment"){ 
		$busDetail=$db->getRow("select fee,installment from hostel where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_REQUEST["hostel"]."'");
		$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="hostelfee<?php echo $i; ?>">Hostel Fee <?php echo $i; ?></label>
					<div class="controls">
						<input type="text" readonly="readonly" id="hostelfee<?php echo $i; ?>" name="hostelfee[]" class="input-block-level hostelfeeInsCharge" value="<?php echo number_format($perFee, 2, '.', ''); ?>">
					</div>
				</div>
				<?php
			}
		}
		?>
		<div class="control-group">
            <label class="control-label" for="hostelfeetotal">Total</label>
            <div class="controls">
                <input readonly type="text" id="hostelfeetotal" name="hostelfeetotal" class="input-block-level" value="">
            </div>
        </div>
        <div class="control-group" style="border-bottom:2px solid #F00">
            <label class="control-label" for="hosteldiscount">Discount</label>
            <div class="controls">
                <input type="text" onkeyup="hostelupdateIns()" id="hosteldiscount" name="hosteldiscount" class="input-block-level" value="0">
                <input type="hidden" id="hosteloldtotal" 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_hostel_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=="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",
							"date"=>$_REQUEST["udate"],
							"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,$_REQUEST["udate"]);
					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),$_REQUEST["udate"]);
						$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),$_REQUEST["udate"]);
						$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),$_REQUEST["udate"]);
					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"){
	//echo $_REQUEST["udate"];exit;
	$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"],
							"date"=>$_REQUEST["udate"],
							"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,$_REQUEST["udate"]);
					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),$_REQUEST["udate"]);
						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),$_REQUEST["udate"]);
						$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),$_REQUEST["udate"]);
					$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=="hostelfeepayment"){
	//echo $_REQUEST["udate"];exit;
	$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."'");
	if($_REQUEST["hostelgiven"]==""){
		echo "please enter fee";
	}elseif(!is_numeric($_REQUEST["hostelgiven"])){
		echo "fee must in numeric formet";
	}elseif($_REQUEST["hostelgiven"]==0){
		echo "fee must greater then 0";
	}else{
		$nextin="i".($_REQUEST["id"]+1);
		$requestid=$_REQUEST["id"];
		$given=$_REQUEST["hostelgiven"];//busgiven
		$total=0;$givenTotal=0;$k=0;
		$reff_receipt=array("amount"=>$_REQUEST["hostelgiven"],
							"date"=>$_REQUEST["udate"],
							"type"=>"hostelfee",
							"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 && $given<$nexttotal";exit;
				if($given<$total && $i==0 && $nexttotal!=$total){
					$newInstallment=$total-$_REQUEST["hostelgiven"];
					$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","Student hostel fee Payment in installment type");
					$inst_id[]=$requestid;
					installment_time($_REQUEST["student_id"],"hp".$requestid,$_REQUEST["udate"]);
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as hostel fee in ".$LinksDetails["site_name"],"fee_submit");
				}
				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","Student hostel fee Payment in installment type");
						installment_time($_REQUEST["student_id"],"hp".($requestid+$j),$_REQUEST["udate"]);
						sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as hostel fee in ".$LinksDetails["site_name"],"fee_submit");
						$inst_id[]=$requestid+$j;
					}
				}
				elseif($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","Student hostel fee Payment in installment type");
						$inst_id[]=$requestid+$j;
						installment_time($_REQUEST["student_id"],"hp".($requestid+$j),$_REQUEST["udate"]);
						$currentTotal+=$aryInstall["i".($requestid+$j)];
					}
					$currentTotal." ";
					$newInstallment=$_REQUEST["hostelgiven"]-$currentTotal;
					$newNextInstallment=$aryInstall["i".($requestid+$i+1)]-$newInstallment+$aryInstall["i".($requestid+$i+2)];
					$insAry1=array( "ip".($requestid+$i+1)	=>	$newInstallment,
								   "i".($requestid+$i+2)	=>	$newNextInstallment
					);
					$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","Student fee Payment in installment type");
					installment_time($_REQUEST["student_id"],"hp".($requestid+$j),$_REQUEST["udate"]);
					$inst_id[]=$requestid+$j;
					sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as hostel 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"],
					'udate'=>$_REQUEST["udate"],
					"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",$_REQUEST["udate"]);
			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");
			$reff_receipt=array("amount"=>$_REQUEST["given"],
							"type"=>$_REQUEST["reasion"],
							"idno"=>$_REQUEST["student_id"],
							"date"=>$_REQUEST["udate"],
							"session"=>$_SESSION["admin"]["session"]
							);
			$reff_receipt["inst_id"]=$inup;
			$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;
			
			echo "0";
		}else{
			echo $db->getErMsg().$db->getLastQuery();
		}
	}
	
}
elseif($ch=="extrapayment"){
	//echo $_REQUEST["udate"];exit;
	$aryInstall=$db->getRow("select * from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$_REQUEST["id"]."' and type='".$_REQUEST["typeid"]."'");
	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{
		$requestid=$_REQUEST["id"];
		$given=$_REQUEST["given"];//busgiven
		$total=$aryInstall["fee"];
		$reff_receipt=array("amount"=>$_REQUEST["given"],
							"date"=>$_REQUEST["udate"],
							"type"=>"extrafee".$_REQUEST["typeid"],
							"idno"=>$_REQUEST["student_id"],
							"session"=>$_SESSION["admin"]["session"]
							);
		$inst_id=array();
		if($given<$total){
			$nextin="p".($requestid+1);
			$newInstallment=$total-$given;
			$oldNewTotFee=$db->getVal("select fee from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='".$nextin."' and type='".$_REQUEST["typeid"]."'");
			$newNextInstallment=$newInstallment+$oldNewTotFee;
			
			$in=$db->updateAry("extrafee",array( "fee"	=>	$newNextInstallment)," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='".$nextin."' and type='".$_REQUEST["typeid"]."'");
			$insAryJ=array( "pay".($requestid+1)	=>	$given);
			$insAry=array( "pay"	=>	$given);
			$inup=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$requestid."' and type='".$_REQUEST["typeid"]."'");
			$feetype=$db->getVal("select type from extrafee_type where id='".$_REQUEST["typeid"]."'");
			deposit($given,$_REQUEST["student_id"],json_encode($insAryJ),$feetype,"Student ".$feetype." fee Payment in installment type");
			$inst_id[]=$requestid;
			installment_time($_REQUEST["student_id"],"ext-".$_REQUEST["typeid"]."-".$requestid,$_REQUEST["udate"]);
			sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as ".$feetype." 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;
			}
			
		}
		elseif($total==$given){			
			$insAryJ=array( "pay".$requestid	=>	$given);
			$insAry=array( "pay"	=>	$given);
			$inup=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$requestid."' and type='".$_REQUEST["typeid"]."'");
			$feetype=$db->getVal("select type from extrafee_type where id='".$_REQUEST["typeid"]."'");
			deposit($given,$_REQUEST["student_id"],json_encode($insAryJ),$feetype,"Student ".$feetype." fee Payment in installment type");
			$inst_id[]=$requestid;
			installment_time($_REQUEST["student_id"],"ext-".$_REQUEST["typeid"]."-".$requestid,$_REQUEST["udate"]);
			sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as ".$feetype." 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;
			}
		}
		elseif($given>$total){
			//echo "You can not make installment morethen ".$total;exit;
			$dataA=array();
			for($i=$requestid;$i<=11;$i++){
				$aryInstallment=$db->getRow("select * from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$i."' and type='".$_REQUEST["typeid"]."'");
				if($given>=$aryInstallment["fee"]){
					$insAryJ=array( "pay".$i	=>	$aryInstallment["fee"]);
					$insAry=array( "pay"	=>	$aryInstallment["fee"]);
					$inup=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$i."' and type='".$_REQUEST["typeid"]."'");
					$feetype=$db->getVal("select type from extrafee_type where id='".$_REQUEST["typeid"]."'");
					deposit($aryInstallment["fee"],$_REQUEST["student_id"],json_encode($insAryJ),$feetype,"Student ".$feetype." fee Payment in installment type");
					$inst_id[]=$i;
					installment_time($_REQUEST["student_id"],"ext-".$_REQUEST["typeid"]."-".$i,$_REQUEST["udate"]);
				}elseif($given>0){
					$nextin="p".($i+1);
					$newInstallment=$aryInstallment["fee"]-$given;
					$oldNewTotFee=$db->getVal("select fee from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='".$nextin."' and type='".$_REQUEST["typeid"]."'");
					$newNextInstallment=$newInstallment+$oldNewTotFee;
					if($oldNewTotFee==0 || $oldNewTotFee==""){
						$in=$db->insertAry("extrafee",array( "fee"	=>	$newNextInstallment,"pay"=>0,"school_id"=>$_SESSION["admin"]["school_id"],"idno"=>$_REQUEST["student_id"],"pid"=>$nextin,"type"=>$_REQUEST["typeid"],"added_by"=>$_SESSION["admin"]["added_by"]));
					}else{
						$in=$db->updateAry("extrafee",array( "fee"	=>	$newNextInstallment)," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='".$nextin."' and type='".$_REQUEST["typeid"]."'");
					}
					$insAryJ=array( "pay".($i)	=>	$given);
					$insAry=array( "pay"	=>	$given);
					$inup=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["student_id"]."' and pid='p".$i."' and type='".$_REQUEST["typeid"]."'");
					$feetype=$db->getVal("select type from extrafee_type where id='".$_REQUEST["typeid"]."'");
					deposit($given,$_REQUEST["student_id"],json_encode($insAryJ),$feetype,"Student ".$feetype." fee Payment in installment type");
					$inst_id[]=$i;
					installment_time($_REQUEST["student_id"],"ext-".$_REQUEST["typeid"]."-".$requestid,$_REQUEST["udate"]);
				}
				$given=$given-$aryInstallment["fee"];
			}
			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;
				sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$given." successfully submitted as ".$feetype." fee in ".$LinksDetails["site_name"],"fee_submit");
			}
			echo $db->getErMsg();
		}
	}
}
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"]."' order by id");
		?>
		<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"]."' order by id");
					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"]."' 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{
			?><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"]."' order by id");
	?><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 school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_REQUEST["class"]."' and issue_TC=0 and status=1 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"]." - (".$List['fname'].")" ?>
                </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"]." - (".$List['fname'].")"; ?>
                </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.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['class']).'",';
			$aryData.='"'.POST($iList['medium']).'",';
			$aryData.='"'.POST($iList['admissionno']).'",';
			$aryData.='"'.POST($db->getVal("select root from bus where id=".$iList["bus"])).'",';
			$aryData.='"'.POST($iList["cast"]).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($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.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['class']).'",';
			$aryData.='"'.POST($iList['admissionno']).'",';
			$aryData.='"'.POST($iList["cast"]).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($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 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.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['class']).'",';
			$aryData.='"'.POST($iList['admissionno']).'",';
			$aryData.='"'.POST($iList["cast"]).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($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=="idcard_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"]."' AND status=1 and issue_TC=0 $where order by name";;
	$aryList=$db->getRows($sql_limit);
	
	$aryPgAct=array("page_id"=>"idCard","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.='"<img src=\''.URL_ROOT.'media/photo/'.$iList['photo'].'\' height=\'36px\' width=\'36px\'>",';
			$aryData.='"'.POST($iList['name']).'",';
			$aryData.='"'.POST($iList['class']).'",';
			$aryData.='"'.POST($iList['medium']).'",';
			$aryData.='"'.POST($iList['fname']).'",';
			$aryData.='"'.POST($iList['mobile']).'",';
			$aryData.='"';
			$aryPgAct['action']="issue";
			$aryData.='<a href=\''.URL_ROOT.'print/?page_id=idcard&id='.$iList['idno'].'\'  target=\'_blank\' data-class=\'idcard\'><button type=\'button\' class=\'btn btn-info\'>Issue ID Card</button></a>';
			$aryPgAct['action']="issue";
			$aryData.='<a href=\''.URL_ROOT.'print/?page_id=admitcard&id='.$iList['idno'].'\'  target=\'_blank\' data-class=\'idcard\'><button type=\'button\' class=\'btn btn\'>Admit Card</button></a>';
			$aryData.='<a href=\'javascript:;\' onclick=\'takePhoto(\"'.$iList['idno'].'\")\'  target=\'_blank\' data-class=\'idcard\'><button type=\'button\' class=\'btn btn-info\'>Take Photo</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";
	}
}
elseif($ch=="DeleteInstallment"){
	//echo $_REQUEST["paytype"];exit;
	if($_REQUEST["paytype"]=="otherfee"){
		$detail=$_REQUEST["install"]."-otherfee";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."' and reasion='".$_REQUEST["detail"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			$detail2="other".$_REQUEST["install"]."_detail";
			$where1="where ins_name='".$detail2."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
								$db->delete("installment_time",$where1);
			//echo $db->getLastQuery();
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}
	}
	
	elseif($_REQUEST["paytype"]=="classfinefee"){
		$detail=$_REQUEST["install"]."-class";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}
	}
	elseif($_REQUEST["paytype"]=="hostelfinefee"){
		$detail=$_REQUEST["install"]."-hostel";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}
	}
	elseif($_REQUEST["paytype"]=="busfinefee"){
		$detail=$_REQUEST["install"]."-bus";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}
	}
	elseif($_REQUEST["paytype"]=="extrafinefee"){
		$detail=$_REQUEST["install"]."-extra".$_REQUEST["refid"];
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}
		//echo $db->getLastQuery();
	}
	elseif($_REQUEST["paytype"]=="otherfinefee"){
		$detail=$_REQUEST["install"]."-other";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}	
	}
	elseif($_REQUEST["paytype"]=="extraotherfinefee"){
		$detail=$_REQUEST["install"]."-otherfee";
		$where="where ins_name='".$detail."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."' and fine='".$_REQUEST["amount"]."'";
		$res=$db->delete("transaction","where tid=(select id from installment_fine $where)");
		if(!is_null($res)){
			$db->delete("installment_fine",$where);
			$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["detail"]."' and session='".$_SESSION["admin"]["session"]."'");
			echo $db->getErMsg();echo "1";
		}else{
			echo $db->getLastQuery();
		}	
	}
	elseif($_REQUEST["paytype"]=="hostelfee"){
		$alltransection=$db->getRows("select id,tid from transaction where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and rid='".$_REQUEST["idno"]."'");
		if(is_array($alltransection) && count($alltransection)>0){
			foreach($alltransection as $tran){
				$data=json_decode($tran["tid"],true);
				//print_r($data);continue;
				if(is_array($data) && count($data)>0){
					foreach($data as $key=>$value){
						$iip="ip".$_REQUEST["ip"];$ip="i".$_REQUEST["ip"];$type="hostelfee";
						if($iip==$key){
							$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
							//echo $_REQUEST["amount"]."==".$aryInstall[$ip]."\n".$iip."==".$key;continue;
							if($_REQUEST["amount"]==$aryInstall[$ip]){
								$db->delete("transaction","where id='".$tran["id"]."'");
								$db->updateAry("hostelinstallment",array($iip=>""),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
								$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='$type' and session='".$_SESSION["admin"]["session"]."' and inst_id='".$_REQUEST["ip"]."'");
							if($type="hostelfee"){
								$detail1="hp".$_REQUEST['ip'];
								$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
								$db->delete("installment_time",$where1);
							}
								
							}else{
								$amount=$_REQUEST["amount"];
								//echo $amount;exit;
								$inst_id=array();
								for($i=$_REQUEST["ip"];$i<=15;$i++){
									$ip="ip".$i;$p="i".$i;$t="fee";
									//echo $amount;exit;
									if($amount>0){
										$inst_id[]=$i;
										$tid=$db->getVal("select id from transaction where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and rid='".$_REQUEST["idno"]."' and tid like '%".$ip."%'");
										$db->delete("transaction","where id='".$tid."'");
										
										if($aryInstall[$p]==$aryInstall[$ip]){
											$db->updateAry("hostelinstallment",array($ip=>""),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
										}else{
											$j=(int)($i+1);
											$nip="ip".$j;$np="i".$j;
											$netAmt=abs($aryInstall[$np]+$aryInstall[$ip]-$aryInstall[$p]);
											$nextp=$np;
											$db->updateAry("hostelinstallment",array($ip=>"",$nextp=>$netAmt),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
										}
										$amount=abs($amount-$aryInstall[$ip]);
									}
								}
								$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='$type' and session='".$_SESSION["admin"]["session"]."' and inst_id='".implode(",",$inst_id)."'");
							}
						foreach($inst_id as $ins1){
							if($type="hostelfee"){
								$detail1="hp".$ins1;
								$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
								$db->delete("installment_time",$where1);
							}
						}
							//sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$_REQUEST["amount"]." has been successfully returned in ".$LinksDetails["site_name"],"fee_submit");
						}echo "1";
					}
				}
				else{
					echo "1";exit;
				}
			}
		}
	}
	else{
		$payHere=0;
		$extrafee_type=$db->getRows("select id,type from extrafee_type where status=1");
		if(is_array($extrafee_type) && count($extrafee_type)>0){
			foreach($extrafee_type as $etype){
				//echo "extrafee".$etype["id"]."==".$_REQUEST["paytype"];exit;
				if("extrafee".$etype["id"]==$_REQUEST["paytype"]){$payHere++;
				
					$alltransection=$db->getRows("select id,tid from transaction where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and rid='".$_REQUEST["idno"]."' and tfor='".$etype["type"]."'");
					//echo $db->getErMsg();print_r($alltransection);
					if(is_array($alltransection) && count($alltransection)>0){$tdata=array();
						foreach($alltransection as $tran){//echo $tran["tid"];
							$data=json_decode($tran["tid"],true);
							if(is_array($data) && count($data)>0){
								foreach($data as $key=>$value){//echo $tran["id"];
									$tdata[]=str_replace("pay","",$key);//echo $key."<br />";
									//echo $_REQUEST["ip"].$key."<br />";
									$db->delete("transaction","where id='".$tran["id"]."'");
									for($i=$_REQUEST["ip"];$i<=15;$i++){
										//echo $key=="pay".$i;
										if($key=="pay".$i){
											
											$paydata=$db->getRow("select fee,pay from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."' and type='".str_replace('extrafee', '', $_REQUEST["paytype"])."' and pid='p".$i."'");
											if($paydata["fee"]==$paydata["pay"]){
												$db->updateAry("extrafee",array("pay"=>"0"),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."' and type='".str_replace('extrafee', '', $_REQUEST["paytype"])."' and pid='p".$i."'");
											}else{
												$newpay=$paydata["fee"]-$paydata["pay"]	;
												if($newpay>0){
													$db->updateAry("extrafee",array("pay"=>"0"),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."' and type='".str_replace('extrafee', '', $_REQUEST["paytype"])."' and pid='p".$i."'");
													$nextpayout=$db->getVal("select fee from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."' and type='".str_replace('extrafee', '', $_REQUEST["paytype"])."' and pid='p".($i+1)."'");
													$db->updateAry("extrafee",array("fee"=>($nextpayout-$newpay)),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."' and type='".str_replace('extrafee', '', $_REQUEST["paytype"])."' and pid='p".($i+1)."'");
												}
											}
											//echo $db->getLastQuery();
											
										}
									}
								}
								//echo 1;
								//echo implode(",",$tdata);
								//
								//echo $db->getLastQuery();
							}
						}echo 1;
						sort($tdata);
						$my6data=implode(",",$tdata);
						//echo $my6data;
						$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["paytype"]."' and session='".$_SESSION["admin"]["session"]."' and inst_id='".$my6data."'");
						foreach($tdata as $inss){
						$detail1="ext-".$etype['id']."-".$inss;
								$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
								$db->delete("installment_time",$where1);
					}
				}
					//$db->delete("reff_receipt","");
				}
			}
		}
		if($payHere==0){
			$alltransection=$db->getRows("select id,tid from transaction where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and rid='".$_REQUEST["idno"]."'");
			if(is_array($alltransection) && count($alltransection)>0){
				foreach($alltransection as $tran){
					$data=json_decode($tran["tid"],true);
					//print_r($data);continue;
					if(is_array($data) && count($data)>0){
						foreach($data as $key=>$value){
							$iip="bp".$_REQUEST["ip"];
							$ip="b".$_REQUEST["ip"];
							$type="busfee";
							if($_REQUEST["detail"]=="School fee"){
								$iip="ip".$_REQUEST["ip"];
								$ip="i".$_REQUEST["ip"];
								$type="fee";
							}
							
							if($iip==$key){
								$aryInstall=$db->getRow("select * from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
								//echo $_REQUEST["amount"]."==".$aryInstall[$ip]."\n".$iip."==".$key;continue;
								if($_REQUEST["amount"]==$aryInstall[$ip]){
									$db->delete("transaction","where id='".$tran["id"]."'");
									$db->updateAry("installment",array($iip=>""),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
									$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='$type' and session='".$_SESSION["admin"]["session"]."' and inst_id='".$_REQUEST["ip"]."'");
									if($type=="fee"){
										$detail1="ip".$_REQUEST['install'];
											$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
											$db->delete("installment_time",$where1);
										}
									elseif($type=="busfee"){
										$detail1="bp".$_REQUEST['install'];
											$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
											$db->delete("installment_time",$where1);
									}
																	
								}else{
									$amount=$_REQUEST["amount"];
									//echo $amount;exit;
									$inst_id=array();
									for($i=$_REQUEST["ip"];$i<=15;$i++){
										$ip="bp".$i;$p="b".$i;$t="busfee";
										if($_REQUEST["detail"]=="School fee"){
											$ip="ip".$i;$p="i".$i;$t="fee";
										}
										//echo $amount;exit;
										if($amount>0){
											$inst_id[]=$i;
											$tid=$db->getVal("select id from transaction where  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and rid='".$_REQUEST["idno"]."' and tid like '%".$ip."%'");
											$db->delete("transaction","where id='".$tid."'");
											
											if($aryInstall[$p]==$aryInstall[$ip]){
												$db->updateAry("installment",array($ip=>""),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
											}else{
												$j=(int)($i+1);
												$nip="bp".$j;$np="b".$j;
												if($_REQUEST["detail"]=="School fee"){
													$nip="ip".$j;$np="i".$j;
												}
												$netAmt=abs($aryInstall[$np]+$aryInstall[$ip]-$aryInstall[$p]);
												$nextp=$np;
												$db->updateAry("installment",array($ip=>"",$nextp=>$netAmt),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
											}
											$amount=abs($amount-$aryInstall[$ip]);
										}
									}
									$db->delete("reff_receipt","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["idno"]."' and type='$type' and session='".$_SESSION["admin"]["session"]."' and inst_id='".implode(",",$inst_id)."'");
									//echo $_REQUEST['install'];exit;
								
									if($type=="fee"){
										foreach($inst_id as $ins){
										$detail1="ip".$ins;
											$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
											$db->delete("installment_time",$where1);
										}
									}
									elseif($type=="busfee"){
										foreach($inst_id as $ins){
										$detail1="bp".$ins;
											$where1="where ins_name='".$detail1."' and idno=".$_REQUEST["idno"]." and  school_id='".$_SESSION["admin"]["school_id"]."'";
											$db->delete("installment_time",$where1);
									}
								}
								
							}
								
								//sms(mydetail("mobile",$_REQUEST["student_id"],"student"),"Dear Parent, Amount of ".$_REQUEST["amount"]." has been successfully returned in ".$LinksDetails["site_name"],"fee_submit");
							}echo "1";
						}
					}
					else{
						//print_r($_REQUEST);echo "on transection";
						echo "1";exit;
					}
				}
			}
		}
	}
}elseif($ch=="checkreceptNo"){
	$data=$db->getVal("select id from receipt where id=".$_REQUEST["id"]." and type='".$_REQUEST["paytype"]."'");  
	if($data!="")echo "data already exit";
}elseif($ch=="updateImage"){
	$name=microtime()."-ms.jpg";
	file_put_contents("media/photo/".$name, file_get_contents($_POST["imgsrc"]));
	$db->updateAry("student",array("photo"=>$name),"where idno=".$_POST["idno"]);
}