<?php
require "../commonincludefiles.php";

$out = "";
$xaction = "";
$data = "";

$where = '';
$qs = '';
$order_field = "";
$order_type  = "";

if ($_SESSION["VedantnrCollegeId"] != "" && isset($_SESSION["VedantnrCollegeId"])) {
	$objStudentdtailMgmt = new StudentDetailMgmt();

	$college_id = $_SESSION["VedantnrCollegeId"];
	$branch_id = $_REQUEST['branch_id'];
	$year_id = $_REQUEST['year_id'];
	$branchyear_id = $_REQUEST['branchyear_id'];
	$semester_id = $_REQUEST['semester_id'];
	$student_id = $_REQUEST['student_id'];
	$receipt_no = $_REQUEST['recipt_no'];
	$student_gturegno = $_REQUEST['student_gturegno'];
	$fees_head = $_REQUEST['head_name'];

	if ($receipt_no == "") {
		$condition = "";
		$where = "";
		if ($branch_id != "") {
			$where .= " and frm.branch_id='$branch_id'";
		}
		if ($year_id != "") {
			$where .= " and frm.year_id='$year_id'";
		}
		if ($branchyear_id != "") {
			$where .= " and frm.branchyear_id='$branchyear_id'";
		}
		if ($allsem == "true" && $semester_id == '2') {
			$where .= " and frm.semester_id='$semester_id'-'1'";
		} else {
			$where .= " and frm.semester_id='$semester_id'";
		}
		if ($student_id != "") {
			$where .= " and frm.student_id='$student_id'";
		}
		if ($fees_type != "") {
			$where .= " AND fh.fees_type = '" . $fees_type . "' ";
		}

		//fees year start
		$SQL_year = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%" . $adm_year . "%' LIMIT 0 , 1";
		$SQL_c2d_year = dbQuery($SQL_year);
		$year_arr = dbArray($SQL_c2d_year);
		$c2d_year = $year_arr[0];
		$fees_year = "";
		$test = "Freess4";
		$SQL_fees = "SELECT year_id FROM `cms_yearmaster` WHERE `year_name` LIKE '%" . $adm_year . "%' LIMIT 0 , 1";
		$SQL_fees_res = dbQuery($SQL_fees);
		$fees_year_arr = dbArray($SQL_fees_res);
		$fees_year = $fees_year_arr[0];
		
		// Fees year end
		if ($fees_year == "") {
			$fees_year = $c2d_year;
		}
		if ($amount != '' && $fees_type != 'Stationary') {
			$SQL = "select
				fh.head_name,
				fh.head_id,
				fh.fees_type,
				fs.setup_id,
				fs.semester_id,
				fs.year_id,
				fs.branchyear_id,
				(fs.fees_amount - $amount)as fees_amount
			from
				cms_feeshead fh left join cms_feessetup fs on fh.head_id = fs.head_id
					AND fs.year_id =$year_id
			WHERE
				(fs.fees_amount != '' OR fh.head_active = 0)
				AND fh.college_id = " . $_SESSION['VedantnrCollegeId'];

			if ($year_id != "") {
				$SQL .= "AND fs.year_id =' $year_id ' ";
			}
			if ($branchyear_id != "") {
				$SQL .= " AND fs.branchyear_id = '$branchyear_id'";
			}
			if ($semester_id != "") {
				$SQL .= " AND fs.semester_id = '$semester_id'";
			}
			if ($fees_type != "Transport") {
				$SQL .= " AND (fh.fees_type = '" . $fees_type . "' OR fh.fees_type = 'Other') AND fs.branch_id = '" . $branch_id . "'  ";
			} else {
				$SQL .= " AND fh.fees_type = '" . $fees_type . "' ";
				$SQL .= " AND fh.head_name = '" . $fees_head . "' ";
				$SQL .= " AND fs.branch_id = '" . $branch_id . "' ";
			}
			if ($gender == 'FEMALE' || $gender == 'Female') {
				$SQL .= " AND fh.head_name != 'Tuition Fee'";
			}
			if ($lastcollege == '') {
				$SQL .= " AND fh.head_name != 'Deposite Fee'";
				$SQL .= " AND fh.head_name != 'Medical Fee'";
				/*$SQL .= " AND fh.head_name != 'College Deposite'";
				$SQL .= " AND fh.head_name != 'Medical Fee.'";*/
			}
			if ($pshy == '') {
				$SQL .= " AND fh.head_name != 'PSY Laboratory Fee'";
			}
			$SQL .= " order by fh.head_id,fh.fees_type ASC ";
		} else {
			$SQL = "select
				fh.head_name,
				fh.head_id,
				fh.fees_type,
				fs.setup_id,
				fs.year_id,
				fs.branchyear_id,
				fs.semester_id,
				fs.fees_amount
			from
				cms_feeshead fh left join cms_feessetup fs on fh.head_id = fs.head_id	
			WHERE
				(fs.fees_amount != '' OR fh.head_active = 0)
				AND fh.college_id = " . $_SESSION['VedantnrCollegeId'];

			if ($year_id != "") {
				$SQL .= " AND fs.year_id =' $year_id ' ";
			}
			if ($branchyear_id != "") {
				$SQL .= " AND fs.branchyear_id = '$branchyear_id'";
			}
			if ($semester_id != "") {
				$SQL .= " AND fs.semester_id = '$semester_id'";
			}
			if ($fees_type != "Transport") {
				$SQL .= " AND (fh.fees_type = '" . $fees_type . "' OR fh.fees_type = 'Other') AND fs.branch_id = '" . $branch_id . "'  ";
			} else {
				$SQL .= " AND fh.fees_type = '" . $fees_type . "' ";
				$SQL .= " AND fh.head_name = '" . $fees_head . "' ";
				$SQL .= " AND fs.branch_id = '" . $branch_id . "' ";
			}
			if ($gender == 'FEMALE' || $gender == 'Female') {
				$SQL .= " AND fh.head_name != 'Tuition Fee'";
			}
			if ($pshy == '') {
				$SQL .= " AND fh.head_name != 'PSY Laboratory Fee'";
			}
			if ($lastcollege == '') {
				$SQL .= " AND fh.head_name != 'Deposite Fee'";
				$SQL .= " AND fh.head_name != 'Medical Fee'";
				/*$SQL .= " AND fh.head_name != 'College Deposite'";
				$SQL .= " AND fh.head_name != 'Medical Fee.'";*/
			}
			$SQL .= " order by fh.head_id ";
		}
		//echo $SQL;
		$result = dbQuery($SQL);
		$total = dbRows($result);
		$flag_admby = 0;
		$flag_amt = 0;
		$studentByAcpdc = $objStudentFeesRecMgmt->CheckAdmission($student_id);
		$flag_tfw = $studentByAcpdc['tfw_scheme'];

		if ($studentByAcpdc['student_admby'] == "ACPDC") {
			$flag_admby = 1;
		}

		if ($branch_id != "") {
			$SQL .= " AND fs.branch_id = '$branch_id'";
		}

		if ($year_id != "") {
			$SQL .= " AND fs.year_id =' $year_id ' ";
		}

		if ($branchyear_id != "") {
			$SQL .= " AND fs.branchyear_id = '$branchyear_id'";
		}

		if ($semester_id != "") {
			$SQL .= " AND fs.semester_id = '$semester_id'";
		}
		/* START RETEST FEE CALCULATION */
		$SQL = "SELECT * FROM cms_resultmaster 
				WHERE 
					result = 'Fail' AND
					college_id = " . $_SESSION["VedantnrCollegeId"] . " AND	
					student_id  = " . $student_id;
		//echo $SQL;
		$RES = dbQuery($SQL);
		$NOOFRES = dbRows($RES);
		$retestfee = "";
		$totalfees = $RetestFeePerSub * $NOOFRES;
		if ($totalfees > $RetestFeeMax) {
			$retestfee = $RetestFeeMax;
		} else {
			$retestfee = $totalfees;
		}
		$temparrayhead_id = array();
		$temparrayfees_type = array();
		$temparrayhead_name = array();
		$temparrayfees_amount = array();
		/* END RETEST FEE CALCULATION */
		if (dbRows($result) > 0) {
			$i = 0;//falguni
			while ($row = dbArray($result)) {
				$college_id = $_SESSION["VedantnrCollegeId"];
				if (($college_id == "5") && ($semester_id == "39")) {
					if ($row["head_name"] == "TUITION FEES" && ($admby == 'Online' && ($gender == 'Female' || $gender == 'FEMALE'))) {
						$temparrayhead_id[$i] = $row["head_id"];
						$temparrayfees_type[$i] = $row["fees_type"];
						$temparrayhead_name[$i] = $row["head_name"];
						$temparrayfees_amount[$i] = $row["fees_amount"] - 200;
						$i++;
					} else if ($row["head_name"] == "TUITION FEES" && ($admby == 'Online' && $gender == 'Male')) {
						$temparrayhead_id[$i] = $row["head_id"];
						$temparrayfees_type[$i] = $row["fees_type"];
						$temparrayhead_name[$i] = $row["head_name"];
						$temparrayfees_amount[$i] = $row["fees_amount"] - 500;
						$i++;
					} else {
						$temparrayhead_id[$i] = $row["head_id"];
						$temparrayfees_type[$i] = $row["fees_type"];
						$temparrayhead_name[$i] = $row["head_name"];
						$temparrayfees_amount[$i] = $row["fees_amount"];
						$i++;
					}
				} else {
					$temparrayhead_id[$i] = $row["head_id"];
					$temparrayfees_type[$i] = $row["fees_type"];
					$temparrayhead_name[$i] = $row["head_name"];
					$temparrayfees_amount[$i] = $row["fees_amount"];
					$i++;
				}
			}
		}
		$resultRecived = $objStudentFeesRecMgmt->getAllRecordsRecivedFees($limit = 0, $start = 0, $where, $order_field = "fh.head_id", $order_type = "ASC");
		$totalRecived = dbRows($resultRecived);
		if (dbRows($resultRecived) > 0) {
			$Recivedheadid = array();
			$Recivedamt = array();
			$temparrayfees_discount = array();
			$i = 0;
			while ($row = dbArray($resultRecived)) {
				$Recivedheadid[$i] = $row["head_id"];
				$Recivedamt[$i] = $row["recdetail_amt"];
				$temparrayfees_discount[$i] = $row["amount_discount"];
				$i++;
			}
		}
		$j = 0;
		$feesHeadData = array("total" => count($temparrayhead_id), "data" => array());
		for ($i = 0; $i < count($temparrayhead_id); $i++) {
			if ($temparrayhead_id[$i] == $Recivedheadid[$i]) {

				$temparray["selected"] = 1;
				$temparray["head_id"] = $temparrayhead_id[$i];
				$temparray["fees_type"] = $temparrayfees_type[$i];
				$temparray["head_name"] = $temparrayhead_name[$i];
				$temparray["fees_amount"] = $temparrayfees_amount[$i];
				$temparray["fees_recviced_amount"] = $Recivedamt[$i];
				$temparray["amount_discount"] = $temparrayfees_discount[$i];
				if ($temparrayfees_amount[$i] > 0) {
					$temparray["fees_due_amount"] = floatval($temparray["fees_amount"]) - (floatval($Recivedamt[$i]) + floatval($temparrayfees_discount[$i]));
				} else {
					$temparray["fees_due_amount"] = "";
				}
			} else {
				$temparray["selected"] = 1;
				$temparray["head_id"] = $temparrayhead_id[$i];
				$temparray["fees_type"] = $temparrayfees_type[$i];
				$temparray["head_name"] = $temparrayhead_name[$i];
				$temparray["fees_amount"] = $temparrayfees_amount[$i];
				$temparray["fees_recviced_amount"] = $Recivedamt[$i];
				$temparray["fees_due_amount"] = $temparrayfees_amount[$i];
				$temparray["fees_due_amount"] = $temparrayfees_amount[$i] - $Recivedamt[$i];
			}
			$temparray["branch_name"] = "Fees Receive";
			$feesHeadData["data"][$j] = $temparray;
			$j++;
		}
	} else {
		if ($fees_type != 'Transport') {
			$condition = "";
			$where = "";
			$condition .= " and frm.receipt_no='$receipt_no'    ";
			$j = 0;
			$result1 = $objStudentFeesRecMgmt->getAllRecordsRecivedFeesEdit($limit = 0, $start = 0, $condition, $order_field, $order_type);
			$total = dbRows($result1);
			$feesHeadData = array("total" => $total, "data" => array());
			while ($row = dbArray($result1)) {
				$temparray["selected"] = 1;
				$temparray["head_id"] = $row['head_id'];
				$temparray["head_name"] = $row['head_name'];
				$temparray["fees_type"] = $row['fees_type'];
				$temparray["fees_amount"] = $row['fees_amount'];
				$temparray["fees_due_amount"] = $row['recdetail_amt'];
				//$temparray["month_duration_to"] = $row['month_duration_to'];
				//$temparray["month_duration_from"] = $row['month_duration_from'];
				$temparray["discount"] = $row['discount'];
				if ($temparray["fees_amount"] > 0) {
					$temparray["fees_recviced_amount"] = $temparray["fees_amount"] - $row['recdetail_amt'];
				} else {
					$temparray["fees_recviced_amount"] = "";
				}
				$temparray["branch_name"] = "Fees Receive";
				$feesHeadData["data"][$j] = $temparray;
				$j++;
			}
		} else {
			$condition = "";
			$where = "";
			$condition .= " and frm.receipt_no='$receipt_no' and frm.fees_type='T'   ";
			$j = 0;
			$result1 = $objStudentFeesRecMgmt->getAllRecordsRecivedFeesEdit1($limit = 0, $start = 0, $condition, $order_field, $order_type);
			$total = dbRows($result1);
			$feesHeadData = array("total" => $total, "data" => array());
			while ($row = dbArray($result1)) {

				$temparray["selected"] = 1;
				$temparray["head_id"] = $row['head_id'];
				$temparray["head_name"] = $row['head_name'];
				$temparray["fees_type"] = $row['fees_type'];
				$temparray["fees_amount"] = $row['fees_amount'];
				$temparray["fees_due_amount"] = $row['recdetail_amt'];
				//$temparray["month_duration_to"] = $row['month_duration_to'];
				//$temparray["month_duration_from"] = $row['month_duration_from'];
				$temparray["discount"] = $row['discount'];

				if ($temparray["fees_amount"] > 0) {
					$temparray["fees_recviced_amount"] = $temparray["fees_amount"] - $row['recdetail_amt'];
				} else {
					$temparray["fees_recviced_amount"] = "";
				}
				$temparray["branch_name"] = "Fees Receive";
				$feesHeadData["data"][$j] = $temparray;
				$j++;
			}
		}
	}
}
echo json_encode($feesHeadData);
?>