<?php
require "../commonincludefiles.php";
error_reporting(1);

$student_id = $_REQUEST['student_id'];
$year_id = $_REQUEST['year_id'];

$SQL = "SELECT * FROM cms_studentdetail WHERE student_id = " . $student_id . " and year_id < " . $year_id . "";
$RES = dbQuery($SQL);
if (dbRows($RES) > 0) {

	$count = dbRows($RES);
	$DispStr1 = "";
	$hasDueAmount = false;

	$objStudentFeesRecMgmt = new StudentFeesRecMgmt();

	while ($row = dbArray($RES)) {

		$year_id = $row['year_id'];
		$student_id = $row['student_id'];

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

		$totalFees = [];

		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 = " AND sd.student_id= " . $student_id . " AND sd.year_id = " . $year_id . " ";
			$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;
			}

		}

		/* END FEE SETUP DATA */

		/* START FEE RECEIVE DATA */
		$condition = "";

		if ($student_id != "") {
			$condition .= "   and frm.student_id = '" . $student_id . "' and frm.year_id = '" . $year_id . "' ";
		}
		$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"];

			}
		}
		$received = $receivedamt;
		/* END FEE RECEIVE DATA */

		/* START COUNT TOTAL FEE */
		$receivable1 = $receivable - $received;
		$branch_name = explode('-', $branch_name)[0];
		$DispStr1 .= "<tr><td  style=color:red>" . $branch_name . "</td><td  style=color:red>" . $year_name . "</td><td  style=color:red>" . $branchyear_name . "</td><td  style=color:red>" . $semester_name . "</td><td style=color:red>" . $receivable . "</td><td  style=color:red>" . $received . "</td><td  style=color:red>" . $receivable1 . "</td></tr>";

		/* END COUNT TOTAL FEE */

		unset($temparrayfees_amount);
		unset($Recivedamt);
	}

	if (	$hasDueAmount && $DispStr1 != "") {
		$DispStr = "<table width='90%' class='tblstyle'>";
		$DispStr .= "<tr><th colspan='4'>:: Last Years Fees Information ::</th></tr>";
		$DispStr .= "<tr><td><b>Course</b></td><td><b>Batch Year</b></td><td><b>Course Year</b></td><td><b>Semester</b></td><td><b>Receivable Amt.</b></td><td><b>Received Amt.</b></td><td><b>Due Amt.</b></td></tr>";
		$DispStr .= $DispStr1;
		$DispStr .= "</table>";
	}


	if ($hasDueAmount) {
		echo '{"success":"true","DispStr":"' . $DispStr . '"}';
	} else {
		echo '{"success":"false"}';
	}
	
}


?>