MMCT TEAM
Server IP : 103.191.208.50  /  Your IP : 216.73.216.53
Web Server : LiteSpeed
System : Linux orion.herosite.pro 4.18.0-553.53.1.lve.el8.x86_64 #1 SMP Wed May 28 17:01:02 UTC 2025 x86_64
User : celkcksm ( 1031)
PHP Version : 7.4.33
Disable Function : show_source, system, shell_exec, passthru, popen, exec
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/celkcksm/ecampus.ncriptech.com/application/controllers/admin/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/celkcksm/ecampus.ncriptech.com/application/controllers/admin/ReportsPayments.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH . '/third_party/vendor/autoload.php';
/**
 * 
 */
class ReportsPayments  extends BaseAdminController
{
    protected $inst_id='';

    function __construct()
    {
        parent::__construct();

        $institute_data=check_institute($this->data['userdata']);

        $this->inst_name=$institute_data['inst_name'];
    }

    public function indexIncome(){
       
        if(session_userdata('isAdminLoggedin')){

            $institute_data=check_institute($this->data['userdata']);
            $inst_id=$institute_data['institute_id'];

            $this->data['page_title']='Admission Income Reports';

            $current_year=date('Y');

            $this->data['current_year']=$current_year;

            $this->data['total_admissions']=$this->adm->get_total_admissions(array('YEAR(created_at)'=>$current_year));

            $this->data['total_earning']='18800';

            $this->data['sessions']=$this->sessm->get_session(array('session_inst_id'=>$inst_id,'session_status'=>'active'),FALSE);

            $this->theme->title($this->data['page_title'])->load('reports/vw_reports_admission_income', $this->data);
        }else{

            redirect($this->data['base_url']);
        }
    }

    public function indexExpense(){

        if($this->data[userdata]->user_type=='student' ||  $this->data[userdata]->user_type=='agent' )
{
    
    redirect($this->data['base_url']);
}
        if(session_userdata('isAdminLoggedin')){

            $institute_data=check_institute($this->data['userdata']);
            $inst_id=$institute_data['institute_id'];

            $this->data['page_title']='Expense Reports';

            $current_year=date('Y');

            $this->data['current_year']=$current_year;

            $this->data['total_admissions']=$this->adm->get_total_admissions(array('YEAR(created_at)'=>$current_year));

            $this->data['total_earning']='18800';

            $this->data['sessions']=$this->sessm->get_session(array('session_inst_id'=>$inst_id,'session_status'=>'active'),FALSE);

            $this->theme->title($this->data['page_title'])->load('reports/vw_reports_expense', $this->data);
        }else{

            redirect($this->data['base_url']);
        }
    }


    public function indexDues(){
       
        if(session_userdata('isAdminLoggedin')){

            $this->data['page_title']='Dues Reports';

            $current_year=date('Y');

            $this->data['current_year']=$current_year;

            $this->data['total_admissions']=$this->adm->get_total_admissions(array('YEAR(created_at)'=>$current_year));

            $this->data['total_earning']='18800';

            $this->theme->title($this->data['page_title'])->load('reports/vw_reports_dues', $this->data);
        }else{

            redirect($this->data['base_url']);
        }
    }


    public function indexBank(){
       
        if(session_userdata('isAdminLoggedin')){

            $this->data['page_title']='Bank Reports';

            $current_year=date('Y');

            $this->data['current_year']=$current_year;

            $this->data['total_admissions']=$this->adm->get_total_admissions(array('YEAR(created_at)'=>$current_year));

            $this->data['total_earning']='18800';

            $this->theme->title($this->data['page_title'])->load('reports/vw_reports_bank', $this->data);
        }else{

            redirect($this->data['base_url']);
        }
    }


