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


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

    $branch_id   = isset($_REQUEST['branch_id'])     ? $_REQUEST['branch_id']   :  "All" ;
    $year_id  = isset($_REQUEST['year_hidden']) ? $_REQUEST['year_hidden'] : '';
    $branchyear_id  = isset($_REQUEST['branchyear_hidden']) ? $_REQUEST['branchyear_hidden'] : '';
    $semester_id  = isset($_REQUEST['semester_hidden']) ? $_REQUEST['semester_hidden'] : '';
    $student_id   = isset($_REQUEST['student_id']) ? $_REQUEST['student_id'] :  0;

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

    $condition = "";
    if($student_id != ""){
        $condition .= " and student_id='$student_id' ";
    }
    if($branch_id != ""){
        $condition .= " and branch_id='$branch_id' ";
    }
    if($year_id != ""){
        $condition .= " and year_id='$year_id' ";
    }
    if($branchyear_id != ""){
        $condition .= " and branchyear_id='$branchyear_id' ";
    }
    if($semester_id != ""){
        $condition .= " and semester_id='$semester_id' ";
    }

    $objRecFees = new StudentFeesRecMgmt();
    $result = $objRecFees->getAllRecordsRecivedFeesView($limit, $start, $condition, $order_field = "", $order_type = "");


    $total = dbRows($result);
    $i = 0;
    $feesHeadData = ["total" => $total, "data1" => []];

    if (dbRows($result) > 0) {
        while ($row = dbArray($result)) {
            $temparray = [
                "feesreceive_id" => $row["feesreceive_id"],
                "branch_id" => $row["branch_id"],
                "year_id" => $row["year_id"],
                "year_name" => $row["year_name"],
                "branchyear_id" => $row["branchyear_id"],
                "branchyear_name" => $row["branchyear_name"],
                "semester_id" => $row["semester_id"],
                "semester_name" => $row["semester_name"],
                "receipt_no" => $row["receipt_no"],
                "receipt_date" => $row["receipt_date"],
                "receive_amt" => $row["receive_amt"],  
                "discount" => $row["discount"]
            ];
            $str = "";
            foreach ($headNames as $head) {
              
                $fee_field = $head['fee_field'];
                $mafi_field = $head['mafi_field'];
                $fees_amount = $row[$fee_field] - $row[$mafi_field];
                $received_amount = 0; 
                $condition1 = " fd.feesreceive_id = '" . $row["feesreceive_id"] . "' AND fd. head_id = '" . $head['head_id'] . "'";
                $outFeesDetail = $objRecFees->getAllRecordsRecivedFeesDetailView("", "", $condition1, $order_field1 = "", $order_type1 = "");

                if (dbRows($outFeesDetail) > 0) {
                    while ($rowFeesDetail = dbArray($outFeesDetail)) {
                        $received_amount = $rowFeesDetail["recdetail_amt"];
                    }
                }
                $due_amount = $fees_amount - $received_amount;

                $str .= "<div class=\"tplmain\">";
                $str .= "<div class=\"tplleft\">" . $head['name'] . ":</div>";
                $str .= "<div class=\"tplright\">" . $received_amount . "</div>";
                $str .= "</div><br/>";
            }
            $temparray["feeshead_detail"] = $str;
            $feesHeadData["data1"][$i] = $temparray;
            $i++;
        }
    }
    echo json_encode($feesHeadData);
}
?>
