<?php require "_header.php";
error_reporting(1);
?>
<script language="javascript" type="text/javascript">
</script>
<style>
	.table-report {
		font-family: Verdana, Geneva, sans-serif;
		font-size: 12px;
		text-align: left;
		border-right: 0px none;
		border-bottom: 0px none;
	}

	.table-report-inner {
		font-family: Verdana, Geneva, sans-serif;
		font-size: 12px;
	}

	.table-report tr {
		padding: 2px 2px 2px 2px;
	}

	.table-report td {
		padding: 2px 2px 2px 2px;
		border-top: 0px none;
		border-left: 0px none;
	}

	.table-report th {
		padding: 2px 2px 2px 2px;
		font-weight: bold;
		text-align: left;
		border-top: 0px none;
		border-left: 0px none;
	}

	.college_name {
		font-family: QTAbbie;
		font-size: 22px;
		font-weight: bold;
		padding: 2px 2px 2px 2px;
		text-align: center;
	}

	.college_address {
		font-family: Tahoma, Geneva, sans-serif;
		font-size: 11px;
		text-align: center;
		padding: 2px 2px 2px 2px;
	}

	.report_name {
		font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
		font-size: 13px;
		background-color: #CCC;
		text-align: center;
		font-weight: bold;
		padding: 2px 2px 2px 2px;
	}
</style>
<?php
$adm_year = '';
$header_row_string = "Student Debit Fees Report";
$branch_id = Escape($_REQUEST['branch_hidden']);
$semester_id = Escape($_REQUEST['semester_hidden']);
$branchyear_id = Escape($_REQUEST['branchyear_hidden']);
$year_id = Escape($_REQUEST['year_hidden']);
$student_id = Escape($_REQUEST['student_hidden']);
$head_id = Escape($_REQUEST['head_hidden']);



$branch_disp = Escape($_REQUEST['branch_disp']);
$year_disp = Escape($_REQUEST['year_disp']);
$branchyear_disp = Escape($_REQUEST['branchyear_disp']);
$semester_disp = Escape($_REQUEST['semester_disp']);
$head_disp = Escape($_REQUEST['head_disp']);


// $pos = strpos($_REQUEST['head_hidden'], 'All');
// if ($pos !== false) {
// 	$head_id = "All";
// 	$head_name = "All";
// }


if ($head_id == "") {
	$head_id = "All";
	$head_name = "All";
} else if ($head_id == "All") {
	$head_name = "All";
}