    public function indexReports() {



 if($this->data['userdata']->user_type=='student'   ||  $this->data['userdata']->user_type=='agent'  ){


    redirect($this->data['base_url']);
 }
        if (session_userdata('isAdminLoggedin')) {
    
            $this->data['page_title'] = 'Bank Reports';
    
            $institute_data = check_institute($this->data['userdata']);
            $inst_id = $institute_data['institute_id'];
    
            $current_year = date('Y');
            $this->data['current_year'] = $current_year;
            $this->data['total_admissions'] = $this->adm->get_total_admissions(array('YEAR(created_at)' => $current_year));
            $this->data['total_earning'] = '18800';
    
            $payments = array();
            $payment_details = array();
    
            // Payments details
            // Calculate start date and end date for the last three months
            $end_date = date('Y-m-d');
            $start_date = date('Y-m-d', strtotime('-3 months'));

            $param['start_date'] = $start_date;
            $param['end_date'] = $end_date;

            $students_from_payments=$this->um->get_students_from_payments($param);

    
            if (!empty($students_from_payments)) {
                
    
                foreach ($students_from_payments as $dt) {
                    $param['student_id'] = $dt->details_stu_id;
                    $param['accounting_type'] = 'income';
                    
    
                    $pay_details = $this->fm->_____get_payments_details($param, FALSE, FALSE);
    
                    if (!empty($pay_details)) {
                        foreach ($pay_details as $payment) {
                            if (in_array($payment->details_payment_platform, array('online_before_software', 'split_online_before_software'))) {
                                $details_payment_platform = 'online';
                            } else if (in_array($payment->details_payment_platform, array('split_cash'))) {
                                $details_payment_platform = 'cash';
                            } else {
                                $details_payment_platform = $payment->details_payment_platform;
                            }
    
                            $payments[$dt->details_stu_id][] = array(
                                'details-amount' => $payment->details_amount,
                                'details_created_at' => $payment->details_created_at,
                                'details_created_at_actual'=>$payment->details_created_at_actual,
                                'details_payment_platform' => $details_payment_platform,
                                'details_payment_note'=>$payment->details_payment_note
                            );
                        }
                    }
    
                    $payment_details[] = array(
                        'details_stu_id' => $dt->details_stu_id,
                        'details_stu_name' => $dt->student_name,
                        'details_course' => $dt->course_code,
                        'details_session' => $dt->session_start_year . '-' . $dt->session_end_year,
                        'details_payments' => $payments[$dt->details_stu_id]
                    );
                }
            }
    
            // print_obj($payment_details);die;
    
            $this->data['payment_details'] = $payment_details;
            $this->data['sessions'] = $this->sessm->get_session(array('session_inst_id' => $inst_id, 'session_status' => 'active'), FALSE);
            
            $institute_data=check_institute($this->data['userdata']);
            $this->data['inst_code']=$institute_data['inst_code'];
    
            $this->theme->title($this->data['page_title'])->load('reports/vw_reports_payments', $this->data);
        } else {
            redirect($this->data['base_url']);
        }
    }

    public function onLoadPaymentReports(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $session_id=post_data('session_id');
                $course_id=post_data('course_id');

                $start_date=post_data('start_date');
                $end_date=post_data('end_date');

                $end_date = (!empty($end_date))?date('Y-m-d',strtotime($end_date)):null;
                $start_date = (!empty($start_date))?date('Y-m-d',strtotime($start_date)):null;//date('Y-m-d', strtotime('-3 months'));
                
                $payments=array();
                $payment_details=array();

                //Payments details
                //if($start_date!=''){
                    $param['start_date'] =$start_date;
                //}

                //if($end_date!=''){
                    $param['end_date'] = $end_date;
                //}

                // echo $param['start_date'];

                $students_from_payments=$this->um->get_students_from_payments($param);

                //print_obj($students_from_payments);die;

                if(!empty($students_from_payments)){
                    foreach($students_from_payments as $dt){

                        $param['student_id']=$dt->details_stu_id;
                        $param['accounting_type']='income';

                        if($session_id!=0){
                            $param['session_id'] = $session_id;
                        }

                        if($course_id!=0){
                            $param['course_id'] = $course_id;
                        }

                        $pay_details=$this->fm->_____get_payments_details($param,FALSE,FALSE);

                       // print_obj($pay_details);die;

                        if(!empty($pay_details)){
                            foreach($pay_details as $payment){
                                
                                if(in_array($payment->details_payment_platform,array('online_before_software','split_online_before_software'))){
                                    $details_payment_platform='online';
                                }else if(in_array($payment->details_payment_platform,array('split_cash'))){
                                    $details_payment_platform='cash';
                                }else{
                                    $details_payment_platform=$payment->details_payment_platform;
                                }

                                /*if($details_payment_platform=='cheque'){
                                    $cheque_details=$this->fm->get_cheque_details(array(''));
                                }*/

                                $payments[$dt->details_stu_id][]=array(
                                    'details-amount'=>$payment->details_amount,
                                    'details_created_at'=>$payment->details_created_at,
                                    'details_created_at_actual'=>$payment->details_created_at_actual,
                                    'details_payment_platform'=>$details_payment_platform,
                                    'details_payment_note'=>$payment->details_payment_note
                                );
                            }
                        }

                        $payment_details[]=array(
                            'details_stu_id'=>$dt->details_stu_id,
                            'details_stu_name'=>$dt->student_name,
                            'details_course'=>$dt->course_code,
                            'details_session'=>$dt->session_start_year.'-'.$dt->session_end_year,
                            'details_payments'=>$payments[$dt->details_stu_id]
                        );
                    }
                }


                //print_obj($payment_details);die;

                $this->data['payment_details']=$payment_details;                

                $return['html']=$this->theme->view('_pages/reports/vw_reports_payments_dyna',$this->data,true);

                json_headers($return);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }

