<?php
	require "../commonincludefiles.php";
 	$arr = array();
 	$total = 0;
	$i=0;
	$sub = array();
	if($_REQUEST['branch_name']!="" && $_REQUEST['semester_name']!="" && $_REQUEST['gtuhead_id']!="")
		{
			$qry="select subject_id from cms_examsetup 
					where 
					branch_id = '".Escape($_REQUEST['branch_name'])."' AND 
					semester_id = '".Escape($_REQUEST['semester_name'])."' AND
					year_id = '".$_REQUEST["year_id"]."' AND 
					branchyear_id = '".$_REQUEST["branchyear_id"]."' AND 
					total_marks = 0 AND 
					passing_marks = 0 AND 
					subject_id != 0";
			
					
			$rs = dbQuery($qry);		
			while($rec_tamount = dbArray($rs)){
				$sub []  =  $rec_tamount['subject_id'];
			}
			$sub1= implode(',',$sub);
			
					
		 $sql = "
			SELECT
				CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
				sm.student_id,
				sm.student_gturegno,
				gfr.noofsub,
				gfr.receive_amount,
				gfr.paid,
				sd.cur_divison,
				count(res.subject_id) as totalfail_sub
			FROM
					cms_studentmaster sm LEFT JOIN cms_gtufeesreceive gfr ON sm.student_id = gfr.student_id 
					
					AND gfr.branch_id = ".Escape($_REQUEST['branch_name'])."
					AND gfr.semester_id = ".Escape($_REQUEST['semester_name'])." ";
				
					if($_REQUEST['division']!=""){
					$sql.=" AND gfr.division = '".Escape($_REQUEST['division'])."'  ";
					}
				
				$sql.="  
					AND gfr.gtuhead_id = ".Escape($_REQUEST['gtuhead_id'])." 
					AND FIND_IN_SET(".$_SESSION["VedantnrYearId"].", gfr.year_id), 
				   	cms_studentdetail sd,
					cms_resultmaster res
					
			WHERE 
				sm.student_id = sd.student_id AND
				sm.student_id = res.student_id AND
				sm.student_active = 1 AND
				
				sd.cur_branchid = ".Escape($_REQUEST['branch_name'])." AND 
				sd.year_id = ".Escape($_REQUEST['year_name'])."  ";
				if($_REQUEST['branchyear_name']!=""){
					$sql.=" AND sd.cur_branchyearid = ".Escape($_REQUEST['branchyear_name'])."   ";
				}
				if($_REQUEST['semester_name']!=""){
					$sql.=" AND sd.cur_semesterid = ".Escape($_REQUEST['semester_name'])."   ";
				}
				$sql.="AND 	
				res.test_type = 'f' AND
				res.result = 'fail' AND
				res.subject_id NOT IN ('".$sub1."') 
				group by res.student_id
				ORDER BY cur_divison ASC,student_gturegno ASC, student_name ASC ";	
		
		}
	//echo $sql;exit;
	$rs = dbQuery($sql);		
	$total = dbRows($rs);
	
	$sql_tamount = "SELECT * FROM  cms_gtufeeshead WHERE gtuhead_id = ".Escape($_REQUEST['gtuhead_id'])." ";
	$res_tamount = dbQuery($sql_tamount);			
	$rec_tamount = dbArray($res_tamount);
	$amt_receivable = $rec_tamount['gtuhead_amount'];
	$max_amount = $rec_tamount['max_amount'];
	$gtufee_type = $rec_tamount['gtufee_type'];
	
	$GTUHeadData = array("count" => $total, "data" => array());
	$j=0;
	if(dbRows($rs) > 0){
		while($row = dbArray($rs)){
			$temparray["student_name"]  	= $row["student_name"];
			$temparray["student_id"] 		= $row["student_id"];
			$temparray["student_gturegno"]	= $row["student_gturegno"];
			$temparray["noofsub"] 			= ($row["noofsub"]==""?$row['totalfail_sub']:$row["noofsub"]);
			$receive_amount=$row["receive_amount"];
			
			if($row["receive_amount"]=="" || $row["receive_amount"]==0){
				$receive_amount=($temparray["noofsub"]*$amt_receivable);
				if($receive_amount > $max_amount){
					$receive_amount=$max_amount;
				}
				
			}
			
			$temparray["receive_amount"]	= $receive_amount;
			$temparray["gtuhead_amount"]	= $amt_receivable;
			
			if($row["paid"] == 1){
				 $temparray["paid"] 		= true;
				 //$temparray["obtain_mark"]  	= $row["obtain_mark"];
			} else {
				 $temparray["paid"]  		= false;
				//$temparray["obtain_mark"]  	= '';	
			}
			//$temparray["obtain_mark"]  	= $j++;	
	
			$GTUHeadData["data"][$i] 			= $temparray;   
			$i++;
		}
	}
	echo json_encode($GTUHeadData);	
?>