<?php
include("../db.php");

header("Access-Control-Allow-Origin: * ");

	$idno=$_REQUEST['idno'];
	$session=$_REQUEST['session'];
	$school_id=$_REQUEST['school_id'];
	$idno=mysql_real_escape_string($idno);
	$session=mysql_real_escape_string($session);
	$school_id=mysql_real_escape_string($school_id);
						$tution=$bus=$hostel=$hostel_other=$other=array();$fee=array();
						$current=array();
						$month=date("m");

$new=$month-3;
//echo $new;exit;
						$mn=$month;
						// $current[1]["order"]=1;
					 	// $current[1]["month"]="Total";
						$current["subject"]='Current Month-('.date("F", mktime(0, 0, 0, $mn)).')';
						//echo $month;exit;
		////---------------Tution--------------------------////				
						$aryInstall=$db->getRow("select * from installment where school_id='".$school_id."' and idno='".$idno."' and session='".$session."'");
						//echo $db->getLastQuery();exit;
						//print_r($aryInstall);exit;
						$install=$aryInstall;
						//echo "<pre>";
						//print_r($install);
						$feeCount=array();$totalclasspayfee=0;$totalbuspayfee=0;$busFine=0;$schoolFine=0;
						//$tution["type"]="tution-fee";
						for($i=1;$i<=12;$i++){
							$iis="i".$i;
							$iip="ip".$i;
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
							?>
                           <?php $ij=$i;
						   $tution[$i]["order"]= $i;
						  if($ij<=9){
							$newDate=date("Y")."-".($ij+3)."-1";
							$tution[$i]["month"]= date("F",strtotime($newDate));//."(".($ij+3).")";
						  }
						  else{
							$newDate=date("Y")."-".($ij-9)."-1";
							$tution[$i]["month"]= date("F",strtotime($newDate));//."(".($ij-9).")";
						  } ?>
              					
                                <?php $tution[$i]["amount"]= $aryInstall[$iis]; ?>
                               <?php $tution[$i]["given"]= $aryInstall[$iip]; 
                               
                                      $current["amount"]+= $aryInstall[$iis];
				$current["given"]+= $aryInstall[$iip];
				
                               ?>                                
                               <?php $udate=$db->getVal("select date from installment_time where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$iip."'");
                               
                               //print_r($update); die;
								if(trim($udate)!="")$tution[$i]["date"]= date("d-M-Y",strtotime($udate));//.$db->getErMsg().$db->getLastQuery(); ?>
                               <?php if($aryInstall[$iip]>0)$tution[$i]["status"]='Paid';else $tution[$i]["status"]='Due'; ?>
                        <?php 
						
				}}		
			////-------------------BUS-----------------------////
			
						for($i=1;$i<=12;$i++){
							$iis="b".$i;
							$iip="bp".$i;
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
							?>
                           <?php $ij=$i;
						   $bus[$i]["order"]= $i;
						  if($ij<=9){
							$newDate=date("Y")."-".($ij+3)."-1";
							$bus[$i]["month"]= date("F",strtotime($newDate));//."(".($ij+3).")";
						  }
						  else{
							$newDate=date("Y")."-".($ij-9)."-1";
							$bus[$i]["month"]= date("F",strtotime($newDate));//."(".($ij-9).")";
						  } ?>
              					
                                <?php $bus[$i]["amount"]= $aryInstall[$iis]; ?>
                               <?php $bus[$i]["given"]= $aryInstall[$iip];
                               
                                    
				    $current["given"]+= $aryInstall[$iip];
                                ?>                                
                               <?php $udate=$db->getVal("select date from installment_time where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$iip."'");
								if(trim($udate)!="")$bus[$i]["date"]= date("d-M-Y",strtotime($udate));//.$db->getErMsg().$db->getLastQuery(); ?>
                               <?php if($aryInstall[$iip]>0)$bus[$i]["status"]='Paid';else $bus[$i]["status"]='Due'; ?>
                        <?php
                        		
						 }
						 
						 }
						 // for transport or bus
						 $current["amount"]+= $aryInstall[$iis]*12;
						
		////-----------------Hostel------------------------////
						
						$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$school_id."' and idno='".$idno."' and session='".$session."'");
						//$tution["type"]="tution-fee";
						for($i=1;$i<=12;$i++){
							$iis="i".$i;
							$iip="ip".$i;
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
							?>
                           <?php $ij=$i;
						   $hostel[$i]["order"]= $i;
						  if($ij<=9){
							$newDate=date("Y")."-".($ij+3)."-1";
							$hostel[$i]["month"]= date("F",strtotime($newDate));//."(".($ij+3).")";
						  }
						  else{
							$newDate=date("Y")."-".($ij-9)."-1";
							$hostel[$i]["month"]= date("F",strtotime($newDate));//."(".($ij-9).")";
						  } ?>
              					
                                <?php $hostel[$i]["amount"]= $aryInstall[$iis]; ?>
                               <?php $hostel[$i]["given"]= $aryInstall[$iip]; 
                                    
                                     //$current["amount"]+= $aryInstall[$iis];
				     $current["given"]+= $aryInstall[$iip];
                               
                               ?>                                
                               <?php $udate=$db->getVal("select date from installment_time where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$iip."'");
								if(trim($udate)!="")$hostel[$i]["date"]= date("d-M-Y",strtotime($udate));//.$db->getErMsg().$db->getLastQuery(); ?>
                               <?php if($aryInstall[$iip]>0)$hostel[$i]["status"]='Paid';else $hostel[$i]["status"]='Due'; ?>
                        <?php
							
						 }}
						 
						  $current["amount"]+= $aryInstall[$iis]*12;
						 $class=$db->getVal("Select class from student where idno='".$idno."'");
