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

$out = "";
$xaction = "";
$data = "";
$where = '';
$qs = '';
$order_field = "";
$order_type = "";
$i = 0;

if ($_SESSION["VedantnrCollegeId"] != "" && isset($_SESSION["VedantnrCollegeId"])) {

    $objStudentdtailMgmt = new StudentDetailMgmt();
    $objStudentFeesRecMgmtdetail = new StudentFeesRecDetailMgmt();

    $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'];
    $student_name = $_REQUEST['student_name'];

    $student_gturegno = $_REQUEST['student_gturegno'];

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

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

    $condition1 = "";
    if ($student_id != "" && $student_id != 0) {
        $condition1 .= " and sd.student_id = $student_id ";
    }
    if ($branch_id != "" && $branch_id != 0) {
        $condition1 .= " and sd.cur_branchid = $branch_id ";
    }
    if ($year_id != "" && $year_id != 0) {
        $condition1 .= " and sd.year_id = $year_id ";
    }
    if ($branchyear_id != "" && $branchyear_id != 0) {
        $condition1 .= " and sd.cur_branchyearid = $branchyear_id ";
    }
    if ($semester_id != "" && $semester_id != 0) {
        $condition1 .= " and sd.cur_semesterid = $semester_id ";
    }

    $fees = $objStudentdtailMgmt->getfeesrecord($headNames, $condition1);

    if (dbRows($fees) > 0) {
        while ($row = dbArray($fees)) {
            $temparray = array();
            $temparray["student_id"] = $row['student_id'];
            $temparray["student_name"] = $row['student_name'];
            $temparray["transport_facility"] = $row["transport_facility"];
 
            foreach ($headNames as $head) {
        
                $fees_type = $head['fees_type'];
                $head_id = $head['head_id'];
                $head_name = $head['name'];
                $fee_field = $head['fee_field'];
                $mafi_field = $head['mafi_field'];


                if (($row[$fee_field] == 0 || $row[$fee_field] == '') && ($row[$mafi_field] == 0 || $row[$mafi_field] == '')) {
                    continue;
                }

                /*if ($head_name == "Hostel") {
                    if ($row["transport_facility"] != "1") {
                        // If the student is not assigned to the hostel, show total and received amount only
                        $fees_amount = intval($row[$fee_field]) - intval($row[$mafi_field]);
                        $condition2 = "";
                      
                        if ($branch_id != "" && $branch_id != 0) {
                            $condition2 .= " and fm.branch_id = $branch_id ";
                        }
                        if ($year_id != "" && $year_id != 0) {
                            $condition2 .= " and fm.year_id = $year_id ";
                        }
                        if ($branchyear_id != "" && $branchyear_id != 0) {
                            $condition2 .= " and fm.branchyear_id = $branchyear_id ";
                        }
                        if ($semester_id != "" && $semester_id != 0) {
                            $condition2 .= " and fm.semester_id = $semester_id ";
                        }
                        $Recivedamt = $objStudentFeesRecMgmtdetail->getRecivedfees($student_id, $head_id, $condition2);
                        $Recivedamt = dbArray($Recivedamt)['recdetail_amt'];

                        $temparray["selected"] = 1;
                        $temparray["fees_type"] = $fees_type;
                        $temparray["head_id"] = $head_id;
                        $temparray["head_name"] = $head_name;  // Include head name
                        $temparray["fees_amount"] = $fees_amount;
                        $temparray["fees_received_amount"] = intval($Recivedamt);
                        $temparray["fees_due_amount"] = 0; // Do not calculate due amount if not in hostel

                        // Store the data
                        $FeesData["data"][$i] = $temparray;
                        $i++;
                        continue;
                    }
                }*/
                $condition2 = "";
                      
                if ($branch_id != "" && $branch_id != 0) {
                    $condition2 .= " and fm.branch_id = $branch_id ";
                }
                if ($year_id != "" && $year_id != 0) {
                    $condition2 .= " and fm.year_id = $year_id ";
                }
                if ($branchyear_id != "" && $branchyear_id != 0) {
                    $condition2 .= " and fm.branchyear_id = $branchyear_id ";
                }
                if ($semester_id != "" && $semester_id != 0) {
                    $condition2 .= " and fm.semester_id = $semester_id ";
                }
                    $Recivedamt = $objStudentFeesRecMgmtdetail->getRecivedfees($student_id, $head_id, $condition2);
                    $Recivedamt = dbArray($Recivedamt)['recdetail_amt'];
                    $fees_amount = intval($row[$fee_field]) - intval($row[$mafi_field]);


                    $temparray["selected"] = 1;
                    $temparray["fees_type"] = $fees_type;
                    $temparray["head_id"] = $head_id;
                    $temparray["head_name"] = $head_name;
                    $temparray["fees_amount"] = $fees_amount;
                    $temparray["fees_received_amount"] = intval($Recivedamt);
                    $temparray["fees_due_amount"] = $fees_amount - $Recivedamt;

                    $FeesData["data"][$i] = $temparray;
                    $i++;
                
            }
        }
    }

    echo json_encode($FeesData);

}
?> 