<?php
	require "commonincludefiles.php";	
	$adm_yearId  = '';
	$received = 0;
		error_reporting(1);
	if($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])) 
	{
		if($_REQUEST['year_hidden_right'] < $_REQUEST['year_hidden_left'])
		{								
				$StudentDetailMgmt = new StudentDetailMgmt();
				$StudentDetailMgmt->student_id		= $_REQUEST['studentlist_left']; 
				$StudentDetailMgmt->DeleteStudentDetail();
				
				echo '{"success":"true"}';
		}
		else if($_REQUEST['year_hidden_right'] < $_REQUEST['year_hidden_left'] && $_REQUEST['branchyear_hidden_right'] < $_REQUEST['branchyear_hidden_left'])
		{								
				$StudentDetailMgmt = new StudentDetailMgmt();
				$StudentDetailMgmt->student_id		= $_REQUEST['studentlist_left']; 
				$StudentDetailMgmt->DeleteStudentDetail();
				
				echo '{"success":"true"}';
		}
		else if($_REQUEST['year_hidden_right'] < $_REQUEST['year_hidden_left'] && $_REQUEST['semester_hidden_right'] < $_REQUEST['semester_hidden_left'])
		{								
				$StudentDetailMgmt = new StudentDetailMgmt();
				$StudentDetailMgmt->student_id		= $_REQUEST['studentlist_left']; 
				$StudentDetailMgmt->DeleteStudentDetail();
				
				echo '{"success":"true"}';
		}
		
		else
		{	
					
	 		$std=explode(',',$_REQUEST['studentlist_left']);
			$objStudentDetailMgmt = new StudentDetailMgmt();
			$condition = " and student_id in (".$_REQUEST['studentlist_left'].")";
			$result = $objStudentDetailMgmt ->getAllRecords1(0,0,$condition,"","");
			$studentids="";
	
			if(dbRows($result) > 0)
			{	
				while($record = dbArray($result))
				{										 
			      										
							$objStudentDetailMgmt = new StudentDetailMgmt();							

							$condition1= " and student_id in (".$record['student_id'].")";

							$result1 = $objStudentDetailMgmt ->getAllRecords(0,0,$condition1,"","");
							
							if(dbRows($result1) > 0)
							{
								$objStudentDetailMgmt->college_id= 	$_SESSION["VedantnrCollegeId"] ;	
								$objStudentDetailMgmt->cur_branchid	=	$_REQUEST['branch_hidden_right'];	
                                $objStudentDetailMgmt->year_id	= 	$_REQUEST['year_hidden_right'];
								$objStudentDetailMgmt->cur_branchyearid	= 	$_REQUEST['branchyear_hidden_right'];
								$objStudentDetailMgmt->cur_semesterid = 	$_REQUEST['semester_hidden_right'];
								$objStudentDetailMgmt->current_active	=	'1';
								$objStudentDetailMgmt->student_id =	$record['student_id'];
								$objStudentDetailMgmt->student_rollno	=	$record['student_rollno'];
								$objStudentDetailMgmt ->add();
								
								
							}	


							/*$objFeeshead = new FeesheadMgmt();
							$feeHeads = $objFeeshead->getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "");
							$headNames = [];
							$total = dbRows($feeHeads);
					
							$totalFees = [];
							
							$total_remain = 0;
							if (dbRows($feeHeads)) {
								while ($row = dbArray($feeHeads)) {
									$fees_type = $row['fees_type'];
									$head_id = $row['head_id'];
									$head_name = $row['head_name'];
									$headNames[] = [
										'fees_type' => $fees_type,
										'head_id' => $head_id,
										'name' => $head_name,
										'fee_field' => $head_name . "_fee",
										'mafi_field' => $head_name . "_mafifee"
									];
					
									$totalFees = 0;
								}
								$objStudentdtailMgmt = new StudentDetailMgmt();
								$condition ="";
								$condition .= " AND sd.student_id= " . $record['student_id']. " 
								AND sd.year_id = " . $_REQUEST['year_hidden_right'] . " 
								AND sd.cur_branchid = " . $_REQUEST['branch_hidden_right'] . "";
								if($_REQUEST['branchyear_hidden_right']!=""){
									$condition .= " AND sd.cur_branchyearid = " . $_REQUEST['branchyear_hidden_right'] . "";
								}
								if($_REQUEST['semester_hidden_right']!=""){
									$condition .= " AND sd.cur_semesterid = " . $_REQUEST['semester_hidden_right'] . "";
								}


								$fees = $objStudentdtailMgmt->getfeesrecord($headNames, $condition);
								if (dbRows($fees) > 0) {
									while ($row = dbArray($fees)) {
					
										foreach ($headNames as $head) {
											$fee_field = $head['fee_field'];
											$mafi_field = $head['mafi_field'];
					
											$fees_amount = intval($row[$fee_field]) - intval($row[$mafi_field]);
					
					
											$totalFees += $fees_amount;
										}
									}
							 	$receivable = $totalFees;
								
				
								 $condition ="";
								 $condition .= " AND frm.student_id= " . $record['student_id']. " 
								 AND frm.year_id = " . $_REQUEST['year_hidden_right'] . " 
								 AND frm.branch_id = " . $_REQUEST['branch_hidden_right'] . "";
								 if($_REQUEST['branchyear_hidden_right']!=""){
									 $condition .= " AND frm.branchyear_id = " . $_REQUEST['branchyear_hidden_right'] . "";
								 }
								 if($_REQUEST['semester_hidden_right']!=""){
									 $condition .= " AND frm.semester_id = " . $_REQUEST['semester_hidden_right'] . "";
								 }
							$objRecFees = new StudentFeesRecMgmt();
							$outFeesDetail = $objRecFees->getAllRecordsRecivedFeesView($limit, $start, $condition, $order_field = "", $order_type = "");
							$totalRecived = dbRows($outFeesDetail);
					
							$received = 0;
							if (dbRows($outFeesDetail) > 0) {
					
								while ($row = dbArray($outFeesDetail)) {
									$branch_name = $row["branch_name"];
									$semester_name = $row["semester_name"];
									$branchyear_name = $row["branchyear_name"];
									$year_name = $row["year_name"];
									$receivedamt += $row["receive_amt"];
					
								}
								$total_remain = $gtotal - $receivedamt;
							}
							
							
							
							
						
							$update_last_year = "UPDATE cms_studentmaster SET last_year_debit = '$total_remain' where 
							student_id = '".$record['student_id']."' AND
							branch_id='". $record['branch_id']."'
							AND year_id='". $record['year_id']."'";


							
							if($record['branchyear_id']!="") {
							$update_last_year .= "AND branchyear_id='". $record['branchyear_id']."'";
							}
							if($record['semester_id']!="") {
								$update_last_year .= "AND semester_id='". $record['semester_id']."'";
								}

								echo $update_last_year;
							$out2=dbQuery($update_last_year);
							$row2 = dbArray($out2);		
						}
					
					}else
					{
						$update_last_year = "UPDATE sms_studentmaster SET last_year_debit = '0' where 
						student_id = '".$record['student_id']."' AND
						branch_id='". $record['branch_id']."'
						AND year_id='". $record['year_id']."'";


						
						if($record['branchyear_id']!="") {
						$update_last_year .= "AND branchyear_id='". $record['branchyear_id']."'";
						}
						if($record['semester_id']!="") {
							$update_last_year .= "AND semester_id='". $record['semester_id']."'";
							}

						
						$out2=dbQuery($update_last_year);
						$row2 = dbArray($out2);		
					}	*/				
				}
			}						
		}
		echo '{"success":"true"}';
 	}	 	 	 	 	 	 
	else
	{ 
	    echo '{"success":"false"}';
	}	
?>