    public function onExportPaymentReportToCSV() {
        if (session_userdata('isAdminLoggedin') == TRUE && session_userdata('admin_id')) {
            if ($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD') == 'POST') {
                
                $session_id = post_data('session_id');
                $course_id = post_data('course_id');
    
                $start_date = post_data('start_date');
                $end_date = post_data('end_date');
    
                // Totals
                $totalCashPayments = 0;
                $totalOnlinePayments = 0;
                $totalChequePayments = 0;
                $totalPayments = 0;
    
                // Payments details
                $end_date = (!empty($end_date))?date('Y-m-d',strtotime($end_date)):null;
                $start_date = (!empty($start_date))?date('Y-m-d',strtotime($start_date)):null;//date('Y-m-d', strtotime('-3 months'));
                
                $payments=array();
                $payment_details=array();

                //Payments details
                //if($start_date!=''){
                    $param['start_date'] =$start_date;
                //}

                //if($end_date!=''){
                    $param['end_date'] = $end_date;
                //}

                // echo $param['start_date'];

                $students_from_payments=$this->um->get_students_from_payments($param);
    
                if (!empty($students_from_payments)) {
                    foreach ($students_from_payments as $dt) {
    
                        $param['student_id'] = $dt->details_stu_id;
                        $param['accounting_type'] = 'income';
    
                        if ($session_id != 0) {
                            $param['session_id'] = $session_id;
                        }
    
                        if ($course_id != 0) {
                            $param['course_id'] = $course_id;
                        }
    
                        if ($start_date != '') {
                            $param['start_date'] = date('Y-m-d', strtotime($start_date));
                        }
    
                        if ($end_date != '') {
                            $param['end_date'] = date('Y-m-d', strtotime($end_date));
                        }
    
                        $pay_details = $this->fm->_____get_payments_details($param, FALSE, FALSE);
    
                        if (!empty($pay_details)) {
                            foreach ($pay_details as $payment) {
                                if (in_array($payment->details_payment_platform, array('online_before_software', 'split_online_before_software'))) {
                                    $details_payment_platform = 'online';
                                    $totalOnlinePayments += $payment->details_amount;
                                } else if (in_array($payment->details_payment_platform, array('split_cash'))) {
                                    $details_payment_platform = 'cash';
                                    $totalCashPayments += $payment->details_amount;
                                } else if ($payment->details_payment_platform == 'cheque') {
                                    $details_payment_platform = 'cheque';
                                    $totalChequePayments += $payment->details_amount;
                                } else {
                                    $details_payment_platform = $payment->details_payment_platform;
                                }
    
                                $payments[$dt->details_stu_id][] = array(
                                    'details-amount' => $payment->details_amount,
                                    'details_created_at' => $payment->details_created_at,
                                    'details_created_at_actual' => $payment->details_created_at_actual,
                                    'details_payment_platform' => $details_payment_platform,
                                    'details_payment_note'=>$payment->details_payment_note
                                );
                                $totalPayments += $payment->details_amount;
                            }
                        }
    
                        $payment_details[] = array(
                            'details_stu_id' => $dt->details_stu_id,
                            'details_stu_name' => $dt->student_name,
                            'details_course' => $dt->course_code,
                            'details_session' => $dt->session_start_year . '-' . $dt->session_end_year,
                            'details_payments' => $payments[$dt->details_stu_id]
                        );
                    }
                }

               // print_obj($payment_details);die;
    
                // Generate CSV content
                $csv_output = '';
    
                // Clean output buffer
                ob_start();
                $output = fopen("php://output", "w");
    
                fputcsv($output, ['Student Data', 'Course/Session', 'Payment', 'Payment Date', 'Mode of Payment', 'Payment Note', 'Total Payment']);
    
                foreach ($payment_details as $k=>$student) {
                    $totalPayment = 0;
                    foreach ($student['details_payments'] as $payment) {
                        $totalPayment += $payment['details-amount'];
                    }
                    $paymentCount = count($student['details_payments']);
                    $studentName = $student['details_stu_name'] ?: '';
                    $courseSession = $student['details_course'] . ' / ' . $student['details_session'];
    
                    foreach ($student['details_payments'] as $index => $payment) {
                        if ($index == 0) {
                            fputcsv($output, [
                                $studentName,
                                $courseSession,
                                number_format($payment['details-amount'], 2),
                                date('d-m-Y', strtotime($payment['details_created_at'])),
                                $payment['details_payment_platform'],
                                $payment['details_payment_note'],
                                number_format($totalPayment, 2)
                            ]);
                        } else {
                            fputcsv($output, [
                                '',
                                '',
                                number_format($payment['details-amount'], 2),
                                date('d-m-Y', strtotime($payment['details_created_at'])),
                                $payment['details_payment_platform'],
                                $payment['details_payment_note'],
                                ''
                            ]);
                        }
                    }
                    // Insert blank row after each student's total payment data rows
                    // if($k>0){
                    //     fputcsv($output, []);
                    // }
                    
                }
    
                // Add totals to the CSV
                fputcsv($output, []);
                fputcsv($output, ['Total Cash Payments', '', '', '', '', '', number_format($totalCashPayments, 2)]);
                fputcsv($output, ['Total Online Payments', '', '', '', '', '', number_format($totalOnlinePayments, 2)]);
                fputcsv($output, ['Total Cheque Payments', '', '', '', '', '', number_format($totalChequePayments, 2)]);
                fputcsv($output, ['Total Payments', '', '', '', '', '', number_format($totalPayments, 2)]);
    
                fclose($output);
                $csv_output = ob_get_clean();
    
                header('Content-Type: application/json');
                echo json_encode(['csv_data' => $csv_output]);
    
            } else {
                redirect($this->data['base_url']);
            }
        } else {
            redirect($this->data['base_url']);
        }
    }


    public function indexReportsDues(){
        if (session_userdata('isAdminLoggedin')) {

            $this->data['page_title'] = 'Bank Reports';

            

        } else {
            redirect($this->data['base_url']);
        }
    }
    
    
    