/*							$extrafee=array();$extfee=array();
					   		$checkextFee=$db->getRows("select * from extrafee_type where school_id='".$school_id."' and session='".$session."' and class='".$class."'");
						//echo $db->getLastQuery();//print_r($checkextFee);	
					if(is_array($checkextFee) && count($checkextFee)>0){$ij=0;
						foreach($checkextFee as $chkExtFee){$ij++;
							$mycheckInstall=$db->getRows("select * from extrafee where idno='".$idno."' and type='".$chkExtFee["id"]."'");
							$countFee=count($mycheckInstall);
							if(is_array($mycheckInstall) && count($mycheckInstall)>0){
								 $extrafee[$ij]["type"]=$chkExtFee["type"];$i=0;
								 foreach($mycheckInstall as $myinstall){$i++;
								 $ipi=ltrim ($myinstall["pid"], 'p');
									if($ipi<=9){
										$newDate=date("Y")."-".($ipi+3)."-1";
										$extrafee.$ij[$i]["month"]= date("F",strtotime($newDate));//."(".($ij+3).")";
									  }
									  else{
										$newDate=date("Y")."-".($ipi-9)."-1";
										$extrafee.$ij[$i]["month"]= date("F",strtotime($newDate));//."(".($ij-9).")";
									  }
									  $extrafee.$ij[$i]["amount"]=$myinstall["fee"];
									  $extrafee.$ij[$i]["given"]=$myinstall["pay"];
									  $udate=$db->getVal("select date from installment_time where school_id='".$school_id."' and idno='".$idno."' and ins_name='ext-".$myinstall["type"]."-".$ipi."'");
									if(trim($udate)!="") $extrafee.$ij[$i]["date"]=date("d-M-Y",strtotime($udate));
									if($myinstall["pay"]>0)$extrafee.$ij[$i]["status"]='Paid';else $extrafee.$ij[$i]["status"]='Due';
								 }
							}
						}
					}$j=0;
					foreach($extrafee as $ext){$j++;
						$extfee[$ext]=array($extrafee.$j);
					}
*/

		 ////--------------------------Other---------------------////
		 
			/*$utype=$db->getVal("Select stype from student where idno='".$idno."'");
 					 $all_other=$db->getRows("select * from otherfee_structure where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (otype='' OR otype='".$utype."' OR otype='".$idno."')");
					 //echo"<pre>";print_r($all_other);echo"</pre>";
					  if(is_array($all_other) && count($all_other))
                      {$i=0; foreach($all_other as $iPro)
                        {
							$i++;
							$paid_Fee=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherfee'");
							$paid_Fee1=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-other'");
							
							//print_r($paid_Fee);
							//echo '<br>';
							if($paid_Fee1>0)
								$paid_Fee+=$paid_Fee1;
							$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$i."-other'");
							$other_Fee=$db->getRow("select discount,detail from otherfee_discount where school_id='".$school_id."' and idno='".$idno."' and other_id='".$iPro["id"]."'");		
							$OtherSiteTotal+=(int)$other_Fee["discount"];
							$other[$i]["order"]= $i;
							$other[$i]["month"]=$iPro["name"];
							$other[$i]["amount"]+=$iPro["fee"]-(int)$other_Fee["discount"];
							$other[$i]["given"]+=$paid_Fee;
							
							$udate=$db->getVal("select udate from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%otherfee%'");
								if(trim($udate)!="")$other[$i]["date"]= date("d-M-Y",strtotime($udate));
								if($paid_Fee=="" || $paid_Fee=="0")$other[$i]["status"]='Due';  else $other[$i]["status"]='Paid';
						}
					  }*/
					  
			///////----------------------Other Fee Total--------------------////////////////		  
					 //echo 1; die;
					 $utype=$db->getVal("Select stype from student where idno='".$idno."'");
 					 $all_other=$db->getRows("select * from otherfee_structure where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (otype='' OR otype='".$utype."' OR otype='".$idno."')");
					 
					// $all_other=$db->getRows("select * from otherfee_structure where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (otype='' OR otype='".$utype."' OR otype='".$idno."') and month<='".$new."'");
					 //echo"<pre>";print_r($all_other);echo"</pre>";
                      //echo $db->getLastQuery();
					  if(is_array($all_other) && count($all_other))
                      {$i=0; foreach($all_other as $iPro)
                        {
							$i++;
							$paid_Fee=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherfee'");
							$paid_Fee1=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-other'");
							//print_r($paid_Fee);
							//echo '<br>';
							
							if($paid_Fee1>0)
								$paid_Fee+=$paid_Fee1;
							$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$i."-other'");
							$other_Fee=$db->getRow("select discount,detail from otherfee_discount where school_id='".$school_id."' and idno='".$idno."' and other_id='".$iPro["id"]."'");		
							$OtherSiteTotal+=(int)$other_Fee["discount"];
							$other[$i]["order"]= $i;
							$other[$i]["month"]=$iPro["name"];
							$other[$i]["amount"]+=$iPro["fee"]-(int)$other_Fee["discount"];
							$other[$i]["given"]+=$paid_Fee;
							
								//$current["amount"]+= $iPro["fee"]-(int)$other_Fee["discount"];
								$current["given"]+= $paid_Fee;
								
								$udate=$db->getVal("select udate from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%otherfee%'");
								if(trim($udate)!="")$other[$i]["date"]= date("d-M-Y",strtotime($udate));
								if($paid_Fee=="" || $paid_Fee=="0")$other[$i]["status"]='Due';  else $other[$i]["status"]='Paid';
								
								
						}
					  }
			////----------------------Hostel Other Fee-------------------////
			
					  $utype=$db->getVal("Select htype from student where idno='".$idno."'");
					  if($utype!=""){
					 $all_other=$db->getRows("select * from hostel_otherfee_structure where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (otype='' OR otype='".$utype."' OR otype='".$idno."')");
					 //echo"<pre>";print_r($all_other);echo"</pre>";exit;
					  if(is_array($all_other) && count($all_other))
                      {$i=0; foreach($all_other as $iPro)
                        {$i++;$ijk=trim($iPro['month']," ");$amt=0;
							$paid_Fee=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherfeehostel'");
							$paid_Fee1=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherhostel'");
							
							
							if($paid_Fee1>0)
								$paid_Fee+=$paid_Fee1;
							$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$i."-otherhostel'");
							//print_r($fineAry);echo $db->getLastQuery();
							$other_Fee=$db->getVal("select sum(discount) from hostelotherfee_discount where school_id='".$school_id."' and idno='".$idno."' and other_id='".$iPro["id"]."'");		
							$OtherSiteTotalhostel+=(int)$other_Fee;
							$name=$iPro['name'];
							$name=trim($name," ");
							$amt=$iPro["fee"];
							$amt=($iPro["fee"]-(int)$other_Fee);
							
							$hostel_other[$i]["order"]= $i;
							$hostel_other[$i]["month"]=$name;
							$hostel_other[$i]["amount"]=$amt;
							$hostel_other[$i]["given"]=$paid_Fee;
							$udate=$db->getVal("select udate from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%otherfeehostel%'");
								if(trim($udate)!="")$hostel_other[$i]["date"]= date("d-M-Y",strtotime($udate));
								if($paid_Fee=="" || $paid_Fee=="0"){ $hostel_other[$i]["status"]='Due';} else $hostel_other[$i]["status"]='Paid';
						}
					  }
					 }
					 
					 
					 

					 
					 
			
		////---------------------------Hostel other fee total-------------------/////
		if($utype!=""){
					 $all_other=$db->getRows("select * from hostel_otherfee_structure where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (otype='' OR otype='".$utype."' OR otype='".$idno."') and month<='".$month."'");
					 //echo"<pre>";print_r($all_other);echo"</pre>";exit;
					  if(is_array($all_other) && count($all_other))
                      {$i=0; foreach($all_other as $iPro)
                        {$i++;$ijk=trim($iPro['month']," ");$amt=0;
							$paid_Fee=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherfeehostel'");
							$paid_Fee1=$db->getVal("select fine from installment_fine where school_id='".$school_id."' and idno='".$idno."' and reasion='".$iPro["name"]."' and ins_name like '%-otherhostel'");
							if($paid_Fee1>0)
								$paid_Fee+=$paid_Fee1;
							$fineAry=$db->getRow("select fine,reasion from installment_fine where school_id='".$school_id."' and idno='".$idno."' and ins_name='".$i."-otherhostel'");
							//print_r($fineAry);echo $db->getLastQuery();
							$other_Fee=$db->getVal("select sum(discount) from hostelotherfee_discount where school_id='".$school_id."' and idno='".$idno."' and other_id='".$iPro["id"]."'");		
							$OtherSiteTotalhostel+=(int)$other_Fee;
							$name=$iPro['name'];
							$name=trim($name," ");
							$amt=$iPro["fee"];
							$amt=($iPro["fee"]-(int)$other_Fee);
								$current["amount"]+= $amt;
								$current["given"]+= $paid_Fee;
						}
					  }
					 }
					//$current[1]["date"]="";
					
					$current["amount"]+=$other[$i]["amount"];
					//$current["amount"]+=;;
						
					/* $current["remain"]=$current["amount"]-$current["given"];
				
					 if($current["amount"]==$current["given"])
				
						 $current["status"]="paid";	
					 else 
				
					 	 $current["status"]="due";*/
					 					 			 
				$count=1;	 
				if(is_array($tution) && count($tution)>0){ $data[]=array('subject'=>'Tution Fee','sequence'=>$count,'months'=>$tution); $count++;}
					 
				if(is_array($bus) && count($bus)>0){ $data[]=array('subject'=>'Transport Fee','sequence'=>$count,'months'=>$bus); $count++;}
					
				if(is_array($hostel) && count($hostel)>0){ $data[]=array('subject'=>'Hostel Fee','sequence'=>$count,'months'=>$hostel);$count++;}
					 
				if(is_array($hostel_other) && count($hostel_other)>0){ $data[]=array('subject'=>'Hostel Other Fee','sequence'=>$count,'months'=>$hostel_other);$count++;}
					
   			    if(is_array($other) && count($other)>0){ $data[]=array('subject'=>'Other Fee','sequence'=>$count,'months'=>$other);$count++;}
				
				
				
				
				//-------------------extra-fee-start-------------------------------------------//


                  $fee_type= $db->getRows("select type from extrafee where school_id='1' and idno='".$idno."' and session='".$session."' group by type");
                   
                   
                  //print_r($fee_type); die;
                   $x=1;
                  // $count=1;
                   foreach($fee_type as $type){
                   
                   $type=$type[type]; 
                   
                   $fee_name=$db->getRow("select type from  extrafee_type where id='$type'");
                   
                   //print_r($fee_name[type]); 
                   
                   $extra_fee_subject=$fee_name[type];

                            $aryInstall=$db->getRows("select * from extrafee where school_id='".$school_id."' and idno='".$idno."' and session='".$session."' and type='$type'");
                        //$q="select * from extrafee where school_id='".$school_id."' and idno='".$idno."' and session='".$session."' and type='$type'";
                       // echo $q; die;
                        // $extra[$i]["month"]='March';
                         //$extra[$i]["order"]= '1';
                         
                         /* $extra[$i]["amount"]='100';
                          $extra[$i]["given"]='500';
                         
                          $extra[$i]["status"]='Due';*/
                          //print_r($aryInstall);
                          $monthsList = array(
    "April"=>'p1',
    "May"=>'p2',
    "June"=>'p3',
    "July "=>'p4',
    "August"=>'p5',
    "September"=>'p6',
    "October"=>'p7',
    "November"=>'p8',
    "December"=>'p9',
    "January"=>'p10',
    "February"=>'p11',
    "March"=>'p12'
    
);                          
                         $i=1;
						
                         foreach($aryInstall as $install){
                         // $install=$aryInstall;
                         
                         //print_r($install);
                         $m_id= $install[pid]; 
                         
                        $months= array_search("$m_id", $monthsList);
                        
                        $amt_fee=$install[fee];
                       // echo $amt;die;
                       // echo $months;
                         
                        
                         //for($i=1;$i<=count($install);$i++){
                         
                          $extra[$i]["order"]=$i;
                           $extra[$i]["month"]= $months;
                          // $extra[$i]["month"]= $months;
                         $extra[$i]["amount"]=$amt_fee;
                         $extra[$i]["given"]=$install[pay];
                         
                         $current["amount"]+= $amt_fee;
				    $current["given"]+= $install[pay];
                         
                        // $extra[$i]["fee_type"]=;
                          if($install[pay]>0)$extra[$i]["status"]='Paid';else $extra[$i]["status"]='Due';
						  
						  
                        // }
                          
                          $i++;
						  
                          }
                            //$value=array_values($install);
							
							//$current["amount"]+= $amt_fee*12;
                          if(is_array($extra) && count($extra)>0){ $data[]=array('subject'=>$extra_fee_subject,'sequence'=>$count,'months'=>$extra);$count++;}
                          $x++;
                          
                         }
			




