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

		 /*$id= 832;
		 $_GET["id"]= $id;
		 $total=6000; // payment given by parent
		 $_REQUEST['total']=$total;
		 $clas= 'I';
		 $m=12;
		 $session='2019-2020';*/
		 		 		 
		 $id= $_REQUEST['idno'];
		 $_GET["id"]= $id;
		 $total=$_REQUEST['total']; // payment given by parent
		 $m=$_REQUEST['month'];
		 $session=$_REQUEST['session'];	
		 $clas= $_REQUEST['class'];
		 
		 $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';
     //h s..

         

$tution=$db->getRow("SELECT * from hostelinstallment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."'" );

for($x=1; $x<=$m; $x++)
 {	
	// tution fee pay start
    if(!empty($tution[i.$x])){
		if(empty($tution[ip.$x]) OR $tution[ip.$x]==null){
			$monthary[]=$x;
			if(!empty($total)){
				$monthary[]=$x;
			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-hostel";
				$recData[$recept]+=$total;
				$reff_receipt=array(				                   
									"type"=>"hostelfee",
									"inst_id"=>"$x",
									"amount"=>$total,
									"date"=>$udate,
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);																				
					$insAry=array("ip".($x)=>$total);
					$given=$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-hostel";
				$recData[$recept]+=$tution[i.$x];
					$reff_receipt=array(				                   
									"type"=>"hostelfee",
									"inst_id"=>"$x",
									"amount"=>$tution[i.$x],
									"date"=>$udate,
									"idno"=>$_GET["id"],
									"session"=>$_SESSION["admin"]["session"]
									);
									
					$insAry=array( "ip".($x)=>$tution[i.$x]);				
									
			 $given=$tution[i.$x];			
			 $total= $rest_data;				 
			}				
				$inup=$db->updateAry("hostelinstallment",$tution_inp," where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");
								
					installment_time($_GET["id"],"hp".$x);
						
                    $reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
					$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
					$reff_receipt["pay_type"]= $pay_type;
					$rrid[]=$db->insertAry("reff_receipt",$reff_receipt);
					
					$receipt_data["hostel"]+=$given;
					deposit($given,$_GET["id"],json_encode($insAry),"hostel","Student hostel 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]);													
				$insAry=array( "ip1".($x)=>$tution[i.$x]);													
				$given=$tution[i.$x];			
				$given1=$tution[i.$x]-$tution[ip.$x];			
			
			    $total= $rest_data;				 
			    //$total1= $rest_data-$tution[i.$x];				 
			}
			
			$inup=$db->query("update hostelinstallment 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

         }	 
//h e..

// other fee start..........
			
		$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 hostel_otherfee_structure  WHERE class='$clas' and month='$x' and (otype='$id' or otype='$aryForm[htype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");
		
		$other_name=$db->getRows(" SELECT @serial := @serial+1 AS serial_number, id,name from hostel_otherfee_structure,(SELECT @serial:= 0) AS s  WHERE class='$clas' and (otype='$id' or otype='$aryForm[htype]' 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 hostel_otherfee_structure  WHERE class='$clas' and name='$other_name[name]'  and (otype='$id'  or otype='$aryForm[htype]' or otype='') and session='".$_SESSION["admin"]["session"]."' ");

	$total_other_discount=$db->getVal("SELECT COALESCE(sum(discount),0) as result from hostelotherfee_discount  WHERE  idno='$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
					
					
					installment_time($id,"hostelother".$num."_detail",$udate);
									
				if($final>=$total){
					$recept="fee-".$other_name[name];
				$recData[$recept]+=$total;
					
				$insAry=array(  "ins_name"	=>	$num."-otherfeehostel",
					"fine"		=>	$total,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $udate,
					"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["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."-otherfeehostel",
					"fine"		=>	$final,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $udate,
					"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["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."-otherhostel",
					"fine"		=>	$total,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $udate,
					"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["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."-otherhostel",
					"fine"		=>	$final,
					"reasion"	=>	$other_name[name],
					'udate'		=>  $udate,
					"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["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;
				}
			 }			 
			}
		  }
		}
		//other fee end
 }

//print_r($receipt_data); 

for($i=1; $i<=$m; $i++){
$m1[]=$i;
}
    $m2= implode(',',$m1);	
	$rrid7= implode('-',$rrid);
	
	$deta= json_encode($receipt_data);	
				
		$rec1= array(
	          'idno'=>$_GET["id"],
			  "udate"=>date('Y-m-d'),
			  "detail"=>$deta,
			  "amount"=>$_REQUEST['total'],
			  "payment_mode"=>"online",
			  "remaining"=>0,
			  "total"=>$_REQUEST['total'],
			  "Months"=>$m2,
			  "session"=>$session,
			  "status"=>0,
			  "class"=>$clas,
			  "type"=>"fee",
			  "install"=>$m,
			  "paytype"=>"overallFee",
			  "school_id"=>1,
			  "added_by"=>1,
			  "rrid"=>$rrid7
				);
			//print_r($rec1);
	
	$db->insertAry("hostel_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]);

		$msg="Data Inserted Successfully..!";
	header("location:https://savarnacloud.com/online_payment/hostel.php?id=".$id."&msg=".$msg);
	
	}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]);

		$msg="Data not found..!";
	header("location:https://savarnacloud.com/online_payment/hostel.php?msg=".$msg);
					
	}

?>