$show_mobile_no = Escape($_REQUEST['show_mobile_no']);
if ($branch_id == "") { ?>
	<script language="javascript" type="text/javascript">
		location.href = 'report-fees-dueamt.php';
	</script><?php
} elseif ($_SESSION['VedantnrCollegeId'] != "" && isset($_SESSION['VedantnrCollegeId'])) {
	$objFees = new StudentFeesRecMgmt();
	ob_start();
	$i = 1;
	/*$sql_student = "
					   SELECT
						   sm.student_id,
						   sm.student_gturegno,
						   CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
						   YEAR(sm.student_admdate) as adm_year,
						   sm.student_admby,
						   sm.student_techcerti,
						   sm.tfw_scheme,
						   sm.student_scholarship,
						   student_mobilestudents,
						   student_mobileparents,
						   (SELECT branch_name 
								   FROM cms_branchmaster bm 
								   WHERE bm.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sd.cur_branchid =bm.branch_id) AS branch_name,
						   (SELECT year_name 
								   FROM cms_yearmaster ym  WHERE  sd.year_id  =ym.year_id) AS year_name,
						   (SELECT branchyear_name
								   FROM cms_branchyear bry 
								   WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_branchyearid = bry.branchyear_id) AS branchyear_name,
						   (SELECT semester_name
								   FROM cms_semestermaster sem 
								   WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_semesterid = sem.semester_id) AS semester_name	  							
					   FROM
						   cms_studentmaster sm,
						   cms_studentdetail sd
					   WHERE
						   sm.student_id = sd.student_id AND
						   sm.student_active = 1";*/
	$condition = "";
	if ($student_id != 0 && $student_id != "")
		$condition .= " AND sm.student_id = $student_id ";

	if ($branch_id != "" && $branch_id != "All")
		$condition .= " AND sd.cur_branchid = $branch_id ";
	if ($branchyear_id != "")
		$condition .= " AND sd.cur_branchyearid = $branchyear_id ";
	if ($year_id != "")
		$condition .= " AND sd.year_id = $year_id ";

	if ($semester_id != "")
		$condition .= " AND sd.cur_semesterid = $semester_id ";


	$condition .= " ORDER BY sm.student_surname ,sm.student_firstname,sm.student_lastname,sm.student_gturegno ";
	$res_student = $objFees->getfeesdueamt($condition);
	//echo $sql_student;
	//$res_student = dbQuery($sql_student);
	if (dbRows($res_student) > 0) { ?>
		<img src="<?php echo $SiteBaseUrl . $CollegeImagePath . $_SESSION["VedantnrCollegeLogo"]; ?>" alt="College Logo"
			style="width: 80px; height: auto; float: left; margin-right: 10px;">
		<div style="display: inline-block; vertical-align: middle;">
			<b style="font-size: 30px;"><?php echo $_SESSION["VedantnrCollegeName"]; ?></b><br>
			<?php echo $_SESSION["VedantnrCollegeAddress"]; ?><br>
			Phone: <?php echo $_SESSION["VedantnrCollegePhone"]; ?>&nbsp;&nbsp;
			Email: <?php echo $_SESSION["VedantnrCollegeEmail"]; ?>
		</div>


		<div class="report_name">
			Student Debit Fees Report
		</div>
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<?php if ($branch_id != "") { ?>
					<td align="left" style="border:solid 0px red;"><b>Course:</b><?php echo $branch_disp; ?></td>

				<?php }
				if ($year_id != "") { ?>

					<td align="left" style="border:solid 0px red;"><b>Batch Year :</b><?php echo $year_disp; ?></td>
				<?php }
				if ($branchyear_id != "") { ?>
					<td align="left" style="border:solid 0px red;"><b>Course Year :</b><?php echo $branchyear_disp; ?></td>
				<?php }
				if ($semester_id != "") { ?>
					<td align="left" style="border:solid 0px red;"><b>Course Year :</b><?php echo $semester_disp; ?></td>

				<?php }
				if ($head_disp != "") { ?>
					<td align="right" style="border:solid 0px red;"><b>Head:</b><?php echo $head_disp; ?></td>
				<?php } ?>
			</tr>
		</table>
		<table width="100%" class="table-report" border="1" cellpadding="0" cellspacing="0">
			<tr>
				<th style="text-align:left;">Sr No</th>
				<th style="text-align:left;">Student Name</th>
				<th style="text-align:center;">Moblie No</th>
				<th style="text-align:center;">Facility</th>
				<th style="text-align:right;">Receiveble</th>
				<th style="text-align:right;">Received</th>
				<th style="text-align:right;">Debit Amt</th>
				<!-- <th style="text-align:right;">Mafi Amt</th> -->
			</tr>

			<?php

			$TEMPCOUNT = 1;
			$i = 1;
			while ($rec_student = dbArray($res_student)) {

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

				// Populate the fee names (fee_field and mafi_field)
				if (dbRows($feeHeads)) {
					while ($row = dbArray($feeHeads)) {
						$head_id1 = $row['head_id'];
						$head = $row['head_name'];
						$headNames[] = [
							'head_id1' => $head_id1,
							'name' => $head,
							'fee_field' => $head . "_fee",
							'mafi_field' => $head . "_mafifee"
						];
					}
				}
				$student_id1 = $rec_student['student_id'];
				//$student_gturegno = $rec_student['student_gturegno'];
				$student_name = $rec_student['student_name'];
				$tfw_scheme = $rec_student['tfw_scheme'];
				$transport_facility = $rec_student['transport_facility'];
				$student_scholarship = $rec_student['student_scholarship'];

				$total_amount = 0;
				$received_amount = 0;
				$debit_amount = 0;
				$cur_semesterid = $rec_student['cur_semesterid'];
				$semester_id = $rec_student['cur_semesterid'];
				$adm_semester_id = $rec_student['semester_id'];
				$adm_year = $rec_student['adm_year'];
				$student_techcerti = $rec_student['student_techcerti'];
				$flag_amt = 0;
				$condition1 = "";

				/*$sql_setupamt = "SELECT sd.student_id ";
							foreach ($headNames as $head) {
								$sql_setupamt .= ", sd.{$head['fee_field']} , sd.{$head['mafi_field']}";
							}
							$sql_setupamt .= " FROM cms_studentdetail sd
											   WHERE 1=1 */
				$condition1 .= "AND (";
				foreach ($headNames as $head) {
					$condition1 .= "sd.{$head['fee_field']} >=0  OR sd.{$head['mafi_field']}>=0 OR ";
				}
				$condition1 = substr_replace($condition1, "", -3, 2);
				$condition1 .= ")";
				if ($student_id1 != 0 && $student_id1 != "") {
					$condition1 .= " AND sd.student_id  = $student_id1 ";
				}
				if ($branch_id != "" && $branch_id != "All") {
					$condition1 .= " AND sd.cur_branchid = $branch_id ";
				}
				if ($branch_id == "All") {
					$condition1 .= " Group By sd.cur_branchid";
				}
				if ($semester_id != "") {
					$condition1 .= "AND sd.cur_semesterid = $semester_id ";
				}
				if ($branchyear_id != "") {
					$condition1 .= " AND sd.cur_branchyearid = $branchyear_id ";
				}
				if ($year_id != "") {
					$condition1 .= " AND sd.year_id = $year_id";
				}
				$res_setupamt = $objFees->getfeesdueamt1($headNames, $condition1);
				//	echo $sql_setupamt;
				//$res_setupamt = dbQuery($sql_setupamt);
				$flag_amt = 0;
				$total_amount = 0;
				$total_fee_field = 0;
				$fee_value = 0;
				$mafi_fee_value = 0;
				$total_amount1 = 0;
				while ($rec_setupamt = dbArray($res_setupamt)) {
					if ($tfw_scheme == 1) {
						$total_amount = 0;
					} else {
						foreach ($headNames as $head) {
							$fee_value = $rec_setupamt[$head['fee_field']];
							$mafi_fee_value += $rec_setupamt[$head['mafi_field']];
							$total_fee_field = $fee_value;
							$head_ids_array = explode(",", $head_id);
							if ($head_id == "All" || in_array($head['head_id1'], $head_ids_array)) {
								$total_amount1 += $total_fee_field;
							}
						}
						$total_amount = $total_amount1 - $mafi_fee_value;
					}
				}
				?>
				<?php
				/*$sql_receivedamt = "
											SELECT
												SUM(recdetail_amt) AS receive_amt
											FROM
												cms_feesreceivemaster frm,
												cms_feesreceivedetail frd
											WHERE
												frm.feesreceive_id = frd.feesreceive_id ";*/
				$condition2 = "";
				if ($student_id1 != 0 && $student_id1 != "")
					$condition2 .= " AND frm.student_id = $student_id1 ";

				if ($branch_id != "" && $branch_id != "All")
					$condition2 .= " AND frm.branch_id = $branch_id ";

				if ($year_id != "")
					$condition2 .= " AND frm.year_id = $year_id ";

				if ($branchyear_id != "")
					$condition2 .= " AND frm.branchyear_id = $branchyear_id ";

				if ($semester_id != "")
					$condition2 .= " AND frm.semester_id = $semester_id ";

				if ($head_id != "" && $head_id != "undefined" && $head_id != "All") {
					$condition2 .= " AND frd.head_id IN(" . $head_id . ") ";
				}
				//$res_receivedamt = dbQuery($sql_receivedamt);
				$res_receivedamt = $objFees->getfeesdueamt3($condition2);
				$rec_receivedamt = dbArray($res_receivedamt);
				$received_amount = $rec_receivedamt['receive_amt'];
				$debit_amount = $total_amount - $received_amount;

				?>
				<tr>
					<td>&nbsp;<?php echo $i++; ?></td>

					<td>&nbsp;<?php echo $student_name; ?></td>

					<td style="text-align:center;">&nbsp;
						<?php
						if ($rec_student['student_mobileother'] != " " && $rec_student['student_mobileother'] != 0) {
							echo $rec_student['student_mobileother'];
						}

						if ($rec_student['student_fathermobile'] != " " && $rec_student['student_fathermobile'] != 0) {
							echo "&nbsp;&nbsp;" . $rec_student['student_fathermobile'];
						}
						//	if($rec_student['student_mobileparents']!=" " && $rec_student['student_mobileparents']!=0){
						//		echo "&nbsp;&nbsp;" .$rec_student['student_mobileparents'];
						//	}
						// if($rec_student['student_mothermobile']!=" " && $rec_student['student_mothermobile']!=0){
						//	echo "&nbsp;&nbsp;" .$rec_student['student_mothermobile'];
						//}
						//if($rec_student['student_phoner']!=" " && $rec_student['student_phoner']!=0){
						//	echo "&nbsp;&nbsp;" .$rec_student['student_phoner'];
						//}
			
						?>
					<td style="text-align:center;">&nbsp; <?php echo $rec_student['transport_facility'] == 1 ? 'H' : 'U'; ?></td>
					<td style="text-align:right; padding-right:2px;">&nbsp;<?php
					echo $total_amount;

					if ($received_amount == 0 && $total_amount > 0) {
						$received_amount = 0;
					}

					?>
					</td>
					<td style="text-align:right; padding-right:2px;">&nbsp;<?php echo $received_amount; ?></td>
					<td style="text-align:right; padding-right:2px;">&nbsp;<?php echo $debit_amount; ?></td>
					<!-- <td style="text-align:right; padding-right:2px;">&nbsp;<?php echo $mafi_fee_value; ?></td> -->

				</tr>
				<?php
				$gtotal_amount += $total_amount;
				$greceived_amount += $received_amount;
				$gdebit_amount += $debit_amount;
				$gmafi_amount += $mafi_fee_value;


			}
			//$ntotal_amount += $gtotal_amount;
			//$nreceived_amount += $greceived_amount;
			//$ndebit_amount += $gdebit_amount;
			?>
			<tr>
				<th colspan="4" style="text-align:center;">Total</th>
				<th style="text-align:right; padding-right:2px;">&nbsp;<?php echo $gtotal_amount; ?></th>
				<th style="text-align:right; padding-right:2px;">&nbsp;<?php echo $greceived_amount; ?></th>
				<th style="text-align:right; padding-right:2px;">&nbsp;<?php echo $gdebit_amount; ?></th>
				<!-- <th style="text-align:right; padding-right:2px;">&nbsp;<?php echo $gmafi_amount; ?></th> -->
			</tr>
			
		</table>
		<?php
	} else {
		echo "No data found for selected criteria.";
	}
	?>
	<?php
	$HtmlCode = ob_get_contents();
	ob_end_flush();
	$header_row_string = "";
	$HtmlUrl = "feesdueamt-report.html";
	$HtmlPath = "report/" . $HtmlUrl;
	createHTMLReportPage($header_row_string, $HtmlUrl, $HtmlPath, $HtmlCode);
	?>
	<br />
	<form name="frm" id="frm" action="" method="post">
		<input type="hidden" name="txtFile" id="txtFile" value="<?php echo $HtmlUrl; ?>" />
		<input type="button" name="btnPrint" id="btnPrint" value="Print" onClick="SetPageAction('PRINT');" />
		<input type="button" name="btnPdf" id="btnPdf" value="Create PDF" onClick="SetPageAction('PDF');" />
		<input type="button" name="btnHtml" id="btnHtml" value="Create HTML" onClick="SetPageAction('HTML');" />
		<!--<input type="button" name="btnWord" id="btnWord" value="Create WORD" onClick="SetPageAction('WORD');" />
		<input type="button" name="btnExcel" id="btnExcel" value="Create EXCEL" onClick="SetPageAction('Excel');" />-->
	</form>
	<script language="javascript" type="text/javascript">
		function SetPageAction(varAction) {
			if (varAction == "PRINT") {
				document.frm.action = "print/create-print.php";
				document.frm.target = "_blank";
				document.frm.submit();
			}
			else if (varAction == "PDF") {
				document.frm.action = "convertpdf/create-pdf.php";
				document.frm.target = "_blank";
				document.frm.submit();
			}
			else if (varAction == "HTML") {
				document.frm.action = "<?php echo $HtmlPath; ?>";
				document.frm.target = "_blank";
				document.frm.submit();
			}
			else if (varAction == "WORD") {
				document.frm.action = "convertword/create-word.php";
				document.frm.target = "_blank";
				document.frm.submit();
			}
			else if (varAction == "Excel") {
				document.frm.action = "convertword/create-excel.php";
				document.frm.target = "_blank";
				document.frm.submit();
			}
		}
	</script>
	<?php

} else {
	echo "Session Expired";
}
?>
<?php require "_footer.php"; ?>