<?php

	require "commonincludefiles.php";
	
	$adm_yearId  = '';
	$received = 0;
	/*function getAdmYear($StudentId)
	{
		if($StudentId != 0 || $StudentId != '' || $StudentId != NULL)
		{
			$Query_String_year = "SELECT YEAR( `student_admdate`) adm_year FROM `cms_studentmaster` WHERE `student_id` = '".$StudentId."' ";
			$Res_year = dbQuery($Query_String_year);
			$Year_arr = dbArray($Res_year);
			$adm_year = $Year_arr[0];	
			
			$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] - 2;
			
			if($fees_year != '' || $fees_year != 0)
			{
				return $fees_year;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}*/
	
	
	
	function checkdebitfees($student_id,$semester_id,$branch_id,$adm_yearId)
	{
		//for new entry between semester
		//----------------- start --------------------
		
		
		$sql= "SELECT student_gturegno,student_techcerti  FROM `cms_studentmaster` 
				WHERE 
					`student_id` = '".$student_id."' AND 
					`branch_id` = '".$branch_id."' AND 
					`semester_id` ='".$semester_id."'";
		$stud_res = dbQuery($sql);
		$fees_year_arr = dbArray($stud_res);
		$student_gturegno = $fees_year_arr[0];
		$student_techcerti = $fees_year_arr[1];
		
		
		
		
		$fees_year = "";
		$sem_id= "";
			if($student_gturegno !="" && $student_gturegno !=0 && $student_techcerti!="" && $semester_id == 3 && $adm_yearId == $_SESSION["VedantnrYearId"])
			{
			
			$test = "Freess";
				$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] - 2;
				$sem_id = $semester_id - 2;
				
			}
			
			elseif($student_gturegno !="" && $student_gturegno !=0 && $student_techcerti =="")
			{
				
				 $test = "Freess1";
				$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 && $adm_yearId == $_SESSION["VedantnrYearId"] )
			{
				
				 $test = "Freess2";
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_yearId."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] - 2;
				$sem_id = $semester_id - 2;
				
				
			}elseif($student_techcerti !="" && $adm_yearId != $_SESSION["VedantnrYearId"] )
			{
			
				 $test = "Freess3";
				$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_yearId."%' LIMIT 0 , 1";
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] ;
			}
			/*else
			{
				
				echo $test = "Freess4";
				 $SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%".$adm_yearId."%' LIMIT 0 , 1";
				 echo $SQL_fees;
				 
				$SQL_fees_res = dbQuery($SQL_fees);
				$fees_year_arr = dbArray($SQL_fees_res);
				$fees_year = $fees_year_arr[0] ;
			}
			echo $adm_yearId."yearid";*/
		if($fees_year !=""){
			/*echo $fees_year."feesid";
			exit;*/
				$adm_yearId = $fees_year;
		}
		if($sem_id == ""){
			$sem_id = $semester_id;
		}
		
		
		
		
		
		
		// ---------------- END -------------
		
		
		//echo $student_id."<br/>".$semester_id."<br/>".$branch_id."<br/>".$adm_yearId;
		
		
	    $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 = '".$adm_yearId."' AND fs.semester_id = '".$sem_id."'
			WHERE
				(fs.fees_amount != '' AND 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 ";		
			
			//echo $SQL;
			
			$result = dbQuery($SQL);
			$total = dbRows($result);
			
			$flag_admby = 0;
			$flag_amt = 0;
			$objStudentFeesRecMgmt = new StudentFeesRecMgmt();
			$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++;
				}
			}
			
			for($j = 0; $j < count($Recivedamt); $j++)
			{
					$received += $Recivedamt[$j];	
			}
			
			/* END FEE RECEIVE DATA */
		
			/* START COUNT TOTAL FEE */
			//echo  'receiveable--'.$receivable;
			
			//echo  'received--'.$received;
			
			/*echo $receivable."ss";
			echo $received."tt<br/>";*/
			
			$receivable1 = 0 ;
			$receivable1 = $receivable - $received;
			
			//echo "recievable".$receivable1."kk"."<br/>";
			
			//echo  'recev1--'.$receivable1;
			if($receivable1 > 0  )
			{
				return $receivable1;
			}
			else
			{
				return 0;
			}
	}
	
	if($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])) 
	{
		if($_REQUEST['semester_hidden_right'] < $_REQUEST['semester_hidden_left'])
		{
				//$std=explode(',',$_REQUEST['studentlist_left']);
				
				$StudentDetailMgmt = new StudentDetailMgmt();
				$StudentDetailMgmt->student_id		= $_REQUEST['studentlist_left']; 
				$StudentDetailMgmt->DeleteStudentDetail();
				
				echo '{"success":"true"}';
		}
		else
		{
		
			if(($_REQUEST['semester_hidden_right']=='3' or $_REQUEST['semester_hidden_right']=='5') && $_REQUEST['year_hidden_right'] == $_SESSION['aYearId'])
			{
				$objYearMgmt = new YearMgmt();
				$objYearMgmt->year_name            = 	"'".(date("Y")) ."(Odd)','".(date("Y")) ."(Even)'";
				$out=$objYearMgmt ->getAllRecords();
				 
			 	if(dbRows($out)==0)
		 		{
					$objYearMgmt = new YearMgmt();
					$objYearMgmt ->edit(); 
					$objYearMgmt = new YearMgmt();
					$objYearMgmt->year_name            = 	date("Y") ."(Odd)";
					$objYearMgmt ->add(); 
					$objYearMgmt->year_name            = 	date("Y") ."(Even)";
					$objYearMgmt ->add(); 
				}
			}
			
			 
	 		$std=explode(',',$_REQUEST['studentlist_left']);
	
			$objStudentDetailMgmt = new StudentDetailMgmt();
			$condition= " and student_id in (".$_REQUEST['studentlist_left'].")";
			$condition .= " and year_id = ".$_SESSION["VedantnrYearId"]." ";
			$result = $objStudentDetailMgmt ->getAllRecords1(0,0,$condition,"","");
			$studentids="";
	
			if(dbRows($result) > 0)
			{	
				while($record = dbArray($result))
				{
					
					
			      $adm_yearId = getAdmYear($record['student_id']);
					
				  if($adm_yearId != 0)
				  {
					 $debitamt = checkdebitfees($record['student_id'],$record['cur_semesterid'],$record['cur_branchid'],$adm_yearId);
						
						if($debitamt == 0)
						{
							
							$objStudentDetailMgmt = new StudentDetailMgmt();
							// to check record is exist or not if exist then skip
							$condition1= " and student_id in (".$record['student_id'].")";
							$condition1 .= " and year_id = ".($_SESSION["VedantnrYearId"]+1)." ";
							$result1 = $objStudentDetailMgmt ->getAllRecords1(0,0,$condition1,"","");
						
							if(dbRows($result1)== 0)
							{
								//$objStudentDetailMgmt->year_id				= 	$_SESSION["VedantnrYearId"]+1;
								if($_REQUEST['year_hidden_right'] == $_SESSION['aYearId'])
								{
									$year_id1= $_SESSION['aYearId'] +1;
								}
								else
								{
									$year_id1=$_REQUEST['year_hidden_right'];
								}
								$objStudentDetailMgmt->year_id				= 	$year_id1;
								$objStudentDetailMgmt->college_id            = 	$_SESSION["VedantnrCollegeId"] ;
								$objStudentDetailMgmt->student_id			=	$record['student_id'];
								$objStudentDetailMgmt->cur_branchid	=	$_REQUEST['branch_hidden_right'];										
								$objStudentDetailMgmt->batch_id		=	$record['batch_id'];
								$objStudentDetailMgmt->cur_semesterid	= $_REQUEST['semester_hidden_right'];
								$objStudentDetailMgmt->cur_divison	=	$_REQUEST['division_right'];
								$objStudentDetailMgmt->cur_shift	=	$_REQUEST['shift_right'];
								$objStudentDetailMgmt->current_active	=	'1';		
								$objStudentDetailMgmt ->add();
								
								$sql_update = "
								UPDATE cms_studentdetail SET current_active = 0
								WHERE student_id = ".$record['student_id']." AND year_id = ".$_SESSION["VedantnrYearId"]." AND current_active = 1 ";
							dbQuery($sql_update);	 
							}
							
						}
						else
						{
							continue;
						}
				  	}
				  }
			}
			echo '{"success":"true"}';
		}
		
 	 }	 	 	 	 	 	 
	 else
	 { 
	    echo '{"success":"false"}';
  	 }
?>
