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

	//$idno=$_REQUEST['idno'];
	$session=$_REQUEST['session'];
	$school_id=$_REQUEST['school_id'];
	$fdate = $_REQUEST['from_date'];
	$tdate = $_REQUEST['to_date'];
	
	//$idno=mysql_real_escape_string($idno);
	$session=mysql_real_escape_string($session);
	$school_id=mysql_real_escape_string($school_id);
	$fdate==mysql_real_escape_string($fdate);
	$tdate==mysql_real_escape_string($tdate);
		
	//$fdate='2017-04-01';
//$tdate='2017-04-30';
	$collection=array();
	
	
 // my code

      	$overAllTotal=0;$overAlltution=0;$overAllTransport=0;$overAllhotel=0;$overAllHostelOther=$overAllOther=0;$overAllFine=0;$overAllextra=array();$overAllNewextra=0;$overAllTot=0;
		
		
		$aryFeeType=$db->getRows("select distinct(type) from extrafee_type where school_id='$school_id' and session='$session' and status=1 order by type");
		
		//echo "select distinct(type) from extrafee_type where school_id='$school_id' and session='$session' and status=1 order by type";
		
		$aryStudentList=$db->getRows("select name,fname,admissionno,idno,class,location from student where school_id='$school_id' and session='$session' and status=1  and issue_TC=0");
		
		
		//echo "select name,fname,admissionno,idno,class,location from student where school_id='$school_id' and session='$session' and status=1  and issue_TC=0";
		
		//echo $db->getErMsg().$db->getLastQuery();
		if(is_array($aryStudentList) && count($aryStudentList)>0){
			$tndate = date('Y/m/d', strtotime('-1 days',strtotime($tdate)));
			
			
				   if(is_array($aryFeeType) && count($aryFeeType)>0){
					foreach($aryFeeType as $ftype){
						
					}
				   }
				   $s=0;
					foreach($aryStudentList as $fStudent){
						$whereLocation=0;
						$tution=0;$trasnport=0;$hostel=0;$otherExtra=0;$hotherExtra=$h1otherExtra=$hostelotherExtra=0;$total=0;$fine=0;
						$installTution=$db->getRows("select ins_name from installment_time where ins_name like 'ip%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						//$q="select ins_name from installment_time where ins_name like 'ip%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'";
						//echo $q;
						if(is_array($installTution) && count($installTution)>0){
							foreach($installTution as $installT){
								$tution+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$fStudent["idno"]);
							}
							$overAlltution+=$tution;
							$total+=$tution;
						}
						$installTution=$db->getRows("select ins_name from installment_time where ins_name like 'bp%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						if(is_array($installTution) && count($installTution)>0){
							foreach($installTution as $installT){
								$trasnport+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$fStudent["idno"]);
							}
							$overAllTransport+=$trasnport;
							$total+=$trasnport;
						}
						$installTution=$db->getRows("select ins_name from installment_time where ins_name like 'hp%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						if(is_array($installTution) && count($installTution)>0){
							foreach($installTution as $installT){
								$inst="ip".preg_replace("/hp/","",$installT["ins_name"]);
								$hostel+=$db->getVal("select ".$inst." from hostelinstallment where idno=".$fStudent["idno"]);
							}
							$overAllhotel+=$hostel;
							$total+=$hostel;
						}
						$extrasum=array();
						if(is_array($aryFeeType) && count($aryFeeType)>0){
							foreach($aryFeeType as $ftype){  
								$sums=0;
								$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class='".$fStudent["class"]."'");
								
								
								//echo "select id from extrafee_type where type='".$ftype["type"]."' and class='".$fStudent["class"]."'";
								
								
								
								$installTution=$db->getRows("select ins_name from installment_time where ins_name like 'ext-".$Ftypeid."-%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");$no=array(); //if($s==0) {echo $db->getLastQuery();}
								if(is_array($installTution) && count($installTution)>0){
									foreach($installTution as $installT){
										$no=explode("-",$installT[0]);
										$sums+=$db->getVal("select pay from extrafee where idno='".$fStudent["idno"]."' and  pid='p".$no[2]."' and type='".$Ftypeid."'");
										echo $db->getErMsg();
									}
									
								}
								$extrasum[]=$sums;
								$total+=$sums;$tp=$ftype["type"];
								$overAllextra[$tp]+=$sums;
							}
						}
						$installOtherExtra=$db->getRows("select ins_name from installment_time where ins_name like 'other%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						if(is_array($installOtherExtra) && count($installOtherExtra)>0){
							foreach($installOtherExtra as $installT){
								//other1_detail
								$idSdetail=str_replace("_detail","",$installT[0]);
								$idSdetail=str_replace("other","",$idSdetail);
								$otherExtra+=$db->getVal("select fine from installment_fine where ins_name='".$idSdetail."-otherfee' and idno=".$fStudent["idno"]);
								//echo $db->getLastQuery();
							}
							$overAllOther+=$otherExtra;
							$total+=$otherExtra;
						}
						$installOtherExtra=$db->getRows("select ins_name from installment_time where ins_name like 'hostelother%' and (date between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						if(is_array($installOtherExtra) && count($installOtherExtra)>0){
							foreach($installOtherExtra as $installT){
								//other1_detail
								$idSdetail=str_replace("_detail","",$installT[0]);
								$idSdetail=str_replace("hostelother","",$idSdetail);
								$hotherExtra+=$db->getVal("select fine from installment_fine where ins_name='".$idSdetail."-otherfeehostel' and idno=".$fStudent["idno"]);          						//echo $db->getLastQuery();
								$h1otherExtra+=$db->getVal("select sum(fine) from installment_fine where ins_name='".$idSdetail."-otherhostel' and idno=".$fStudent["idno"]);
								//echo $db->getLastQuery();
							}$hostelotherExtra=$hotherExtra+$h1otherExtra;
							$overAllHostelOther+=$hostelotherExtra;
							$total+=$hostelotherExtra;
						}
						$fine=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-class' or ins_name like '%-hostel' or ins_name like '%-bus' or ins_name like '%-extra%') and (udate between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						$total+=$fine;
						$overAllFine+=$fine;
						$newExtra=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-other') and (udate between '$fdate' and '$tdate') and idno='".$fStudent["idno"]."'");
						$rdate=date("Y-m-d",strtotime($fdate));
						$sdate=date("Y-m-d",strtotime($tdate));
						$ref=$db->getRows("select * from reff_receipt where date > '$rdate' and date < '$sdate' and school_id='$school_id' and session='$session' and idno='".$fStudent["idno"]."'");
							//echo $db->getLastQuery();
								//print_r($ref);exit;
									$rids=array();$rrmonth=array();
									$rrde=$db->getRows("select * from receipt where session='$session' and school_id='$school_id' and idno='".$fStudent["idno"]."'");
									//echo $db->getLastQuery();
								   //print_r($rrde);exit;
									//echo "<pre>";print_r($rrde);echo "</pre>";
									if(is_array($rrde) && count($rrde)>0){
										foreach($rrde as $rr){
											$rl_rrid=explode("-",$rr["rrid"]);
											//print_r($rl_rrid);exit;
										foreach($ref as $reff){
											//echo $reff['given_date'];exit;
											//if($reff["date"]!=$reff["given_date"])
												///$date_ary[]=date("d-m-Y",strtotime($reff["given_date"]));
											foreach($rl_rrid as $rrids){
											//echo $rrids;exit;
											if($rrids==$reff["id"]){
													$r=$rr['id'];
													if(!($r==$ridss)) {
													$ridss=$r;
													$rrmonth[]=$rr['Months'];
													$rids[]=$ridss;}
												}
											}
										}
										
									}
								}    
								//$recno=$ridss;
									$recno= implode(",",$rids);
						$total+=$newExtra;
						$overAllNewextra+=$newExtra;
						$fmonths="";$monthname1="";$monthname=array();
						$fmonths=implode(",",$rrmonth);
					$im=explode(",",$fmonths);
					$mn=array_unique($im);$months="";
					if(is_array($mn) && count($mn)){
						foreach($mn as $ipi){					
							if($ipi!=""){
								if($months!="")$months.=",";
								if($ipi<=9){
									$newDate=date("Y")."-".($ipi+3)."-1";
									$months.= date("M",strtotime($newDate));//."(".($ij+3).")";
								}
								else{
									$newDate=date("Y")."-".($ipi-9)."-1";
									$months.= date("M",strtotime($newDate));//."(".($ij-9).")";
								}
							}
						}
					}
						
						
						//echo $db->getErMsg();
						if($total>0){ $s++;
						
                    $collection["details"][] =array(
						
						                     's.no'=>$s,
					                         'student_name'=>$fStudent["name"],
											  'father_name'=>$fStudent["fname"],
											  'addmission_no'=>$fStudent["admissionno"],
											  'class'=>$fStudent["class"],
											  'receipt_no'=>$recno,
											  'month'=>$months,
											  'tution_fee'=>$tution,
											  'transport_fee'=>$trasnport,
											  'hostel_fee'=>$hostel,

						                                          'other_extra'=>$otherExtra,
											 'fine'=>$fine,
											 'hostel_other'=>$hostelotherExtra,
											 'new_extra'=>$newExtra,
											 'income'=>'0',
											 'total'=>$total,
											 
											 
											 );
					  //$data[]=array('subject'=>$extra_fee_subject,'sequence'=>$count,'months'=>$extra);
$GrandTotal+=$total;
							 if(is_array($aryFeeType) && count($aryFeeType)>0){
								$ij=0;
								
					foreach($aryFeeType as $ftype){
								
								
							$collection["extra"][] =array('s.no'=>$s,'student_name'=>$fStudent["name"],$ftype["type"]=>$extrasum[$ij]);
							
														
							$ij++;
							}
							
							
							}
							if( count($collection["extra"])==0){ $collection["extra"]= "null"; }
						
						}
						  //$tot=$db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '$fdate' and '$tdate')  and (tfor='cash' or tfor='cheque') and session='".$_SESSION["admin"]["session"]."'"); 
						$overAllTot=$overAllTot+$tot; //$GrandTotal+=$overAllTotal+$tot;
						
					}
					
						if(is_array($aryFeeType) && count($aryFeeType)>0){
							foreach($aryFeeType as $installT){
								
							}
						}
						
						$GrandOther+=$overAllOther;
						 $GrandFine+=$overAllFine; 
						 $GrandNewextra+=$overAllNewextra;
						


 //echo  "select sum(amount) from transaction where type='deposit' and (udate between '$fdate' and '$tdate')  and (tfor='cash' or tfor='cheque') and session='$session'";
  
  



						  $collection1["gross_total"][] =array(
						
						                     'over_all_tution'=> $overAlltution,
						                     'over_All_Transport'=> $overAllTransport,
						                     'overAllhotel'=> $overAllhotel,
						                     'over_All_hotel'=> $overAllhotel,
											      
						                     'over_All_Other'=> $overAllOther,
						                     'over_All_Fine'=> $overAllFine,
						                     'over_All_New_extra'=> $overAllNewextra,
						                     'over_All_Tot'=> $overAllTot,
						                     'over_All_Total'=> $overAllTotal,
                                                                      'GrandTotal' =>$GrandTotal
											 
											 );
						  
						  
					if(is_array($overAllextra) && count($overAllextra)>0){
					
						foreach($overAllextra as $ftype1){
							
							$collection2[extra][] = array( 'total_extra'=>$ftype1);
							
												
						}
						 
					   }
					   
					   //$gross =array_merge($collection1,$collection2);
					   
					   //echo count($collection2);
					   
					   if(count($collection2)>0){
				$gross =array_merge($collection1,$collection2);
				}
				
				else{
				$collection2[extra] = "null";
				$gross =array_merge($collection1,$collection2);
				}	
		}
	
	
	// my code end
	
	if(is_array($collection) && count($collection)>0)
	{  
		//echo "<pre>";
		echo json_encode(array('status' => 1, 'msg'=>'Message found Successfully..!', "data"=>$collection, 'gross'=>$gross));    
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'No message Found..!'));
	}