<?php  
	require "commonincludefiles.php";
	
	if($_SESSION["VedantnrCollegeId"]!="" && isset($_SESSION["VedantnrCollegeId"]))
	{
		/* start new code */
		$objcms_gtufeesreceiveMgmt = new cms_gtufeesreceiveMgmt();
		$branch_id 		= $_POST['branch_hidden'];	
		$semester_id 	= $_POST['semester_hidden'];
		$gtuhead_id 		= $_POST['gtuhead_id'];	
		$division_id 	= $_POST['division_id'];
		$gtufee_type 	= $_POST['gtufee_type'];
		
		$receive_amount	= explode(",",substr($_REQUEST['receive_amount'],0,-1));
		$student_id 	= explode(",",substr($_REQUEST['student_id'],0,-1));
		$paid 		= explode(",",substr($_REQUEST['paid'],0,-1));
		$noofsub 		= explode(",",substr($_REQUEST['noofsub'],0,-1));
		
		
		$sql_select = "SELECT * FROM cms_gtufeesreceive WHERE branch_id = '".$branch_id."' AND 
		               semester_id = '".$semester_id."' AND division = '".$division_id."' AND gtuhead_id = '".$gtuhead_id."'
					  AND FIND_IN_SET(".$_SESSION["VedantnrYearId"].", year_id) AND student_id IN(".substr($_REQUEST['student_id'],0,-1).") ";
		$res_select = dbQuery($sql_select);
		$tblyearid="";
		if(dbRows($res_select) > 0){
			$rec = dbArray($res_select);
			$tblyearid=$rec['year_id'];
			$sql_delete = "DELETE FROM cms_gtufeesreceive WHERE branch_id = '".$branch_id."' AND 
		                   semester_id = '".$semester_id."' AND division = '".$division_id."' AND gtuhead_id = '".$gtuhead_id."'
					  AND FIND_IN_SET(".$_SESSION["VedantnrYearId"].", year_id) AND student_id IN(".substr($_REQUEST['student_id'],0,-1).") ";
			$res_delete = dbQuery($sql_delete);
		}
		
		if($tblyearid != "")
			{
				$objcms_gtufeesreceiveMgmt->year_id	 = $tblyearid;
			}
		else
			{
				if($gtufee_type == 'year')
				{
					$objcms_gtufeesreceiveMgmt->year_id	 = $_SESSION['aYearId'].",".($_SESSION['aYearId']+1);
				}
				else
				{
					$objcms_gtufeesreceiveMgmt->year_id	 = $_SESSION['aYearId'];
				}
			}
		$objcms_gtufeesreceiveMgmt->branch_id	 = $branch_id;
		$objcms_gtufeesreceiveMgmt->semester_id	 = $semester_id;
		$objcms_gtufeesreceiveMgmt->division		 = $division_id;
		$objcms_gtufeesreceiveMgmt->gtuhead_id	 = $gtuhead_id;
		
		
		
		for($i = 0; $i < count($student_id); $i++){
		$objcms_gtufeesreceiveMgmt->student_id	= $student_id[$i];
		$objcms_gtufeesreceiveMgmt->noofsub	= $noofsub[$i];
		if($paid[$i]=='true' || $paid[$i]==' true'){
				
				$objcms_gtufeesreceiveMgmt->paid	= 1;	
				$objcms_gtufeesreceiveMgmt->receive_amount	= $receive_amount[$i];
			$objcms_gtufeesreceiveMgmt->add();
			
			} /* else{
				
				$objcms_gtufeesreceiveMgmt->paid	= 0;
				$objcms_gtufeesreceiveMgmt->receive_amount=0;
			}
			$objcms_gtufeesreceiveMgmt->add();*/
		}
		/* end new code */
		
		echo '{"success":"true"}';
	}
	else
	{
		echo '{"success":"false","msg":"session expiry"}';
	}
?>