<?php
include("../db.php");
header("Access-Control-Allow-Origin: * ");
date_default_timezone_set("Asia/kolkata");

		 /*$id= '830';
		 $_GET["id"]= $id;
		 $total='500';
         $_REQUEST['total']=$total;		 // payment given by parent
		 $clas= 'I';
		 $m='12';
		 $session='2019-2020';*/
		 

	    $count_month=$_REQUEST['countmonth'];
		 $id= $_REQUEST['idno'];
		 $_GET["id"]= $id;
		 $total=$_REQUEST['total']; // payment given by parent
		 $m=$_REQUEST['month2'];
		 $session=$_REQUEST['session'];	
		 $clas= $_REQUEST['cls'];
		 $txnid = $_REQUEST['txnid'];
		// print_r($_REQUEST);
		 $pay_type='online';
		 $_SESSION["admin"]["session"]=$session;
         $_POST["select_date"]=date('Y-m-d H:i:s');
		 $_SESSION["admin"]["uid"]='1';
        $_SESSION["admin"]["school_id"]='1';
		
     $tution=$db->getRow("SELECT * from installment  WHERE idno='$id' and session='$session'" );

for($x=1; $x<=$m; $x++){

if(!empty($tution[i.$x])){
  if(empty($tution[ip.$x])){
	 if(!empty($total))
	 {
		if($tution[i.$x]>=$total)
		        {								
				$rest_data1=$tution[i.($x+1)]+$tution[i.$x]-$total;
				$tution_inp= array(ip.$x=>$total, i.($x+1)=>$rest_data1);
				//$reff_receipt["amount"]=$total;
				$recept="fee-tution";
				$recData[$recept]+=$total;
				$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$total,									
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
																						
				$insAry=array("ip".($x)=>$total);
				$given=$total;			
				$given1[]=$total;			
				$rest_data='';				
			    $total= $rest_data; 	
			    }
		if($tution[i.$x]<$total)
			{				
				$rest_data=$total-$tution[i.$x];
				$tution_inp= array(ip.$x=>$tution[i.$x]);
				//$reff_receipt["amount"]=$tution[i.$x];
				$recept="fee-tution";
				$recData[$recept]+=$tution[i.$x];
					$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$tution[i.$x],
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
									
				$insAry=array( "ip".($x)=>$tution[i.$x]);													
				$given=$tution[i.$x];			
				$given1[]=$tution[i.$x];			
			    $total= $rest_data;				 
			}
				
				//print_r($tution_inp);
				
				$inup=$db->updateAry("installment",$tution_inp," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
				
						
			installment_time($_GET["id"],"ip".$x,$_POST["select_date"]);
						
            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
			$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			$reff_receipt["date"]=$_POST["select_date"];
			$reff_receipt["pay_type"]= $pay_type;
								
			$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
			$receipt_data["Tution_Fees"]+=$given;
				$insAry=array( "ip".($x)=>$tution[i.$x]);					
			deposit($given,$_GET["id"],json_encode($insAry),"fee","Student fee Payment in installment type");	
		} 			
	}
	// tution 12 start
	else{
	
	if($x=='12'){
		$x=12;
		$df=$tution[i12]-$tution[ip12];
		if($df!=0){
		if($df>=$total)
		        {
				
				$rest_data1=$tution[i.($x+1)]+$tution[i.$x]-$total;
				$tution_inp= array(ip.$x=>$total, i.($x+1)=>$rest_data1);
				//$reff_receipt["amount"]=$total;
				$recept="fee-tution";
				$recData[$recept]+=$total;
				$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$total,									
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
																						
				$insAry=array( "ip".($x)=>$total);
				$given=$total;			
				$given1=$total;	
							
				$rest_data='';				
			    $total= $rest_data; 	
			    }
						
			if($df<$total)
			{				
				//$rest_data=$total-$tution[i.$x];
				$t=$tution[i12]-$tution[ip12];
				$rest_data=$total-$df;
				$tution_inp= array(ip.$x=>$tution[i.$x]);
				//$reff_receipt["amount"]=$tution[i.$x];
				$recept="fee-tution";
				$recData[$recept]+=$tution[i.$x];
					$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$tution[i.$x],
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
									
				$insAry=array( "ip".($x)=>$tution[i.$x]);													
				$given=$tution[i.$x];			
				$given1=$tution[i.$x]-$tution[ip.$x];			
							
							
			    $total= $rest_data;				 
			}
			
			$inup=$db->query("update installment set ip12=COALESCE(ip12,0)+'".$given1."' where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
			
			//echo $rest_data;
			//echo '12 data';
			installment_time($_GET["id"],"ip".$x,$_POST["select_date"]);
						
            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
			$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			$reff_receipt["date"]=$_POST["select_date"];
			$reff_receipt["pay_type"]= $pay_type;
								
			$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
			$receipt_data["Tution_Fees"]+=$given;
				$insAry=array( "ip".($x)=>$tution[i.$x]);					
			deposit($given,$_GET["id"],json_encode($insAry),"fee","Student fee Payment in installment type");
						
						
						
	                  } 
	}
		}
		// tution 12 end
	
  }
	
if(!empty($tution[b.$x])){
	if(empty($tution[bp.$x]))
	{		
	    if(!empty($total))
		 {	
			if($tution[b.$x]>=$total){
				
				$after_bus_rest_data=$tution[b.($x+1)]+$tution[b.$x]-$total;
				$tution_inp= array(bp.$x=>$total, b.($x+1)=>$after_bus_rest_data);
							
				$recept="fee-bus";
				$recData[$recept]+=$total;
				
				$reff_receipt=array(				                   
									"type"=>"busfee",
									"inst_id"=>"$x",
									"amount"=>$total,									
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
																						
				$insAry=array( "bp".($x)=>$total);
				$given=$total;			
				$rest_data='';				
			    $total= $rest_data; 					
			}
			if($tution[b.$x]<$total){
				
				$after_bus_rest_data=$total-$tution[b.$x];
				$tution_inp= array(bp.$x=>$tution[b.$x]);
				$recept="fee-bus";
				$recData[$recept]+=$tution[b.$x];
				$reff_receipt=array(				                   
									"type"=>"busfee",
									"inst_id"=>"$x",
									"amount"=>$tution[b.$x],
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
									
				$insAry=array( "bp".($x)=>$tution[b.$x]);				
									
				$given=$tution[b.$x];	
					
               $total=$after_bus_rest_data;								
				
			}				
				$inup=$db->updateAry("installment",$tution_inp," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
				
				installment_time($_GET["id"],"bp".$x,$_POST["select_date"]);						
                $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
				$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
				$reff_receipt["date"]=$_POST["select_date"];
				$reff_receipt["pay_type"]= $pay_type;
				
				$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
				
				$receipt_data["Bus_Fees"]+=$given;
				
				deposit($given,$_GET["id"],json_encode($insAry),"bus","Student bus fee Payment in installment type");							
			}
		
		  }
		  // bus 12 start
		  else{
	
	if($x=='12'){
		$x=12;
		$df=$tution[b12]-$tution[bp12];
		if($df!=0){
		if($df>=$total)
		        {								
				$rest_data1=$tution[b.($x+1)]+$tution[b.$x]-$total;
				$tution_inp= array(bp.$x=>$total, b.($x+1)=>$rest_data1);
				//$reff_receipt["amount"]=$total;
				$recept="fee-tution";
				$recData[$recept]+=$total;
				$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$total,									
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
																						
				$insAry=array( "bp".($x)=>$total);
				$given=$total;			
				$given1=$total;	
							
				$rest_data='';				
			    $total= $rest_data; 	
			    }
			
			
			if($df<$total)
			{				
				//$rest_data=$total-$tution[i.$x];
				$t=$tution[b12]-$tution[bp12];
				$rest_data=$total-$df;
				$tution_inp= array(bp.$x=>$tution[b.$x]);
				//$reff_receipt["amount"]=$tution[i.$x];
				$recept="fee-tution";
				$recData[$recept]+=$tution[b.$x];
					$reff_receipt=array(				                   
									"type"=>"fee",
									"inst_id"=>"$x",
									"amount"=>$tution[b.$x],
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
									
				$insAry=array( "bp".($x)=>$tution[b.$x]);													
				$given=$tution[b.$x];			
				$given1=$tution[b.$x]-$tution[bp.$x];			
							
			    $total= $rest_data;				 
			}
			
			$inup=$db->query("update installment set bp12=COALESCE(bp12,0)+'".$given1."' where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
			
			//echo $rest_data;
			//echo '12 databus';
			installment_time($_GET["id"],"bp".$x,$_POST["select_date"]);
						
            $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
			$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			$reff_receipt["date"]=$_POST["select_date"];
			$reff_receipt["pay_type"]= $pay_type;
								
			$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
			$receipt_data["Tution_Fees"]+=$given;
				$insAry=array( "bp".($x)=>$tution[b.$x]);					
			deposit($given,$_GET["id"],json_encode($insAry),"fee","Student fee Payment in installment type");
						
						
						
	                  } 
		  }
		}
		 // bus 12 end
	 }				
		
	//extra	s
		$extrafee_list= $db->getRows("SELECT id from extrafee_type  WHERE class='$clas' and session='".$_SESSION["admin"]["session"]."' ");
	
	foreach($extrafee_list as $extra_list){
	
	 $extra_list1[]=$extra_list['id'];
	
	}
	//print_r($extra_list1);
	  $extralist=implode(',',$extra_list1);
		
		
		$type=$db->getRows("SELECT DISTINCT(type)  from extrafee  WHERE type in ($extralist) and  idno='$id' and session='".$_SESSION["admin"]["session"]."'");
				
		if(!empty($total)){		
	foreach($type as $type1){
			
		$extra_selected=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p".$x."' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");
		
	$type_name= $db->getVal("SELECT type  from extrafee_type  WHERE id='$extra_selected[type]' and session='".$_SESSION["admin"]["session"]."'");
		if(empty($extra_selected[pay])){
				//$monthary[]=$x;		
			$n=$x+1;
			$extra_next=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p".$n."' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");
			
			if($extra_selected[fee]>=$total){
			if(!empty($total))	{
				$monthary[]=$x;
				$recept="fee-".$type_name;
				$recData[$recept]+=$total;
				
           // if($extra_selected[pay]!='0'){
						
			    $after_extra_rest_data=$extra_next[fee]+$extra_selected[fee]-$total;
				//die;
				$extra_inp1= array(pay=>$total);
				$extra_inp2= array(fee=>$after_extra_rest_data);
				
				$inup1=$db->updateAry("extrafee",$extra_inp1," where  idno='$id' and pid='p".$x."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
				
				$inup2=$db->updateAry("extrafee",$extra_inp2," where  idno='$id' and pid='p".$n."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
				
				
				// reff receipt	table insert
				$reff_receipt=array(
				               "amount"=>$total,
								"type"=>"extrafee".$extra_selected[type],
								"idno"=>$id,
								"session"=>$_SESSION["admin"]["session"]
								);
					
  			        $reff_receipt["inst_id"]=$x;
					$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
					$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
					$reff_receipt["date"]=$_POST["select_date"];
					$reff_receipt["pay_type"]= $pay_type;
					$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
										
					// transaction table insert
					$insAryJ=array( "pay".$x	=>	$total);
										
					$extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected[type]' ");
										
                    deposit($extra_selected[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");
					$receipt_data[$extra_type]+=$total;
					
					
					
			$rest_data='';
				
			$total= $rest_data;
		   }
			}

     if($extra_selected[fee]<$total)
		{
         	if(!empty($extra_selected[fee])){		
			
			$monthary[]=$x;
             $recept="fee-".$type_name;
				$recData[$recept]+=$extra_selected[fee];		

				
			    $after_extra_rest_data=$total-$extra_selected[fee];
							
				$extra_inp1= array(pay=>$extra_selected[fee]);
				//$extra_inp2= array(fee=>$after_extra_rest_data);
				
				$inup1=$db->updateAry("extrafee",$extra_inp1," where  idno='$id' and pid='p".$x."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
				// reff receipt	
				$reff_receipt=array(
				               "amount"=>$extra_selected[fee],
								"type"=>"extrafee".$extra_selected[type],
								"idno"=>$id,
								"session"=>$_SESSION["admin"]["session"]
								);
					
					
					
  			        $reff_receipt["inst_id"]=$x;
					$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
					$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
					$reff_receipt["date"]=$_POST["select_date"];
					$reff_receipt["pay_type"]= $pay_type;
					$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
					
					
				// transaction table insert				
						
				$insAryJ=array( "pay".$x	=>	$extra_selected[fee]);
				
					$extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected[type]' ");
										
                    deposit($extra_selected[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");
					$receipt_data[$extra_type]+=$extra_selected[fee];
					
                    									
				$total=$after_extra_rest_data;	
			     }				
			}

   // installment time table insert
                  $detailDtatamu="ext-".$extra_selected[type]."-".$x;
					installment_time($_GET["id"],$detailDtatamu,$_POST["select_date"]);
					
			 $e =$total-after_extra_rest_data;
						
            //}
						
			}
			
			//....
			
			// ext 12 start
		  else{
	
	if($x=='12'){
		$x=12;
		
		$extra_selected12=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p".$x."' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");
	
		$type_name= $db->getVal("SELECT type  from extrafee_type  WHERE id='$extra_selected12[type]' and session='".$_SESSION["admin"]["session"]."'");
		
		$n=$x+1;
			$extra_next=$db->getRow("SELECT *  from extrafee  WHERE   idno='$id' and pid='p12' and type='$type1[type]' and session='".$_SESSION["admin"]["session"]."'");
		
		
		$df=$extra_selected12[fee]-$extra_selected12[pay];
		if($df!=0){
		if($df>=$total)
		        {

//echo 777;					
				$monthary[]=$x;
				$recept="fee-".$type_name;
				$recData[$recept]+=$total;
				
           // if($extra_selected[pay]!='0'){
						
			    $after_extra_rest_data=$extra_next[fee]+$extra_selected12[fee]-$total;
				//die;
				$extra_inp1= array(pay=>$total);
				$extra_inp2= array(fee=>$after_extra_rest_data);
				
				//$inup1=$db->updateAry("extrafee",$extra_inp1," where  idno='$id' and pid='p".$x."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
				
				$oldFee=$db->getVal("select pay from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p12' and type='".$type1["type"]."'");

				//echo 'given-'.$given;
				
					//$g=$given+$oldFee;
					$g=$total+$oldFee;
					$insAry=array( "pay"	=>	$g);
					//print_r($insAry);
					$inup1=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p12' and type='".$type1["type"]."'");
				
				
				
				$inup2=$db->updateAry("extrafee",$extra_inp2," where  idno='$id' and pid='p".$n."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
				
				
				// reff receipt	table insert
				$reff_receipt=array(
				               "amount"=>$total,
								"type"=>"extrafee".$extra_selected12[type],
								"idno"=>$id,
								"session"=>$_SESSION["admin"]["session"]
								);
					
  			        $reff_receipt["inst_id"]=$x;
					$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
					$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
					$reff_receipt["date"]=$_POST["select_date"];
					$reff_receipt["pay_type"]= $pay_type;
					$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
										
					// transaction table insert
					$insAryJ=array( "pay".$x	=>	$total);
										
					$extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected12[type]' ");
										
                    deposit($extra_selected12[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");
					$receipt_data[$extra_type]+=$total;
					
					
					
			$rest_data='';
				
			$total= $rest_data; 	
			    }
			
			
			if($df<$total)
			{				
				if(!empty($extra_selected12[fee])){		
			//echo 888;
			$monthary[]=$x;
             $recept="fee-".$type_name;
				$recData[$recept]+=$extra_selected12[fee];		

				
			   // $after_extra_rest_data=$total-$extra_selected12[fee];
			    $after_extra_rest_data=$total-$df;
				/*			
				$extra_inp1= array(pay=>$extra_selected12[fee]);
				//$extra_inp2= array(fee=>$after_extra_rest_data);
				
				$inup1=$db->updateAry("extrafee",$extra_inp1," where  idno='$id' and pid='p".$x."' and type='$type1[type]' and  school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");*/
				
				$oldFee=$db->getVal("select pay from extrafee where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p12' and type='".$type1["type"]."'");

				//echo 'given-'.$given;
				
					//$g=$given+$oldFee;
					$g=$df+$oldFee;
					$insAry=array( "pay"	=>	$g);
					//print_r($insAry);
					$inup1=$db->updateAry("extrafee",$insAry," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and pid='p12' and type='".$type1["type"]."'");
								
				// reff receipt	
				$reff_receipt=array(
				               "amount"=>$extra_selected12[fee],
								"type"=>"extrafee".$extra_selected12[type],
								"idno"=>$id,
								"session"=>$_SESSION["admin"]["session"]
								);
					
  			        $reff_receipt["inst_id"]=$x;
					$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
					$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
					$reff_receipt["date"]=$_POST["select_date"];
					$reff_receipt["pay_type"]= $pay_type;
					$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
					
					
				// transaction table insert				
						
				$insAryJ=array( "pay".$x	=>	$extra_selected12[fee]);
				
					$extra_type=$db->getVal("SELECT type  from extrafee_type  WHERE   id='$extra_selected12[type]' ");
										
                    deposit($extra_selected12[fee],$id,json_encode($insAryJ),$extra_type,"Student ".$extra_type." fee Payment in installment type");

					$receipt_data[$extra_type]+=$extra_selected12[fee];
					
                    									
				$total=$after_extra_rest_data;	
			     }				 
			}
					
	                  } 
		  }
		}
		 // ext 12 end

			if($e=='0') { break; exit(); }
			
		}	

	}
// ext en

		$aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='$id'");
		
		$other_fee_name=$db->getRows("SELECT  id,name from otherfee_structure  WHERE class='$clas' and month='$x' and (otype='$id' or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");
				
		$other_name=$db->getRows(" SELECT @serial := @serial+1 AS serial_number, id,name from otherfee_structure,(SELECT @serial:= 0) AS s  WHERE class='$clas' and (otype='$id' or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' " );
						
		$n=0;
		$a= array();
		foreach($other_name as $name){
			$n++;
			$a[]=$name[name];
		}		
		foreach($other_fee_name as $other_name){
		foreach($a as $key=>$value){
			
			if($value==$other_name[name]){
				$n= $key+1;
			}
		}							
			
	$total_other=$db->getVal("SELECT COALESCE(sum(fee),0) as result from otherfee_structure  WHERE class='$clas' and name='$other_name[name]'  and (otype='$id'  or otype='$aryForm[stype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");
	
	
	$total_other_discount=$db->getVal("SELECT COALESCE(sum(discount),0) as result from otherfee_discount  WHERE  idno='$id' and other_id='$other_name[id]'  and month='$x'   and session='".$_SESSION["admin"]["session"]."' ");
			
	$total_other_given=$db->getVal("SELECT COALESCE(sum(fine),0) as result from installment_fine  WHERE  idno='$id' and reasion ='$other_name[name]'   and session='".$_SESSION["admin"]["session"]."' ");
				
			  $final= $total_other-$total_other_discount-$total_other_given;
							
					 $num=$n*2;
					
			if(!empty($total)){
			if(!empty($final)){
				
				if(empty($total_other_given)){
					$monthary[]=$x;
					
					// if given is empty then data inserted in both table installment_fine & installment_time
					$udate=$_POST["select_date"];
					
					installment_time($id,"other".$num."_detail",$_POST["select_date"]);
									
				if($final>=$total){
					$recept="fee-".$other_name[name];
				$recData[$recept]+=$total;
					
				$insAry=array(  "ins_name"	=>	$num."-otherfee",
					"fine"		=>	$total,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $_POST["select_date"],
					"status"	=>	"1",
					"idno"		=>	$id					
				);
				
				$insAry["school_id"]=$_SESSION["admin"]["school_id"];
				$insAry["added_by"]=$_SESSION["admin"]["uid"];
				$insAry["session"]=$_SESSION["admin"]["session"];
				
				$inup=$db->insertAry("installment_fine",$insAry);
				
				// reff receipt start				
				$reff_receipt=array("amount"=>$total,
										"type"=>$other_name[name],
										"idno"=>$id,
										"date"=>$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"];
						$reff_receipt["date"]=$_POST["select_date"];
						$reff_receipt["pay_type"]= $pay_type;
						$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
				// reff receipt end 
				
				deposit($total,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
				$n=$other_name[name];
				$receipt_data[$n]+=$total;
				$rest_data='';
				
			    $total= $rest_data;
				
				}
				
				if($final<$total){
					
			    $recept="fee-".$other_name[name];
				$recData[$recept]+=$final;
				
					$after_other_rest_data=$total-$final;
				$insAry=array(  "ins_name"	=>	$num."-otherfee",
					"fine"		=>	$final,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $_POST["select_date"],
					"status"	=>	"1",
					"idno"		=>	$id
					
				);
				$insAry["school_id"]=$_SESSION["admin"]["school_id"];
				$insAry["added_by"]=$_SESSION["admin"]["uid"];
				$insAry["session"]=$_SESSION["admin"]["session"];
				
				$inup=$db->insertAry("installment_fine",$insAry);
				
				// reff receipt start
				
				$reff_receipt=array("amount"=>$final,
										"type"=>$other_name[name],
										"idno"=>$id,
										"date"=>$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"];
						$reff_receipt["date"]=$_POST["select_date"];
						$reff_receipt["pay_type"]= $pay_type;
						
						$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
				// reff receipt end 
				
				deposit($final,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
				$n=$other_name[name];
				$receipt_data[$n]+=$final;
				
				$total=$after_other_rest_data;
				}
			 }
			 else{
				 	$monthary[]=$x;			
				if($final>=$total){
					
					$recept="fee-".$other_name[name];
				$recData[$recept]+=$total;
				
				$insAry=array(  "ins_name"	=>	$num."-other",
					"fine"		=>	$total,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $_POST["select_date"],
					"status"	=>	"1",
					"idno"		=>	$id
					
				);
				$insAry["school_id"]=$_SESSION["admin"]["school_id"];
				$insAry["added_by"]=$_SESSION["admin"]["uid"];
				$insAry["session"]=$_SESSION["admin"]["session"];
				
				$inup=$db->insertAry("installment_fine",$insAry);
								
				// reff receipt start
				
				$reff_receipt=array("amount"=>$total,
										"type"=>$other_name[name],
										"idno"=>$id,
										"date"=>$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"];
						$reff_receipt["date"]=$_POST["select_date"];
						$reff_receipt["pay_type"]= $pay_type;
						
						$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
				// reff receipt end 
				deposit($total,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
				
				$n=$other_name[name];
				$receipt_data[$n]+=$total;
				
				$rest_data='';				
			    $total= $rest_data;				
				}
				
				if($final<$total){					
				$recept="fee-".$other_name[name];
				$recData[$recept]+=$final;
					
					$after_other_rest_data=$total-$final;
				$insAry=array(  "ins_name"	=>	$num."-other",
					"fine"		=>	$final,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $_POST["select_date"],
					"status"	=>	"1",
					"idno"		=>	$id
					
				);
				$insAry["school_id"]=$_SESSION["admin"]["school_id"];
				$insAry["added_by"]=$_SESSION["admin"]["uid"];
				$insAry["session"]=$_SESSION["admin"]["session"];
				
				$inup=$db->insertAry("installment_fine",$insAry);
								
				// reff receipt start				
				$reff_receipt=array("amount"=>$final,
										"type"=>$other_name[name],
										"idno"=>$id,
										"date"=>$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"];
						$reff_receipt["date"]=$_POST["select_date"];
						$reff_receipt["pay_type"]= $pay_type;
						$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
				// reff receipt end 
				deposit($final,$id,$inup,"other".$num."_fee","Student Other fee ".$other_name[name]);
				$n=$other_name[name];
				$receipt_data[$n]+=$final;
				$total=$after_other_rest_data;
				}
				$e =$total-$after_other_rest_data;
				//if($e=='0') { //break; exit(); }
			 }		 
			}
		  }
		}				
}	

for($i=1; $i<=$m; $i++){
$m1[]=$i;
}
    $m2= implode(',',$m1);	
	$rrid7= implode('-',$rrid);
		//$c_month= implode(',',$_REQUEST['countmonth']);
			$ssp=$_REQUEST['countmonth'];
	
	        $sr=trim($ssp,"[ ]");
	 
	
		
		
	$deta= json_encode($receipt_data);	
	
		$id=$db->getval("select max(id) from receipt where school_id='".$_SESSION["admin"]["school_id"]."' and type='fee'")+1;
			
		$rec1= array(
		      'id'=>$id,
	          'idno'=>$_GET["id"],
			  "udate"=>date('Y-m-d'),
			  "detail"=>$deta,
			  "amount"=>$_REQUEST['total'],
			  "payment_mode"=>"online",
			  "remaining"=>0,
			  "total"=>$_REQUEST['total'],
			  "Months"=>$sr,
			  "session"=>$session,
			  "status"=>0,
			  "class"=>$clas,
			  "type"=>"fee",
			  "install"=>$m,
			  "paytype"=>"overallFee",
			  "school_id"=>1,
			  "added_by"=>1,
			  "rrid"=>$rrid7,
			  "txnid" => $txnid,
			  "tdate" => $_POST["select_date"]
				);
// 			print_r($rec1);
	
	$db->insertAry("receipt",$rec1);
			
if(is_array($rrid) && count($rrid)>0)
	{  
	echo json_encode(array('status' => 1, 'msg'=>'Data updated Successfully..!'));   
// 	header("location:https://www.savarnacloud.com/payment/process.php?online_payment_status=1&txnid=".$_REQUEST[txnid]);
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'Data not found..!'));
		
// 		header("location:https://www.savarnacloud.com/payment/process.php?online_payment_status=0&txnid=".$_REQUEST[txnid]);
		
	}

?>