<?php
require "../commonincludefiles.php";
error_reporting(1);
$branch_id = (Escape($_POST['branch_id'] != "")) ? Escape($_POST['branch_id']) : 0;
$year_id = (Escape($_POST['year_id'] != "")) ? Escape($_POST['year_id']) : 0;
$branchyear_id = (Escape($_POST['branchyear_id'] != "")) ? Escape($_POST['branchyear_id']) : 0;
$semester_id = (Escape($_POST['semester_id'] != "")) ? Escape($_POST['semester_id']) : "";
$transport_facility = (Escape($_POST['transport_facility'] != "")) ? Escape($_POST['transport_facility']) : "";

$objStudentMgmt = new StudentMgmt();
$i = 0;


$objFeeshead = new FeesheadMgmt();
$feeHeads = $objFeeshead->getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "");
$headNames = [];

if (dbRows($feeHeads)) {
	while ($row = dbArray($feeHeads)) {
		$head = $row['head_name'];
		$headNames[] = [
			'name' => $head,
			'fee_field' => $head . "_fee",
			'mafi_field' => $head . "_mafifee"
		];
	}
}


$condition = "";
$condition .= " and sd.cur_branchid = $branch_id and sd.year_id =$year_id ";
if ($branchyear_id != "") {
	$condition .= " 	and sd.cur_branchyearid =$branchyear_id  ";
}
if ($semester_id != "") {
	$condition .= " and sd.cur_semesterid=$semester_id ";
}
$condition .= " order by student_surname ,student_firstname ASC";

$rs = $objStudentMgmt->getAllStudentfees($headNames, $condition);
$total = dbRows($rs);
$FeesData = array("count" => $total, "data" => array());
if (dbRows($rs) > 0) {
	while ($row = dbArray($rs)) {
		$temparray["student_id"] = $row["student_id"];
		$temparray["branch_id"] = $row["branch_id"];
		$temparray["year_id"] = $row["year_id"];
		$temparray["branchyear_id"] = $row["branchyear_id"];
		$temparray["semester_id"] = $row["semester_id"];
		$temparray["student_gturegno"] = $row["student_gturegno"];
		$temparray["transport_facility"] = ($row["transport_facility"] == "1") ?  'Yes' : 'No';
		$temparray["mafifees_refname"] = $row["mafifees_refname"];
		$temparray["mafifees_date"] = ($row["mafifees_date"] == "0000-00-00")? "" :$row["mafifees_date"];
      
		foreach ($headNames as $head) {
			$temparray[$head['mafi_field']] = $row[$head['mafi_field']];
		}
		$temparray["student_name"] = $row["student_surname"] . " " . $row["student_firstname"] . " " . $row["student_lastname"];
      
		$FeesData["data"][$i] = $temparray;
		$i++;
	}
}
echo json_encode($FeesData);
?>