<?php
	require "../commonincludefiles.php";
	set_time_limit(0);
	
	if($_SESSION["VedantnrCollegeId"]!="" && isset($_SESSION["VedantnrCollegeId"])){	
		
		$sql_year = " SELECT year_id,year_name FROM cms_yearmaster WHERE year_active = 1 ";
		$res_year = dbQuery($sql_year);
		if(dbRows($res_year) > 0){
			$rec_year = dbArray($res_year);
			$current_year = $rec_year['year_id'];
			$current_year_name = $rec_year['year_name'];
			$new_year = $current_year + 1;
			
			if(substr($current_year_name,5,-1) == 'Even')
			{
				$new_year_name = intval($current_year_name) + 1;
				$new_year_name = $new_year_name."(Odd)";
				$create_year = "Insert into cms_yearmaster SET year_name = '".$new_year_name."'";	
				dbQuery($create_year);
				$new_year=dbInsertedId();
			}else{
				$new_year_name = intval($current_year_name);
				$new_year_name = $new_year_name."(Even)";
				$create_year = "Insert into cms_yearmaster SET year_name = '".$new_year_name."'";	
				dbQuery($create_year);
				$new_year=dbInsertedId();
			}
			
			//cancellation strat
			//$sql_studcancel = "Update  `cms_studentmaster`  set `student_active` = 1, `leave_type` =''  where `leave_type` = 'c'";
			//$res_studcancel = dbQuery($sql_studcancel);
			//cancellation end
			
			$update_year = "UPDATE cms_yearmaster SET year_active = 0 WHERE year_id = $current_year";	
			dbQuery($update_year);
			$update_year = "UPDATE cms_yearmaster SET year_active = 1 WHERE year_id = $new_year";
			dbQuery($update_year);
			
			/* insert Student Data */
			
			
			
		/*	$sql_studdetail = " SELECT * FROM cms_studentdetail WHERE year_id = ".$current_year." AND current_active = 1 ";
			
			$res_studdetail = dbQuery($sql_studdetail);
			
			if(dbRows($res_studdetail) > 0){
				while($rec_studdetail = dbArray($res_studdetail))
				{
					$studentdetail_id 	= $rec_studdetail['studentdetail_id'];
					$student_id 		= $rec_studdetail['student_id'];
					$cur_branchid 		= $rec_studdetail['cur_branchid']; 	
					$cur_semesterid 	= $rec_studdetail['cur_semesterid']; 	
					$cur_divison 		= $rec_studdetail['cur_divison']; 	
					$cur_shift 			= $rec_studdetail['cur_shift']; 	
					$college_id 		= $rec_studdetail['college_id']; 	
					$current_active 	= 1; 	
					$year_id 			= $new_year;
					
				$sql_insert = "	
						INSERT INTO cms_studentdetail(student_id,cur_branchid,cur_semesterid,cur_divison,cur_shift,
						college_id,current_active,year_id)
						VALUES('".$student_id."','".$cur_branchid."','".$cur_semesterid."','".$cur_divison."','".$cur_shift."','".$college_id."','".$current_active."','".$year_id."')";
					dbQuery($sql_insert);
					
					$sql_update = "
						UPDATE cms_studentdetail SET current_active = 0
						WHERE studentdetail_id = ".$studentdetail_id." AND year_id = ".$current_year." AND current_active = 1 ";
					dbQuery($sql_update);	
				}	
			}
	*/
			
			
			
			$sq1l = "SELECT * FROM cms_feessetup WHERE year_id = ".$current_year;
			$res1 = dbQuery($sq1l);
			if(dbRows($res1)){
				$objFees = new FeessetupMgmt();
				while($rec1 = dbArray($res1)){
					
					$semester_id	= $rec1['semester_id'];
					$head_id 		= $rec1['head_id'];
					$college_id 	= $rec1['college_id'];
					$fees_amount 	= $rec1['fees_amount'];
					$year_id		= $new_year;
					
					$sql_insert = "	
						INSERT INTO cms_feessetup(semester_id,head_id,college_id,fees_amount,year_id)
						VALUES('".$semester_id."','".$head_id."','".$college_id."','".$fees_amount."','".$year_id."')";
					dbQuery($sql_insert);
				}	
			}
			echo "{success:'true', msg:'Data transferd successfully.'}";	
		}
		else{
			echo "{success:'false', msg:'No data available for transfer.'}";
		}
	
	}
?>