    // modify the code to do the following
  
    // 1.add a blank row after student's total payment data rows

    //Income Reports
    public function onSearchIncomeReports(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $institute_data=check_institute($this->data['userdata']);
                $inst_id=$institute_data['institute_id'];

                $param['column_order'] = array(
                    null,
                    'details_amount',
                    'details_discount_amount'
                );

                $param['column_search'] = array('details_accounting_type');
                $param['order'] = array('details_id' => 'ASC');
                $posts=$this->input->post();


                $param['inst_id']=$inst_id;

                $param['accounting_type']='income';

                if(isset($posts['data_of_session']) && $posts['data_of_session']>0){
                    $param['session_id']=$posts['data_of_session'];
                }

                if(isset($posts['data_of_course']) && $posts['data_of_course']>0){
                    $param['course_id']=$posts['data_of_course'];
                }


                if(isset($posts['data_of_student']) && $posts['data_of_student']>0){
                    $param['student_id']=$posts['data_of_student'];
                }


                if(isset($posts['data_of_start_date']) && !empty($posts['data_of_start_date'])){
                    $param['start_date']=date('Y-m-d',strtotime($posts['data_of_start_date']));
                }

                if(isset($posts['data_of_end_date']) && !empty($posts['data_of_end_date'])){
                    $param['end_date']=date('Y-m-d',strtotime($posts['data_of_end_date']));
                }


                $list = $this->fm->__get_payments_details($posts,$param,FALSE,FALSE,FALSE);

                //print_obj($list);die;
                
                
                $data = array();
                $no = isset($posts['start'])?$posts['start']:0;

                $action='';

                $current_date=date('Y-m-d');
                $current_month=date('m');
                $current_year=date('Y');

                $_total_income_in_current_date=$this->fm->__get_fees_details_total(array('details_inst_id'=>$inst_id,'details_accounting_type'=>'income','DATE(details_created_at)'=>$current_date),'details_amount');

                $_total_income_in_current_month=$this->fm->__get_fees_details_total(array('details_inst_id'=>$inst_id,'details_accounting_type'=>'income','MONTH(details_created_at)'=>$current_month,'YEAR(details_created_at)'=>$current_year),'details_amount');

                $_total_income_in_current_year=$this->fm->__get_fees_details_total(array('details_inst_id'=>$inst_id,'details_accounting_type'=>'income','YEAR(details_created_at)'=>$current_year),'details_amount');

                $total_income='0';
                $total_income_in_current_date=(!empty($_total_income_in_current_date) && $_total_income_in_current_date[0]->total)?$_total_income_in_current_date[0]->total:'0';
                $total_income_in_current_month=(!empty($_total_income_in_current_month) && $_total_income_in_current_month[0]->total)?$_total_income_in_current_month[0]->total:'0';
                $total_income_in_current_year=(!empty($_total_income_in_current_year) && $_total_income_in_current_year[0]->total)?$_total_income_in_current_year[0]->total:'0';

                foreach ($list as $fees){
                    $no++;

                    $row = array();

                    $agent_payment=$this->fm->get_payment_details(array('details_order_code'=>$fees->details_order_code,'details_type'=>'agent_payment'));

                    if(!empty($agent_payment)){
                        $agent_paid_out=$agent_payment->details_amount;
                    }else{
                        $agent_paid_out='0';
                    }

                    $_total_income=$fees->details_amount-($fees->details_discount_amount+$agent_paid_out);


                    $course_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_course_id'=>$fees->details_course_id,'session_id'=>$fees->details_sess_id));

                    if(!empty($fees->stu_mid_name)){
                        $student_name=$fees->stu_first_name.' '.$fees->stu_mid_name.' '.$fees->stu_last_name;
                    }else{
                        $student_name=$fees->stu_first_name.' '.$fees->stu_last_name;
                    }

                    $details_payment_type='<span class="badge badge-xs badge-success">'.strtoupper(str_replace("_", " ", $fees->details_payment_type)).'</span>';

                    if($fees->details_type=='new_admission'){
                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$fees->details_type_id));
                        $admission_code='Admission Code:'.$admission_data->admission_code;                        
                    }else if($fees->details_type=='seat_booking'){
                        $seatbooking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$fees->details_type_id));
                        $admission_code='Booking Code:'.$seatbooking_data->booking_code;
                    }

                    $student_name.='<br>Code:'.$fees->stu_user_code.'<br>Course:'.$fees->course_code.'<br>Session:'.$fees->session_start_year.'-'.$fees->session_end_year.'<br>'.$admission_code;

                    // $row[]  =   $no;
                    $row[]  =   '<input type="checkbox" value="'.$fees->details_id.'" class="details_ids" id="btn_income_export_to_excel'.$fees->details_id.'" name="details_ids[]">';
                    $row[]  =   '<strong>'.$student_name.'</strong><br>'.$details_payment_type;
                    $row[]  =   '<strong>'.$fees->details_order_code.'</strong>';
                    $row[]  =   date('d-m-Y',strtotime($fees->details_created_at));
                    $row[]  =   $course_data->session_course_fees;
                    $row[]  =   $fees->details_amount; 
                    $row[]  =   $fees->details_discount_amount;
                    $row[]  =   $agent_paid_out;

                    $row[]  =   $_total_income;
                    
                    //$row[]  =   '<button type="button" class="btn btn-xs btn-danger btn_delete_fees" data-fees=""><i class="fa fa-trash"></i></button>';

                    $data[] = $row; 
                }

                $output = array(
                    "draw" => isset($posts['draw'])?$posts['draw']:'',
                    "recordsTotal" => $this->fm->__get_payments_details($posts,$param,FALSE,TRUE),
                    "recordsFiltered" => $this->fm->__get_payments_details($posts,$param,FALSE,TRUE),
                    "data" => $data,
                    "total_income"=>$total_income,
                    "total_income_in_current_date"=>$total_income_in_current_date,
                    "total_income_in_current_month"=>$total_income_in_current_month,
                    "total_income_in_current_year"=>$total_income_in_current_year
                );
                
                echo json_encode($output);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }

    public function onExportAdmissionIncomeReports(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $institute_data=check_institute($this->data['userdata']);
                $inst_id=$institute_data['institute_id'];

                $export_type=post_data('export_type');

                $payment_ids=$this->input->post('payment_ids');

                $data_of_session=post_data('data_of_session');
                $data_of_course=post_data('data_of_course');
                $data_of_student=post_data('data_of_student');
                $data_of_start_date=post_data('data_of_start_date');
                $data_of_end_date=post_data('data_of_end_date');

                $data_to_write=array();

                if(!empty($payment_ids)){

                    //$payment_ids = array_filter($payment_ids, function($k) { return $k !== "key2"; };

                    $param['inst_id']=$inst_id;
                    $param['accounting_type']='income';

                    if($data_of_session>0){
                        $param['session_id']=$data_of_session;
                    }

                    if($data_of_course>0){
                        $param['course_id']=$data_of_course;
                    }

                    if($data_of_student>0){
                        $param['student_id']=$data_of_student;
                    }

                    if(!empty($data_of_start_date)){
                        $param['start_date']=date('Y-m-d',strtotime($data_of_start_date));
                    }

                    if(!empty($data_of_end_date)){
                        $param['end_date']=date('Y-m-d',strtotime($data_of_end_date));
                    }

                    $i=1;
                    $row=2;
                    foreach ($payment_ids as $key => $value) {

                        $param['details_id']=$value;

                        $fees=$this->fm->___get_payments_details($param);

                        if(!empty($fees)){

                            $course_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_course_id'=>$fees->details_course_id,'session_id'=>$fees->details_sess_id));

                            if(!empty($fees->stu_mid_name)){
                                $student_name=$fees->stu_first_name.' '.$fees->stu_mid_name.' '.$fees->stu_last_name;
                            }else{
                                $student_name=$fees->stu_first_name.' '.$fees->stu_last_name;
                            }

                            if($fees->details_type=='new_admission'){
                                $admission_data=$this->adm->get_admission_data(array('admission_id'=>$fees->details_type_id));
                                $admission_code=$admission_data->admission_code;                        
                            }else if($fees->details_type=='seat_booking'){
                                $seatbooking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$fees->details_type_id));
                                $admission_code=$seatbooking_data->booking_code;
                            }

                            $agent_payment=$this->fm->get_payment_details(array('details_order_code'=>$fees->details_order_code,'details_type'=>'agent_payment'));

                            if(!empty($agent_payment)){
                                $agent_paid_out=$agent_payment->details_amount;
                            }else{
                                $agent_paid_out='0';
                            }

                            $_total_income=$fees->details_amount-($fees->details_discount_amount+$agent_paid_out);

                            $data_to_write[]=array(
                                'A'.$row=>$i,
                                'B'.$row=>strtoupper($student_name),
                                'C'.$row=>$fees->stu_user_code,
                                'D'.$row=>$admission_code,
                                'E'.$row=>$fees->details_type_text,
                                'F'.$row=>$fees->course_code,
                                'G'.$row=>$fees->session_start_year.'-'.$fees->session_end_year,
                                'H'.$row=>number_format($course_data->session_course_fees,2),
                                'I'.$row=>number_format($fees->details_discount_amount,2),
                                'J'.$row=>number_format($fees->details_amount,2),
                                'K'.$row=>number_format($agent_paid_out,2),
                                'L'.$row=>number_format($_total_income,2)
                            );

                            $i++;
                            $row++;
                        }
                    }

                    //print_obj($data_to_write);die;

                    if(!empty($data_to_write)){
                        $outputFile='Admission_income_report_'.$inst_id.'_'.date('d_m_Y');
                        $file_path=base_url('uploads/temp_files/'.$outputFile.'.xlsx');
                        $actual_path=FCPATH.'uploads/temp_files/'.$outputFile.'.xlsx';
                        $excel_return=write_excel_active_sheet($data_to_write,$outputFile,'save');

                        //echo basename($actual_path);die;

                        $return['success']=$excel_return;
                        $return['file_path']=$file_path;
                        // // Set the appropriate headers
                        // header('Content-Description: File Transfer');
                        // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                        // header('Content-Disposition: attachment; filename=' . basename($actual_path));
                        // header('Content-Transfer-Encoding: binary');
                        // header('Expires: 0');
                        // header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                        // header('Pragma: public');
                        // header('Content-Length: ' . filesize($actual_path));
                        
                        // // Read the file and output it to the browser
                        // @readfile($actual_path);

                        // //exit;
                    }else{
                        $return['error']='No data found to write in excel.';
                    }

                }else{
                    $return['error']='No data selected.';
                }

                json_headers($return);
            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }

    //Income Reports


    //Expense Reports
    public function onSearchExpenseReports(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $institute_data=check_institute($this->data['userdata']);
                $inst_id=$institute_data['institute_id'];

                $param['column_order'] = array(
                    null,
                    'details_amount',
                    'details_discount_amount'
                );

                $param['column_search'] = array('details_accounting_type');
                $param['order'] = array('details_id' => 'ASC');
                $posts=$this->input->post();


                $param['inst_id']=$inst_id;

                $param['accounting_type']='expenditure';


                if(isset($posts['data_of_session']) && $posts['data_of_session']>0){
                    $param['session_id']=$posts['data_of_session'];
                }

                if(isset($posts['data_of_course']) && $posts['data_of_course']>0){
                    $param['course_id']=$posts['data_of_course'];
                }


                if(isset($posts['data_of_student']) && $posts['data_of_student']>0){
                    $param['student_id']=$posts['data_of_student'];
                }


                if(isset($posts['data_of_start_date']) && !empty($posts['data_of_start_date'])){
                    $param['start_date']=date('Y-m-d',strtotime($posts['data_of_start_date']));
                }

                if(isset($posts['data_of_end_date']) && !empty($posts['data_of_end_date'])){
                    $param['end_date']=date('Y-m-d',strtotime($posts['data_of_end_date']));
                }


                $list = $this->fm->__get_payments_details($posts,$param,FALSE,FALSE);

                //print_obj($list);die;
                
                
                $data = array();
                $no = isset($posts['start'])?$posts['start']:0;

                $action='';

                foreach ($list as $fees){
                    $no++;

                    $row = array();

                    $order_id=explode('_', $fees->details_order_id);

                    // $details_order_id=

                    $course_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_course_id'=>$fees->details_course_id,'session_id'=>$fees->details_sess_id));

                    if(in_array($fees->details_payment_type, array('admission_agent_fees'))){
                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$fees->details_type_id));
                        $admission_code='Admission Code:'.$admission_data->admission_code;



                    }else if(in_array($fees->details_payment_type, array('seatbooking_fees'))){
                        $seatbooking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$fees->details_type_id));
                        $admission_code='Booking Code:'.$seatbooking_data->booking_code;

                        //$student_paid=$this->adm->get_seat_booking_payment_data(array('booking_payment_id'=>$fees->details_payment_id,'booking_inst_id'=>$inst_id));

                        
                    }

                    if(!empty($fees->stu_mid_name)){
                        $student_name=$fees->stu_first_name.' '.$fees->stu_mid_name.' '.$fees->stu_last_name;
                    }else{
                        $student_name=$fees->stu_first_name.' '.$fees->stu_last_name;
                    }

                    $course_cost='₹'.number_format($course_data->session_course_fees,2);

                    $student_name.='<br>Code:'.$fees->stu_user_code.'<br>Course:'.$fees->course_code.'<br>Course Cost:'.$course_cost.'<br>Session:'.$fees->session_start_year.'-'.$fees->session_end_year.'<br>'.$admission_code;

                    $agent_data=$this->um->get_agents(array('agent_user_id'=>$fees->details_agent_id));     
                    $student_paid=$this->fm->get_payment_details(array('details_type_id'=>$fees->details_type_id,'details_accounting_type'=>'income'));               

                    //print_obj($agent_data);die;

                    $row[]  =   '<input type="checkbox" value="'.$fees->details_id.'" class="details_ids" id="btn_income_export_to_excel'.$fees->details_id.'" name="details_ids[]">';
                    $row[]  =   str_replace('AGENT','CONSULTANT',strtoupper(str_replace('_', ' ', $fees->details_type)));
                    $row[]  =   '<strong>'.$agent_data->agent_name.'<br>'.$agent_data->agent_code.'</strong>';
                    $row[]  =   '<strong>'.$student_name.'</strong>';
                    $row[]  =   number_format($student_paid->details_amount,2); 
                    $row[]  =   number_format($fees->details_amount,2);

                    $data[] = $row; 
                }

                $output = array(
                    "draw" => isset($posts['draw'])?$posts['draw']:'',
                    "recordsTotal" => $this->fm->__get_payments_details($posts,$param,FALSE,TRUE),
                    "recordsFiltered" => $this->fm->__get_payments_details($posts,$param,FALSE,TRUE),
                    "data" => $data,
                );
                
                echo json_encode($output);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    public function onExportAdmissionExpenseReport(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $institute_data=check_institute($this->data['userdata']);
                $inst_id=$institute_data['institute_id'];

                $export_type=post_data('export_type');

                $payment_ids=$this->input->post('payment_ids');

                $data_of_session=post_data('data_of_session');
                $data_of_course=post_data('data_of_course');
                $data_of_student=post_data('data_of_student');
                $data_of_start_date=post_data('data_of_start_date');
                $data_of_end_date=post_data('data_of_end_date');

                $data_to_write=array();

                if(!empty($payment_ids)){

                    $param['inst_id']=$inst_id;
                    $param['accounting_type']='expenditure';

                    if($data_of_session>0){
                        $param['session_id']=$data_of_session;
                    }

                    if($data_of_course>0){
                        $param['course_id']=$data_of_course;
                    }

                    if($data_of_student>0){
                        $param['student_id']=$data_of_student;
                    }

                    if(!empty($data_of_start_date)){
                        $param['start_date']=date('Y-m-d',strtotime($data_of_start_date));
                    }

                    if(!empty($data_of_end_date)){
                        $param['end_date']=date('Y-m-d',strtotime($data_of_end_date));
                    }

                    //print_obj($payment_ids);die;

                    $i=1;
                    $row=2;
                    foreach ($payment_ids as $key => $value) {

                        $param['details_id']=$value;

                        $fees=$this->fm->___get_payments_details($param);

                        if(!empty($fees)){

                            $agent_data=$this->um->get_agents(array('agent_user_id'=>$fees->details_agent_id));

                            $course_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_course_id'=>$fees->details_course_id,'session_id'=>$fees->details_sess_id));

                            if(!empty($fees->stu_mid_name)){
                                $student_name=$fees->stu_first_name.' '.$fees->stu_mid_name.' '.$fees->stu_last_name;
                            }else{
                                $student_name=$fees->stu_first_name.' '.$fees->stu_last_name;
                            }

                            if($fees->details_type=='new_admission'){
                                $admission_data=$this->adm->get_admission_data(array('admission_id'=>$fees->details_type_id));
                                $admission_code=$admission_data->admission_code;                        
                            }else if($fees->details_type=='seat_booking'){
                                $seatbooking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$fees->details_type_id));
                                $admission_code=$seatbooking_data->booking_code;
                            }

                            $agent_payment=$this->fm->get_payment_details(array('details_order_code'=>$fees->details_order_code,'details_type'=>'agent_payment'));

                            if(!empty($agent_payment)){
                                $agent_paid_out=$agent_payment->details_amount;
                            }else{
                                $agent_paid_out='0';
                            }

                            $_total_expense=$agent_paid_out;

                            $data_to_write[]=array(
                                'A'.$row=>$i,
                                'B'.$row=>strtoupper($agent_data->agent_name),
                                'C'.$row=>$agent_data->agent_code,
                                'D'.$row=>$admission_code,
                                'E'.$row=>strtoupper($student_name),
                                'F'.$row=>$fees->student_code,
                                'G'.$row=>$fees->details_type_text,
                                'H'.$row=>$fees->course_code,
                                'I'.$row=>$fees->session_start_year.'-'.$fees->session_end_year,
                                'J'.$row=>number_format($course_data->session_course_fees,2),
                                'K'.$row=>number_format($fees->details_discount_amount,2),
                                'L'.$row=>number_format($fees->details_amount,2),
                                'M'.$row=>number_format($agent_paid_out,2)
                            );

                            $i++;
                            $row++;
                        }
                    }

                    //print_obj($data_to_write);die;

                    if(!empty($data_to_write)){
                        $outputFile='Admission_expense_report_'.$inst_id.'_'.date('d_m_Y');
                        $excel_return=write_excel_active_sheet($data_to_write,$outputFile,'save');
                        $return['success']=$excel_return;
                        $return['file_path']=base_url('uploads/temp_files/'.$outputFile.'.xlsx');
                    }else{
                        $return['error']='No data found to write in excel.';
                    }

                }else{
                    $return['error']='No data selected.';
                }

                json_headers($return);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }

    //Dues Reports
    public function onSearchDuesReports(){
         if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){

                $param['column_order'] = array(
                    null,
                    'details_amount',
                    'details_discount_amount'
                );

                $param['column_search'] = array('details_accounting_type');
                $param['order'] = array('details_id' => 'ASC');
                $posts=$this->input->post();


                $param['inst_id']=$this->data['userdata']->user_id;

                $param['accounting_type']='income';


                $list = $this->fm->__get_payments_details($posts,$param,FALSE,FALSE);

                //print_obj($list);die;
                
                
                $data = array();
                $no = isset($posts['start'])?$posts['start']:0;

                $action='';

                foreach ($list as $fees){
                    $no++;

                    $row = array();

                    $agent_payment=$this->fm->get_payment_details(array('details_order_code'=>$fees->details_order_code,'details_type'=>'agent_payment'));

                    if(!empty($agent_payment)){
                        $agent_paid_out=$agent_payment->details_amount;
                    }else{
                        $agent_paid_out='0';
                    }

                    $total_income=$fees->details_amount-($fees->details_discount_amount+$agent_paid_out);

                    $row[]  =   $no;
                    $row[]  =   number_format($fees->details_amount,2); 
                    $row[]  =   number_format($fees->details_discount_amount,2);
                    $row[]  =   number_format($agent_paid_out,2);

                    $row[]  =   number_format($total_income,2);
                    
                    $row[]  =   '<button type="button" class="btn btn-xs btn-danger btn_delete_fees" data-fees=""><i class="fa fa-trash"></i></button>';

                    $data[] = $row; 
                }

                $output = array(
                    "draw" => isset($posts['draw'])?$posts['draw']:'',
                    "recordsTotal" => $this->fm->_get_payments_details($posts,$param,TRUE),
                    "recordsFiltered" => $this->fm->_get_payments_details($posts,$param,TRUE),
                    "data" => $data,
                );
                
                echo json_encode($output);

            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    /***Generate Consolidated Admission Payment Report */
    public function onConsolidatedAdmissionPaymentReport() {
        if (session_userdata('isAdminLoggedin') == TRUE && session_userdata('admin_id')) {
            $session_id = $this->input->get('session_id'); // Use GET to pass parameters for direct access
            $course_id = $this->input->get('course_id');
            $admission_data = [];
    
            $admissions = $this->adm->get_admission_data(null, FALSE);
    
            $total_payment_paid = 0;
            $total_payment_payable = 0;
            $total_payment_balance = 0;
    
            if (!empty($admissions)) {
                foreach ($admissions as $adm) {
                    $late_fees_total = $this->fm->get_late_fees_total([
                        'late_fee_inst_id' => $adm->admission_inst_id,
                        'late_fee_stu_id' => $adm->admission_stu_id,
                        'late_fee_admission_id' => $adm->admission_id,
                        'late_fee_session_id' => $adm->admission_sess_id,
                        'late_fee_course_id' => $adm->admission_course_id
                    ]);
    
                    $late_fee = !empty($late_fees_total) ? $late_fees_total : 0;
    
                    $discount_fees_total = $this->fm->get_discount_fees_total([
                        'discount_fee_inst_id' => $adm->admission_inst_id,
                        'discount_fee_stu_id' => $adm->admission_stu_id,
                        'discount_fee_admission_id' => $adm->admission_id,
                        'discount_fee_session_id' => $adm->admission_sess_id,
                        'discount_fee_course_id' => $adm->admission_course_id
                    ]);
    
                    $discount_fee = !empty($discount_fees_total) ? $discount_fees_total : 0;
    
                    $stu_name = ($adm->stu_mid_name != '') 
                        ? $adm->stu_first_name . ' ' . $adm->stu_mid_name . ' ' . $adm->stu_last_name 
                        : $adm->stu_first_name . ' ' . $adm->stu_last_name;
    
                    $student_course_data = $this->sessm->get_session_course_student_wise([
                        'session_id' => $adm->admission_sess_id,
                        'session_inst_id' => $adm->admission_inst_id,
                        'session_course_id' => $adm->admission_course_id,
                        'session_stu_id' => $adm->admission_stu_id
                    ]);
    
                    $course_fees = $student_course_data->session_course_fees;
                    $total_payment = $this->fm->__get_fees_details_total([
                        'details_stu_id' => $adm->admission_stu_id,
                        'details_inst_id' => $adm->admission_inst_id,
                        'details_sess_id' => $adm->admission_sess_id,
                        'details_course_id' => $adm->admission_course_id
                    ], 'details_amount');
    
                    $total_paid = $total_payment[0]->total;
                    $total_payable = ($course_fees - $discount_fee) + $late_fee;
                    $balance = ($total_payable - $total_paid);
    
                    $total_payment_paid += $total_paid;
                    $total_payment_payable += $total_payable;
                    $total_payment_balance += $balance;
    
                    $course_name = ($student_course_data->is_lateral == 'yes') ? $adm->course_name . '(L)' : $adm->course_name;
    
                    $admission_data[] = [
                        'stu_code' => $adm->stu_user_code,
                        'admission_code' => $adm->admission_code,
                        'stu_name' => $stu_name,
                        'course' => $course_name,
                        'session' => $adm->session_start_year . '-' . $adm->session_end_year,
                        'course_fees' => $course_fees,
                        'discount' => $discount_fees_total,
                        'late_fee' => $late_fees_total,
                        'total_paid' => $total_paid,
                        'total_payable' => $total_payable,
                        'balance_payment' => $balance,
                        'last_pay_date' => ''
                    ];
                }
            }
    
            $mpdf = new \Mpdf\Mpdf([
                'format' => 'A4-L',
                'margin_top' => 15,
                'margin_bottom' => 15,
                'margin_left' => 10,
                'margin_right' => 10,
                'default_font_size' => 10
            ]);
    
            $html = $this->theme->view(
                '_pages/reports/vw_reports_consolidated_admission_payment_dyna',
                [
                    'inst_name' => $this->inst_name,
                    'admission_data' => $admission_data,
                    'total_payment_paid' => $total_payment_paid,
                    'total_payment_payable' => $total_payment_payable,
                    'total_payment_balance' => $total_payment_balance
                ],
                true
            );
    
            $mpdf->SetHeader($this->inst_name . '|Consolidated Admission Payment Report|{PAGENO}');
            $mpdf->SetFooter('Generated on {DATE j-m-Y}||Page {PAGENO} of {nbpg}');
            $mpdf->WriteHTML($html);
    
            $mpdf->Output('Consolidated_Admission_Payment_Report.pdf', 'D');
        } else {
            redirect($this->data['base_url']);
        }
    }
    
    

}

MMCT - 2023