<?php
namespace App\Controllers;
SESSION_START();
use \Core\View;
use \Core\Inputs;
use \Core\Userlog;
use \Core\Menu;
use \Core\Form;
use App\Models\Homem;
use App\Email\Emails;
use App\Controllers\Extra;
class Gateway extends \Core\Controller
{
    public static function userid($id)
    {
        if($id == 1)
        {
            return 639;
        }
        if($id == 3)
        {
            return 202;
        }
        if($id == 4)
        {
            return 139;
        }
        if($id == 5)
        {
            return 176;
        }
    }
    public static function orgname($id)
    {
        if($id == 1)
        {
            return "HINDU";
        }
        if($id == 3)
        {
            return "HCET";
        }
        if($id == 4)
        {
            return "MCA";
        }
        if($id == 5)
        {
            return "MBA";
        }
    }
    public static function daywisetotalreceipts()
    {
        if($_SESSION['user_role'] == 6 || $_SESSION['user_role'] == 5)
        {
            $subtable = '';
            $a = '';
            if(isset($_POST["fromdate"]))
            {
                $date1 = $_POST["fromdate"];
                $date2 = $_POST["todate"];
                $_SESSION["org_name"] = static::orgname($_SESSION["org_id"]);
                // $_SESSION["user_id"] = static::userid($i);
                $query = static::userquery($_SESSION["org_id"], $date1, $date2);
                $data = Homem::getdata($query);
                $entryid = array();
                foreach ($data as $h) {
                    $entryid[] = $h['entry_id'];
                }
                $userid = array_values(array_unique($entryid));
                sort($userid);
                $users = '(' . implode(', ', $userid) . ')';
                $udata = Homem::getdata("SELECT id, name FROM user_master WHERE id IN ".$users);
                $a .= '<table border="1px">';
                $a .= '
                    <thead>
                        <tr>
                            <th>Fee Type</th>
                            <th>SID</th>
                            <th>Regd . No.</th>
                            <th>Name</th>
                            <th>Amount</th>
                            <th>Fee Mode</th>
                            <th>paid date</th>
                            <th>User Name</th>
                            <th>Receipt ID</th>
                            <th>recieptno</th>
                            <th>remarks</th>
                            <th>orderid</th>
                            <th>bankref</th>
                        </tr>
                    </thead>';
                $a .= '<tbody>';
                $tutionfee = Homem::getdata("SELECT um.id, um.no, um.name, sn.nid, sn.amount, sn.recieptno, sn.paid_date, sn.orderid, sn.remarks, sn.paid_date, sn.bankref, sn.fk_feemode_id, sn.entry_id FROM stdnt_normalfee sn JOIN user_master um ON um.id=sn.no WHERE DATE(sn.paid_date) BETWEEN '$date1' AND '$date2' ORDER BY 'entry_id' ASC");
                $a .= static::receiptlist($tutionfee, $udata, "Tution Fee");
                $examfee = Homem::getdata("SELECT um.id, um.no, um.name, sn.eid as nid, sn.paidamount as amount, sn.eid as recieptno, sn.entry_date as paid_date, sn.remarks as orderid, sn.remarks, sn.entry_date as paid_date, sn.remarks as bankref, sn.fk_feemode_id, sn.entry_id FROM stdnt_examfee sn JOIN user_master um ON um.id=sn.no WHERE DATE(sn.entry_date) BETWEEN '$date1' AND '$date2' ORDER BY 'entry_id' ASC");
                $a .= static::receiptlist($examfee, $udata, "Exam Fee");
                $additionalfee = Homem::getdata("SELECT um.id, um.no, um.name, sn.id as nid, sn.total as amount, sn.id as recieptno, sn.paid_date as paid_date, sn.remarks as orderid, sn.remarks, sn.paid_date as paid_date, sn.remarks as bankref, sn.mode as fk_feemode_id, sn.fk_user_id as entry_id FROM stdnt_additionalfee sn JOIN user_master um ON um.id=sn.sid WHERE DATE(sn.paid_date) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC");
                $a .= static::receiptlist($additionalfee, $udata, "Additional Fee");
                $monthlyfee = Homem::getdata("SELECT um.id, um.no, um.name, sn.id as nid, sn.amount as amount, sn.id as recieptno, sn.edate as paid_date, sn.remarks as orderid, sn.remarks, sn.edate as paid_date, sn.remarks as bankref, sn.feemode, sn.fk_user_id as entry_id FROM stdnt_monthlyfee sn JOIN user_master um ON um.id=sn.sid WHERE DATE(sn.edate) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC;");
                $a .= static::receiptlist($monthlyfee, $udata, "Monthly Fee");
                $eventfee = Homem::getdata("SELECT sn.sid, sn.sno as no, sn.name, sn.id as nid, sn.amount as amount, sn.id as recieptno, sn.edate as paid_date, sn.orderid as orderid, sn.remarks, sn.edate as paid_date, sn.bankref as bankref, sn.feemode, sn.fk_user_id as entry_id FROM stdent_eventfee sn JOIN user_master um ON um.id=sn.fk_user_id WHERE DATE(sn.edate) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC");
                $a .= static::receiptlist($eventfee, $udata, "Event Fee");
                $extrafee = Homem::getdata("SELECT sn.id, sn.id as no, sn.name, sn.id as nid, sn.amount as amount, sn.id as recieptno, sn.edate as paid_date, sn.remarks as orderid, sn.remarks, sn.edate as paid_date, sn.remarks as bankref, sn.feemode, sn.fk_user_id as entry_id FROM stdent_extrafee sn JOIN user_master um ON um.id=sn.fk_user_id WHERE DATE(sn.edate) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC;");
                $a .= static::receiptlist($extrafee, $udata, "Extra Fee");
                $applicationfee = Homem::getdata("SELECT sn.id, sn.fk_stdntenquiry_id as no, sn.name, sn.id as nid, sn.amount as amount, sn.receipt_no as recieptno, sn.regdate as paid_date, sn.fk_stdntenquiry_id as orderid, sn.fk_stdntenquiry_id, sn.regdate as paid_date, sn.fk_stdntenquiry_id as bankref, sn.feemode, sn.fk_user_id as entry_id FROM stdnt_admissionfee sn JOIN user_master um ON um.id=sn.fk_user_id WHERE DATE(sn.regdate) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC");
                $a .= static::receiptlist($applicationfee, $udata, "Application Fee");
                $admissionfee = Homem::getdata("SELECT sn.id, sn.fk_stdntenquiry_id as no, sn.name, sn.id as nid, sn.amount as amount, sn.receipt_no as recieptno, sn.regdate as paid_date, sn.fk_stdntenquiry_id as orderid, sn.fk_stdntenquiry_id, sn.regdate as paid_date, sn.fk_stdntenquiry_id as bankref, sn.feemode, sn.fk_user_id as entry_id FROM stdnt_provisionaladmissionfee sn JOIN user_master um ON um.id=sn.fk_user_id WHERE DATE(sn.regdate) BETWEEN '$date1' AND '$date2' ORDER BY `fk_user_id` ASC;");
                $a .= static::receiptlist($admissionfee, $udata, "Pro-Admission Fee");
                $proadditionalfee = Homem::getdata("SELECT sn.sid as id, sn.sno as no, um.name, sn.id as nid, sn.total as amount, sn.id as recieptno, sn.paid_date as paid_date, sn.remarks as orderid, sn.remarks, sn.paid_date as paid_date, sn.remarks as bankref, sn.mode as fk_feemode_id, sn.fk_user_id as entry_id FROM stdnt_additional_addfee sn JOIN stdnt_admissionfee um ON um.id=sn.sid WHERE DATE(sn.paid_date) BETWEEN '$date1' AND '$date2' ORDER BY sn.fk_user_id ASC");
                $a .= static::receiptlist($proadditionalfee, $udata, "Pro-Additional Fee");
                $a .= '</tbody>';
                $a .= '</table>';
            }
            echo Menus::css();
            echo '<body>';
            echo $_SESSION['menu'];
            echo '
                <div class="clearfix"></div>
                <form action="#" method="POST">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-lg-4"></div>
                            <div class="col-lg-4">                                       
                                <div class="row">
                                    <div class="col-lg-12">
                                        <section class="box" style="margin-top: 10px;">
                                            <header class="panel_header">
                                                <h5 class="title pull-left">Date Wise Fee Collection Report</h5>
                                                <div class="actions panel_actions pull-right">
                                                    <a class="fa fa-refresh" onclick="refreshClick()"></a>
                                                    <a class="box_toggle fa fa-chevron-down"></a>
                                                </div>
                                            </header>
                                            <div class="content-body">    
                                                <div class="row">
                                                    <div class="col-lg-6">
                                                        <div class="form-group">                                            
                                                            <input placeholder="From Date" class="form-control" name="fromdate" id="fromdate" onfocus="(this.type=`date`)" onblur="(this.type=`text`)">
                                                        </div>
                                                    </div>    
                                                    <div class="col-lg-6">
                                                        <div class="form-group">             
                                                        <input placeholder="To Date" class="form-control" name="todate" id="todate" onfocus="(this.type=`date`)" onblur="(this.type=`text`)">                               
                                                        </div>
                                                    </div>                            
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">  
                                                        <div class="form-group">
                                                            <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>                        
                                        </section>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-4"></div>
                        </div>
                        <div class="row print-this">
                            <div class="col-lg-1"></div>
                            <div class="col-lg-10">
                            '.$a.'
                            </div>
                            <div class="col-lg-1"></div>
                        </div>
                    </div>
                </form>
                ';
            echo '</body>';
            echo Menus::script();
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function receiptlist($tfee, $udata, $name)
    {
        if(sizeof($tfee) > 0)
        {
            $a = '';
            for($j=0; $j<sizeof($tfee); $j++)
            {
                $a .= '<tr>';
                $a .= '<td>'.$name.'</td>';
                $a .= '<td>'.$tfee[$j]["id"].'</td>';
                $a .= '<td>'.$tfee[$j]["no"].'</td>';
                $a .= '<td>'.$tfee[$j]["name"].'</td>';
                $a .= '<td>'.$tfee[$j]["amount"].'</td>';
                $a .= '<td>'.static::feemode($tfee[$j]["fk_feemode_id"]).'</td>';
                $a .= '<td>'.$tfee[$j]["paid_date"].'</td>';
                $a .= '<td>'.static::getnamebyid($tfee[$j]["entry_id"], $udata).'</td>';
                $a .= '<td>'.$tfee[$j]["nid"].'</td>';
                $a .= '<td>'.$tfee[$j]["recieptno"].'</td>';
                $a .= '<td>'.$tfee[$j]["remarks"].'</td>';
                $a .= '<td>'.$tfee[$j]["orderid"].'</td>';
                $a .= '<td>'.$tfee[$j]["bankref"].'</td>';
                $a .= '</tr>';
            }
            return $a;
        }
    }
    public static function feemode($id)
    {
        if($id == 1)
        {
            return "Cash";
        }
        if($id == 2)
        {
            return "Bank Challana";
        }
        if($id == 3)
        {
            return "Online";
        }
        if($id == 4)
        {
            return "Scholarship";
        }
        if($id == 5)
        {
            return "Concession";
        }
        if($id == 6)
        {
            return "No Receipt";
        }
        if($id == 7)
        {
            return "Payment Gateway";
        }
    }
    public static function userquery($id, $date1, $date2)
    {
        if($id != 3)
        {
            $data = "SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paid_date` FROM `stdnt_normalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paydate` FROM `stdnt_examfee` WHERE DATE(`paydate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `mode`, `paid_date` FROM `stdnt_additionalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdnt_monthlyfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdent_eventfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_admissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_provisionaladmissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2'";
            return $data;
        }
        else
        {
            $data = "SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paid_date` FROM `stdnt_normalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paydate` FROM `stdnt_examfee` WHERE DATE(`paydate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `mode`, `paid_date` FROM `stdnt_additionalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdnt_monthlyfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdent_eventfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_admissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_provisionaladmissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `mode`, `paid_date` FROM `stdnt_additional_addfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' UNION SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdent_extrafee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2'";
            return $data;
        }
    }

    public static function getnamebyid($id, $data) 
    {
        // Loop through each item in the array
        foreach ($data as $item) {
            // Check if the id matches
            if ($item['id'] == $id) {
                return $item['name'];  // Return the name
            }
        }
        return null;  // Return null if id not found
    }

    public static function userwisefeemode()
    {
        if($_SESSION['user_role'] == 6 || $_SESSION['user_role'] == 5)
        {
            $subtable = '';
            $a = '';
            if(isset($_POST["fromdate"]))
            {
                $date1 = $_POST["fromdate"];
                $date2 = $_POST["todate"];
                $query = static::userquery($_SESSION["org_id"], $date1, $date2);
                $data = Homem::getdata($query);
                $entryid = array();
                $femode = array();
                $date = array();
                foreach ($data as $h) {
                    $entryid[] = $h['entry_id'];
                    $femode[] = $h['fk_feemode_id'];
                    $date[] = $h['paid_date'];
                }
                $userid = array_values(array_unique($entryid));
                sort($userid);
                $feemode = array_values(array_unique($femode));
                sort($feemode);
                $feedate = array_values(array_unique($date));
                sort($feedate);
                $users = '(' . implode(', ', $userid) . ')';
                $udata = Homem::getdata("SELECT id, name FROM user_master WHERE id IN ".$users);
                $data = Homem::getdata("SELECT `entry_id`, `fk_feemode_id` as `feemode`, `paid_date`, SUM(`amount`) AS `amount` FROM `stdnt_normalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' GROUP BY `entry_id`, `fk_feemode_id`, `paid_date` ORDER BY `entry_id`, `fk_feemode_id`, `paid_date`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Tution Fee");
                $data = Homem::getdata("SELECT `entry_id`, `fk_feemode_id` as `feemode`, `entry_date` as `paid_date`, SUM(`paidamount`) AS `amount` FROM `stdnt_examfee` WHERE DATE(`entry_date`) BETWEEN '$date1' AND '$date2' GROUP BY `entry_id`, `fk_feemode_id`, `entry_date` ORDER BY `entry_id`, `fk_feemode_id`, `entry_date`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Exam Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `mode` as `feemode`, `paid_date` as `paid_date`, SUM(`total`) AS `amount` FROM `stdnt_additionalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `mode`, `paid_date` ORDER BY `fk_user_id`, `mode`, `paid_date`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Additional Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `feemode` as `feemode`, `edate` as `paid_date`, SUM(`amount`) AS `amount` FROM `stdnt_monthlyfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `feemode`, `edate` ORDER BY `fk_user_id`, `feemode`, `edate`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Monthly Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `feemode` as `feemode`, `edate` as `paid_date`, SUM(`amount`) AS `amount` FROM `stdent_eventfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `feemode`, `edate` ORDER BY `fk_user_id`, `feemode`, `edate`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Event Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `feemode` as `feemode`, `edate` as `paid_date`, SUM(`amount`) AS `amount` FROM `stdent_extrafee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `feemode`, `edate` ORDER BY `fk_user_id`, `feemode`, `edate`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Extra Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `feemode` as `feemode`, `regdate` as `paid_date`, SUM(`amount`) AS `amount` FROM `stdnt_admissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `feemode`, `regdate` ORDER BY `fk_user_id`, `feemode`, `regdate`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Application Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `feemode` as `feemode`, `regdate` as `paid_date`, SUM(`amount`) AS `amount` FROM `stdnt_provisionaladmissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `feemode`, `regdate` ORDER BY `fk_user_id`, `feemode`, `regdate`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Pro-Admission Fee");
                $data = Homem::getdata("SELECT `fk_user_id` as `entry_id`, `mode` as `feemode`, `paid_date` as `paid_date`, SUM(`total`) AS `amount` FROM `stdnt_additional_addfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' GROUP BY `fk_user_id`, `mode`, `paid_date` ORDER BY `fk_user_id`, `mode`, `paid_date`");
                $a .= static::feereport($userid, $feemode, $feedate, $data, $udata, "Pro-Additional Fee");
            }
            echo Menus::css();
            echo '<body>';
            echo $_SESSION['menu'];
            echo '
                <div class="clearfix"></div>
                <form action="#" method="POST">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-lg-4"></div>
                            <div class="col-lg-4">                                       
                                <div class="row">
                                    <div class="col-lg-12">
                                        <section class="box" style="margin-top: 10px;">
                                            <header class="panel_header">
                                                <h5 class="title pull-left">Date Wise Fee Collection Report</h5>
                                                <div class="actions panel_actions pull-right">
                                                    <a class="fa fa-refresh" onclick="refreshClick()"></a>
                                                    <a class="box_toggle fa fa-chevron-down"></a>
                                                </div>
                                            </header>
                                            <div class="content-body">    
                                                <div class="row">
                                                    <div class="col-lg-6">
                                                        <div class="form-group">                                            
                                                            <input placeholder="From Date" class="form-control" name="fromdate" id="fromdate" onfocus="(this.type=`date`)" onblur="(this.type=`text`)">
                                                        </div>
                                                    </div>    
                                                    <div class="col-lg-6">
                                                        <div class="form-group">             
                                                        <input placeholder="To Date" class="form-control" name="todate" id="todate" onfocus="(this.type=`date`)" onblur="(this.type=`text`)">                               
                                                        </div>
                                                    </div>                            
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">  
                                                        <div class="form-group">
                                                            <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>                        
                                        </section>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-4"></div>
                        </div>
                        <div class="row print-this">
                            <div class="col-lg-1"></div>
                            <div class="col-lg-10">
                            '.$a.'
                            </div>
                            <div class="col-lg-1"></div>
                        </div>
                    </div>
                </form>
                ';
            echo '</body>';
            echo Menus::script();
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function feereport($userid, $feemode, $feedate, $data, $udata, $name)
    {
        if(sizeof($data) > 0)
        {
            $a = '<table border="1px">';
            $a .= '<thead>';
            
            $a .= '<tr>';
            $a .= '<td rowspan="2">Date</td>';
            $a .= '<td rowspan="2">'.$name.'</td>';
            for($j=0; $j<sizeof($userid); $j++)
            {
                $a .= '<td colspan="'.sizeof($feemode).'">'.static::getnamebyid($userid[$j], $udata).'</td>';
            }
            $a .= '</tr>';
            $a .= '<tr>';
            for($j=0; $j<sizeof($userid); $j++)
            {
                for($k=0; $k<sizeof($feemode); $k++)
                {
                    $a .= '<td>'.static::feemode($feemode[$k]).'</td>';
                }
            }
            $a .= '</tr>';
            $a .= '</thead>';
            $a .= '<tbody>';
            for($j=0; $j<sizeof($feedate); $j++)
            {
                $a .= '<tr>';
                $a .= '<td>'.$feedate[$j].'</td>';
                $a .= '<td>'.$name.'</td>';
                for($k=0; $k<sizeof($userid); $k++)
                {
                    for($l=0; $l<sizeof($feemode); $l++)
                    {
                        $pstatus = 0;
                        for($m=0; $m<sizeof($data); $m++)
                        {
                            if($data[$m]["paid_date"] == $feedate[$j] && $data[$m]["entry_id"] == $userid[$k] && $data[$m]["feemode"] == $feemode[$l])
                            {
                                $a .= '<td>'.$data[$m]["amount"].'</td>';
                                $pstatus =  1;
                            }
                            if($m == sizeof($data)-1 && $pstatus == 0)
                            {
                                $a .= '<td>0</td>';
                            }
                        }
                    }
                }
                $a .= '</tr>';
            }
            $a .= '</tbody>';
            $a .= '</table>';
            return $a;
        }
    }
    
    // SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paid_date` FROM `stdnt_normalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `entry_id`, `fk_feemode_id`, `paydate` FROM `stdnt_examfee` WHERE DATE(`paydate`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `mode`, `paid_date` FROM `stdnt_additionalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdnt_monthlyfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdent_eventfee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_admissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `feemode`, `regdate` FROM `stdnt_provisionaladmissionfee` WHERE DATE(`regdate`) BETWEEN '$date1' AND '$date2';
    
    // SELECT DISTINCT `fk_user_id`, `mode`, `paid_date` FROM `stdnt_additional_addfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2';
    // SELECT DISTINCT `fk_user_id`, `feemode`, `edate` FROM `stdent_extrafee` WHERE DATE(`edate`) BETWEEN '$date1' AND '$date2';
    
    // SELECT `entry_id`, `fk_feemode_id`, `paid_date`, SUM(`amount`) AS `total_amount` FROM `stdnt_normalfee` WHERE DATE(`paid_date`) BETWEEN '$date1' AND '$date2' GROUP BY `entry_id`, `fk_feemode_id`, `paid_date` ORDER BY `entry_id`, `fk_feemode_id`, `paid_date`;
}