<?php
include("../db.php");
$ch=$_REQUEST["type"];
if($ch=="FetchStudentClass"){
	?><table class="table">
	<tr>
        <th><input  onchange="checkAll()" class="check" type="checkbox" name="check[]" value="" id="checkboxa" /></th>
        <th>Name</th>
        <th>Faather Name</th>
        <th>Medium</th>
        <th>Email</th>
        <th>Mobile</th>
        <th>Cast</th>
    </tr>
	<?php
	$allstudent=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 order by name");
	if(is_array($allstudent) && count($allstudent)>0){
		foreach($allstudent as $allS){
			?>
			<tr>
            	<td><input type="checkbox" class="checkboxes" name="student" value="<?php echo $allS["idno"] ?>" checked="checked" /></td>
                <td><?php echo $allS["name"] ?></td>
                <td><?php echo $allS["fname"] ?></td>
                <td><?php echo $allS["medium"] ?></td>
                <td><?php echo $allS["email"] ?></td>
                <td><?php echo $allS["mobile"] ?></td>
                <td><?php echo $allS["cast"] ?></td>
            </tr>
			<?php
		}
	}else{
		?><tr>
        <td colspan="4">No Student Found</td>
        </tr><?php
	}
	//echo "<pre>";print_r($allstudent);echo $db->getErMsg();echo "</pre>";
	?></table><?php
}



