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

require_once APPPATH."third_party/PHPExcel.php";
require_once APPPATH."third_party/PHPExcel/IOFactory.php";


/**
 * 
 */
class Seatbook  extends BaseAdminController
{
    protected $inst_id='';
    protected $inst_parent_id='';
    protected $branch_parent_inst_id='';
    protected $user_id='';
    protected $inst_code='';
    protected $inst_ph='';
    protected $inst_email='';
    protected $inst_type='';
    protected $user_type='';
    protected $seatbooking_view_access='';
    protected $seatbooking_add_access='';
    protected $seatbooking_del_access='';
    protected $seatbooking_edit_access='';
    protected $student_upload_limit='';
    protected $student_data_can_be_uploaded='no';
    protected $course_fees_edit_access='no';
    protected $course_booking_fees_edit_access='no';

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

        //print_obj(ge_rand_code('SB'));die;

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

        //print_obj($institute_data);die;

        $this->user_type=$institute_data->user_type;

        $this->inst_id=$institute_data['institute_id'];
        $this->inst_parent_id=$institute_data['inst_parent_id'];
        $this->branch_parent_inst_id=$institute_data['branch_parent_inst_id'];
        $this->inst_ph=$institute_data['inst_ph'];
        $this->inst_email=$institute_data['inst_email'];
        $this->user_id=$institute_data['user_id'];
        $this->inst_code=$institute_data['inst_code'];
        $this->inst_type=$institute_data['inst_type'];
        $this->user_type=$institute_data['user_type'];
        $this->ams_access=$institute_data['ams_access'];
        $this->seatbooking_view_access=$institute_data['seatbooking_view_access'];
        $this->seatbooking_add_access=$institute_data['seatbooking_add_access'];
        $this->seatbooking_del_access=$institute_data['seatbooking_delete_access'];
        $this->seatbooking_edit_access=$institute_data['seatbooking_edit_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'];
    }


    public function index(){

        if(session_userdata('isAdminLoggedin')){

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

            $this->data['page_title']='Seat Bookings';

            if($this->seatbooking_view_access=='yes'){
                
                $check_upload_limit=$this->data['userdata']->student_data_can_be_uploaded;//check_max_upload_limit($this->data['userdata']);                

                $check_account=check_account_renewal_status($this->data['userdata']);

                $session_user_id=decode_data(session_userdata('admin_id'));

                $sessions=$this->sessm->get_session(array('session_inst_id'=>$this->inst_id),FALSE);

                $courses=$this->cm->get_course(array('course_inst_id'=>$this->inst_id),FALSE);

                $session_courses=$this->sessm->get_session_courses(array('session_inst_id'=>$this->inst_id),FALSE);
            
                if(session_userdata('payment_error')){
                    $this->data['payment_error']=session_userdata('payment_error');
                }else if(session_userdata('payment_success')){
                    $this->data['payment_success']='Payment has been recieved';
                }

                if(session_userdata('booking_code')){
                    $seat_booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>session_userdata('booking_code')));

                    //print_obj($seat_booking_data);die;

                    //echo $student_id;die;

                    if(!empty($seat_booking_data)){
                        $session_course_data=$this->sessm->get_session_courses(array('session_id'=>$seat_booking_data->booking_sess_id,'session_course_id'=>$seat_booking_data->booking_course_id,'session_inst_id'=>$seat_booking_data->booking_inst_id));

                        $payment_details=$this->fm->get_payment_details(array('details_stu_id'=>$seat_booking_data->stu_user_id,'details_type'=>'seat_booking','details_payment_type'=>'seatbooking_fees','details_order_code'=>$seat_booking_data->booking_code));

                        // print_obj($payment_details);die;

                        $_seat_booking_data=array(
                            'student_code'=>$seat_booking_data->stu_user_code,
                            'booking_id'=>$seat_booking_data->booking_id,
                            'booking_code'=>$seat_booking_data->booking_code,
                            'course_name'=>$seat_booking_data->course_name,
                            'course_fees'=>(!empty($session_course_data))?number_format($session_course_data->session_course_fees,2):'',
                            'booking_payment'=>number_format($seat_booking_data->booking_payment),
                            'paid_amount'=>(!empty($payment_details) && $seat_booking_data->booking_payment_paid=='yes')?number_format($payment_details->details_amount,2):'0.00',
                            'details_id'=>(!empty($payment_details))?$payment_details->details_id:'',
                            'session_data'=>$seat_booking_data->session_start_year.'-'.$seat_booking_data->session_end_year,
                            'booking_data'=>date('d-m-Y',strtotime($seat_booking_data->created_at)),
                            'payment_date'=>(!empty($payment_details))?date('d-m-Y',strtotime($payment_details->details_created_at)):'--',
                            'payment_paid'=>$seat_booking_data->booking_payment_paid
                        );
                    }else{
                       $_seat_booking_data=array(); 
                    }


                    //print_obj($_seat_booking_data);die;

                    $this->data['booking_data']=$_seat_booking_data;
                    unset($_SESSION['payment_gateway']);
                    unset($_SESSION['payment_error']);
                    unset($_SESSION['payment_success']);
                    unset($_SESSION['booking_code']);
                }

                $this->data['sessions']=$sessions;
                $this->data['courses']=$courses;
                $this->data['session_courses']=$session_courses;

                $data_of='own';

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

                $this->data['student_upload_limit']=$check_upload_limit['student_upload_limit'];
                $this->data['student_data_can_be_uploaded']=$check_upload_limit['student_data_can_be_uploaded'];

                $this->data['expiry_message']=$check_account['expiry_message'];
                $this->data['expired_message']=$check_account['expired_message'];

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

                $view='admission/vw_admission_seatbook_list';
            }else{
                $this->data['bredcrumb_data']='Seat Bookings';
                $view='admission/vw_permission_denied';
            }


            $this->theme->title($this->data['page_title'])->add_partial('partial_upload_excel_modal',$this->data)->add_partial('partial_temp_exceldata_load_modal',$this->data)->load($view, $this->data);

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

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

            if($this->data['userdata']->user_type==='institute_branch'){
                redirect($this->data['base_url'].'/admission/bookings');
            }else{
               $this->data['page_title']='Branch Seat Bookings';

                $session_user_id=decode_data(session_userdata('admin_id'));

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

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

                $data_of='branch';

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


                $this->theme->title($this->data['page_title'])->load('admission/vw_admission_branch_seatbook_list', $this->data); 
            }

                

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


    public function paycallback(){
        echo 'hi';
    }

    public function indexSeatBooking($booking_id=null){
        if(session_userdata('isAdminLoggedin')){

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

            $this->data['page_title']='Seat Booking';

            $this->data['booking_data']=array();
            $this->data['student_academic_data']=array();
            $this->data['agent_payment']=array();
            $this->data['course_fees_data']=array();
            $this->data['bank_accounts'] = array();
            $this->data['booking_id']=$booking_id;
            $_courses=[];
            $_agents=[];

            if($this->data[userdata]->user_role=='1'){
                $this->seatbooking_view_access='yes';
                $this->seatbooking_add_access='yes';
            }

            if($this->seatbooking_add_access=='yes'){

                if($booking_id!=null){
                   $booking_id=decode_data($booking_id);

                   $booking_data=$this->adm->get_seat_bookings_data(array('booking_id'=>$booking_id),TRUE,$db_group);
                   $booking_payment_data=$this->adm->get_seat_booking_payment_data(array('booking_pk_id'=>$booking-id),TRUE,$db_group);
                   $this->data['student_academic_data']=$this->um->get_students_academics(array('acad_stu_id'=>$booking_data->stu_user_id));

                   $this->data['agent_payment']=$this->fm->get_agent_payment(array('inst_id'=>$booking_data->booking_inst_id,'booking_type'=>'seat_booking','session_id'=>$booking_data->booking_sess_id,'course_id'=>$booking_data->booking_course_id,'stu_id'=>$booking_data->stu_user_id));

                   $courses=$this->sessm->get_session_courses(array('session_id'=>$booking_data->booking_sess_id),FALSE);

                   if(!empty($courses)){
                    foreach($courses as $course){
                        $_courses[]=array(
                            'course_id'=>$course->course_id,
                            'course_name'=>$course->course_name,
                            'selected'=>($course->course_id==$booking_data->booking_course_id)?'selected':''
                        );
                    }
                   }

                   $this->data['courses']=$_courses;

                   $this->data['course_fees_data']=$this->sessm->get_session_courses_student_wise(array('session_course_id'=>$booking_data->booking_course_id,'session_stu_id'=>$booking_data->booking_stu_id),TRUE,'session_inst_id','ASC',$db_group);

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

                   
                }

                $check_upload_limit=check_max_upload_limit($this->data['userdata'],$db_group);

                $check_account=check_account_renewal_status($this->data['userdata']);

                $countries=$this->sm->_get_countries(null,FALSE);
                $states=$this->sm->_get_states(null,FALSE);

                $caste_category=$this->cst->get_caste_category(null,FALSE);
                $religions=$this->cst->get_religion(null,FALSE);

                $sessions=$this->sessm->get_session(array('session_inst_id'=>$this->inst_id),FALSE);

                $courses=$this->cm->get_course(array('course_inst_id'=>$this->inst_id),FALSE);

                $session_courses=$this->sessm->get_session_courses(array('session_inst_id'=>$this->inst_id),FALSE);

                //print_obj($session_courses);die;

                $blood_groups=$this->sm->_get_blood_groups(null,FALSE);

                $banks=$this->sm->get_banks(array('bank_inst_id'=>$this->inst_id),FALSE);

                $this->data['agents']=$this->um->get_agents(array('agent_inst_id' =>$this->inst_id,'agent_approval_status'=>'approved'),FALSE);

                // if($this->data['userdata']->user_type=='institite'){
                //     $student_upload_limit=$this->data['userdata']->user_max_students;
                // }else if($this->data['userdata']->user_type=='institute_branch'){
                //     $student_upload_limit=$this->data['userdata']->branch_max_students;
                // }

                $student_upload_limit=$this->student_upload_limit;


                // $max_student_uploaded=$this->um->get_count_students(array('stu_inst_id'=>$this->inst_id));

                // if($student_upload_limit==$max_student_uploaded){
                //     $student_data_can_be_uploaded='no';
                // }else if($student_upload_limi<$max_student_uploaded){
                //     $student_data_can_be_uploaded='yes';
                // }           

                $genders=array(
                    'Male'=>'',
                    'Female'=>'',
                    'Other'=>''
                );

                $this->data['twelfth_boards']= $this->config->item('twelfth_boards');

                $_bank_accounts = $this->sm->get_banks(array('bank_status' => 'active'),FALSE);
                $bank_accounts=array();                

                if($this->ams_access=='yes'){
                    if(!empty($_bank_accounts)){
                        foreach ($_bank_accounts as $key => $value) {
                            $bank_accounts[]=array(
                                'bank_id'=>$value->bank_id,
                                'bank_name'=>$value->bank_name,
                                'bank_account_no'=>$value->bank_account_no
                            );                            
                        }
                    }                    
                } 

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

                $agents=$this->um->get_agents(array('agent_inst_id' =>$this->inst_id,'agent_approval_status'=>'approved'),FALSE);

                if(!empty($agents)){
                    foreach ($agents as $key => $value) {
                        $_agents[]=array(
                            'agent_id'=>$value->agent_id,
                            'agent_name'=>$value->agent_name,
                            'selected'=>($value->agent_id==$booking_data->booking_agent_id)?'selected':''
                        );
                    }
                }

                $this->data['agents']=$_agents;


                $this->data['student_upload_limit']=$student_upload_limit;//$check_upload_limit['student_upload_limit'];
                $this->data['student_data_can_be_uploaded']=$this->student_data_can_be_uploaded;//$check_upload_limit['student_data_can_be_uploaded'];

                $this->data['course_fees_edit_access']=$this->course_fees_edit_access;
                $this->data['course_booking_fees_edit_access']=$this->course_booking_fees_edit_access;

                $this->data['expiry_message']=$check_account['expiry_message'];
                $this->data['expired_message']=$check_account['expired_message'];

                $this->data['countries']=$countries;
                $this->data['states']=$states;

                $this->data['cate_category']=$caste_category;
                $this->data['religions']=$religions;

                $this->data['blood_groups']=$blood_groups;
                $this->data['genders']=$genders;
                $this->data['sessions']=$sessions;
                $this->data['session_courses']=$session_courses;
                $this->data['banks']=$banks;
                $this->data['payment_mode']='direct_payment';
                $this->data['online_payment_url']=$this->data['base_url'].'/admission_bookings_payment_initialize';

                

                $view='admission/vw_admission_seatbook';
            }else{
                $this->data['bredcrumb_data']='Seat Bookings';
                $view='admission/vw_permission_denied';
            }


            if($this->course_fees_edit_access=='yes'){
                $this->theme->title($this->data['page_title'])->add_partial('partial_booking_fee_payment_inst_mode_modal',$this->data)->add_partial('partial_online_payment_form')->add_partial('partial_student_course_fees_update',$this->data)->add_partial('partial_payment_split_modal',$this->data)->load($view, $this->data);
            }else{
                $this->theme->title($this->data['page_title'])->add_partial('partial_booking_fee_payment_inst_mode_modal',$this->data)->add_partial('partial_online_payment_form')->add_partial('partial_payment_split_modal',$this->data)->load($view, $this->data);
            }


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


    public function indexConvertToadmission($booking_id){
        if(session_userdata('isAdminLoggedin')){

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

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

            $_sessions=array();
            $_courses=array();
            $_agents=array();
            $twelfth_boards=array();

            $booking_id=decode_data($booking_id);

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

            if(!empty($booking_data)){

                $countries=$this->sm->_get_countries(null,FALSE);
                $states=$this->sm->_get_states(null,FALSE);
                $tenth_boards= $this->config->item('tenth_boards');
                $_twelfth_boards= $this->config->item('twelfth_boards');

                $stu_academic_data=$this->um->get_students_academics(array('acad_stu_inst_id'=>$this->inst_id,'acad_stu_id'=>$booking_data->booking_stu_id));

                if(!empty($_twelfth_boards)){
                    foreach ($_twelfth_boards as $key => $value) {
                        $twelfth_boards[]=array(
                            'board_id'=>$key,
                            'board_name'=>$key.' - '.$value,
                            'selected'=>(!empty($stu_academic_data) && ($key==$stu_academic_data->acad_12th_board))?'selected':''
                        );
                    }
                }

                $this->data['twelfth_boards']=$twelfth_boards;
                $this->data['tenth_boards']=$tenth_boards;
                $this->data['stu_academic_data']=$stu_academic_data;

                $_subjects=array();

                $caste_category=$this->cst->get_caste_category(null,FALSE);
                $sections=$this->sec->get_section(array('section_institute_id'=>$this->inst_id),FALSE);
                $religions=$this->cst->get_religion(null,FALSE);

                $sessions=$this->sessm->get_session(array('session_inst_id'=>$this->inst_id),FALSE);

                if(!empty($sessions)){
                    foreach ($sessions as $key => $value) {
                        $_sessions[]=array(
                            'session_id'=>$value->session_id,
                            'session_value'=>$value->session_start_year.'-'.$value->session_end_year,
                            'selected'=>($value->session_id==$booking_data->booking_sess_id)?'selected':''
                        );
                    }
                }

                $courses=$this->sessm->get_session_courses(array('session_id'=>$booking_data->booking_sess_id),FALSE);

                //print_obj($courses);die;

                if(!empty($courses)){
                    foreach ($courses as $key => $value) {
                        $_courses[]=array(
                            'course_id'=>$value->course_id,
                            'course_name'=>$value->course_name,
                            'selected'=>($value->course_id==$booking_data->booking_course_id)?'selected':''
                        );
                    }
                }

                $subject_data=$this->cm->get_subject(array('subject_inst_id'=>$this->data['userdata']->user_id,'subject_course_id'=>$booking_data->booking_course_id),FALSE);

                if(!empty($subject_data)){
                    foreach ($subject_data as $key => $value) {
                        $_subjects[]=array(
                            'subject_id'=>$value->subject_id,
                            'subject_name'=>$value->subject_name,
                            'selected'=>($value->subject_id==$booking_data->booking_subject_id)?'selected':''
                        );
                    }
                }


                $blood_groups=$this->sm->_get_blood_groups(null,FALSE);

                $occupations=$this->sm->_get_occupations(null,FALSE);


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

                $genders=array(
                    'Male'=>($booking_data->stu_gender=='Male')?'selected':'',
                    'Female'=>($booking_data->stu_gender=='Female')?'selected':'',
                    'Other'=>($booking_data->stu_gender=='Other')?'selected':'',
                );


                $agents=$this->um->get_agents(array('agent_inst_id' =>$this->data['userdata']->user_id,'agent_approval_status'=>'approved'),FALSE);

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

                        $_agents[]=array(
                            'agent_id'=>$value->agent_user_id,
                            'agent_name'=>$value->agent_name,
                            'selected'=>($value->agent_user_id==$booking_data->booking_agent_id)?'selected':''
                        );
                    }
                }

                $agent_payment=$this->fm->_get_agent_payment(array('institute_payments_agents.booking_id'=>$booking_data->booking_id,'booking_type'=>'seat_booking','inst_id'=>$booking_data->booking_inst_id,'institute_payments_agents.agent_id'=>$booking_data->booking_agent_id));

                $student_course_cost=$this->um->get_course_cost(array('cost_stu_id'=>$booking_data->booking_stu_id,'cost_inst_id'=>$booking_data->booking_inst_id,'cost_inst_type'=>'institute','cost_cousre_id'=>$booking_data->booking_course_id,'cost_session_id'=>$booking_data->booking_sess_id),TRUE,$db_group);

                if(!empty($student_course_cost)){
                    $course_cost=$student_course_cost->cost_value;
                }else{
                    $session_course_cost=$this->sessm->get_session_course(array('session_id'=>$booking_data->booking_sess_id,'session_course_id'=>$booking_data->booking_course_id,'session_inst_id'=>$booking_data->booking_inst_id),TRUE,$db_group);

                    $course_cost=$session_course_cost->session_course_fees;
                }


                $this->data['agent_payment']=(!empty($agent_payment))?number_format($agent_payment->amount_value):0;

                $this->data['countries']=$countries;
                $this->data['states']=$states;
                $this->data['cate_category']=$caste_category;
                $this->data['sections']=$sections;
                $this->data['religions']=$religions;
                $this->data['blood_groups']=$blood_groups;
                $this->data['genders']=$genders;
                $this->data['sessions']=$_sessions;
                $this->data['courses']=$_courses;
                $this->data['course_cost']=$course_cost;
                $this->data['subjects']=$_subjects;
                $this->data['agents']=$_agents;
                $this->data['occupations']=$occupations;
                $this->data['banks']=$banks;

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

                $this->theme->title($this->data['page_title'])->load('admission/vw_admission_seatbook_convert', $this->data);
            }else{
                redirect($this->data['base_url'].'/admission/bookings');
            }
            
        }else{
             redirect($this->data['admin_base_url']);
        }
    }


    public function onAddEditSeatBooking() {
        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;
    
                $seatbooking_data = array();
                $inst_id = $this->inst_id;
                $inst_type = $this->inst_type;
                $inst_email = $this->inst_email;
                $inst_ph = $this->inst_ph;
                $parent_inst_id = $this->inst_parent_id;
                $inst_code = $this->inst_code;
                $student_code_settings=$this->data['student_code_settings'];
                if($userdata->user_type=='agent'){
                    $user_code=$userdata->user_code;
                }else if($userdata->user_type=='employee'){
                    $user_code=$userdata->user_code;
                }
                else{
                    $user_code=$this->inst_code;
                }                
    
                $paid='no-default';
                $cheque_error='';
                $phonepe_error='';
                $payment_id='';
    
                $seat_booking_id = post_data('seat_booking_id');
    
                $first_name = post_data('booking_first_name');
                $middle_name = post_data('booking_middle_name');
                $last_name = post_data('booking_last_name');
    
                $booking_dob = post_data('booking_dob');
                $gender = post_data('booking_gender');
                $ph_no = post_data('booking_ph_no');
    
                $whatsapp_no = post_data('booking_whatsapp_no');
                $email_address = post_data('booking_email_address');
                $nationality = post_data('booking_nationality');
    
                $last_qualification = post_data('booking_last_qualification');
                $passing_year = post_data('booking_last_qualification_passing_year');
                $passing_percentage = post_data('booking_last_qualification_percentage_marks');

                $booking_agent=post_data('booking_agent');
                $booking_agent_payout=post_data('booking_agent_payout');
    
                $session = post_data('booking_session');
                $course = post_data('booking_course');
    
                $booking_subject = post_data('booking_subject');
    
                $booking_amount = post_data('booking_amount');

                $is_lateral=post_data('stu_course_is_lateral');

                $booking_admission_amount=post_data('booking_admission_amount');

                $booking_course_fees=post_data('booking_course_fees');
    
                $booking_pay_now = post_data('booking_pay_now');
    
                $pay_mode = post_data('booking_pay_mode');
    
                $pay_bank_name = post_data('pay_bank_name');
                $pay_bank_cheque_no = post_data('pay_bank_cheque_no');
                $pay_deposite_bank = post_data('pay_deposite_bank');

                if($userdata->user_type=='agent'){
                    $pay_booking_agent = $userdata->agent_id;
                }else{
                    $pay_booking_agent = post_data('booking_pay_agent');
                }
                
    
                $booking_agent_payout_amount = post_data('booking_agent_payout_amount');
    
                $booking_last_qualification_board = post_data('booking_last_qualification_board');

                $total_marks=post_data('booking_last_qualification_total_marks');
    
                $booking_payment_date = post_data('booking_payment_recieved_date');
    
                $booking_payment_note = post_data('booking_payment_note');
    
                $booking_payment_date = date('Y-m-d', strtotime($booking_payment_date));
                $created_at = date('Y-m-d');

                $pay_auto_credit_account=post_data('pay_auto_credit_account');
                $emp_id=$this->user_id;
                $amount_paid=$booking_amount;
    
                // Convert both dates to timestamps
                $booking_timestamp = strtotime($booking_payment_date);
                $current_timestamp = strtotime($created_at);
    
                // Compare the dates
                $booking_is_new = ($booking_timestamp >= $current_timestamp) ? 'yes' : 'no';
    
                // Check course amount changed for the student
                $course_amount_changed = post_data('course_amount_changed');
    
                $student_upload_limit = check_max_upload_limit($this->data['userdata'],$db_group);
    
                if (!empty($seat_booking_id)) {
                    $seat_booking_id = decode_data($seat_booking_id);
                    $seatbooking_data = $this->adm->get_seat_booking_data(array('booking_id' => $seat_booking_id));
                }
    
                if ($this->user_type == 'institute') {
                    $parent_inst_id = $this->inst_id;
                } else if ($this->user_type == 'institute_branch') {
                    $parent_inst_id = $this->branch_parent_inst_id;
                }
    
                // Validations
                $this->form_validation->set_rules('booking_first_name', 'First Name', 'trim|required|alpha_numeric_spaces|max_length[255]');
                $this->form_validation->set_rules('booking_middle_name', 'Middle Name', 'trim|alpha_numeric_spaces|max_length[255]');
                $this->form_validation->set_rules('booking_last_name', 'Last Name', 'trim|required|alpha_numeric_spaces|max_length[255]');
    
                // Custom validation for session and course
                $this->form_validation->set_rules('booking_session', 'Session', 'trim|required|numeric|greater_than[0]', ['greater_than' => 'The %s field must be greater than zero.']);
                $this->form_validation->set_rules('booking_course', 'Course', 'trim|required|numeric|greater_than[0]', ['greater_than' => 'The %s field must be greater than zero.']);
    
                if ($this->form_validation->run() == FALSE) {
                    $validation_error_data = strip_tags($this->form_validation->error_string());
                    $return['error'] = $validation_error_data;
                } else {
                    if($student_upload_limit['student_data_can_be_uploaded']=='yes'){
                        $user_age=calculate_age($booking_dob);
    
                        if($user_age>=17){
                            $booking_impt_data=booking_impt_data($inst_id,$session,$course,$db_group);
    
                            $course_code=$booking_impt_data['course_data']->course_code;
                            $course_id=$booking_impt_data['course_data']->course_id;
                            $course_cost=$booking_impt_data['course_data']->course_cost;
                            $course_name=$booking_impt_data['course_data']->course_name;
                            $session_start_year=$booking_impt_data['session_data']->session_start_year;
                            $session_end_year=$booking_impt_data['session_data']->session_end_year;
    
                            if(!empty($booking_impt_data['get_college_seat_settings'])){
                                $available_seats=$booking_impt_data['get_college_seat_settings']->seats_available;
                            }else{
                                $available_seats='0';
                            }
    
                            if($available_seats>0){
                                if(!empty($booking_impt_data['subject_data']) && $booking_subject==0){
                                    $subject_required=true;
                                    $booking_subject_id=null;
                                }else if(!empty($booking_impt_data['subject_data']) && $booking_subject>0){
                                    $subject_required=false;
                                    $booking_subject_id=$booking_subject;
                                }else{
                                    $subject_required=false;
                                    $booking_subject_id=null;
                                }
    
                                if($subject_required==false){
                                    $password=generate_password('Password@123');
                                    $password_visible='Password@123';
                                    $user_name=$ph_no;
                                    $user_type='student';
                                    $user_status='active';
                                    $created_by_type=$this->user_type;
                                    $created_by=$this->user_id;

                                    echo $seat_booking_id;die;
                                    
                                    $check_duplicate_students_data=check_duplicate_students_data($ph_no);                                                                        
    
                                    if($check_duplicate_students_data==false){
                                        $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$session,'session_inst_id'=>$inst_id,'session_course_id'=>$course),TRUE,'session_inst_id','ASC',$db_group);

                                        $stu_booking_fees=post_data('stu_booking_fees');
                                        $booking_amount=$stu_booking_fees;
                                        $stu_course_last_fees=$booking_amount_data->session_course_fees;
                                        if($course_amount_changed=='yes'){
                                            $stu_course_fees=post_data('stu_course_fees');
                                            $course_booking_amount=($stu_booking_fees>0)?$stu_booking_fees:$stu_course_fees;
                                        }if($course_amount_changed=='no'){
                                            $stu_course_fees= $booking_amount_data->session_course_fees;                                 
                                            $course_booking_amount=($booking_amount_data->session_course_booking_fee>0)?$booking_amount_data->session_course_booking_fee:$booking_amount_data->session_course_fees;
                                        }

                                        if(!empty($booking_amount_data)){
                                            $student_userdata=compact('user_name','password','password_visible','user_type','permissions','user_status','created_by_type','user_id','user_code');
        
                                            $student_user_id=store_users($student_userdata);
        
                                            if(is_numeric($student_user_id)){    
                                                $course_amount_data=compact('session','inst_id','stu_course_fees','student_user_id','course','course_amount_changed');
                                                $booking_amount_data=change_course_amount($course_amount_data);
                                                if(!empty($middle_name)){
                                                    $student_name=$first_name.' '.$middle_name.' '.$last_name;
                                                }else{
                                                    $student_name=$first_name.' '.$last_name;
                                                }    
                                                
                                                if($booking_amount<=$course_booking_amount){
                                                    if($booking_pay_now=='yes'){
                                                        if($pay_mode==='cash'){
                                                            $paid='yes';
                                                        }else if($pay_mode==='online_before_software'){
                                                            $paid='yes';
                                                        }else if($pay_mode==='cheque'){
                                                            if($pay_bank_name!='' && $pay_bank_cheque_no!='' && $pay_deposite_bank>0){
                                                                $paid='yes';
                                                            }else{
                                                                $paid='no';
                                                            }
                                                        }else if($pay_mode==='phonepeutr'){
                                                            $pay_phonepe_utr=post_data('pay_phonepe_utr');
                                                            $pay_phonepe_utr_txn=post_data('pay_phonepe_utr_txn');

                                                            if($pay_phonepe_utr!='' && $pay_phonepe_utr_txn!=''){
                                                                $paid='yes';
                                                            }else{
                                                                $paid='no';
                                                            }
                                                        }else if($pay_mode==='online'){
                                                            $paid='yes';
                                                        }else if($pay_mode==='split'){
                                                            $paid='yes';
                                                        }
                                                    }else{
                                                        $paid='no';
                                                    }

                                                    $is_record_old='no';

                                                    $stu_data_to_add=compact('inst_type','inst_id','parent_inst_id','first_name','middle_name','last_name','student_user_id','student_code','email_address','ph_no','whatsapp_no','booking_dob','gender','nationality','booking_last_qualification_board','last_qualification','passing_year','passing_percentage','created_by','is_record_old');

                                                    //print_obj($stu_data_to_add);die;

                                                    $student_profile_id=store_students_minimal($stu_data_to_add);

                                                    if($student_profile_id){
                                                        $student_id_val=$student_profile_id;
                                                        $course_code=($is_lateral=='yes')?$course_code.'(L)':$course_code;
                                                        $stu_code_data=compact('student_code_settings','user_code','course_code','session_start_year','session_end_year','student_id_val');
                                                        $stu_user_code=generate_stu_code($stu_code_data);
                                                        $this->um->update_students(array('stu_user_code'=>$stu_user_code),array('stu_id'=>$student_profile_id),FALSE,$db_group);                                                        

                                                        $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$student_profile_id,'booking_inst_id'=>$inst_id,'booking_sess_id'=>$session,'booking_course_id'=>$course),TRUE,$db_group);

                                                        if(empty($booking_data)){
                                                            $booking_data_to_store=compact('student_user_id','student_profile_id','inst_id','parent_inst_id','session','course','booking_subject_id','booking_code','pay_booking_agent','created_by','is_lateral');

                                                            $booking_id=store_seat_booking_data($booking_data_to_store);
                                                            if($booking_id && !empty($get_college_seat_settings)){
                                                                $remaining_seats=$available_seats-1;
                                                                $this->sm->update_college_seat_settings(array('seats_available'=>$remaining_seats),array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));
                                                            }

                                                            if($booking_id){
                                                                if($student_user_id){

                                                                    if($booking_admission_amount>0){
                                                                        $booking_admission_fees_data=array(
                                                                            'payment_through'=>'seatbooking',
                                                                            'payment_through_id'=>$booking_id,
                                                                            'payment_stu_id'=>$student_user_id,
                                                                            'payment_inst_id'=>$inst_id,
                                                                            'payment_admission_id'=>'0',
                                                                            'payment_sess_id'=>$session,
                                                                            'payment_course_id'=>$course,
                                                                            'payment_fees_type_id'=>'0',
                                                                            'payment_fees_value'=>$stu_course_fees,
                                                                            'payment_admission_fees_value'=>$booking_admission_amount,
                                                                            'created_by'=>$userdata->user_id,
                                                                            'created_at'=>date('Y-m-d H:i:s')
                                                                        );
                                                                        $this->fm->_store_fees_details($booking_admission_fees_data,false,$db_group);
                                                                    }


                                                                    $this->um->update_students(array('stu_user_id'=>$student_user_id),array('stu_id'=>$student_profile_id));
                                                                    $this->um->delete_students_academics(array('acad_stu_id'=>$student_user_id));
                                                                    //Update Academic Details
                                                                    $academic_details=compact('student_user_id','inst_id','parent_inst_id','booking_last_qualification_board','total_marks','passing_year','passing_percentage','created_by');

                                                                    store_students_academics_minimal($academic_details);

                                                                    //Update Student course cost

                                                                    $course_updated_cost=compact('inst_id','parent_inst_id','inst_type','student_user_id','session','course','stu_course_fees','stu_course_last_fees','created_by');

                                                                    update_stu_course_cost($course_updated_cost);
                                                                }
                                                            }
                                                        }else{
                                                            $booking_id=0;
                                                        }

                                                        /**Agent Data**/
                                                        if($booking_id>0){
                                                            if(!empty($pay_booking_agent) && $pay_booking_agent>0){
                                                                $agent_payment_data=compact('inst_id','parent_inst_id','pay_booking_agent','session','course','student_user_id','booking_subject_id','booking_id','booking_code','booking_agent_payout_amount');
                                                                store_agent_payment($agent_payment_data);
                                                            }
                                                        }

                                                        if($userdata->user_type=='agent'){
                                                            $booking_pay_now='no';
                                                            $paid='no';
                                                        }

                                                        if($booking_pay_now=='yes' && $paid=='yes'){

                                                            if($pay_mode==='phonepeutr'){
                                                                $utr=$pay_phonepe_utr;
                                                                $txn=$pay_phonepe_utr_txn;
                                                            }else if($pay_mode==='online'){
                                                                $utr=null;
                                                                $txn=null;
                                                            }else if($pay_mode==='split'){
                                                                $utr=null;
                                                                $txn=null;
                                                            }
                                                            else{
                                                                $utr=null;
                                                                $txn=ge_rand_code_v1('TXN'.$inst_id.$booking_id);
                                                            }

                                                            if($pay_mode=='cheque'){
                                                                $details_status='waiting';
                                                                $details_msg='Cheque Recieved';
                                                                if(!empty($pay_bank_cheque_no) && !empty($pay_bank_name) && $pay_deposite_bank>0){
                                                                    if(!empty($pay_bank_cheque_no) && !empty($pay_bank_name) && $pay_deposite_bank>0){
                                                                        $cheque_data=compact('storage_dir','student_user_id','inst_id','user_id');
                                                                        $cheques=upload_cheque($cheque_data,$this);
                                                                        $cheque_scan_frontcopy_file_id=$cheques['cheque_scan_frontcopy_file_id'];
                                                                        $cheque_scan_backcopy_file_id=$cheques['cheque_scan_backcopy_file_id'];
                                                                    }

                                                                    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{
                                                                            $booking_payment_checq_files_ids='';
                                                                        }
                                                                        
                                                                    }else{
                                                                        $booking_payment_checq_files_ids='';
                                                                    }

                                                                    $cheque_data=$this->fm->get_cheque_details(array('cheque_details_no'=>$pay_bank_cheque_no));

                                                                    if(empty($cheque_data)){
                                                                        $booking_payment_data=compact('booking_id','inst_id','parent_inst_id','student_user_id','course','booking_subject_id','session','booking_amount','pay_mode','paid','pay_bank_name','pay_bank_cheque_no','booking_payment_checq_files_ids','pay_deposite_bank','paid','booking_payment_date','booking_payment_note','created_by');

                                                                        $payment_id=store_seat_booking_payment_data($booking_payment_data);

                                                                        if($payment_id){
                                                                            $cheque_details_to_add=compact('payment_id','inst_id','parent_inst_id','student_user_id','pay_deposite_bank','pay_bank_name','pay_bank_cheque_no','booking_amount','created_by');

                                                                            store_cheque_details($cheque_details_to_add);
                                                                        }

                                                                        $cheque_error='';

                                                                    }else{
                                                                        $cheque_error='Cheque details already exists in the system with same chque no.Cheque can be recieved later.';
                                                                    }
                                                                }else{
                                                                    $details_status='not recieved yet';
                                                                    $details_msg='not recieved yet';
                                                                    $cheque_error='Cheque details required to complete the payment.You can deposite the Cheque later.';
                                                                }
                                                            }else if($pay_mode=='phonepeutr'){
                                                                if($utr===null && $txn==null){
                                                                    $phonepe_error='Phonepe UTR & TXN no. are required.You can recieve amount later.';
                                                                    $details_status='not recieved yet';
                                                                    $details_msg='not recieved yet';
                                                                }else{
                                                                    $details_status='waiting';
                                                                    $details_msg='Phonepe Recieved';
                                                                }
                                                            }else if($pay_mode=='cash'){
                                                                $details_status='success';
                                                                $details_msg='Cash Recieved';
                                                            }else if($pay_mode=='online_before_software'){
                                                                $details_status='success';
                                                                $details_msg='Online Recieved (before software installation)';
                                                            }else if($pay_mode=='online'){
                                                                $details_status='not yet recieved';
                                                                $details_msg='not yet recieved';
                                                            }else if($pay_mode=='split'){
                                                                $details_status='not yet recieved';
                                                                $details_msg='not yet recieved';
                                                            }

                                                            $booking_payment_data=compact('booking_id','inst_id','parent_inst_id','student_user_id','course','booking_subject_id','session','booking_amount','pay_mode','pay_bank_name','booking_payment_date','booking_payment_note','created_by','paid','created_at');

                                                            $seat_booking_payment_id=store_seat_booking_payment_data($booking_payment_data);

                                                            if($seat_booking_payment_id>0){

                                                                /*$details_type='seat_booking';
                                                                $details_type_text='Seat Booking Fees for '.$booking_impt_data['course_data']->course_name.' Session:'.$booking_impt_data['session_data']->session_start_year.'-'.$booking_impt_data['session_data']->session_end_year.'['.$booking_code.']';
                                                                $details_payment_type='seatbooking_fees';
                                                                $booking_order_id=$booking_code;*/
                                                                $details_status=($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'not recieved yet':'recieved';
                                                                $details_msg=($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'not recieved yet':'recieved';
                                                                $user_type=$this->user_type;
                                                                $details_accounting_type='income';
                                                                $details_discount_amount='0';

                                                                //$booking_code=$booking_code.'_stuinst';
                                                                $payment_details=compact('course_code','session_end_year','session_start_year','seat_booking_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','created_by');

                                                                $payment_details_id=store_seatbooking_payment_details($payment_details);

                                                                if($payment_details_id>0){                                                                        
                                                                    $auto_credit_data=compact('pay_auto_credit_account','inst_id','inst_type','emp_id','payment_details_id','amount_paid');
                                                                    auto_credit($auto_credit_data);
                                                                }
                                                                
                                                                if($agent_commision_error==''){
                                                                    if($booking_subject_id==null){
                                                                        $agent_payment_found=$this->fm->get_agent_payment(array('inst_id'=>$inst_id,'inst_parent_id'=>$parent_inst_id,'agent_id'=>$pay_booking_agent,'booking_type'=>'seat_booking','session_id'=>$session,'course_id'=>$course,'stu_id'=>$student_user_id));
                                                                    }else{
                                                                        $agent_payment_found=$this->fm->get_agent_payment(array('inst_id'=>$inst_id,'inst_parent_id'=>$parent_inst_id,'agent_id'=>$pay_booking_agent,'booking_type'=>'seat_booking','session_id'=>$session,'course_id'=>$course,'subject_id'=>$booking_subject_id,'stu_id'=>$student_user_id));
                                                                    }

                                                                    if(empty($agent_payment_found) && $pay_booking_agent>0){
                                                                        $agent_payment_data=compact('inst_id','parent_inst_id','pay_booking_agent','session','course','student_user_id','booking_subject_id','booking_id','booking_code','booking_agent_payout_amount');

                                                                        store_agent_payment($agent_payment_data);
                                                                    }
                                                                }  
                                                            }

                                                            if($pay_mode!='online'){
                                                                $mail_data['inst_id']=$inst_id;
                                                                $mail_data['inst_parent_id']=$parent_inst_id;
                                                                $mail_data['notify_type']='stu_seat_booking';
                                                                $mail_data['mail_from']='webmaster';
                                                                $mail_data['to_email']=$email_address;
                                                                $mail_data['mail_from_name']=$inst_name;
                                                                $mail_data['mail_subject']=ucwords($this->data['profile_name']).' Seatbook Details';
                                                                $mail_data['mail_type']='html';
                                                                $mail_data['mail_custom_type']='html';
                                                                $mail_data['mail_data']=array(
                                                                    'stu_name'=>$student_name,
                                                                    'stu_email'=>$email_address,
                                                                    'stu_ph'=>$ph_no,
                                                                    'booking_code'=>$booking_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'=>number_format($booking_amount,2),
                                                                    'user_name'=>$email_address,
                                                                    'user_password'=>'Password@123',
                                                                    'portal_url'=>$this->data['base_url'],
                                                                    'portal_logo'=>$this->data['link_logo'],
                                                                    'portal_inst_name'=>$inst_name,
                                                                    'college_email'=>$inst_email,
                                                                    'college_phone'=>$inst_ph,
                                                                    'default_logo_small'=>$this->data['default_logo_small'],
                                                                    'created_at'=>date('d-m-Y')
                                                                );
                                                                
                                                                $mail_data['mail_view']='_pages/mails/vw_seatbook_mail';

                                                                $mail_data['log_user_type']='student';
                                                                $mail_data['log_user_id']=$student_user_id;
                                                                $mail_data['log_type']='student_seat_booking_mail';

                                                                //$mail_msg=$this->onSendMail($mail_data);
                                                                
                                                            }else{
                                                                $mail_msg='';
                                                            }                                                    

                                                            $return['user_code']=$stu_user_code;
                                                            $return['admission_code']=$booking_code;
                                                            $return['admission_booking_amount']=$booking_amount;

                                                            if($pay_mode==='online'){                                                            

                                                                $booking_data=$this->adm->get_seat_bookings_data(array('booking_stu_id'=>$student_user_id,'booking_payment_id'=>$seat_booking_payment_id));

                                                                if(!empty($booking_data)){
                                                                    $_booking_data=array(
                                                                        'booking_code'=>$booking_data->booking_code,
                                                                        'booking_session'=>$booking_data->session_start_year.'-'.$booking_data->session_end_year,
                                                                        'booking_course'=>$booking_data->course_name,
                                                                        'booking_amount'=>$booking_data->booking_payment,
                                                                        'booking_date'=>date('d-m-Y',strtotime($booking_data->created_at))
                                                                    );

                                                                    $booking_session_data=array(
                                                                        'booking_id'=>$booking_data->booking_id,
                                                                        'booking_student_id'=>$booking_data->stu_id,
                                                                        'booking_inst_id'=>$booking_data->stu_inst_id,
                                                                        'booking_session_id'=>$booking_data->booking_sess_id,
                                                                        'booking_course_id'=>$booking_data->booking_course_id,
                                                                        'booking_subject_id'=>$booking_data->booking_subject_id,
                                                                        'booking_code'=>$booking_data->booking_code,
                                                                        'booking_amount'=>$booking_data->booking_payment,
                                                                        'booking_course'=>$booking_data->course_name,
                                                                        'booking_session'=>$booking_data->session_start_year.'-'.$booking_data->session_end_year
                                                                    );

                                                                    session_set_userdata($booking_session_data);
                                                                }

                                                                $return['booking_data']=$_booking_data;
                                                            }


                                                            if($pay_mode=='split' && $payment_id>0){
                                                                $payment_split_data=$this->adm->__get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id),TRUE);
                                                            }

                                                            $return['success']=$agent_commision_error.$cheque_error.$phonepe_error.'Seat has been booked.'.$mail_msg;
                                                            $return['payemnt_id']=encode_data($payment_id);
                                                            $return['payment_booking_id']=encode_data($booking_id);
                                                            $return['payment_details_id']=encode_data($payment_details_id);
                                                            $return['payment_inst_id']=encode_data($inst_id);
                                                            $return['payment_stu_id']=encode_data($student_user_id);
                                                            $return['payment_order_code']=$booking_code.'_stuinst';
                                                            $retunr['payment_mode']=$pay_mode;
                                                            $return['payment_split_data']=$payment_split_data;

                                                            if($pay_mode=='cash'){
                                                                $return['bill_url']=$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($booking_id);
                                                            }else{
                                                                $return['bill_url']='';
                                                            }

                                                        }else if($booking_pay_now=='no' && $paid=='no'){   
                                                            if($booking_id>0){
                                                                $mail_data['inst_id']=$inst_id;
                                                                $mail_data['inst_parent_id']=$parent_inst_id;
                                                                $mail_data['notify_type']='stu_seat_booking';
                                                                $mail_data['mail_from']='webmaster';
                                                                $mail_data['mail_log']=false;
                                                                $mail_data['to_email']=$email_address;
                                                                $mail_data['mail_from_name']=$this->data['profile_name'];
                                                                $mail_data['mail_subject']=ucwords($this->data['profile_name']).' Seatbook Details';
                                                                $mail_data['mail_type']='html';
                                                                $mail_data['mail_custom_type']='html';
                                                                $mail_data['mail_data']=array(
                                                                    'stu_name'=>$student_name,
                                                                    'stu_email'=>$email_address,
                                                                    'stu_ph'=>$ph_no,
                                                                    'booking_code'=>$booking_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'=>number_format($booking_amount,2),
                                                                    'user_name'=>$email_address,
                                                                    'user_password'=>'Password@123',
                                                                    'portal_url'=>$this->data['base_url'],
                                                                    'portal_logo'=>$this->data['link_logo'],
                                                                    'portal_inst_name'=>$this->data['profile_name'],
                                                                    'college_email'=>$this->data['userdata']->user_email,
                                                                    'college_phone'=>$this->data['userdata']->user_contact_no,
                                                                    'default_logo_small'=>$this->data['default_logo_small'],
                                                                    'created_at'=>date('d-m-Y')
                                                                );
                                                                
                                                                $mail_data['mail_view']='_pages/mails/vw_seatbook_mail';

                                                                $mail_data['log_user_type']='student';
                                                                $mail_data['log_user_id']=$student_user_id;
                                                                $mail_data['log_type']='student_seat_booking_mail';

                                                                //$mail_msg=$this->onSendMail($mail_data);

                                                                $return['user_code']=$stu_user_code;
                                                                $return['admission_code']=$booking_code;
                                                                $return['admission_booking_amount']=$booking_amount;

                                                                $return['success']=$agent_commision_error.'Seat has been booked.'.$mail_msg;
                                                            }else{
                                                                $return['error']='There was an error occurred';
                                                            }
                                                        }else{
                                                            if($pay_mode=='cheque'){
                                                                $return['error']='Valid Cheque related data needs to be given to book seat.';
                                                            }else if($pay_mode==='phonepeutr'){
                                                                $return['error']='Valid PhonePe UTR & TXN no. needs to be given to book seat.';
                                                            }
                                                            else{
                                                                $return['error']='Payment mode data not given';
                                                            }
                                                        } 
                                                    }else{
                                                        $return['error']='Data not saved.Error Occurred';
                                                    }                                                    
                                                }else{
                                                    $return['error']='Booking amount can not exceeds the total cost of the course (₹ '.$booking_amount_data->session_course_fees.') or booking amount (₹ '.$booking_amount_data->session_course_booking_fee.') set in the system';
                                                }
        
                                            }else{
                                                $return['error']='There was an error';
                                            } 
                                        }else{
                                            $return['error']='Course cost data not found in the system.';
                                        }
                                        
                                    }else{
                                        $return['error']='Data already exists';
                                    }
                                }else{
                                    $return['error']='Select subject';
                                }
                            }else{
                                $return['error']='No seats available for booking.';
                            }
                        }else{
                            $return['error']='Student is minor.';
                        }
    
                        
    
                    }else if($student_upload_limit['student_data_can_be_uploaded']=='no'){
                        $return['info']='You have reached to your student upload limit of '.$student_upload_limit['student_upload_limit'].' students.';
                    }
                }
    
                json_headers($return);
                session_write_close();
            } else {
                redirect($this->data['base_url']);
            }
        } else {
            redirect($this->data['base_url']);
        }
    }


    


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

                $inst_id=$this->inst_id;
                $inst_type=$this->inst_type;
                $inst_email=$this->inst_email;
                $inst_ph=$this->inst_ph;
                $parent_inst_id=$this->inst_parent_id;
                $inst_code=$this->inst_code;

                $seat_booking_id=post_data('seat_booking_id');

                $first_name=post_data('booking_first_name');
                $middle_name=post_data('booking_middle_name');
                $last_name=post_data('booking_last_name');

                $booking_dob=post_data('booking_dob');
                $gender=post_data('booking_gender');
                $ph_no=post_data('booking_ph_no');

                $whatsapp_no=post_data('booking_whatsapp_no');
                $email_address=post_data('booking_email_address');
                $nationality=post_data('booking_nationality');

                $last_qualification=post_data('booking_last_qualification');
                $passing_year=post_data('booking_last_qualification_passing_year');
                $passing_percentage=post_data('booking_last_qualification_percentage_marks');

                $session=post_data('booking_session');
                $course=post_data('booking_course');

                $booking_subject=post_data('booking_subject');

                $booking_amount=post_data('booking_amount');

                $booking_pay_now=post_data('booking_pay_now');

                $pay_mode=post_data('booking_pay_mode');

                $pay_bank_name=post_data('pay_bank_name');
                $pay_bank_cheque_no=post_data('pay_bank_cheque_no');
                $pay_deposite_bank=post_data('pay_deposite_bank');

                $pay_booking_agent=post_data('booking_pay_agent');

                $booking_agent_payout_amount=post_data('booking_agent_payout_amount');

                $booking_last_qualification_board=post_data('booking_last_qualification_board');

                $booking_payment_date=post_data('booking_payment_recieved_date');

                $booking_payment_note=post_data('booking_payment_note');

                $booking_payment_date=date('Y-m-d',strtotime($booking_payment_date));
                $created_at=date('Y-m-d');

     
                // Convert both dates to timestamps
                $booking_timestamp = strtotime($booking_payment_date);
                $current_timestamp = strtotime($created_at);

                // Compare the dates
                if ($booking_timestamp >= $current_timestamp) {
                    $booking_is_new='yes';
                } else {
                    $booking_is_new='no';
                }
                                

                // if(!empty($booking_payment_date)){
                //     $created_at=date('Y-m-d',strtotime($booking_payment_date));
                // }else{
                //     $created_at=date('Y-m-d');
                // }

                $seatbooking_data=array();


                //Check course amount changed for the student
                $course_amount_changed=post_data('course_amount_changed');

                $student_upload_limit=check_max_upload_limit($this->data['userdata']);

                if(!empty($seat_booking_id)){
                    $seat_booking_id=decode_data($seat_booking_id);
                    $seatbooking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$seat_booking_id));
                }

                

                if($this->user_type=='institute'){
                    $parent_inst_id=$this->inst_id;
                }else if($this->user_type=='institute_branch'){
                    $parent_inst_id=$this->branch_parent_inst_id;
                }

                //Validations
                $this->form_validation->set_rules('booking_first_name', 'First Name', 'trim|required|alpha_numeric_spaces|max_length[255]');   
                $this->form_validation->set_rules('booking_middle_name', 'Middle Name', 'trim|alpha_numeric_spaces|max_length[255]'); 
                $this->form_validation->set_rules('booking_last_name', 'Last Name', 'trim|required|alpha_numeric_spaces|max_length[255]');        

                if ($this->form_validation->run() == FALSE){
                    $validation_error_data = strip_tags($this->form_validation->error_string());
                    $return['error']=$validation_error_data;
                }else{
                    if($student_upload_limit['student_data_can_be_uploaded']=='yes'){

                        $paid='no-default';
                        $cheque_error='';
                        $phonepe_error='';
                        $payment_id='';
                        $course_data=$this->cm->get_course(array('course_id'=>$course,'course_inst_id'=>$inst_id));

                        $session_data=$this->sessm->get_session(array('session_id'=>$session,'session_inst_id'=>$inst_id));

                        $course_session_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_id'=>$session,'session_course_id'=>$course));

                        $get_college_seat_settings=$this->sm->get_college_seat_settings(array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));


                        if(!empty($get_college_seat_settings)){
                            $available_seats=$get_college_seat_settings->seats_available;
                        }else{
                            $available_seats='0';
                        }


                        $student_code_settings=$this->data['student_code_settings'];
                        $user_code=$this->data['user_code'];
                        $course_code=$course_data->course_code;
                        $course_id=$course_data->course_id;
                        $session_start_year=$session_data->session_start_year;
                        $session_end_year=$session_data->session_end_year;

                        if($available_seats>0){
                            $subject_data=$this->cm->get_subject(array('subject_inst_id'=>$inst_id,'subject_course_id'=>$booking_subject),TRUE);

                            if(!empty($subject_data) && $booking_subject==0){
                                $subject_required=true;
                                $booking_subject_id=null;
                            }else if(!empty($subject_data) && $booking_subject>0){
                                $subject_required=false;
                                $booking_subject_id=$booking_subject;
                            }else{
                                $subject_required=false;
                                $booking_subject_id=null;
                            }


                            if($subject_required==false){
                                $password=generate_password('Password@123');
                                $password_visible='Password@123';                

                                $student_userdata=compact('user_name','password','password_visible','user_type','permissions','user_status','created_by_type','user_id','user_code');

                                $student_user_id=store_users($student_userdata);

                                //print_obj($student_user_id);die;

                                if(is_numeric($student_user_id)){
                                    $user_age=calculate_age($booking_dob);

                                    if($user_age>=17){
                                        $booking_amount_data=$this->sessm->get_session_courses(array('session_id'=>$session,'session_inst_id'=>$inst_id,'session_course_id'=>$course));

                                        $stu_booking_fees=post_data('stu_booking_fees');
                                        $booking_amount=$stu_booking_fees;
                                        $stu_course_last_fees=$booking_amount_data->session_course_fees;
                                        if($course_amount_changed=='yes'){
                                            $stu_course_fees=post_data('stu_course_fees');
                                            $course_booking_amount=($stu_booking_fees>0)?$stu_booking_fees:$stu_course_fees;
                                        }if($course_amount_changed=='no'){
                                            $stu_course_fees= $booking_amount_data->session_course_fees;                                 
                                            $course_booking_amount=($booking_amount_data->session_course_booking_fee>0)?$booking_amount_data->session_course_booking_fee:$booking_amount_data->session_course_fees;
                                        }

                                        $this->sessm->store_session_course_student_wise(array('session_id'=>$session,'session_inst_id'=>$inst_id,'session_stu_id'=>$student_user_id,'session_course_id'=>$course,'session_course_fees'=>$stu_course_fees,'session_course_booking_fee'=>$course_booking_amount));
                                        

                                        if(!empty($booking_amount_data)){                                    

                                            if($booking_amount<=$course_booking_amount){
                                                if($booking_pay_now=='yes'){
                                                    if($pay_mode==='cash'){
                                                        $paid='yes';
                                                    }else if($pay_mode==='online_before_software'){
                                                        $paid='yes';
                                                    }else if($pay_mode==='cheque'){
                                                        if($pay_bank_name!='' && $pay_bank_cheque_no!='' && $pay_deposite_bank>0){
                                                            $paid='yes';
                                                        }else{
                                                            $paid='no';
                                                        }
                                                    }else if($pay_mode==='phonepeutr'){
                                                        $pay_phonepe_utr=post_data('pay_phonepe_utr');
                                                        $pay_phonepe_utr_txn=post_data('pay_phonepe_utr_txn');

                                                        if($pay_phonepe_utr!='' && $pay_phonepe_utr_txn!=''){
                                                            $paid='yes';
                                                        }else{
                                                            $paid='no';
                                                        }
                                                    }else if($pay_mode==='online'){
                                                        $paid='yes';
                                                    }else if($pay_mode==='split'){
                                                        $paid='yes';
                                                    }
                                                }else{
                                                    $paid='no';
                                                }

                                                $data_to_add=array(  
                                                    'stu_inst_type'=>$inst_type,                      
                                                    'stu_inst_id'=>$inst_id,
                                                    'stu_inst_parent_id'=>$parent_inst_id,
                                                    'stu_first_name'=>$first_name,
                                                    'stu_mid_name'=>$middle_name,
                                                    'stu_last_name'=>$last_name,
                                                    'stu_email'=>$email_address,
                                                    'stu_ph_no'=>$ph_no,
                                                    'stu_whatsapp_no'=>$whatsapp_no,
                                                    'stu_dob'=>date('Y-m-d',strtotime($booking_dob)),
                                                    'stu_gender'=>$gender,
                                                    'stu_nationality'=>$nationality,
                                                    'stu_last_qualification_board'=>$booking_last_qualification_board,
                                                    'stu_last_qualification'=>$last_qualification,
                                                    'stu_passing_year'=>$passing_year,
                                                    'stu_passing_percentage'=>$passing_percentage,
                                                    'created_by'=>$user_id,
                                                    'created_at'=>$created_at
                                                );



                                                $student_data=$this->um->get_user(array('stu_inst_id'=>$inst_id,'stu_ph_no'=>$ph_no),'student');


                                                if(!empty($pay_booking_agent) && $pay_booking_agent>0){

                                                    $agent_commision_preset=$this->sm->get_agent_commission_settings(array('com_inst_id'=>$inst_id,'com_course_id'=>$course));                                           

                                                    if(!empty($agent_commision_preset)){
                                                        $commision_preset=$agent_commision_preset->com_max_value;

                                                        $agent_preset_dt=compact('inst_id','course_id','session_id','commision_preset');

                                                        $agent_commission=calculate_agent_commision($agent_preset_dt);

                                                        //print_obj($agent_commission);die;

                                                        if($agent_commission<=$stu_course_fees){
                                                            $agent_commision_error='';                                                    
                                                        }else{
                                                            $agent_commision_error='Consultant commission can not exceeds of max commission ₹ '.number_format($stu_course_fees,2).'.';
                                                        }
                                                    }else{
                                                        $agent_commision_error='Consultant commission presets not found for this session and course but ';
                                                    }

                                                }else{
                                                    $agent_commision_error='';
                                                }

                                                if($booking_pay_now=='yes' && $paid=='yes'){

                                                    if(empty($student_data)){

                                                        $student_profile_id=$this->um->store_students($data_to_add);

                                                        if($student_profile_id){


                                                            $student_id_val=$student_profile_id;

                                                            $stu_code_data=compact('student_code_settings','user_code','course_code','session_start_year','session_end_year','student_id_val');

                                                            $stu_user_code=generate_stu_code($stu_code_data);

                                                            $this->um->update_students(array('stu_user_code'=>$stu_user_code),array('stu_id'=>$student_profile_id));

                                                            $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$student_profile_id,'booking_inst_id'=>$inst_id,'booking_sess_id'=>$session,'booking_course_id'=>$course));

                                                            if(empty($booking_data)){

                                                                //$booking_code=ge_rand_code_v1('SB'.$student_profile_id,2,TRUE);

                                                                $booking_code=ge_rand_code('SB');

                                                                $this->adm->delete_seat_booking_data(array('booking_code'=>$booking_code));

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

                                                                $booking_data_to_store=array(
                                                                    'booking_stu_profile_id'=>$student_profile_id,
                                                                    'booking_inst_id'=>$inst_id,
                                                                    'booking_inst_parent_id'=>$parent_inst_id,
                                                                    'booking_sess_id'=>$session,
                                                                    'booking_course_id'=>$course,
                                                                    'booking_subject_id'=>$booking_subject_id,
                                                                    'booking_code'=>$booking_code,
                                                                    'booking_agent_id'=>$pay_booking_agent,
                                                                    'booking_is_new'=>$booking_is_new,
                                                                    'created_by'=>$user_id,
                                                                    'created_at'=>$created_at
                                                                );

                                                                $booking_id=$this->adm->store_seat_booking_data($booking_data_to_store);

                                                                if($booking_id){

                                                                    if(!empty($get_college_seat_settings)){
                                                                        $remaining_seats=$available_seats-1;
                                                                        $this->sm->update_college_seat_settings(array('seats_available'=>$remaining_seats),array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));
                                                                    }
                                                                    

                                                                    if($student_user_id){
                                                                        $this->um->update_students(array('stu_user_id'=>$student_user_id),array('stu_id'=>$student_profile_id));

                                                                        $this->um->delete_students_academics(array('acad_stu_id'=>$student_user_id));

                                                                        //Update Academic Details
                                                                        $academic_details=array(
                                                                            'acad_stu_id'=>$student_user_id,
                                                                            'acad_stu_inst_id'=>$inst_id,
                                                                            'acad_stu_inst_parent_id'=>$parent_inst_id,
                                                                            'acad_grad_board'=>$booking_last_qualification_board,
                                                                            'acad_grad_degree'=>$last_qualification,
                                                                            'acad_grad_yop'=>$passing_year,
                                                                            'acad_grad_percentage'=>$passing_percentage,
                                                                            'created_by'=>$user_id,
                                                                            'created_at'=>$created_at,
                                                                        );

                                                                        $this->um->store_students_academics($academic_details);
                                                                        

                                                                        if(!empty($pay_bank_cheque_no) && !empty($pay_bank_name) && $pay_deposite_bank>0){
                                                                            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'=>$student_user_id,
                                                                                    'file_parent_id'=>$inst_id,
                                                                                    'file_compress'=>false,
                                                                                    'file_operation_type'=>'new',
                                                                                    'file_uploaded_by'=>$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'=>$student_user_id,
                                                                                    'file_parent_id'=>$inst_id,
                                                                                    'file_compress'=>false,
                                                                                    'file_operation_type'=>'new',
                                                                                    'file_uploaded_by'=>$user_id
                                                                                );

                                                                                $cheque_scan_backcopy_file_id=$this->onUploadFiles($cheque_scan_backcopy_file);
                                                                            }
                                                                        }                                                                        
                                                                    }


                                                                    if($pay_mode==='phonepeutr'){
                                                                        $utr=$pay_phonepe_utr;
                                                                        $txn=$pay_phonepe_utr_txn;
                                                                    }else if($pay_mode==='online'){
                                                                        $utr=null;
                                                                        $txn=null;
                                                                    }else if($pay_mode==='split'){
                                                                        $utr=null;
                                                                        $txn=null;
                                                                    }
                                                                    else{
                                                                        $utr=null;
                                                                        $txn=ge_rand_code_v1('TXN'.$inst_id.$booking_id);
                                                                    }


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

                                                                    if($pay_mode=='cheque'){
                                                                        $details_status='waiting';
                                                                        $details_msg='Cheque Recieved';
                                                                        if(!empty($pay_bank_cheque_no) && !empty($pay_bank_name) && $pay_deposite_bank>0){
                                                                            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{
                                                                                    $booking_payment_checq_files_ids='';
                                                                                }
                                                                                
                                                                            }else{
                                                                                $booking_payment_checq_files_ids='';
                                                                            }

                                                                            //echo $booking_payment_checq_files_ids;die;



                                                                            $cheque_data=$this->fm->get_cheque_details(array('cheque_details_no'=>$pay_bank_cheque_no));

                                                                            if(empty($cheque_data)){
                                                                                 $booking_payment_data=array(
                                                                                    'booking_pk_id'=>$booking_id,
                                                                                    'booking_inst_id'=>$inst_id,
                                                                                    'booking_inst_parent_id'=>$parent_inst_id,
                                                                                    'booking_stu_id'=>$student_user_id,
                                                                                    'booking_course_id'=>$course,
                                                                                    'booking_subject_id'=>$booking_subject_id,
                                                                                    'booking_sess_id'=>$session,
                                                                                    'booking_payment'=>$booking_amount,
                                                                                    'booking_pay_mode'=>$pay_mode,
                                                                                    'booking_payment_bank'=>$pay_bank_name,
                                                                                    'booking_payment_checq_no'=>($pay_mode==='cheque')?$pay_bank_cheque_no:null,
                                                                                    'booking_payment_checq_files_ids'=>($pay_mode==='cheque')?$booking_payment_checq_files_ids:null,
                                                                                    'booking_payment_deposite_bank'=>$pay_deposite_bank,
                                                                                    'booking_payment_paid'=>($pay_mode=='cheque' || $pay_mode=='online')?'no':$paid,
                                                                                    'booking_payment_recieved_date'=>$booking_payment_date,
                                                                                    'booking_payment_note'=>$booking_payment_note,
                                                                                    'created_at'=>$created_at,
                                                                                    'created_by'=>$user_id
                                                                                );

                                                                                $payment_id=$this->adm->store_seat_booking_payment_data($booking_payment_data);

                                                                                if($payment_id){

                                                                                    $cheque_details_to_add=array(
                                                                                        'cheque_details_payment_id'=>$payment_id,//PK of nt_institute_payments_details
                                                                                        'cheque_details_inst_id'=>$inst_id,
                                                                                        'cheque_details_inst_parent_id'=>$parent_inst_id,
                                                                                        'cheque_details_stu_id'=>$student_user_id,
                                                                                        'cheque_details_agent_id'=>'0',
                                                                                        'cheque_details_deposite_bank_id'=>$pay_deposite_bank,
                                                                                        'cheque_details_bank_name'=>$pay_bank_name,
                                                                                        'cheque_details_no'=>$pay_bank_cheque_no,
                                                                                        'cheque_details_amount'=>$booking_amount,
                                                                                        'cheque_details_bounce_amount'=>'0',
                                                                                        'cheque_is_cleared'=>'no',
                                                                                        'cheque_is_bounced'=>'no',
                                                                                        'cheque_details_recieved_date'=>$created_at,
                                                                                        'cheque_details_deposite_date'=>null,
                                                                                        'cheque_details_cash_date'=>null,
                                                                                        'cheque_details_created_by'=>$user_id
                                                                                    );


                                                                                    $this->fm->store_cheque_details($cheque_details_to_add);
                                                                                }

                                                                                $cheque_error='';

                                                                            }else{
                                                                                $cheque_error='Cheque details already exists in the system with same chque no.Cheque can be recieved later.';
                                                                            }
                                                                        }else{
                                                                            $details_status='not recieved yet';
                                                                            $details_msg='not recieved yet';
                                                                            $cheque_error='Cheque details required to complete the payment.You can deposite the Cheque later.';
                                                                        }
                                                                    }else if($pay_mode=='phonepeutr'){
                                                                        if($utr===null && $txn==null){
                                                                            $phonepe_error='Phonepe UTR & TXN no. are required.You can recieve amount later.';
                                                                            $details_status='not recieved yet';
                                                                            $details_msg='not recieved yet';
                                                                        }else{
                                                                            $details_status='waiting';
                                                                            $details_msg='Phonepe Recieved';
                                                                        }
                                                                    }else if($pay_mode=='cash'){
                                                                        $details_status='success';
                                                                        $details_msg='Cash Recieved';
                                                                    }else if($pay_mode=='online_before_software'){
                                                                        $details_status='success';
                                                                        $details_msg='Online Recieved (before software installation)';
                                                                    }else if($pay_mode=='online'){
                                                                        $details_status='not yet recieved';
                                                                        $details_msg='not yet recieved';
                                                                    }else if($pay_mode=='split'){
                                                                        $details_status='not yet recieved';
                                                                        $details_msg='not yet recieved';
                                                                    }




                                                                    if(!empty($middle_name)){
                                                                        $student_name=$first_name.' '.$middle_name.' '.$last_name;
                                                                    }else{
                                                                        $student_name=$first_name.' '.$last_name;
                                                                    }

                                                                    //$course_data->course_code.'/'.


                                                                    if(empty($payment_id)){
                                                                        $booking_payment_data=array(
                                                                            'booking_pk_id'=>$booking_id,
                                                                            'booking_inst_id'=>$inst_id,
                                                                            'booking_inst_parent_id'=>$parent_inst_id,
                                                                            'booking_stu_id'=>$student_user_id,
                                                                            'booking_course_id'=>$course,
                                                                            'booking_subject_id'=>$booking_subject_id,
                                                                            'booking_sess_id'=>$session,
                                                                            'booking_payment'=>$booking_amount,
                                                                            'booking_pay_mode'=>$pay_mode,
                                                                            'booking_payment_bank'=>$pay_bank_name,
                                                                            'booking_payment_checq_no'=>null,
                                                                            'booking_payment_checq_files_ids'=>null,
                                                                            'booking_payment_deposite_bank'=>null,
                                                                            'booking_payment_paid'=>($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'no':$paid,
                                                                            'booking_payment_recieved_date'=>$booking_payment_date,
                                                                            'booking_payment_note'=>$booking_payment_note,
                                                                            'created_at'=>$created_at,
                                                                            'created_by'=>$user_id
                                                                        );

                                                                        $payment_id=$this->adm->store_seat_booking_payment_data($booking_payment_data);
                                                                    }

                                                                    if($payment_id>0){

                                                                        $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';
                                                                        $booking_order_id=$booking_code;
                                                                        $details_status=($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'not recieved yet':'recieved';
                                                                        $details_msg=($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'not recieved yet':'recieved';
                                                                        $user_type=$this->user_type;
                                                                        $details_accounting_type='income';
                                                                        $details_discount_amount='0';

                                                                       /* $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);*/
                                                                        
                                                                        if($agent_commision_error==''){
                                                                            if($booking_subject_id==null){
                                                                                $agent_payment_found=$this->fm->get_agent_payment(array('inst_id'=>$inst_id,'inst_parent_id'=>$parent_inst_id,'agent_id'=>$pay_booking_agent,'booking_type'=>'seat_booking','session_id'=>$session,'course_id'=>$course,'stu_id'=>$student_user_id));
                                                                            }else{
                                                                                $agent_payment_found=$this->fm->get_agent_payment(array('inst_id'=>$inst_id,'inst_parent_id'=>$parent_inst_id,'agent_id'=>$pay_booking_agent,'booking_type'=>'seat_booking','session_id'=>$session,'course_id'=>$course,'subject_id'=>$booking_subject_id,'stu_id'=>$student_user_id));
                                                                            }

                                                                            if(empty($agent_payment_found)){
                                                                                $agent_payment_data=array(
                                                                                    'booking_id'=>$booking_id,
                                                                                    'booking_code'=>$booking_code,
                                                                                    'inst_id'=>$inst_id,
                                                                                    'inst_parent_id'=>$parent_inst_id,
                                                                                    'stu_id'=>$student_user_id,
                                                                                    'agent_id'=>$pay_booking_agent,
                                                                                    'booking_type'=>'seat_booking',
                                                                                    'session_id'=>$session,
                                                                                    'course_id'=>$course,
                                                                                    'subject_id'=>$booking_subject_id,
                                                                                    'amount_value'=>$booking_agent_payout_amount,
                                                                                    'amount_paid'=>($pay_mode=='cheque' || $pay_mode=='online' || $pay_mode=='split')?'no':'no',
                                                                                    'amount_created_at'=>$created_at
                                                                                );

                                                                                $this->fm->store_agent_payment($agent_payment_data);

                                                                                $payment_details=array(
                                                                                    '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',
                                                                                    '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_agent_id'=>($pay_booking_agent>0)?$pay_booking_agent:null,
                                                                                    'details_sess_id'=>$session,
                                                                                    'details_course_id'=>$course,
                                                                                    'details_subject_id'=>$booking_subject_id,
                                                                                    'details_payment_platform'=>$pay_mode,
                                                                                    'details_amount'=>$booking_amount,
                                                                                    'details_discount_amount'=>'0',
                                                                                    'details_txn_id'=>$txn,
                                                                                    'details_utr_id'=>$utr,
                                                                                    'details_cheque_no'=>($pay_mode==='cheque')?$pay_bank_cheque_no:null,
                                                                                    'details_pay_checq_files_ids'=>($pay_mode==='cheque')?$booking_payment_checq_files_ids:null,
                                                                                    'details_order_id'=>$booking_code.'_stuinst',
                                                                                    'details_order_code'=>$booking_code,
                                                                                    'details_accounting_type'=>'income',
                                                                                    'details_status'=>$details_status,
                                                                                    'details_msg'=>$details_msg,
                                                                                    'details_created_by_type'=>$this->data['userdata']->user_type,
                                                                                    'details_msg'=>($pay_mode!=='online')?ucwords($pay_mode.' recieved'):'',
                                                                                    'details_created_by'=>$user_id,
                                                                                    'details_created_at'=>$created_at
                                                                                );

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

                                                                    if($pay_mode!='online'){
                                                                        $mail_data['inst_id']=$inst_id;
                                                                        $mail_data['inst_parent_id']=$parent_inst_id;
                                                                        $mail_data['notify_type']='stu_seat_booking';
                                                                        $mail_data['mail_from']='webmaster';
                                                                        $mail_data['to_email']=$email_address;
                                                                        $mail_data['mail_from_name']=$inst_name;
                                                                        $mail_data['mail_subject']=ucwords($this->data['profile_name']).' Seatbook Details';
                                                                        $mail_data['mail_type']='html';
                                                                        $mail_data['mail_custom_type']='html';
                                                                        $mail_data['mail_data']=array(
                                                                            'stu_name'=>$student_name,
                                                                            'stu_email'=>$email_address,
                                                                            'stu_ph'=>$ph_no,
                                                                            'booking_code'=>$booking_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'=>number_format($booking_amount,2),
                                                                            'user_name'=>$email_address,
                                                                            'user_password'=>'Password@123',
                                                                            'portal_url'=>$this->data['base_url'],
                                                                            'portal_logo'=>$this->data['link_logo'],
                                                                            'portal_inst_name'=>$inst_name,
                                                                            'college_email'=>$inst_email,
                                                                            'college_phone'=>$inst_ph,
                                                                            'default_logo_small'=>$this->data['default_logo_small'],
                                                                            'created_at'=>date('d-m-Y')
                                                                        );
                                                                        
                                                                        $mail_data['mail_view']='_pages/mails/vw_seatbook_mail';

                                                                        $mail_data['log_user_type']='student';
                                                                        $mail_data['log_user_id']=$student_user_id;
                                                                        $mail_data['log_type']='student_seat_booking_mail';

                                                                        //$mail_msg=$this->onSendMail($mail_data);
                                                                        
                                                                    }else{
                                                                        $mail_msg='';
                                                                    }                                                    

                                                                    $return['user_code']=$stu_user_code;
                                                                    $return['admission_code']=$booking_code;
                                                                    $return['admission_booking_amount']=$booking_amount;

                                                                    if($pay_mode==='online'){                                                            

                                                                        $booking_data=$this->adm->get_seat_bookings_data(array('booking_stu_profile_id'=>$student_profile_id,'booking_payment_id'=>$payment_id));

                                                                        if(!empty($booking_data)){
                                                                            $_booking_data=array(
                                                                                'booking_code'=>$booking_data->booking_code,
                                                                                'booking_session'=>$booking_data->session_start_year.'-'.$booking_data->session_end_year,
                                                                                'booking_course'=>$booking_data->course_name,
                                                                                'booking_amount'=>$booking_data->booking_payment,
                                                                                'booking_date'=>date('d-m-Y',strtotime($booking_data->created_at))
                                                                            );

                                                                            $booking_session_data=array(
                                                                                'booking_id'=>$booking_data->booking_id,
                                                                                'booking_student_id'=>$booking_data->stu_id,
                                                                                'booking_inst_id'=>$booking_data->stu_inst_id,
                                                                                'booking_session_id'=>$booking_data->booking_sess_id,
                                                                                'booking_course_id'=>$booking_data->booking_course_id,
                                                                                'booking_subject_id'=>$booking_data->booking_subject_id,
                                                                                'booking_code'=>$booking_data->booking_code,
                                                                                'booking_amount'=>$booking_data->booking_payment,
                                                                                'booking_course'=>$booking_data->course_name,
                                                                                'booking_session'=>$booking_data->session_start_year.'-'.$booking_data->session_end_year
                                                                            );

                                                                            session_set_userdata($booking_session_data);
                                                                        }

                                                                        $return['booking_data']=$_booking_data;
                                                                    }


                                                                    if($pay_mode=='split' && $payment_id>0){
                                                                        $payment_split_data=$this->adm->__get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id),TRUE);
                                                                    }

                                                                        

                                                                    $return['success']=$agent_commision_error.$cheque_error.$phonepe_error.'Seat has been booked.'.$mail_msg;
                                                                    $return['payemnt_id']=encode_data($payment_id);
                                                                    $return['payment_booking_id']=encode_data($booking_id);
                                                                    $return['payment_details_id']=encode_data($payment_details_id);
                                                                    $return['payment_inst_id']=encode_data($inst_id);
                                                                    $return['payment_stu_id']=encode_data($student_user_id);
                                                                    $return['payment_order_code']=$booking_code.'_stuinst';
                                                                    $retunr['payment_mode']=$pay_mode;
                                                                    $return['payment_split_data']=$payment_split_data;

                                                                    if($pay_mode=='cash'){
                                                                        $return['bill_url']=$this->data['base_url'].'/bills?type=seatbook&code='.encode_data($booking_id);
                                                                    }else{
                                                                        $return['bill_url']='';
                                                                    }
                                                                    
                                                                }else{
                                                                    $return['error']='There was an error occurred';
                                                                }

                                                            }else{
                                                                $return['error']='Data already exists1';
                                                            }
                                                        }else{
                                                            $return['error']='Data not saved.Error Occurred';
                                                        }

                                                    }else if(!empty($student_data)){

                                                        $return['error']='Data already exists';
                                                    }
                                                }else if($booking_pay_now=='no' && $paid=='no'){

                                                    if(empty($student_data)){
                                                        $student_profile_id=$this->um->store_students($data_to_add);
                                                        if($student_profile_id){
                                                            
                                                            $student_id_val=$student_profile_id;

                                                            $stu_code_data=compact('student_code_settings','user_code','course_code','session_start_year','session_end_year','student_id_val');

                                                            $stu_user_code=generate_stu_code($stu_code_data);

                                                            $this->um->update_students(array('stu_user_code'=>$stu_user_code),array('stu_id'=>$student_profile_id));

                                                            $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$student_profile_id,'booking_inst_id'=>$inst_id,'booking_sess_id'=>$session,'booking_course_id'=>$course));

                                                            if(empty($booking_data)){

                                                                // $booking_code=ge_rand_code('SB'.$student_profile_id,2,TRUE);

                                                                $booking_code=ge_rand_code('SB');

                                                                //$this->adm->delete_seat_booking_data(array('booking_code'=>$booking_code));

                                                                //$this->fm->delete_payment_details(array('details_order_code'=>$booking_code));

                                                                $booking_data_to_store=array(
                                                                    'booking_stu_profile_id'=>$student_profile_id,
                                                                    'booking_inst_id'=>$inst_id,
                                                                    'booking_inst_parent_id'=>$parent_inst_id,
                                                                    'booking_sess_id'=>$session,
                                                                    'booking_course_id'=>$course,
                                                                    'booking_subject_id'=>$booking_subject_id,
                                                                    'booking_code'=>$booking_code,
                                                                    'booking_agent_id'=>$pay_booking_agent,                               
                                                                    'created_by'=>$user_id,
                                                                    'created_at'=>$created_at
                                                                );

                                                                $booking_id=$this->adm->store_seat_booking_data($booking_data_to_store);

                                                                if($booking_id){

                                                                    if(!empty($get_college_seat_settings)){
                                                                        $remaining_seats=$available_seats-1;
                                                                        $this->sm->update_college_seat_settings(array('seats_available'=>$remaining_seats),array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));
                                                                    }

                                                                    if($student_user_id){
                                                                        $this->um->update_students(array('stu_user_id'=>$student_user_id),array('stu_id'=>$student_profile_id));
                                                                        $this->um->delete_students_academics(array('acad_stu_id'=>$student_user_id));
                                                                        //Update Academic Details
                                                                        $academic_details=array(
                                                                            'acad_stu_id'=>$student_user_id,
                                                                            'acad_stu_inst_id'=>$inst_id,
                                                                            'acad_stu_inst_parent_id'=>$parent_inst_id,
                                                                            'acad_grad_board'=>$booking_last_qualification_board,
                                                                            'acad_grad_degree'=>$last_qualification,
                                                                            'acad_grad_yop'=>$passing_year,
                                                                            'acad_grad_percentage'=>$passing_percentage,
                                                                            'created_by'=>$user_id,
                                                                            'created_at'=>$created_at,
                                                                        );

                                                                        $this->um->store_students_academics($academic_details);

                                                                        //Update Student course cost

                                                                        $course_updated_cost=compact('inst_id','parent_inst_id','inst_type','student_user_id','session','course','stu_course_fees','stu_course_last_fees','user_id');

                                                                        update_stu_course_cost($course_updated_cost);
                                                                    }


                                                                    $booking_payment_checq_files_ids='';

                                                                    $booking_payment_data=compact('booking_id','inst_id','parent_inst_id','student_user_id','course','booking_subject_id','session','booking_amount','pay_mode','pay_bank_name','pay_bank_cheque_no','booking_payment_checq_files_ids','pay_deposite_bank','pay_mode','paid','user_id','booking_payment_date');

                                                                    $payment_id=store_seat_booking_payment_data($booking_payment_data);

                                                                    if($payment_id){
                                                                        if($agent_commision_error==''){
                                                                            $agent_payment_data=compact('inst_id','parent_inst_id','pay_booking_agent','session','course','booking_subject_id','student_user_id','booking_id','booking_code','booking_agent_payout_amount');

                                                                            store_agent_payment($agent_payment_data);
                                                                        }

                                                                        $utr=null;
                                                                        $txn=ge_rand_code_v1('TXN'.$inst_id.$booking_id);
                                                                     

                                                                        $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';
                                                                        $booking_order_id=$booking_code;
                                                                        $details_status='not recieved yet';
                                                                        $details_msg='not recieved yet';
                                                                        $user_type=$this->user_type;
                                                                        $details_accounting_type='income';
                                                                        $details_discount_amount='0';

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

                                                                        _store_payment_details($payment_details);

                                                                    }


                                                                    if(!empty($middle_name)){
                                                                        $student_name=$first_name.' '.$middle_name.' '.$last_name;
                                                                    }else{
                                                                        $student_name=$first_name.' '.$last_name;
                                                                    }

                                                                    //$course_data->course_code.'/'.


                                                                    $mail_data['inst_id']=$inst_id;
                                                                    $mail_data['inst_parent_id']=$parent_inst_id;
                                                                    $mail_data['notify_type']='stu_seat_booking';
                                                                    $mail_data['mail_from']='webmaster';
                                                                    $mail_data['mail_log']=false;
                                                                    $mail_data['to_email']=$email_address;
                                                                    $mail_data['mail_from_name']=$this->data['profile_name'];
                                                                    $mail_data['mail_subject']=ucwords($this->data['profile_name']).' Seatbook Details';
                                                                    $mail_data['mail_type']='html';
                                                                    $mail_data['mail_custom_type']='html';
                                                                    $mail_data['mail_data']=array(
                                                                        'stu_name'=>$student_name,
                                                                        'stu_email'=>$email_address,
                                                                        'stu_ph'=>$ph_no,
                                                                        'booking_code'=>$booking_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'=>number_format($booking_amount,2),
                                                                        'user_name'=>$email_address,
                                                                        'user_password'=>'Password@123',
                                                                        'portal_url'=>$this->data['base_url'],
                                                                        'portal_logo'=>$this->data['link_logo'],
                                                                        'portal_inst_name'=>$this->data['profile_name'],
                                                                        'college_email'=>$this->data['userdata']->user_email,
                                                                        'college_phone'=>$this->data['userdata']->user_contact_no,
                                                                        'default_logo_small'=>$this->data['default_logo_small'],
                                                                        'created_at'=>date('d-m-Y')
                                                                    );
                                                                    
                                                                    $mail_data['mail_view']='_pages/mails/vw_seatbook_mail';

                                                                    $mail_data['log_user_type']='student';
                                                                    $mail_data['log_user_id']=$student_user_id;
                                                                    $mail_data['log_type']='student_seat_booking_mail';

                                                                    //$mail_msg=$this->onSendMail($mail_data);

                                                                    $return['user_code']=$stu_user_code;
                                                                    $return['admission_code']=$booking_code;
                                                                    $return['admission_booking_amount']=$booking_amount;

                                                                    $return['success']=$agent_commision_error.'Seat has been booked.'.$mail_msg;
                                                                }else{
                                                                    $return['error']='There was an error occurred';
                                                                }

                                                            }else{
                                                                $return['error']='Data already exists';
                                                            }

                                                        }else{
                                                            $return['error']='Data not saved.Error Occurred';
                                                        }
                                                    }else if(!empty($student_data)){
                                                        $stu_user_code=$student_data->stu_user_code;

                                                        $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$student_data->booking_stu_profile_id,'booking_inst_id'=>$inst_id,'booking_sess_id'=>$session,'booking_course_id'=>$course));

                                                        if(empty($booking_data)){

                                                            //$booking_code=ge_rand_code('SB'.$student_data->stu_id,2,TRUE);

                                                            $booking_code=ge_rand_code('SB');
                                                            
                                                            $booking_data_to_store=compact('booking_stu_profile_id','inst_id','parent_inst_id','session','course','booking_subject_id','booking_code','pay_booking_agent','user_id');

                                                            $booking_id=store_seat_booking_data($booking_data_to_store);

                                                            if($booking_id){

                                                                $student_user_id=$student_data->booking_stu_profile_id;
                                                                $booking_payment_checq_files_ids='';
                                                                $booking_payment_data=compact('inst_id','parent_inst_id','booking_id','course','session','booking_subject_id','booking_amount','pay_mode','pay_bank_name','pay_bank_cheque_no','pay_deposite_bank','paid','user_id','booking_payment_date');

                                                                $payment_id=store_seat_booking_payment_data($booking_payment_data);


                                                                if($payment_id){
                                                                    if($agent_commision_error==''){

                                                                        $agent_payment_data=compact('inst_id','parent_inst_id','pay_booking_agent','session','course','booking_subject_id','student_user_id','booking_id','booking_agent_payout_amount');

                                                                        store_agent_payment($agent_payment_dat);
                                                                    }
                                                                    

                                                                    $payment_details=compact('inst_id','parent_inst_id','booking_id','booking_amount','booking_code','user_id');

                                                                    store_payment_details($payment_details);                                     
                                                                }


                                                                $mail_data['inst_id']=$inst_id;
                                                                $mail_data['inst_parent_id']=$parent_inst_id;
                                                                $mail_data['notify_type']='stu_seat_booking';
                                                                $mail_data['mail_from']='webmaster';
                                                                $mail_data['to_email']=$email_address;
                                                                $mail_data['mail_from_name']=$this->data['profile_name'];
                                                                $mail_data['mail_subject']=ucwords($this->data['profile_name']).' Seatbook Details';
                                                                $mail_data['mail_type']='html';
                                                                $mail_data['mail_custom_type']='html';
                                                                $mail_data['mail_data']=array(
                                                                    'stu_name'=>$student_name,
                                                                    'stu_email'=>$email_address,
                                                                    'stu_ph'=>$ph_no,
                                                                    'booking_code'=>$booking_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'=>number_format($booking_amount,2),
                                                                    'user_name'=>$email_address,
                                                                    'user_password'=>'Password@123',
                                                                    'portal_url'=>$this->data['base_url'],
                                                                    'portal_logo'=>$this->data['link_logo'],
                                                                    'portal_inst_name'=>$this->data['profile_name'],
                                                                    'college_email'=>$this->data['userdata']->user_email,
                                                                    'college_phone'=>$this->data['userdata']->user_contact_no,
                                                                    'default_logo_small'=>$this->data['default_logo_small'],
                                                                    'created_at'=>$created_at
                                                                );
                                                                
                                                                $mail_data['mail_view']='_pages/mails/vw_seatbook_mail';

                                                                $mail_data['log_user_type']='student';
                                                                $mail_data['log_user_id']=$student_user_id;
                                                                $mail_data['log_type']='student_seat_booking_mail';

                                                                //$mail_msg=$this->onSendMail($mail_data);

                                                                $return['user_code']=$stu_user_code;
                                                                $return['admission_code']=$booking_code;
                                                                $return['admission_booking_amount']=$booking_amount;

                                                                $return['success']=$agent_commision_error.'Seat has been booked';
                                                            }else{
                                                                $return['error']='There was an error occurred';
                                                            }

                                                        }else{
                                                            $return['error']='Data already exists';
                                                        }
                                                    }
                                                }
                                                else{
                                                    if($pay_mode=='cheque'){
                                                        $return['error']='Valid Cheque related data needs to be given to book seat.';
                                                    }else if($pay_mode==='phonepeutr'){
                                                        $return['error']='Valid PhonePe UTR & TXN no. needs to be given to book seat.';
                                                    }
                                                    else{
                                                        $return['error']='Payment mode data not given';
                                                    }
                                                }                                                
                                            }else{
                                                $return['error']='Booking amount can not exceeds the total cost of the course (₹ '.$booking_amount_data->session_course_fees.') or booking amount (₹ '.$booking_amount_data->session_course_booking_fee.') set in the system';
                                            }
                                        }else{
                                            $return['error']='Course cost data not found in the system.';
                                        }
                                    }else{
                                        $return['error']='Student is minor.';
                                    }
                                }else{
                                    $return['error']=$ph_no.' is already in use1.';
                                }                                        
                            }else{
                                $return['error']='Select subject';
                            }
                        }else{
                            $return['error']='No seats available for booking.';
                        }
                    }else if($student_upload_limit['student_data_can_be_uploaded']=='no'){
                        $return['info']='You have reached to your student upload limit of '.$student_upload_limit['student_upload_limit'].' students.';
                    }
                }

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


    private function add_payment_details($param){
        $utr=$param['utr'];
        $txn=ge_rand_code_v1('TXN'.$param['inst_id'].$param['booking_id']);

        $payment_details=array(
            'details_type'=>'seat_booking',
            'details_type_text'=>'Seat Booking Fees for '.$param['course_name'].' Session:'.$param['session_start_year'].'-'.$param['session_end_year'].'['.$param['booking_code'].']',
            'details_payment_type'=>'seatbooking_fees',
            'details_payment_id'=>$param['payment_id'],
            'details_type_id'=>$param['booking_id'],
            'details_inst_id'=>$param['inst_id'],
            'details_parent_inst_id'=>$param['parent_inst_id'],
            'details_stu_id'=>$param['student_user_id'],
            'details_sess_id'=>$param['session'],
            'details_course_id'=>$param['course'],
            'details_subject_id'=>$param['booking_subject_id'],
            'details_payment_platform'=>$param['pay_mode'],
            'details_amount'=>$param['booking_amount'],
            'details_discount_amount'=>'0',
            'details_txn_id'=>$param['txn'],
            'details_utr_id'=>$param['utr'],
            'details_order_id'=>$param['booking_code'],
            'details_order_code'=>$param['booking_code'],
            'details_accounting_type'=>'income',
            'details_status'=>$param['details_status'],
            'details_msg'=>$param['details_msg'],
            'details_created_by'=>$param['user_id'],
            'details_created_at'=>date('Y-m-d')
        );

        $this->fm->store_payment_details($payment_details);
    }


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

                $seat_booking_id=post_data('seatbook_id');
                $stu_id=post_data('stu_id');
                $payment_details_id=post_data('payment_details_id');
                $inst_id=post_data('inst_id');

                $seat_booking_id=decode_data($seat_booking_id);
                $stu_id=decode_data($stu_id);
                $payment_details_id=decode_data($payment_details_id);
                $inst_id=decode_data('inst_id');
                $seat_booking_balance_data=array();

                $seat_book_payment_data=array();
                $seat_book_last_balance_data=array();

                $seat_booking_data=$this->adm->get_seat_bookings_data(array('booking_id'=>$seat_booking_id));


                if(!empty($seat_booking_data)){
                    $seat_book_payment_data=$this->fm->get_payment_details(array('details_id'=>$payment_details_id));

                   // print_obj($seat_booking_data);die;

                   $seat_book_payment_balance=$this->fm->get_installments_balance(array('inst_stu_id'=>$stu_id,'inst_installment_id'=>$seat_booking_id),FALSE);

                   $seat_book_last_balance_data=$this->fm->get_installments_balance_last(array('inst_stu_id'=>$stu_id,'inst_installment_id'=>$seat_booking_id));

                }

                $this->data['seat_booking_data']=$seat_booking_data;
                $this->data['seat_book_payment_data']=$seat_book_payment_data;
                $this->data['seat_book_payment_balance']=$seat_book_payment_balance;
                $this->data['seat_book_last_balance_data']=$seat_book_last_balance_data;
                $this->data['stu_id']=encode_data($stu_id);
                $this->data['inst_id']=encode_data($inst_id);
                $this->data['form_type']=post_data('form_type');

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

                $return['html']=$this->theme->view('_pages/admission/vw_admission_seatbook_payment_screen',$this->data,true);
                $return['success']='Data fetched';

                header('Content-Type: application/json');

                echo json_encode($return);


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


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

                $student_user_id=session_userdata('booking_student_id');

                $booking_id=session_userdata('booking_id');
                $booking_code=session_userdata('booking_code');

                $seat_bookingdata=$this->adm->get_seat_bookings_data(array('booking_id'=>$booking_id,'booking_code'=>$booking_code));

                if(!empty($seat_bookingdata)){
                    $return['booking_code']=session_userdata('booking_code').'_stuinst';
                    $return['booking_value']=session_userdata('booking_amount');
                    $return['success']='Payment can be proceessed';
                }else{
                    $return['error']='Booking data not found in the system.';
                }

                return json_headers($return);

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


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

                $booking_id=post_data('booking_id');
                $payment_id=post_data('payment_id');
                $payment_order_id=post_data('payment_order_id');
                $payment_mode=post_data('full_pay_mode');

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

                if(!empty($payment_id)){

                    $payment_id=decode_data($payment_id);

                    if(is_numeric($payment_id)){

                        $payment_data=$this->adm->get_seat_booking_payment_data(array('booking_payment_id'=>$payment_id));

                        if(!empty($payment_data)){

                            $booking_data=$this->adm->get_seat_bookings_data(array('booking_id'=>$payment_data->booking_pk_id));

                            //print_obj($booking_data);die;

                            $booking_order_id=$booking_data->booking_code.'_stuinst';

                            if($payment_mode=='online'){
                                $return['success']='Payment can be proceessed.';
                                $return['booking_value']=$payment_data->booking_payment;
                                $return['booking_code']=$booking_data->booking_code.'_stuinst';
                            }else if($payment_mode=='online_before_software'){

                                $payment_data_to_update=array(
                                    'booking_pay_mode'=>'online_before_software',
                                    'booking_payment_paid'=>'yes',
                                    'updated_by'=>$user_id,
                                    'updated_by_type'=>$this->data['userdata']->user_type
                                );

                                $updated=$this->adm->update_seat_booking_payment_data($payment_data_to_update,array('booking_payment_id'=>$payment_data->booking_payment_id,'booking_stu_id'=>$payment_data->booking_stu_id));

                                if($updated){
                                    $payment_details=array('details_order_id'=>$booking_order_id,'details_payment_platform'=>'cash','details_status'=>'success','details_msg'=>ucwords($payment_mode.' recieved'));

                                    $this->fm->update_payment_details($payment_details,array('details_payment_id'=>$payment_data->booking_payment_id,'details_type_id'=>$payment_data->booking_pk_id,
                                        'details_inst_id'=>$payment_data->booking_inst_id));

                                    $return['success']='Payment has been recieved successfully.';
                                }else{
                                    $return['error']='Payment can not be done now.Try another method or try after sometime.';
                                }

                            }else if($payment_mode=='cash'){

                                $payment_data_to_update=array(
                                    'booking_pay_mode'=>'cash',
                                    'booking_payment_paid'=>'yes',
                                    'updated_by'=>$user_id,
                                    'updated_by_type'=>$this->data['userdata']->user_type
                                );

                                $updated=$this->adm->update_seat_booking_payment_data($payment_data_to_update,array('booking_payment_id'=>$payment_data->booking_payment_id,'booking_stu_id'=>$payment_data->booking_stu_id));

                                if($updated){
                                    $payment_details=array('details_order_id'=>$booking_order_id,'details_payment_platform'=>'cash','details_status'=>'success','details_msg'=>ucwords($payment_mode.' recieved'));

                                    $this->fm->update_payment_details($payment_details,array('details_payment_id'=>$payment_data->booking_payment_id,'details_type_id'=>$payment_data->booking_pk_id,
                                        'details_inst_id'=>$payment_data->booking_inst_id));

                                    $return['success']='Payment has been recieved successfully.';
                                }else{
                                    $return['error']='Payment can not be done now.Try another method or try after sometime.';
                                }

                            }else if($payment_mode=='cheque'){

                                $pay_bank_name=post_data('pay_bank_name');
                                $pay_bank_cheque_no=post_data('pay_bank_cheque_no');
                                $pay_deposite_bank=post_data('pay_deposite_bank');

                                if(!empty($pay_bank_name) && !empty($pay_bank_cheque_no) && $pay_deposite_bank>0){

                                    $cheque_data=$this->fm->get_cheque_details(array('cheque_details_no'=>$pay_bank_cheque_no));

                                    if(empty($cheque_data)){
                                        $payment_data_to_update=array(
                                            'booking_pay_mode'=>'cheque',
                                            'booking_payment_paid'=>'no',
                                            'booking_payment_checq_no'=>$pay_bank_cheque_no,
                                            'booking_payment_bank'=>$pay_bank_name,
                                            'updated_by'=>$user_id,
                                            'updated_by_type'=>$this->data['userdata']->user_type
                                        );

                                        $updated=$this->adm->update_seat_booking_payment_data($payment_data_to_update,array('booking_payment_id'=>$payment_data->booking_payment_id,'booking_stu_id'=>$payment_data->booking_stu_id));

                                        if($updated){
                                            $payment_details=array('details_payment_platform'=>'cheque','details_order_id'=>$booking_order_id,'details_status'=>'success','details_msg'=>ucwords($payment_mode.' recieved'));

                                            $this->fm->update_payment_details($payment_details,array('details_payment_id'=>$payment_data->booking_payment_id,'details_type_id'=>$payment_data->booking_pk_id,
                                                'details_inst_id'=>$payment_data->booking_inst_id));

                                            $cheque_details_to_add=array(
                                                'cheque_details_payment_id'=>$payment_data->booking_payment_id,//PK of nt_institute_payments_details
                                                'cheque_details_inst_id'=>$payment_data->booking_inst_id,
                                                'cheque_details_inst_parent_id'=>$payment_data->booking_inst_parent_id,
                                                'cheque_details_stu_id'=>$payment_data->booking_stu_id,
                                                'cheque_details_agent_id'=>'0',
                                                'cheque_details_deposite_bank_id'=>$pay_deposite_bank,
                                                'cheque_details_bank_name'=>$pay_bank_name,
                                                'cheque_details_no'=>$pay_bank_cheque_no,
                                                'cheque_details_amount'=>$payment_data->booking_payment,
                                                'cheque_details_bounce_amount'=>'0',
                                                'cheque_is_cleared'=>'no',
                                                'cheque_is_bounced'=>'no',
                                                'cheque_details_recieved_date'=>date('Y-m-d'),
                                                'cheque_details_deposite_date'=>null,
                                                'cheque_details_cash_date'=>null,
                                                'cheque_details_created_by'=>$user_id
                                            );


                                            $this->fm->store_cheque_details($cheque_details_to_add);

                                            $return['success']='Payment has been recieved successfully.';
                                        }else{
                                            $return['error']='Payment can not be done now.Try another method or try after sometime.';
                                        }
                                    }else{
                                        $return['error']='Cheque already registrered in the system.';
                                    }

                                        
                                }else{
                                    $return['error']='Cheque details required to complete the payment.';
                                }

                                    

                            }else if($payment_mode=='phonepeutr'){
                                $pay_phonepe_utr=post_data('pay_phonepe_utr');
                                $pay_phonepe_utr_txn=post_data('pay_phonepe_utr_txn');

                                if(!empty($pay_phonepe_utr) && !empty($pay_phonepe_utr_txn)){
                                    $payment_data_to_update=array(
                                        'booking_pay_mode'=>'phonepeutr',
                                        'booking_payment_paid'=>'yes',
                                        'updated_by'=>$user_id,
                                        'updated_by_type'=>$this->data['userdata']->user_type
                                    );

                                    $updated=$this->adm->update_seat_booking_payment_data($payment_data_to_update,array('booking_payment_id'=>$payment_data->booking_payment_id,'booking_stu_id'=>$payment_data->booking_stu_id));

                                    if($updated){
                                        $payment_details=array('details_payment_platform'=>'phonepeutr','details_order_id'=>$booking_order_id,'details_utr_id'=>$pay_phonepe_utr,'details_txn_id'=>$pay_phonepe_utr_txn,'details_status'=>'success','details_msg'=>ucwords($payment_mode.' recieved'));

                                        $this->fm->update_payment_details($payment_details,array('details_payment_id'=>$payment_data->booking_payment_id,'details_type_id'=>$payment_data->booking_pk_id,
                                            'details_inst_id'=>$payment_data->booking_inst_id));

                                        $return['success']='Payment has been recieved successfully.';
                                    }else{
                                        $return['error']='Payment can not be done now.Try another method or try after sometime.';
                                    }
                                }else{
                                    $return['error']='PhonePe UTR & TXN no are required.';
                                }

                                    
                            }


                        }else{
                            $return['error']='No payment related data found in the system.';
                        }

                    }else{
                        $return['error']='Data mnipulation is not allowed';
                    }

                }else{
                    $return['error']='data manipulation is not allowed';
                }

                json_headers($return);

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

    public function onSearchSeatBookings(){
        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;

                $user_id=$userdata->user_id;

                $param['column_order'] = array(
                    null,
                    'booking_code',
                    null,
                    null,
                    null,
                    null,
                    'booking_payment',
                    null,
                    'booking_payment_paid'
                );

                $param['column_search'] = array('booking_code','stu_first_name','stu_mid_name','stu_last_name','stu_email','stu_ph_no','stu_whatsapp_no','booking_payment');
                $param['order'] = array('booking_id' => 'DESC');
                $posts=$this->input->post();

                //$param['inst_id']=$this->data['userdata']->user_id;//session_userdata('admin_id');


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

                if($userdata->user_type=='agent'){
                    $param['agent_user_id']=$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_session']) && !empty($posts['data_of_session'])){
                    $param['inst_session_id']=$posts['data_of_session'];
                }


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



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

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


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

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

                $action='';

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

                    $row = array();

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

                    if($booking->booking_payment_paid=='yes'){
                        $pay_status='<button class="btn btn-xs btn-success">Yes</button>';
                    }else if($booking->booking_payment_paid=='no'){
                        $pay_status='<button class="btn btn-xs btn-danger">No</button>';
                    }else if($booking->booking_payment_paid==''){
                        $pay_status='<button class="btn btn-xs btn-danger">No</button>';
                    }else{
                        $pay_status='';
                    }

                    $course_fees=$this->sessm->get_session_courses_student_wise(array('session_stu_id'=>$booking->stu_user_id),TRUE);

                    //print_obj($course_fees);die;

                    $course_fees_amount = isset($course_fees->session_course_fees) ? number_format($course_fees->session_course_fees, 2) : 'N/A';

                    $row[]  =   $no;

                    $agent_payment=$this->fm->_get_agent_payment(array('booking_id'=>$booking->booking_id,'booking_type'=>'seat_booking','inst_id'=>$this->inst_id),$db_group);

                    $agent=(!empty($agent_payment))?$agent_payment->agent_name:'';
                    $agent_amount=(!empty($agent_payment))?number_format($agent_payment->amount_value):'';

                    if($booking->booking_converted=='no' && $booking->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($booking->booking_id).'"><strong>Convert to Admission</strong></a>';
                    }else if($booking->booking_converted=='yes' && $booking->booking_payment_paid=='yes'){
                        $convert_url='';
                    }else if($booking->booking_converted=='no' && $booking->booking_payment_paid=='no'){
                        $convert_url='<br><span style="margin-left: 0px;text-decoration: underline;"><strong>Admission can be done after payment</strong></span>';
                    }else if($booking->booking_converted=='no' && $booking->booking_payment_paid==''){
                        $convert_url='<br><span style="margin-left: 0px;text-decoration: underline;"><strong>Admission can be done after payment</strong></span>';
                    }else{
                        $convert_url='';
                    }
                    

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

                    if($userdata->user_type=='agent'){
                        $convert_url='';
                    }

                    if(!empty($agent)){
                        $convert_url.='<br><br><strong>Consultant:'.$agent.'<br>Consultant Payout: ₹ '.$agent_amount.'</strong>';
                    }

                    $edit_delete='';

                    //<a href="'.$this->data['base_url'].'/admission/bookings/register/'.encode_data($booking->booking_id).'" class="btn btn-xs btn-dark">Edit</a>

                    if($this->seatbooking_edit_access=='yes' && $this->seatbooking_del_access=='yes'){
                        $edit_delete=($booking->booking_payment_paid=='no')?'<br><div class="btn-group" role="group" aria-label="Basic example">
                          
                          <button type="button" class="btn btn-xs btn-danger btn_del_seat_booking" data-booking_id="'.$booking->booking_id.'">Delete</button>
                        </div>':'';
                    }else if($this->seatbooking_edit_access=='no' && $this->seatbooking_del_access=='yes'){
                        $edit_delete=($booking->booking_payment_paid=='no')?'<br><div class="btn-group" role="group" aria-label="Basic example">
                          <button type="button" class="btn btn-xs btn-danger btn_del_seat_booking" data-booking_id="'.$booking->booking_id.'">Delete</button>
                        </div>':'';
                    }

                    


                    // $stu_name='<a class="mytooltip" href="javascript:void(0)"> '.$student_name.'<span class="tooltip-content5"><span class="tooltip-text3"><span class="tooltip-inner2">Howdy, Ben!<br /> There are 13 unread messages in your inbox.</span></span></span></a>';

                    $stu_name=$student_name;

                    $student_details=$stu_name.'<br>Email: '.$booking->stu_email.'<br>Phone: '.$booking->stu_ph_no.'<br>Whatsapp: '.$booking->stu_whatsapp_no;

                    

                    $course_details=$course_fees->course_name.'<br>Session: '.$course_fees->session_start_year.'-'.$course_fees->session_end_year.'<br><strong>Fees: ₹ '.$course_fees_amount.'</strong>';


                    $row[]  =   $booking->booking_code.$convert_url.$edit_delete;
                    $row[]  =   $student_details;
                    $row[]  =   $course_details;

                    if($booking->booking_payment>0){
                        $booking_amount=$booking->booking_payment;
                    }else{
                        $student_course_fees=$this->sessm->get_session_course_student_wise(array('session_stu_id'=>$booking->stu_user_id),TRUE,$db_group);
                        $booking_amount=(!empty($student_course_fees))?$student_course_fees->session_course_booking_fee:0;
                    }
                    
                    $row[]  =   '<strong>₹ '.number_format($booking_amount).'</strong><br><a href="'.$this->data['base_url'].'/admission/bookings/register/'.encode_data($booking->booking_id).'" class="btn btn-xs btn-dark " data-section="'.encode_data($section->section_id).'" data-name="'.$section->section_name.'"><i class="fa fa-edit"></i></a>';
                    //<button class="btn btn-xs btn-danger" data-section="'.encode_data($section->section_id).'"><i class="fa fa-trash"></i></button>'';
                    //$row[]  =   (!empty($agent_payment))?$agent_payment->agent_name:'';
                    //$row[]  =   (!empty($agent_payment))?number_format($agent_payment->amount_value):0;
                    $row[]  =   $pay_status; 
                    
                    //$row[]  =   '<button class="btn btn-xs btn-info btn_edit_section" data-section="'.encode_data($section->section_id).'" data-name="'.$section->section_name.'"><i class="fa fa-edit"></i></button>';
                    //<button class="btn btn-xs btn-danger" data-section="'.encode_data($section->section_id).'"><i class="fa fa-trash"></i></button>';

                    $data[] = $row; 
                }

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

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

    Public function onCreateAdmission(){
        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;

                $booking_id=post_data('booking_id');

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

               $admission_type=post_data('admission_type');
               $admission_date=post_data('admission_date');
               $admission_session=post_data('admission_session');
               $admission_course=post_data('admission_course');
               $admission_section=post_data('admission_section');
               $admission_agent=post_data('admission_agent');


               //$admission_code=ge_rand_code_v1('SB');

               $admission_code=ge_rand_code('ADM');

               //echo $admission_code;die;

               $stu_first_name=post_data('stu_first_name');
               $stu_middle_name=post_data('stu_middle_name');
               $stu_last_name=post_data('stu_last_name');
               $stu_dob=post_data('stu_dob');
               $stu_gender=post_data('stu_gender');
               $stu_religion=post_data('stu_religion');
               $stu_caste=post_data('stu_caste');
               $stu_blood_grp=post_data('stu_blood_grp');
               $stu_aadhar_no=post_data('stu_aadhar_no');
               $stu_ph_no=post_data('stu_ph_no');
               $stu_whatsapp_no=post_data('stu_whatsapp_no');
               $stu_email_address=post_data('stu_email_address');
               $stu_has_illness=post_data('stu_has_illness');


               $stu_illness_remarks=post_data('stu_illness_remarks');
               $stu_identiy_mark=post_data('stu_identiy_mark');
               $stu_nationality=post_data('stu_nationality');

               $stu_10th_board=post_data('stu_10th_board');
               $stu_10th_yop=post_data('stu_10th_year_of_passing');
               $stu_10th_total_marks=post_data('stu_10th_total_marks');
               $stu_10th_percentage_marks=post_data('stu_10th_percentage_marks');
               $stu_10th_division=post_data('stu_10th_division');
               $stu_10th_subjects=post_data('stu_10th_subjects');
               $stu_10_2_board=post_data('stu_10_2_board');
               $stu_10_2_yop=post_data('stu_10_2_year_of_passing');
               $stu_10_2_total_marks=post_data('stu_10_2_total_marks');
               $stu_10_2_percentage_marks=post_data('stu_10_2_percentage_marks');
               $stu_10_2_division=post_data('stu_10_2_division');
               $stu_10_2_subjects=post_data('stu_10_2_subjects');

               $stu_grad_board=post_data('stu_grad_board');
               $stu_grad_yop=post_data('stu_grad_year_of_passing');
               $stu_grad_total_marks=post_data('stu_grad_total_marks');
               $stu_grad_percentage_marks=post_data('stu_grad_percentage_marks');
               $stu_grad_division=post_data('stu_grad_division');
               $stu_grad_subjects=post_data('stu_grad_subjects');

               $stu_post_grad_board=post_data('stu_post_grad_board');
               $stu_post_grad_yop=post_data('stu_post_grad_year_of_passing');
               $stu_post_grad_total_marks=post_data('stu_post_grad_total_marks');
               $stu_post_grad_percentage_marks=post_data('stu_post_grad_percentage_marks');
               $stu_post_grad_division=post_data('stu_post_grad_division');
               $stu_post_grad_subjects=post_data('stu_post_grad_subjects');


               $stu_other_grad_board=post_data('stu_other_grad_board');
               $stu_other_grad_yop=post_data('stu_other_grad_year_of_passing');
               $stu_other_grad_total_marks=post_data('stu_other_grad_total_marks');
               $stu_other_grad_percentage_marks=post_data('stu_other_grad_percentage_marks');
               $stu_other_grad_division=post_data('stu_other_grad_division');
               $stu_other_grad_subjects=post_data('stu_other_grad_subjects');

               $stu_father_first_name=post_data('stu_father_first_name');
               $stu_father_mid_name=post_data('stu_father_mid_name');
               $stu_father_last_name=post_data('stu_father_last_name');
               $stu_father_occupation=post_data('stu_father_occupation');
               $stu_father_mobile=post_data('stu_father_mobile');
               $stu_father_email_address=post_data('stu_father_email_address');
               $stu_father_income=post_data('stu_father_income');


               $stu_mother_first_name=post_data('stu_mother_first_name');
               $stu_mother_mid_name=post_data('stu_mother_mid_name');
               $stu_mother_last_name=post_data('stu_mother_last_name');
               $stu_mother_occupation=post_data('stu_mother_occupation');
               $stu_mother_mobile=post_data('stu_mother_mobile');
               $stu_mother_email_address=post_data('stu_mother_email_address');
               $stu_mother_income=post_data('stu_mother_income');


               $stu_guardian_first_name=post_data('stu_guardian_first_name');
               $stu_guardian_mid_name=post_data('stu_guardian_mid_name');
               $stu_guardian_last_name=post_data('stu_guardian_last_name');
               $stu_guardian_occupation=post_data('stu_guardian_occupation');
               $stu_guardian_mobile=post_data('stu_guardian_mobile');
               $stu_guardian_email_address=post_data('stu_guardian_email_address');
               $stu_guardian_income=post_data('stu_guardian_income');


               $stu_c_o=post_data('stu_c_o');
               $stu_city_village=post_data('stu_city_village');
               $stu_district=post_data('stu_district');
               $stu_state=post_data('stu_state');
               $stu_p_s=post_data('stu_p_s');
               $stu_p_o=post_data('stu_p_o');
               $stu_pincode=post_data('stu_pincode');
               $stu_address_landmark=post_data('stu_address_landmark');


               $stu_perma_c_o=post_data('stu_perma_c_o');
               $stu_perma_city_village=post_data('stu_perma_city_village');
               $stu_perma_district=post_data('stu_perma_district');
               $stu_perma_state=post_data('stu_perma_state');
               $stu_perma_p_s=post_data('stu_perma_p_s');
               $stu_perma_p_o=post_data('stu_perma_p_o');
               $stu_perma_pincode=post_data('stu_perma_pincode');
               $stu_perma_address_landmark=post_data('stu_perma_address_landmark');

               $address_same_as=post_data('address_same_as');

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

                if($this->data['userdata']->user_type=='institute'){
                    $parent_inst_id=$this->data['userdata']->user_id;
                }else if($this->data['userdata']->user_type=='institute_branch'){
                    $parent_inst_id=$this->data['userdata']->branch_parent_inst_id;
                }

                $course_data=$this->cm->get_course(array('course_id'=>$admission_course,'course_inst_id'=>$booking_data->booking_inst_id),TRUE,$db_group);

                $session_data=$this->sessm->get_session(array('session_id'=>$admission_session,'session_inst_id'=>$booking_data->booking_inst_id),TRUE,$db_group);

                $student_found=$this->um->get_students(array('stu_email'=>$stu_email_address),$db_group);

               //print_obj($student_found);die;

                if(!empty($student_found)){

                    $student_data=array(
                        'stu_inst_id'=>$college_id,
                        'stu_inst_parent_id'=>$parent_inst_id,
                        'stu_first_name'=>$stu_first_name,
                        'stu_mid_name'=>$stu_middle_name,
                        'stu_last_name'=>$stu_last_name,
                        'stu_email'=>$stu_email_address,
                        'stu_ph_no'=>$stu_ph_no,
                        'stu_whatsapp_no'=>$stu_whatsapp_no,
                        'stu_aadhar'=>$stu_aadhar_no,
                        'stu_dob'=>date('Y-m-d',strtotime($stu_dob)),
                        'stu_gender'=>$stu_gender,
                        'stu_religion'=>$stu_religion,
                        'stu_caste'=>$stu_caste,
                        'stu_blood_group'=>$stu_blood_grp,
                        'stu_nationality'=>$stu_nationality,
                        'stu_has_illness'=>$stu_has_illness,
                        'stu_illness_remarks'=>$stu_illness_remarks,
                        'stu_identity_marks'=>$stu_identiy_mark,
                        'stu_father_first_name'=>$stu_father_first_name,
                        'stu_father_mid_name'=>$stu_father_mid_name,
                        'stu_father_last_name'=>$stu_father_last_name,
                        'stu_father_ph_no'=>$stu_father_mobile,
                        'stu_father_email'=>$stu_father_email_address,
                        'stu_father_income'=>$stu_father_income,
                        'stu_father_occupation'=>$stu_father_occupation,
                        'stu_mother_first_name'=>$stu_mother_first_name,
                        'stu_mother_mid_name'=>$stu_mother_mid_name,
                        'stu_mother_last_name'=>$stu_mother_last_name,
                        'stu_mother_ph_no'=>$stu_mother_mobile,
                        'stu_mother_email'=>$stu_mother_email_address,
                        'stu_mother_income'=>$stu_mother_income,
                        'stu_mother_occupation'=>$stu_mother_occupation,
                        'stu_guardian_first_name'=>$stu_guardian_first_name,
                        'stu_guardian_mid_name'=>$stu_guardian_mid_name,
                        'stu_guardian_last_name'=>$stu_guardian_last_name,
                        'stu_guardian_ph_no'=>$stu_guardian_mobile,
                        'stu_guardian_email'=>$stu_guardian_email_address,
                        'stu_guardian_income'=>$stu_guardian_income,
                        'stu_guardian_occupation'=>$stu_guardian_occupation,
                        'stu_cur_addr_co'=>$stu_c_o,
                        'stu_cur_addr_country'=>$stu_nationality,
                        'stu_cur_state'=>$stu_state,
                        'stu_cur_city'=>$stu_city_village,
                        'stu_cur_district'=>$stu_district,
                        'stu_cur_pincode'=>$stu_pincode,
                        'stu_cur_ps'=>$stu_p_s,
                        'stu_cur_po'=>$stu_p_o,
                        'stu_cur_address_line'=>'',
                        'stu_cur_address_landmark'=>$stu_address_landmark,
                        'stu_perma_addr_co'=>$stu_perma_c_o,
                        'stu_perma_addr_country'=>$stu_nationality,
                        'stu_perma_state'=>$stu_perma_state,
                        'stu_perma_city'=>$stu_perma_city_village,
                        'stu_perma_district'=>$stu_perma_district,
                        'stu_perma_pincode'=>$stu_perma_pincode,
                        'stu_perma_ps'=>$stu_perma_p_s,
                        'stu_perma_po'=>$stu_perma_p_o,
                        'stu_perma_address_line'=>'',
                        'stu_perma_address_landmark'=>$stu_perma_address_landmark,
                        'stu_same_address'=>$address_same_as,
                        'stu_regster_mode'=>$admission_type,
                        'stu_last_qualification'=>null,
                        'stu_passing_year'=>null,
                        'stu_passing_percentage'=>null,
                        'created_by'=>$this->data['userdata']->user_id,
                        'created_at'=>date('Y-m-d')
                    );

                    $updated=$this->um->update_students($student_data,array('stu_id'=>$student_found->stu_id),FALSE,$db_group);

                    if($updated){

                        $stu_password=post_data('stu_password');

                        $password=generate_password($stu_password);

                        $stu_username=post_data('stu_username');

                        if(!empty($stu_username)){
                            $username=$stu_username;
                        }else{
                            $username=$stu_email_address;
                        }

                        $student_userdata=array(
                            'user_name'=>$username,
                            'user_password'=>$password,
                            'user_type'=>'student',
                            'user_permissions'=>null,
                            'user_status'=>'active',
                            'created_by_type'=>'ntadmin',
                            'created_by'=>$this->data['userdata']->user_id,
                            'created_at'=>date('Y-m-d')
                        );

                        $get_student_user=$this->um->get_user(array('user_name'=>$username),'student',null,$db_group);

                        if(!empty($get_student_user)){
                            $this->um->update_users($student_userdata,array('user_id'=>$get_student_user->user_id),FALSE,$db_group);
                            $student_user_id=$get_student_user->user_id;
                        }else{
                            $student_user_id=$this->um->store_users($student_userdata,$db_group);
                        }

                        if($student_user_id){
                            $this->um->update_students(array('stu_user_id'=>$student_user_id),array('stu_id'=>$student_found->stu_id),FALSE,$db_group);
                        }

                        $academic_data=array(
                            'acad_stu_id'=>$student_user_id,
                            'acad_stu_inst_id'=>$college_id,
                            'acad_stu_inst_parent_id'=>$parent_inst_id,
                            'acad_10th_board'=>$stu_10th_board,
                            'acad_10th_yop'=>$stu_10th_yop,
                            'acad_10th_total_marks'=>$stu_10th_total_marks,
                            'acad_10th_percentage'=>$stu_10th_percentage_marks,
                            'acad_10th_division'=>$stu_10th_division,
                            'acad_10th_subjects'=>$stu_10th_subjects,
                            'acad_12th_board'=>$stu_10_2_board,
                            'acad_12th_yop'=>$stu_10_2_yop,
                            'acad_12th_total_marks'=>$stu_10_2_total_marks,
                            'acad_12th_percentage'=>$stu_10_2_percentage_marks,
                            'acad_12th_division'=>$stu_10_2_division,
                            'acad_12th_subjects'=>$stu_10_2_subjects,
                            'acad_grad_board'=>$stu_grad_board,
                            'acad_grad_yop'=>$stu_grad_yop,
                            'acad_grad_total_marks'=>$stu_grad_total_marks,
                            'acad_grad_percentage'=>$stu_grad_percentage_marks,
                            'acad_grad_division'=>$stu_grad_division,
                            'acad_grad_subjects'=>$stu_grad_subjects,
                            'acad_pgd_board'=>$stu_post_grad_board,
                            'acad_pgd_yop'=>$stu_post_grad_yop,
                            'acad_pgd_total_marks'=>$stu_post_grad_total_marks,
                            'acad_pgd_percentage'=>$stu_post_grad_percentage_marks,
                            'acad_pgd_division'=>$stu_post_grad_division,
                            'acad_pgd_subjects'=>$stu_post_grad_subjects,
                            'acad_others_board'=>$stu_other_grad_board,
                            'acad_others_yop'=>$stu_other_grad_yop,
                            'acad_others_total_marks'=>$stu_other_grad_total_marks,
                            'acad_others_percentage'=>$stu_other_grad_percentage_marks,
                            'acad_others_division'=>$stu_other_grad_division,
                            'acad_others_subjects'=>$stu_other_grad_subjects,
                            'created_by'=>$this->data['userdata']->user_id,
                            'created_at'=>date('Y-m-d')
                        );

                        $this->um->store_students_academics($academic_data,$db_group);


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

                            $addhar_file=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_addhar_card_file',
                                'file_types'=>'png,jpg,jpeg',
                                'file_storage_type'=>'aadhar_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($addhar_file);
                        }



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

                            $madhyamik_admitcard=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_madhyamik_admit_card_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'madhyamik_admit_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($madhyamik_admitcard);
                        }


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

                            $madhyamik_result=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_madhyamik_result_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'madhyamik_result_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($madhyamik_result);
                        }


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

                            $hs_result=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_12th_result_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'12th_result_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($hs_result);
                        }

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

                            $grad_result=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_grad_result_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'grad_result_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($grad_result);
                        }


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

                            $post_grad_result=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_post_grad_result_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'post_grad_result_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($post_grad_result);
                        }


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

                            $other_certificate=array(
                                'file_size'=>'5',
                                'file_name'=>'stu_other_certificate_file',
                                'file_types'=>'png,jpg,jpeg,pdf',
                                'file_storage_type'=>'other_certificate_card',
                                'file_storage_dir'=>'students',
                                'file_storage_type_id'=>$student_user_id,
                                'file_parent_id'=>$college_id,
                                'file_compress'=>false,
                                'file_operation_type'=>'new',
                                'file_uploaded_by'=>$this->data['userdata']->user_id
                            );

                            $this->onUploadFiles($other_certificate);
                        }

                        $admission_details=array(
                            'admission_seat_booking_converted'=>'yes',
                            'admission_seat_booking_id'=>($booking_id>0)?$booking_id:NULL,
                            'admission_stu_id'=>$student_user_id,
                            'admission_inst_id'=>$college_id,
                            'admission_inst_parent_id'=>$parent_inst_id,
                            'admission_sess_id'=>$admission_session,
                            'admission_course_id'=>$admission_course,
                            'admission_section_id'=>$admission_section,
                            'admission_type'=>$admission_type,
                            'admission_application_type'=>'new_admission',
                            'admission_agent_id'=>$admission_agent,
                            'admission_code'=>$admission_code,
                            'admission_date'=>(!empty($admission_date))?date('Y-m-d',strtotime($admission_date)):null,
                            'created_by'=>$this->data['userdata']->user_id,
                            'created_at'=>date('Y-m-d')
                        );

                        $admission_id=$this->adm->store_admission_data($admission_details,$db_group);

                        if($booking_type=='seat_booking_convert'){
                            $this->adm->update_seat_booking_data(array('booking_converted'=>'yes'),array('booking_id'=>$booking_id),FALSE,$db_group);
                        }


                        $return['success']='Registration successfull';
                        $return['registration_admission_id']=encode_data($admission_id);
                        $return['document_verification']='I am giving my consent that all documents and data has been verified and checked.';
                        $return['redirect_payment']=$this->data['base_url'].'/admission/payments/'.encode_data($admission_id);


                    }else{
                        $return['error']='Error occurred.';
                    }


               }else{
                $return['error']='Data not found.';
               }

               json_headers($return);
               session_write_close();

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


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

                $booking_id=post_data('booking_id');
                $booking_id=decode_data($booking_id);
                $inst_id=$this->data['userdata']->user_id;
                $mail_content=$this->input->post('mail_content');

                $booking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$booking_id,'booking_inst_id'=>$inst_id));

                if(!empty($booking_data)){

                    if(!empty($mail_content)){
                        $student_data=$this->um->get_students(array('stu_id'=>$booking_data->booking_stu_profile_id));

                        $updated=$this->adm->update_seat_booking_data(array('booking_converted'=>'cancel','updated_by'=>$this->data['userdata']->user_id,'updated_at'=>date('Y-m-d')),array('booking_id'=>$booking_id,'booking_inst_id'=>$inst_id));

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

                        if($booking_data->booking_subject_id!=null){
                            $subject_data=$this->cm->get_subject(array('subject_course_id'=>$booking_data->booking_course_id,'subject_id'=>$booking_data->booking_subject_id));
                            $message='Seat Booking - <b>Code:'.$booking_data->booking_code.',Course:'.$course_data->course_name.',Subject:'.$subject_data->subject_name.' dated '.date('d-m-Y').'</b> has been cancelled for the following reason.';
                        }else{
                            $message='Seat Booking - <b>Code:'.$booking_data->booking_code.',Course:'.$course_data->course_name.' dated '.date('d-m-Y').'</b> has been cancelled for the following reason.';
                        }
                        

                        if($updated){

                            $get_college_seat_settings=$this->sm->get_college_seat_settings(array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$booking_data->booking_sess_id,'seats_course_id'=>$booking_data->booking_course_id));

                            if(!empty($get_college_seat_settings)){
                                $remaining_seats=$get_college_seat_settings->seats_available+1;
                                $this->sm->update_college_seat_settings(array('seats_available'=>$remaining_seats),array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$booking_data->booking_sess_id,'seats_course_id'=>$booking_data->booking_course_id));
                            }

                            if($this->data['userdata']->user_type=='institute'){
                                $parent_inst_id=$this->data['userdata']->user_id;
                            }else if($this->data['userdata']->user_type=='institute_branch'){
                                $parent_inst_id=$this->data['userdata']->branch_parent_inst_id;
                            }

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


                            $mail_data['inst_id']=$this->data['userdata']->user_id;
                            $mail_data['mail_from']='webmaster';
                            $mail_data['to_email']=$student_data->stu_email;
                            $mail_data['mail_from_name']=$this->data['profile_name'];
                            $mail_data['mail_subject']='Seat Booking Cancelled - Code:'.$booking_data->booking_code;
                            $mail_data['mail_type']='html';
                            $mail_data['mail_custom_type']='custom';
                            $mail_data['mail_data']=$message.'<br>'.$mail_content;
                            $mail_data['mail_log']='true';
                            $mail_data['inst_parent_id']=$parent_inst_id;
                            $mail_data['notify_type']='stu_seat_booking_cancel';
                            $mail_data['notify_type_data']=$student_name;
                            $mail_data['notify_type_id']=$student_data->stu_user_id;
                            $mail_data['notify_has_attachment']='no';
                            $mail_data['notify_status']='unread';

                            $mail_data['log_user_type']='student';
                            $mail_data['log_user_id']=$student_data->stu_user_id;
                            $mail_data['log_type']='student_seat_booking_cancel';

                            $mail_msg=$this->onSendMail($mail_data);

                            $return['success']='Booking has been cancelled.'.$mail_msg;
                        }else{
                            $return['error']='Data can not be proceessed now.';
                        }
                    }else{
                        $return['error']='Enter reason of cancel.';
                    }                        

                }else{
                    $return['error']='Data not found.';
                }

                json_headers($return);
                session_write_close();

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


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

                $booking_id=post_data('booking_id');
                $booking_code=post_data('booking_code');
                $booking_id1=$booking_id;
                //print_obj($booking_code);
              /* print_obj($booking_id);
               print_obj($booking_code);
               die();*/

                $cheque_deposite_date=post_data('cheque_deposite_date');
                $cheque_cashed_date=post_data('cheque_cashed_date');
                $cheque_is_cleared=post_data('cheque_is_cleared');
              

                if(!empty($booking_id)){
                    $booking_id=decode_data($booking_id);

                    if(is_numeric($booking_id)){

                        //echo $booking_id;die;

                        if((!empty($cheque_deposite_date) && !empty($cheque_cashed_date)) || (!empty($cheque_deposite_date))  ){
                            $booking_data=$this->adm->get_seat_bookings_data(array('booking_code'=>$booking_code));

                            //print_obj($booking_data);die;

                            $cheque_data=$this->fm->get_cheque_details(array('cheque_details_payment_id'=>$booking_data->booking_payment_id));

                            //print_obj($cheque_data);die;

                            if(!empty($booking_data)){
                              

                             //print_obj($booking_data);

                                    

                                    if(!empty($cheque_data)){
                                            if($cheque_is_cleared=='yes'){
                                        $cheque_data_to_update=array(
                                            'cheque_is_cleared'=>'yes',
                                            'cheque_is_bounced'=>'no',
                                            'cheque_details_deposite_date'=>date('Y-m-d',strtotime($cheque_deposite_date)),
                                            'cheque_details_cash_date'=>date('Y-m-d',strtotime($cheque_cashed_date)),
                                            'cheque_details_updated_by'=>$this->data['userdata']->user_id
                                        );
                                        $updated=$this->adm->update_seat_booking_payment_data(array('booking_payment_paid'=>'yes','updated_by_type'=>$this->data['userdata']->user_type,'updated_at'=>date('Y-m-d'),'updated_by'=>$this->data['userdata']->user_id),array('booking_pk_id'=>$booking_data->booking_payment_id));
                                    }
                                    else{
                                        $cheque_data_to_update=array(
                                            'cheque_is_cleared'=>'no',
                                            'cheque_is_bounced'=>'yes',
                                            'cheque_details_deposite_date'=>date('Y-m-d',strtotime($cheque_deposite_date)),
                                            'cheque_details_cash_date'=>date('Y-m-d',strtotime($cheque_cashed_date)),
                                            'cheque_details_updated_by'=>$this->data['userdata']->user_id
                                        );
                                        $updated=$this->adm->update_seat_booking_payment_data(array('booking_payment_paid'=>'no','updated_by_type'=>$this->data['userdata']->user_type,'updated_at'=>date('Y-m-d'),'updated_by'=>$this->data['userdata']->user_id),array('booking_pk_id'=>$booking_data->booking_payment_id));

                                    }

                                        $this->fm->update_cheque_details($cheque_data_to_update,array('cheque_details_payment_id'=>$booking_data->booking_payment_id));
                                        $return['success']='Data updated successfully.';
                                   
                                    }


                                   
                                else{
                                    $return['error']='Data can not be updated now.Try after some time.';
                                }

                            }else{
                                $return['error']='No data found for this booking.';
                            }
                        }else{
                            $return['error']='Cheque dates are required.';
                        }
                            
                    }else{
                         $return['error']='Data manipulation not allowed';
                    }
                }else{
                    $return['error']='Data manipulation not allowed';
                }


                json_headers($return);
                session_write_close();

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



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

                $user_id=decode_data(session_userdata('admin_id'));
                $data_imported=FALSE;

                $today=date('Y-m-d');

                $tempdata=array();

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

                if($this->data['userdata']->user_type=='institute'){
                    $parent_inst_id=$this->data['userdata']->user_id;
                }else if($this->data['userdata']->user_type=='institute_branch'){
                    $parent_inst_id=$this->data['userdata']->branch_parent_inst_id;
                }

                $course=post_data('booking_course');
                $session=post_data('booking_session');
                $subject=post_data('booking_subject');

                $excel_type=post_data('excel_type');

                if($session>0 && $course>0){
                    $table_rows='';

                    $course_data=$this->cm->get_course(array('course_id'=>$course,'course_inst_id'=>$inst_id));

                    $session_data=$this->sessm->get_session(array('session_id'=>$session,'session_inst_id'=>$inst_id));

                    $course_session_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_id'=>$session,'session_course_id'=>$course));

                    $get_college_seat_settings=$this->sm->get_college_seat_settings(array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));

                    if(!empty($get_college_seat_settings)){
                        $available_seats=$get_college_seat_settings->seats_available;
                    }else{
                        $available_seats='0';
                    }

                    if(isset($_FILES['excel_file']) && $_FILES['excel_file']['name']!=''){
                        $get_excel_tempstatus_students=$this->um->get_excel_tempstatus_students(array('session_id'=>$session,'course_id'=>$course,'inst_id'=>$inst_id,'excel_file_name'=>$_FILES['excel_file']['name']));                         

                        if(empty($get_excel_tempstatus_students)){
                            $excel_data=array(
                                'file_size'=>'10',
                                'file_name'=>'excel_file',
                                'file_types'=>'xls,xlsx',
                            );

                            $file_path=$this->onUploadExcelFiles($excel_data);

                            // print_obj($file_path);
                            // print_obj(is_dir($file_path));die;

                            // is_file(filename)

                            if(is_file($file_path)){
                                $validated=$this->onValidateExcel($file_path);

                                //print_obj($validated);die;

                                if($validated['validated']==200){
                                    $exceldata=$validated['exceldata'];

                                    //print_obj($exceldata);die;

                                    foreach ($exceldata as $_key => $_value) {
                                        $_exceldata[]=array_filter($_value[0]);
                                    }

                                    //print_obj($exceldata);die;

                                    if(!empty($_exceldata)){

                                        $temp_status_data_to_add=array(
                                            'inst_id'=>$inst_id,
                                            'session_id'=>$session,
                                            'course_id'=>$course,
                                            'subject_id'=>$subject_id,
                                            'excel_upload_type'=>$excel_type,
                                            'excel_file_name'=>$_FILES['excel_file']['name'],
                                            'excel_upload_status'=>'uploaded_not_imported',
                                            'excel_uploaded_by'=>$user_id
                                        );

                                        $status_added=$this->um->store_excel_tempstatus_students($temp_status_data_to_add);

                                        if($status_added){
                                            foreach ($_exceldata as $key => $value){
                                                $row=$key+1;

                                                $f_name             =   isset($value[0])?strip_javascript(xss_clean(trim($value[0]))):'';
                                                $m_name             =   isset($value[1])?strip_javascript(xss_clean(trim($value[1]))):'';
                                                $l_name             =   isset($value[2])?strip_javascript(xss_clean(trim($value[2]))):'';
                                                $email              =   isset($value[3])?strip_javascript(xss_clean(trim($value[3]))):'';
                                                $phno               =   isset($value[4])?strip_javascript(xss_clean(trim($value[4]))):'';
                                                $whatsapp_no        =   isset($value[5])?strip_javascript(xss_clean(trim($value[5]))):'';
                                                $dob                =   isset($value[6])?strip_javascript(xss_clean(trim($value[6]))):'';
                                                $aadhar             =   isset($value[7])?strip_javascript(xss_clean(trim($value[7]))):'';
                                                $gender             =   isset($value[8])?strip_javascript(xss_clean(trim($value[8]))):'';
                                                $last_qualification =   isset($value[9])?strip_javascript(xss_clean(trim($value[9]))):'';
                                                $university_board   =   isset($value[10])?strip_javascript(xss_clean(trim($value[10]))):'';
                                                $year_of_passing    =   isset($value[11])?strip_javascript(xss_clean(trim($value[11]))):'';
                                                $obtained_marks     =   isset($value[12])?strip_javascript(xss_clean(trim($value[12]))):'';

                                                if(in_array($gender, array('m','M','male','Male','MALE'))){
                                                    $gender='male';
                                                }else if(in_array($gender, array('f','F','female','Female','FEMALE'))){
                                                    $gender='female';
                                                }else{
                                                    $gender='others';
                                                }


                                                $tempdata[]=array(
                                                    'temp_status_id'=>$status_added,
                                                    'first_name'=>$f_name,
                                                    'middle_name'=>$m_name,
                                                    'last_name'=>$l_name,
                                                    'email'=>$email,
                                                    'phone_no'=>$phno,
                                                    'whatsapp_no'=>$whatsapp_no,
                                                    'dob'=>$dob,
                                                    'aadhar'=>$aadhar,
                                                    'gender'=>$gender,
                                                    'last_qualification'=>$last_qualification,
                                                    'university_board'=>$university_board,
                                                    'year_of_passing'=>$year_of_passing,
                                                    'obtained_marks'=>$obtained_marks,
                                                    'created_by'=>$user_id
                                                );
                                            }

                                            //print_obj($tempdata);die;

                                            if(!empty($tempdata)){
                                                $added=$this->um->store_excel_temp_students($tempdata,TRUE);

                                                if($added){
                                                    $this->data['excel_temp_data']=$this->um->get_excel_temp_students(array('created_by'=>$user_id,'DATE(created_at)'=>$today),FALSE);
                                                    $this->data['session_id']=$session;
                                                    $this->data['course_id']=$course;
                                                    $this->data['subject_id']=$subject;
                                                    $return['html']=$this->theme->view('_pages/admission/vw_admission_seatbook_excel_temp_data',$this->data,true);
                                                    $return['success']='Excel data uploaded.You need to verify the data in next popup for final data import';
                                                }else{
                                                    $return['error']='Error occurred.Try after some time.';
                                                }
                                            }else{
                                                $return['error']='No data found to import';
                                            }
                                        }else{
                                            $return['error']='Excel can not be uploadede now.Try after sometime.';
                                        }      
                                    }else{
                                        $return['error']='Data can not be imported now.try after sometime.';
                                    }

                                        
                                    

                                    // if($data_imported==TRUE){
                                    //     @unlink($file_path);
                                    //     $return['success']='Data imported successfully';
                                    // }else{
                                    //     $return['error']='Data not imported into the system';
                                    // }
                                }else{
                                    $return['error']=$validated['error'];
                                }
                            }else{
                                $return['error']='File upload error';
                            }
                        }else{
                            $this->data['excel_temp_data']=$this->um->get_excel_temp_students(array('temp_status_id'=>$get_excel_tempstatus_students->excel_status_id),FALSE);
                            $this->data['session_id']=$get_excel_tempstatus_students->session_id;
                            $this->data['course_id']=$get_excel_tempstatus_students->course_id;
                            $this->data['subject_id']=$get_excel_tempstatus_students->subject_id;
                            $return['html']=$this->theme->view('_pages/admission/vw_admission_seatbook_excel_temp_data',$this->data,true);
                            $return['error']='Excel file already uploaded with same file name.Please check again.';
                        }                           

                    }else{
                        $return['error']='Excel file is missing';
                    }
                }else{
                    $return['error']='Select Session and Course';
                }

                   

                header('Content-Type: application/json');

                echo json_encode($return);
                session_write_close();

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


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

                $user_id=decode_data(session_userdata('admin_id'));
                $data_imported=FALSE;

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

                if($this->data['userdata']->user_type=='institute'){
                    $parent_inst_id=$this->data['userdata']->user_id;
                }else if($this->data['userdata']->user_type=='institute_branch'){
                    $parent_inst_id=$this->data['userdata']->branch_parent_inst_id;
                }

                //$excel_data=$this->input->post('excel_data');

                $session=post_data('session_id');
                $course=post_data('course_id');
                $subject=post_data('subject_id');

                $first_name=post_data('first_name');
                $middle_name=post_data('middle_name');
                $last_name=post_data('last_name');
                $email=post_data('email');
                $phone_no=post_data('phone_no');
                $whatsapp_no=post_data('whatsapp_no');
                $dob=post_data('dob');
                $aadhar=post_data('aadhar');
                $gender=post_data('gender');
                $last_qualification=post_data('last_qualification');
                $university_board=post_data('university_board');
                $year_of_passing=post_data('year_of_passing');
                $obtained_marks=post_data('obtained_marks');

                $temp_data_id=post_data('temp_data_id');

                $data_row=post_data('data_row');


                if(!empty($session) && !empty($course) && is_numeric($session) && is_numeric($course)){
                    $course_data=$this->cm->get_course(array('course_id'=>$course,'course_inst_id'=>$inst_id));

                    $session_data=$this->sessm->get_session(array('session_id'=>$session,'session_inst_id'=>$inst_id));

                    $course_session_data=$this->sessm->get_session_course(array('session_inst_id'=>$inst_id,'session_id'=>$session,'session_course_id'=>$course));

                    $get_college_seat_settings=$this->sm->get_college_seat_settings(array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));

                    if(!empty($get_college_seat_settings)){
                        $available_seats=$get_college_seat_settings->seats_available;
                    }else{
                        $available_seats='0';
                    }

                    //$student_data=$this->um->get_duplicate_student(array('stu_inst_id'=>$inst_id),array('stu_ph_no'=>$phone_no,'stu_email'=>$email),null,null,TRUE,FALSE);

                    $student_data_with_phone=$this->um->get_user(array('stu_inst_id'=>$inst_id,'stu_ph_no'=>$phone_no),'student');
                    $student_data_with_mail=$this->um->get_user(array('stu_inst_id'=>$inst_id,'stu_email'=>$email),'student');

                    //print_obj($student_data[0]->counted);die;                             

                    if(empty($student_data_with_phone) && empty($student_data_with_mail)){
                        if(!empty($email) && !empty($phone_no)){

                            $user_age=calculate_age($dob);

                            //echo $user_age;

                            if(valid_email($email) && isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)==12)){                               

                                $user_age=calculate_age($dob);


                                if($user_age>=18){
                                    $data_to_add=array(  
                                        'stu_inst_type'=>$this->data['userdata']->user_type,                      
                                        'stu_inst_id'=>$inst_id,
                                        'stu_inst_parent_id'=>$parent_inst_id,
                                        'stu_first_name'=>$first_name,
                                        'stu_mid_name'=>(!empty($middle_name))?$middle_name:null,
                                        'stu_last_name'=>$last_name,
                                        'stu_email'=>$email,
                                        'stu_ph_no'=>$phone_no,
                                        'stu_whatsapp_no'=>$whatsapp_no,
                                        'stu_dob'=>date('Y-m-d',strtotime($dob)),
                                        'stu_gender'=>$gender,
                                        'stu_nationality'=>'1',
                                        'stu_last_qualification_board'=>$university_board,
                                        'stu_last_qualification'=>$last_qualification,
                                        'stu_passing_year'=>$year_of_passing,
                                        'stu_passing_percentage'=>str_replace('%', '', $obtained_marks),
                                        'created_by'=>$this->data['userdata']->user_id,
                                        'created_at'=>date('Y-m-d')
                                    );

                                    $student_profile_id=$this->um->store_students($data_to_add);

                                    if($student_profile_id){
                                        if(!empty($this->data['student_code_settings'])){
                                            if ($this->data['student_code_settings']=='college_code/course_code/session/student_number') {
                                                $stu_user_code=strtoupper($this->data['user_code']).'/'.$course_data->course_code.'/'.$session_data->session_start_year.'-'.$session_data->session_end_year.'/'.$student_profile_id;
                                            }
                                        }else{
                                            $stu_user_code=strtoupper($this->data['user_code']).'/'.$course_data->course_code.'/'.$session_data->session_start_year.'-'.$sessios_data->session_end_year.'/'.$student_profile_id;
                                        }


                                        $booking_data=$this->adm->get_seat_booking_data(array('booking_stu_profile_id'=>$student_profile_id,'booking_inst_id'=>$inst_id,'booking_sess_id'=>$session,'booking_course_id'=>$course));


                                        if(empty($booking_data)){
                                            $booking_code=ge_rand_code_v1('SB'.$student_profile_id,2,TRUE);

                                            $booking_data_to_store=array(
                                                'booking_stu_profile_id'=>$student_profile_id,
                                                'booking_inst_id'=>$inst_id,
                                                'booking_inst_parent_id'=>$parent_inst_id,
                                                'booking_sess_id'=>$session,
                                                'booking_course_id'=>$course,
                                                'booking_subject_id'=>$subject,
                                                'booking_code'=>$booking_code,
                                                'booking_agent_id'=>0,                               
                                                'created_by'=>$this->data['userdata']->user_id,
                                                'created_at'=>date('Y-m-d')
                                            );

                                            $booking_id=$this->adm->store_seat_booking_data($booking_data_to_store);

                                            if($booking_id){
                                                if(!empty($get_college_seat_settings)){
                                                    $remaining_seats=$available_seats-1;
                                                    $this->sm->update_college_seat_settings(array('seats_available'=>$remaining_seats),array('seats_inst_id'=>$inst_id,'seats_sess_id'=>$session,'seats_course_id'=>$course));
                                                }

                                                $password=generate_password('Password@123');

                                                $student_userdata=array(
                                                    'user_name'=>$email,
                                                    'user_password'=>$password,
                                                    'user_password_visible'=>encode_data('Password@123'),
                                                    'user_type'=>'student',
                                                    'user_permissions'=>null,
                                                    'user_status'=>'active',
                                                    'created_by_type'=>'ntadmin',
                                                    'created_by'=>$this->data['userdata']->user_id,
                                                    'created_at'=>date('Y-m-d')
                                                );

                                                $student_user_id=$this->um->store_users($student_userdata);

                                                $this->um->update_students(array('stu_user_code'=>$stu_user_code,'stu_user_id'=>$student_user_id),array('stu_id'=>$student_profile_id));

                                                $seat_booking_payment_data=array(
                                                    'booking_pk_id'=>$booking_id,
                                                    'booking_inst_id'=>$inst_id,
                                                    'booking_inst_parent_id'=>$parent_inst_id,
                                                    'booking_stu_id'=>$student_user_id,
                                                    'booking_course_id'=>$course,
                                                    'booking_subject_id'=>'0',
                                                    'booking_sess_id'=>$session,
                                                    'booking_payment'=>$course_session_data->session_course_fees,
                                                    'booking_payment_bank'=>'',
                                                    'booking_payment_checq_no'=>'0',
                                                    'booking_payment_checq_files_ids'=>'0',
                                                    'booking_payment_deposite_bank'=>'1',
                                                    'booking_payment_paid'=>'no',
                                                    'created_at'=>date('Y-m-d'),
                                                    'created_by'=>$this->data['userdata']->user_id
                                                );

                                                $seat_booking_payment_data_found=$this->adm->get_seat_booking_payment_data(array('booking_inst_id'=>$inst_id,'booking_stu_id'=>$student_user_id));

                                                if(empty($seat_booking_payment_data_found)){
                                                    $this->adm->store_seat_booking_payment_data($seat_booking_payment_data);
                                                }


                                                $this->um->delete_excel_temp_students(array('phone_no'=>$phone_no));

                                                $temp_data_count=$this->um->get_count_excel_temp_students(array('temp_status_id'=>$temp_data_id));

                                                $return['temp_data_count']=$temp_data_count;

                                                //if(!empty($temp_data_count) && $
                                            }
                                        }

                                        $data_row_color='#00c22f';

                                         $return['success']='Students for seat bookings added at row no:'.$data_row;
                                    }else{
                                        $data_row_color='#f12409';
                                        $return['error']='Undefined error occurred at row no:'.$data_row;
                                    }
                                }else{
                                    $data_row_color='#f12409';
                                    $return['error']='Student is underage at row:'.$data_row;
                                }

                            }else if(valid_email($email) && !isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)==12)){
                                $data_row_color='#f12409';
                                $return['error']='DOB format is not valid at row:'.$data_row;
                            }else if(!valid_email($email) && isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)==12)){
                                $data_row_color='#f12409';
                                $return['error']='Email address is not valid at row:'.$data_row;
                            }else if(valid_email($email) && isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)!=12)){
                                $data_row_color='#f12409';
                                $return['error']='Aadhar no is not valid at row:'.$data_row;
                            }else if(!valid_email($email) && isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)!=12)){
                                $data_row_color='#f12409';
                                $return['error']='Email address & Aadhar no is not valid at row:'.$data_row;
                            }else if(valid_email($email) && !isDateValid($dob) && (is_numeric($aadhar) && strlen($aadhar)!=12)){
                                $data_row_color='#f12409';
                                $return['error']='DOB & Aadhar no is not valid at row:'.$data_row;
                            }
                            else{
                                $data_row_color='#f12409';
                                $return['error']='Email address , DOB, Aadhar is not valid at row:'.$data_row;
                            }

                                

                                
                        }else{
                            $data_row_color='#f12409';
                            $return['error']='Data not added at row no:'.$data_row;;
                        }
                                                                                         
                    }else if(empty($student_data_with_phone) && !empty($student_data_with_mail)){
                        $data_row_color='#f12409';
                        $return['error']='Student alreday exists with same email address at row:'.$data_row;
                    }else if(!empty($student_data_with_phone) && empty($student_data_with_mail)){
                        $data_row_color='#f12409';
                        $return['error']='Student alreday exists with same phone no at row:'.$data_row;
                    }
                    else{
                        $data_row_color='#f12409';
                        $return['error']='Student alreday exists with same phone no or email address at row:'.$data_row;
                    }

                    $return['data_row']=$data_row;
                    $return['data_row_color']=$data_row_color;
                    
                }else{
                    $return['data_row']=$data_row;
                    $return['error']='Data manipulation is not allowed.';
                }

                json_headers($return);
                session_write_close();

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



    public function onValidateExcel($uploaded_file){
        if($uploaded_file!=''){
            $row_arrays=array(
                'first_name',
                'middle_name',
                'last_name',
                'email',
                'phone_no',
                'whatsapp_no',
                'dob',
                'aadhar',
                'gender',
                'last_qualification',
                'university_board',
                'year_of_passing',
                'obtained_marks(%)'
            );

            $heading=$this->excel->getRangedColumnData($uploaded_file,'A','M',true,false);

            $diff = array_diff($heading[0][0], $row_arrays);

            if(empty($diff) && (count($heading[0][0])===13)){
                $count=1;

                $exceldata=$this->excel->getRangedColumnData($uploaded_file,'A','M',true,false);

                //print_obj($exceldata);die;

                $table_rows='';
                if($exceldata!='' || !empty($exceldata)){
                    // foreach ($exceldata as $key => $value){
                    //  $count++;
                    //  $row=$key+2;

                    //  $name   =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][1]))):'';
                    //  $email  =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][2]))):'';
                    //  $phno   =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][3]))):'';
                    //  $estd   =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][4]))):'';
                    //  $country    =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][5]))):'';
                    //  $state  =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][6]))):'';
                    //  $district   =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][7]))):'';
                    //  $city   =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][8]))):'';
                    //  $address    =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][9]))):'';
                    //  $pincode    =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][10]))):'';
                    //  $university =   isset($value[0][1])?strip_javascript(xss_clean(trim($value[0][11]))):'';

                        
                    // }

                    unset($exceldata[0]);

                    return ['validated'=>200,'exceldata'=>$exceldata];
                }else{
                    //return 'Excel data format is wrong.Please download the sample excel for your reference 1';
                    $table_rows='';
                    return ['validated'=>400,'table_rows'=>$table_rows,'error'=>'Excel data format headings are wrong.Please download the sample excel for your reference'];
                }
            }else{
                $table_rows='';
                //return 'Excel data format is wrong.Please download the sample excel for your reference 2';
                return ['validated'=>400,'table_rows'=>$table_rows,'error'=>'Excel data format headings are wrong.Please download the sample excel for your reference'];
            }
        }else{
            return ['error'=>'Excel file not selected'];
        }
    }



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

                $booking_id=post_data('booking_id');

                $booking_data=$this->adm->get_seat_booking_data(array('booking_id'=>$booking_id));

                if(!empty($booking_data)){
                    $payment_data=$this->adm->get_seat_booking_payment_data(array('booking_pk_id'=>$booking_data->booking_id));

                    if($payment_data->booking_payment_paid=='no'){
                        $updated=$this->adm->update_seat_booking_data(array('is_deleted'=>'yes','deleted_by'=>$this->user_id,'deleted_at'=>date('Y-m-d')),array('booking_id'=>$booking_id));

                        if($updated){
                            $student_data=$this->um->get_students(array('stu_id'=>$booking_data->booking_stu_profile_id));
                            $student_user_data=$this->um->_get_user(array('user_id'=>$student_data->stu_user_id));

                            if(!empty($student_data)){
                                
                                $this->um->update_students(array('stu_removed'=>'yes','deleted_by'=>$this->user_id,'deleted_at'=>date('Y-m-d H:i:s')),array('stu_user_id'=>$student_data->stu_user_id));

                                $this->um->update_users(array('is_deleted'=>'yes','deleted_by'=>$this->user_id,'deleted_at'=>date('Y-m-d H:i:s')),array('user_id'=>$student_data->stu_user_id));                                
                            }

                            $log_user_type=$this->user_type;
                            $log_user_id=$this->user_id;
                            $log_type='seatbooking_soft_delete';
                            $log_details=json_encode(array($booking_data->booking_code=>array('booking_data'=>$booking_data,'student_profile_data'=>$student_data,'student_user_data'=>$student_user_data)));

                            $log_data=compact('log_user_type','log_user_id','log_type','log_details');
                            create_log($log_data);

                            $return['success']='Data has been deleted successfully.';
                        }else{
                            $return['error']='Data can not be deleted now.';
                        }
                    }else{
                        $return['error']='Booking data can not be deleted as payment already done.';
                    }
                }else{
                    $return['error']='Booking data not found in the system';
                }

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



    
    
}

MMCT - 2023