<?php
require "commonincludefiles.php";
error_reporting(1);
if ($_SESSION['VedantnrCollegeId'] != "" && isset($_SESSION['VedantnrCollegeId'])) {
	$std = explode(',', $_REQUEST['studentlist_left']);

	$objStudentDetailMgmt = new StudentDetailMgmt();
	$condition = "";
	$condition .= " and student_id in (" . $_REQUEST['studentlist_left'] . ") and year_id = " . $_REQUEST['year_hidden_left'] . " and 
					cur_branchid = " . $_REQUEST['branch_hidden_left'] . "  ";
	if ($_REQUEST['branchyear_hidden_left']!="") {
		$condition .= " and cur_branchyearid = " . $_REQUEST['branchyear_hidden_left'] . "  ";
	}
	if ($_REQUEST['semester_hidden_left'] !=" " && $_REQUEST['semester_hidden_left']!= 0 && $_REQUEST['semester_hidden_left'] !="undefined") {
		$condition .= " and cur_semesterid = " . $_REQUEST['semester_hidden_left'] . "  ";
	}

	$result = $objStudentDetailMgmt->getAllRecords1(0, 0, $condition, "", "");


	if (dbRows($result) > 0) {
		while ($record = dbArray($result)) {

			$condition1 = "";
			$condition1 .= "  and student_id in (" . $record['student_id'] . ") and year_id = " . $_REQUEST['year_hidden_right'] . "
                             and cur_branchid = " . $_REQUEST['branch_hidden_right'] . "  ";

			if ($_REQUEST['branchyear_hidden_right'] !=" ") {
				$condition1 .= " and cur_branchyearid = " . $_REQUEST['branchyear_hidden_right'] . "  ";
			}
			if ($_REQUEST['semester_hidden_right'] !=" " && $_REQUEST['semester_hidden_right'] !=0 && $_REQUEST['semester_hidden_right'] !="undefined") {
				$condition1 .= " and cur_semesterid = " . $_REQUEST['semester_hidden_right'] . "  ";
			}

			$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"
					];


				}

				$condition2 = "";
				$condition2 .= " AND sd.student_id in (" . $record['student_id'] . ") 
								AND sd.year_id = " . $record['year_id'] . " 
								AND sd.cur_branchid = " . $record['cur_branchid'] . "";
				if ($record['cur_branchyearid'] != "") {
					$condition2 .= " AND sd.cur_branchyearid = " . $record['cur_branchyearid'] . "";
				}
				if ($record['cur_semesterid'] != "") {
					$condition2 .= " AND sd.cur_semesterid = " . $record['cur_semesterid'] . "";
				}

				$fees = $objStudentDetailMgmt->getfeesrecord($headNames, $condition2);
				if (dbRows($fees) > 0) {
					while ($row = dbArray($fees)) {
						$totalFees = 0;
						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;
					$condition3 = "";
					$condition3 .= " AND frm.student_id in (" . $record['student_id'] . ")
								 AND frm.year_id = " . $record['year_id'] . " 
								 AND frm.branch_id = " . $record['cur_branchid'] . "";
					if ($_REQUEST['cur_branchyearid'] != "") {
						$condition3 .= " AND frm.branchyear_id = " . $record['cur_branchyearid'] . "";
					}
					if ($_REQUEST['cur_semesterid'] != "") {
						$condition3 .= " AND frm.semester_id = " . $record['cur_semesterid'] . "";
					}
					$objRecFees = new StudentFeesRecMgmt();
					$outFeesDetail = $objRecFees->getAllRecordsRecivedFeesView($limit, $start, $condition3, $order_field = "", $order_type = "");
					$totalRecived = dbRows($outFeesDetail);

					if (dbRows($outFeesDetail) > 0) {
						$receivedamt = 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 = $receivable - $receivedamt;

				}

			} 
		}
	}

	echo '{"success":"true"}';
} else {
	echo '{"success":"false"}';
}
?>