elseif($ch=="MigrateStudentClass"){

	$check=$db->getVal("select idno from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_REQUEST["session"]."' limit 0,1");
	if($check!=""){
		echo "You already migrate class: ".$_REQUEST["class"]." to session : ".$_REQUEST["session"];
	}else{
			
		$idnoList=explode(",",$_REQUEST["student"]);
		if(is_array($idnoList) && count($idnoList)>0){
			foreach($idnoList as $idno){
			    $checkhostel = $db->getVal("select htype from student where idno='".$idno."' ");
			    
				$maxIDNO=(int)($db->getVal("SELECT MAX(idno) FROM student where school_id='".$_SESSION["admin"]["school_id"]."'"))+1;
				$qry1="CREATE TEMPORARY TABLE temp_table AS SELECT * FROM student WHERE idno='".$idno."'";
				
				if($checkhostel==""){
				    $qry2="UPDATE temp_table SET session='".$_REQUEST["session"]."', stype='old', class='".$_REQUEST["nextcla"]."', idno='".$maxIDNO."' WHERE idno='".$idno."'";
				} else {
				    $qry2="UPDATE temp_table SET session='".$_REQUEST["session"]."', stype='old', htype='old', class='".$_REQUEST["nextcla"]."', idno='".$maxIDNO."' WHERE idno='".$idno."'";
				}
				$qry3="INSERT INTO student SELECT * FROM temp_table";
				$qry4="DROP TEMPORARY TABLE temp_table";
				$db->query($qry1);$db->query($qry2);$db->query($qry3);$db->query($qry4);
				$is_root="";
				$is_root=$db->getVal("select idno from session_idno where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and idno='".$idno."'");
				if($is_root=="")
					$is_root=$idno;
				$db->insertAry("session_idno",array("session"=>$_REQUEST["session"], "school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
"idno"=>$maxIDNO,"is_root"=>$is_root));
			}
			$rows=$db->getRows("select subject from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["nextcla"]."' and session='".$_REQUEST["session"]."'");
			if(is_array($rows) && count($rows)>0){
				foreach($rows as $r){
					$arySave=array( "class"=>$_REQUEST["nextcla"],
									"subject"=>$r["subject"],
									"session"=>$_REQUEST["session"]
					);
					insertAry("subject",$arySave);
				}
			}
			echo "Selected Student of Class Migrate successfully".$db->getErMsg();
		}
		
	}
}


elseif($ch=="Migrateteacher"){

	$check=$db->getVal("select idno from staff where school_id='".$_SESSION["admin"]["school_id"]."'  and session='".$_REQUEST["session"]."' limit 0,1");
	if($check!=""){
		echo "You already migrate  session : ".$_REQUEST["session"];
	}else{
			
 print_r($_REQUEST["teacher"]);
	$idnoList=explode(",",$_REQUEST["teacher"]);



		if(is_array($idnoList) && count($idnoList)>0){	
	      foreach($idnoList as $idno){
	    

	$maxIDNO=(int)($db->getVal("SELECT MAX(idno) FROM staff where school_id='".$_SESSION["admin"]["school_id"]."'"))+1;
				$qry1="CREATE TEMPORARY TABLE temp_table AS SELECT * FROM staff WHERE idno='".$idno."'";
				$qry2="UPDATE temp_table SET session='".$_REQUEST["session"]."',  idno='".$maxIDNO."' WHERE idno='".$idno."'";
				$qry3="INSERT INTO staff SELECT * FROM temp_table";
				$qry4="DROP TEMPORARY TABLE temp_table";
				$db->query($qry1);$db->query($qry2);$db->query($qry3);$db->query($qry4);	    
	    
      
		}
		
	}
}


}

elseif($ch=="Migrateadmin"){
// echo "select id from users where school_id='".$_SESSION["admin"]["school_id"]."'  and session='".$_REQUEST["session"]."' limit 0,1";
// 	$check=$db->getVal("select id from users where school_id='".$_SESSION["admin"]["school_id"]."'  and session='".$_REQUEST["session"]."' limit 0,1");
// 	if($check!=""){
// 		echo "You already migrate  session : ".$_REQUEST["session"];
// 	}else{

    // print_r($_REQUEST["teacher"]);
    // die();
	$idnoList=explode(",",$_REQUEST["teacher"]);

        if(is_array($idnoList) && count($idnoList)>0){
            foreach($idnoList as $idno){
            	$maxIDNO=(int)($db->getVal("SELECT MAX(id) FROM admin where school_id='".$_SESSION["admin"]["school_id"]."'"))+1;
    			$qry1="CREATE TEMPORARY TABLE temp_table AS SELECT * FROM admin WHERE id='".$idno."'";
    			$qry2="UPDATE temp_table SET session='".$_REQUEST["session"]."',  id='".$maxIDNO."' WHERE id='".$idno."'";
    			$qry3="INSERT INTO admin SELECT * FROM temp_table";
    			$qry4="DROP TEMPORARY TABLE temp_table";
    			$db->query($qry1);$db->query($qry2);$db->query($qry3);$db->query($qry4);
    		}
	    }
    // }

}

elseif($ch=="payfee_data"){
	$where="";
	if(isset($_REQUEST["class"]))$where=" AND class='".$_REQUEST["class"]."'";
	if(isset($_REQUEST["section"]) && $_REQUEST["section"]!="undefined")$where.=" AND section='".$_REQUEST["section"]."' ";
	$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"=>"feediscount","action"=>"add");
	if(is_array($aryList) && count($aryList)>0)
	{
	   $i=0;
	   $aryData='{ "aaData": [
		';
		foreach($aryList as $iList)
		{   
			 $aryPgAct["id"]=$iList['idno'];
			$i++;
			$aryData.='["'.$iList['admissionno'].'",';
			$aryData.='"'.POST($iList['name']).'",';
		$aryData.='"'.POST($iList['class'])."(".POST($iList['section']).")".'",';
			$aryData.='"'.POST($iList['gender']).'",';
			$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\'>Discount</button></a>';
			$aryPgAct['action']="back_dues";
			$aryData.='<a href=\'#\' onclick=\"backdue(\''.$iList['idno'].'\',\''.$iList['class'].'\')\"><button type=\'button\' class=\'btn btn-info\'>Back Dues</button></a>';			
			$aryData.='"]';
			if($i!=count($aryList))$aryData.=",";
		 }		
		 $aryData.="] }";	
		 echo $aryData;	
	}
	else{
		echo $db->getLastQuery().$db->getErMsg();
	}
}

// code for extra fee start

elseif($ch=="extra_fee_discount"){
	
	$id= $_REQUEST["id"];
	$old_value= $_REQUEST["oldvalue"];
	$newValue= $_REQUEST["newValue"];
	
	$value= $old_value-$newValue;
	$discount= $_REQUEST["discount"]+$_REQUEST["newValue"];
	
	
	
	
	$update=mysql_query("update extrafee set discount='$discount', fee='$value' where id='$id'") or die("Update Error.".mysql_error());
	
	$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["did"],
									"type"=>"extra",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);		
	
}

elseif($ch=="resetdiscount"){
 
 
	if($_REQUEST["paytype"]=="fee"){
    	$aryData=array("i".$_REQUEST["id"]=>($_REQUEST["newValue"]));
    
        $db->delete("discount_detail","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and installment='"."i".$_REQUEST["id"]."' and type='tution'");

    	$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
	}
	
	elseif($_REQUEST["paytype"]=="busfee"){

        $aryData=array("b".$_REQUEST["id"]=>$_REQUEST["newValue"]);

        $db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");

        $db->delete("discount_detail","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and installment='"."b".$_REQUEST["id"]."' and type='bus'");

	}
	
	
	elseif($_REQUEST["paytype"]=="hostelfee"){
	
        $aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
        
        $db->updateAry("hostelinstallment",$aryData,"where idno='".$_REQUEST["idno"]."'");
        
        $db->delete("discount_detail","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and installment='"."i".$_REQUEST["id"]."' and type='hostel'");
	
	}


}

//code for extra fee end

elseif ($ch=="resetExtra"){

        $aryData=array("fee" => $_REQUEST["newValue"]);
        
        $db->updateAry("extrafee",$aryData,"where idno='".$_REQUEST["idno"]."' and type='".$_REQUEST["extra_id"]."' and pid='"."p".$_REQUEST['installment']."' ");
        
        $db->delete("discount_detail","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and installment='"."i".$_REQUEST["installment"]."' and type='extra' and other_id='".$_REQUEST["extra_id"]."' ");

    
}

elseif ($ch=="resetOther"){

    // print_r($_POST);
    
    $reff_data = $db->getRows("select * from reff_receipt where idno='".$_REQUEST["idno"]."' and type='$_REQUEST[name]' and amount='0' and session='".$_SESSION["admin"]["session"]."' ");
    // echo "<br>";
    // print_r($reff_data);
    
    foreach($reff_data as $reff){

        $ins_fine = $db->getRow("select * from installment_fine where id='$reff[inst_id]' and idno='".$_REQUEST["idno"]."' and reasion='$_REQUEST[name]' and session='".$_SESSION["admin"]["session"]."' ");

        $feeid = explode("-", $ins_fine['ins_name']);

    	$db->delete("installment_time","where idno='".$_REQUEST["idno"]."' and ins_name='other$feeid[0]_detail' ");
    	$db->delete("installment_fine","where idno='".$_REQUEST["idno"]."' and id='$reff[inst_id]' ");
        $db->delete("reff_receipt","where idno='".$_REQUEST["idno"]."' and id='$reff[id]' ");
    }
    
// die();

	$db->delete("discount_detail","where idno='".$_REQUEST["idno"]."' and id='".$_REQUEST["dd_id"]."' ");

	$db->delete("otherfee_discount","where idno='".$_REQUEST["idno"]."' and id='".$_REQUEST["other_id"]."' ");

}

elseif ($ch=="resethostelOther"){

	$db->delete("discount_detail","where idno='".$_REQUEST["idno"]."' and id='".$_REQUEST["dd_id"]."' ");
	
	$db->delete("hostelotherfee_discount","where idno='".$_REQUEST["idno"]."' and id='".$_REQUEST["other_id"]."' ");

}

elseif($ch=="updateTotal"){
	$class=$db->getVal("Select class from student where idno='".$_REQUEST["idno"]."'");
	$exist=$db->getRow("Select * from installment_discount where idno='".$_REQUEST["idno"]."'");
	if($_REQUEST["paytype"]=="fee"){
		
		$field="";$value="";
		if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
			$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
			$aryData["total"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
			$aryData["discount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
		}else{
			$aryData=array("i".$_REQUEST["id"]=>($_REQUEST["oldvalue"]-$_REQUEST["newValue"]));
			$aryData["total"]=$_REQUEST["total"]-$_REQUEST["newValue"];
			$aryData["discount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
		}
		
		$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"tution",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
					//print_r($arySave);
		
		$db->insertAry("discount_detail",$arySave);		
		
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
				if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
					$aryData=array("i".$_REQUEST["id"]=>($_REQUEST["newValue"]-($_REQUEST["oldvalue"]+$exist["i".$_REQUEST["id"]])));
					$aryData["total"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
					$aryData["discount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
				}else{
					$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
					$aryData["total"]=$_REQUEST["total"]-$_REQUEST["newValue"];
					$aryData["discount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
				}
			if(is_array($exist) && count($exist)>0){
				$db->updateAry("installment_discount",$aryData,"where idno='".$_REQUEST["idno"]."'");
			}else{
				$aryData["idno"]=$_REQUEST["idno"];
				$aryData["class"]=$class; 
				$aryData["session"]=$_SESSION["admin"]["session"];
		      	$aryData["school_id"]=$_SESSION["admin"]["school_id"];
        		$aryData["added_by"]=$_SESSION["admin"]["uid"];
				$flgIn=$db->insertAry("installment_discount",$aryData);		
			}
		
		echo $db->getErMsg().$db->getLastQuery();
	}elseif($_REQUEST["paytype"]=="busfee"){
		//$aryData=array("b".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$field="";$value="";
		if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
			$aryData=array("b".$_REQUEST["id"]=>$_REQUEST["newValue"]);
			$aryData["bus"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
			$aryData["busdiscount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
		}else{
			$aryData=array("b".$_REQUEST["id"]=>($_REQUEST["oldvalue"]-$_REQUEST["newValue"]));
			$aryData["bus"]=$_REQUEST["total"]-$_REQUEST["newValue"];
			$aryData["busdiscount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
		}
		
	$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"bus",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);			
		
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
				if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
					$aryData=array("b".$_REQUEST["id"]=>($_REQUEST["newValue"]-($_REQUEST["oldvalue"]+$exist["b".$_REQUEST["id"]])));
					$aryData["bus"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
					$aryData["busdiscount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
				}else{
					$aryData=array("b".$_REQUEST["id"]=>$_REQUEST["newValue"]);
					$aryData["bus"]=$_REQUEST["total"]-$_REQUEST["newValue"];
					$aryData["busdiscount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
				}
			if(is_array($exist) && count($exist)>0){
				$db->updateAry("installment_discount",$aryData,"where idno='".$_REQUEST["idno"]."'");
			}else{
				$aryData["idno"]=$_REQUEST["idno"];
				$aryData["class"]=$class; 
				$aryData["session"]=$_SESSION["admin"]["session"];
		      	$aryData["school_id"]=$_SESSION["admin"]["school_id"];
        		$aryData["added_by"]=$_SESSION["admin"]["uid"];
				$flgIn=$db->insertAry("installment_discount",$aryData);		
			}
		echo $db->getErMsg().$db->getLastQuery();
	}elseif($_REQUEST["paytype"]=="hostelfee"){
		//$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$field="";$value="";
		if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
			$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
			$aryData["total"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
			$aryData["discount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
		}else{
			$aryData=array("i".$_REQUEST["id"]=>($_REQUEST["oldvalue"]-$_REQUEST["newValue"]));
			$aryData["total"]=$_REQUEST["total"]-$_REQUEST["newValue"];
			$aryData["discount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
		}
		$db->updateAry("hostelinstallment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
		
		$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"hostel",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
		
		
			if($_REQUEST["newValue"]>$_REQUEST["oldvalue"]){
					$aryData=array("h".$_REQUEST["id"]=>($_REQUEST["newValue"]-($_REQUEST["oldvalue"]+$exist["h".$_REQUEST["id"]])));
					$aryData["hostel"]=$_REQUEST["total"]+($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
					$aryData["hosteldiscount"]=$_REQUEST["discount"]-($_REQUEST["newValue"]-$_REQUEST["oldvalue"]);
				}else{
					$aryData=array("h".$_REQUEST["id"]=>$_REQUEST["newValue"]);
					$aryData["hostel"]=$_REQUEST["total"]-$_REQUEST["newValue"];
					$aryData["hosteldiscount"]=$_REQUEST["discount"]+$_REQUEST["newValue"];
				}
			if(is_array($exist) && count($exist)>0){
				$db->updateAry("installment_discount",$aryData,"where idno='".$_REQUEST["idno"]."'");
			}else{
				$aryData["idno"]=$_REQUEST["idno"];
				$aryData["class"]=$class; 
				$aryData["session"]=$_SESSION["admin"]["session"];
		      	$aryData["school_id"]=$_SESSION["admin"]["school_id"];
        		$aryData["added_by"]=$_SESSION["admin"]["uid"];
				$flgIn=$db->insertAry("installment_discount",$aryData);		
			}
		echo $db->getErMsg().$db->getLastQuery();
	}elseif($_REQUEST["paytype"]=="otherfee"){
		if($_REQUEST["newValue"]<=$_REQUEST["oldvalue"]){
			$arr=$db->getRow("Select class,section from student where idno='".$_REQUEST["idno"]."'");
		$aryData=array(
						"other_id"=>$_REQUEST["id"],
						"idno"=>$_REQUEST["idno"],
						"discount"=>$_REQUEST["newValue"],
						"month"=>$_REQUEST["discount"],
						"class"=>$arr["class"],
						"section"=>$arr["section"],
						"session"=>$_SESSION["admin"]["session"],
		      			"school_id"=>$_SESSION["admin"]["school_id"],
        				"added_by"=>$_SESSION["admin"]["uid"]						
					 );
			$flgIn=$db->insertAry("otherfee_discount",$aryData);
			
			
			$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"",
									"type"=>"otherfee",
									"other_id"=>$_REQUEST["id"],
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
			
		}
		else
			echo "Amount can not be greater than old value";	
	}elseif($_REQUEST["paytype"]=="hostelotherfee"){
		if($_REQUEST["newValue"]<=$_REQUEST["oldvalue"]){
			$arr=$db->getRow("Select class,section from student where idno='".$_REQUEST["idno"]."'");
		$aryData=array(
						"other_id"=>$_REQUEST["id"],
						"idno"=>$_REQUEST["idno"],
						"discount"=>$_REQUEST["newValue"],
						"month"=>$_REQUEST["discount"],
						"class"=>$arr["class"],
						"section"=>$arr["section"],
						"session"=>$_SESSION["admin"]["session"],
		      			"school_id"=>$_SESSION["admin"]["school_id"],
        				"added_by"=>$_SESSION["admin"]["uid"]						
					 );
			$flgIn=$db->insertAry("hostelotherfee_discount",$aryData);
			
		$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"hostelother",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);		
			
		}
		else
			echo "Amount can not be greater than old value";	
	}
}elseif($ch=="activeMe"){
	$db->updateAry("student",array("status"=>1),"where idno='".$_REQUEST["id"]."'");
}elseif($ch=="updateNewTotal"){
if($_REQUEST["paytype"]=="fee"){
		$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
	$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"tution",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
					
		
		$db->insertAry("discount_detail",$arySave);		
		
		
	}elseif($_REQUEST["paytype"]=="busfee"){
		$aryData=array("b".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
		$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
                                   "discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"bus",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);		
		
	}elseif($_REQUEST["paytype"]=="hostelfee"){
		$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$db->updateAry("hostelinstallment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
		$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"hostel",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
		
	}elseif($_REQUEST["paytype"]=="otherfee"){
		$aryData=array("fee"=>$_REQUEST["newValue"]);
		if($_REQUEST["sid"]!='' && $_REQUEST["sid"]!='old' && $_REQUEST["sid"]!='new' )
		{
		$db->updateAry("otherfee_structure",$aryData,"where otype='".$_REQUEST["sid"]."' and id='".$_REQUEST["id"]."'");
		
		if($_REQUEST["oldvalue"] > $_REQUEST["newValue"])
			{
		$arySave=array( 
		                "idno"=>$_REQUEST["sid"],
									"discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									"installment"=>"i".$_REQUEST["id"],
									"type"=>"otherfee",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
			}
			else
			{
			}
		}
		
		//echo $_REQUEST["newValue"];
		$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$db->delete("otherfee_discount","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and other_id='".$_REQUEST["id"]."'");
	}elseif($_REQUEST["paytype"]=="hostelotherfee"){
		
		$aryData=array("fee"=>$_REQUEST["newValue"]);
		if($_REQUEST["sid"]!='' && $_REQUEST["sid"]!='old' && $_REQUEST["sid"]!='new')
		{
		$db->updateAry("hostel_otherfee_structure",$aryData,"where otype='".$_REQUEST["sid"]."' and id='".$_REQUEST["id"]."'");
		
		if($_REQUEST["oldvalue"] > $_REQUEST["newValue"])
			{
		$arySave=array( 
		                "idno"=>$_REQUEST["sid"],
									"discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									
									"type"=>"hostelother",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
			}
			else
			{
			}
		}
		
		//$aryData=array("i".$_REQUEST["id"]=>$_REQUEST["newValue"]);
		$db->delete("hostelotherfee_discount","where session='".$_SESSION["admin"]["session"]."' and idno='".$_REQUEST["idno"]."' and other_id='".$_REQUEST["id"]."'");
	}
}



elseif($ch=="updateclassfee"){
if($_REQUEST["paytype"]=="classfee"){
		$aryData=array("total"=>$_REQUEST["newValue"]);
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
	}


if($_REQUEST["paytype"]=="busfee"){
		$aryData=array("bus"=>$_REQUEST["newValue"]);
		$db->updateAry("installment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
	}
	
if($_REQUEST["paytype"]=="hostel"){
		$aryData=array("total"=>$_REQUEST["newValue"]);
		$db->updateAry("hostelinstallment",$aryData,"where idno='".$_REQUEST["idno"]."'");
		
	}	
}

elseif($ch=="resetextrafees"){
	if($_REQUEST["paytype"]=="extra"){
		$aryData=array(["fee"]=>($_REQUEST["total"]-$_REQUEST["newValue"]));
		
			
		$totalremainfee=($_REQUEST["total"]-$_REQUEST["newValue"]);
		
		$update = "UPDATE extrafee SET fee='".$_REQUEST["newValue"]."'  where pid = '"."p".$_REQUEST["id"]."' and idno = '".$_REQUEST["idno"]."' and type='".$_REQUEST["typeid"]."'";
		
		if($_REQUEST["oldvalue"] >= $_REQUEST["newValue"])
		{
$arySave=array( 
		                "idno"=>$_REQUEST["idno"],
									"discount_amount"=>$_REQUEST["oldvalue"]-$_REQUEST["newValue"],
									"installment"=>"p".$_REQUEST["id"],
									"type"=>"extra",
									"session"=>$_SESSION["admin"]["session"],
		      	                   "school_id"=>$_SESSION["admin"]["school_id"],
        		                     "added_by"=>$_SESSION["admin"]["uid"]
									
					);
			$db->insertAry("discount_detail",$arySave);	
		}
else
{

}	
	
		
		
//echo $s="UPDATE extrafee SET fee='".$_REQUEST["newValue"]."'  where pid = '"."p".$_REQUEST["id"]."' and idno = '".$_REQUEST["idno"]."'";
		if ($db->query($update) === TRUE) {
   // echo "Record updated successfully";
} else {
    //echo "Error updating record: " . $con->error;
}
	
	//echo $s="UPDATE extrafee SET fee='".$totalremainfee."' where pid = '"."p".$_REQUEST["id"]."' and idno = '".$_REQUEST["idno"]."'";
	}	
}



elseif($ch=="BackDue"){
	
	$chkname=$db->getVal("select id from otherfee_structure where otype='".$_REQUEST['idno']."' and name='".$_REQUEST['title']."'");

		$classname=$db->getVal("select class from student where idno='".$_REQUEST['id']."' and session='".$_SESSION["admin"]["session"]."' ");



	if($chkname>0){
		$_SESSION['msg']='Back Dues is already submitted for this fee title please enter another fee title';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));		exit;
		}else
	$saveAry=array( "session"=>$_SESSION["admin"]["session"],
					"school_id"=>$_SESSION["admin"]["school_id"],
					"added_by"=>$_SESSION["admin"]["uid"],
					"class"=>$classname,
					"month"=>$_REQUEST['month'],
					"otype"=>$_REQUEST["id"],
					);
	$nameList=explode(",",$_REQUEST["title"]);
	$feeList=explode(",",$_REQUEST["fixedfee"]);
	$monthList=explode(",",$_REQUEST["class"]);;
	$month="";
	if(isset($monthList) && count($monthList))
		$month=$monthList;
	//print_r($month);exit;
	if(is_array($nameList) && count($nameList)>0){$i=0;
		foreach($nameList as $name){
			$saveAry["name"]=$name;
			$saveAry["fee"]=$feeList[$i];
			
			
	 $check_val=$db->getVal("Select id from otherfee_structure where otype='".$_REQUEST["id"]."' and name='".$name."' and fee='".$feeList[$i]."' and month='".$_REQUEST['month']."'  and session='".$_SESSION["admin"]["session"]."'");
			
				//echo "Select id from otherfee_structure where otype='".$idno."' and name='".$name."' and fee='".$feeList[$j]."' and month='".$month[$i]."'  and session='".$_SESSION["admin"]["session"]."'";
if (!empty($check_val)) { 

$ary_exit[]=$name;


}
else
{		
			
			
			$flgIn=$db->insertAry("otherfee_structure",$saveAry);
}
			$i++;
		
	    
		    
		}
	}
	if(is_null($db->getErMsg()))
		{
		    		   $rr=implode(" ,",$ary_exit);
echo '<script language="javascript">';
echo 'alert("this is already exit")';
echo '</script>';


			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod,"ad"=>$rr)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	
}elseif($ch=="cheacktitle"){
	
	
$data=$db->getVal("select id from otherfee_structure where id='".$_REQUEST['idno']."' and title='".$_REQUEST['title']."'");
	echo $data;
	}
	
?>