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/Payments.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * 
 */
class Payments  extends BaseAdminController
{
    private $inst_id='';
    private $inst_type='';
    private $inst_data='';
    private $inst_phonepay_txn_required='';


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

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

        //print_obj($institute_data);die;

        $this->user_type=$this->data['userdata']->user_type;

        $this->inst_id=$institute_data['institute_id'];
        $this->inst_parent_id=$institute_data['inst_parent_id'];
        $this->branch_parent_inst_id=$this->data['userdata']->branch_parent_inst_id;
        $this->inst_ph=$this->data['userdata']->inst_ph;
        $this->inst_email=$this->data['userdata']->inst_email;
        $this->user_id=$this->data['userdata']->user_id;
        $this->user_type=$this->data['userdata']->user_type;
        $this->inst_code=$institute_data['inst_code'];
        $this->inst_type=$institute_data['inst_type'];
        $this->admission_view_access=$institute_data['admission_view_access'];
        $this->admission_add_access=$institute_data['admission_add_access'];
        $this->student_upload_limit=$institute_data['student_upload_limit'];
        $this->student_data_can_be_uploaded=$institute_data['student_data_can_be_uploaded'];
        $this->course_fees_edit_access=$institute_data['course_fees_edit'];
        $this->course_booking_fees_edit_access=$institute_data['course_booking_fees_edit'];
        $this->inst_in_bill_print=$institute_data['inst_in_bill_print'];
        $this->inst_phonepay_txn_required=$institute_data['inst_phonepay_txn_required'];

