<?php
include("../db.php");
header('Content-Type: application/json'); 

/*

 //$m=$_REQUEST[month2];
	  $id= $_REQUEST[id2];
	  $cls= $_REQUEST[cl2];
	  $typ= $_REQUEST[type2];
	  $bpl= $_REQUEST[bpl];
	  $_SESSION["admin"]["session"]=$_REQUEST[session];
      $_SESSION["admin"]["school_id"]=$_REQUEST[school_id];
*/

	
	  $m=$_REQUEST[month2];
	  $id= $_REQUEST[id2];
	  $cls= $_REQUEST[cl2];
	  $typ= $_REQUEST[type2];
	  $bpl= $_REQUEST[bpl];
	  $_SESSION["admin"]["session"]=$_REQUEST[session];
      $_SESSION["admin"]["school_id"]=$_REQUEST[school_id];



	  $extrafee_list= $db->getRows("SELECT id from extrafee_type  WHERE class='$cls' and session='".$_SESSION["admin"]["session"]."' ");
	  //echo $ss="SELECT id from extrafee_type  WHERE class='$cls' and session='".$_SESSION["admin"]["session"]."' ";
	
	foreach($extrafee_list as $extra_list)
	           {
	
	 $extra_list1[]=$extra_list[id];
	
	            }
	//print_r($extra_list1);
	 $extralist=implode(',',$extra_list1);
	 
	 
	 
	     $feeArray = array();

	 for($i=1; $i<=$m; $i++){
 
     $total=$db->getVal("SELECT    COALESCE(sum(i".$i."),0)  as result from installment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."' and (ip".$i."='' OR ip".$i." IS NULL )");

  // echo "SELECT    COALESCE(sum(i".$i."),0)  as result from installment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."' and (ip".$i."='' OR ip".$i." IS NULL )";
	$total_bus=$db->getVal("SELECT COALESCE(sum(b".$i."),0)  as result from installment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."' and (bp".$i."='' OR  bp".$i." IS NULL)");
	
	$total_extra=$db->getVal("SELECT sum(fee) as result from extrafee  WHERE type in ($extralist) and pid='p".$i."' and idno='$id' and session='".$_SESSION["admin"]["session"]."' and (pay='' OR pay IS NULL)");
	
	//other fee
	//if($bpl==0){
	$total_other=$db->getVal("SELECT COALESCE(sum(fee),0) as result from otherfee_structure  WHERE class='$cls' and month='$i' and (otype='$id'  or otype='$typ' 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 month='$i' and class='$cls' and session='".$_SESSION["admin"]["session"]."' ");
	//}
	// other fee given amt start
	$other_fee_name= $db->getRows("SELECT name from otherfee_structure  WHERE class='$cls' and month='$i' and (otype='$id' or otype='$typ' or otype='') and session='".$_SESSION["admin"]["session"]."' ");
		
	foreach($other_fee_name as $other_fee_name1){
		
		$other_fee_name1[name];
		$string .= "'$other_fee_name1[name]'".',';
					
	}	
	// other fee end 

	if($i==12){

	$total12=$db->getVal("SELECT    (COALESCE(i12,0)-COALESCE(ip12,0))  as result from installment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."' and (ip12!='' and ip12 is NOT NULL) ");

	

	$total_bus12=$db->getVal("SELECT    (COALESCE(b12,0)-COALESCE(bp12,0))  as result from installment  WHERE idno='$id' and session='".$_SESSION["admin"]["session"]."' and (bp".$i."!='' AND bp".$i." IS NOT NULL ) ");

	$total_extra12=$db->getVal("SELECT (sum(fee)-sum(pay)) as result from extrafee  WHERE type in ($extralist) and pid='p12' and idno='$id' and session='".$_SESSION["admin"]["session"]."' and pay!=0 ");	
}	


 	$tot+=$total+$total12+$total_bus+$total_bus12+$total_extra+$total_extra12+$total_other-$total_other_discount;
 
 
 	if($i==12)
              {
                  
      
      
      
      	$total_extra_monthly=$db->getRows("SELECT extrafee.id, extrafee.type, extrafee.pid, extrafee.fee, extrafee.idno, extrafee.pay,extrafee_type.type as extra_name, extrafee.session   from extrafee INNER join extrafee_type on extrafee_type.id =extrafee.type WHERE extrafee.type in ($extralist) and  extrafee.idno='$id'  and extrafee.pid='p".$i."' and extrafee.session='".$_SESSION["admin"]["session"]."'");
			
		//	echo $ss="SELECT extrafee.id, extrafee.type, extrafee.pid, extrafee.fee, extrafee.idno, extrafee.pay,extrafee_type.type as extra_name, extrafee.session   from extrafee INNER join extrafee_type on extrafee_type.id =extrafee.type WHERE extrafee.type in ($extralist) and  extrafee.idno='$id' and extrafee.pay='' and extrafee.pid='p".$i."' and extrafee.session='".$_SESSION["admin"]["session"]."'";
						  $extraArray = array();
			foreach($total_extra_monthly as $total_extra_monthly1)
			{			
			$ins= "i".$i;			
			if(!empty($total_extra_monthly1))
				{
				$installment_name='Extra Fee';
					
				if(!empty($total_extra_monthly1[fee]))
				{
				
				
			//	$jsone[]=array("head"=>$total_extra_monthly1['extra_name'],"amount"=>$total_extra_monthly1[fee]);
				
				    	            array_push($extraArray, ["head"=>$total_extra_monthly1['extra_name'],"amount"=>$total_extra_monthly1['fee']-$total_extra_monthly1['pay']]);


				                

				}
			}			
		}            
                  
                  
              }
 
 	else
 	{
 
 
 				
			$total_extra_monthly=$db->getRows("SELECT extrafee.id, extrafee.type, extrafee.pid, extrafee.fee, extrafee.idno, extrafee.pay,extrafee_type.type as extra_name, extrafee.session   from extrafee INNER join extrafee_type on extrafee_type.id =extrafee.type WHERE extrafee.type in ($extralist) and  extrafee.idno='$id' and extrafee.pay='' and extrafee.pid='p".$i."' and extrafee.session='".$_SESSION["admin"]["session"]."'");
			
		//	echo $ss="SELECT extrafee.id, extrafee.type, extrafee.pid, extrafee.fee, extrafee.idno, extrafee.pay,extrafee_type.type as extra_name, extrafee.session   from extrafee INNER join extrafee_type on extrafee_type.id =extrafee.type WHERE extrafee.type in ($extralist) and  extrafee.idno='$id' and extrafee.pay='' and extrafee.pid='p".$i."' and extrafee.session='".$_SESSION["admin"]["session"]."'";
						  $extraArray = array();
			foreach($total_extra_monthly as $total_extra_monthly1)
			{			
			$ins= "i".$i;			
			if(!empty($total_extra_monthly1))
				{
				$installment_name='Extra Fee';
					
				if(!empty($total_extra_monthly1[fee]))
				{
				
				
			//	$jsone[]=array("head"=>$total_extra_monthly1['extra_name'],"amount"=>$total_extra_monthly1[fee]);
				
				    	            array_push($extraArray, ["head"=>$total_extra_monthly1['extra_name'],"amount"=>$total_extra_monthly1['fee']]);


				                

				}
			}			
		}
		
 	}	
		
		

		if($i==12)
              {
$total_extra_monthly12=$db->getRows("SELECT (fee-pay) as result, type from extrafee  WHERE type in ($extralist) and pid='p12' and idno='$id' and session='".$_SESSION["admin"]["session"]."' and pay!=0 ");

    //print_r($total_extra_monthly12);
   

   foreach($total_extra_monthly12 as $total_extra_monthly121)
                        {

       $total_extra_monthly_name12=$db->getVal("SELECT  type from extrafee_type  WHERE  id='".$total_extra_monthly121['type']."' ");
	
       $installment_name= $total_extra_monthly_name12;
         if($total_extra_monthly121[result]>0)
         {
   	     //echo "<script>$('#fee12').append('<td class=test_value style=width:100px >$total_extra_monthly_name12<br>$total_extra_monthly121[result]</td>');</script>";

                 }     }	
          }
 
 
 
 
 
 	//$other_fee_data=$db->getRows("SELECT name,  fee, discount  from otherfee_structure inner JOIN otherfee_discount on  otherfee_structure.id=otherfee_discount.other_id WHERE otherfee_structure.class='V'  and otherfee_structure.month='$i' and (otherfee_structure.otype='$id' or otherfee_structure.otype='$typ' or otherfee_structure.otype='') and otherfee_structure.session='".$_SESSION["admin"]["session"]."'  and otherfee_discount.idno='$id'");

      $other_fee_data=$db->getRows("SELECT *  from otherfee_structure   WHERE class='$cls'  and month='$i' and (otype='$id' or otype='$typ' or otype='') and session='".$_SESSION["admin"]["session"]."' ");
	  
$otherArray=array();
		foreach($other_fee_data as $other_fee_data2){
			
		$other_dis= $db->getVal("SELECT discount FROM `otherfee_discount` WHERE other_id='$other_fee_data2[id]' and idno='$id'");
					
			 $other_fee=$other_fee_data2[fee]- $other_dis;

			//if(!empty($other_fee)  ){
					
			 
				$other_final=$db->getVal(" SELECT COALESCE(sum(fine),0) as result from installment_fine WHERE idno='$id' and reasion='$other_fee_data2[name]' and session='".$_SESSION["admin"]["session"]."'");
				//echo "SELECT COALESCE(sum(fine),0) as result from installment_fine WHERE idno='$id' and reasion='$other_fee_data2[name]' and session='".$_SESSION["admin"]["session"]."'";
				$final_other= $other_fee-$other_final;
				
				if(!empty($final_other)){

			////	echo "<script>$('#fee$i').append('<td  class=test_value style=width:100px >$other_fee_data2[name]<br>$final_other</td>');</script>";
			
				 array_push($otherArray, ["head"=>$other_fee_data2['name'],"amount"=>$final_other]);

			
				}
			//}
		}
 
 
 
 
 
 
 
                 array_push($feeArray,["Tuition"=>$total+$total12,"BUS"=>$total_bus+$total_bus12,"extrafee"=>json_encode($extraArray),"otherfee"=>json_encode($otherArray) ]);

 	

	 }


	 
	


	
	$string =  rtrim($string, ',');
		
	$total_other_given=$db->getVal("SELECT COALESCE(sum(fine),0) as result from installment_fine  WHERE  idno='$id' and reasion in($string)   and session='".$_SESSION["admin"]["session"]."' ");
//}		
	 $total_amt=$tot-$total_other_given;
	 // echo "<p id='total23'>Total: <b>$total_amt</b></p>";

	 // echo "<p id='total'>Total: <b> <input type='text' value=$total_amt name='total_amt' id='total_amt' readonly style='border: none;background: transparent;' /></b></p>";
	//echo "<script>$('#mytable tr:last').after('<tr style=text-align:left; width:400px><td colspan=2>Total :' +$total_amt +'</td></tr>'); </script>";
	
	//echo "<script>$('#mytable tr:last').append('<p>Total :' +$total_amt +'</p>'); </script>";		
	
	$json['data']=$feeArray;
		echo json_encode($json);

?>