<?php
	require "../commonincludefiles.php";
	
   	$objStudent = new StudentMgmt();
	
	$student_id 		  = Escape($_REQUEST['student_id']);
	
	
	$student_gturegno    = Escape($_REQUEST['student_gturegno']);
	$adm_year 			= Escape($_REQUEST['adm_year']);
	$student_techcerti   = Escape($_REQUEST['student_techcerti']);
	
	
	$SQL = "SELECT * FROM cms_studentdetail WHERE current_active = 1 AND student_id = ".$student_id;
	
	$RES = dbQuery($SQL);
	if(dbRows($RES) > 0){
		
		$DispStr = "<table width='97%' class='tblstyle'>";
		$DispStr .= "<tr><th colspan='4'>:: Last Semester Fees Information ::</th></tr>";
		$DispStr .= "<tr><td><b>Semester</b></td><td><b>Total Fee</b></td><td><b>Received</b></td><td><b>Receivable</b></td></tr>";
		
		//$REC = dbArray($RES);
	//	$cur_semesterid = $REC['cur_semesterid'];
		
		$objStudentFeesRecMgmt = new StudentFeesRecMgmt();
		
		while($row = dbArray($RES))
			{
			$semester_id = $row['cur_semesterid'];
			
			$SemesterName = "";
			
			$sql_semname = "SELECT semester_name FROM cms_semestermaster WHERE semester_id = ".$semester_id;
			$res_semname = dbQuery($sql_semname);
			if(dbRows($res_semname) > 0){
				$rec_semname = dbArray($res_semname);
				$SemesterName = $rec_semname['semester_name'];
			}
		
			/* START FEE SETUP DATA */
			
				//fees year start
				$SQL_year = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_year."%' LIMIT 0 , 1";
				$SQL_c2d_year = dbQuery($SQL_year);
				$year_arr = dbArray($SQL_c2d_year);
				$c2d_year = $year_arr[0] ;
			
			
			
			$fees_year = "";
			if($student_gturegno !="" && $student_gturegno !=0 && $student_techcerti!="" && $semester_id == 3 && $c2d_year == $_SESSION["VedantnrYearId"])
			{
				$student_feesyear = substr($student_gturegno,0,-10);
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$student_feesyear."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] ;
				$semester_id = $semester_id ;
				
			}elseif($student_gturegno !="" && $student_gturegno !=0 && $student_techcerti =="")
			{
				$student_feesyear = substr($student_gturegno,0,-10);
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$student_feesyear."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0];
				
			}
			elseif($student_techcerti !="" && $semester_id == 3 && $c2d_year == $_SESSION["VedantnrYearId"] )
			{
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_year."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0];
				$semester_id = $semester_id ;
				
			}elseif($student_techcerti !="" && $c2d_year != $_SESSION["VedantnrYearId"] )
			{
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_year."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] ;
			}
			else
			{
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_year."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] ;
			}
			// Fees year end
			
			
			
		$SQL = "select
				fh.head_name,
				fh.head_id,
				fh.fees_type,
				fs.setup_id,
				fs.semester_id,
				fs.fees_amount
			from
				cms_feeshead fh left join cms_feessetup fs on fh.head_id = fs.head_id
				AND	fs.year_id = '".$fees_year."' AND fs.semester_id = '".$semester_id."'
			WHERE
				(fs.fees_amount != '' OR fh.head_active = 0)
				AND fh.college_id = '".$_SESSION['VedantnrCollegeId']."' ";

			if($fees_type != ""){
				$SQL .= " AND (fh.fees_type = '".$fees_type."' OR fh.fees_type = 'Other') ";
			}

			$SQL .= " order by fh.head_id,fh.fees_type ASC ";		
			
			$result = dbQuery($SQL);
			$total = dbRows($result);
			
			$flag_admby = 0;
			$flag_amt = 0;
			$studentByAcpdc = $objStudentFeesRecMgmt->CheckAdmission($student_id);
			$flag_tfw = $studentByAcpdc['tfw_scheme'];
			if($studentByAcpdc['student_admby'] == "ACPDC"){
				$flag_admby = 1;
			}
			
			if(dbRows($result) > 0){
				$temparrayhead_id 		= array();
				$temparrayfees_type 	= array();
				$temparrayhead_name 	= array();
				$temparrayfees_amount 	= array();
				$i = 0;
				while($row = dbArray($result)){
					if($flag_admby == 1 && $row["head_name"] == "ACPDC Adm Fee"){
						$flag_amt = $row["fees_amount"];
					}		
					if($flag_admby == 1 && $row["head_name"] == "Tution Fee"){	
						if($flag_tfw == 0){
							$temparrayhead_id[$i]     = $row["head_id"];
							$temparrayfees_type[$i]   = $row["fees_type"];
							$temparrayhead_name[$i]   = $row["head_name"];
							$temparrayfees_amount[$i] = $row["fees_amount"] - $flag_amt;
							$i++;
						}
					} else if($row["head_name"] == "College Retest Fee"){
						$temparrayhead_id[$i]     = $row["head_id"];
						$temparrayfees_type[$i]   = $row["fees_type"];
						$temparrayhead_name[$i]   = $row["head_name"];
						$temparrayfees_amount[$i] = $retestfee;
						$i++;
					} else if($row["head_name"] != "ACPDC Adm Fee" && $row["head_name"] != "College Retest Fee"){
						
						if($flag_tfw == 1 && $row["head_name"] == "Tution Fee"){
							continue;
						}
						
						$temparrayhead_id[$i]     = $row["head_id"];
						$temparrayfees_type[$i]   = $row["fees_type"];
						$temparrayhead_name[$i]   = $row["head_name"];
						$temparrayfees_amount[$i] = $row["fees_amount"];
						$i++;
					} 
				}
			}
			
			$receivable = 0;
			for($j = 0; $j < count($temparrayfees_amount); $j++){
				$receivable = $temparrayfees_amount[$j];
			}
			/* END FEE SETUP DATA */
			
			/* START FEE RECEIVE DATA */
			$where = "";
			if($semester_id != ""){
				$where .= " and frm.semester_id = '".$semester_id."' ";
			}
			
			if($student_id != ""){
				$where .= " and frm.student_id = '".$student_id."' ";
			}
			$resultRecived = $objStudentFeesRecMgmt->getAllRecordsRecivedFees($limit=0,$start=0,$where,$order_field="frd.head_id",$order_type="ASC"); 
			$totalRecived = dbRows($resultRecived);
		
			if(dbRows($resultRecived) > 0){
				$Recivedheadid = array();
				$Recivedamt = array();
				$i=0;
				
				while($row = dbArray($resultRecived)){
					$Recivedheadid[$i] 	= $row["head_id"];
					$Recivedamt[$i]    	= $row["recdetail_amt"];
					$i++;
				}
			}
			$received = 0;
			for($j = 0; $j < count($Recivedamt); $j++){
				$received += $Recivedamt[$j];	
			}
			/* END FEE RECEIVE DATA */
		
			/* START COUNT TOTAL FEE */
			$receivable1 = $receivable - $received;
			if($receivable1 > 0){
				$DispStr .= "<tr><td>".$SemesterName."</td><td>".$receivable."</td><td>".$received."</td><td>".$receivable1."</td></tr>";
			}			
			/* END COUNT TOTAL FEE */
			
			unset($temparrayfees_amount);
			unset($Recivedamt);
		}
		$DispStr .= "</table>";
		echo '{"success":"true","DispStr":"'.$DispStr.'"}';
	}	
?>