        $this->inst_data=$institute_data;
    }

    function indexAdmission(){
       


        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']='Admission Payments Dashboard';

            $current_year=date('Y');

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

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

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

            $agent_payment=$this->fm->_get_payments_total(array('details_type'=>'agent_payment','details_payment_type'=>'admission_agent_fees','details_inst_id'=>$this->inst_id));

            $_agent_payment=(!empty($agent_payment) && isset($agent_payment[0]->total))?$agent_payment[0]->total:0;


            //$total_new_admission_payment=$this->fm->_get_payments_in_total(array('details_inst_id'=>$this->data['userdata']->user_id),'details_type',"'new_admission','seat_booking'");

            $total_new_admission_payment=$this->fm->_get_payments_in_total(array('details_inst_id'=>$this->inst_id),'details_type',"'new_admission'");
            $seat_booking_payment_detail=$this->adm->get_seat_booking_payment_total(array('booking_inst_id'=>$this->inst_id));

            $_total_new_admission_payment=(!empty($total_new_admission_payment) && isset($total_new_admission_payment[0]->total))?$total_new_admission_payment[0]->total:0;

            // if(!empty($seat_booking_payment_detail)){
            //     $_total_new_admission_payment=$_total_new_admission_payment+$seat_booking_payment_detail[0]->total;
            // }

            //print_obj($_total_new_admission_payment);die;

            $income=($_total_new_admission_payment>0)?($_total_new_admission_payment-$_agent_payment):$_total_new_admission_payment;

            $this->data['total_new_admission_payment']=(!empty($_total_new_admission_payment))?number_format($_total_new_admission_payment,2):'0.00';


            $this->data['total_agent_payment']=(!empty($_agent_payment))?number_format($_agent_payment,2):'0.00';
            $this->data['total_income']=(!empty($income))?number_format($income,2):'0.00';

            $this->theme->title($this->data['page_title'])->load('payments/vw_payment_admission', $this->data);
        }else{
            redirect($this->data['base_url']);
        }
    }

    function indexAdmissionBalance(){
        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']='Admission Payments Banalace';
            $this->data['in_bill_print']=$this->inst_in_bill_print;
            $this->data['sessions']=$this->sessm->get_session(array('session_inst_id'=>$this->inst_id,'session_status'=>'active'),FALSE);


            $this->theme->title($this->data['page_title'])->load('payments/vw_payment_balance_admission', $this->data);

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


    public function onSearchAdmissionPayments(){
        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,
                    'admission_code'
                );

                $param['inst_id']=$this->inst_id;
                $param['accounting_type']='income';
                $param['details_type']='new_admission';

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

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

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

                $action='';

                $agent_data=array();

                //print_obj($list);die;

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

                    $row = array();

                    $total_agent_payment=0;

                    $admission_data=$this->adm->get_admission_data(array('admission_code'=>$payment->admission_code));

                    $course_data=$this->cm->get_course(array('course_id'=>$payment->details_course_id));

                    $student_data=$this->um->get_user(array('user_id'=>$payment->details_stu_id),'student');

                    $session_course_data=$this->sessm->get_session_course(array('session_id'=>$payment->details_sess_id,'session_inst_id'=>$payment->details_inst_id,'session_course_id'=>$payment->details_course_id));

                    $session_course_student_data=$this->sessm->get_session_course_student_wise(array('session_id'=>$payment->details_sess_id,'session_inst_id'=>$payment->details_inst_id,'session_course_id'=>$payment->details_course_id,'session_stu_id'=>$payment->details_stu_id));                      

                    if(!empty($session_course_student_data)){
                        $course_fees=$session_course_student_data->session_course_fees;
                    }else{
                        if(!empty($session_course_data)){
                            $course_fees=$session_course_data->session_course_fees;
                        }else{
                            $course_fees=0;
                        }
                    }

                    $seat_booking_payment_detail=$this->adm->get_seat_booking_payment_data(array('booking_inst_id'=>$payment->details_inst_id,'booking_stu_id'=>$payment->details_stu_id,'booking_course_id'=>$payment->details_course_id,'booking_sess_id'=>$payment->details_sess_id));

                    $paid_amount=$payment->details_amount;

                    if($course_fees>$payment->details_amount){
                        if(!empty($seat_booking_payment_detail)){
                            $paid_amount=$paid_amount+$seat_booking_payment_detail->booking_payment;
                            $due_amount=$course_fees-$paid_amount;
                        }else{
                            $due_amount=$course_fees-$paid_amount;
                        }
                        
                    }else{
                        $due_amount=0;
                    }



                    //print_obj($student_data);die;

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

                    

                    //$agent_payment=$this->fm->_get_payments_total(array('details_type'=>'agent_payment','details_inst_id'=>$this->data['userdata']->user_id,'details_accounting_type'=>'expenditure'));

                    //$total_agent_payment=(!empty($agent_payment))?number_format($agent_payment[0]->total,2):'0.00';

                    if(!empty($admission_data) && $admission_data->admission_agent_id>0){
                        $agent_data=$this->um->get_user(array('agent_id'=>$admission_data->admission_agent_id),'agent');

                        $payment_booking_data=$this->fm->get_agent_payment(array('agent_id'=>$agent_data->agent_user_id,'booking_id'=>$admission_data->admission_id,'booking_type'=>'admission'));
                    }else{
                        $agent_data=array();
                    }

                    

                    $total_agent_payment=0;

                    

                    $admission_session=$payment->session_start_year.'-'.$payment->session_end_year;

                    $agent_id=!empty($agent_data)?$agent_data->agent_user_id:'';

                    if($due_amount>0){
                        $label='danger';
                    }else{
                        $label='success';
                    }

                    $row[]  =   $no;
                    $row[]  =   '<strong>'.$payment->admission_code.'</strong>';
                    $row[]  =   '<strong>'.$admission_session.'</strong>';
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($course_fees,2).'</span>';
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($paid_amount,2).'</span>';
                    $row[]  =   '<span class="label label-'.$label.' "><i class="fa fa-inr"></i> '.number_format($due_amount,2).'</span>';

                    $total_income=(!empty($payment))?number_format($paid_amount,2):'0.00';

                    if(!empty($agent_data)){
                        $agent='<a href="'.$this->data['base_url'].'/payments/agents/'.encode_data($agent_data->agent_user_id).'" target="_blank">'.$agent_data->agent_name.'</a>';

                        if(!empty($payment_booking_data)){
                            $agent_payment=(!empty($payment_booking_data))?number_format($payment_booking_data->amount_value,2):'0.00';
                            $agent_paid_payment=$this->fm->get_agent_payment(array('booking_type'=>'admission','stu_id'=>$admission_data->admission_stu_id,'inst_id'=>$inst_id,'agent_id'=>$agent_id));

                            $income=(!empty($agent_paid_payment) && $agent_paid_payment->amount_paid=='yes')?($paid_amount-$payment_booking_data->amount_value):$paid_amount;

                            $label=(!empty($agent_paid_payment) && $agent_paid_payment->amount_paid=='yes')?'label label-danger':'label label-success';

                            $total_income=(!empty($income))?number_format($income,2):'0.00';
                            $agent.='<br><span class="'.$label.'"><i class="fa fa-inr"></i> '.$agent_payment.'</span>';
                        }else{
                            $agent_commision_preset=$this->sm->get_agent_commission_settings(array('com_inst_id'=>$admission_data->admission_inst_id,'com_session_id'=>$admission_data->admission_sess_id,'com_course_id'=>$admission_data->admission_course_id));

                            $agent_commission_settings=$agent_commision_preset->com_max_value;

                            $agent_commission_text='Agent maximum commission for '.$course_data->course_code.'/'.$admission_session.' is ₹ '.number_format($agent_commision_preset->com_max_value,2);

                            $agent.='<br><button type="button" class="btn btn-xs btn-dark btn_set_agent_payment" data-admission_code="'.$admission_data->admission_code.'" data-admission_id="'.encode_data($admission_data->admission_id).'" data-admission_amount="'.$payment->details_amount.'"
                            data-admission_session="'.$admission_session.'" data-admission_course="'.$course_data->course_name.'" data-agent_name="'.$agent_data->agent_name.'" data-agent_id="'.encode_data($admission_data->admission_agent_id).'" data-admission_student="'.$student_name.'" data-agent_max_commission="'.$agent_commission_settings.'" data-agent_commission_text="'.$agent_commission_text.'" data-toggle="modal" data-target="#paySetAgentModal">Set Payment</button>';
                        }

                        
                    }else{
                        $agent='N/A';
                    }

                    $row[]  =  $agent; 
                   // $row[]  =   '<span class="label label-danger "><i class="fa fa-inr"></i> '.$total_agent_payment.'</span>';
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.$total_income.'</span>';
                    

                    if($no=='1'){
                        $aria='true';
                    }else{
                        $aria='false';
                    }

                    $row[]  =  '<a href="'.$this->data['base_url'].'/admission/payments/'.encode_data($payment->admission_id).'" class="btn btn-xs btn-dark">View Details</a>';
                    //' <button class="toggleButton">Toggle</button>';
                                       

                    // Inside the foreach loop
                    //$row[] = '<tr class="hidden-row"><td colspan="5">Hidden Row' . $no . '</td></tr>';


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

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

                $installments=[];

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

                $param['column_search'] = array('course_name','stu_first_name','stu_mid_name','stu_last_name','stu_user_code');
                $param['order'] = array('admission_id' => 'DESC');
                $posts=$this->input->post();

                if($posts['data_of']=='own'){
                  $param['inst_id']=$this->inst_id;  
                }else{
                  $param['inst_id']=$posts['data_of_id'];
                }

                $list = $this->adm->_get_admissions_data($posts,$param,FALSE,FALSE); 

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

                $action='';

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

                    $row = array();

                    $row[]  =   $no;

                    if(isset($stu->stu_user_id) && !empty($stu->stu_user_id)){
                        $installments=$this->fm->__get_installments_total($stu->stu_user_id);

                        if($stu->stu_mid_name!=''){
                            $stu_name=$stu->stu_first_name.' '.$stu->stu_mid_name.' '.$stu->stu_last_name;
                        }else{
                            $stu_name=$stu->stu_first_name.' '.$stu->stu_last_name;
                        }
    
                        // $stu_image=$this->sm->get_file(array('storage_type'=>'student_profile_photo','storage_type_id'=>$stu->stu_user_id,'storage_parent_id'=>$stu->stu_inst_id));
    
    
                        // if(!empty($stu_image) && !empty($stu_image->media_disk_path)){
                        //     if(file_exists_any_separator(FCPATH.$stu_image->media_disk_path)){
                        //         $image=base_url($stu_image->media_disk_path_relative);
                        //     }else{
                        //         $image=$this->data['no_image'];
                        //     }
                        // }else{
                        //     $image=$this->data['no_image'];
                        // }
                    }

                    if (!empty($installments)) {
                        $i_table[$stu->stu_user_id] = '<table style="width:100%;">';
                        $i_table[$stu->stu_user_id] .= '<thead><th>#</th><th>Amount</th><th>Discount</th><th>Due Date</th><th>Paid</th><th>Due</th><th></th></thead>';
                        $i_table[$stu->stu_user_id] .= '<tbody>';
                        $total_installments=0;
                        $total_paid=0;
                        $total_due=0;
                        $total_dues=0;
                        $total_discount=0;
                        $installment_no=1;
                        foreach ($installments as $key => $value) {
                            $total_discount_data=$this->fm->__get_fees_details_total(array('details_type'=>'new_admission','details_stu_id'=>$stu->stu_user_id,'details_installment_id'=>$value->installment_id,'details_course_id'=>$value->installment_course_id,'details_sess_id'=>$value->installment_sess_id,'details_inst_id'=>$value->installment_inst_id),'details_discount_amount');
                            $discount=$total_discount_data[0]->total;
                            $total_installments+=$value->installment_value;                            
                            $total_paid+=$value->total_paid;
                            $total_discount+=$discount;
                            $total_due+=$value->total_due;

                            if($stu->is_old_data=='yes'){
                                if($value->installment_date!='0000-00-00'){
                                    $due_date=date('d-m-Y',strtotime($value->installment_date));
                                }else{
                                    $due_date='N/A';
                                }
                            }else{
                                if($value->installment_date!='0000-00-00'){
                                    $due_date=date('d-m-Y',strtotime($value->installment_date));
                                }else{
                                    $due_date='N/A';
                                }
                            }
                            
                            $i_table[$stu->stu_user_id] .= '<tr>
                                <td>' . $installment_no . '</td>
                                <td>' . number_format(floatval($value->installment_value),2) . '</td>
                                <td>' . number_format(floatval($discount),2) . '</td>
                                <td>'.$due_date.'</td>
                                <td>' . number_format(floatval($value->total_paid),2) . '</td>
                                <td>' . number_format(floatval($value->total_due-$discount),2) . '</td>
                                <td></td>
                            </tr>';
                            $installment_no++;
                        }

                        $total_dues=$total_due-$total_discount;

                        if($total_dues>0){
                            $btn_text='Receive';
                        }else{
                            $btn_text='Details';
                        }
                        $i_table[$stu->stu_user_id] .= '<tr>
                                <td><b>Total</b></td>
                                <td><b>₹ ' . number_format(floatval($total_installments),2) . '</b></td>
                                <td><b>₹ ' . number_format(floatval($total_discount),2) . '</b></td>
                                <td></td>
                                <td style="color:green;"><b>₹ ' . number_format(floatval($total_paid),2) . '</b></td>
                                <td style="color:red;"><b>₹ ' . number_format(floatval($total_dues),2) . '</b></td>
                                <td><a href="'.$this->data['base_url'].'/admission/payments/'.encode_data($stu->admission_id).'" target="__blank" class="btn btn-sm btn-dark">'.$btn_text.'</a></td>';
                        
                                
                        $i_table[$stu->stu_user_id] .= '</tr>';
                        $i_table[$stu->stu_user_id] .= '</tbody>';
                        $i_table[$stu->stu_user_id] .= '</table>'; // Close the table
                    } else {

                        $i_table[$stu->stu_user_id] = '<table style="width:100%;"><tbody><tr><td colspan="4">Installments not created yet</td></tr></tbody></table>'; // Set an empty string if no installments
                    }  


                    // $row[]  ='<div class="container">
                    //             <img class="img-thumbnail" src="'.$image.'" height="30px;" width="30px;">
                    //             <div>
                    //                 <p>'.$stu_name.'</p>
                    //                 <p>'.$stu->stu_user_code.'</p>
                    //                 <p>'.$stu->stu_ph_no.'</p>
                    //                 <p>'.$stu->stu_email.'</p>
                    //             </div>
                    //         </div>';

                    $row[]  ='<div class="container">
                                <div>
                                    <p>'.$stu->stu_user_code.'</p>
                                    <p>'.$stu_name.'</p>                                    
                                    <p>'.$stu->stu_ph_no.'</p>
                                    <p>'.$stu->stu_email.'</p>
                                </div>
                            </div>';

                    $t='';


                    $row[] = $i_table[$stu->stu_user_id];

  

                    $data[] = $row; 
                }

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

            }else{

            }
        }else{

        }
    }

    public function onSearchAdmissionBalancePayments_backup_2(){
        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']);

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

                $param['column_search'] = array('stu_first_name','stu_mid_name','stu_last_name','stu_user_code','stu_ph_no','stu_email');
                $param['is_removed']='no';
                $param['order'] = array('stu_id' => 'ASC');
                $posts=$this->input->post();

                if($posts['data_of']==='own'){
                    $param['inst_id']=$this->inst_id;
                }else if($posts['data_of']==='branch'){
                    $param['inst_parent_id']=$this->inst_parent_id;
                }


                if(isset($posts['data_of_id']) && !empty($posts['data_of_id'])){
                    $param['inst_id']=$posts['data_of_id'];
                }

                if(isset($posts['stu_status']) && !empty($posts['stu_status'])){
                    $param['stu_status']=$posts['stu_status'];
                }

                $admission_data_param=array('admission_stu_id'=>$stu->stu_user_id);

                // if(isset($posts['data_of_session_id']) && !empty($posts['data_of_session_id']) && $posts['data_of_session_id']>0){
                //     $_admission_data_param=array('admission_sess_id'=>$posts['data_of_session_id']);

                //     array_push($_admission_data_param, $admission_data_param);
                // }

                $list = $this->um->_get_students($posts,$param,FALSE,FALSE);        
                
                $data = array();
                $no = isset($posts['start'])?$posts['start']:0;

                $action='';

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

                    $row = array();

                    $row[]  =   $no;

                    $admission_data=$this->adm->_get_admission_data($admission_data_param);


                    $installments=$this->fm->__get_installments_total($stu->stu_user_id);

                    // print_obj($installments);die;


                    if($stu->stu_mid_name!=''){
                        $stu_name=$stu->stu_first_name.' '.$stu->stu_mid_name.' '.$stu->stu_last_name;
                    }else{
                        $stu_name=$stu->stu_first_name.' '.$stu->stu_last_name;
                    }

                    $stu_image=$this->sm->get_file(array('storage_type'=>'student_profile_photo','storage_type_id'=>$stu->stu_user_id,'storage_parent_id'=>$stu->stu_inst_id));


                    if(!empty($stu_image) && !empty($stu_image->media_disk_path)){
                        if(file_exists_any_separator(FCPATH.$stu_image->media_disk_path)){
                            $image=base_url($stu_image->media_disk_path_relative);
                        }else{
                            $image=$this->data['no_image'];
                        }
                    }else{
                        $image=$this->data['no_image'];
                    }

                    if (!empty($installments)) {
                        $i_table[$stu->stu_user_id] = '<table style="width:100%;">';
                        $i_table[$stu->stu_user_id] .= '<thead><th>#</th><th>Installment</th><th>Due Date</th><th>Paid</th><th>Due</th><th></th></thead>';
                        $i_table[$stu->stu_user_id] .= '<tbody>';
                        $total_installments=0;
                        $total_paid=0;
                        $total_due=0;
                        $installment_no=1;
                        foreach ($installments as $key => $value) {
                            $total_installments+=$value->installment_value;
                            $total_paid+=$value->total_paid;
                            $total_due+=$value->total_due;
                            $i_table[$stu->stu_user_id] .= '<tr>
                                <td>' . $installment_no . '</td>
                                <td>' . number_format(floatval($value->installment_value),2) . '</td>
                                <td>'.date('d-m-Y',strtotime($value->installment_date)).'</td>
                                <td>' . number_format(floatval($value->total_paid),2) . '</td>
                                <td>' . number_format(floatval($value->total_due),2) . '</td>
                                <td></td>
                            </tr>';
                            $installment_no++;
                        }
                        $i_table[$stu->stu_user_id] .= '<tr>
                                <td><b>Total</b></td>
                                <td><b>' . number_format(floatval($total_installments),2) . '</b></td>
                                <td></td>
                                <td style="color:green;"><b>' . number_format(floatval($total_paid),2) . '</b></td>
                                <td style="color:red;"><b>' . number_format(floatval($total_due),2) . '</b></td>
                                <td><a href="'.$this->data['base_url'].'/admission/payments/'.encode_data($admission_data->admission_id).'" target="__blank" class="btn btn-sm btn-dark">Receive</a></td>';
                        
                                
                        $i_table[$stu->stu_user_id] .= '</tr>';
                        $i_table[$stu->stu_user_id] .= '</tbody>';
                        $i_table[$stu->stu_user_id] .= '</table>'; // Close the table
                    } else {

                        $i_table[$stu->stu_user_id] = '<table style="width:100%;"><tbody><tr><td colspan="4">Installments not created yet</td></tr></tbody></table>'; // Set an empty string if no installments
                    }  


                    $row[]  ='<div class="container">
                                <img class="img-thumbnail" src="'.$image.'" height="30px;" width="30px;">
                                <div>
                                    <p>'.$stu_name.'</p>
                                    <p>'.$stu->stu_user_code.'</p>
                                    <p>'.$stu->stu_ph_no.'</p>
                                    <p>'.$stu->stu_email.'</p>
                                </div>
                            </div>';

                    $t='';


                    $row[] = $i_table[$stu->stu_user_id];

  

                    $data[] = $row; 
                }

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



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

    public function onSearchAdmissionBalancePayments_backup(){
         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,
                    'admission_code'
                );

                $param['column_search'] = array('course_name','stu_first_name','stu_mid_name','stu_last_name','stu_user_code');
                $param['order'] = array('balance_id' => 'DESC');
                $posts=$this->input->post();

                if($posts['data_of']=='own'){
                  $param['inst_id']=$this->inst_id;  
                }else{
                  $param['inst_id']=$posts['data_of_id'];
                }

                //$_total_payable=$this->fm->_get_installments_total(array('installment_inst_id'=>$inst_id));

                $total_balance_paid_param=array('inst_id'=>$this->inst_id);
                $total_balance_param=array('inst_id'=>$this->inst_id,'inst_balance_paid'=>'no');

                if(!empty($posts['data_of_session_id']) && $posts['data_of_session_id']>0){
                    $param['sess_id']=$posts['data_of_session_id'];
                    $total_balance_paid_param=array('inst_session_id'=>$posts['data_of_session_id'],'inst_id'=>$this->inst_id);
                    $total_balance_param=array('inst_session_id'=>$posts['data_of_session_id'],'inst_id'=>$this->inst_id,'inst_balance_paid'=>'no');
                }

                if(!empty($posts['data_of_course_id']) && $posts['data_of_course_id']>0){
                    $param['course_id']=$posts['data_of_course_id'];
                    $total_balance_paid_param=array('inst_session_id'=>$posts['data_of_session_id'],'inst_course_id'=>$posts['data_of_course_id'],'inst_id'=>$this->inst_id);
                    $total_balance_param=array('inst_session_id'=>$posts['data_of_session_id'],'inst_course_id'=>$posts['data_of_course_id'],'inst_id'=>$this->inst_id,'inst_balance_paid'=>'no');
                }

                if(!empty($posts['data_of_pay_status']) && $posts['data_of_pay_status']!='0'){
                    $param['paid_status']=$posts['data_of_pay_status'];
                }

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

                //$total_balance=$this->fm->get_installments_paid_total();

                $_total_paid=$this->fm->get_installments_balance_total($total_balance_paid_param);

                $_total_balance=$this->fm->get_installments_balance_total($total_balance_param);

                $total_balance=(!empty($_total_balance) && !empty($_total_balance[0]->total))?$_total_balance[0]->total:0;
                $total_paid=(!empty($_total_paid) && !empty($_total_paid[0]->total))?$_total_paid[0]->total:0;

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

                $action='';

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

                    $row = array();

                    $payment_details=$this->fm->get_fees_details(array('payment_fees_details_id'=>$payment->inst_payment_id,'payment_installment_id'=>$payment->inst_installment_id,'payment_inst_id'=>$payment->inst_id,'payment_course_id'=>$payment->inst_course_id,'payment_session_id'=>$payment->inst_session_id));

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

                    if($payment->inst_balance_paid=='yes'){
                        $action='<span class="btn btn-xs btn-success">Paid</span>';                        
                    }else if($payment->inst_balance_paid=='no'){
                        $action='<a href="'.$this->data['base_url'].'/admission/payments/'.encode_data($payment_details->payment_admission_id).'" class="btn btn-xs btn-dark">Recieve Now</a>';
                    }                    


                    $row[]  =   $no;
                    $row[]  =   'Session:'.$payment->session_start_year.'-'.$payment->session_end_year.'<br>Course:'.$payment->course_name;
                    $row[]  =   $student_name.'<br>Code:'.$payment->stu_user_code;
                    $row[]  =   $payment_details->payment_fees_total;
                    $row[]  =   $payment_details->payment_fees_admission_amount.'<br>Date:'.date('d-m-Y',strtotime($payment_details->created_at));
                    $row[]  =   number_format($payment->inst_payment_balance,2);
                    $row[]  =   $action;                                      

                    $data[] = $row;
                }

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

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

    //Own Seat Booking
    function indexSeatbookingPayments(){
        if($this->data['userdata']->user_type=='student'){
         redirect($this->data['base_url']);
        }

        if(session_userdata('isAdminLoggedin')){

            $this->data['page_title']='Seatbooking Payments Dashboard';

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

            $current_year=date('Y');

            $total_tobe_pay_amount=0;
            $total_agent_payment=0;
            $total_income=0;

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

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

            $this->data['total_seat_bookings']=$this->adm->get_seat_booking_count(array('booking_inst_id'=>$this->data['userdata']->user_id));

            $this->data['total_conversion']=$this->adm->get_seat_booking_count(array('booking_inst_id'=>$this->data['userdata']->user_id,'booking_converted'=>'yes'));

            $total_booking_payments=$this->adm->get_seat_booking_payment_total(array('booking_inst_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'yes'));
            $total_booking_payments_due=$this->adm->get_seat_booking_payment_total(array('booking_inst_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'no'));

            $this->data['total_booking_payments']=(!empty($total_booking_payments) && $total_booking_payments[0]->total!=null)?number_format($total_booking_payments[0]->total,2):'0.00';
            $this->data['total_booking_payments_due']=(!empty($total_booking_payments_due) && $total_booking_payments_due[0]->total!=null)?number_format($total_booking_payments_due[0]->total,2):'0.00';

            $agent_payment=$this->fm->_get_agent_payments_total(array('booking_type'=>'seat_booking','inst_id'=>$this->data['userdata']->user_id,'amount_paid'=>'yes'));

            //print_obj($agent_payment);die;

            if(!empty($agent_payment) && $agent_payment[0]->total!=null){
                $total_agent_payment=$agent_payment[0]->total;
            }

            if(!empty($total_booking_payments) && $total_booking_payments[0]->total!=null){
                if(!empty($agent_payment) && $agent_payment[0]->total!=null){
                    $total_tobe_pay_amount=$total_booking_payments[0]->total-$agent_payment[0]->total;
                }                    
            }

            $total_income=$total_tobe_pay_amount;


            $this->data['total_tobe_pay_amount']=number_format($total_tobe_pay_amount,2);
            

            $this->data['total_agent_payment']=number_format($total_agent_payment,2);

            $this->data['total_income']=number_format($total_income,2);

            if($userdata->user_type=='institute'){
                $sessions_param=array('session_parent_inst_id'=>$userdata->user_id);
            }else if($userdata->user_type=='institute_branch'){
                $sessions_param=array('session_inst_id'=>$userdata->user_id);
            }


            $this->data['sessions']=$this->sessm->get_session($sessions_param,FALSE);


            $banks=$this->sm->get_banks(array('bank_inst_id'=>$this->data['userdata']->user_id),FALSE);

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

            $this->data['payment_mode']='indirect_payment';
            $this->data['online_payment_url']=$this->data['base_url'].'/admission_bookings_payment_initialize_inst';

            $this->theme->title($this->data['page_title'])->add_partial('partial_booking_fee_payment_inst_mode_modal',$this->data)->add_partial('partial_booking_fee_payment_inst_mode_modal',$this->data)->load('payments/vw_payment_seatbooking', $this->data);
        }else{

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


    //Branch Seat Booking
    function indexSeatbookingBranchPayments(){
       
        if(session_userdata('isAdminLoggedin')){

            $this->data['page_title']='Branch Seatbooking Payments Dashboard';

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

            $current_year=date('Y');

            $this->data['branches']=$this->um->get_institute_branch(array('branch_parent_inst_id'=>$user_id),FALSE);

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

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

            $this->data['total_seat_bookings']=$this->adm->get_seat_booking_count(array('booking_inst_parent_id'=>$user_id));

            $this->data['total_conversion']=$this->adm->get_seat_booking_count(array('booking_inst_parent_id'=>$user_id,'booking_converted'=>'yes'));

            $total_booking_payments=$this->adm->get_seat_booking_payment_total(array('booking_inst_parent_id'=>$user_id,'booking_payment_paid'=>'yes'));
            $_total_booking_payments=(!empty($total_booking_payments) && isset($total_booking_payments[0]->total))?$total_booking_payments[0]->total:0;

            $total_booking_payments_due=$this->adm->get_seat_booking_payment_total(array('booking_inst_parent_id'=>$user_id,'booking_payment_paid'=>'no'));

            $_total_booking_payments_due=(!empty($total_booking_payments_due) && isset($total_booking_payments_due[0]->total))?$total_booking_payments_due[0]->total:0;

            $this->data['total_booking_payments']=(!empty($_total_booking_payments))?number_format($_total_booking_payments,2):'0.00';
            $this->data['total_booking_payments_due']=(!empty($_total_booking_payments_due))?number_format($_total_booking_payments_due,2):'0.00';

            $agent_payment=$this->fm->_get_agent_payments_total(array('booking_type'=>'seat_booking','inst_parent_id'=>$user_id,'amount_paid'=>'yes'));

            $_agent_payment=(!empty($agent_payment) && isset($agent_payment[0]->total))?$agent_payment[0]->total:0;

            if(!empty($agent_payment)){
                $total_income=$_total_booking_payments-$_agent_payment;
            }else{
                $total_income=$_total_booking_payments;
            }
            

            $this->data['total_agent_payment']=(!empty($_agent_payment))?number_format($_agent_payment,2):'0.00';

            $this->data['total_income']=number_format($total_income,2);


            $total_tobe_pay_amount=$_total_booking_payments+$_total_booking_payments_due;


            $this->data['total_tobe_pay_amount']=number_format($total_tobe_pay_amount,2);

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

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


    public function onSearchSeatBookingsPayments(){
        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,
                    'course_name'
                );

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

                $param['column_search'] = array('course_name','booking_code');
                $param['order'] = array('booking_payment_id' => 'DESC');
                $posts=$this->input->post();

                if($posts['data_of']==='own'){
                    $param['inst_id']=$this->data['userdata']->user_id;
                }else if($posts['data_of']==='branch'){
                    $param['inst_parent_id']=$this->data['userdata']->user_id;
                }

                if(isset($posts['data_of_id']) && !empty($posts['data_of_id'])){
                    $param['inst_id']=$posts['data_of_id'];
                }

                if(isset($posts['data_of_pay_status']) && !empty($posts['data_of_pay_status'])){
                    $param['inst_payment_paid']=$posts['data_of_pay_status'];
                }

                if(isset($posts['data_of_session_id']) && !empty($posts['data_of_session_id'])){
                    $param['inst_session_id']=$posts['data_of_session_id'];
                }

                if(isset($posts['data_of_course_id']) && !empty($posts['data_of_course_id'])){
                    $param['inst_session_course_id']=$posts['data_of_course_id'];
                }

                if(isset($posts['data_payment_mode']) && !empty($posts['data_payment_mode'])){
                    $param['inst_booking_pay_mode']=$posts['data_payment_mode'];
                }


                $param['is_deleted']='no';
               

                $list = $this->adm->_get_seat_booking_payment_data($posts,$param,FALSE,FALSE);
                
     //print_obj($list);
          
// die();

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

                $action='';

                $current_year=date('Y');

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

                if($posts['data_of']==='own'){
                    if(!empty($posts['data_of_id'])){
                        $total_booking_payments_param=array('booking_inst_id'=>$posts['data_of_id'],'booking_payment_paid'=>'yes');
                        $total_booking_payments_due_param=array('booking_inst_id'=>$posts['data_of_id'],'booking_payment_paid'=>'no');
                        $total_admissions_param=array('YEAR(created_at)'=>$current_year,'admission_inst_id'=>$posts['data_of_id']);
                        $total_seat_bookings_param=array('booking_inst_parent_id'=>$posts['data_of_id']);
                        $total_conversion_param=array('booking_inst_parent_id'=>$posts['data_of_id'],'booking_converted'=>'yes');
                    }else{
                        $total_booking_payments_param=array('booking_inst_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'yes');
                        $total_booking_payments_due_param=array('booking_inst_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'no');
                        $total_admissions_param=array('YEAR(created_at)'=>$current_year,'admission_inst_id'=>$this->data['userdata']->user_id);
                        $total_seat_bookings_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id);
                        $total_conversion_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_converted'=>'yes');
                    }
                        
                }else if($posts['data_of']==='branch'){
                    if(!empty($posts['data_of_id'])){
                        $total_booking_payments_param=array('booking_inst_id'=>$posts['data_of_id'],'booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'yes');
                        $total_booking_payments_due_param=array('booking_inst_id'=>$posts['data_of_id'],'booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'no');
                        $total_admissions_param=array('YEAR(created_at)'=>$current_year,'admission_inst_parent_id'=>$this->data['userdata']->user_id,'admission_inst_id'=>$posts['data_of_id']);
                        $total_seat_bookings_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_inst_id'=>$posts['data_of_id']);
                        $total_conversion_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_inst_id'=>$posts['data_of_id'],'booking_converted'=>'yes');
                    }else if(empty($posts['data_of_id'])){
                        $total_booking_payments_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'yes');
                        $total_booking_payments_due_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_payment_paid'=>'no');
                        $total_admissions_param=array('YEAR(created_at)'=>$current_year,'admission_inst_parent_id'=>$this->data['userdata']->user_id);
                        $total_seat_bookings_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id);
                        $total_conversion_param=array('booking_inst_parent_id'=>$this->data['userdata']->user_id,'booking_converted'=>'yes');
                    }                    
                }

                if(isset($posts['data_of_session_id']) && !empty($posts['data_of_session_id'])){  
                    $booking_sess_id['booking_sess_id']=$posts['data_of_session_id'];
                    $total_booking_payments_param+=$booking_sess_id;
                    $total_booking_payments_due_param+=$booking_sess_id;
                    $admission_sess_id['admission_sess_id']=$posts['data_of_session_id'];
                    $total_admissions_param+=$admission_sess_id;
                    $total_seat_bookings_param+=$booking_sess_id;
                    $total_conversion_param+=$booking_sess_id;
                }

                if(isset($posts['data_of_course_id']) && !empty($posts['data_of_course_id'])){
                    $booking_course_id['booking_course_id']=$posts['data_of_course_id'];
                    $total_booking_payments_param+=$booking_course_id;
                    $total_booking_payments_due_param+=$booking_course_id;
                    $admission_course_id['admission_course_id']=$posts['data_of_course_id'];
                    $total_admissions_param+=$admission_course_id;
                    $total_seat_bookings_param+=$booking_course_id;
                    $total_conversion_param+=$booking_course_id;
                }

                $total_admissions=$this->adm->get_total_admissions($total_admissions_param);

                $total_seat_bookings=$this->adm->get_seat_booking_count($total_seat_bookings_param);  

                $total_conversion=$this->adm->get_seat_booking_count($total_conversion_param);    

                $total_booking_payments=$this->adm->get_seat_booking_payment_total($total_booking_payments_param,FALSE);

                //print_obj($total_booking_payments);die;

                $total_booking_payments_due=$this->adm->get_seat_booking_payment_total($total_booking_payments_due_param);

                $total_tobe_pay_amount=0;

                if(!empty($total_booking_payments) && $total_booking_payments[0]->total!=null){
                    if(!empty($total_booking_payments_due) && $total_booking_payments_due[0]->total!=null){
                        $total_tobe_pay_amount=$total_booking_payments[0]->total+$total_booking_payments_due[0]->total;
                    }                    
                }
                

                if(!empty($posts['data_of_id']) && ($posts['data_of']==='own')){
                    $agent_payment_param=array('booking_type'=>'seat_booking','inst_id'=>$posts['data_of_id'],'amount_paid'=>'yes');
                }else if(!empty($posts['data_of_id']) && ($posts['data_of']==='branch')){
                    $agent_payment_param=array('booking_type'=>'seat_booking','inst_id'=>$posts['data_of_id'],'inst_parent_id'=>$this->data['userdata']->user_id,'amount_paid'=>'yes');
                }
                else if(empty($posts['data_of_id']) && $posts['data_of']=='own'){
                    $agent_payment_param=array('booking_type'=>'seat_booking','inst_id'=>$this->data['userdata']->user_id,'amount_paid'=>'yes');
                }else if(empty($posts['data_of_id']) && ($posts['data_of']==='branch')){
                    $agent_payment_param=array('booking_type'=>'seat_booking','inst_parent_id'=>$this->data['userdata']->user_id,'amount_paid'=>'yes');
                }
                else if(empty($posts['data_of_id']) && empty($posts['data_of'])){
                    $agent_payment_param=array('booking_type'=>'seat_booking','inst_id'=>$this->data['userdata']->user_id,'amount_paid'=>'yes');
                }

                if(isset($posts['data_of_session_id']) && !empty($posts['data_of_session_id'])){ 
                    $agent_payment_param['session_id']=$posts['data_of_session_id'];
                }

                if(isset($posts['data_of_course_id']) && !empty($posts['data_of_course_id'])){
                    $agent_payment_param['course_id']=$posts['data_of_course_id'];
                }

                $_agent_payment=$this->fm->get_agent_payments_total($agent_payment_param,FALSE);

                //print_obj($_agent_payment);die;

                if(!empty($total_booking_payments) && $total_booking_payments[0]->total!=null){
                    if(!empty($_agent_payment) && $_agent_payment[0]->total!=null){
                        $total_income=$total_booking_payments[0]->total-$_agent_payment[0]->total;
                    }else{
                        $total_income=$total_booking_payments[0]->total;
                    }
                }else{
                    $total_income=$total_booking_payments[0]->total;
                }

                //echo $total_income;die;

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

                    $row = array();

                    $course_data=$this->cm->get_course(array('course_id'=>$payment->booking_course_id));

                    $agent_payment=$this->fm->get_agent_payment(array('booking_id'=>$payment->booking_id,'booking_type'=>'seat_booking','inst_id'=>$this->data['userdata']->user_id));

                    if(!empty($agent_payment)){
                        if($agent_payment->amount_paid=='yes'){
                            $income=$payment->booking_payment-$agent_payment->amount_value;
                        }else{
                            $income=$payment->booking_payment;
                        }
                        
                    }else{
                        $income=$payment->booking_payment;
                    }


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

                    // if(!empty($payment->stu_ph_no)){
                    //     $student_name.='<br><b>Phone No</b>:'.$payment->stu_ph_no;
                    // }

                    // if(!empty($payment->stu_whatsapp_no)){
                    //     $student_name.='<br><b>Whatsapp</b>:'.$payment->stu_whatsapp_no;
                    // }


                    // if(!empty($payment->stu_email)){
                    //     $student_name.='<br><b>Email</b>:'.$payment->stu_email;
                    // }

                    $stu_image=$this->sm->get_file(array('storage_type'=>'student_profile_photo','storage_type_id'=>$payment->stu_user_id,'storage_parent_id'=>$payment->stu_inst_id));

                    if(!empty($stu_image) && is_file(FCPATH.$stu_image->media_disk_path)){
                        $image=$this->data['base_url'].$stu_image->media_disk_path;
                    }else{
                        $image=$this->data['no_image'];
                    }

                    if($payment->booking_converted=='no' && $payment->booking_payment_paid=='yes'){
                        $convert_url='<br><a style="margin-left: 0px;text-decoration: underline;" href="'.$this->data['base_url'].'/admission/bookings/convert/'.encode_data($payment->booking_id).'">Convert to Admission</a>';
                    }else if($payment->booking_converted=='yes' && $payment->booking_payment_paid=='yes'){
                        $convert_url='<br><label class="badge badge-inverse">Converted to Admission<label>';
                    }else if($payment->booking_converted=='cancel' && $payment->booking_payment_paid=='yes'){
                        $convert_url='<br><label class="badge badge-danger">Booking Cancelled<label>';
                    }else if($payment->booking_converted=='cancel' && $payment->booking_payment_paid=='no'){
                        $convert_url='<br><label class="badge badge-danger">Booking Cancelled<label>';
                    }else if($payment->booking_converted=='no' && $payment->booking_payment_paid=='yes'){
                        $convert_url='<br><label class="badge badge-danger">Admission can be done after payment<label>';
                    }else if($payment->booking_converted=='no' && $payment->booking_payment_paid=='no'){
                        $convert_url='<br><label class="badge badge-danger">Admission can be done after payment<label>';
                    }


                   // $admission_data=$this->adm->get_admission_data(array('admission_seat_booking_id'=>$payment->booking_id,'admission_inst_id'=>$this->data['userdata']->user_id));

                   
                    $booking_date='<br>Booking Date: '.date('d-m-Y',strtotime($payment->booking_date));//.'<br>Payment Recieved Date:'.date('d-m-Y',strtotime($payment->booking_payment_recieved_date));
                    

                    if($payment->booking_converted=='yes' && $payment->admission_date!=null){
                        $convert_url.='<br>Admission Date: '.date('d-m-Y',strtotime($payment->admission_date));
                    }else if($payment->booking_converted=='cancel' && $payment->admission_date!=null){
                        $convert_url.=' : '.date('d-m-Y',strtotime($payment->admission_date));
                    }

                    if($posts['data_of']==='branch'){
                        $convert_url='';
                    }

                    if($payment->booking_payment_paid==='yes'){                        

                        if($payment->booking_pay_mode==='cheque'){

                            if(!empty($payment->booking_payment_checq_files_ids)){
                                $cheque_file_data=$this->sm->get_file_in('storage_id',$payment->booking_payment_checq_files_ids);

                                if(!empty($cheque_file_data)){
                                    foreach ($cheque_file_data as $k => $v) {
                                        $c_file[$payment->booking_id][]=base_url($v->media_disk_path_relative);
                                    }
                                }else{
                                    $c_file='';
                                }

                                if($c_file!=''){
                                    $_cfile=char_separated($c_file[$payment->booking_id]);                                
                                }else{
                                    $_cfile='';
                                } 
                            }else{
                                $_cfile='';
                            }

                            //<button type="button" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-bill_type="seat_booking" data-booking_code="'.$payment->booking_code.'" data-target="#paymentBillModal" data-toggle="modal">View Bill</button>
                             // print("payment");    
                          //  print_obj($payment);
                           // die();

                          /* if($payment->booking_stu_id==50){
                            print_obj($payment->stu_first_name);
                        
                           }
                           else{

                            print("$payment->stu_first_name");
                           }
                           die();*/
                            $view_bill='<br><a href="'.$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($payment->booking_id).'" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" target="_blank">View Bill</a> <button type="button" class="btn btn-xs btn-dark btn_view_cheque" data-booking_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" data-checq_no="'.$payment->booking_payment_checq_no.'"data-student_name="'.$payment->stu_first_name.'" data-student_email="'.$payment->stu_email.'"data-payment_amount="'.$payment->booking_payment.' "data-cheque_cleared="'.$payment->cheque_is_cleared.'"data-cheque_cash_date="'.$payment->cheque_details_cash_date.'"data-cheque_deposite_date="'.$payment->cheque_details_deposite_date.'"data-cfile="'.$_cfile.'" >View Cheque Details</button>';
                        }else{
                            // $view_bill='<br><button type="button" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-bill_type="seat_booking" data-booking_code="'.$payment->booking_code.'" data-target="#paymentBillModal" data-toggle="modal">View Bill</button>';

                            $view_bill='<br><a href="'.$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($payment->booking_id).'" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" target="_blank">View Bill</a>';
                        } 
                    }else{

                        if($payment->booking_pay_mode==='cheque'){

                            if(!empty($payment->booking_payment_checq_files_ids)){
                                 $cheque_file_data=$this->sm->get_file_in('storage_id',$payment->booking_payment_checq_files_ids);

                                if(!empty($cheque_file_data)){
                                    foreach ($cheque_file_data as $k => $v) {
                                        $c_file[$payment->booking_id][]=base_url($v->media_disk_path_relative);
                                    }
                                }else{
                                    $c_file='';
                                }

                                if($c_file!=''){
                                    $_cfile=char_separated($c_file[$payment->booking_id]);                                
                                }else{
                                    $_cfile='';
                                }
                            }else{
                                $_cfile='';
                            }

                            //<button type="button" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-bill_type="seat_booking" data-booking_code="'.$payment->booking_code.'" data-target="#paymentBillModal" data-toggle="modal">View Bill</button>
                               
                                       if($payment->cheque_is_cleared=='no'){
                            $view_bill='<br><a href="'.$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($payment->booking_id).'" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" >View Bill</a>
                                
                            <button type="button"       style="background-color:red;"  class="btn btn-xs btn-dark btn_view_cheque" data-booking_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" data-checq_no="'.$payment->booking_payment_checq_no.'" data-student_name="'.$payment->stu_first_name.'" data-student_email="'.$payment->stu_email.'" data-payment_amount="'.$payment->booking_payment.'" data-cfile="'.$_cfile.'"data-cheque_cleared="'.$payment->cheque_is_cleared.'"data-cheque_cash_date="'.$payment->cheque_details_cash_date.'"data-cheque_deposite_date="'.$payment->cheque_details_deposite_date.'" data-paid="no">View Cheque Details</button>';
                                       }
                                       else{

                                        $view_bill='<br><a href="'.$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($payment->booking_id).'" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" >View Bill</a>
                                
                                        <button type="button"     class="btn btn-xs btn-dark btn_view_cheque" data-booking_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" data-checq_no="'.$payment->booking_payment_checq_no.'" data-student_name="'.$payment->stu_first_name.'" data-student_email="'.$payment->stu_email.'" data-payment_amount="'.$payment->booking_payment.'" data-cfile="'.$_cfile.'"data-cheque_cleared="'.$payment->cheque_is_cleared.'"data-cheque_cash_date="'.$payment->cheque_details_cash_date.'"data-cheque_deposite_date="'.$payment->cheque_details_deposite_date.'" data-paid="no">View Cheque Details</button>';

                                       }
                        }else{
                            $view_bill='';
                        } 
                    }

                    $course_name='<br>Course:'.$course_data->course_code;

                    $session=$payment->session_start_year.'-'.$payment->session_end_year;

                    //$stu_course_custom_cost=$this->um->get_course_cost(array('cost_inst_id'=>$payment->booking_inst_id,'cost_stu_id'=>$payment->booking_stu_id,'cost_cousre_id'=>$payment->booking_course_id,'cost_session_id'=>$payment->booking_sess_id));


                    //$session_course_data=$this->sessm->get_session_course(array('session_id'=>$session_id,'session_inst_id'=>$inst_id,'session_course_id'=>$course_id));

                    $stu_course_custom_cost=$this->sessm->get_session_course_student_wise(array('session_id'=>$payment->booking_sess_id,'session_inst_id'=>$payment->booking_inst_id,'session_course_id'=>$payment->booking_course_id,'session_stu_id'=>$payment->booking_stu_id));

                    //print_obj($stu_course_custom_cost);

                    if(!empty($stu_course_custom_cost)){
                        $course_cost='<br>Course Fees:'.number_format($stu_course_custom_cost->session_course_fees,2);
                    }else{
                        $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$payment->booking_sess_id,'session_inst_id'=>$payment->booking_inst_id,'session_course_id'=>$payment->booking_course_id));
                         $course_cost='<br>Course Fees:'.number_format($booking_amount_data->session_course_fees,2);
                    }

                    $row[]  =   $no;
                    $row[]  =   $payment->booking_code.$course_name.$course_cost.$booking_date.$convert_url;
                    //$row[]  =   '<img class="img-thumbnail" src="'.$image.'" height="80px;" width="80px;">';
                    //$row[]  =   $student_name;
                    $row[]  =   $session.$view_bill;
                    // $row[]  =   $payment->course_name;
                    // $row[]  =   $payment->subject_name;

                    if($payment->booking_payment_paid=='yes'){
                        $payment_paid  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($payment->booking_payment,2).' paid</span>'; 
                    }else{
                        $payment_paid  =   '<span class="label label-danger "><i class="fa fa-inr"></i> '.number_format($payment->booking_payment,2).' not paid</span>';


                    }

                    if($payment->booking_is_new=='no'){
                        $payment_paid.='<br><br><span class="badge badge-dark">Old Data</span>';
                    }

                    $row[]  = $payment_paid;


                    if(!empty($agent_payment) && $agent_payment->amount_value>0){
                        if($agent_payment->amount_paid=='yes'){
                            $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($agent_payment->amount_value,2);
                        }else{
                            $row[]  =   '<span class="label label-danger "><i class="fa fa-inr"></i> '.number_format($agent_payment->amount_value,2);
                        }
                        
                    }else{
                        $row[]  = '-';
                    }
                   
                    if($payment->booking_payment_paid=='yes'){
                        $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($income,2);
                    }else{
                        $row[]  =   '-';
                    }
                    

                    $payment_and_cancel='<div class="button-group">';

                    if($payment->booking_payment_paid=='yes'){
                        $payment_and_cancel  =   ''; 
                    }else{
                        $payment_code=$payment->booking_code.'_stuinst';
                        $booking_date=date('d-m-Y',strtotime($payment->booking_date));
                        if($payment->booking_converted=='yes'){
                            $payment_and_cancel  .=   ''; 
                        }else if($payment->booking_converted=='no'){
                             
                            if($payment->booking_pay_mode==='cheque'){
                                $payment_and_cancel.='<label class="badge badge-dark">Waiting for Cheque to cash</label>';
                            }else{
                               $payment_and_cancel .=   '<button type="button" class="btn btn-xs btn-danger btn_cancel_booking" data-booking_id="'.encode_data($payment->booking_id).'" data-booking_code="'.$payment->booking_code.'" data-student_name="'.$student_name.'" data-student_email="'.$payment->stu_email.'" data-payment_amount="'.$payment->booking_payment.'" data-booking_date="'.date('d-m-Y',strtotime($payment->created_at)).'">Cancel</button>';
                               $payment_and_cancel.='<button class="btn btn-xs btn-dark btn_recieve_seatbooking_payment_now" type="button" data-amount_to_pay="'.$payment->booking_payment.'" data-booking_code="'.$payment->booking_code.'" data-enrolled_course="'.$course_data->course_code.'/'.$session.'" data-booking_date="
                                '.$booking_date.'" data-payment_code="'.$payment_code.'" data-payment_id="'.encode_data($payment->booking_payment_id).'" data-student_name="'.$student_name.'" data-toggle="modal" data-target="#payBookingFeesInstModeModal">Recieve Payment Now</button>'; 
                            }
                            
                        }else if($payment->booking_converted=='cancel'){
                            $payment_and_cancel  .=   '<label class="badge badge-danger">Canceled</label>'; 
                        }else if($payment->booking_converted!='cancel'){
                            if($payment->booking_pay_mode==='cheque'){
                                $payment_and_cancel.='<label class="label label-inverse">Waiting for Cheque to cash</label>';
                            }else{

                                $payment_and_cancel.='<button class="btn btn-xs btn-dark btn_recieve_seatbooking_payment_now" type="button" data-amount_to_pay="'.$payment->booking_payment.'" data-booking_code="'.$payment->booking_code.'" data-enrolled_course="'.$course_data->course_code.'/'.$session.'" data-booking_date="
                                '.$booking_date.'" data-payment_code="'.$payment_code.'" data-payment_id="'.encode_data($payment->booking_payment_id).'" data-student_name="'.$student_name.'" data-toggle="modal" data-target="#payBookingFeesInstModeModal">Recieve Payment Now</button>';
                                    
                            }
                        }  
                    }

                    $payment_and_cancel.='</div>';

                    $row[]  =$payment_and_cancel;               

                    $data[] = $row; 
                }

                //print_obj($total_booking_payments);die;

                $output = array(
                    "draw" => isset($posts['draw'])?$posts['draw']:'',
                    "recordsTotal" => $this->adm->_get_seat_booking_payment_data($posts,$param,TRUE),
                    "recordsFiltered" => $this->adm->_get_seat_booking_payment_data($posts,$param,TRUE),
                    "data" => $data,
                    "total_tobe_pay_amount"=>number_format($total_tobe_pay_amount,2),
                    "total_admissions"=>$total_admissions,
                    "total_seat_bookings"=>$total_seat_bookings,
                    "total_conversion"=>$total_conversion,
                    "total_booking_payments"=>(!empty($total_booking_payments) && $total_booking_payments[0]->total!=null)?number_format($total_booking_payments[0]->total,2):'0.00',
                    "total_booking_payments_due"=>(!empty($total_booking_payments_due) && $total_booking_payments_due[0]->total!=null)?number_format($total_booking_payments_due[0]->total,2):'0.00',
                    "total_agent_payment"=>(!empty($_agent_payment) && $_agent_payment[0]->total!=null)?number_format($_agent_payment[0]->total,2):'0.00',
                    "total_income"=>number_format($total_income,2)
                );

                

                //print_obj($output);die;
                
                echo json_encode($output);

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


    //Agents

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

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

        }       
        if(session_userdata('isAdminLoggedin')){

            $agent_id=$this->uri->segment(3,0);

            //echo $agent_id;die;

            if($agent_id!='0'){
                $agent_id=decode_data($agent_id);

                if(!empty($agent_id)){

                    $agent_data=$this->um->get_user(array('agent_user_id'=>$agent_id),'agent');

                    //print_obj($agent_data);die;

                    if(!empty($agent_data)){
                        $this->data['page_title']='Agent - '.$agent_data->agent_name.' Payments Dashboard';

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

                        $total_payments=$this->fm->_get_agent_payments_total(array('agent_id'=>$agent_id,'amount_paid'=>'yes','inst_id'=>$agent_data->agent_inst_id));
                        $total_payments_due=$this->fm->_get_agent_payments_total(array('agent_id'=>$agent_id,'amount_paid'=>'no','inst_id'=>$agent_data->agent_inst_id));

                        $this->data['total_payments']=(!empty($total_payments) && $total_payments[0]->total)?number_format($total_payments[0]->total,2):'0.00';
                        $this->data['total_payments_due']=(!empty($total_payments_due) && $total_payments_due[0]->total!=null)?number_format($total_payments_due[0]->total,2):'0.00';

                        $this->data['total_seats_bookings_given']=$this->fm->_get_agent_booking_count(array('agent_id'=>$agent_id,'booking_type'=>'seat_booking','inst_id'=>$agent_data->agent_inst_id));


                        $this->theme->title($this->data['page_title'])->load('payments/vw_payment_agent', $this->data);
                    }else{
                        redirect($this->data['base_url'].'/agents');
                    }
                }else{
                    redirect($this->data['base_url'].'/agents');
                }
            }else{

                $this->data['page_title']='Agent Payments Dashboard';

                if($this->data['userdata']->user_type=='institute'){
                    $this->data['total_seats_bookings_given']=$this->fm->_get_agent_booking_count(array('agent_id!='=>NULL,'booking_type'=>'seat_booking','inst_id'=>$this->data['userdata']->user_id));

                    $total_payments=$this->fm->_get_payments_total(array('details_type'=>'agent_payment'));

                    $this->data['total_agent_payments']=(!empty($total_payments) && isset($total_payments[0]->total))?number_format($total_payments[0]->total,2):'0.00';

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

                
           }
            
        }else{

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


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

                $db_group=$this->data['userdata']->user_code;

                $param['column_order'] = array(
                    null,
                    'agent_code',
                    'agent_name'
                );


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

                $param['column_search'] = array('agent_code','agent_name');
                $param['order'] = array('details_amount' => 'ASC');
                $posts=$this->input->post();

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

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

                $action='';

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

                    $row = array();

                    $row[]  =   $no;
                    $row[]  =   $payment->agent_code;
                    $row[]  =   $payment->agent_name;
                    $row[]  =   $payment->agent_email;
                    $row[]  =   $payment->agent_ph;
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($payment->details_amount,2).'</span>';

                    $row[]  =   '<a href="'.$this->data['base_url'].'/payments/agents/'.encode_data($payment->agent_user_id).'" class="btn btn-xs btn-dark">Payments</a>';
                    

                    $data[] = $row; 
                }

                $output = array(
                    "draw" => isset($posts['draw'])?$posts['draw']:'',
                    "recordsTotal" => $this->fm->__get_agent_payments($posts,$param,TRUE,FALSE,$db_group),
                    "recordsFiltered" => $this->fm->__get_agent_payments($posts,$param,TRUE,FALSE,$db_group),
                    "data" => $data,
                );
                
                echo json_encode($output);
            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    //Specific Payments
    public function onSearchAgentPayments(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){
                
                $db_group=decode_data(session_userdata('admin_db_group'));

                //echo $db_group;die;

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

                $param['inst_id']=$this->inst_id;

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

                $param['agent_id']=decode_data($posts['agent_id']);

                //echo $param['agent_id'];die;

                $list = $this->fm->_get_agent_payments($posts,$param,FALSE,FALSE,$db_group);
                
                $data = array();
                $no = isset($posts['start'])?$posts['start']:0;

                $action='';

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

                    $row = array();
                    $balance_amount=0;

                    $agent_payment_data=$this->fm->_get_payments_total(array('details_payment_id'=>$payment->payment_id,'details_agent_id'=>$payment->agent_id,'details_type'=>'agent_payment'),$db_group);

                    $balance_payments=$this->fm->_get_installments_balance_last(array('inst_balance_type'=>'agent_balance','inst_agent_id'=>$payment->agent_id,'inst_payment_id'=>$payment->payment_id),$db_group);

                    //print_obj($balance_payments);die;

                    if(!empty($balance_payments)){
                        $balance_amount=$balance_payments->inst_payment_balance;
                    }else{

                        if(!empty($agent_payment_data) && isset($agent_payment_data[0]->total)){
                            $balance_amount=$payment->amount_value-$agent_payment_data[0]->total;
                        }                        
                    }

                    if(!empty($agent_payment_data) && isset($agent_payment_data[0]->total)){
                        $last_paid_amount=$agent_payment_data[0]->total;
                    }else{
                        $last_paid_amount='0';
                    }
                    

                    if($payment->booking_type=='seat_booking'){

                        $booking_data=$this->adm->get_seat_bookings_data(array('booking_id'=>$payment->booking_id),TRUE,$db_group);

                        if(!empty($booking_data)){
                            $student_name=$booking_data->stu_first_name.' '.$booking_data->stu_mid_name.' '.$booking_data->stu_last_name.'<br><strong>'.$booking_data->stu_user_code.'</strong>';
                            $booking_code=$booking_data->booking_code;
                        }else{
                            $student_name='';
                            $booking_code='';
                        }                            

                    }else if($payment->booking_type=='admission'){
                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$payment->booking_id),TRUE,$db_group);

                        $student_name=$admission_data->stu_first_name.' '.$admission_data->stu_mid_name.' '.$admission_data->stu_last_name.'<br><strong>'.$admission_data->stu_user_code.'</strong>';
                        $booking_code=$admission_data->admission_code;
                    }

                    if($payment->amount_paid_at!=null && $payment->amount_paid_at!='0000-00-00'){
                        $amount_paid_at=date('d-m-Y',strtotime($payment->amount_paid_at));
                    }else{
                        $amount_paid_at='--';
                    }

                    if($payment->amount_paid=='yes'){
                        $payment_status='<span class="badge badge-sm badge-success">Yes</span>';
                        $dates='<br><strong>Date Initiated:'.date('d-m-Y',strtotime($payment->amount_created_at)).'</strong><br><strong>Date Completed:'.$amount_paid_at.'</strong>';
                    }else if($payment->amount_paid=='no'){
                        $payment_status='<span class="badge badge-sm badge-danger">No</span>';
                        $dates='<br><strong>Date Initiated:'.date('d-m-Y',strtotime($payment->amount_created_at)).'</strong>';
                    }


                    $row[]  =   $no;
                    $row[]  =   '<b>'.$booking_code.'</b>'.$dates;
                    $row[]  =   $student_name;
                    $row[]  =   $payment->session_start_year.'-'.$payment->session_end_year;
                    $row[]  =   $payment->course_code;
                    $row[]  =   '<i class="fa fa-inr"></i> <b>'.number_format($payment->amount_value).'</b>';
                    $row[]  =   (!empty($agent_payment_data) && isset($agent_payment_data[0]->total))?'<i class="fa fa-inr"></i> <b>'.number_format($agent_payment_data[0]->total):$payment_status;

                    $row[]  =   '<i class="fa fa-inr"></i> <b>'.number_format($balance_amount);


                    if($payment->amount_paid=='yes'){
                        $row[]  =   '<button class="btn btn-xs btn-dark btn_view_agent_payments" data-session_year="'.$payment->session_start_year.'-'.$payment->session_end_year.'" data-booking_code="'.$payment->booking_code.'" data-amount="'.$payment->amount_value.'" data-stu_id="'.encode_data($payment->stu_id).'" data-agent_id="'.encode_data($payment->agent_id).'" data-booking_id="'.encode_data($payment->booking_id).'" data-inst_id="'.encode_data($payment->inst_id).'" data-payment_id="'.encode_data($payment->payment_id).'" data-last_paid_amount="'.$last_paid_amount.'" data-toggle="modal" data-target="#payToAgentModal"><i class="fa fa-eye"></i></button>';
                    }else{

                        if($this->data['userdata']->user_type=='agent'){
                            $row[]='<span class="badge badge-dark">Payment Pending</span>';
                        }else{
                            if($payment->booking_type=='admission'){
                                $admission_payment=$this->adm->get_seat_booking_payment_data(array('booking_inst_id'=>$payment->inst_id,'booking_pk_id'=>$payment->booking_id),TRUE,$db_group);

                                $row[]  =  '<button type="button" class="btn btn-xs btn-dark btn_pay_now_to_agent" data-session_year="'.$payment->session_start_year.'-'.$payment->session_end_year.'" data-booking_code="'.$payment->booking_code.'" data-amount="'.$payment->amount_value.'" data-stu_id="'.encode_data($payment->stu_id).'" data-agent_id="'.encode_data($payment->agent_id).'" data-booking_id="'.encode_data($payment->booking_id).'" data-inst_id="'.encode_data($payment->inst_id).'" data-payment_id="'.encode_data($payment->payment_id).'" data-last_paid_amount="'.$last_paid_amount.'" data-toggle="modal" data-target="#payToAgentModal">Pay now</button>';
                            }else{
                                $seatbooking_payment=$this->adm->get_seat_booking_payment_data(array('booking_inst_id'=>$payment->inst_id,'booking_pk_id'=>$payment->booking_id),TRUE,$db_group);

                                if(!empty($seatbooking_payment) && $seatbooking_payment->booking_payment_paid=='yes'){
                                    if($balance_amount>0){
                                        if(!empty($agent_payment_data)){
                                            $row[]  =   '<button type="button" class="btn btn-xs btn-dark btn_pay_balance_now_to_agent" data-booking_code="'.$payment->booking_code.'" data-amount="'.$payment->amount_value.'" data-session_year="'.$payment->session_start_year.'-'.$payment->session_end_year.'" data-stu_id="'.encode_data($payment->stu_id).'" data-agent_id="'.encode_data($payment->agent_id).'" data-booking_id="'.encode_data($payment->booking_id).'" data-inst_id="'.encode_data($payment->inst_id).'" data-payment_id="'.encode_data($payment->payment_id).'" data-balance_amount="'.$balance_amount.'" data-last_paid_amount="'.$last_paid_amount.'" data-toggle="modal" data-target="#payToAgentModal">Pay balance now</button>';
                                        }else{
                                            $row[]  =   '<button type="button" class="btn btn-xs btn-dark btn_pay_now_to_agent" data-booking_code="'.$payment->booking_code.'" data-amount="'.$payment->amount_value.'" data-session_year="'.$payment->session_start_year.'-'.$payment->session_end_year.'" data-stu_id="'.encode_data($payment->stu_id).'" data-agent_id="'.encode_data($payment->agent_id).'" data-booking_id="'.encode_data($payment->booking_id).'" data-inst_id="'.encode_data($payment->inst_id).'" data-payment_id="'.encode_data($payment->payment_id).'" data-balance_amount="'.$balance_amount.'" data-last_paid_amount="'.$last_paid_amount.'" data-toggle="modal" data-target="#payToAgentModal">Pay now</button>';
                                        }
                                        
                                    }else{
                                        $row[]  =   '<button type="button" class="btn btn-xs btn-dark btn_pay_now_to_agent" data-booking_code="'.$payment->booking_code.'" data-amount="'.$payment->amount_value.'" data-session_year="'.$payment->session_start_year.'-'.$payment->session_end_year.'" data-stu_id="'.encode_data($payment->stu_id).'" data-agent_id="'.encode_data($payment->agent_id).'" data-booking_id="'.encode_data($payment->booking_id).'" data-inst_id="'.encode_data($payment->inst_id).'" data-payment_id="'.encode_data($payment->payment_id).'" data-balance_amount="'.$balance_amount.'" data-last_paid_amount="'.$last_paid_amount.'" data-toggle="modal" data-target="#payToAgentModal">Pay now</button>';
                                    }
                                    

                                    //$row[]  =  'Payment feature is getting updated'; 
                                }else{
                                    $row[]  =   'Student not paid yet';
                                }
                            } 
                        }

                                               
                    }
                    
                    

                    $data[] = $row; 
                }

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

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

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

                $userdata=$this->data['userdata'];
                $db_group=$userdata->user_code;

                $payment_id=post_data('payment_id');
                $agent_id=post_data('agent_id');
                $student_id=post_data('stu_id');
                $session_year=post_data('session_year');

                $_payment_details=array();
                $_student_data=array();

                if(!empty($payment_id) && !empty($agent_id)){

                    $agent_id=decode_data($agent_id);
                    $payment_id=decode_data($payment_id);
                    $student_id=decode_data($student_id);

                    $param['agent_id']=$agent_id;
                    $param['payment_id']=$payment_id;
                    $param['details_type']='agent_payment';

                    $session_student_course_data=$this->sessm->get_session_courses_student_wise(array('session_stu_id'=>$student_id),TRUE,'session_inst_id','ASC',$db_group);

                    $payment_details=$this->fm->___get_payments_details($param,FALSE,FALSE,FALSE,$db_group);

                    // print_obj($payment_details);die;

                    $student_data=$this->um->get_user(array('user_id'=>$student_id),'student',null,$db_group);

                    //print_obj($student_data);die;

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

                        $_student_data=array(
                            'student_name'=>$student_name,
                            'student_code'=>$student_data->stu_user_code,
                            'student_session'=>$session_year
                        );
                    }

                    $agent_total_payment=$this->fm->get_agent_payment(array('payment_id'=>$payment_id,'agent_id'=>$agent_id),TRUE,FALSE,$db_group);

                    if(!empty($agent_total_payment)){
                        $_payment_details['total_payment']=number_format($agent_total_payment->amount_value);
                    }else{
                        $_payment_details['total_payment']=number_format('0');
                    }

                    $agent_payment_data=$this->fm->_get_payments_total(array('details_payment_id'=>$payment_id,'details_agent_id'=>$agent_id,'details_type'=>'agent_payment'),$db_group);

                    if(!empty($agent_payment_data) && isset($agent_payment_data[0]->total)){
                        $_payment_details['total_paid']=number_format($agent_payment_data[0]->total);
                    }else{
                        $_payment_details['total_paid']=number_format('0');
                    }

                    $balance_payments=$this->fm->_get_installments_balance_last(array('inst_balance_type'=>'agent_balance','inst_agent_id'=>$agent_id,'inst_payment_id'=>$payment_id),$db_group);

                    if(!empty($balance_payments) && isset($balance_payments)){
                        $_payment_details['total_due']=number_format($balance_payments->inst_payment_balance);
                    }else{
                        $_payment_details['total_due']=number_format('0');
                    }

                    //print_obj($payment_details);die;

                    if(!empty($payment_details)){
                        foreach ($payment_details as $key => $value) {
                            $_payment_details['payment_data'][]=array(
                                'details_id'=>$value->details_id,
                                'amount_value'=>number_format($value->details_amount),
                                'payment_date'=>date('d-m-Y',strtotime($value->details_created_at)),
                                'txn_code'=>$value->details_txn_id,
                                'payment_type'=>$value->details_msg,
                                'booking_code'=>$value->details_order_code,
                                'student_name'=>$student_name,
                                'student_code'=>$value->stu_user_code,
                                'course_name'=>$value->course_name.' [ '. $value->course_code .' ]',
                                'course_session'=>$value->session_start_year.'-'.$value->session_end_year
                            );
                        }
                    }

                    $_payment_details['bill_generation_link']=$this->data['base_url'].'/bills?type=agent_bills&code='.encode_data($payment_id).'&agtcode='.encode_data($agent_id).'&stucode='.encode_data($student_id).'&sesscode='.encode_data($session_year);


                    $_payment_details['student_data']=$_student_data;


                    //print_obj($_payment_details);die;

                    $this->data=$_payment_details;
                    $this->data['session_student_course_data']=$session_student_course_data;

                    $return['html']=$this->theme->view('_pages/payments/vw_payment_agent_consolidated',$this->data,true);
                }else{
                    $return['error']='Required fields are missing';
                }

                json_headers($return);  

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


    public function onLoadAgentPaymentForm(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){
                $userdata=$this->data['userdata'];
                $db_group=$userdata->user_code;

                $payment_id=post_data('payment_id');
                $agent_id=post_data('agent_id');
                $booking_id=post_data('booking_id');
                $stu_id=post_data('stu_id');
                $action_type=post_data('action_type');

                $agent_id=decode_data($agent_id);
                $payment_id=decode_data($payment_id);
                $student_id=decode_data($stu_id);

                $agent_data=$this->um->get_agents(array('agent_id'=>$agent_id),TRUE,$db_group);

                $session_student_course_data=$this->sessm->get_session_course_student_wise(array('session_stu_id'=>$student_id),TRUE,$db_group);

                $agent_payment_data=$this->fm->_get_payments_total(array('details_payment_id'=>$payment_id,'details_agent_id'=>$agent_id,'details_type'=>'agent_payment'),$db_group);

                $agent_total_payment=$this->fm->get_agent_payment(array('payment_id'=>$payment_id,'agent_id'=>$agent_id),TRUE,FALSE,$db_group);

                //print_obj($agent_total_payment);die;

                if(!empty($agent_total_payment)){
                    $total_payment=$agent_total_payment->amount_value;
                }else{
                    $total_payment=0;
                }

                $balance_payments=$this->fm->_get_installments_balance_last(array('inst_balance_type'=>'agent_balance','inst_agent_id'=>$agent_id,'inst_payment_id'=>$payment_id),$db_group);               


                if(!empty($balance_payments)){
                    $balance_amount=$balance_payments->inst_payment_balance;
                }else{

                    $balance_amount=$total_payment-$agent_payment_data[0]->total;                        
                }

                if(!empty($agent_payment_data) && isset($agent_payment_data[0]->total)){
                    $last_paid_amount=$agent_payment_data[0]->total;
                }else{
                    $last_paid_amount='0';
                }


                $this->data['session_student_course_data']=$session_student_course_data;
                $this->data['agent_payment_data']=$agent_payment_data;
                $this->data['balance_payments']=$balance_payments;
                $this->data['balance_amount']=$balance_amount;
                $this->data['last_paid_amount']=$last_paid_amount;
                $this->data['payment_gateway_settings']=$payment_gateway_settings;
                $this->data['payment_id']=encode_data($payment_id);
                $this->data['agent_id']=encode_data($agent_id);
                $this->data['agent_inst_id']=encode_data($agent_data->agent_inst_id);
                $this->data['booking_id']=$booking_id;
                $this->data['total_payment']=$total_payment;
                $this->data['student_id']=encode_data('student_id');
                $this->data['action_type']=$action_type;
                $return['html']=$this->theme->view('_pages/users/vw_agent_payment_form',$this->data,true);

                json_headers($return);

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

    //Agents




    public function indexHostelPayments(){

   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']='Hostel Payments';






            $this->theme->title($this->data['page_title'])->add_partial('partial_payment_bill_modal')->load('payments/vw_payment_hostels', $this->data);

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


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

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

                $check_institute=check_institute($userdata);

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

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


                $param['inst_id']=$check_institute['institute_id'];


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

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

                $action='';

                $next=1;

                $date_now = new DateTime();

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

                    $row = array();

                    $due_date    = new DateTime($fees->due_at);

                    if($userdata->user_type=='student'){
                        $order_id='ORDHT'.date('Ymd').'_'.$fees->payment_year.'_'.$fees->payment_month.'_'.gen_code(2).'_'.$fees->payment_id.'_stu_hostel';
                    }else if($userdata->user_type=='institute'){
                        $order_id='ORDHT'.date('Ymd').'_'.$fees->payment_year.'_'.$fees->payment_month.'_'.gen_code(2).'_'.$fees->payment_id.'_stuinst_hostel';
                    }

                   

                    if($date_now>$due_date){
                        $penalty_applied=TRUE;
                        $total_due=$fees->payment_value+$fees->payment_late_value;  
                    }else if($date_now==$due_date){
                        $penalty_applied=FALSE;
                        $total_due=$fees->payment_value;
                    }else{
                        $penalty_applied=FALSE;
                        $total_due=$fees->payment_value;
                    }

                    $order_id='';

                    if($fees->payment_status=='not paid'){

                        if($no==1){
                            $paybtn='<button type="button" class="btn btn-xs btn-dark btn_pay_hostel_fees" data-payment_id="'.encode_data($fees->payment_id).'" data-order_id="'.$order_id.'" data-hostel_name="'.$fees->hostel_name.'" data-month="'.ucwords($fees->payment_month).'" data-year="'.$fees->payment_year.'">Recieve Now</button>';
                        }else{
                            if($next==$no){
                                 $paybtn='<button type="button" class="btn btn-xs btn-dark btn_pay_hostel_fees" data-payment_id="'.encode_data($fees->payment_id).'" data-order_id="'.$order_id.'" data-hostel_name="'.$fees->hostel_name.'" data-month="'.ucwords($fees->payment_month).'" data-year="'.$fees->payment_year.'">Recieve Now</button>';                               
                            }else{
                                $paybtn=''; 
                            }
                        }

                    }else{
                        $next+=1;
                        $paybtn='<label class="label label-success">Paid</label>';
                    }

                    $student_info='<b>Student Code:'.$fees->stu_user_code.'</b><br><b>Session:</b>'.$fees->session_start_year.'-'.$fees->session_end_year.'<br><b>Course:</b>'.$fees->course_name.'<br><b>Name:</b>'.$fees->stu_first_name.' '.$fees->stu_mid_name.' '.$fees->stu_last_name;

                    
                    $row[]  =   $no;
                    //$row[]  =   $fees->session_start_year.'-'.$fees->session_end_year;
                    $row[]  =   $student_info;
                    $row[]  =   ucwords($fees->payment_month).' - '.$fees->payment_year;
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($fees->payment_value,2).'</span>';
                    $row[]  =   ($penalty_applied==TRUE)?'<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($fees->payment_late_value,2).'</span>':'';
                    
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($total_due,2).'</span>';
                    $row[]  =   date('d-m-Y',strtotime($fees->due_at));
                    $row[]  =   ($fees->payment_status=='paid')?date('d-m-Y',strtotime($fees->recieved_at)):'<span class="label label-danger ">'.ucwords($fees->payment_status).'</span>';
                    $row[]  =   $paybtn;



                    $data[] = $row; 
                }

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

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


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

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

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

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

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

                $list = $this->hm->_get_hostel_admission_fees_assign_data($posts,$param,FALSE,FALSE);

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

                $action='';

                $next=1;

                $date_now = new DateTime();

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

                    $row = array();

                    $total_fees=$fees->admission_fees_value+$fees->admission_caution_fees_value;


                    $payment_data=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_payment_type'=>'hostel_admission_fees_payment','details_payment_id'=>$fees->assigned_id));

                    if($fees->assign_fees_paid=='yes'){
                        $paid_date=date('d-m-Y',strtotime($fees->assign_fees_paid_date));
                        $action='<button type="button" class="btn btn-xs btn-dark btn_view_bill" data-payment_id="'.encode_data($payment_data->details_id).'" data-bill_type="hostel_admission_fees_payment">Bill</button>';
                    }else{
                        $paid_date='Not Paid';
                        $action='<button type="button" class="btn btn-xs btn-dark btn_pay_hostel_admission_fees" data-payment_id="" data-academic_session="'.$fees->assign_sess_id.'" data-admission_fee="'.$fees->admission_fees_value.'" data-caution_fee="'.$fees->admission_caution_fees_value.'" data-total_fee="'.$total_fees.'" data-order_id="'.$fees->assigned_id.'" data-hostel_name="'.$fees->hostel_name.'">Pay Now</button>';
                    }

                    $row[]  =   $fees->session_start_year.'-'.$fees->session_end_year;
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($fees->admission_fees_value,2).'</span>';
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($fees->admission_caution_fees_value,2).'</span>';
                    $row[]  =   '<span class="label label-success "><i class="fa fa-inr"></i> '.number_format($total_fees,2).'</span>';

                    

                    $row[]  =   $paid_date;

                    $row[]  =   $action;


                    $data[] = $row; 
                }

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

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


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

                $userdata=$this->data['userdata'];
                $db_group=$userdata->user_code;

                $admission_id=post_data('admission_id');
                $pay_date=post_data('pay_date');
                $pay_auto_credit_account=post_data('pay_auto_credit_account');
                $pay_note=post_data('pay_note');
                $user_id=decode_data(session_userdata('admin_id'));
                $utype=$this->user_type;
                $pay_mode=post_data('pay_mode');
                $pay_amount=post_data('pay_amount');
                $inst_id=$this->inst_id;
                $inst_type=$this->inst_type;
                $details_utr_id=null;
                $payment_can_be_recieved=true;
                $booking_payment_checq_files_ids=null;
                $accounting_type='income';
                $date=date('Y-m-d');
                $split_balance=0;
                $balance_id=0;
                $balance_amount=0;

                if($admission_id!=''){
                    $details_amount=$pay_amount;
                    $admissionfees_data=$this->fm->_get_fees_details(array('payment_admission_id'=>$admission_id));
                    $admission_data=$this->adm->__get_admission_data(array('admission_id'=>$admission_id));

                    if(!empty($admissionfees_data)){                        
                        $details_order_code=$admission_data->admission_code;
                        $details_order_id=$details_order_code.'_'.date('Y').'_'.generate_string(4).'_'.$details_order_code.'_stuinst';                        
                        $txn_id=ge_rand_code_v1('TXN'.$inst_id.$user_id.$admission_id.'0');

                        $admissionfees_last_balance_data=$this->fm->_get_installments_balance_last(array('inst_id'=>$inst_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admissionfees_data->payment_sess_id,'inst_course_id'=>$admissionfees_data->payment_course_id,'inst_balance_type'=>'_admission_fees_balance'),$db_group);

                        if(!empty($admissionfees_last_balance_data)){
                            $txn_amount=$admissionfees_last_balance_data->inst_payment_balance;
                        }else{
                            $txn_amount=$admissionfees_data->payment_admission_fees_value;
                        }
                        

                        if($pay_amount<$txn_amount){
                            $balance_amount=$txn_amount-$pay_amount;
                        }

                        if($pay_mode=='phonepeutr'){
                            $details_utr_id=post_data('pay_phonepe_utr');
                            $txn_id=post_data('pay_phonepe_utr_txn');
                            $details_msg='Phonepay Recieved';

                            if($this->inst_phonepay_txn_required=='yes'){
                                if(!empty($details_utr_id) && !empty($txn_id)){
                                    $payment_can_be_recieved=false;                                                    
                                    $payment_msg='UTR No. and TXN No. from PhonePe are required.';
                                }
                            }
                        }else if($pay_mode=='cheque'){
                            $cheque_no=post_data('pay_bank_cheque_no');
                            $cheque_bank=post_data('pay_bank_name');
                            $cheque_pay_deposite_bank=post_data('pay_deposite_bank');
                            $details_msg='Cheque Recieved';
                            $accounting_type='not_settled';

                            if($cheque_bank!='' && $cheque_no!='' && ($cheque_pay_deposite_bank!='' && $cheque_pay_deposite_bank>0)){
                                if($userdata->user_type=='institute'){
                                    $parent_inst_id=$this->inst_id;
                                    $storage_dir='institutes/'.$userdata->user_code.'/payments/'.date('Y');
                                }

                                if(isset($_FILES['cheque_scan_frontcopy_file']) && $_FILES['cheque_scan_frontcopy_file']['name']!==''){

                                    $cheque_scan_frontcopy_file=array(
                                        'file_size'=>'5',
                                        'file_name'=>'cheque_scan_frontcopy_file',
                                        'file_types'=>'png,jpg,jpeg',
                                        'file_storage_type'=>'cheque_scan_frontcopy_file',
                                        'file_storage_dir'=>$storage_dir,
                                        'file_storage_type_id'=>$admission_data->admission_stu_id,
                                        'file_parent_id'=>$inst_id,
                                        'file_compress'=>false,
                                        'file_operation_type'=>'new',
                                        'file_uploaded_by'=>$userdata->user_id
                                    );
                            
                                    $cheque_scan_frontcopy_file_id=$this->onUploadFiles($cheque_scan_frontcopy_file);
                                }

                                if(isset($_FILES['cheque_scan_backcopy_file']) && $_FILES['cheque_scan_backcopy_file']['name']!=''){

                                    $cheque_scan_backcopy_file=array(
                                        'file_size'=>'5',
                                        'file_name'=>'cheque_scan_backcopy_file',
                                        'file_types'=>'png,jpg,jpeg',
                                        'file_storage_type'=>'cheque_scan_backcopy_file',
                                        'file_storage_dir'=>$storage_dir,
                                        'file_storage_type_id'=>$admission_data->admission_stu_id,
                                        'file_parent_id'=>$inst_id,
                                        'file_compress'=>false,
                                        'file_operation_type'=>'new',
                                        'file_uploaded_by'=>$userdata->user_id
                                    );
                            
                                    $cheque_scan_backcopy_file_id=$this->onUploadFiles($cheque_scan_backcopy_file);
                                }

                                if(isset($cheque_scan_frontcopy_file_id) && isset($cheque_scan_backcopy_file_id)){

                                    if(is_numeric($cheque_scan_frontcopy_file_id) && is_numeric($cheque_scan_backcopy_file_id)){
                                        $booking_payment_checq_files_ids=$cheque_scan_frontcopy_file_id.','.$cheque_scan_backcopy_file_id;
                                    }                           
                                }                                
                            }else{
                                $payment_can_be_recieved=false;
                                $payment_msg='Cheque No.,Bank Name and  Deposite Bank details are required.';
                            }
                        }else if($pay_mode=='cash'){
                            $details_msg='Cash Recieved';
                        }else if($pay_mode=='split'){
                            $txn_amount=$pay_amount;
                            $split_payment_mode=post_data('pay_split_mode');
                            $split_payment_amount=post_data('pay_split_amount');
                            $details_amount=$split_payment_amount;

                            $split_balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$admission_data->admission_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admission_data->admission_sess_id,'inst_course_id'=>$admission_data->admission_course_id,'inst_id'=>$inst_id,'inst_balance_type'=>'admission_fees_split_balance'));

                            if(!empty($split_balance_last_data) && $split_balance_last_data->inst_payment_balance>0){
                                $split_balance=($split_balance_last_data->inst_payment_balance-$split_payment_amount);
                            }else{
                                if($txn_amount>0 && $txn_amount>$split_payment_amount){
                                    $split_balance=($txn_amount-$split_payment_amount);
                                }else{
                                    $split_balance='0';
                                }
                            } 

                            if($split_balance>0){
                                if(!empty($split_balance_last_data)){
                                    $prev_split_balance=$split_balance_last_data->inst_payment_balance;
                                    $prev_split_payment=$split_balance_last_data->inst_payment_total;
                                }else{
                                    $prev_split_balance=0;
                                    $prev_split_payment=0;
                                }
                
                                if($prev_split_balance>0){
                                    $total_split_payable_amount=$prev_split_balance;
                                    //$split_balance=$prev_balance-$txn_amount;
                                }
                            }else{
                                $prev_split_balance=0;
                                $prev_split_payment=0;
                            }

                            if(!empty($split_balance_last_data) && $split_balance_last_data->inst_balance_paid=='no'){
                                $balance_id=$split_balance_last_data->balance_split_id;
                            }

                            if($split_payment_mode=='cheque'){
                                $cheque_no=post_data('pay_bank_cheque_no');
                                $cheque_bank=post_data('pay_bank_name');
                                $cheque_pay_deposite_bank=post_data('pay_deposite_bank');
                                $details_msg='Cheque Recieved';
                                $accounting_type='not_settled';
                                $pay_mode='cheque_split';

                                if($cheque_bank!='' && $cheque_no!='' && ($cheque_pay_deposite_bank!='' && $cheque_pay_deposite_bank>0)){
                                    if($userdata->user_type=='institute'){
                                        $parent_inst_id=$this->inst_id;
                                        $storage_dir='institutes/'.$userdata->user_code.'/payments/'.date('Y');
                                    }

                                    if(isset($_FILES['cheque_scan_frontcopy_file']) && $_FILES['cheque_scan_frontcopy_file']['name']!==''){

                                        $cheque_scan_frontcopy_file=array(
                                            'file_size'=>'5',
                                            'file_name'=>'cheque_scan_frontcopy_file',
                                            'file_types'=>'png,jpg,jpeg',
                                            'file_storage_type'=>'cheque_scan_frontcopy_file',
                                            'file_storage_dir'=>$storage_dir,
                                            'file_storage_type_id'=>$admission_data->admission_stu_id,
                                            'file_parent_id'=>$inst_id,
                                            'file_compress'=>false,
                                            'file_operation_type'=>'new',
                                            'file_uploaded_by'=>$userdata->user_id
                                        );
                                
                                        $cheque_scan_frontcopy_file_id=$this->onUploadFiles($cheque_scan_frontcopy_file);
                                    }

                                    if(isset($_FILES['cheque_scan_backcopy_file']) && $_FILES['cheque_scan_backcopy_file']['name']!=''){

                                        $cheque_scan_backcopy_file=array(
                                            'file_size'=>'5',
                                            'file_name'=>'cheque_scan_backcopy_file',
                                            'file_types'=>'png,jpg,jpeg',
                                            'file_storage_type'=>'cheque_scan_backcopy_file',
                                            'file_storage_dir'=>$storage_dir,
                                            'file_storage_type_id'=>$admission_data->admission_stu_id,
                                            'file_parent_id'=>$inst_id,
                                            'file_compress'=>false,
                                            'file_operation_type'=>'new',
                                            'file_uploaded_by'=>$userdata->user_id
                                        );
                                
                                        $cheque_scan_backcopy_file_id=$this->onUploadFiles($cheque_scan_backcopy_file);
                                    }

                                    if(isset($cheque_scan_frontcopy_file_id) && isset($cheque_scan_backcopy_file_id)){

                                        if(is_numeric($cheque_scan_frontcopy_file_id) && is_numeric($cheque_scan_backcopy_file_id)){
                                            $booking_payment_checq_files_ids=$cheque_scan_frontcopy_file_id.','.$cheque_scan_backcopy_file_id;
                                        }                           
                                    }                                
                                }else{
                                    $payment_can_be_recieved=false;
                                    $payment_msg='Cheque No.,Bank Name and  Deposite Bank details are required.';
                                }

                            }else if($split_payment_mode=='cash'){
                                $details_msg='Cash Recieved';
                                $pay_mode='cash_split';
                            }else if($split_payment_mode=='phonepeutr'){
                                $details_utr_id=post_data('pay_phonepe_utr');
                                $txn_id=post_data('pay_phonepe_utr_txn');
                                $details_msg='Phonepay Recieved';
                                $pay_mode='phonepeutr_split';

                                if($this->inst_phonepay_txn_required=='yes'){
                                    if(!empty($details_utr_id) && !empty($txn_id)){
                                        $payment_can_be_recieved=false;                                                    
                                        $payment_msg='UTR No. and TXN No. from PhonePe are required.';
                                    }
                                }
                            }

                            $txn_amount=$split_payment_amount;
                        }

                        //echo $details_amount;die;

                        if($payment_can_be_recieved==true){

                            $payment_details=array(
                                'details_type'=>'new_admission',
                                'details_payment_type'=>'admission_fees',
                                'details_type_text'=>'Admission Payment',
                                'details_payment_id'=>$admissionfees_data->payment_fees_id,
                                'details_stu_id'=>$admissionfees_data->payment_stu_id,
                                'details_agent_id'=>$admission_data->admission_agent_id,
                                'details_type_id'=>$admissionfees_data->payment_admission_id,
                                'details_sess_id'=>$admissionfees_data->payment_sess_id,
                                'details_course_id'=>$admissionfees_data->payment_course_id,
                                'details_subject_id'=>$admission_data->admission_subject_id,
                                'details_payment_platform'=>$pay_mode,
                                'details_inst_id'=>$admissionfees_data->payment_inst_id,                                
                                'details_parent_inst_id'=>($admission_data->admission_inst_parent_id!=null)?$admission_data->admission_inst_parent_id:$admissionfees_data->payment_inst_id,
                                'details_txn_id'=>$txn_id,
                                'details_utr_id'=>$details_utr_id,
                                'details_cheque_no'=>$cheque_no,
                                'details_pay_checq_files_ids'=>$booking_payment_checq_files_ids,
                                'details_pay_id'=>null,
                                'details_pay_signature'=>null,
                                'details_pay_order_id'=>null,
                                'details_pay_currency'=>'inr',
                                'details_payee_email'=>$admission_data->stu_email,
                                'details_payee_contact'=>$admission_data->stu_ph_no,
                                'details_gateway_created_at'=>null,
                                'details_gateway_status'=>null,
                                'details_order_id'=>$details_order_id,
                                'details_order_code'=>$details_order_code,
                                'details_amount'=>$details_amount,
                                'details_discount_amount'=>'0',
                                'details_created_by_type'=>$inst_type,
                                'details_accounting_type'=>$accounting_type,
                                'details_accounting_type_id'=>'1',
                                'details_created_by'=>$user_id,
                                'details_gateway_status'=>null,
                                'details_msg'=>$details_msg,
                                'details_payment_note'=>$pay_note,
                                'details_status'=>'success',
                                'details_created_at'=>date('Y-m-d',strtotime($pay_date)),
                                'details_gateway_created_at'=>null
                            );

                            $payment_id=$this->fm->store_payment_details($payment_details);

                            if($payment_id){
                                if($pay_mode!='cheque'){
                                    $auto_credit_data=array(
                                        'amount_paid'=>$txn_amount,
                                        'inst_id'=>$inst_id,
                                        'inst_type'=>$inst_type,
                                        'pay_auto_credit_account'=>$pay_auto_credit_account,
                                        'emp_id'=>$user_id,
                                        'payment_details_id'=>$payment_id
                                    );

                                    auto_credit($auto_credit_data);
                                }
                                
                                if($pay_mode=='cheque'){
                                    $cheque_details=array(
                                        'cheque_details_payment_id'=>$payment_id,
                                        'cheque_details_inst_id'=>$inst_id,
                                        'cheque_details_inst_parent_id'=>$admission_data->admission_inst_parent_id,
                                        'cheque_details_stu_id'=>$admission_data->admission_stu_id,
                                        'cheque_details_agent_id'=>$admission_data->admission_agent_id,
                                        'cheque_details_deposite_bank_id'=>$cheque_pay_deposite_bank,
                                        'cheque_details_bank_name'=>$cheque_bank,
                                        'cheque_details_no'=>$cheque_no,
                                        'cheque_details_amount'=>$txn_amount,
                                        'cheque_is_cleared'=>'no',
                                        'cheque_is_bounced'=>'no',
                                        'cheque_details_recieved_date'=>$date,
                                        'cheque_details_created_at'=>$date,
                                        'cheque_details_created_by'=>$userdata->user_id
                                    );
    
                                    $this->fm->store_cheque_details($cheque_details);
                                }

                                if(isset($split_payment_mode) && $split_payment_mode=='cheque'){
                                    $split_cheque_details=array(
                                        'cheque_details_payment_id'=>$payment_id,
                                        'cheque_details_inst_id'=>$inst_id,
                                        'cheque_details_inst_parent_id'=>$admission_data->admission_inst_parent_id,
                                        'cheque_details_stu_id'=>$admission_data->admission_stu_id,
                                        'cheque_details_agent_id'=>$admission_data->admission_agent_id,
                                        'cheque_details_deposite_bank_id'=>$cheque_pay_deposite_bank,
                                        'cheque_details_bank_name'=>$cheque_bank,
                                        'cheque_details_no'=>$cheque_no,
                                        'cheque_details_amount'=>$split_payment_amount,
                                        'cheque_is_cleared'=>'no',
                                        'cheque_is_bounced'=>'no',
                                        'cheque_details_recieved_date'=>$date,
                                        'cheque_details_created_at'=>$date,
                                        'cheque_details_created_by'=>$userdata->user_id
                                    );                                    
    
                                    $this->fm->store_cheque_details($split_cheque_details);
                                }

                                //Balance Data
                                if($balance_amount>0){
                                    $payment_balance_data=array(
                                        'inst_id'=>$this->inst_id,
                                        'inst_parent_id'=>$this->inst_id,
                                        'inst_stu_id'=>$admissionfees_data->payment_stu_id,
                                        'inst_session_id'=>$admissionfees_data->payment_sess_id,
                                        'inst_course_id'=>$admissionfees_data->payment_course_id,
                                        'inst_subject_id'=>'0',
                                        'inst_installment_id'=>'0',
                                        'inst_payment_id'=>$payment_id,
                                        'inst_payment_total'=>$pay_amount,
                                        'inst_payment_balance'=>$balance_amount,
                                        'inst_balance_paid'=>'no',
                                        'inst_balance_partialy_paid'=>'no',
                                        'inst_balance_type'=>'_admission_fees_balance',
                                        'created_at'=>date('Y-m-d'),
                                        'created_by'=>$userdata->user_id,
                                        'created_by_type'=>$inst_type
                                    );

                                   // print_obj($payment_balance_data);die;

                                    $this->fm->store_installments_balance($payment_balance_data,false,$db_group);
                                }


                                if(!empty($admissionfees_last_balance_data)){

                                    if($balance_amount>0){
                                        $balance_update_data=array('inst_balance_partialy_paid'=>'yes','updated_at'=>date('Y-m-d'),'updated_by'=>$userdata->user_id,'updated_by_type'=>$inst_type);
                                        $this->fm->update_installments_balance($balance_update_data,array('balance_id'=>$admissionfees_last_balance_data->balance_id,'inst_id'=>$inst_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admissionfees_data->payment_sess_id,'inst_course_id'=>$admissionfees_data->payment_course_id,'inst_balance_type'=>'_admission_fees_balance'),FALSE,$db_group);
                                    }else if($balance_amount==0){
                                        $payment_balance_data=array(
                                            'inst_id'=>$this->inst_id,
                                            'inst_parent_id'=>$this->inst_id,
                                            'inst_stu_id'=>$admissionfees_data->payment_stu_id,
                                            'inst_session_id'=>$admissionfees_data->payment_sess_id,
                                            'inst_course_id'=>$admissionfees_data->payment_course_id,
                                            'inst_subject_id'=>'0',
                                            'inst_installment_id'=>'0',
                                            'inst_payment_id'=>$payment_id,
                                            'inst_payment_total'=>$pay_amount,
                                            'inst_payment_balance'=>$balance_amount,
                                            'inst_balance_paid'=>'no',
                                            'inst_balance_partialy_paid'=>'no',
                                            'inst_balance_type'=>'_admission_fees_balance',
                                            'created_at'=>date('Y-m-d'),
                                            'created_by'=>$userdata->user_id,
                                            'created_by_type'=>$inst_type
                                        );
    
                                        $this->fm->store_installments_balance($payment_balance_data,false,$db_group);

                                        $balance_update_data=array('inst_balance_partialy_paid'=>'yes','inst_balance_paid'=>'yes','updated_at'=>date('Y-m-d'),'updated_by'=>$userdata->user_id,'updated_by_type'=>$inst_type);
                                        $this->fm->update_installments_balance($balance_update_data,array('inst_id'=>$inst_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admissionfees_data->payment_sess_id,'inst_course_id'=>$admissionfees_data->payment_course_id,'inst_balance_type'=>'_admission_fees_balance'),FALSE,$db_group);
                                    }                                    
                                }

                                //Split Payment Balance Data
                                if($pay_mode=='split'){
                                    
                                    if($split_balance>0){
                                        $split_balance_data=array(
                                            'balance_split_id'=>$balance_id,
                                            'inst_id'=>$inst_id,
                                            'inst_parent_id'=>$admission_data->admission_inst_parent_id,
                                            'inst_stu_id'=>$admissionfees_data->payment_stu_id,
                                            'inst_session_id'=>$admissionfees_data->payment_sess_id,
                                            'inst_course_id'=>$admissionfees_data->payment_course_id,
                                            'inst_subject_id'=>$admission_data->admission_subject_id,
                                            'inst_year'=>'',
                                            'inst_installment_id'=>$admissionfees_data->payment_admission_id,
                                            'inst_payment_id'=>$payment_id,
                                            'inst_payment_total'=>$txn_amount,
                                            'inst_payment_balance'=>$split_balance,
                                            'inst_balance_partialy_paid'=>'yes',   
                                            'inst_balance_type'=>'admission_fees_split_balance',                     
                                            'created_at'=>date('Y-m-d'),
                                            'created_by'=>$userdata->user_id,
                                            'created_by_type'=>$userdata->user_type
                                        );
                                    
                                        $split_balance_id=$this->fm->store_installments_balance($split_balance_data);
                                    }else if($split_balance==0){
                                        $split_balance_data=array(
                                            'balance_split_id'=>$balance_id,
                                            'inst_id'=>$inst_id,
                                            'inst_parent_id'=>$admission_data->admission_inst_parent_id,
                                            'inst_stu_id'=>$admissionfees_data->payment_stu_id,
                                            'inst_session_id'=>$admissionfees_data->payment_sess_id,
                                            'inst_course_id'=>$admissionfees_data->payment_course_id,
                                            'inst_subject_id'=>$admission_data->admission_subject_id,
                                            'inst_year'=>'',
                                            'inst_installment_id'=>$admissionfees_data->payment_admission_id,
                                            'inst_payment_id'=>$payment_id,
                                            'inst_payment_total'=>$txn_amount,
                                            'inst_payment_balance'=>0,
                                            'inst_balance_partialy_paid'=>'yes',   
                                            'inst_balance_type'=>'admission_fees_split_balance',                     
                                            'created_at'=>date('Y-m-d'),
                                            'created_by'=>$userdata->user_id,
                                            'created_by_type'=>$userdata->user_type
                                        );
                                    
                                        $split_balance_id=$this->fm->store_installments_balance($split_balance_data);
                                    }
                                }
                                
                                

                                $payment_details=$this->fm->get_payment_details(array('details_id'=>$payment_id));                                
                                
                                $return['success']='Admission fess recieved successfully.';
                                $return['redirect']=$this->data['base_url'].'/admission/payments/'.encode_data($admission_id).'?p='.$payment_details->details_order_id;
                            }else{
                                $return['error']='Payment data not found.';
                            }
                        }else{
                            $return['error']=$payment_msg;
                        }
                        
                    }else{
                        $return['error']='Admission fees data not found.';
                    }
                }else{
                    $return['error']='Admission data not found';
                }

                json_headers($return);

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



    //Recive payments through gateway
    public function onRedirectToPaymentGateway(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){    

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

                $db_group=$userdata->user_code;

                if($userdata->user_type=='insttitute'){
                    $parent_inst_id=$userdata->user_id;
                }else if($userdata->user_type=='institute_branch'){
                    $parent_inst_id=$userdata->branch_parent_inst_id;
                }else if($userdata->user_type=='student'){
                    $parent_inst_id=$userdata->stu_inst_parent_id;
                }  

                $payment_gateway=post_data('payment_gateway');
                $payment_order_id=post_data('payment_order_id');
                $payment_amount=post_data('payment_amount');

                //echo $payment_amount;die;

                $order_data=explode('_', $payment_order_id);

                //print_obj($order_data);die;

                $_fisrt_sec_order_id=$order_data[0];
                $order_prefix = preg_replace("/[^A-Z]+/", "", $_fisrt_sec_order_id);

                //echo $order_prefix;die;

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

                $gatewaydata=array('payment_gateway'=>$payment_gateway);

                session_set_userdata($gatewaydata);

                $payment_settings=$this->sm->get_system_settings(array('settings_inst_id'=>$inst_id,'settings_type'=>'payment_gateway','settings_type_key'=>$payment_gateway),TRUE,$db_group);

                // print_obj($payment_settings);die;

                if(!empty($payment_settings->settings_value)){
                    $gateway_settings=unserialize($payment_settings->settings_value);

                    //print_obj($gateway_settings);die;

                    //echo $_fisrt_sec_order_id.'<br>';

                    //echo $inst_id;

                    if($order_prefix=='SB'){
                        $payment_details=$this->fm->_get_payment_details(array('details_order_code'=>$_fisrt_sec_order_id,'details_inst_id'=>$inst_id));


                        //print_obj($payment_details);die;


                        $student_name=$payment_details->stu_first_name.' '.$payment_details->stu_mid_name.' '.$payment_details->stu_last_name;
                        $student_email=$payment_details->stu_email;
                        $student_ph=$payment_details->stu_ph_no;
                        $payment_description=$payment_details->details_type_text;
                    }else if($order_prefix=='ADM'){
                        $order__id=$order_data[0];
                        $installemnt_unique_code=$order_data[4];
                        $utype=(isset($order_data[5]))?$order_data[5]:'';

                        $booking_data=$this->adm->get_admission_data(array('admission_code'=>$order__id));

                        //print_obj($booking_data);die;

                        if($utype=='stu'){
                          $stu_id=$booking_data->admission_stu_id;
                        }else if($utype=='stuinst'){
                          $stu_id=$booking_data->admission_stu_id;                      
                        }

                        $student_data=$this->um->get_user(array('user_id'=>$stu_id),'student');

                        $student_name=$student_data->stu_first_name.' '.$student_data->stu_mid_name.' '.$student_data->stu_last_name;
                        $student_email=$student_data->stu_email;
                        $student_ph=$student_data->stu_ph_no;
                        $payment_description='Admission installment fees';


                    }else if($order_prefix=='ORDHT'){
                        $order__id=$order_data[4];
                        $utype=(isset($order_data[5]))?$order_data[5]:'';

                        if($utype=='admfee'){
                            $hostel_admission_fees=$this->hm->__get_hostel_admission_fees_assign_data(array('assign_unique_payid'=>$order__id));

                            //print_obj($hostel_admission_fees);die;
                            $student_data=$this->um->get_user(array('user_id'=>$hostel_admission_fees->assign_stu_id),'student');

                            $student_name=$student_data->stu_first_name.' '.$student_data->stu_mid_name.' '.$student_data->stu_last_name;
                            $student_email=$student_data->stu_email;
                            $student_ph=$student_data->stu_ph_no;
                            $payment_amount=$hostel_admission_fees->admission_fees_value+$hostel_admission_fees->admission_caution_fees_value;
                            $payment_description='Hostel Admission fees';
                        }else if($utype=='stu'){
                            $hostel_fees_data=$this->fm->get_hostel_fees_payment(array('payment_unique_code'=>$order__id));

                            //print_obj($hostel_fees_data);die;

                            $student_data=$this->um->get_user(array('user_id'=>$hostel_fees_data->payment_hostel_stu_id),'student');

                            $student_name=$student_data->stu_first_name.' '.$student_data->stu_mid_name.' '.$student_data->stu_last_name;
                            $student_email=$student_data->stu_email;
                            $student_ph=$student_data->stu_ph_no;

                            $date=date('Y-m-d');
                            $due_date=$hostel_fees_data->due_at;

                            if($date>$due_date){
                                $payment_amount=$hostel_fees_data->payment_value+$hostel_fees_data->payment_late_value;
                            }else{
                                $payment_amount=$hostel_fees_data->payment_value;
                            }

                            $payment_description='Hostel Rent fees of '.ucwords($hostel_fees_data->payment_month).'-'.$hostel_fees_data->payment_year;
                        }else{
                            $hostel_fees_data=$this->fm->get_hostel_fees_payment(array('payment_id'=>$order__id));
                        }
                    }


                    //print_obj($gateway_settings);die;

                        
                    //print_obj($payment_details);die;

                    //$gateway_data=$this->sm->get_payment_gateway_settings(array('gateway_id'=>$gateway_settings->PAYMENT_GATEWAY));

                    if($payment_gateway=='PAYTM'){
                        //$payment_settings=$this->data['payment_settings'];
                        //print_obj($payment_settings);die;

                        if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='TEST'){
                            $param['mid']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_MID'];
                            $param['mkey']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_KEY'];
                            $param['industry_type_id']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_INDUSTRY_TYPE_ID'];
                            $param['channel_id']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_CHANNEL_ID'];
                            $param['mwebsite']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_WEBSITE'];
                            $param['txn_url']=PAYTM_TEST_TXN_URL;
                        }else if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='PROD'){
                            $param['mid']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_MID'];
                            $param['mkey']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_KEY'];
                            $param['industry_type_id']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_INDUSTRY_TYPE_ID'];
                            $param['channel_id']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_CHANNEL_ID'];
                            $param['mwebsite']=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_WEBSITE'];
                            $param['txn_url']=PAYTM_PROD_TXN_URL;
                        }

                        //print_obj($param);die;

                        //$cust_id=post_data('payment_user_code');

                        $cust_id=ge_rand_code('CUST');//str_replace('/', '|', $cust_id);


                        $param["txn_amount"]=$payment_amount;
                        $param["order_id"]=$payment_order_id.'_'.$payment_gateway.'_'.$inst_id;
                        $param["cust_id"]=$cust_id;
                        $param['callback']=$this->data['base_url'].'/payments_reciept_success';


                        

                        //$total_discount=$scholarship+$special_amount;

                        if($this->input->post('payment_scholarship_amount')){
                            $scholarship=post_data('payment_scholarship_amount');
                            $special_amount=post_data('payment_special_amount');
                            $scholarship_reason=post_data('payment_scholarship_reason');
                            $special_reason=post_data('payment_special_reason');
                            $checksum=post_data('checksum');

                            $total_discount=$scholarship+$special_amount;

                            $data_to_add=array(
                                'temp_checksum'=>$param["order_id"],
                                'temp_scholarship_discount'=>$scholarship,
                                'temp_payment_actual'=>$param["txn_amount"],
                                'temp_payment_total_discount'=>$total_discount,
                                'temp_special_discount'=>$special_amount,
                                'temp_scholarship_discount_reason'=>$scholarship_reason,
                                'temp_special_discount_reason'=>$special_reason
                            );

                            $this->fm->store_temp_payment($data_to_add);
                        }
                        
                        

                        $payment_procees_form=pgRedirect($param);

                        //print_obj($payment_procees_form);die;

                        echo $payment_procees_form;

                        //json_headers($return);
                    }else if($payment_gateway=='RAZORPAY'){

                        if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='TEST'){
                            $rz_mid=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_MID'];
                            $rz_key=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_KEY'];
                            $rz_key_secret=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_SECRET_KEY'];
                        }else if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='PROD'){
                            $rz_mid=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_MID'];
                            $rz_key=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_KEY'];
                            $rz_key_secret=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_SECRET_KEY'];
                        }

                        //$order_name=post_data('payment_name');

                        $reciept_no=ge_rand_code('RCPT');

                        // $rz_key='rzp_test_JOC0wRKpLH1cVW';

                        // $rz_key_secret='9EzSlxvJbTyQ2Hg0Us5ZX4VD';

                        $param=array(
                            'key'=>$rz_key,
                            'key_secret'=>$rz_key_secret,
                            'order_amount'=>$payment_amount,
                            'reciept_no'=>$reciept_no,
                            'order_name'=>$student_name,
                            'order_description'=>$payment_description,
                            'order_cust_email'=>$student_email,
                            'order_cust_ph'=>$student_ph,
                            'order_address'=>'',
                            'order_id'=>$payment_order_id,
                            'order_screen_color'=>'#212529',
                            'order_logo'=>$this->data['link_logo'],
                            'order_action_url'=>$this->data['base_url'].'/payments_reciept_success'
                        );

                        //print_obj($param);die;



                        $razorpayRedirect=$this->razorpay->rzRedirect($param);

                        //print_obj($razorpayRedirect);die;

                        //echo get_cookie('payment_gateway');die;

                        echo $razorpayRedirect;
                    }else if($payment_gateway=='CCAVENUE'){

                        if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='TEST'){
                            $cc_mid=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_MID'];
                            $cc_key=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_KEY'];
                            $cc_key_secret=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_TEST_MERCHANT_SECRET_KEY'];
                        }else if($gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE']=='PROD'){
                            $cc_mid=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_MID'];
                            $cc_key=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_KEY'];
                            $cc_key_secret=$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_PROD_MERCHANT_SECRET_KEY'];
                        }

                        $reciept_no=ge_rand_code('RCPT');

                        $param=array(
                            'mode'=>$gateway_settings[$payment_settings->settings_type_id]['PAYMENT_MODE'],                            
                            'key'=>$cc_key,
                            'key_secret'=>$cc_key_secret,
                            'tid'=>'1236547895',
                            'merchant_id'=>'1883831',
                            'order_id'=>$payment_order_id,
                            'amount'=>$payment_amount,
                            'currency'=>'INR',
                            'language'=>'EN',
                            'integration_type'=>'iframe_normal',
                            'cancel_url'=>$this->data['base_url'].'/payments_reciept_cancel',
                            'redirect_url'=>$this->data['base_url'].'/payments_reciept_success',
                            'reciept_no'=>$reciept_no,
                            'order_name'=>$student_name,
                            'order_description'=>$payment_description,
                            'order_cust_email'=>$student_email,
                            'order_cust_ph'=>$student_ph,
                            'order_address'=>'',                            
                            'order_screen_color'=>'#212529',
                            'order_logo'=>$this->data['link_logo']
                        );

                        //print_obj($param);die;



                        $ccavenueRedirect=$this->ccavenue->ccRedirect($param);

                        print_obj($ccavenueRedirect);die;

                        //echo get_cookie('payment_gateway');die;

                        echo $ccavenueRedirect;

                    }
                }else{
                    $return['error']='Payment settings not found';
                }
            }else{
                redirect($this->data['base_url']);
            }
        }else{
            redirect($this->data['base_url']);
        }
    }


    public function indexPaymentResponse(){
        header("Pragma: no-cache");
        header("Cache-Control: no-cache");
        header("Expires: 0");

        //print_obj($this->config);exit;

        //print_obj($_SERVER);

        //print_obj($_POST);die;

       //print_obj($_SESSION);die;

        //print_obj($_SESSION);die;


        if($this->input->get()){
            $payment_error=array(
                'payment_error'=>$this->input->get('errorMessage')
            );

            session_set_userdata($payment_error);

            $admin_type=session_userdata('admin_type');

            $booking_code=session_userdata('booking_code');

            $order_prefix = preg_replace("/[^A-Z]+/", "", $booking_code);

            if($admin_type==='institute' || $admin_type==='institute_branch'){

                $payment_details=$this->fm->get_payment_details(array('details_order_code'=>$booking_code));

                if(!empty($payment_details)){
                    $this->fm->update_payment_details(array('details_accounting_type'=>'not_settled','details_status'=>'error','details_gateway_status'=>'error','details_msg'=>$this->input->get('errorMessage')),array('details_order_code'=>$booking_code));
                }


                $redirect=$this->data['base_url'].'/admission/bookings';

                // if($order_prefix==='SB'){
                //     $redirect=$this->data['base_url'].'/admission/bookings?err='.session_userdata('booking_code');
                // }else{
                //     $redirect=$this->data['base_url'].'/admission/bookings?err='.session_userdata('booking_code');
                // }                
            }
            else{
                $redirect=$this->data['base_url'].'/account';
            }

            redirect($redirect);
        }else{
            if(isset($_POST['ORDERID'])){
                $exp_od=explode('_',$_POST['ORDERID']);
                $payment_gateway=$exp_od[2];
                $inst_id=$exp_od[3];
            }else{
                $payment_gateway=session_userdata('payment_gateway');
                $inst_id=$this->data['inst_id'];
            }

            //echo $payment_gateway;exit;

            if($payment_gateway){

                //print_obj($payment_gateway);die;

                //echo session_userdata('payment_gateway');die;

                $_payment_settings=$this->sm->get_system_settings(array('settings_inst_id'=>$inst_id,'settings_type_key'=>$payment_gateway,'settings_type'=>'payment_gateway'));

                //print_obj($_payment_settings);die;

                $gateway_settings=unserialize($_payment_settings->settings_value);

                $gatewaysettings_data=$gateway_settings[$_payment_settings->settings_type_id];

                if($payment_gateway=='PAYTM'){

                    //print_obj($_POST);

                    //[RESPCODE] => 01  success
                    //[RESPCODE] => 402  pending
                    //[RESPCODE] => 141 user cancelled

                    $order_id=$this->input->post('ORDERID');

                    $status=$this->input->post('STATUS');

                    $msg=$this->input->post('RESPMSG');

                    $txn_id=$this->input->post('TXNID');

                    $resp_code=$this->input->post('RESPCODE');

                    //echo $status;

                    //echo $msg;die;

                    if($gatewaysettings_data['PAYMENT_MODE']=='TEST'){
                        $mkey=$gatewaysettings_data['PAYMENT_TEST_MERCHANT_KEY'];
                        $mid=$gatewaysettings_data['PAYMENT_TEST_MERCHANT_MID'];
                        //$PAYTM_STATUS_QUERY_URL=$payment_settings['PAYTM_TEST_STATUS_QUERY_URL'];
                        $PAYTM_STATUS_QUERY_NEW_URL=PAYTM_TEST_STATUS_QUERY_NEW_URL;
                    }else if($gatewaysettings_data['PAYMENT_MODE']=='PROD'){
                        $mkey=$gatewaysettings_data['PAYMENT_PROD_MERCHANT_KEY'];
                        $mid=$gatewaysettings_data['PAYMENT_PROD_MERCHANT_MID'];
                        //$PAYTM_STATUS_QUERY_URL=$payment_settings['PAYTM_PROD_STATUS_QUERY_URL'];
                        $PAYTM_STATUS_QUERY_NEW_URL=PAYTM_PROD_STATUS_QUERY_NEW_URL;
                    }

                    $param['mkey']=$mkey;
                    $param['mid']=$mid;
                    //$param['paytm_status_query_url']=$PAYTM_STATUS_QUERY_URL;
                    $param['paytm_status_query_new_url']=$PAYTM_STATUS_QUERY_NEW_URL;

                    $payment_response=pgResponse($param);

                    //print_obj($payment_response);die;                 

                    if(!empty($payment_response)){

                        if($status==='TXN_SUCCESS'){
                            $order_id=$payment_response['ORDERID'];
                            $txn_amount=$payment_response['TXNAMOUNT'];
                            $txn_id=$payment_response['TXNID'];
                            $payment_id=$payment_response['BANKTXNID'];
                        }else{
                            $order_id=$order_id;
                            $txn_amount=$payment_response['TXNAMOUNT'];
                            $txn_id=$payment_response['TXNID'];
                            $payment_id='0';
                        }                        

                        //echo $order_id;die;

                        $order_exp_id=char_separated_to_array($order_id,'_');


                        $order_prefix = preg_replace("/[^A-Z]+/", "", $order_exp_id[0]);

                        //print_obj($order_exp_id);die;
                        

                        //print_obj($payment_data);die;

                        if($order_prefix==='SB'){
                            if($order_exp_id[1]==='stuinst'){
                                $payment_data=$this->fm->get_payment_details(array('details_order_id'=>$order_id));
                                //print_obj($payment_data);die;
                                $user_id_to_login=(!empty($payment_data))?$payment_data->details_inst_id:$order_exp_id[3];
                            }else if($order_exp_id[1]==='stu'){
                                $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));
                                $user_id_to_login=$booking_data->stu_user_id;
                            }                        
                        }else if($order_prefix==='ADM'){
                            if($order_exp_id[5]==='stuinst'){
                                $admission_data=$this->adm->get_admission_data(array('admission_code'=>$order_exp_id[0]));

                                //print_obj($admission_data);

                                $user_id_to_login=$admission_data->admission_inst_id;

                                //echo $user_id_to_login;die;
                            }else if($order_exp_id[5]==='stu'){
                                //$booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));

                                $admission_data=$this->adm->get_admission_data(array('admission_code'=>$order_exp_id[0]));

                                $user_id_to_login=$admission_data->stu_user_id;
                            }                            
                        }else if($order_prefix==='ORDHT'){
                            //print_obj($order_exp_id);die;

                            if($order_exp_id[5]==='stu'){
                                $booking_data=$this->fm->get_hostel_fees_payment(array('payment_unique_code'=>$order_exp_id[4]));
                                $user_id_to_login=$booking_data->payment_hostel_stu_id;
                            }
                        }

                        //echo $user_id_to_login;


                        $userdata = $this->um->_get_user(array('user_id'=>$user_id_to_login));

                        //print_obj($userdata);die;

                        $payment_session_data=array(
                            'isAdminLoggedin'=>true,
                            'admin_id'=>encode_data($userdata->user_id),
                            'admin_type'=>$userdata->user_type,
                            'loggedin_time'=>time(),
                            'payment_success'=>'Payment has been recieved.'
                        );

                        session_set_userdata($payment_session_data);

                        //print_obj($payment_data);

                        $paymant_param=array(
                            'order_id'=>$order_id,
                            'payment_platform'=>'paytm',
                            'payment_amount'=>$txn_amount,
                            'msg'=>$msg,
                            'gateway_status'=>strtolower($status),
                            'pay_id'=>$payment_id,
                            'txn_id'=>$txn_id,
                            'user_id_to_login'=>$user_id_to_login
                        );

                        //print_obj($paymant_param);die;

                        $this->get_payment_response($paymant_param);

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

                   
                }else if($payment_gateway=='RAZORPAY'){

                    //echo 'RAZORPAY 1';die;

                    //echo session_userdata('payment_gateway');
                    unset($_SESSION['payment_gateway']);

                    $payment_id=post_data('razorpay_payment_id');
                    $signature=post_data('razorpay_signature');
                    $order_id=post_data('razorpay_order_id');
                    $order_code=post_data('razorpay_order_code');

                    //echo $order_code;die;

                    $gateway_key=($gatewaysettings_data['PAYMENT_MODE']=='TEST')?$gatewaysettings_data['PAYMENT_TEST_MERCHANT_KEY']:$gatewaysettings_data['PAYMENT_PROD_MERCHANT_KEY'];

                    $gateway_key_secret=($gatewaysettings_data['PAYMENT_MODE']=='TEST')?$gatewaysettings_data['PAYMENT_TEST_MERCHANT_SECRET_KEY']:$gatewaysettings_data['PAYMENT_PROD_MERCHANT_SECRET_KEY'];               

                    $param=array(
                        'key'=>$gateway_key,
                        'key_secret'=>$gateway_key_secret,
                        'order_id'=>$order_id,
                        'order_payment_id'=>$payment_id,
                        'order_signature'=>$signature
                    );

                    //print_obj($param);

                    $payment_response=$this->razorpay->rzResponse($param);

                    $response=json_decode($payment_response);

                    //print_obj($response);die;

                    $order_exp_id=char_separated_to_array($order_code,'_');

                    $order_prefix = preg_replace("/[^A-Z]+/", "", $order_exp_id[0]);

                    // print_obj($order_exp_id);die;
                    

                    //print_obj($payment_data);die;

                    //print_obj($order_exp_id);die;

                    if($order_prefix==='SB'){
                        if($order_exp_id[1]==='stuinst'){
                            $payment_data=$this->fm->get_payment_details(array('details_order_code'=>$order_exp_id[0]));
                            $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));

                            //print_obj($payment_data);die;

                            $user_id_to_login=$payment_data->details_inst_id;
                            $txn_id='T'.date('YmdHis').$payment_data->details_inst_id.$payment_data->details_stu_id.$booking_data->booking_id.$payment_data->details_payment_id;
                        }else if($order_exp_id[1]==='stu'){
                            $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));
                            $user_id_to_login=$booking_data->stu_user_id;
                            $txn_id='T'.date('YmdHis').$booking_data->booking_inst_id.$booking_data->booking_stu_id.$booking_data->booking_pk_id.$booking_data->booking_payment_id;
                        }else if($order_exp_id[1]==='agt'){
                            $payment_data=$this->fm->_get_agent_payment(array('booking_code'=>$order_code));
                            $booking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$payment_data->booking_id));
                            $user_id_to_login=$payment_data->inst_id;
                            $txn_id='T'.date('YmdHis').$payment_data->inst_id.$payment_data->agent_id.$payment_data->booking_id.$payment_data->payment_id ;

                            //print_obj($payment_data);die;
                        }                    
                    }else if($order_prefix==='ADM'){
                        if(isset($order_exp_id[5]) && $order_exp_id[5]==='stuinst'){
                            $admission_data=$this->adm->get_admission_data(array('admission_code'=>$order_exp_id[0]));

                            //print_obj($admission_data);

                            $user_id_to_login=$admission_data->admission_inst_id;

                            $txn_id='T'.date('YmdHis').$admission_data->admission_inst_id.$admission_data->admission_stu_id.$admission_data->admission_id;

                            //echo $user_id_to_login;die;
                        }else if(isset($order_exp_id[5]) && $order_exp_id[5]==='stu'){
                            //$booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));

                            $admission_data=$this->adm->get_admission_data(array('admission_code'=>$order_exp_id[0]));

                            //print_obj($admission_data);die;

                            $user_id_to_login=$admission_data->admission_stu_id;

                            $txn_id='T'.date('YmdHis').$admission_data->admission_inst_id.$admission_data->admission_stu_id.$admission_data->admission_id;
                        }else if(isset($order_exp_id[1]) && $order_exp_id[1]==='agt'){
                            $payment_data=$this->fm->_get_agent_payment(array('booking_code'=>$order_code));
                            $booking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$payment_data->booking_id));
                            $user_id_to_login=$payment_data->inst_id;
                            $txn_id='T'.date('YmdHis').$payment_data->inst_id.$payment_data->agent_id.$payment_data->booking_id.$payment_data->payment_id ;

                            //print_obj($payment_data);die;
                        }
                            
                    }else if($order_prefix==='ORDHT'){
                        //print_obj($order_exp_id);die;

                        if($order_exp_id[5]==='stu'){
                            $booking_data=$this->fm->get_hostel_fees_payment(array('payment_unique_code'=>$order_exp_id[4]));
                            $user_id_to_login=$booking_data->payment_hostel_stu_id;
                            $txn_id='T'.date('YmdHis').$booking_data->payment_hostel_inst_id.$booking_data->payment_hostel_stu_id.$booking_data->payment_hostel_id.$booking_data->payment_id;
                        }else if($order_exp_id[5]==='admfee'){
                            $booking_data=$this->hm->get_hostel_admission_fees_assign_data(array('assign_unique_payid'=>$order_exp_id[4]));
                            $user_id_to_login=$booking_data->assign_stu_id;
                            $txn_id='T'.date('YmdHis').$booking_data->assign_hostel_inst_id.$booking_data->assign_stu_id.$booking_data->assign_hostel_id.$booking_data->assigned_id;
                            //print_obj($booking_data);
                        }
                    }

                    //die;

                    //print_obj($user_id_to_login);die;

                    //print_obj($response);die;

                    // if($order_exp_id[1]==='stuinst'){
                    //     $payment_data=$this->fm->get_payment_details(array('details_order_id'=>$order_code));

                    //     //print_obj($payment_data);die;

                    //     $user_id_to_login=$payment_data->details_inst_id;
                    // }else if($order_exp_id[1]==='stu'){
                    //     $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order_exp_id[0]));
                    //     $user_id_to_login=$booking_data->stu_user_id;
                    // }

                    $payment_response_check_param=array(
                        'key'=>$gateway_key,
                        'key_secret'=>$gateway_key_secret,
                        'fetch_type'=>'single',
                        'payment_id'=>$payment_id
                    );


                    $rzFetchPayments=$this->razorpay->rzFetchPayments($payment_response_check_param);

                    $payment_param=array(
                        'order_id'=>$order_code,
                        'payment_platform'=>'razorpay',
                        'payment_amount'=>$response->payment_response->payment_amount,
                        'msg'=>$response->payment_response->status,
                        'pay_status'=>$rzFetchPayments->status,
                        'pay_signature'=>$signature,
                        'pay_id'=>$payment_id,
                        'txn_id'=>$txn_id,
                        'pay_order_id'=>$rzFetchPayments->order_id,                    
                        'pay_currency'=>$rzFetchPayments->currency,
                        'pay_email'=>$rzFetchPayments->email,
                        'pay_contact'=>$rzFetchPayments->contact,
                        'pay_created_at'=>$rzFetchPayments->created_at,
                        'user_id_to_login'=>$user_id_to_login
                    );

                    //print_obj($payment_param);


                    $this->get_payment_response($payment_param);


                }

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


    protected function get_payment_response($param){

        //$loogeduserdata = $this->data['userdata'];

        //print_obj($param);

        $userdata = $this->um->_get_user(array('user_id'=>$param['user_id_to_login']));

       //print_obj($userdata);exit;

        if($userdata->user_type=='institute'){
            $parent_inst_id=$userdata->user_id;
        }else if($userdata->user_type=='institute_branch'){
            $inst_branch_data=$this->um->get_user(array('user_id'=>$userdata->user_id),'institute_branch');
            $parent_inst_id=$inst_branch_data->branch_parent_inst_id;
        }else if($userdata->user_type=='student'){
            $student_data=$this->um->get_user(array('user_id'=>$userdata->user_id),'student');

            // if($student_data->stu_inst_type==='institute'){
            //     $parent_inst_id=$userdata->stu_inst_parent_id;
            // }elseif($student_data->stu_inst_type==='institute_branch'){
            //     $parent_inst_id=$userdata->stu_inst_parent_id;
            // }

            $parent_inst_id=$student_data->stu_inst_id;
            
        }

        // $payment_session_data=array(
        //     'isAdminLoggedin'=>true,
        //     'admin_id'=>encode_data($userdata->user_id),
        //     'admin_type'=>$userdata->user_type,
        //     'loggedin_time'=>time()
        // );

        // //print_obj($session_data);

        // //echo $redirect_url;

        // //die;

         //session_set_userdata($payment_session_data);

        if(!empty($param['order_id'])){

            $_order_id=explode('_', $param['order_id']);

            //print_obj($_order_id);die;

            //print_obj($_order_id);

            $_fisrt_sec_order_id=$_order_id[0];

            $order_prefix = preg_replace("/[^A-Z]+/", "", $_fisrt_sec_order_id);
            $balance='0';

            //echo $order_prefix;die;

            if($order_prefix=='SB'){
                $order__id=$_order_id[0];
                $utype=$_order_id[1];
                //print_obj($_order_id);die;
                
                if($utype=='agt'){
                    $booking_type='agent_payment';
                    $booking_data=$this->fm->_get_agent_payment(array('booking_code'=>$param['order_id']));
                    $txn_amount=$booking_data->amount_value;
                    $booking_inst_id=$booking_data->inst_id;
                    $booking_inst_parent_id=$booking_data->inst_parent_id;
                    $session_id=$booking_data->session_id;
                    $course_id=$booking_data->course_id;
                    $subject_id=$booking_data->subject_id;
                }else{
                    $booking_type='seat_booking';
                    $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$order__id));                    
                    $txn_amount=$booking_data->booking_payment;

                    $booking_inst_id=$booking_data->booking_inst_id;
                    $booking_inst_parent_id=$booking_data->booking_inst_parent_id;
                    $session_id=$booking_data->booking_sess_id;
                    $course_id=$booking_data->booking_course_id;
                    $subject_id=$booking_data->booking_subject_id;
                }
                    
                if($utype=='stu'){
                    //$student_data=$this->um->get_user(array('user_id'=>$booking_data->booking_stu_id),'student');
                    //print_obj($student_data);die;
                    $user_id=$booking_data->stu_user_id;
                    $stu_id=$booking_data->stu_user_id;                    
                }else if($utype=='stuinst'){
                   $user_id=$booking_data->booking_inst_id;
                   $stu_id=$booking_data->stu_user_id;
                }else if($utype=='agt'){
                   $user_id=$booking_data->inst_id;
                   $agent_id=$booking_data->agent_id;
                   $stu_id=$booking_data->stu_id;
                }
                
                
                $booking_id=$booking_data->booking_id;

                if($utype=='stu'){
                    $redirect_url=$this->data['base_url'].'/account';
                }else if($utype=='stuinst'){
                    $redirect_url=$this->data['base_url'].'/admission/bookings?p='.$order__id;
                }else if($utype=='agt'){
                    $redirect_url=$this->data['base_url'].'/payments/agents/'.encode_data($agent_id);
                }
                
                
            }else if($order_prefix=='ADM'){                

                if(isset($_order_id[5])){
                    $utype=$_order_id[5];
                }else if(isset($_order_id[1])){
                    $utype=$_order_id[1];
                }else{
                    $utype='';
                }
                

                // print_obj($booking_data);die;

                $booking_type='new_admission';
                if($utype=='stu'){
                    $installemnt_unique_code=$_order_id[4];
                    $booking_data=$this->adm->get_admission_data(array('admission_code'=>$_fisrt_sec_order_id));                  
                    $details_payment_type='installment_fees';
                    $user_id=$booking_data->admission_stu_id;
                    $stu_id=$booking_data->admission_stu_id;
                    $admission_id=$booking_data->admission_id;
                    $session_id=$booking_data->admission_sess_id;
                    $course_id=$booking_data->admission_course_id;
                    $session_year=$_order_id[1];
                    $txn_amount=$param['payment_amount'];
                    $booking_id=$booking_data->admission_id;
                    $booking_inst_id=$booking_data->admission_inst_id;
                    $booking_inst_parent_id=$booking_data->admission_inst_parent_id;
                }else if($utype=='stuinst'){
                    $installemnt_unique_code=$_order_id[4];
                    $booking_data=$this->adm->get_admission_data(array('admission_code'=>$_fisrt_sec_order_id));
                    $details_payment_type='installment_fees';
                    $user_id=$booking_data->admission_inst_id; 
                    $stu_id=$booking_data->admission_stu_id;
                    $admission_id=$booking_data->admission_id;
                    $session_id=$booking_data->admission_sess_id;
                    $course_id=$booking_data->admission_course_id;
                    $session_year=$_order_id[1];
                    $txn_amount=$param['payment_amount'];
                    $booking_id=$booking_data->admission_id;
                    $booking_inst_id=$booking_data->admission_inst_id;
                    $booking_inst_parent_id=$booking_data->admission_inst_parent_id; 
                }else if($utype=='agt'){
                    $booking_data=$this->fm->_get_agent_payment(array('booking_code'=>$param['order_id']));
                    $admission_data=$this->adm->get_admission_data(array('admission_id'=>$booking_data->booking_id));
                    $details_payment_type='admission_agent_fees';
                    $user_id=$booking_data->inst_id;
                    $agent_id=$booking_data->agent_id;
                    $session_id=$booking_data->session_id;
                    $course_id=$booking_data->course_id;
                    $subject_id=$booking_data->subject_id;
                    $stu_id=$booking_data->stu_id;
                    $txn_amount=$booking_data->amount_value;
                    $booking_id=$booking_data->booking_id;
                    $booking_inst_id=$booking_data->inst_id;
                    $booking_inst_parent_id=$booking_data->inst_parent_id;
                    $details_payment_id=$booking_data->payment_id;
                    $details_stu_id=null;
                    $details_agent_id=$booking_data->agent_id;
                    $details_order_code=$admission_data->admission_code;


                }
                
                    
                if($utype=='stu'){
                    $redirect_url=$this->data['base_url'].'/account/installments';
                }else if($utype=='stuinst'){
                    $redirect_url=$this->data['base_url'].'/admission/payments/'.encode_data($booking_id).'?p='.$param['order_id']; 
                }else if($utype=='agt'){
                    $redirect_url=$this->data['base_url'].'/payments/agents/'.encode_data($agent_id);
                }

                //print_obj($redirect_url);die;
            }else if($order_prefix=='ORDHT'){
                $order__id=$_order_id[4];
                $utype=(isset($_order_id[5]))?$_order_id[5]:'';

                if($utype=='admfee'){
                    $hostel_admission_fees=$this->hm->__get_hostel_admission_fees_assign_data(array('assign_unique_payid'=>$order__id));

                    //print_obj($hostel_admission_fees);die;
                    $student_data=$this->um->get_user(array('user_id'=>$hostel_admission_fees->assign_stu_id),'student');

                    $student_name=$student_data->stu_first_name.' '.$student_data->stu_mid_name.' '.$student_data->stu_last_name;
                    $student_email=$student_data->stu_email;
                    $student_ph=$student_data->stu_ph_no;

                    $payment_amount=$hostel_admission_fees->admission_fees_value+$hostel_admission_fees->admission_caution_fees_value;

                    $txn_amount=$payment_amount;
                    $hostel_id=$hostel_admission_fees->assign_hostel_id;
                    $booking_type='hostel_fees_payment';
                    $user_id=$hostel_admission_fees->assign_stu_id;
                    $stu_id=$hostel_admission_fees->assign_stu_id;
                    $session_id=$hostel_admission_fees->assign_sess_id;
                    $course_id=$hostel_admission_fees->assign_course_id;
                    $booking_id=$hostel_admission_fees->assigned_id;
                    $booking_inst_id=$hostel_admission_fees->assign_hostel_inst_id;
                    $booking_inst_parent_id=$parent_inst_id;
                    $payment_description='Hostel Admission fees';
                    $redirect_url=$this->data['base_url'].'/account/hostels';
                }else if($utype=='stu'){
                    $hostel_fees_data=$this->fm->get_hostel_fees_payment(array('payment_unique_code'=>$order__id));

                    $date=date('Y-m-d');
                    $due_date=$hostel_fees_data->due_at;

                    if($date>$due_date){
                        $txn_amount=$hostel_fees_data->payment_value+$hostel_fees_data->payment_late_value;
                    }else{
                        $txn_amount=$hostel_fees_data->payment_value;
                    }

                    //print_obj($hostel_fees_data);
                    $hostel_id=$hostel_fees_data->payment_hostel_id;
                    $booking_type='hostel_fees_payment';
                    $user_id=$hostel_fees_data->payment_hostel_stu_id;
                    $stu_id=$hostel_fees_data->payment_hostel_stu_id;
                    $booking_due_date=$hostel_fees_data->due_at;
                    $session_id=$hostel_fees_data->payment_hostel_sess_id;
                    $course_id=$hostel_fees_data->payment_hostel_course_id;
                    $session_year=$_order_id[1];
                    $booking_id=$hostel_fees_data->payment_id;
                    $booking_inst_id=$hostel_fees_data->payment_hostel_inst_id;
                    $booking_inst_parent_id=$parent_inst_id;
                    $payment_description='Hostel rent fees of '.ucwords($hostel_fees_data->payment_month).'-'.$hostel_fees_data->payment_year;
                    $redirect_url=$this->data['base_url'].'/account/hostels';
                }else if($utype=='stuinst'){
                    echo 'hi';die;
                }
                
            }

            //echo $session_year;

            //echo $redirect_url;die;

            $course_data=$this->cm->get_course(array('course_id'=>$course_id));
            $session_data=$this->sessm->get_session(array('session_id'=>$session_id));

            ///print_obj($session_data);die;


            
            //echo $redirect_url;
            //print_obj($userdata);die;

            //print_obj($userdata);

            

            // print_obj($_SESSION);die;

            if($userdata->user_type=='institute'){
                $inst_id=$userdata->user_id;
                $student_data=$this->um->get_user(array('user_id'=>$stu_id),'student');
                $institute_data=$this->um->get_user(array('user_id'=>$inst_id),'institute');
                $institute_profile_name=$institute_data->user_profile_name;
                $institute_email=$institute_data->user_email;
                $institute_phone_no=$institute_data->user_contact_no;
            }else if($userdata->user_type=='institute_branch'){
                $inst_id=$userdata->user_id;

                $student_data=$this->um->get_user(array('user_id'=>$stu_id),'student');
                $institute_data=$this->um->get_user(array('user_id'=>$inst_id),'institute_branch');
                $institute_profile_name=$institute_data->branch_name;
                $institute_email=$institute_data->branch_email;
                $institute_phone_no=$institute_data->branch_contact_no;

            }else if($userdata->user_type=='agent'){
                $inst_id=$userdata->user_id;
            }else if($userdata->user_type=='student'){
                $student_data=$this->um->get_user(array('user_id'=>$userdata->user_id),'student');
                $inst_id=$student_data->stu_inst_id;
                $institute_data=$this->um->get_user(array('user_id'=>$inst_id),'institute');
                $institute_profile_name=$institute_data->user_profile_name;
                $institute_email=$institute_data->user_email;
                $institute_phone_no=$institute_data->user_contact_no;
            }else{
                $inst_id='';
            }


            $_payment_settings=$this->sm->get_system_settings(array('settings_inst_id'=>$inst_id));

            if($order_prefix=='SB'){

                if($_order_id[1]=='agt'){
                    $this->fm->update_agent_payment(array('amount_paid'=>'yes','booking_pay_mode'=>'online','amount_paid_at'=>date('Y-m-d H:i:s'),'amount_paid_by'=>$param['user_id_to_login']),array('booking_code'=>$param['order_id'],'agent_id'=>$agent_id));

                    $details_payment_type='seatbooking_agent_fees';
                    $details_payment_id=$booking_data->payment_id;
                    $details_stu_id=$booking_data->stu_id;
                    $details_agent_id=$agent_id;
                    $details_accounting_type='expenditure';
                    $details_order_code=$param['order_id'];
                    $payment_details_param=array('details_order_code'=>$_fisrt_sec_order_id,'details_inst_id'=>$booking_inst_id,'details_agent_id'=>$agent_id,
                        'details_type_id'=>$booking_id,
                        'details_sess_id'=>$session_id,
                        'details_course_id'=>$course_id);
                }else{
                    $this->adm->update_seat_booking_payment_data(array('booking_payment_paid'=>'yes','booking_pay_mode'=>'online'),array('booking_pk_id'=>$booking_id));


                    //$this->adm->update_seat_booking_data(array('booking_converted'=>'yes','updated_at'=>date('Y-m-d'),'updated_by'=>$user_id),array('booking_code'=>$order__id));

                    // if($param['payment_platform']=='razorpay'){
                    //     $txn_id='T'.date('YmdHis').$inst_id.$user_id.$booking_id.$booking_data->booking_payment_id;
                    // }else if($param['payment_platform']=='paytm'){
                    //     $txn_id=$param['txn_id'];
                    // }

                    $details_payment_type='seatbooking_fees';
                    $details_payment_id=$booking_data->booking_payment_id;
                    $details_stu_id=$student_data->stu_user_id;
                    $details_agent_id=null;
                    $details_accounting_type='income';
                    $details_order_code=$_fisrt_sec_order_id;

                    $payment_details_param=array('details_order_code'=>$_fisrt_sec_order_id,'details_inst_id'=>$booking_inst_id,'details_stu_id'=>$student_data->stu_user_id,
                        'details_type_id'=>$booking_id,
                        'details_sess_id'=>$session_id,
                        'details_course_id'=>$course_id);
                }

                $payment_details=array(
                    'details_type'=>$booking_type,
                    'details_payment_type'=>$details_payment_type,
                    'details_payment_id'=>$details_payment_id,
                    'details_stu_id'=>$details_stu_id,
                    'details_agent_id'=>$details_agent_id,
                    'details_type_id'=>$booking_id,
                    'details_sess_id'=>$session_id,
                    'details_course_id'=>$course_id,
                    'details_subject_id'=>$subject_id,
                    'details_payment_platform'=>$param['payment_platform'],
                    'details_inst_id'=>$booking_inst_id,                                
                    'details_parent_inst_id'=>$booking_inst_parent_id,
                    'details_txn_id'=>$param['txn_id'],
                    'details_pay_id'=>(isset($param['pay_id']))?$param['pay_id']:null,
                    'details_pay_signature'=>(isset($param['pay_signature']))?$param['pay_signature']:null,
                    'details_pay_order_id'=>(isset($param['pay_order_id']))?$param['pay_order_id']:null,
                    'details_pay_currency'=>(isset($param['pay_currency']))?$param['pay_currency']:null,
                    'details_payee_email'=>(isset($param['pay_email']))?$param['pay_email']:null,
                    'details_payee_contact'=>(isset($param['pay_contact']))?$param['pay_contact']:null,
                    'details_gateway_created_at'=>(isset($param['pay_created_at']))?$param['pay_created_at']:null,
                    'details_gateway_status'=>(isset($param['pay_status']))?$param['pay_status']:null,
                    'details_order_id'=>$param['order_id'],
                    'details_order_code'=>$details_order_code,
                    'details_amount'=>$txn_amount,
                    'details_discount_amount'=>'0',
                    'details_created_by_type'=>($utype=='stu')?'student':'institute',
                    'details_accounting_type'=>$details_accounting_type,
                    'details_created_by'=>$user_id,
                    'details_gateway_status'=>$param['gateway_status'],
                    'details_msg'=>$param['msg'],
                    'details_status'=>'success',
                    'details_created_at'=>date('Y-m-d'),
                    'details_gateway_created_at'=>''
                );


                //print_obj($payment_details);die;

                $paymentdetails=$this->fm->_get_payment_details($payment_details_param);

                if(!empty($paymentdetails)){
                    $this->fm->update_payment_details($payment_details,array('details_id'=>$paymentdetails->details_id));
                }else{
                    $this->fm->store_payment_details($payment_details);
                }

               
               //mail Section

               $session_course_data=$this->sessm->get_session_courses(array('session_id'=>$session_id,'session_course_id'=>$course_id,'session_inst_id'=>$booking_inst_id));


                $mail_subject='Seat Booking Payment for booking no :'.$booking_data->booking_code;

                $mail_data['inst_id']=$booking_inst_id;
                $mail_data['mail_from']='webmaster';

                if(isset($student_data) && !empty($student_data)){

                    if(!empty($booking_data->stu_mid_name)){
                        $student_name=ucwords(strtolower($student_data->stu_first_name)).' '.ucwords(strtolower($student_data->stu_mid_name)).' '.ucwords(strtolower($student_data->stu_last_name));
                    }else{
                        $student_name=ucwords(strtolower($student_data->stu_first_name)).' '.ucwords(strtolower($student_data->stu_last_name));
                    }


                    $mail_data['to_email']=$booking_data->stu_email;
                }

                if($userdata->user_type=='student'){
                    $mail_data['mail_from_name']=$institute_profile_name;

                    $mail_data['mail_subject']=$mail_subject;
                    
                    $profile_name=$institute_profile_name;
                    $college_email=$institute_email;
                    $college_phone=$institute_phone_no;
                    $link_logo=$this->data['link_logo'];
                }else{

                    //print_obj($institute_data);die;

                    $mail_data['mail_from_name']=$institute_data->user_profile_name;
                    $mail_data['mail_subject']=$mail_subject;
                    $profile_name=$institute_data->user_profile_name;
                    $college_email=$institute_data->user_email;
                    $college_phone=$institute_data->user_contact_no;
                    $link_logo=$this->data['link_logo'];

                    //print_obj($mail_data);die;
                }
                
                
                $mail_data['mail_type']='html';
                $mail_data['mail_custom_type']='html';
                $mail_data['mail_data']=array(
                    'stu_name'=>ucwords($student_name),
                    'stu_email'=>$student_data->stu_email,
                    'stu_ph'=>$student_data->stu_ph_no,
                    'booking_code'=>$booking_data->booking_code,
                    'course_fees'=>(!empty($session_course_data))?number_format($session_course_data->session_course_fees,2):'',
                    'booking_data'=>$booking_data,
                    'payment_details'=>$payment_details,
                    'session_data'=>$session_data->session_start_year.'-'.$session_data->session_end_year,
                    'portal_url'=>$this->data['base_url'],
                    'portal_logo'=>$link_logo,
                    'portal_inst_name'=>$profile_name,
                    'college_email'=>$college_email,
                    'college_phone'=>$college_phone,
                    'default_logo_small'=>$this->data['default_logo_small'],
                    'created_at'=>date('d-m-Y')
                );


                //print_obj($mail_data);die;

                
                $mail_data['mail_view']='_pages/mails/vw_seatbook_payment_mail';

                $this->onSendMail($mail_data);

                $mail_data_2=$mail_data;

                $mail_data_2['to_email']=$institute_email;
                $mail_data_2['mail_subject']=$mail_subject.' [Copy mail]';
                
                $this->onSendMail($mail_data_2);

                // unset($_SESSION['payment_gateway']);


                redirect($redirect_url);

            }else if($order_prefix=='ADM'){

                if($_order_id[1]=='agt'){

                    if($param['payment_platform']=='razorpay'){
                        $txn_id='T'.date('YmdHis').$inst_id.$user_id.$booking_id.$booking_data->admission_id;
                    }else if($param['payment_platform']=='paytm'){
                        $txn_id=$param['txn_id'];
                        $temp_payment_data=$this->fm->get_temp_payment(array('temp_checksum'=>$param['order_id']));
                    }

                    $this->fm->update_agent_payment(array('amount_paid'=>'yes','booking_pay_mode'=>'online','amount_paid_at'=>date('Y-m-d H:i:s'),'amount_paid_by'=>$user_id),array('booking_code'=>$param['order_id'],'agent_id'=>$agent_id));

                    $payment_details=array(
                        'details_type'=>$booking_type,
                        'details_payment_type'=>$details_payment_type,
                        'details_payment_id'=>$details_payment_id,
                        'details_stu_id'=>$details_stu_id,
                        'details_agent_id'=>$details_agent_id,
                        'details_type_id'=>$booking_id,
                        'details_sess_id'=>$session_id,
                        'details_course_id'=>$course_id,
                        'details_subject_id'=>$subject_id,
                        'details_payment_platform'=>$param['payment_platform'],
                        'details_inst_id'=>$booking_inst_id,                                
                        'details_parent_inst_id'=>$booking_inst_parent_id,
                        'details_txn_id'=>$param['txn_id'],
                        'details_pay_id'=>(isset($param['pay_id']))?$param['pay_id']:null,
                        'details_pay_signature'=>(isset($param['pay_signature']))?$param['pay_signature']:null,
                        'details_pay_order_id'=>(isset($param['pay_order_id']))?$param['pay_order_id']:null,
                        'details_pay_currency'=>(isset($param['pay_currency']))?$param['pay_currency']:null,
                        'details_payee_email'=>(isset($param['pay_email']))?$param['pay_email']:null,
                        'details_payee_contact'=>(isset($param['pay_contact']))?$param['pay_contact']:null,
                        'details_gateway_created_at'=>(isset($param['pay_created_at']))?$param['pay_created_at']:null,
                        'details_gateway_status'=>(isset($param['pay_status']))?$param['pay_status']:null,
                        'details_order_id'=>$param['order_id'],
                        'details_order_code'=>$details_order_code,
                        'details_amount'=>$txn_amount,
                        'details_discount_amount'=>'0',
                        'details_created_by_type'=>($utype=='stu')?'student':'institute',
                        'details_accounting_type'=>'expenditure',
                        'details_created_by'=>$user_id,
                        'details_gateway_status'=>$param['gateway_status'],
                        'details_msg'=>$param['msg'],
                        'details_status'=>'success',
                        'details_created_at'=>date('Y-m-d'),
                        'details_gateway_created_at'=>''
                    );


                    //print_obj($payment_details);die;

                    $paymentdetails=$this->fm->_get_payment_details(array('details_order_code'=>$details_order_code,'details_inst_id'=>$booking_inst_id,'details_agent_id'=>$agent_id,'details_type_id'=>$booking_id,'details_sess_id'=>$session_id,'details_course_id'=>$course_id));

                    if(!empty($paymentdetails)){
                         $this->fm->update_payment_details($payment_details,array('details_id'=>$paymentdetails->details_id));
                    }else{
                        $this->fm->store_payment_details($payment_details);
                    }


                    redirect($redirect_url);

                }else{
                    $this->adm->update_admission_data(array('updated_at'=>date('Y-m-d'),'updated_by'=>$user_id),array('admission_id'=>$booking_id));

                    if($param['payment_platform']=='razorpay'){
                        $txn_id='T'.date('YmdHis').$inst_id.$user_id.$booking_id.$booking_data->admission_id;
                    }else if($param['payment_platform']=='paytm'){
                        $txn_id=$param['txn_id'];
                        $temp_payment_data=$this->fm->get_temp_payment(array('temp_checksum'=>$param['order_id']));
                    }

                    $total_fees_value=0;

                    if(isset($_order_id[4])){
                        $installment_data=$this->fm->get_installment(array('installment_unique_code'=>$_order_id[4]));
                        $installment_id=$installment_data->installment_id;
                        $installment_no=$_order_id[2];
                        $details_order_code=$_fisrt_sec_order_id.$_order_id[4];
                        //echo $installment_id;die;
                    }else{
                        $installment_no=$_order_id[2];
                        $details_order_code=$_fisrt_sec_order_id.$_order_id[4];
                        $year_data=$this->sessm->get_session_year(array('year_value'=>$session_year,'year_session_id'=>$session_id,'year_session_inst_id'=>$booking_inst_id));

                        $fees_types=$this->fm->__get_fees_type(array('fees_inst_id'=>$booking_inst_id,'fees_session_id'=>$session_id,'fees_course_id'=>$course_id,'fees_year_id'=>$year_data->year_id));

                        // print_obj($fees_types);die;

                        if(!empty($fees_types)){


                            foreach ($fees_types as $k => $v) {

                                $_get_fees_details=$this->fm->_get_fees_details(array('payment_stu_id'=>$stu_id,
                                    'payment_inst_id'=>$booking_inst_id,
                                    'payment_admission_id'=>$admission_id,
                                    'payment_sess_id'=>$session_id,
                                    'payment_course_id'=>$course_id,
                                    'payment_fees_type_id'=>$v->fees_type_id,
                                    'payment_fees_value'=>$v->fees_value,
                                    'payment_sess_year'=>$session_year));

                                if(empty($_get_fees_details)){
                                    $total_fees_value+=$v->fees_value;
                                    $fees_type_data=array(
                                        'payment_stu_id'=>$stu_id,
                                        'payment_inst_id'=>$booking_inst_id,
                                        'payment_admission_id'=>$admission_id,
                                        'payment_sess_id'=>$session_id,
                                        'payment_course_id'=>$course_id,
                                        'payment_fees_type_id'=>$v->fees_type_id,
                                        'payment_fees_value'=>$v->fees_value,
                                        'payment_sess_year'=>$session_year,
                                        'created_by'=>$user_id,
                                        'created_at'=>date('Y-m-d')
                                    );

                                    $this->fm->_store_fees_details($fees_type_data,TRUE);
                                }

                                    
                            }

                            //print_obj($fees_type_data);die;

                            
                        }

                        $get_installments=$this->fm->get_installments(array('installment_stu_id'=>$stu_id,
                            'installment_inst_id'=>$booking_inst_id,
                            'installment_no'=>$installment_no,
                            'installment_admission_id'=>$admission_id,
                            'installment_sess_id'=>$session_id,
                            'installment_sess_year'=>$session_year,
                            'installment_course_id'=>$course_id));

                        //print_obj($get_installments);die;

                        if(!empty($get_installments)){
                            $installment_id=$get_installments->installment_id;
                        }else{
                            $installment_data_to_add=array(
                                'installment_stu_id'=>$stu_id,
                                'installment_inst_id'=>$booking_inst_id,
                                'installment_admission_id'=>$admission_id,
                                'installment_sess_id'=>$session_id,
                                'installment_sess_year'=>$session_year,
                                'installment_course_id'=>$course_id,
                                'installment_value'=>$txn_amount,
                                'installment_date'=>date('Y-m-d'),
                                'created_by'=>$user_id,
                                'created_at'=>date('Y-m-d')
                            );

                            $installment_id=$this->fm->store_installments($installment_data_to_add);
                        }
                        
                    }

                    //echo $installment_id;  

                    if($installment_id){

                        $installment_data=$this->fm->get_installments(array('installment_id'=>$installment_id),TRUE);

                        //print_obj($installment_data);die;

                        $_get_fees_details_last=$this->fm->_get_fees_details_last(array('payment_stu_id'=>$installment_data->installment_stu_id,'payment_inst_id'=>$installment_data->installment_inst_id,'payment_installment_id'=>$installment_data->installment_id));

                        //$balance=$this->fm->get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id));

                        if(!empty($_get_fees_details_last)){
                             // $balance_last_data=$this->fm->get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id,'inst_payment_id'=>$_get_fees_details_last->payment_fees_details_id));

                            $balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id,'inst_payment_id'=>$_get_fees_details_last->payment_fees_details_id));
                        }else{
                             // $balance_last_data=$this->fm->get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id));

                            $balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id));
                        }

                        //print_obj($balance_last_data);die;



                        //$balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$installment_data->installment_id,'inst_stu_id'=>$installment_data->installment_stu_id,'inst_session_id'=>$installment_data->installment_sess_id),TRUE);

                       // print_obj($balance_last_data);die;

                        $date_now = date('Y-m-d');// new DateTime();
                        $installment_date    = date('Y-m-d',strtotime($installment_data->installment_date));//new DateTime($installment_data->installment_date);

                        // print_obj($date_now);
                        // echo '<br>';
                        // print_obj($installment_date);

                        // if($date_now>$installment_date){
                        //     echo 'Date now is greater';
                        // }else if($date_now==$installment_date){
                        //     echo 'date now is equal';
                        // }else{
                        //     echo 'Date now is smaller';
                        // }

                        // die;

                        if(!empty($balance_last_data) && $balance_last_data->inst_payment_balance>0){
                            if($date_now>$installment_date){
                                $penalty_applied=TRUE;  
                            }else if($date_now==$installment_date){
                                $penalty_applied=FALSE;
                            }else{
                                $penalty_applied=FALSE;
                            } 
                            //echo '1';
                            $balance=$balance_last_data->inst_payment_balance-$txn_amount;
                        }else{
                            if($date_now>$installment_date){
                                $penalty_applied=TRUE;
                                $total_due=$installment_data->installment_value+$installment_data->installment_value_penalty;  
                            }else if($date_now==$installment_date){
                                $penalty_applied=FALSE;
                                $total_due=$installment_data->installment_value;
                            }else{
                                $penalty_applied=FALSE;
                                $total_due=$installment_data->installment_value;
                            }                                    

                            if(!empty($_get_fees_details_last)){
                                if(isset($_get_fees_details_last->inst_payment_balance) && $_get_fees_details_last->inst_payment_balance>0){
                                    //echo '2';
                                    $balance=$_get_fees_details_last->inst_payment_balance-$txn_amount;
                                }else{
                                    //echo '3';
                                    $balance=$total_due-$txn_amount;
                                }                                        
                            }else{
                                //echo '4';
                                $balance=$total_due-$txn_amount;
                            }                                    
                        }

                        //echo $balance;die;
                        

                        $pay_data_to_add=array(
                            'payment_stu_id'=>$stu_id,
                            'payment_inst_id'=>$booking_inst_id,
                            'payment_session_id'=>$session_id,
                            'payment_session_year'=>$session_year,
                            'payment_admission_id'=>$admission_id,
                            'payment_course_id'=>$course_id,
                            'payment_installment_id'=>$installment_id,
                            'payment_fees_total'=>$total_fees_value,
                            'payment_fees_scholarship_discount'=>(!empty($temp_payment_data))?$temp_payment_data->temp_scholarship_discount:'0',
                            'payment_fees_scholarship_discount_reason'=>(!empty($temp_payment_data))?$temp_payment_data->temp_scholarship_discount_reason:null,
                            'payment_fees_special_discount'=>(!empty($temp_payment_data))?$temp_payment_data->temp_special_discount:'0',
                            'payment_fees_special_discount_reason'=>(!empty($temp_payment_data))?$temp_payment_data->temp_special_discount_reason:null,
                            'payment_fees_total_discount'=>(!empty($temp_payment_data))?$temp_payment_data->temp_payment_total_discount:'0',
                            'payment_fees_grand_total'=>$total_fees_value,
                            'payment_fees_admission_amount'=>$txn_amount,
                            'payment_fees_balance_amount'=>$balance,
                            'payment_late_fee_applied'=>($penalty_applied==TRUE)?'yes':'no',
                            'payment_late_fee'=>$installment_data->installment_value_penalty,
                            'pay_mode'=>'online',
                            'pay_bank_cheque_no'=>null,
                            'pay_bank_name'=>null,
                            'pay_deposit_bank'=>null,
                            'created_by'=>$user_id,
                            'created_at'=>date('Y-m-d')
                        );

                        //print_obj($pay_data_to_add);die;

                        $payment_id=$this->fm->store_fees_details($pay_data_to_add);

                        if($payment_id){

                            if($balance>0){

                                $balance_data=array(
                                    'inst_id'=>$booking_inst_id,
                                    'inst_parent_id'=>$booking_inst_parent_id,
                                    'inst_stu_id'=>$stu_id,
                                    'inst_session_id'=>$session_id,
                                    'inst_course_id'=>$course_id,
                                    'inst_subject_id'=>$installment_data->installment_subject_id,
                                    'inst_year'=>$session_year,
                                    'inst_installment_id'=>$installment_id,
                                    'inst_payment_id'=>$payment_id,
                                    'inst_payment_total'=>$txn_amount,
                                    'inst_payment_balance'=>$balance,
                                    'created_at'=>date('Y-m-d'),
                                    'created_by'=>$user_id
                                );

                                $this->fm->store_installments_balance($balance_data);
                            }else if($balance==0){

                                //print_obj($balance_last_data);die;

                                if(!empty($balance_last_data)){
                                    $this->fm->update_installments_balance(array('inst_balance_paid'=>'yes'),array('balance_id'=>$balance_last_data->balance_id));
                                        $balance_data=array(
                                            'inst_id'=>$booking_inst_id,
                                            'inst_parent_id'=>$booking_inst_parent_id,
                                            'inst_stu_id'=>$stu_id,
                                            'inst_session_id'=>$session_id,
                                            'inst_course_id'=>$course_id,
                                            'inst_subject_id'=>$installment_data->installment_subject_id,
                                            'inst_year'=>$session_year,
                                            'inst_installment_id'=>$installment_id,
                                            'inst_payment_id'=>$payment_id,
                                            'inst_payment_total'=>$txn_amount,
                                            'inst_payment_balance'=>$balance,
                                            'inst_balance_paid'=>'yes',
                                            'created_at'=>date('Y-m-d'),
                                            'created_by'=>$user_id
                                        );

                                        $this->fm->store_installments_balance($balance_data);

                                        $this->fm->update_installments_balance(array('inst_balance_paid'=>'yes'),array('inst_id'=>$booking_inst_id,
                                            'inst_parent_id'=>$booking_inst_parent_id,
                                            'inst_stu_id'=>$stu_id,
                                            'inst_session_id'=>$session_id,
                                            'inst_course_id'=>$course_id,
                                            'inst_subject_id'=>$installment_data->installment_subject_id,
                                            'inst_year'=>$session_year,
                                            'inst_installment_id'=>$installment_id));
                                }


                                $this->fm->update_installments(array('installment_paid_full'=>'yes'),array('installment_id'=>$installment_id));
                            }


                            if(isset($order_id_exp[6]) && $order_id_exp[6]=='bal'){
                                $details_payment_type='installment_balance_fees';
                            }else{
                                $details_payment_type='installment_fees';
                            }
                                                                

                            $payment_details=array(
                                'details_type_text'=>'Admission ['.$booking_data->admission_code.'] Payment Details',
                                'details_order_code'=>$_fisrt_sec_order_id,
                                'details_payment_platform'=>$param['payment_platform'],
                                'details_type'=>$booking_type,
                                'details_payment_type'=>$details_payment_type,
                                'details_type_id'=>$booking_id,
                                'details_payment_id'=>$payment_id,
                                'details_inst_id'=>$booking_inst_id,
                                'details_parent_inst_id'=>$booking_inst_parent_id,
                                'details_stu_id'=>$student_data->stu_user_id,
                                'details_sess_id'=>$session_id,
                                'details_course_id'=>$course_id,
                                'details_sess_year'=>$session_year,
                                'details_installment_id'=>$installment_id,
                                'details_txn_id'=>$txn_id,
                                'details_order_id'=>$param['order_id'],
                                'details_order_code'=>$details_order_code,
                                'details_pay_id'=>(isset($param['pay_id']))?$param['pay_id']:null,
                                'details_amount'=>$txn_amount,
                                'details_discount_amount'=>(!empty($temp_payment_data))?$temp_payment_data->temp_payment_total_discount:'0',
                                'details_prev_total_amount'=>0,
                                'details_accounting_type'=>'income',
                                'details_created_by_type'=>$userdata->user_type,
                                'details_created_by'=>$user_id,
                                'details_status'=>'success',
                                'details_msg'=>$param['msg'],
                                'details_created_at'=>date('Y-m-d')
                            );

                            //print_obj($payment_details);die;

                            // $paymentdetails=$this->fm->_get_payment_details(array('details_order_code'=>$_fisrt_sec_order_id,'details_inst_id'=>$booking_inst_id,'details_stu_id'=>$user_id,
                            //     'details_type_id'=>$booking_id,
                            //     'details_sess_id'=>$session_id,
                            //     'details_course_id'=>$course_id));

                            // if(!empty($paymentdetails)){
                            //     $this->fm->update_payment_details($payment_details,array('details_id'=>$paymentdetails->details_id));
                            //     $payment_details_id=$paymentdetails->details_id;
                            // }else{
                            //     $payment_details_id=$this->fm->store_payment_details($payment_details);
                            // }

                            $payment_details_id=$this->fm->store_payment_details($payment_details);

                            $prev_payment_data=$this->fm->get_payment_details(array('details_stu_id'=>$stu_id,'details_sess_id'=>$session_id,'details_course_id'=>$course_id,'details_sess_year'=>$session_year,'details_installment_id'=>$installment_id),FALSE);

                            //print_obj($prev_payment_data);die;

                            if(!empty($prev_payment_data) && count($prev_payment_data)>1){
                                $prev_total=$this->fm->_get_payments_total(array('details_stu_id'=>$stu_id,'details_sess_id'=>$session_id,'details_course_id'=>$course_id,'details_sess_year'=>$session_year,'details_installment_id'=>$installment_id,'details_id!='=>$payment_details_id));
                                $_prev_total=(!empty($prev_total) && isset($prev_total[0]->total))?$prev_total[0]->total:0;
                                $details_prev_total_amount=$_prev_total;//+$txn_amount;
                            }else{
                                $details_prev_total_amount=0;
                            }

                            $this->fm->update_payment_details(array('details_prev_total_amount'=>$details_prev_total_amount),array('details_id'=>$payment_details_id));

                            $this->fm->delete_temp_payment(array('temp_checksum'=>$param['order_id']));


                            $payment_details=$this->fm->get_payment_details(array('details_id'=>$payment_details_id));

                            //print_obj($payment_details);die;

                            if(!empty($payment_details)){
                                $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$payment_id));


                                //print_obj($this->data);die;

                                $mail_data['inst_id']=$booking_inst_id;
                                $mail_data['mail_from']='webmaster';

                                if(isset($student_data) && !empty($student_data)){

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


                                    $mail_data['to_email']=$student_data->stu_email;
                                }

                                if($userdata->user_type=='student'){
                                    $mail_data['mail_from_name']=$institute_profile_name;

                                    if($payment_details->details_payment_type=='installment_balance_fees'){
                                        $mail_data['mail_subject']='Admission ['.$booking_data->admission_code.'] Balance Payment Details';
                                    }else{
                                        $mail_data['mail_subject']='Admission ['.$booking_data->admission_code.'] Payment Details';
                                    }


                                    
                                    $profile_name=$institute_profile_name;
                                    $college_email=$institute_email;
                                    $college_phone=$institute_phone_no;
                                    $link_logo=$this->data['link_logo'];
                                }else{
                                    $mail_data['mail_from_name']=$institute_profile_name;
                                    $mail_data['mail_subject']=ucwords($institute_profile_name).' Admission ['.$booking_data->admission_code.'] Details';
                                    $profile_name=$institute_profile_name;
                                    $college_email=$institute_email;
                                    $college_phone=$institute_phone_no;
                                    $link_logo=$this->data['link_logo'];
                                }


                                ///print_obj($session_data);die;
                                
                                
                                $mail_data['mail_type']='html';
                                $mail_data['mail_custom_type']='html';
                                $mail_data['mail_data']=array(
                                    'stu_name'=>ucwords($student_name),
                                    'stu_email'=>$student_data->stu_email,
                                    'stu_ph'=>$student_data->stu_ph_no,
                                    'booking_code'=>$booking_data->admission_code,
                                    'booking_session'=>$session_data->session_start_year.'-'.$session_data->session_end_year,
                                    'booking_course'=>$course_data->course_name,
                                    'booking_subject'=>(!empty($subject_data))?$subject_data->subject_name:'N/A',
                                    'booking_amount'=>'0',
                                    'installment_data'=>$installment_data,
                                    'payment_data'=>$payment_data,
                                    'payment_details'=>$payment_details,
                                    'portal_url'=>$this->data['base_url'],
                                    'portal_logo'=>$link_logo,
                                    'portal_inst_name'=>$profile_name,
                                    'college_email'=>$college_email,
                                    'college_phone'=>$college_phone,
                                    'default_logo_small'=>$this->data['default_logo_small'],
                                    'created_at'=>date('d-m-Y')
                                );

                                
                                $mail_data['mail_view']='_pages/mails/vw_admission_installments_payment_mail';

                                $this->onSendMail($mail_data);

                                $mail_data_2=$mail_data;

                                $mail_data_2['to_email']=$institute_email;
                                if($payment_details->details_payment_type=='installment_balance_fees'){
                                    $mail_data_2['mail_subject']='Admission ['.$booking_data->admission_code.'] Balance Payment Details [Copy mail]';
                                }else{
                                    $mail_data_2['mail_subject']='Admission ['.$booking_data->admission_code.'] Payment Details [Copy mail]';
                                }
                                
                                $this->onSendMail($mail_data_2); 

                                //unset($_SESSION['payment_gateway']);


                                // $session_data=array(
                                //     'isAdminLoggedin'=>true,
                                //     'admin_id'=>encode_data($user_id),
                                //     'admin_type'=>$userdata->user_type,
                                //     'txn_id'=>$txn_id,
                                //     'loggedin_time'=>time()
                                // );

                                // //print_obj($session_data);

                                // //echo $redirect_url;

                                // //die;

                                // session_set_userdata($session_data);



                                //echo $redirect_url;die;

                                redirect($redirect_url);
                            }else{
                                redirect($redirect_url);
                            }
                        }else{
                            echo 'error 1';
                        }
                    }else{
                        redirect($redirect_url);
                    }
                }


                   
            }else if($order_prefix=='ORDHT'){
                if($utype=='stu'){
                    $order_code=$_fisrt_sec_order_id.$order__id;

                    if($param['payment_platform']=='razorpay'){
                        $txn_id='T'.date('YmdHis').$inst_id.$user_id.$booking_id.$hostel_id;
                    }else if($param['payment_platform']=='paytm'){
                        $txn_id=$param['txn_id'];
                    }

                    //echo $order_code;die;
                    $date_now = date('Y-m-d');// new DateTime();
                    $due_date = date('Y-m-d',strtotime($booking_due_date));//new DateTime($booking_due_date);

                    if($date_now>$due_date){
                        $penalty_applied='applied';  
                    }else if($date_now==$due_date){
                        $penalty_applied='not applied';
                    }else{
                        $penalty_applied='not applied';
                    }

                    //echo $order_code;die;
            

                   $updated=$this->fm->update_hostel_fees_payment(array('recieved_at'=>date('Y-m-d'),'payment_status'=>'paid','payment_penalty'=>$penalty_applied),array('payment_id'=>$booking_id));

                    $payment_details=array(
                        'details_type_text'=>$payment_description,
                        'details_payment_platform'=>$param['payment_platform'],
                        'details_type'=>$booking_type,
                        'details_payment_type'=>'hostel_fees',
                        'details_payment_id'=>$booking_id,
                        'details_type_id'=>$hostel_id,
                        'details_inst_id'=>$booking_inst_id,
                        'details_parent_inst_id'=>$booking_inst_parent_id,
                        'details_stu_id'=>$student_data->stu_user_id,
                        'details_sess_id'=>$session_id,
                        'details_course_id'=>$course_id,
                        'details_sess_year'=>$session_year,
                        'details_installment_id'=>$booking_id,
                        'details_txn_id'=>$txn_id,
                        'details_pay_id'=>(isset($param['pay_id']))?$param['pay_id']:null,
                        'details_order_id'=>$param['order_id'],
                        'details_order_code'=>$order_code,                                 
                        'details_amount'=>$txn_amount,
                        'details_discount_amount'=>'0',
                        'details_accounting_type'=>'income',
                        'details_msg'=>$param['msg'],
                        'details_status'=>'success',
                        'details_created_by_type'=>$userdata->user_type,
                        'details_created_by'=>$user_id,
                        'details_created_at'=>date('Y-m-d')
                    );


                    //print_obj($payment_details);die;

                    $payment_id=$this->fm->store_payment_details($payment_details);

                    if($payment_id){
                        $hostel_data=$this->hm->get_hostel(array('hostel_inst_id'=>$booking_inst_id,'hostel_id'=>$hostel_id));

                        //print_obj($hostel_data);die;

                        $hostel_payment_details=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_txn_id'=>$txn_id));

                        //print_obj($hostel_payment_details);die;

                        if($utype=='stu'){
                            $get_hostel_fees_payment=$this->fm->get_hostel_fees_payment(array('payment_hostel_id'=>$hostel_id,'payment_hostel_inst_id'=>$booking_inst_id,'payment_hostel_sess_id'=>$session_id,'payment_hostel_stu_id'=>$user_id,'payment_year'=>$session_year,'payment_id'=>$booking_id));
                        }else if($utype=='stuinst'){
                            $get_hostel_fees_payment=$this->fm->get_hostel_fees_payment(array('payment_hostel_id'=>$hostel_id,'payment_hostel_inst_id'=>$booking_inst_id,'payment_hostel_sess_id'=>$session_id,'payment_hostel_stu_id'=>$hostel_fees_data->payment_hostel_stu_id,'payment_year'=>$session_year,'payment_id'=>$booking_id));
                        }

                        $session_data=$this->sessm->get_session(array('session_id'=>$session_id));

                        $mail_subject='Hostel Rent Payment Details for '.ucwords($get_hostel_fees_payment->payment_month).'-'.$get_hostel_fees_payment->payment_year;

                        $mail_data['inst_id']=$booking_inst_id;
                        $mail_data['mail_from']='webmaster';

                        if(isset($student_data) && !empty($student_data)){

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


                            $mail_data['to_email']=$student_data->stu_email;
                        }

                        if($userdata->user_type=='student'){
                            $mail_data['mail_from_name']=$institute_profile_name;

                            $mail_data['mail_subject']=$mail_subject;
                            
                            $profile_name=$institute_profile_name;
                            $college_email=$institute_email;
                            $college_phone=$institute_phone_no;
                            $link_logo=$this->data['link_logo'];
                        }else{

                            //print_obj($institute_data);die;

                            $mail_data['mail_from_name']=$institute_data->user_profile_name;
                            $mail_data['mail_subject']=$mail_subject;
                            $profile_name=$institute_data->user_profile_name;
                            $college_email=$institute_data->user_email;
                            $college_phone=$institute_data->user_contact_no;
                            $link_logo=$this->data['link_logo'];

                            //print_obj($mail_data);die;
                        }
                        
                        
                        $mail_data['mail_type']='html';
                        $mail_data['mail_custom_type']='html';
                        $mail_data['mail_data']=array(
                            'stu_name'=>ucwords($student_name),
                            'stu_email'=>$student_data->stu_email,
                            'stu_ph'=>$student_data->stu_ph_no,
                            'payment_details'=>$get_hostel_fees_payment,
                            'hostel_data'=>$hostel_data,
                            'hostel_payment_details'=>$hostel_payment_details,
                            'session_data'=>$session_data,
                            'portal_url'=>$this->data['base_url'],
                            'portal_logo'=>$link_logo,
                            'portal_inst_name'=>$profile_name,
                            'college_email'=>$college_email,
                            'college_phone'=>$college_phone,
                            'default_logo_small'=>$this->data['default_logo_small'],
                            'created_at'=>date('d-m-Y')
                        );

                        
                        $mail_data['mail_view']='_pages/mails/vw_hostel_fees_payment_mail';

                        $this->onSendMail($mail_data);

                        $mail_data_2=$mail_data;

                        $mail_data_2['to_email']=$institute_email;
                        $mail_data_2['mail_subject']=$mail_subject.' [Copy mail]';
                        
                        $this->onSendMail($mail_data_2);

                        unset($_SESSION['payment_gateway']);

                        redirect($redirect_url);
                    }
                }else if($utype=='admfee'){

                    $order_code=$_fisrt_sec_order_id.$order__id;

                    if($param['payment_platform']=='razorpay'){
                        $txn_id='T'.date('YmdHis').$inst_id.$user_id.$booking_id.$hostel_id;
                    }else if($param['payment_platform']=='paytm'){
                        $txn_id=$param['txn_id'];
                        $temp_payment_data=$this->fm->get_temp_payment(array('temp_checksum'=>$param['order_id']));
                    }

                    //echo $order_code;
                    $this->hm->update_hostel_admission_fees_assign_data(array('assign_fees_paid'=>'yes','assign_fees_paid_date'=>date('Y-m-d H:i:s')),array('assigned_id'=>$booking_id));

                    $hpd=$this->hm->__get_hostel_admission_fees_assign_data(array('assigned_id'=>$booking_id));

                    $session_years=$session_data->session_start_year.'-'.$session_data->session_end_year;                                

                    //print_obj($hostel_payment_details);die;

                    $payment_details=array(
                        'details_payment_id'=>$hpd->assigned_id,
                        'details_payment_platform'=>$param['payment_platform'],
                        'details_type'=>$booking_type,
                        'details_payment_type'=>'hostel_admission_fees_payment',
                        'details_type_id'=>$hostel_id,
                        'details_inst_id'=>$booking_inst_id,
                        'details_parent_inst_id'=>$booking_inst_parent_id,
                        'details_sess_id'=>$session_id,
                        'details_course_id'=>$course_id,
                        'details_stu_id'=>$student_data->stu_user_id,
                        'details_sess_year'=>'0',
                        'details_installment_id'=>$booking_id,
                        'details_txn_id'=>$txn_id,
                        'details_pay_id'=>(isset($param['pay_id']))?$param['pay_id']:null,
                        'details_order_id'=>$param['order_id'],
                        'details_order_code'=>$order_code,
                        'details_pay_signature'=>(isset($param['pay_signature']))?$param['pay_signature']:null,
                        'details_type_text'=>'Hostel Admission ['.$session_years.'] Payment Details',
                        'details_pay_order_id'=>(isset($param['pay_order_id']))?$param['pay_order_id']:null,
                        'details_pay_currency'=>(isset($param['pay_currency']))?$param['pay_currency']:null,
                        'details_payee_email'=>(isset($param['pay_email']))?$param['pay_email']:null,
                        'details_payee_contact'=>(isset($param['pay_contact']))?$param['pay_contact']:null,
                        'details_gateway_created_at'=>(isset($param['pay_created_at']))?$param['pay_created_at']:null,
                        'details_gateway_status'=>(isset($param['pay_status']))?$param['pay_status']:null,
                        'details_amount'=>$txn_amount,
                        'details_created_by_type'=>($utype=='stu')?'student':'institute',
                        'details_discount_amount'=>'0',
                        'details_accounting_type'=>'income',
                        'details_msg'=>$param['msg'],
                        'details_status'=>'success',
                        'details_created_by_type'=>$userdata->user_type,
                        'details_created_by'=>$user_id,
                        'details_created_at'=>date('Y-m-d H:i:s')
                    );

                   //print_obj($payment_details);die;

                    $payment_id=$this->fm->store_payment_details($payment_details);

                    if($payment_id){
                        $session_data=$this->sessm->get_session(array('session_id'=>$session_id));

                        $_hpd=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_payment_type'=>'hostel_admission_fees_payment','details_txn_id'=>$txn_id));

                        $hostel_payment_details=array(
                            'hostel_name'=>$hpd->hostel_name,
                            'session_years'=>$session_years,
                            'admission_fee'=>number_format($hpd->admission_fees_value,2),
                            'caution_fee'=>number_format($hpd->admission_caution_fees_value,2),
                            'total_fee'=>number_format(($hpd->admission_fees_value+$hpd->admission_caution_fees_value),2),
                            'payment_mode'=>ucwords($_hpd->details_payment_platform),
                            'taxation_no'=>$_hpd->details_txn_id,
                            'payment_date'=>date('d-m-Y')
                        );

                        $session_years=$session_data->session_start_year.'-'.$session_data->session_end_year;

                        $mail_subject='Hostel Admission Payment Details [Session : '.$session_years.']';

                        $mail_data['inst_id']=$booking_inst_id;
                        $mail_data['mail_from']='webmaster';

                        if(isset($student_data) && !empty($student_data)){

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


                            $mail_data['to_email']=$student_data->stu_email;
                        }

                        if($userdata->user_type=='student'){
                            $mail_data['mail_from_name']=$institute_profile_name;

                            $mail_data['mail_subject']=$mail_subject;
                            
                            $profile_name=$institute_profile_name;
                            $college_email=$institute_email;
                            $college_phone=$institute_phone_no;
                            $link_logo=$this->data['link_logo'];
                        }else{

                            //print_obj($institute_data);die;

                            $mail_data['mail_from_name']=$institute_data->user_profile_name;
                            $mail_data['mail_subject']=$mail_subject;
                            $profile_name=$institute_data->user_profile_name;
                            $college_email=$institute_data->user_email;
                            $college_phone=$institute_data->user_contact_no;
                            $link_logo=$this->data['link_logo'];

                            //print_obj($mail_data);die;
                        }
                        
                        
                        $mail_data['mail_type']='html';
                        $mail_data['mail_custom_type']='html';
                        $mail_data['mail_data']=array(
                            'stu_name'=>ucwords($student_name),
                            'stu_email'=>$student_data->stu_email,
                            'stu_ph'=>$student_data->stu_ph_no,
                            'hostel_payment_details'=>$hostel_payment_details,
                            'session_data'=>$session_data,
                            'portal_url'=>$this->data['base_url'],
                            'portal_logo'=>$link_logo,
                            'portal_inst_name'=>$profile_name,
                            'college_email'=>$college_email,
                            'college_phone'=>$college_phone,
                            'default_logo_small'=>$this->data['default_logo_small'],
                            'created_at'=>date('d-m-Y')
                        );


                        //print_obj($mail_data);die;

                        
                        $mail_data['mail_view']='_pages/mails/vw_hostel_admission_fees_payment_mail';

                        $this->onSendMail($mail_data);

                        $mail_data_2=$mail_data;

                        $mail_data_2['to_email']=$institute_email;
                        $mail_data_2['mail_subject']=$mail_subject.' [Copy mail]';
                        
                        $this->onSendMail($mail_data_2);

                        unset($_SESSION['payment_gateway']);

                        redirect($redirect_url);
                    }
                } 
            }
            else{
                echo 'error 00';
            }

        }else{
            return 'Order ID is missing';
        }

    }


    //Temp Payment 
    public function onTempPaymentStore(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){
            if($this->input->is_ajax_request() && $this->input->server('REQUEST_METHOD')=='POST'){
                $scholarship=post_data('payment_scholarship_amount');
                $special_amount=post_data('payment_special_amount');
                $scholarship_reason=post_data('payment_scholarship_reason');
                $special_reason=post_data('payment_special_reason');
                $checksum=post_data('checksum');

                //$total_discount=$scholarship+$special_amount;
                
                $data_to_add=array(
                    'temp_checksum'=>$checksum,
                    'temp_scholarship_discount'=>$scholarship,
                    'temp_special_discount'=>$special_amount,
                    'temp_scholarship_discount_reason'=>$scholarship_reason,
                    'temp_special_discount_reason'=>$special_reason
                );

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



    //Bill Generation

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

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

                if($userdata->user_type=='student'){
                    $inst_id=$userdata->stu_inst_id;
                }else{
                    $inst_id=$userdata->user_id;
                }

                $institute_user_data=$this->um->get_user(array('user_id'=>$inst_id));

                //print_obj($institute_user_data);die;

                $bill_type=post_data('bill_type');
                $booking_id=post_data('booking_id');

                $booking_payment_details=array();

                if($institute_user_data->user_type==='institute'){
                    $college_data=$this->um->get_institute(array('user_pk_id'=>$inst_id));
                    $college_profile_name=$college_data->user_profile_name;
                    $college_website=$college_data->user_website;
                    $college_email=$college_data->user_email;
                    $college_contact_no=$college_data->user_contact_no;
                }else if($institute_user_data->user_type==='institute_branch'){
                    $college_data=$this->um->get_institute_branch(array('branch_user_id'=>$inst_id));
                    $college_profile_name=$college_data->branch_name;
                    $college_website=$college_data->branch_website;
                    $college_email=$college_data->branch_email;
                    $college_contact_no=$college_data->branch_contact_no;
                }else if($institute_user_data->user_type==='student'){
                    if($userdata->stu_inst_type=='stu_inst_type'){
                        $college_data=$this->um->get_institute_branch(array('branch_user_id'=>$inst_id));
                        $college_profile_name=$college_data->branch_name;
                        $college_website=$college_data->branch_website;
                        $college_email=$college_data->branch_email;
                        $college_contact_no=$college_data->branch_contact_no;
                    }else{
                       $college_data=$this->um->get_institute(array('user_pk_id'=>$inst_id));
                       $college_profile_name=$college_data->user_profile_name;
                       $college_website=$college_data->user_website;
                       $college_email=$college_data->user_email;
                       $college_contact_no=$college_data->user_contact_no;
                    }                    
                }                

                //print_obj($college_data);die;

                if(!empty($booking_id)){                    

                    $college_address='';

                    if($institute_user_data->user_type==='institute'){

                        if($college_data->user_address_1!=''){
                            $college_address.=$college_data->user_address_1;
                        }                        

                        if($college_data->user_address_2!=''){
                            $college_address.=$college_data->user_address_2;
                        }                        

                        if($college_data->user_city!=''){                        
                            $college_address.=','.$college_data->user_city;
                        }

                        if($college_data->user_state!=''){
                            $state_data=$this->sm->_get_states(array('state_id'=>$college_data->user_state));
                            $college_address.=','.$state_data->state_name;
                        }

                        if($college_data->user_pincode!=''){
                            $college_address.=','.$college_data->user_pincode;
                        }

                        $payment_college=$college_data->user_profile_name;

                        $payment_college_website=$college_data->user_website;
                        $payment_college_email=$college_data->user_email;
                        $payment_college_ph_no=$college_data->user_contact_no;

                        $college_img_data=$this->sm->get_file(array('storage_type'=>'college_logo','storage_type_id'=>$inst_id));

                        //print_obj($college_img_data);die;

                        if(!empty($college_img_data) && is_file(FCPATH.$college_img_data->media_disk_path)){
                            $college_logo=base_url($college_img_data->media_disk_path_relative);
                        }else{
                            $college_logo=$this->data['no_image'];
                        }


                        $college_imgseal_data=$this->sm->get_file(array('storage_type'=>'college_seal_logo','storage_type_id'=>$inst_id));

                        if(!empty($college_imgseal_data) && is_file(FCPATH.$college_imgseal_data->media_disk_path)){
                            $college_seal_logo=base_url($college_imgseal_data->media_disk_path_relative);
                        }else{
                            $college_seal_logo=$this->data['no_image'];
                        }


                        $college_signature_data=$this->sm->get_file(array('storage_type'=>'college_authorize_signature_logo','storage_type_id'=>$inst_id));

                        if(!empty($college_signature_data) && is_file(FCPATH.$college_signature_data->media_disk_path)){
                            $college_authorized_signature_logo=base_url($college_signature_data->media_disk_path_relative);
                        }else{
                            $college_authorized_signature_logo=$this->data['no_image'];
                        }
                        
                    }else if($institute_user_data->user_type==='institute_branch'){


                        if($college_data->branch_address!=''){
                            $college_address.=$college_data->branch_address;
                        }

                        if($college_data->branch_address_1!=''){
                            $college_address.=$college_data->branch_address_1;
                        }                        

                        if($college_data->branch_city!=''){
                            $college_address.=$college_data->branch_city;
                        }

                        if($college_data->branch_state!=''){
                            $state_data=$this->sm->_get_states(array('state_id'=>$college_data->branch_state));
                            $college_address.=','.$state_data->state_name;
                        }

                        if($college_data->branch_pincode!=''){
                            $college_address.=','.$college_data->branch_pincode;
                        }

                        $payment_college=$college_data->branch_name;

                        $payment_college_website=$college_data->branch_website;
                        $payment_college_email=$college_data->branch_email;
                        $payment_college_ph_no=$college_data->branch_contact_no;

                        $college_img_data=$this->sm->get_file(array('storage_type'=>'college_branch_logo','storage_type_id'=>$inst_id));

                        //print_obj($college_img_data);die;

                        if(!empty($college_img_data) && is_file(FCPATH.$college_img_data->media_disk_path)){
                            $college_logo=base_url($college_img_data->media_disk_path_relative);
                        }else{
                            $college_logo=$this->data['no_image'];
                        }                        
                    }

                    $booking_id=decode_data($booking_id);

                    $file_data=array();
                    $payment_cheque_data=array();

                    if($bill_type=='seat_booking'){

                        $invd=$this->fm->_get_seat_booking_payments_details(array('booking_id'=>$booking_id));

                        //print_obj($invd);die;

                        if(!empty($invd)){

                            //$course_data=$this->sessm->get_session_course(array('session_course_id'=>'','session_inst_id'=>));

                            $booking_name=$invd->stu_first_name.' '.$invd->stu_mid_name.' '.$invd->stu_last_name;

                            if($invd->details_type=='seat_booking'){
                                $seat_booking_data=$this->adm->get_seat_bookings_data(array('booking_payment_id'=>$invd->details_type_id,'booking_pk_id'=>$invd->details_type_id,'booking_stu_id'=>$invd->details_stu_id));

                                //print_obj($seat_booking_data);die;

                                $pay_mode=ucwords($seat_booking_data->booking_pay_mode);

                                if(!empty($seat_booking_data)){
                                    if($seat_booking_data->booking_pay_mode==='cheque'){
                                        $deposite_bank_data=$this->sm->get_banks(array('bank_id'=>$seat_booking_data->booking_payment_deposite_bank));
                                        if(!empty($seat_booking_data->booking_payment_checq_files_ids)){
                                            $cheque_files=$this->sm->get_file_in('storage_id',$seat_booking_data->booking_payment_checq_files_ids);
                                        }else {
                                            $cheque_files='';
                                        }
                                        

                                        if(!empty($cheque_files)){
                                            foreach ($cheque_files as $key => $value) {
                                                $file_data[]=base_url($value->media_disk_path_relative);
                                            }
                                        }

                                        $payment_cheque_data=array(
                                            'bank_name'=>$seat_booking_data->booking_payment_bank,
                                            'bank_cheque_no'=>$seat_booking_data->booking_payment_checq_no,
                                            'bank_deposite_to'=>ucwords($deposite_bank_data->bank_name).' ['.ucwords($deposite_bank_data->bank_branch).' Branch]',
                                            'bank_cheque_files'=>(isset($file_data))?$file_data:null
                                        );  
                                    }
                                }                                
                            }

                            //print_obj($invd);die;

                            if($invd->booking_subject_id!=null){
                                $payment_subject_data=$this->cm->get_subject(array('subject_id'=>$invd->booking_subject_id));
                                $payment_subject=$payment_subject_data->subject_name;
                            }else{
                                $payment_subject='N/A';
                            }

                            //print_obj($invd);die;

                            $invoice_details=array(
                                'payment_code'=>$invd->booking_code,
                                'payment_stu_code'=>$invd->stu_user_code,
                                'payment_txn_code'=>$invd->details_txn_id,
                                'payment_name'=>ucwords($booking_name),
                                'payment_email'=>$invd->stu_email,
                                'payment_phone_no'=>$invd->stu_ph_no,
                                'payment_course'=>$invd->course_name,
                                'payment_course_amount'=>'',
                                'payment_subject'=>$payment_subject,
                                'payment_session'=>$invd->session_start_year.'-'.$invd->session_end_year,
                                'payment_amount'=>number_format($invd->details_amount,2),
                                'payment_platform'=>ucwords($invd->details_payment_platform),
                                'payment_mode'=>(isset($pay_mode))?$pay_mode:'',
                                'payment_date'=>date('d/m/Y',strtotime($invd->created_at)),
                                'payment_cheque_data'=>$payment_cheque_data,
                                'payment_college'=>$payment_college,
                                'payment_college_logo'=>$this->data['link_logo'],
                                'payment_college_letter_head'=>$college_letter_head,
                                'payment_college_authorized_signature_logo'=>$college_authorized_signature_logo,
                                'payment_college_seal_logo'=>$college_seal_logo,
                                'payment_college_address'=>$college_address,
                                'payment_college_website'=>$payment_college_website,
                                'payment_college_email'=>$payment_college_email,
                                'payment_college_ph_no'=>$payment_college_ph_no
                            );


                            //print_obj($invoice_details);die;


                            $this->data['invoice_data']=$invoice_details;

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

                        }else{
                            $return['error']='Details not found';
                        }
                    }else if($bill_type=='new_admission'){

                        $invd=$this->fm->get_installment(array('installment_id'=>$booking_id));

                        //print_obj($invd);die;

                        if(!empty($invd)){

                            $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                            $payment_details=$this->fm->get_payment_details(array('details_installment_id'=>$booking_id),FALSE);


                            if($invd->stu_mid_name!=''){
                                $booking_name=$invd->stu_first_name.' '.$invd->stu_mid_name.' '.$invd->stu_last_name;
                            }else if($invd->stu_id_name!=''){
                                $booking_name=$invd->stu_first_name.' '.$invd->stu_last_name;
                            }

                            $date_now = new DateTime();
                            $installment_date    = new DateTime($invd->installment_date);

      
                            if($date_now>$installment_date){
                                $penalty_applied=TRUE;
                                $total_due=$invd->installment_value+$invd->installment_value_penalty;  
                            }else if($date_now==$installment_date){
                                $penalty_applied=FALSE;
                                $total_due=$invd->installment_value;
                            }else{
                                $penalty_applied=FALSE;
                                $total_due=$invd->installment_value;
                            }
                            

                            if(!empty($payment_details)){
                                if(!empty($payment_details)){
                                    foreach ($payment_details as $key => $value) {

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

                                        if($value->details_payment_type=='installment_fees'){
                                            $payment_type='Installment payment';
                                        }else if($value->details_payment_type=='installment_balance_fees'){
                                            $paynment_type='Balance Payment';
                                        }

                                        $booking_payment_details[]=array(
                                            'detaile_payment_type'=>$payment_type,
                                            'details_amount'=>number_format($value->details_amount,2)
                                        );
                                    }
                                    
                                }
                            }

                            $invoice_details=array(
                                'booking_code'=>$admission_data->admission_code,
                                'booking_name'=>ucwords($booking_name),
                                'booking_email'=>$invd->stu_email,
                                'booking_phone_no'=>$invd->stu_ph_no,
                                'booking_course'=>$invd->course_name,
                                'booking_session'=>$invd->session_start_year.'-'.$invd->session_end_year,                                
                                'booking_penalty_applied'=>$penalty_applied,
                                'booking_installment'=>number_format($invd->installment_value,2),                                
                                'booking_penalty'=>$invd->installment_value_penalty,
                                'booking_amount'=>number_format($total_due,2),
                                'booking_date'=>date('d/m/Y',strtotime($invd->created_at)),
                                'booking_college'=>$college_data->user_profile_name,
                                'booking_college_logo'=>$college_logo,
                                'payment_college_letter_head'=>$college_letter_head,
                                'bokking_college_address'=>$college_address,
                                'booking_college_website'=>$college_data->user_website,
                                'booking_college_email'=>$college_data->user_email,
                                'booking_college_ph_no'=>$college_data->user_contact_no,
                                'booking_payment_details'=>$booking_payment_details
                            );

                            $this->data['invoice_data']=$invoice_details;

                            //print_obj($invoice_details);die;


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

                        }else{
                            $return['error']='Details not found';
                        }
                        
                    }else if($bill_type=='hostel_fees_payment'){

                        //echo $booking_id;


                        $_payment_data=$this->fm->__get_hostel_fees_payment(array('payment_id'=>$booking_id));

                        //print_obj($_payment_data);die;

                        //$_payment_data=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_payment_type'=>'hostel_fees','details_id'=>$booking_id));

                        //print_obj($_payment_data);die;

                        if(!empty($_payment_data)){


                            // $date_now = new DateTime();
                            // $due_date = new DateTime($_payment_data->due_at);

                            // if($date_now>$due_date){
                            //     $penalty_applied='applied';  
                            // }else if($date_now==$due_date){
                            //     $penalty_applied='not applied';
                            // }else{
                            //     $penalty_applied='not applied';
                            // }


                            $payment_details=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_payment_type'=>'hostel_fees','details_payment_id'=>$_payment_data->payment_id,'details_type_id'=>$_payment_data->payment_hostel_id,'details_inst_id'=>$_payment_data->payment_hostel_inst_id));

                            //print_obj($payment_details);die;

                            // if($_payment_data->stu_mid_name!=''){
                            //     $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_mid_name.' '.$_payment_data->stu_last_name;
                            // }else if($_payment_data->stu_mid_name!=''){
                            //     $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_last_name;
                            // }

                            $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_mid_name.' '.$_payment_data->stu_last_name;

                            $ordeid=explode('_', $payment_details->details_order_id);

                            $payment_data=array(
                                'payment_code'=>$payment_details->details_order_code,
                                'payment_name'=>ucwords($booking_name),
                                'payment_email'=>$_payment_data->stu_email,
                                'payment_phone_no'=>$_payment_data->stu_ph_no,
                                'payment_course'=>$_payment_data->course_name,
                                'payment_session'=>$_payment_data->session_start_year.'-'.$_payment_data->session_end_year,
                                'payment_hostel_name'=>ucwords($_payment_data->hostel_name),
                                'payment_hostel_addresss'=>$_payment_data->hostel_address,
                                'payment_platform'=>ucwords($payment_details->details_payment_platform),
                                'payment_txn_code'=>$payment_details->details_txn_id,
                                'payment_fee'=>number_format($_payment_data->payment_value,2),
                                'payment_late_fee'=>number_format($_payment_data->payment_late_value,2),
                                'payment_month'=>ucwords($_payment_data->payment_month),
                                'payment_year'=>$_payment_data->payment_year,
                                'payment_month_year'=>strtoupper($_payment_data->payment_month).'-'.$_payment_data->payment_year,
                                'payment_amount'=>number_format($payment_details->details_amount,2),
                                'payment_late_fee_applied'=>$_payment_data->payment_penalty,
                                'payment_due_date'=>date('d-m-Y',strtotime($_payment_data->due_at)),
                                'payment_date'=>date('d-m-Y',strtotime($_payment_data->recieved_at)),
                                'payment_college'=>$college_profile_name,
                                'payment_college_logo'=>$college_logo,
                                'payment_college_letter_head'=>$college_letter_head,
                                'payment_college_address'=>$college_address,
                                'payment_college_website'=>$college_website,
                                'payment_college_email'=>$college_email,
                                'payment_college_ph_no'=>$college_contact_no
                            );

                            $this->data['invoice_data']=$payment_data;

                            //print_obj($this->data['invoice_data']);die;


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


                        }else{
                            $return['error']='Payment data not found';
                        }

                    }else if($bill_type=='hostel_admission_fees_payment'){

                        //echo $booking_id;

                        $_payment_data=$this->fm->get_payment_details(array('details_type'=>'hostel_fees_payment','details_payment_type'=>'hostel_admission_fees_payment','details_id'=>$booking_id));

                        //print_obj($_payment_data);die;


                        $hpd=$this->hm->__get_hostel_admission_fees_assign_data(array('assigned_id'=>$_payment_data->details_installment_id));

                        //print_obj($hpd);die;

                        $booking_name=$hpd->stu_first_name.' '.$hpd->stu_mid_name.' '.$hpd->stu_last_name;

                        $admission_fees_value=$hpd->admission_fees_value;
                        $caution_fees_value=$hpd->admission_caution_fees_value;
                        $total_fees_value=$admission_fees_value+$caution_fees_value;

                        $payment_data=array(
                            'payment_code'=>$_payment_data->details_order_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$hpd->stu_email,
                            'payment_stu_code'=>$hpd->stu_user_code,
                            'payment_phone_no'=>$hpd->stu_ph_no,
                            'payment_course'=>$hpd->course_name,
                            'payment_subject'=>(!empty($hpd->subject_name))?$hpd->subject_name:'N/A',
                            'payment_session'=>$hpd->session_start_year.'-'.$hpd->session_end_year,
                            'payment_platform'=>ucwords($_payment_data->details_payment_platform),
                            'payment_txn_code'=>$_payment_data->details_txn_id,
                            'payment_admission_fee'=>number_format($admission_fees_value,2),
                            'payment_caution_fee'=>number_format($caution_fees_value,2),
                            'payment_amount'=>number_format($total_fees_value,2),
                            'payment_date'=>date('d-m-Y',strtotime($_payment_data->details_created_at)),
                            'payment_hostel_name'=>$hpd->hostel_name,
                            'payment_college'=>$college_profile_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_website,
                            'payment_college_email'=>$college_email,
                            'payment_college_ph_no'=>$college_contact_no
                        );

                        $this->data['invoice_data']=$payment_data;

                        //print_obj($this->data['invoice_data']);die;


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

                    }else if($bill_type=='installment_fees'){

                        $_payment_data=$this->fm->_get_payment_details(array('details_type'=>'new_admission','details_payment_type'=>'installment_fees','details_id'=>$booking_id),TRUE,FALSE);


                        //print_obj($_payment_data);die;

                        $invd=$this->fm->get_installment(array('installment_id'=>$_payment_data->details_installment_id));


                        $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));

                        //print_obj($payment_data);die;

                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        //print_obj($_payment_data);die;

                        $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_mid_name.' '.$_payment_data->stu_last_name;


                        if($payment_data->payment_late_fee_applied=='no'){
                            $amount_payable=$invd->installment_value;
                        }else if($payment_data->payment_late_fee_applied=='yes'){
                            $amount_payable=$invd->installment_value+$invd->installment_value_penalty;
                        } 

                        //if($_payment_data->details_payment_platform=='cheque'){
                            $payment_details_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));
                        //}  

                         $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_code'=>$_payment_data->details_order_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$_payment_data->stu_email,
                            'payment_stu_code'=>$_payment_data->stu_user_code,
                            'payment_phone_no'=>$_payment_data->stu_ph_no,
                            'payment_course'=>$_payment_data->course_name,
                            'payment_subject'=>(!empty($_payment_data->subject_name))?$_payment_data->subject_name:'N/A',
                            'payment_session'=>$_payment_data->session_start_year.'-'.$_payment_data->session_end_year,
                            'payment_platform'=>ucwords($_payment_data->details_payment_platform),
                            'payment_mode'=>ucwords($payment_data->pay_mode),
                            'payment_txn_code'=>$_payment_data->details_txn_id,
                            'payment_late_fee_applied'=>$payment_data->payment_late_fee_applied,
                            'payment_late_fee'=>$payment_data->payment_late_fee,
                            'payment_balance_amount'=>(!empty($payment_data) && $payment_data->payment_fees_balance_amount>0)?number_format($payment_data->payment_fees_balance_amount,2):'',
                            'payment_amount'=>number_format($amount_payable,2),
                            'payment_amount_paid'=>number_format($_payment_data->details_amount,2),
                            'payment_details_data'=>(isset($payment_details_data) && !empty($payment_details_data))?$payment_details_data:'',
                            'payment_discount'=>($_payment_data->details_discount_amount>0)?number_format($_payment_data->details_discount_amount,2):'0',
                            'payment_installment_data'=>$invd,
                            'payment_date'=>date('d-m-Y',strtotime($_payment_data->details_created_at)),
                            'payment_college'=>$payment_college,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$payment_college_website,
                            'payment_college_email'=>$payment_college_email,
                            'payment_college_ph_no'=>$payment_college_ph_no
                        );

                        $this->data['invoice_data']=$payment_data;

                        //print_obj($this->data['invoice_data']);die;


                        $return['html']=$this->theme->view('_pages/bills/vw_bill_admission_installments',$this->data,true);
                    }else if($bill_type=='installment_balance_fees'){

                        $payment_details=$this->fm->get_payment_details(array('details_id'=>$booking_id));

                        $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$payment_details->details_payment_id));

                        $_get_fees_details_last=$this->fm->_get_fees_details_last(array('payment_stu_id'=>$payment_details->details_stu_id,'payment_inst_id'=>$payment_details->details_inst_id,'payment_installment_id'=>$payment_details->details_installment_id));



                        $invd=$this->fm->get_installment(array('installment_id'=>$payment_details->details_installment_id));
                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        // //print_obj($admission_data);die;

                        $booking_name=$admission_data->stu_first_name.' '.$admission_data->stu_mid_name.' '.$admission_data->stu_last_name;

                        $prev_payment_data=$this->fm->_get_payment_details(array('details_type'=>'new_admission','details_installment_id'=>$payment_details->details_installment_id,'details_payment_id'=>$payment_details->details_payment_id),TRUE);

                        $balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$payment_details->details_installment_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admission_data->admission_sess_id));

                        $last_payment_data=$this->fm->get_payment_details_last(array('details_type'=>'new_admission','details_payment_type'=>'installment_balance_fees','details_stu_id'=>$admission_data->admission_stu_id,'details_parent_inst_id'=>$admission_data->admission_inst_id,'details_sess_id'=>$admission_data->admission_sess_id,'details_course_id'=>$admission_data->admission_course_id));

                        /*if($payment_data->payment_late_fee_applied=='no'){
                            $amount_payable=$invd->installment_value;
                        }else if($payment_data->payment_late_fee_applied=='yes'){
                            $amount_payable=$invd->installment_value+$invd->installment_value_penalty;
                        }*/

                        //Late fees
                        //if($payment_data->payment_late_fee_applied=='yes'){
                            $late_fee_data=$this->fm->get_late_fees(array('late_fee_inst_id'=>$admission_data->admission_inst_id,'late_fee_stu_id'=>$admission_data->admission_stu_id,'late_fee_course_id'=>$admission_data->admission_course_id,'late_fee_session_id'=>$admission_data->admission_sess_id,'late_fee_admission_id'=>$admission_data->admission_id,'late_fee_installment_id'=>$payment_details->details_installment_id));

                            print_obj($late_fee_data);die;

                            if(!empty($late_fee_data)){
                                $late_fee=$late_fee_data->late_fee;
                            }else{
                                $late_fee=0;
                            }  
                        // }else{
                        //     $late_fee=0;
                        // }

                        $amount_payable=$invd->installment_value+$late_fee;

                        $prev_balance=$amount_payable-$prev_payment_data->details_prev_total_amount;
                        $due_amount=$prev_balance-$payment_details->details_amount;

                        $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_code'=>$last_payment_data->details_order_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$invd->stu_email,
                            'payment_stu_code'=>$invd->stu_user_code,
                            'payment_phone_no'=>$invd->stu_ph_no,
                            'payment_course'=>$invd->course_name,
                            'payment_subject'=>(!empty($invd->subject_name))?$invd->subject_name:'N/A',
                            'payment_session'=>$invd->session_start_year.'-'.$invd->session_end_year,
                            'payment_platform'=>ucwords($last_payment_data->details_payment_platform),
                            'payment_is_balance'=>($payment_details->details_payment_type=='installment_balance_fees')?'[ Balance Payment ]':'',
                            'payment_mode'=>'Online',
                            'payment_txn_code'=>$last_payment_data->details_txn_id,
                            'payment_late_fee_applied'=>$payment_data->payment_late_fee_applied,
                            'payment_late_fee'=>number_format($payment_data->payment_late_fee,2),
                            'payment_balance_amount'=>number_format($prev_balance,2),
                            'payment_prev_amount_paid'=>number_format($prev_payment_data->details_prev_total_amount,2),
                            'payment_amount_paid'=>number_format($payment_details->details_amount,2),
                            'payment_amount'=>number_format($amount_payable,2),
                            'payment_due_amount'=>($due_amount>0)?number_format($due_amount,2):'',
                            'payment_installment_data'=>$invd,
                            'payment_date'=>date('d-m-Y',strtotime($last_payment_data->details_created_at)),
                            'payment_college'=>$college_data->user_profile_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_data->user_website,
                            'payment_college_email'=>$college_data->user_email,
                            'payment_college_ph_no'=>$college_data->user_contact_no
                        );


                        $this->data['invoice_data']=$payment_data;

                        //print_obj($this->data['invoice_data']);die;


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

                        

                }else{
                    $return['error']='No booking data found';
                }


                


                json_headers($return);
                

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



    //Bills Print
    public function indexBills(){
        if(session_userdata('isAdminLoggedin')){

            $bill_type=$this->input->get('type');
            $bill_code=$this->input->get('code');
            $bill_balance_code=$this->input->get('bcode');

            $userdata=$this->data['userdata'];
            $db_group=$userdata->user_code;

            if(!empty($bill_type) && !empty($bill_code)){

                $booking_payment_details=array();

                $institute_user_data=$this->um->get_user(array('user_id'=>$this->user_id));
                $this->data['money_receipt_settings']=$this->sm->get_system_settings(array('settings_type'=>'money_reciept_note','settings_inst_id'=>$this->inst_id));

          
                $booking_payment_details=array();

                if($institute_user_data->user_type==='institute'){
                    $college_data=$this->um->get_institute(array('user_pk_id'=>$this->inst_id));
                    $college_profile_name=$college_data->user_profile_name;
                    $college_website=$college_data->user_website;
                    $college_email=$college_data->user_email;
                    $college_contact_no=$college_data->user_contact_no;
                    $college_address=$college_data->user_address_1.','.$college_data->user_city;
                }else if($institute_user_data->user_type==='institute_branch'){
                    $college_data=$this->um->get_institute_branch(array('branch_user_id'=>$this->inst_id));
                    $college_profile_name=$college_data->branch_name;
                    $college_website=$college_data->branch_website;
                    $college_email=$college_data->branch_email;
                    $college_contact_no=$college_data->branch_contact_no;
                    if($college_data->user_address_1!=''){
                        $college_address.=$college_data->user_address_1;
                    }                        

                    if($college_data->user_address_2!=''){
                        $college_address.=$college_data->user_address_2;
                    }                        

                    if($college_data->user_city!=''){                        
                        $college_address.=','.$college_data->user_city;
                    }

                    if($college_data->user_state!=''){
                        $state_data=$this->sm->_get_states(array('state_id'=>$college_data->user_state));
                        $college_address.=','.$state_data->state_name;
                    }

                    if($college_data->user_pincode!=''){
                        $college_address.=','.$college_data->user_pincode;
                    }
                }else if($institute_user_data->user_type==='student'){
                    if($userdata->stu_inst_type=='stu_inst_type'){
                        $college_data=$this->um->get_institute_branch(array('branch_user_id'=>$this->inst_id));
                        $college_profile_name=$college_data->branch_name;
                        $college_website=$college_data->branch_website;
                        $college_email=$college_data->branch_email;
                        $college_contact_no=$college_data->branch_contact_no;
                        if($college_data->user_address_1!=''){
                            $college_address.=$college_data->user_address_1;
                        }                        

                        if($college_data->user_address_2!=''){
                            $college_address.=$college_data->user_address_2;
                        }                        

                        if($college_data->user_city!=''){                        
                            $college_address.=','.$college_data->user_city;
                        }

                        if($college_data->user_state!=''){
                            $state_data=$this->sm->_get_states(array('state_id'=>$college_data->user_state));
                            $college_address.=','.$state_data->state_name;
                        }

                        if($college_data->user_pincode!=''){
                            $college_address.=','.$college_data->user_pincode;
                        }
                    }else{
                       $college_data=$this->um->get_institute(array('user_pk_id'=>$this->inst_id));
                       $college_profile_name=$college_data->user_profile_name;
                       $college_website=$college_data->user_website;
                       $college_email=$college_data->user_email;
                       $college_contact_no=$college_data->user_contact_no;
                        if($college_data->user_address_1!=''){
                            $college_address.=$college_data->user_address_1;
                        }                        

                        if($college_data->user_address_2!=''){
                            $college_address.=$college_data->user_address_2;
                        }                        

                        if($college_data->user_city!=''){                        
                            $college_address.=','.$college_data->user_city;
                        }

                        if($college_data->user_state!=''){
                            $state_data=$this->sm->_get_states(array('state_id'=>$college_data->user_state));
                            $college_address.=','.$state_data->state_name;
                        }

                        if($college_data->user_pincode!=''){
                            $college_address.=','.$college_data->user_pincode;
                        }
                    }                    
                }

                // $college_img_data=$this->sm->get_file(array('storage_type'=>'college_logo','storage_type_id'=>$this->inst_id));

                // //print_obj($college_img_data);die;

                // if(!empty($college_img_data) && is_file(FCPATH.$college_img_data->media_disk_path)){
                //     $college_logo=base_url($college_img_data->media_disk_path_relative);
                // }else{
                //     $college_logo=$this->data['no_image'];
                // }

                //echo $college_logo;die;

                $college_logo=base_url('uploads/app/jtti_bill_header.png');


                $college_imgseal_data=$this->sm->get_file(array('storage_type'=>'college_seal_logo','storage_type_id'=>$this->inst_id));

                if(!empty($college_imgseal_data) && is_file(FCPATH.$college_imgseal_data->media_disk_path)){
                    $college_seal_logo=base_url($college_imgseal_data->media_disk_path_relative);
                }else{
                    $college_seal_logo='';
                }


                $college_signature_data=$this->sm->get_file(array('storage_type'=>'college_authorize_signature_logo','storage_type_id'=>$this->inst_id));

                if(!empty($college_signature_data) && is_file(FCPATH.$college_signature_data->media_disk_path)){
                    $college_authorized_signature_logo=base_url($college_signature_data->media_disk_path_relative);
                }else{
                    $college_authorized_signature_logo='';
                }

                $college_letter_head_data=$this->sm->get_file(array('storage_type'=>'college_letter_head_image','storage_type_id'=>$this->inst_id));

                if(!empty($college_letter_head_data) && is_file(FCPATH.$college_letter_head_data->media_disk_path)){
                    $college_letter_head=base_url($college_letter_head_data->media_disk_path_relative);
                }else{
                    $college_letter_head='';
                }

                $this->data['in_bill_print']=$this->inst_in_bill_print;

                $booking_id=decode_data($bill_code);

                if(!empty($bill_balance_code)){
                    $current_balance_id=$bill_balance_code;
                }else{
                    $current_balance_id=0;
                }

                $file_data=array();
                $payment_cheque_data=array();
                $adm_fees_data=array();

                if($bill_type=='seatbook'){

                    $seat_booking_data=$this->adm->get_seat_bookings_data(array('booking_id'=>$booking_id));

                    $_payment_details=[];

                    $invd=$this->fm->_get_seat_booking_payments_details(array('booking_id'=>$booking_id));

                    $invd_balance=$this->fm->get_installments_balance(array('inst_installment_id'=>$booking_id),FALSE);

                    $stu_course_custom_cost=$this->sessm->get_session_course_student_wise(array('session_id'=>$invd->booking_sess_id,'session_inst_id'=>$invd->booking_inst_id,'session_course_id'=>$invd->booking_course_id,'session_stu_id'=>$invd->details_stu_id));

                    if(!empty($stu_course_custom_cost)){
                        $course_cost=number_format($stu_course_custom_cost->session_course_fees,2);
                    }else{
                        $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$invd->booking_sess_id,'session_inst_id'=>$invd->booking_inst_id,'session_course_id'=>$invd->booking_course_id));
                        $course_cost=number_format($booking_amount_data->session_course_fees,2);
                    }


                    if(!empty($invd)){

                        //$course_data=$this->sessm->get_session_course(array('session_course_id'=>'','session_inst_id'=>));

                        $booking_name=$invd->stu_first_name.' '.$invd->stu_mid_name.' '.$invd->stu_last_name;

                        if($invd->details_type=='seat_booking'){

                            $pay_mode=ucwords($seat_booking_data->booking_pay_mode);

                            if(!empty($seat_booking_data)){
                                if($seat_booking_data->booking_pay_mode==='cheque'){
                                    $deposite_bank_data=$this->sm->get_banks(array('bank_id'=>$seat_booking_data->booking_payment_deposite_bank));
                                    if(!empty($seat_booking_data->booking_payment_checq_files_ids)){
                                        $cheque_files=$this->sm->get_file_in('storage_id',$seat_booking_data->booking_payment_checq_files_ids);
                                    }else {
                                        $cheque_files='';
                                    }
                                    

                                    if(!empty($cheque_files)){
                                        foreach ($cheque_files as $key => $value) {
                                            $file_data[]=base_url($value->media_disk_path_relative);
                                        }
                                    }

                                    $payment_cheque_data=array(
                                        'bank_name'=>$seat_booking_data->booking_payment_bank,
                                        'bank_cheque_no'=>$seat_booking_data->booking_payment_checq_no,
                                        'bank_deposite_to'=>ucwords($deposite_bank_data->bank_name).' ['.ucwords($deposite_bank_data->bank_branch).' Branch]',
                                        'bank_cheque_files'=>(isset($file_data))?$file_data:null
                                    );  
                                }
                            }                                
                        }

                        if($seat_booking_data->is_lateral=='yes'){
                            $course_name=strtoupper($invd->course_name).' [ '.strtoupper($invd->course_code).'(L)]';
                        }else{
                            $course_name=strtoupper($invd->course_name).' [ '.strtoupper($invd->course_code).' ]';
                        }

                        //print_obj($invd);die;

                        if($invd->booking_subject_id!=null){
                            $payment_subject_data=$this->cm->get_subject(array('subject_id'=>$invd->booking_subject_id));
                            $payment_subject=$payment_subject_data->subject_name;
                        }else{
                            $payment_subject='';
                        }

                        //print_obj($invd);die;

                        if(isset($pay_mode)){
                            $pay_mode=str_replace("split_", "", $pay_mode);
                        }else{
                            $pay_mode="";
                        }

                        $payment_platform=str_replace("split_", "", $invd->details_payment_platform);

                       // echo $pay_mode;die;


                        if(!empty($seat_booking_data)){
                            if($seat_booking_data->is_lateral=='yes'){
                                $course_name=strtoupper($invd->course_name).' [ '.strtoupper($invd->course_code).'(L)]';
                            }else{
                                $course_name=strtoupper($invd->course_name).' [ '.strtoupper($invd->course_code).' ]';
                            }
                        }else{
                            $course_name='';
                        }

                        $invoice_details=array(
                            'payment_code'=>$invd->booking_code,
                            'payment_stu_code'=>strtoupper($invd->stu_user_code),
                            'payment_txn_code'=>$invd->details_txn_id,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$invd->stu_email,
                            'payment_phone_no'=>$invd->stu_ph_no,
                            'payment_course'=>$course_name,
                            'payment_course_amount'=>$course_cost,
                            'payment_subject'=>$payment_subject,
                            'payment_session'=>$invd->session_start_year.'-'.$invd->session_end_year,
                            'payment_amount'=>number_format($invd->details_amount,2),
                            'payment_platform'=>ucwords($payment_platform),
                            'payment_mode'=>$pay_mode,
                            'payment_date'=>date('d/m/Y',strtotime($invd->booking_payment_recieved_date)),
                            'payment_cheque_data'=>$payment_cheque_data,
                            'payment_college'=>$college_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_authorized_signature_logo'=>$college_authorized_signature_logo,
                            'payment_college_seal_logo'=>$college_seal_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_website,
                            'payment_college_email'=>$college_email,
                            'payment_college_ph_no'=>$college_ph_no,
                            'payment_details'=>$_payment_details,
                            'payment_total'=>number_format($payment_total,2)
                        );


                        //print_obj($invoice_details);die;


                        $this->data['invoice_data']=$invoice_details;

                        $this->theme->view('_pages/bills/vw_bill_seatbooking',$this->data);

                    }else{
                        $return['error']='Details not found';  
                    }

                }else if($bill_type=='installments'){

                    $installments=[];

                    $scholarship_total=0;
                    $new_course_fees=0;

                    $adm_data=$this->adm->get_admission_data(array('admission_id'=>$booking_id));

                   //print_obj($adm_data);die;

                    $student_name='';

                    if(!empty($adm_data)){
                        $session_student_course_data=$this->sessm->get_session_course_student_wise(array('session_id'=>$adm_data->admission_sess_id,'session_inst_id'=>$adm_data->admission_inst_id,'session_course_id'=>$adm_data->admission_course_id,'session_stu_id'=>$adm_data->admission_stu_id));

                        $session_course_data=$this->sessm->get_session_course(array('session_id'=>$adm_data->admission_sess_id,'session_inst_id'=>$adm_data->admission_inst_id,'session_course_id'=>$adm_data->admission_course_id));

                        if(!empty($session_student_course_data)){
                            $course_fees=$session_student_course_data->session_course_fees;
                            $course_admission_fees=0;
                        }else{
                            if(!empty($session_course_data)){
                                $course_fees=$session_course_data->session_course_fees;
                                $course_admission_fees=$session_course_data->session_course_booking_fee;
                            }else{
                                $course_fees=0;
                                $course_admission_fees=0;
                            }
                        } 


                        $seat_book_data=$this->adm->get_seat_booking_payment_data(array('booking_pk_id'=>$adm_data->admission_seat_booking_id,'booking_inst_id'=>$adm_data->admission_inst_id));

                        //print_obj($seat_book_data);die;

                        if(!empty($seat_book_data)){
                            $seatbook_amount_exists='yes';
                            $seatbook_amount=$seat_book_data->booking_payment;
                            $remaining_amount=($course_fees>0 && $course_fees>=$seatbook_amount)?($course_fees-$seatbook_amount):'0';
                        }else{
                            $seatbook_amount='0';
                            $seatbook_amount_exists='no';
                            $remaining_amount=$course_fees;
                        }

                        $student_name=(!empty($adm_data->stu_mid_name))?ucwords($adm_data->stu_first_name.' '.$adm_data->stu_mid_name.' '.$adm_data->stu_last_name):ucwords($adm_data->stu_first_name.' '.$adm_data->stu_last_name);

                        $installments_data=$this->fm->get_installments(array('installment_inst_id'=>$adm_data->admission_inst_id,'installment_admission_id'=>$adm_data->admission_id,'installment_sess_id'=>$adm_data->admission_sess_id,'installment_course_id'=>$adm_data->admission_course_id),FALSE);
                       

                        $adm_fees_data=$this->fm->_get_fees_details(array('payment_stu_id'=>$adm_data->admission_stu_id,'payment_inst_id'=>$adm_data->admission_inst_id,'payment_sess_id'=>$adm_data->admission_sess_id,'payment_admission_id'=>$adm_data->admission_id,'payment_course_id'=>$adm_data->admission_course_id));

                        $i=1;
                        
                        if(!empty($installments_data)){

                            $scholarship_total=$installments_data[0]->installment_scholarship_discount+$installments_data[0]->installment_special_discount;

                            $new_course_fees=($course_fees-$scholarship_total);
                            $remaining_amount=($remaining_amount-$scholarship_total);


                            foreach ($installments_data as $key => $value) {
                                $get_installment_payment_details=$this->fm->get_payment_details(array('details_type'=>'new_admission','details_payment_type'=>'installment_fees','details_type_id'=>$adm_data->admission_id,'details_inst_id'=>$adm_data->admission_inst_id,'details_sess_id'=>$adm_data->admission_sess_id,'details_course_id'=>$adm_data->admission_course_id,'details_installment_id'=>$value->installment_id));

                                $get_fees_details=$this->fm->get_fees_details(array('payment_stu_id'=>$adm_data->admission_stu_id,'payment_inst_id'=>$adm_data->admission_inst_id,'payment_session_id'=>$adm_data->admission_sess_id,'payment_course_id'=>$adm_data->admission_course_id,'payment_admission_id'=>$adm_data->admission_id,'payment_installment_id'=>$value->installment_id));
                                
                                if($value->installment_date!='0000-00-00'){
                                    $installment_date=date('d-m-Y',strtotime($value->installment_date));
                                }else{
                                    
                                    $installment_date='N/A';
                                }

                                $installments[]=array(
                                    'installment_no'=>ordinal($value->installment_no),
                                    'installment_id'=>$value->installment_id,
                                    'installment_amount'=>$value->installment_value,
                                    'installment_penalty'=>$value->installment_value_penalty,
                                    'installment_date'=>$installment_date,
                                    'installment_get_fees_details'=>$get_fees_details
                                );

                                $i++;
                            }
                        }
                    }

                    $this->data['student_name']=$student_name;
                    $this->data['course_fees']=$course_fees;
                    $this->data['course_admission_fees']=$course_admission_fees;
                    $this->data['seatbook_amount_exists']=$seatbook_amount_exists;
                    $this->data['seatbook_amount']=$seatbook_amount;
                    $this->data['seatbooking_paid']=$seat_book_data->booking_payment_paid;
                    $this->data['remaining_amount']=$remaining_amount;
                    $this->data['new_course_fees']=$new_course_fees;
                    $this->data['scholarship_total']=$scholarship_total;

                    $this->data['admission_data']=$adm_data;
                    $this->data['admission_fees_data']=$adm_fees_data;
                    $this->data['installments']=$installments;

                    $this->data['collage_data']=array(
                        'college_name'=>$college_profile_name,
                        'college_logo'=>$college_logo,
                        'college_authorized_signature_logo'=>$college_authorized_signature_logo,
                        'college_seal_logo'=>$college_seal_logo,
                        'college_letter_head'=>$college_letter_head,
                        'college_address'=>$college_address,
                        'college_website'=>$college_website,
                        'college_email'=>$college_email,
                        'college_ph_no'=>$college_ph_no
                    );

                    $this->theme->view('_pages/bills/vw_bill_installments_consolidated',$this->data);

                }else if($bill_type=='installment_balance_fees'){

                    $payment_details=$this->fm->get_payment_details(array('details_id'=>$booking_id));

                    $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$payment_details->details_payment_id));

                    $_get_fees_details_last=$this->fm->_get_fees_details_last(array('payment_stu_id'=>$payment_details->details_stu_id,'payment_inst_id'=>$payment_details->details_inst_id,'payment_installment_id'=>$payment_details->details_installment_id));

                    $invd=$this->fm->get_installment(array('installment_id'=>$payment_details->details_installment_id));
                    $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                    $stu_course_custom_cost=$this->sessm->get_session_course_student_wise(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id,'session_stu_id'=>$invd->installment_stu_id));

                    if(!empty($stu_course_custom_cost)){
                        $course_cost=number_format($stu_course_custom_cost->session_course_fees,2);
                    }else{
                        $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id));
                        $course_cost=number_format($booking_amount_data->session_course_fees,2);
                    }

                    // //print_obj($admission_data);die;

                    $booking_name=$admission_data->stu_first_name.' '.$admission_data->stu_mid_name.' '.$admission_data->stu_last_name;

                    $prev_payment_data=$this->fm->_get_payment_details(array('details_type'=>'new_admission','details_installment_id'=>$payment_details->details_installment_id,'details_payment_id'=>$payment_details->details_payment_id),TRUE);


                    //$prev_payment_data=$this->fm->_get_payments_total(array('details_type'=>'new_admission','details_installment_id'=>$payment_details->details_installment_id,'details_payment_id!='=>$payment_details->details_payment_id));

                    $balance_last_data=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$payment_details->details_installment_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admission_data->admission_sess_id));

                    //print_obj($balance_last_data);die;

                    //Check any split payment exists for the last_balance_id

                    if($current_balance_id>0){
                        $split_balance_data=$this->fm->get_installments_paid_total(array('balance_split_id'=>null,'balance_id<'=>$current_balance_id,'inst_installment_id'=>$payment_details->details_installment_id,'inst_stu_id'=>$admission_data->admission_stu_id,'inst_session_id'=>$admission_data->admission_sess_id));

                        if(!empty($split_balance_data)){
                            $split_balance_total=$split_balance_data[0]->total;
                        }else{
                            $split_balance_total=0;
                        }
                    }else{
                        $split_balance_total=0;
                    }

                    $last_payment_data=$this->fm->get_payment_details_last(array('details_type'=>'new_admission','details_payment_type'=>'installment_balance_fees','details_stu_id'=>$admission_data->admission_stu_id,'details_parent_inst_id'=>$admission_data->admission_inst_id,'details_sess_id'=>$admission_data->admission_sess_id,'details_course_id'=>$admission_data->admission_course_id));

                    /*if($payment_data->payment_late_fee_applied=='no'){
                        $amount_payable=$invd->installment_value;
                    }else if($payment_data->payment_late_fee_applied=='yes'){
                        $amount_payable=$invd->installment_value+$invd->installment_value_penalty;
                    }*/

                    $amount_payable=$invd->installment_value;

                    //Late fees
                    if($payment_data->payment_late_fee_applied=='yes'){
                        $late_fee_data=$this->fm->get_late_fees(array('late_fee_inst_id'=>$admission_data->admission_inst_id,'late_fee_stu_id'=>$admission_data->admission_stu_id,'late_fee_course_id'=>$admission_data->admission_course_id,'late_fee_session_id'=>$admission_data->admission_sess_id,'late_fee_admission_id'=>$admission_data->admission_id,'late_fee_installment_id'=>$payment_details->details_installment_id));

                        if(!empty($late_fee_data)){
                            $late_fee=$late_fee_data->late_fee;
                        }else{
                            $late_fee=0;
                        }  
                    }else{
                        $late_fee=0;
                    }

                    $amount_payable=$amount_payable+$late_fee;

                    if($split_balance_total>0){
                        $prev_balance=$amount_payable-$split_balance_total;
                        $due_amount=$prev_balance-$payment_details->details_amount;
                    }else{
                        $prev_balance=$amount_payable-$prev_payment_data->details_prev_total_amount;
                        $due_amount=$prev_balance-$payment_details->details_amount;
                    }

                    $payment_data=array(
                        'payment_admission_code'=>$admission_data->admission_code,
                        'payment_code'=>$last_payment_data->details_order_code,
                        'payment_name'=>ucwords($booking_name),
                        'payment_email'=>$invd->stu_email,
                        'payment_stu_code'=>$invd->stu_user_code,
                        'payment_phone_no'=>$invd->stu_ph_no,
                        'payment_course'=>$invd->course_name,
                        'payment_course_cost'=>$course_cost,
                        'payment_subject'=>(!empty($invd->subject_name))?$invd->subject_name:'',
                        'payment_session'=>$invd->session_start_year.'-'.$invd->session_end_year,
                        'payment_platform'=>ucwords($last_payment_data->details_payment_platform),
                        'payment_is_balance'=>($payment_details->details_payment_type=='installment_balance_fees')?'[ Balance Payment ]':'',
                        'payment_mode'=>'Online',
                        'payment_txn_code'=>$last_payment_data->details_txn_id,
                        'payment_late_fee_applied'=>$payment_data->payment_late_fee_applied,
                        'payment_late_fee'=>number_format($late_fee,2),
                        'payment_balance_amount'=>number_format($prev_balance,2),
                        'payment_prev_amount_paid'=>($split_balance_total>0)?number_format($split_balance_total,2):number_format($prev_payment_data->details_prev_total_amount,2),
                        'payment_amount_paid'=>number_format($payment_details->details_amount,2),
                        'payment_amount'=>number_format($amount_payable,2),
                        'payment_due_amount'=>($due_amount>0)?number_format($due_amount,2):'',
                        'payment_installment_data'=>$invd,
                        'payment_date'=>date('d-m-Y',strtotime($last_payment_data->details_created_at)),
                        'payment_college'=>$college_profile_name,
                        'payment_college_logo'=>$college_logo,
                        'payment_college_letter_head'=>$college_letter_head,
                        'payment_college_address'=>$college_address,
                        'payment_college_website'=>$college_website,
                        'payment_college_email'=>$college_email,
                        'payment_college_ph_no'=>$college_ph_no
                    );

                    //print_obj($payment_data);die;


                    $this->data['invoice_data']=$payment_data;

                   $this->theme->view('_pages/bills/vw_bill_admission_balance_installments',$this->data);
                }else if($bill_type=='installments_fees'){

                        $_payment_data=$this->fm->_get_payment_details(array('details_type'=>'new_admission','details_id'=>$booking_id),TRUE,FALSE);

                        $invd=$this->fm->get_installment(array('installment_id'=>$_payment_data->details_installment_id));

                        $total_payment_data=$this->fm->__get_payment_details(array('details_type'=>'new_admission','details_installment_id'=>$_payment_data->details_installment_id,'details_stu_id'=>$invd->installment_stu_id),"*",FALSE,FALSE);

                       $stu_course_custom_cost=$this->sessm->get_session_course_student_wise(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id,'session_stu_id'=>$invd->installment_stu_id));

                       if(!empty($stu_course_custom_cost)){
                           $course_cost=number_format($stu_course_custom_cost->session_course_fees,2);
                       }else{
                           $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id));
                           $course_cost=number_format($booking_amount_data->session_course_fees,2);
                       }

                        $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));

                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        //print_obj($_payment_data);die;

                        $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_mid_name.' '.$_payment_data->stu_last_name;


                        // if($payment_data->payment_late_fee_applied=='no'){
                        //     $amount_payable=$invd->installment_value;
                        // }else if($payment_data->payment_late_fee_applied=='yes'){
                        //     $amount_payable=$invd->installment_value+$invd->installment_value_penalty;
                        // }

                        $amount_payable=$invd->installment_value;

                        //if($_payment_data->details_payment_platform=='cheque'){
                            $payment_details_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));
                        //}  

                        if($payment_data->pay_mode==''){
                            $payment_mode='Cash';
                        }else{
                            $payment_mode=ucwords($payment_data->pay_mode);
                        }

                        //Late fees
                        if($payment_data->payment_late_fee_applied=='yes'){
                            $late_fee_data=$this->fm->get_late_fees(array('late_fee_inst_id'=>$invd->installment_inst_id,'late_fee_stu_id'=>$invd->installment_stu_id,'late_fee_course_id'=>$invd->installment_course_id,'late_fee_session_id'=>$invd->installment_sess_id,'late_fee_admission_id'=>$invd->installment_admission_id,'late_fee_installment_id'=>$_payment_data->details_installment_id));

                            if(!empty($late_fee_data)){
                                $late_fee=$late_fee_data->late_fee;
                            }else{
                                $late_fee=0;
                            }  
                        }else{
                            $late_fee=0;
                        }

                        $amount_payable=$amount_payable+$late_fee;
                        $payment_totals=[];

                        if(!empty($total_payment_data)){

                            foreach($total_payment_data as $payment){
                                $installment_data=$this->fm->get_installments(array('installment_id'=>$payment->details_installment_id));

                                if($payment->details_payment_platform=='old_payment_before_software'){
                                    $payment_mode='---';
                                }else if($payment->details_payment_platform=='Online_before_software'){
                                    $payment_mode='Online';
                                }else{
                                    $payment_mode=ucwords($payment->details_payment_platform);
                                }                           

                                $payment_totals[]=array(
                                    'installment_no'=>ordinal($installment_data->installment_no),
                                    'installment_amount'=>number_format($payment->details_amount,2),
                                    'installment_created_at'=>date('d-m-Y',strtotime($payment->details_created_at)),
                                    'payment_mode'=>$payment_mode
                                );
                            }
                            
                        }

                        $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_code'=>$_payment_data->details_order_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$_payment_data->stu_email,
                            'payment_stu_code'=>$_payment_data->stu_user_code,
                            'payment_phone_no'=>$_payment_data->stu_ph_no,
                            'payment_course'=>$_payment_data->course_name,
                            'payment_course_cost'=>$course_cost,
                            'payment_subject'=>(!empty($_payment_data->subject_name))?$_payment_data->subject_name:'N/A',
                            'payment_session'=>$_payment_data->session_start_year.'-'.$_payment_data->session_end_year,
                            'payment_platform'=>ucwords($_payment_data->details_payment_platform),
                            'payment_mode'=>ucwords($payment_data->pay_mode),
                            'payment_txn_code'=>$_payment_data->details_txn_id,
                            'payment_late_fee_applied'=>$payment_data->payment_late_fee_applied,
                            'payment_late_fee'=>$late_fee,
                            'payment_balance_amount'=>(!empty($payment_data) && $payment_data->payment_fees_balance_amount>0)?number_format($payment_data->payment_fees_balance_amount,2):'',
                            'payment_amount'=>number_format($amount_payable,2),
                            'payment_amount_paid'=>number_format($_payment_data->details_amount,2),
                            'payment_details_data'=>(isset($payment_details_data) && !empty($payment_details_data))?$payment_details_data:'',
                            'payment_discount'=>($_payment_data->details_discount_amount>0)?number_format($_payment_data->details_discount_amount,2):'0',
                            'payment_installment_data'=>$invd,
                            'payment_date'=>date('d-m-Y',strtotime($_payment_data->details_created_at)),
                            'payment_college'=>$college_profile_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_website,
                            'payment_college_email'=>$college_email,
                            'payment_college_ph_no'=>$college_contact_no,
                            'payment_totals'=>$payment_totals,
                            'total_payment_data'=>$total_payment_data
                        );

                        //print_obj($total_payment_data);die;

                        $this->data['invoice_data']=$payment_data;

                        //print_obj($this->data['invoice_data']);die;


                        $this->theme->view('_pages/bills/vw_bill_admission_installments',$this->data);

                }else if($bill_type=='installments_split_total_fees'){

                    $payment_prev_total=0;

                    $payment_split_balance_details=$this->fm->get_installments_balance(array('balance_id'=>$booking_id));

                    //print_obj($payment_split_balance_details);

                    if(!empty($payment_split_balance_details)){
                        $invd=$this->fm->get_installment(array('installment_id'=>$payment_split_balance_details->inst_installment_id));
                        $balance_details=$this->fm->get_installments_balance(array('balance_split_id'=>$booking_id),FALSE);
                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        $booking_name=$admission_data->stu_first_name.' '.$admission_data->stu_mid_name.' '.$admission_data->stu_last_name;

                        $payment_details_data=$this->fm->get_payment_details(array('details_payment_id'=>$payment_split_balance_details->inst_payment_id,'details_installment_id'=>$payment_split_balance_details->inst_installment_id),FALSE);

                        //print_obj($payment_details_data);die;

                        $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$admission_data->stu_email,
                            'payment_stu_code'=>$admission_data->stu_user_code,
                            'payment_phone_no'=>$admission_data->stu_ph_no,
                            'payment_course'=>$admission_data->course_name,
                            'payment_subject'=>(!empty($admission_data->subject_name))?$admission_data->subject_name:'N/A',
                            'payment_session'=>$admission_data->session_start_year.'-'.$admission_data->session_end_year,
                            'payment_amount'=>number_format($invd->installment_value,2),
                            'payment_booking_date'=>date('d-m-Y',strtotime($admission_data->created_actual_at)),
                            'payment_installment_data'=>$invd,
                            'payment_split_balance'=>$payment_split_balance_details,
                            'payment_prev_total'=>$payment_prev_total,
                            'payment_splits'=>$balance_details,
                            'payment_data'=>$payment_details_data,
                            'payment_college'=>$college_profile_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_website,
                            'payment_college_email'=>$college_email,
                            'payment_college_ph_no'=>$college_contact_no,
                            'payment_totals'=>$total_payment_data                        
                        );

                        $this->data['invoice_data']=$payment_data;
                    }

                    $this->theme->view('_pages/bills/vw_bill_admission_installments_splits_total',$this->data);

                }else if($bill_type=='installments_split_fees'){
                    $payment_prev_total=0;
                    $prev_payments=null;

                    // echo $booking_id;

                    $payment_split_balance_details=$this->fm->get_installments_balance(array('balance_id'=>$booking_id));

                    //print_obj($payment_split_balance_details);die;

                    if(!empty($payment_split_balance_details)){
                        $get_prev_total_payments=$this->fm->get_installment_split_total_paid(array('balance_id<'=>$booking_id,'balance_split_id'=>null,'inst_installment_id'=>$payment_split_balance_details->inst_installment_id,'inst_id'=>$payment_split_balance_details->inst_id,'inst_stu_id'=>$payment_split_balance_details->inst_stu_id,'inst_session_id'=>$payment_split_balance_details->inst_session_id,'inst_course_id'=>$payment_split_balance_details->inst_course_id));

                        if(!empty($get_prev_total_payments) && $get_prev_total_payments[0]->total!=''){
                            $payment_prev_total=$get_prev_total_payments[0]->total;

                            $_prev_payments=$this->fm->get_installments_balance(array('balance_id<'=>$booking_id,'balance_split_id'=>null,'inst_installment_id'=>$payment_split_balance_details->inst_installment_id,'inst_id'=>$payment_split_balance_details->inst_id,'inst_stu_id'=>$payment_split_balance_details->inst_stu_id,'inst_session_id'=>$payment_split_balance_details->inst_session_id,'inst_course_id'=>$payment_split_balance_details->inst_course_id),FALSE);

                            if(!empty($_prev_payments)){
                                foreach($_prev_payments as $k=>$v){
                                    $_prev_child_payments=$this->fm->get_installments_balance(array('balance_split_id'=>$v->balance_id,'inst_installment_id'=>$payment_split_balance_details->inst_installment_id,'inst_id'=>$payment_split_balance_details->inst_id,'inst_stu_id'=>$payment_split_balance_details->inst_stu_id,'inst_session_id'=>$payment_split_balance_details->inst_session_id,'inst_course_id'=>$payment_split_balance_details->inst_course_id),FALSE);
                                    if(!empty($_prev_child_payments)){
                                        foreach($_prev_child_payments as $pp){
                                            $prev_child_payments[$v->balance_id][]=array(
                                                'inst_payment_total'=>number_format($pp->inst_payment_total,2),
                                                'created_at'=>date('d-m-Y',strtotime($pp->created_at))
                                            );
                                        }
                                    }
                                    $prev_payments[]=array(
                                        'inst_payment_total'=>number_format($v->inst_payment_total,2),
                                        'created_at'=>date('d-m-Y',strtotime($v->created_at)),
                                        'prev_child_payments'=>$prev_child_payments[$v->balance_id]
                                    );
                                }
                            }
                        }


                        $invd=$this->fm->get_installment(array('installment_id'=>$payment_split_balance_details->inst_installment_id));
                        $balance_details=$this->fm->get_installments_balance(array('balance_split_id'=>$booking_id),FALSE);

                        $bd_details=[];
                        if(!empty($balance_details)){
                            foreach($balance_details as $bd){
                                $bd_payment=$this->fm->get_payment_details(array('details_payment_id'=>$bd->inst_payment_id,'details_stu_id'=>$bd->inst_stu_id));

                                $bd_details[]=array(
                                    'inst_payment_total'=>$bd->inst_payment_total,
                                    'details_payment_platform'=>ucwords($bd_payment->details_payment_platform),
                                    'created_at'=>date('d-m-Y',strtotime($bd->created_at))
                                );
                            }
                        }

                        //print_obj($balance_details);die;

                        $stu_course_custom_cost=$this->sessm->get_session_course_student_wise(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id,'session_stu_id'=>$invd->installment_stu_id));

                        if(!empty($stu_course_custom_cost)){
                           $course_cost=number_format($stu_course_custom_cost->session_course_fees,2);
                        }else{
                           $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$invd->installment_sess_id,'session_inst_id'=>$invd->installment_inst_id,'session_course_id'=>$invd->installment_course_id));
                           $course_cost=number_format($booking_amount_data->session_course_fees,2);
                        }

                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        $booking_name=$admission_data->stu_first_name.' '.$admission_data->stu_mid_name.' '.$admission_data->stu_last_name;

                        // $payment_details_data=$this->fm->get_payment_details(array('details_payment_id'=>$payment_split_balance_details->inst_payment_id,'details_installment_id'=>$payment_split_balance_details->inst_installment_id),FALSE);

                        $payment_details_data=$this->fm->get_payment_details(array('details_type_id'=>$booking_id,'details_installment_id'=>$payment_split_balance_details->inst_installment_id),FALSE);

                        $_get_fees_details=$this->fm->get_fees_details(array('payment_stu_id'=>$invd->installment_stu_id,
                        'payment_inst_id'=>$invd->installment_inst_id,
                        'payment_session_id'=>$invd->installment_sess_id,
                        'payment_admission_id'=>$invd->installment_admission_id,
                        'payment_course_id'=>$invd->installment_course_id,
                        'payment_installment_id'=>$invd->installment_admission_id),FALSE);

                        //print_obj($payment_details_data);die;

                        $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$admission_data->stu_email,
                            'payment_stu_code'=>$admission_data->stu_user_code,
                            'payment_phone_no'=>$admission_data->stu_ph_no,
                            'payment_course'=>$admission_data->course_name,
                            'payment_course_cost'=>$course_cost,
                            'payment_subject'=>(!empty($admission_data->subject_name))?$admission_data->subject_name:'',
                            'payment_session'=>$admission_data->session_start_year.'-'.$admission_data->session_end_year,
                            'payment_amount'=>number_format($invd->installment_value,2),
                            'payment_booking_date'=>date('d-m-Y',strtotime($admission_data->created_actual_at)),
                            'payment_installment_data'=>$invd,
                            'payment_split_balance'=>$payment_split_balance_details,
                            'payment_prev_total'=>$payment_prev_total,
                            'payment_splits'=>$bd_details,
                            'payment_data'=>$payment_details_data,
                            'payment_prev'=>$prev_payments,
                            'payment_college'=>$college_profile_name,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_letter_head'=>$college_letter_head,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$college_website,
                            'payment_college_email'=>$college_email,
                            'payment_college_ph_no'=>$college_contact_no,
                            'payment_totals'=>$total_payment_data          
                        );


                        //print_obj($payment_data);die;

                        $this->data['invoice_data']=$payment_data;
                    }

                    $this->theme->view('_pages/bills/vw_bill_admission_installments_splits',$this->data);
                }
                else if($bill_type=='installments_part_bill_own'){

                    $_payment_data=$this->fm->_get_payment_details(array('details_type'=>'new_admission','details_payment_type'=>'installment_fees','details_id'=>$booking_id),TRUE,FALSE);


                        //print_obj($_payment_data);die;

                        $invd=$this->fm->get_installment(array('installment_id'=>$_payment_data->details_installment_id));


                        $payment_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));

                        //print_obj($payment_data);die;

                        $admission_data=$this->adm->get_admission_data(array('admission_id'=>$invd->installment_admission_id));

                        //print_obj($_payment_data);die;

                        $booking_name=$_payment_data->stu_first_name.' '.$_payment_data->stu_mid_name.' '.$_payment_data->stu_last_name;


                        if($payment_data->payment_late_fee_applied=='no'){
                            $amount_payable=$invd->installment_value;
                        }else if($payment_data->payment_late_fee_applied=='yes'){
                            $amount_payable=$invd->installment_value+$invd->installment_value_penalty;
                        } 

                        //if($_payment_data->details_payment_platform=='cheque'){
                            $payment_details_data=$this->fm->get_fees_details(array('payment_fees_details_id'=>$_payment_data->details_payment_id));
                        //}  

                         $payment_data=array(
                            'payment_admission_code'=>$admission_data->admission_code,
                            'payment_code'=>$_payment_data->details_order_code,
                            'payment_name'=>ucwords($booking_name),
                            'payment_email'=>$_payment_data->stu_email,
                            'payment_stu_code'=>$_payment_data->stu_user_code,
                            'payment_phone_no'=>$_payment_data->stu_ph_no,
                            'payment_course'=>$_payment_data->course_name,
                            'payment_subject'=>(!empty($_payment_data->subject_name))?$_payment_data->subject_name:'N/A',
                            'payment_session'=>$_payment_data->session_start_year.'-'.$_payment_data->session_end_year,
                            'payment_platform'=>ucwords($_payment_data->details_payment_platform),
                            'payment_mode'=>ucwords($payment_data->pay_mode),
                            'payment_txn_code'=>$_payment_data->details_txn_id,
                            'payment_late_fee_applied'=>$payment_data->payment_late_fee_applied,
                            'payment_late_fee'=>$payment_data->payment_late_fee,
                            'payment_balance_amount'=>(!empty($payment_data) && $payment_data->payment_fees_balance_amount>0)?number_format($payment_data->payment_fees_balance_amount,2):'',
                            'payment_amount'=>number_format($amount_payable,2),
                            'payment_amount_paid'=>number_format($_payment_data->details_amount,2),
                            'payment_details_data'=>(isset($payment_details_data) && !empty($payment_details_data))?$payment_details_data:'',
                            'payment_discount'=>($_payment_data->details_discount_amount>0)?number_format($_payment_data->details_discount_amount,2):'0',
                            'payment_installment_data'=>$invd,
                            'payment_date'=>date('d-m-Y',strtotime($_payment_data->details_created_at)),
                            'payment_college'=>$payment_college,
                            'payment_college_logo'=>$college_logo,
                            'payment_college_address'=>$college_address,
                            'payment_college_website'=>$payment_college_website,
                            'payment_college_email'=>$payment_college_email,
                            'payment_college_ph_no'=>$payment_college_ph_no
                        );

                        $this->data['invoice_data']=$payment_data;

                        //print_obj($this->data['invoice_data']);die;


                       $this->theme->view('_pages/bills/vw_bill_admission_installments',$this->data);
                    
                }else if($bill_type=='agent_bills'){
                    $agt_code=$this->input->get('agtcode');
                    $agent_id=decode_data($agt_code);
                    $stu_code=$this->input->get('stucode');
                    $student_id=decode_data($stu_code);
                    $sesscode=$this->input->get('sesscode');
                    $session=decode_data($sesscode);

                    $param['agent_id']=$agent_id;
                    $param['payment_id']=$booking_id;
                    $param['details_type']='agent_payment';

                    $payment_details=$this->fm->___get_payments_details($param,FALSE,FALSE,FALSE);

                    $student_data=$this->um->get_user(array('user_id'=>$student_id),'student');

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

                        $_student_data=array(
                            'student_name'=>$student_name,
                            'student_code'=>$student_data->stu_user_code,
                            'student_session'=>$session
                        );
                    }

                    $agent_total_payment=$this->fm->get_agent_payment(array('payment_id'=>$booking_id,'agent_id'=>$agent_id));

                    if(!empty($agent_total_payment)){
                        $this->data['total_payment']=number_format($agent_total_payment->amount_value);
                    }else{
                        $this->data['total_payment']=number_format('0');
                    }

                    $agent_payment_data=$this->fm->_get_payments_total(array('details_payment_id'=>$booking_id,'details_agent_id'=>$agent_id,'details_type'=>'agent_payment'));

                    if(!empty($agent_payment_data) && isset($agent_payment_data[0]->total)){
                        $this->data['total_paid']=number_format($agent_payment_data[0]->total);
                    }else{
                        $this->data['total_paid']=number_format('0');
                    }

                    $balance_payments=$this->fm->_get_installments_balance_last(array('inst_balance_type'=>'agent_balance','inst_agent_id'=>$agent_id,'inst_payment_id'=>$booking_id));

                    if(!empty($balance_payments) && isset($balance_payments)){
                        $this->data['total_due']=number_format($balance_payments->inst_payment_balance);
                    }else{
                        $this->data['total_due']=number_format('0');
                    }

                    //print_obj($payment_details);die;

                    if(!empty($payment_details)){
                        foreach ($payment_details as $key => $value) {
                            $_payment_details['payment_data'][]=array(
                                'details_id'=>$value->details_id,
                                'amount_value'=>number_format($value->details_amount),
                                'payment_date'=>date('d-m-Y',strtotime($value->details_created_at)),
                                'txn_code'=>$value->details_txn_id,
                                'payment_type'=>$value->details_msg,
                                'booking_code'=>$value->details_order_code,
                                'student_name'=>$student_name,
                                'student_code'=>$value->stu_user_code,
                                'course_name'=>$value->course_name.' [ '. $value->course_code .' ]',
                                'course_session'=>$value->session_start_year.'-'.$value->session_end_year
                            );
                        }
                    }

                    $this->data['student_data']=$_student_data;

                    $this->data['invoice_data']=$_payment_details;

                    $this->data['college_profile_name']=$college_profile_name;
                    $this->data['college_logo']=$college_logo;
                    $this->data['college_letter_head']=$college_letter_head;
                    $this->data['college_address']=$college_address;
                    $this->data['college_website']=$college_website;
                    $this->data['college_email']=$college_email;
                    $this->data['college_contact_no']=$college_contact_no;


                    //print_obj($this->data['student_data']);die;


                    $this->theme->view('_pages/bills/vw_bill_agent_payments',$this->data);
                }else if($bill_type=='admission_fees'){
                    $admissionpayment_data=$this->fm->get_payment_details(array('details_type'=>'new_admission',
                            'details_payment_type'=>'admission_fees','details_id'=>$booking_id),$db_group);                   
                    
                    $adm_data=$this->adm->get_admission_data(array('admission_id'=>$admissionpayment_data->details_type_id),TRUE,$db_group);

                    $admission_payment_data=$this->fm->get_payment_details(array('details_type'=>'new_admission',
                            'details_payment_type'=>'admission_fees','details_type_id'=>$adm_data->admission_id),$db_group,FALSE);

                    $admissionfees_data=$this->fm->_get_fees_details(array('payment_stu_id'=>$adm_data->stu_user_id,'payment_inst_id'=>$adm_data->stu_inst_id,'payment_sess_id'=>$adm_data->admission_sess_id,'payment_admission_id'=>$adm_data->admission_id,'payment_course_id'=>$adm_data->admission_course_id),TRUE,$db_group);

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

                    $course_fees_data=$this->sessm->__get_session_course_student_wise(array('session_stu_id'=>$adm_data->stu_user_id));

                    $course_fees=number_format($course_fees_data->session_course_fees,2);

                    $this->data['student_name']=$student_name;
                    $this->data['course_fees']=$course_fees;
                    $this->data['course_admission_fees']=$admission_payment_data->details_amount;
                

                    $this->data['admission_data']=$adm_data;
                    $this->data['admission_fees_data']=$admission_payment_data;
                    $this->data['admissionfees_data']=$admissionfees_data;

                   // print_obj($admissionfees_data);

                    $this->data['collage_data']=array(
                        'college_name'=>$college_profile_name,
                        'college_logo'=>$college_logo,
                        'college_authorized_signature_logo'=>$college_authorized_signature_logo,
                        'college_seal_logo'=>$college_seal_logo,
                        'college_letter_head'=>$college_letter_head,
                        'college_address'=>$college_address,
                        'college_website'=>$college_website,
                        'college_email'=>$college_email,
                        'college_ph_no'=>$college_ph_no
                    );

                    $this->theme->view('_pages/bills/vw_bill_admission_fees',$this->data);
                }
                else {
                    redirect($this->data['base_url']);
                }

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

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




    /***Payment ***/

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

                $user_type=$this->user_type;
                $user_id=$this->user_id;

                $_balance_details=array();

                $payment_id=post_data('split_payment_id');

                if(!empty($payment_id)){

                    $payment_id=decode_data($payment_id);
                    $split_payment_details_id=post_data('split_payment_details_id');

                    if(is_numeric($payment_id) && is_numeric($split_payment_details_id)){
                        $payment_split_data=$this->adm->__get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id),TRUE);

                        $payment_splt_data=$this->adm->get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id));

                        //print_obj($payment_split_data);die;

                        $split_payment_details_data=$this->fm->get_payment_details(array('details_id'=>$split_payment_details_id));

                        //print_obj($split_payment_details_data);die;

                        if(!empty($payment_split_data) && !empty($split_payment_details_data)){

                            $stu_data=$this->um->get_students(array('stu_id' => $payment_split_data->booking_stu_profile_id));

                            $booking_id=$payment_split_data->booking_pk_id;
                            $balance_details=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$booking_id,'inst_balance_paid'=>'no'));

                            $split_booking_pay_mode=post_data('split_booking_pay_mode');
                            $split_booking_pay_amount=post_data('split_booking_pay_amount');
                            $booking_amount=$payment_split_data->booking_payment;

                            $agent_id='0';

                           
                            $inst_id=$payment_split_data->booking_inst_id;
                            $parent_inst_id=$payment_split_data->booking_inst_parent_id;
                            $student_user_id=$stu_data->stu_user_id;
                            $session=$payment_split_data->booking_sess_id;
                            $course=$payment_split_data->booking_course_id;
                            $booking_subject_id=$payment_split_data->booking_subject_id;
                            $pay_booking_agent=$payment_split_data->booking_agent_id;
                            $details_discount_amount=0;
                            $utr=null;
                            $txn=ge_rand_code_v1('TXN'.$inst_id.$booking_id);
                            $booking_code=$payment_split_data->admission_code;
                            $booking_order_id=$booking_code;

                            if(!empty($balance_details)){
                                $payment_balance=$balance_details->inst_payment_balance-$split_booking_pay_amount;
                            }else{
                                $payment_balance=$booking_amount-$split_booking_pay_amount;
                            }
                            

                            $details_type='seat_booking';
                            $details_type_text='Seat Booking Fees for '.$course_data->course_name.' Session:'.$session_data->session_start_year.'-'.$session_data->session_end_year.'['.$booking_code.']';
                            $details_payment_type='seatbooking_fees';
                            $user_type=$this->user_type;
                            $details_accounting_type='income';
                            $details_discount_amount='0';

                            $platform='split_'.$split_booking_pay_mode;

                            //echo $payment_balance;die;

                            if($payment_balance>0){

                                $details_status='recieved';
                                $details_msg='recieved';

                                $balance_data=array(
                                    'inst_id'=>$inst_id,
                                    'inst_parent_id'=>$parent_inst_id,
                                    'inst_stu_id'=>$student_user_id,
                                    'inst_session_id'=>$session,
                                    'inst_course_id'=>$course,
                                    'inst_subject_id'=>'0',
                                    'inst_year'=>'0',
                                    'inst_agent_id'=>$agent_id,
                                    'inst_installment_id'=>$booking_id,
                                    'inst_payment_id'=>$payment_id,
                                    'inst_payment_total'=>$split_booking_pay_amount,
                                    'inst_payment_balance'=>$payment_balance,
                                    'inst_balance_paid'=>'no',
                                    'inst_balance_partialy_paid'=>'yes',
                                    'inst_balance_type'=>'seat_booking_split_balance',
                                    'created_at'=>date('Y-m-d'),
                                    'created_by'=>$user_id
                                );

                                $balance_id=$this->fm->store_installments_balance($balance_data);

                                if($balance_id){
                                    $details_type_text='Seat Booking Balance Fees for '.$course_data->course_name.' Session:'.$session_data->session_start_year.'-'.$session_data->session_end_year.'['.$booking_code.']';
                                    $details_payment_type='seatbooking_balance_fees';
                                    $user_type=$this->user_type;
                                    $details_accounting_type='income';
                                    $details_discount_amount='0';
                                    $pay_mode=$platform;

                                    //if(!empty($balance_details)){
                                        $booking_amount=$split_booking_pay_amount;
                                    //}
                                    

                                    $payment_details=compact('details_type','details_type_text','details_payment_type','payment_id','booking_id','inst_id','parent_inst_id','student_user_id','pay_booking_agent','session','course','booking_subject_id','pay_mode','booking_amount','details_discount_amount','txn','utr','booking_code','booking_order_id','user_type','details_accounting_type','details_status','details_msg','user_id');

                                    $payment_details_id=_store_payment_details($payment_details);

                                    $payment_details=$this->fm->get_payment_details(array('details_payment_id'=>$payment_id,'details_type_id'=>$booking_id,'details_inst_id'=>$inst_id,
                                    'details_parent_inst_id'=>$parent_inst_id,
                                    'details_stu_id'=>$student_user_id,'details_sess_id'=>$session,
                                    'details_course_id'=>$course,'details_payment_type'=>'seatbooking_balance_fees'),FALSE);
                                }else{
                                    $payment_details='';
                                }

                                $_balance_details=$this->fm->_get_installments_balance_last(array('inst_installment_id'=>$booking_id,'inst_balance_paid'=>'no'));

                                $return['success']='Payment Has been recieved & balance remain';
                                $return['balance_details']=$_balance_details;
                                $return['payment_details']=$payment_details;                                

                            }else if($payment_balance==0){
                                $details_status='recieved';
                                $details_msg='recieved';

                                $payment_details_id=$this->fm->update_payment_details(array('details_payment_platform'=>$platform,'details_txn_id'=>$txn,'details_utr_id'=>$utr,'details_status'=>$details_status,'details_msg'=>$details_msg),array('details_id'=>$split_payment_details_id));

                                if($payment_details_id){
                                    if($split_booking_pay_amount>0){
                                        $details_type_text='Seat Booking Balance Fees for '.$course_data->course_name.' Session:'.$session_data->session_start_year.'-'.$session_data->session_end_year.'['.$booking_code.']';
                                        $details_payment_type='seatbooking_balance_fees';
                                        $user_type=$this->user_type;
                                        $details_accounting_type='income';
                                        $details_discount_amount='0';
                                        $pay_mode=$platform;

                                        $booking_amount=$split_booking_pay_amount;
                                        

                                        $payment_details=compact('details_type','details_type_text','details_payment_type','payment_id','booking_id','inst_id','parent_inst_id','student_user_id','pay_booking_agent','session','course','booking_subject_id','pay_mode','booking_amount','details_discount_amount','txn','utr','booking_code','booking_order_id','user_type','details_accounting_type','details_status','details_msg','user_id');

                                        $payment_details_id=_store_payment_details($payment_details);
                                    }

                                    $this->adm->update_seat_booking_payment_data(array('booking_payment_paid'=>'yes'),array('booking_payment_id'=>$payment_id));

                                    $this->fm->update_installments_balance(array('inst_balance_paid'=>'yes'),array('inst_installment_id'=>$booking_id));
                                    $return['balance_details']=$_balance_details;

                                    $return['success']='Payment Has been recieved';

                                }else{
                                    $return['error']='Error occurred.Try later';
                                }
                            }

                        }else{
                            $return['error']='Data not found in the system.';
                        }
                    }else{
                        $return['error']='Data not found in the system';
                    }

                }else{
                    $return['error']='Data manipulation not allowed';
                }

               
                json_headers($return);

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

    public function onPaySplit(){
        if(session_userdata('isAdminLoggedin')==TRUE && session_userdata('admin_id')){

            $user_type=$this->user_type;
            $user_id=$this->user_id;

            $_balance_details=array();

            $payment_id=post_data('split_payment_id');

            if(!empty($payment_id)){
                $payment_id=decode_data($payment_id);
                $split_payment_details_id=post_data('split_payment_details_id');

                if(is_numeric($payment_id)){
                    //echo 'hi';
                    $payment_split_data=$this->adm->get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id));

                    //print_obj($payment_split_data);die;

                    if(!empty($payment_split_data)){

                        $student_user_id=$payment_split_data->booking_stu_id;
                        $stu_data=$this->um->get_students(array('stu_user_id' => $student_user_id));
                        $session_data=$this->sessm->get_session(array('session_id'=>$payment_split_data->booking_sess_id));
                        $course_data=$this->cm->get_course(array('course_id'=>$payment_split_data->booking_course_id));

                        //print_obj($course_data);die;

                        $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$stu_data->stu_id));

                        //print_obj($booking_data);die;

                        $booking_id=$payment_split_data->booking_pk_id;
                        $balance_details=$this->fm->_get_installments_balance_last(array('inst_stu_id'=>$student_user_id,'inst_installment_id'=>$booking_id,'inst_balance_paid'=>'no'));

                        $split_booking_pay_mode=post_data('split_booking_pay_mode');
                        $split_booking_pay_amount=post_data('split_booking_pay_amount');
                        $booking_amount=$payment_split_data->booking_payment;
                        
                        $agent_id='0';
                           
                        $inst_id=$payment_split_data->booking_inst_id;
                        $parent_inst_id=$payment_split_data->booking_inst_parent_id;
                        $session=$payment_split_data->booking_sess_id;
                        $course=$payment_split_data->booking_course_id;
                        $booking_subject_id=$payment_split_data->booking_subject_id;
                        $pay_booking_agent=$agent_id;
                        $details_discount_amount=0;
                        $utr=null;
                        $txn=ge_rand_code_v1('TXN'.$inst_id.$booking_id);
                        $booking_code=$booking_data->booking_code;
                        $booking_order_id=$booking_code;
                        $details_parent_id=decode_data($split_payment_details_id);

                        if(!empty($balance_details)){
                            $payment_balance=$balance_details->inst_payment_balance-$split_booking_pay_amount;
                        }else{
                            $payment_balance=$booking_amount-$split_booking_pay_amount;
                        }                        

                        $details_type='seat_booking';
                        $details_type_text='Seat Booking Fees for '.$course_data->course_name.' Session:'.$session_data->session_start_year.'-'.$session_data->session_end_year.'['.$booking_code.']';
                        $details_payment_type='seatbooking_fees';
                        $user_type=$this->user_type;
                        $details_accounting_type='income';
                        $details_discount_amount='0';

                        $platform='split_'.$split_booking_pay_mode;

                        if($payment_balance>0){

                            $details_status='success';
                            $details_msg='recieved';

                            $balance_data=array(
                                'inst_id'=>$inst_id,
                                'inst_parent_id'=>$parent_inst_id,
                                'inst_stu_id'=>$student_user_id,
                                'inst_session_id'=>$session,
                                'inst_course_id'=>$course,
                                'inst_subject_id'=>'0',
                                'inst_year'=>'0',
                                'inst_agent_id'=>$agent_id,
                                'inst_installment_id'=>$booking_id,
                                'inst_payment_id'=>$payment_id,
                                'inst_payment_total'=>$split_booking_pay_amount,
                                'inst_payment_balance'=>$payment_balance,
                                'inst_balance_paid'=>'no',
                                'inst_balance_partialy_paid'=>'yes',
                                'inst_balance_type'=>'seat_booking_split_balance',
                                'created_at'=>date('Y-m-d'),
                                'created_by'=>$user_id
                            );

                            $balance_id=$this->fm->store_installments_balance($balance_data);

                            if($balance_id){                                
                                $details_payment_type='seatbooking_balance_fees';
                                $user_type=$this->user_type;
                                $details_accounting_type='income';
                                $details_discount_amount='0';
                                $pay_mode=$platform;

                                //if(!empty($balance_details)){
                                    $booking_amount=$split_booking_pay_amount;
                                //}
                                

                                $payment_details=compact('details_parent_id','details_type','details_type_text','details_payment_type','payment_id','booking_id','inst_id','parent_inst_id','student_user_id','pay_booking_agent','session','course','booking_subject_id','pay_mode','booking_amount','details_discount_amount','txn','utr','booking_code','booking_order_id','user_type','details_accounting_type','details_status','details_msg','user_id');

                                $payment_details_id=_store_payment_details($payment_details);

                                $payment_details=$this->fm->get_payment_details(array('details_payment_id'=>$payment_id,'details_type_id'=>$booking_id,'details_inst_id'=>$inst_id,
                                    'details_parent_inst_id'=>$parent_inst_id,
                                    'details_stu_id'=>$student_user_id,'details_sess_id'=>$session,
                                    'details_course_id'=>$course,'details_payment_type'=>'seatbooking_balance_fees'),FALSE);
                            }else{
                                $payment_details='';
                            }

                            $_balance_details=$this->fm->_get_installments_balance_last(array('inst_stu_id'=>$student_user_id,'inst_installment_id'=>$booking_id,'inst_balance_paid'=>'no'));

                            $return['success']='Payment Has been recieved & balance remain';
                            $return['balance_details']=$_balance_details;
                            $return['payment_details']=$payment_details;
                            
                            $return['bill_url']='';
                            

                            //print_obj($payment_details);die;
                            
                        }else if($payment_balance==0){
                            $details_status='success';
                            $details_msg='recieved';

                            $data_to_update=array('details_payment_platform'=>$platform,'details_txn_id'=>$txn,'details_utr_id'=>$utr,'details_status'=>$details_status,'details_msg'=>$details_msg);

                            if($split_booking_pay_amount>0){
                                $details_type_text='Seat Booking Balance Fees for '.$course_data->course_name.' Session:'.$session_data->session_start_year.'-'.$session_data->session_end_year.'['.$booking_code.']';
                                $details_payment_type='seatbooking_balance_fees';
                                $user_type=$this->user_type;
                                $details_accounting_type='income';
                                $details_discount_amount='0';
                                $pay_mode=$platform;

                                $booking_amount=$split_booking_pay_amount;                                

                                $payment_details=compact('details_parent_id','details_type','details_type_text','details_payment_type','payment_id','booking_id','inst_id','parent_inst_id','student_user_id','pay_booking_agent','session','course','booking_subject_id','pay_mode','booking_amount','details_discount_amount','txn','utr','booking_code','booking_order_id','user_type','details_accounting_type','details_status','details_msg','user_id');

                                $payment_details_id=_store_payment_details($payment_details);
                            }

                            $this->adm->update_seat_booking_payment_data(array('booking_payment_paid'=>'yes'),array('booking_payment_id'=>$payment_id));

                            $this->fm->update_payment_details(array('details_status'=>'success'),array('details_id'=>$payment_id));

                            $this->fm->update_installments_balance(array('inst_balance_paid'=>'yes'),array('inst_installment_id'=>$booking_id));
                            $return['balance_details']=$_balance_details;

                            $return['success']='Payment Has been recieved';
                            $return['bill_url']=$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($booking_id);

                        }

                        $total_balance_details=$this->fm->get_installments_balance(array('inst_stu_id'=>$student_user_id,'inst_installment_id'=>$booking_id),FALSE);

                        if(!empty($total_balance_details)){
                            foreach ($total_balance_details as $key => $value) {
                                $_total_balance_details[]=array(
                                    'inst_payment_balance'=>'₹'.number_format($value->inst_payment_balance,2),
                                    'inst_payment_total'=>'₹'.number_format($value->inst_payment_total,2),
                                    'created_at'=>date('d-m-Y',strtotime($value->created_at))
                                );
                            }
                        }

                        $return['payment_id']=encode_data($payment_id);
                        $return['payment_details_id']=encode_data($split_payment_details_id);
                        $return['stu_id']=encode_data($student_user_id);
                        $return['inst_id']=encode_data($inst_id);
                        $return['seatbooking_id']=encode_data($booking_data->booking_id);
                        $return['payment_balance_details']=$total_balance_details;
                        $return['payment_balance']=$payment_balance;

                    }else{
                        $return['error']='Payment data not found';
                    }
                }else{
                    $return['error']='Data not found in the system';
                }

                json_headers($return);

            }

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

    

}

MMCT - 2023