//-------------------extra-fee-end-------------------------------------------//
				$current["remain"]=$current["amount"]-$current["given"];
				
					 if($current["amount"]==$current["given"])
				
						 $current["status"]="paid";	
					 else 
				
					 	 $current["status"]="due";
				
   			    
   			    
                // if(is_array($extra) && count($extra)>0){ $data[]=array('subject'=>$extra_fee_subject,'sequence'=>$count,'months'=>$extra);$count++;}
   			    
   			   // if(is_array($extra) && count($extra)>0){ $data[]=array('subject'=>$extra_fee_subject,'sequence'=>$count,'months'=>$extra);$count++;}
   			   // if(is_array($extra) && count($extra)>0){ $data[]=array('subject'=>$extra_fee_subject,'sequence'=>$count,'months'=>$extra);$count++;}
   			    
   			    
					
					//$current_data=array('subject'=>'Current Month-('.date("F", mktime(0, 0, 0, $month)).')','months'=>$current);
		/*$data=array(
		  			'Tution Fee'=>array('sequence'=>'1','months'=>$tution),
					'Transport Fee'=>array('sequence'=>'2','months'=>$bus),
					'Hostel Fee'=>array('sequence'=>'3','months'=>$hostel), 
					'Hostel Other Fee'=>array('sequence'=>'4','months'=>$hostel_other), 
					'Other Fee'=>array('sequence'=>'5','months'=>$other));*/
	
	if(is_array($install) && count($install)>0)
	{  
		echo json_encode(array('status' => 1, 'msg'=>'Data found Successfully..!', "data"=>$data, "current_status"=>$current));    
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'Data not found..!'));
	}
//echo $arr["name"];	

?>