GIF89a; CRX
KBHT HEHE
Server IP : 172.26.0.195  /  Your IP : 3.133.109.251
Web Server : Apache
System : Linux 43-205-77-33.cprapid.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User : jnclnmuac ( 1026)
PHP Version : 8.0.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/jnclnmuac/public_html/web/../admission/../cas/application/controllers/admin/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/jnclnmuac/public_html/web/../admission/../cas/application/controllers/admin/FeeConfig.php
<?php

/**
 * Controller For Handling All Requests Related To Fee Configuration
 *
 * @author Softpro India Pvt. Ltd.
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class FeeConfig extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/FeeManagement');
        $this->load->model('admin/SessionManagement');
        $this->load->model('admin/CourseManagement');
        $this->load->model('admin/BranchManagement');
        $this->load->model('admin/StudentManagement');
    }

    public function heads() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allFeeHeads'] = $this->FeeManagement->getAllFeeHeads()->result();
            $this->load->view('admin/fee/feeHeads', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->load->view('admin/fee/createFeeHead');
        } else {
            redirect("admin/");
        }
    }

    public function saveNewFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('feeHeadName', 'Fee Head Name', 'trim|required', array('required' => 'Fee Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeHeadShortName', 'Fee Head Short Name', 'trim|required', array('required' => 'Fee Head Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeHeadType', 'Fee Head Type', 'trim|required', array('required' => 'Please Select Any Type.'));
            $this->form_validation->set_rules('feeHeadCalculation', 'Fee Head Calculation', 'trim|required', array('required' => 'Please Select Fee Calculation.'));
            $queryByFeeHeadName = $this->FeeManagement->getFeeHeadInfoByName(addslashes(trim($this->input->post('feeHeadName'))));
            $feeHeadInfoByName = $queryByFeeHeadName->result();
            $queryByFeeHeadShortName = $this->FeeManagement->getFeeHeadInfoByShortName(addslashes(trim($this->input->post('feeHeadShortName'))));
            $feeHeadInfoByShortName = $queryByFeeHeadShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createFeeHead();
            } else if (sizeof($feeHeadInfoByName)) {
                $this->session->set_flashdata('errorMessage', "A Fee Head With This Name (" . addslashes(trim($this->input->post('feeHeadName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->createFeeHead();
            } else if (sizeof($feeHeadInfoByShortName)) {
                $this->session->set_flashdata('errorMessage', "A Fee Head With This Short Name (" . addslashes(trim($this->input->post('feeHeadShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->createFeeHead();
            } else {
                $newFeeHeadInfo = array(
                    'fhm_name' => addslashes(trim($this->input->post('feeHeadName'))),
                    'fhm_short_name' => addslashes(trim($this->input->post('feeHeadShortName'))),
                    'fhm_type' => trim($this->input->post('feeHeadType')),
                    'fhm_calculation' => trim($this->input->post('feeHeadCalculation')),
                    'fhm_description' => addslashes(trim($this->input->post('feeHeadDesc'))),
                    'fhm_added_on' => date("Y-m-d H:i:s"),
                    'fhm_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'fhm_updated_on' => date("Y-m-d H:i:s"),
                    'fhm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->createNewFeeHead($newFeeHeadInfo)) {
                    $this->session->set_flashdata('successMessage', 'Fee Head Created Successfully.');
                    redirect("admin/FeeConfig/heads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Fee Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editFeeHead($fhm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['feeHeadInfo'] = $this->FeeManagement->getFeeHeadInfoBy($fhm_id)->result()[0];
            $this->load->view('admin/fee/editFeeHead', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $fhm_id = $this->input->post('feeHeadId');
            $this->form_validation->set_rules('feeHeadName', 'Fee Head Name', 'trim|required', array('required' => 'Fee Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeHeadShortName', 'Fee Head Short Name', 'trim|required', array('required' => 'Fee Head Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeHeadType', 'Fee Head Type', 'trim|required', array('required' => 'Please Select Any Type.'));
            $this->form_validation->set_rules('feeHeadCalculation', 'Fee Head Calculation', 'trim|required', array('required' => 'Please Select Fee Calculation.'));
            if ($this->form_validation->run() == FALSE) {
                $this->editFeeHead($fhm_id);
            } else if (!$this->FeeManagement->isFeeHeadNameSafeUpdate($fhm_id, trim($this->input->post('feeHeadName')))) {
                $this->session->set_flashdata('errorMessage', "A Fee Head With This Name (" . addslashes(trim($this->input->post('feeHeadName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->editFeeHead($fhm_id);
            } else if (!$this->FeeManagement->isFeeHeadShortNameSafeUpdate($fhm_id, trim($this->input->post('feeHeadShortName')))) {
                $this->session->set_flashdata('errorMessage', "A Fee Head With This Short Name (" . addslashes(trim($this->input->post('feeHeadShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->editFeeHead($fhm_id);
            } else {
                $feeHeadUpdateData = array(
                    'fhm_id' => $fhm_id,
                    'fhm_name' => addslashes(trim($this->input->post('feeHeadName'))),
                    'fhm_short_name' => addslashes(trim($this->input->post('feeHeadShortName'))),
                    'fhm_type' => trim($this->input->post('feeHeadType')),
                    'fhm_calculation' => trim($this->input->post('feeHeadCalculation')),
                    'fhm_description' => addslashes(trim($this->input->post('feeHeadDesc'))),
                    'fhm_updated_on' => date("Y-m-d H:i:s"),
                    'fhm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->updateFeeHeadInfo($feeHeadUpdateData)) {
                    $this->session->set_flashdata('successMessage', 'Fee Head Updated Successfully.');
                    redirect("admin/FeeConfig/heads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Fee Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleFeeHeadStatus($fhm_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeHeadUpdateData = array(
                'fhm_id' => $fhm_id,
                'fhm_updated_on' => date("Y-m-d H:i:s"),
                'fhm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhm_active_status' => $toUpdateStatus
            );
            if ($this->FeeManagement->updateFeeHeadInfo($feeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Head Status Updated Successfully.');
                redirect("admin/FeeConfig/heads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Fee Head Status. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteFeeHead($fhm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeHeadUpdateData = array(
                'fhm_id' => $fhm_id,
                'fhm_updated_on' => date("Y-m-d H:i:s"),
                'fhm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhm_delete_status' => 'T'
            );
            if ($this->FeeManagement->updateFeeHeadInfo($feeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Head Deleted Successfully.');
                redirect("admin/FeeConfig/heads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Fee Head. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteFeeHead($fhm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeHeadUpdateData = array(
                'fhm_id' => $fhm_id,
                'fhm_updated_on' => date("Y-m-d H:i:s"),
                'fhm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhm_delete_status' => 'F'
            );
            if ($this->FeeManagement->updateFeeHeadInfo($feeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Head Recovered Successfully.');
                redirect("admin/FeeConfig/heads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Fee Head. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    /* Request Handlers For Fee Structure */

    public function structure() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $selectedSession = "";
            $selectedEntryType = "";
            $selectedSemOrYear = "";
            $feeStructure = array();
            $viewData['branches'] = $this->BranchManagement->getAllNonDeletedActiveBrances()->result();
            $viewData['sessions'] = $this->SessionManagement->getNonDeletedActiveSessions()->result();
            $viewData['courses'] = $this->CourseManagement->getActiveAndNonDeletedCourses()->result();
            $viewData['categories'] = json_decode(file_get_contents(base_url('assets/admin/json/categories.json')), true);
            if (isset($_POST['getFeeStructure'])) {
                $selectedSession = trim($this->input->post('session'));
                $selectedEntryType = trim($this->input->post('entryType'));
                $selectedCourse = trim($this->input->post('course'));
                $selectedSemOrYear = trim($this->input->post('semesterOrYear'));
                $selectedCategory = trim($this->input->post('category'));
                if ($selectedCourse == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Course.");
                } else if ($selectedSession == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Session.");
                } else {
                    $this->session->set_flashdata('errorMessage', NULL);
                    $feeStructure = $this->FeeManagement->getFeeStructureByCourseAndSession($selectedCourse, $selectedSession, $selectedEntryType, $selectedSemOrYear, $selectedCategory)->result();
                }
            }
            $viewData['selectedSession'] = $selectedSession;
            $viewData['selectedEntryType'] = $selectedEntryType;
            $viewData['selectedSemOrYear'] = $selectedSemOrYear;
            $viewData['allFeeStructures'] = $feeStructure;
            $this->load->view('admin/fee/feeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createEditFeeStructure() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $selectedSession = "";
            $selectedEntryType = "";
            $selectedSemOrYear = "";
            $feeHeadsAndStructure = array();
            $viewData['branches'] = $this->BranchManagement->getAllNonDeletedActiveBrances()->result();
            $viewData['sessions'] = $this->SessionManagement->getNonDeletedActiveSessions()->result();
            $viewData['courses'] = $this->CourseManagement->getActiveAndNonDeletedCourses()->result();
            $viewData['categories'] = json_decode(file_get_contents(base_url('assets/admin/json/categories.json')), true);
            if (isset($_POST['getFeeHeads'])) {
                $selectedSession = trim($this->input->post('session'));
                $selectedEntryType = trim($this->input->post('entryType'));
                $selectedCourse = trim($this->input->post('course'));
                $selectedSemOrYear = trim($this->input->post('semesterOrYear'));
                $selectedCategory = trim($this->input->post('category'));
                if ($selectedCourse == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Course.");
                } else if ($selectedSession == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Session.");
                } else if ($selectedEntryType == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Entry Type.");
                } else if ($selectedSemOrYear == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Semester Or Year.");
                } else if ($selectedCategory == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Category.");
                } else {
                    $this->session->set_flashdata('errorMessage', NULL);
                    $allFeeHeads = $this->FeeManagement->getNonDeletedFeeHeads()->result();
                    for ($i = 0; $i < sizeof($allFeeHeads); $i++) {
                        $feeStructureInfo = $this->FeeManagement->getFeeStructureInfoBy($allFeeHeads[$i]->fhm_id, $selectedSession, $selectedEntryType, $selectedSemOrYear, $selectedCategory)->result();
                        if ($allFeeHeads[$i]->fhm_type == "R") {
                            $feeType = "Recurring";
                        } else if ($allFeeHeads[$i]->fhm_type == "O") {
                            $feeType = "One Time";
                        } else if ($allFeeHeads[$i]->fhm_type == "F") {
                            $feeType = "Fine";
                        } else if ($allFeeHeads[$i]->fhm_type == "RF") {
                            $feeType = "Refundable";
                        } else if ($allFeeHeads[$i]->fhm_type == "E") {
                            $feeType = "Excess";
                        } else if ($allFeeHeads[$i]->fhm_type == "S") {
                            $feeType = "Scholarship";
                        } else {
                            $feeType = "Unknown";
                        }
                        if (sizeof($feeStructureInfo)) {
                            array_push($feeHeadsAndStructure, array(
                                'fhm_id' => $allFeeHeads[$i]->fhm_id,
                                'fhm_name' => stripslashes($allFeeHeads[$i]->fhm_name),
                                'fhm_type' => $feeType,
                                'fhm_type_flag' => $allFeeHeads[$i]->fhm_type,
                                'fsm_id' => $feeStructureInfo[0]->fsm_id,
                                'fsm_amount' => ($feeStructureInfo[0]->fsm_amount == "" || $feeStructureInfo[0]->fsm_amount == NULL) ? 0.00 : $feeStructureInfo[0]->fsm_amount,
                                'if_checked' => TRUE
                            ));
                        } else {
                            array_push($feeHeadsAndStructure, array(
                                'fhm_id' => $allFeeHeads[$i]->fhm_id,
                                'fhm_name' => stripslashes($allFeeHeads[$i]->fhm_name),
                                'fhm_type' => $feeType,
                                'fhm_type_flag' => $allFeeHeads[$i]->fhm_type,
                                'fsm_id' => NULL,
                                'fsm_amount' => 0,
                                'if_checked' => FALSE
                            ));
                        }
                    }
                }
            } else if (isset($_POST['saveBtn'])) {
                $selectedSession = trim($this->input->post('session'));
                $selectedEntryType = trim($this->input->post('entryType'));
                $selectedCourse = trim($this->input->post('course'));
                $selectedSemOrYear = trim($this->input->post('semesterOrYear'));
                $selectedCategory = trim($this->input->post('category'));
                $selectedHeads = $this->input->post('selectedFeeHeads');
                $selectedHeadsFee = $this->input->post('amount');
                $selectedFeeStructureIds = $this->input->post('fsmIds');
                $toUpdateFeeStructureInfo = array();
                $toCreateFeeStructureInfo = array();
                for ($i = 0; $i < sizeof($selectedHeads); $i++) {
                    if (trim($selectedFeeStructureIds[$selectedHeads[$i]]) == "" || trim($selectedFeeStructureIds[$selectedHeads[$i]]) == NULL) {
                        array_push($toCreateFeeStructureInfo, array(
                            'course_id' => $selectedCourse,
                            'ucs_map_id' => $selectedEntryType,
                            'session_id' => $selectedSession,
                            'fhm_id' => $selectedHeads[$i],
                            'fsm_semester' => $selectedSemOrYear,
                            'fsm_amount' => trim($selectedHeadsFee[$selectedHeads[$i]]),
                            'fsm_category' => trim($selectedCategory),
                            'fsm_added_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsm_added_on' => date("Y-m-d H:i:s"),
                            'fsm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsm_updated_on' => date("Y-m-d H:i:s")
                        ));
                    } else {
                        array_push($toUpdateFeeStructureInfo, array(
                            'fsm_id' => trim($selectedFeeStructureIds[$selectedHeads[$i]]),
                            'fsm_amount' => trim($selectedHeadsFee[$selectedHeads[$i]]),
                            'fsm_delete_status' => 'F',
                            'fsm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsm_updated_on' => date("Y-m-d H:i:s")
                        ));
                    }
                }
                $this->db->trans_start();
                if ($this->FeeManagement->setFeeStructureToDeletedInfoBy($selectedSession, $selectedCourse, $selectedSemOrYear, $selectedCategory)) {
                    if (sizeof($toCreateFeeStructureInfo)) {
                        $this->FeeManagement->createNewFeeStructureInfoMulti($toCreateFeeStructureInfo);
                    }
                    if (sizeof($toUpdateFeeStructureInfo)) {
                        $this->FeeManagement->updateFeeStructureInfoMulti($toUpdateFeeStructureInfo);
                    }
                    $this->db->trans_complete();
                    $this->session->set_flashdata('successMessage', 'Fee Structure Updated Successfully.');
                    redirect("admin/FeeConfig/structure");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Fee Structure. Try Later.');
                    redirect(current_url());
                }
            }
            $viewData['selectedSession'] = $selectedSession;
            $viewData['selectedEntryType'] = $selectedEntryType;
            $viewData['selectedSemOrYear'] = $selectedSemOrYear;
            $viewData['feeHeadAndStructure'] = $feeHeadsAndStructure;
            $this->load->view('admin/fee/createEditFeeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    /* Request Handlers For Fee Structure (Student Specific) */

    public function setupStudentFee($tspi_id = '', $newOrOld = '') {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            if (isset($_POST['saveBtn'])) {
                $selectedSession = trim($this->input->post('session'));
                $selectedEntryType = trim($this->input->post('entryType'));
                $selectedCourse = trim($this->input->post('course'));
                $selectedSemOrYear = $this->input->post('semesterOrYear');
                $selectedHeads = $this->input->post('selectedFeeHeads');
                $selectedHeadsFee = $this->input->post('amount');
                $remarks = $this->input->post('remarks');
                $selectedFeeStructureIds = $this->input->post('fsmIds');
                $toCreateFeeStructureInfo = array();
                for ($i = 0; $i < sizeof($selectedFeeStructureIds); $i++) {
                    if (trim($selectedFeeStructureIds[$i]) != "" || trim($selectedFeeStructureIds[$i]) != NULL) {
                        array_push($toCreateFeeStructureInfo, array(
                            'course_id' => $selectedCourse,
                            'ucs_map_id' => $selectedEntryType,
                            'session_id' => $selectedSession,
                            'fhm_id' => $selectedHeads[$i],
                            'fssm_semester' => trim($selectedSemOrYear[$i]),
                            'fssm_amount' => trim($selectedHeadsFee[$i]),
                            'fssm_remarks' => (trim($remarks[$i]) == "") ? NULL : addslashes(trim($remarks[$i])),
                            'tspi_id' => trim($this->input->post("tspi_id")),
                            'fssm_added_by' => $this->session->userdata("adminData")["smember_id"],
                            'fssm_added_on' => date("Y-m-d H:i:s"),
                            'fssm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fssm_updated_on' => date("Y-m-d H:i:s")
                        ));
                    }
                }
                $this->db->trans_start();
                if (sizeof($toCreateFeeStructureInfo)) {
                    $this->FeeManagement->createNewFeeStructureOfStudentMulti($toCreateFeeStructureInfo);
                }
                $this->db->trans_complete();
                $this->session->set_flashdata('successMessage', 'Fee Structure Of Student Updated Successfully.');
                if (trim($this->input->post("new_or_old")) == 'N') {
                    redirect("admin/Fee/collectFee/" . trim($this->input->post("tspi_id")));
                } else {
                    redirect("admin/Fee/collectFeeOld/" . trim($this->input->post("tspi_id")));
                }
            } else {
                if ($tspi_id != '') {
                    $studentData = $this->StudentManagement->getStudentPersonalInfoAndPhotoSignBy($tspi_id)->result();
                    $feeInfo = array();
                    $error = "";
                    if (sizeof($studentData)) {
                        $student_info = $studentData[0];
                        $studentFatherName = $this->StudentManagement->getStudentFatherInfoBy($student_info->tspi_id)->result()[0]->tfbi_name;
                        $studentCurrentSemesterInfo = $this->StudentManagement->getStudentCurrentSectionSemesterInfoBy($student_info->tspi_id)->result();
                        $gender = "";
                        $studentCurrentSemesterOrYear = "";
                        $semesterOrYearToShow = "";
                        $timeMgmtFlag = ($student_info->course_time_mgmt_flag == "S") ? "Sem." : "Year";
                        $entryType = stripslashes($student_info->tcsm_course_type);
                        if ($student_info->tspi_gender == "M") {
                            $gender = "<i class='fa fa-male'></i>|Male";
                        } else if ($student_info->tspi_gender == "F") {
                            $gender = "<i class='fa fa-female'></i>|Female";
                        } else {
                            $gender = "<i class='fa fa-transgender'></i>|Transgender";
                        }
                        if (sizeof($studentCurrentSemesterInfo)) {
                            $studentCurrentSemesterOrYear = $studentCurrentSemesterInfo[0]->tsssi_semester;
                            $semesterOrYearToShow = $studentCurrentSemesterOrYear;
                        } else {
                            $studentCurrentSemesterOrYear = $student_info->tcsm_sem_start_from;
                            $semesterOrYearToShow = "New Admission";
                        }
                        /* Setting Student's Data */
                        $studentData = array(
                            'stu_id' => $student_info->tspi_id,
                            'stu_name' => stripslashes($student_info->tspi_name),
                            'stu_father_name' => stripslashes($studentFatherName),
                            'stu_gender' => $gender,
                            'stu_form_no' => stripslashes($student_info->tspi_form_no),
                            'stu_roll_no' => ($student_info->tspi_form_no == $student_info->tspi_rollNumber) ? "-" : stripslashes($student_info->tspi_rollNumber),
                            'stu_enrollment_no' => ($student_info->tspi_form_no == $student_info->tspi_enrollment_no) ? "-" : stripslashes($student_info->tspi_enrollment_no),
                            'stu_dob' => date('d-m-Y', strtotime($student_info->tspi_dob)),
                            'stu_category' => stripslashes($student_info->tspi_category),
                            'stu_course' => stripslashes($student_info->course_name),
                            'stu_course_id' => stripslashes($student_info->course_id),
                            'stu_entry_type' => $entryType,
                            'stu_ucs_map_id' => $student_info->ucs_map_id,
                            'stu_session' => stripslashes($student_info->session_name),
                            'stu_session_id' => $student_info->session_id,
                            'stu_batch' => explode('-', $student_info->session_name)[0] . "-" . $student_info->session_end_year,
                            'stu_photograph' => base_url("assets/admin/uploads/studentData/") . $student_info->studentPhoto,
                            'stu_sign' => base_url("assets/admin/uploads/studentData/") . $student_info->studentSign,
                            'stu_sch_eligible' => ($student_info->tspi_scholarship_eligible == "T") ? "Yes (" . $student_info->tspi_scholarship_percentage . "%)" : "Not Eligible",
                            'stu_current_semester' => $semesterOrYearToShow
                        );
                        /* Setting Student's Data */
                        $categoryForDefaultFee = ($student_info->tspi_gender == "F") ? "Female" : stripslashes($student_info->tspi_category);
                        $eligibleStructureForPayment = $this->FeeManagement->getFeeStructureForFeePaymentBy('', $student_info->ucs_map_id, '', $categoryForDefaultFee)->result();
                        if (sizeof($eligibleStructureForPayment)) {
                            for ($i = 0; $i < sizeof($eligibleStructureForPayment); $i++) {
                                if ($eligibleStructureForPayment[$i]->fhm_type == "R") {
                                    $feeType = "Recurring";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "O") {
                                    $feeType = "One Time";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "F") {
                                    $feeType = "Fine";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "RF") {
                                    $feeType = "Refundable";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "E") {
                                    $feeType = "Excess";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "S") {
                                    $feeType = "Scholarship";
                                } else {
                                    $feeType = "Unknown";
                                }
                                array_push($feeInfo, array(
                                    'fhm_id' => $eligibleStructureForPayment[$i]->fhm_id,
                                    'fsm_id' => $eligibleStructureForPayment[$i]->fsm_id,
                                    'fhm_name' => stripslashes($eligibleStructureForPayment[$i]->fhm_name),
                                    'fhm_short_name' => stripslashes($eligibleStructureForPayment[$i]->fhm_short_name),
                                    'fhm_type' => $feeType,
                                    'fhm_type_flag' => $eligibleStructureForPayment[$i]->fhm_type,
                                    'fhm_calculation' => $eligibleStructureForPayment[$i]->fhm_calculation,
                                    'fsm_amount' => $eligibleStructureForPayment[$i]->fsm_amount,
                                    'paying_for_year_sem' => $eligibleStructureForPayment[$i]->fsm_semester,
                                    'course_time_type' => $timeMgmtFlag,
                                    'new_or_old_flag' => $newOrOld
                                ));
                            }
                        } else {
                            $error = "Default Fee Has Not Been Setup For This Course & Session (" . stripslashes($student_info->course_name) . "/" . $student_info->session_name . ") Or For This Category/Gender. Please Ask Admin To Setup Default Fee Details First.";
                        }
                    } else {
                        $error = "Sorry! The Selected Student Does Not Exist Any More.";
                        $studentData = array();
                        $feeInfo = array();
                    }
                }
            }
            $finalArray = array();
            for ($i = 0; $i < sizeof($feeInfo); $i++) {
                array_push($finalArray, $feeInfo[$i]);
            }
            $viewData['error'] = $error;
            $viewData['studentInfo'] = $studentData;
            $viewData['studentFatherName'] = stripslashes($studentFatherName);
            $viewData['feeInfo'] = $finalArray;
            $this->load->view('admin/fee/createStudentFeeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function editStudentFee($tspi_id = '', $newOrOld = '') {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            if (isset($_POST['saveBtn'])) {
                $selectedHeadsFee = $this->input->post('amount');
                $remarks = $this->input->post('remarks');
                $selectedFeeStructureIds = $this->input->post('fssmIds');
                $toUpdateFeeStructureInfo = array();
                for ($i = 0; $i < sizeof($selectedFeeStructureIds); $i++) {
                    if (trim($selectedFeeStructureIds[$i]) != "" || trim($selectedFeeStructureIds[$i]) != NULL) {
                        array_push($toUpdateFeeStructureInfo, array(
                            'fssm_id' => trim($selectedFeeStructureIds[$i]),
                            'fssm_amount' => trim($selectedHeadsFee[$i]),
                            'fssm_remarks' => (trim($remarks[$i]) == "") ? NULL : addslashes(trim($remarks[$i])),
                            'fssm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fssm_updated_on' => date("Y-m-d H:i:s")
                        ));
                    }
                }
                $this->db->trans_start();
                if (sizeof($toUpdateFeeStructureInfo)) {
                    $this->FeeManagement->updateFeeStructureOfStudentMulti($toUpdateFeeStructureInfo);
                }
                $this->db->trans_complete();
                $this->session->set_flashdata('successMessage', 'Fee Structure Of Student Updated Successfully.');
                if (trim($this->input->post("new_or_old")) == 'N') {
                    redirect("admin/Fee/collectFee/" . trim($this->input->post("tspi_id")));
                } else {
                    redirect("admin/Fee/collectFeeOld/" . trim($this->input->post("tspi_id")));
                }
            } else {
                if ($tspi_id != '') {
                    $studentData = $this->StudentManagement->getStudentPersonalInfoAndPhotoSignBy($tspi_id)->result();
                    $feeInfo = array();
                    $error = "";
                    if (sizeof($studentData)) {
                        $student_info = $studentData[0];
                        $studentFatherName = $this->StudentManagement->getStudentFatherInfoBy($student_info->tspi_id)->result()[0]->tfbi_name;
                        $studentCurrentSemesterInfo = $this->StudentManagement->getStudentCurrentSectionSemesterInfoBy($student_info->tspi_id)->result();
                        $gender = "";
                        $studentCurrentSemesterOrYear = "";
                        $semesterOrYearToShow = "";
                        $timeMgmtFlag = ($student_info->course_time_mgmt_flag == "S") ? "Sem." : "Year";
                        $entryType = stripslashes($student_info->tcsm_course_type);
                        if ($student_info->tspi_gender == "M") {
                            $gender = "<i class='fa fa-male'></i>|Male";
                        } else if ($student_info->tspi_gender == "F") {
                            $gender = "<i class='fa fa-female'></i>|Female";
                        } else {
                            $gender = "<i class='fa fa-transgender'></i>|Transgender";
                        }
                        if (sizeof($studentCurrentSemesterInfo)) {
                            $studentCurrentSemesterOrYear = $studentCurrentSemesterInfo[0]->tsssi_semester;
                            $semesterOrYearToShow = $studentCurrentSemesterOrYear;
                        } else {
                            $studentCurrentSemesterOrYear = $student_info->tcsm_sem_start_from;
                            $semesterOrYearToShow = "New Admission";
                        }
                        /* Setting Student's Data */
                        $studentData = array(
                            'stu_id' => $student_info->tspi_id,
                            'stu_name' => stripslashes($student_info->tspi_name),
                            'stu_father_name' => stripslashes($studentFatherName),
                            'stu_gender' => $gender,
                            'stu_form_no' => stripslashes($student_info->tspi_form_no),
                            'stu_roll_no' => ($student_info->tspi_form_no == $student_info->tspi_rollNumber) ? "-" : stripslashes($student_info->tspi_rollNumber),
                            'stu_enrollment_no' => ($student_info->tspi_form_no == $student_info->tspi_enrollment_no) ? "-" : stripslashes($student_info->tspi_enrollment_no),
                            'stu_dob' => date('d-m-Y', strtotime($student_info->tspi_dob)),
                            'stu_category' => stripslashes($student_info->tspi_category),
                            'stu_course' => stripslashes($student_info->course_name),
                            'stu_course_id' => stripslashes($student_info->course_id),
                            'stu_entry_type' => $entryType,
                            'stu_ucs_map_id' => $student_info->ucs_map_id,
                            'stu_session' => stripslashes($student_info->session_name),
                            'stu_session_id' => $student_info->session_id,
                            'stu_batch' => explode('-', $student_info->session_name)[0] . "-" . $student_info->session_end_year,
                            'stu_photograph' => base_url("assets/admin/uploads/studentData/") . $student_info->studentPhoto,
                            'stu_sign' => base_url("assets/admin/uploads/studentData/") . $student_info->studentSign,
                            'stu_sch_eligible' => ($student_info->tspi_scholarship_eligible == "T") ? "Yes (" . $student_info->tspi_scholarship_percentage . "%)" : "Not Eligible",
                            'stu_current_semester' => $semesterOrYearToShow
                        );
                        /* Setting Student's Data */
                        $eligibleStructureForPayment = $this->FeeManagement->getFeeStructureOfStudentBy($tspi_id, '')->result();
                        if (sizeof($eligibleStructureForPayment)) {
                            for ($i = 0; $i < sizeof($eligibleStructureForPayment); $i++) {
                                if ($eligibleStructureForPayment[$i]->fhm_type == "R") {
                                    $feeType = "Recurring";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "O") {
                                    $feeType = "One Time";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "F") {
                                    $feeType = "Fine";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "RF") {
                                    $feeType = "Refundable";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "E") {
                                    $feeType = "Excess";
                                } else if ($eligibleStructureForPayment[$i]->fhm_type == "S") {
                                    $feeType = "Scholarship";
                                } else {
                                    $feeType = "Unknown";
                                }
                                array_push($feeInfo, array(
                                    'fhm_id' => $eligibleStructureForPayment[$i]->fhm_id,
                                    'fssm_id' => $eligibleStructureForPayment[$i]->fssm_id,
                                    'fhm_name' => stripslashes($eligibleStructureForPayment[$i]->fhm_name),
                                    'fhm_short_name' => stripslashes($eligibleStructureForPayment[$i]->fhm_short_name),
                                    'fhm_type' => $feeType,
                                    'fhm_type_flag' => $eligibleStructureForPayment[$i]->fhm_type,
                                    'fhm_calculation' => $eligibleStructureForPayment[$i]->fhm_calculation,
                                    'fssm_amount' => $eligibleStructureForPayment[$i]->fssm_amount,
                                    'fssm_remarks' => stripslashes($eligibleStructureForPayment[$i]->fssm_remarks),
                                    'paying_for_year_sem' => $eligibleStructureForPayment[$i]->fssm_semester,
                                    'course_time_type' => $timeMgmtFlag,
                                    'new_or_old_flag' => $newOrOld
                                ));
                            }
                        } else {
                            $error = "Default Fee Has Not Been Setup For The Course & Session (" . stripslashes($student_info->course_name) . "/" . $student_info->session_name . ") Or For This Category/Gender. Please Ask Admin To Setup Default Fee Details First.";
                        }
                    } else {
                        $error = "Sorry! The Selected Student Does Not Exist Any More.";
                        $studentData = array();
                        $feeInfo = array();
                    }
                }
            }
            $finalArray = array();
            for ($i = 0; $i < sizeof($feeInfo); $i++) {
                array_push($finalArray, $feeInfo[$i]);
            }
            $viewData['error'] = $error;
            $viewData['studentInfo'] = $studentData;
            $viewData['studentFatherName'] = stripslashes($studentFatherName);
            $viewData['feeInfo'] = $finalArray;
            $this->load->view('admin/fee/editStudentFeeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    /* Request Handlers For Fee Modes */

    public function modes() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allFeeModes'] = $this->FeeManagement->getAllFeeModes()->result();
            $this->load->view('admin/fee/feeModes', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createFeeMode() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->load->view('admin/fee/createFeeMode');
        } else {
            redirect("admin/");
        }
    }

    public function saveNewFeeMode() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('feeModeName', 'Fee Mode Name', 'trim|required', array('required' => 'Fee Mode Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeModeShortName', 'Fee Mode Short Name', 'trim|required', array('required' => 'Fee Mode Short Name Can Not Be Blank.'));
            $queryByFeeModeName = $this->FeeManagement->getFeeModeInfoByName(addslashes(trim($this->input->post('feeModeName'))));
            $feeModeInfoByName = $queryByFeeModeName->result();
            $queryByFeeModeShortName = $this->FeeManagement->getFeeModeInfoByShortName(addslashes(trim($this->input->post('feeModeShortName'))));
            $feeModeInfoByShortName = $queryByFeeModeShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createFeeMode();
            } else if (sizeof($feeModeInfoByName)) {
                $this->session->set_flashdata('errorMessage', "A Fee Mode With This Name (" . addslashes(trim($this->input->post('feeModeName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->createFeeMode();
            } else if (sizeof($feeModeInfoByShortName)) {
                $this->session->set_flashdata('errorMessage', "A Fee Mode With This Short Name (" . addslashes(trim($this->input->post('feeModeShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->createFeeMode();
            } else {
                $newFeeModeInfo = array(
                    'fmm_name' => addslashes(trim($this->input->post('feeModeName'))),
                    'fmm_short_name' => addslashes(trim($this->input->post('feeModeShortName'))),
                    'fmm_description' => addslashes(trim($this->input->post('feeModeDesc'))),
                    'fmm_added_on' => date("Y-m-d H:i:s"),
                    'fmm_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'fmm_updated_on' => date("Y-m-d H:i:s"),
                    'fmm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->createNewFeeMode($newFeeModeInfo)) {
                    $this->session->set_flashdata('successMessage', 'Fee Mode Created Successfully.');
                    redirect("admin/FeeConfig/modes");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Fee Mode. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editFeeMode($fmm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['feeModeInfo'] = $this->FeeManagement->getFeeModeInfoBy($fmm_id)->result()[0];
            $this->load->view('admin/fee/editFeeMode', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateFeeMode() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $fmm_id = $this->input->post('feeModeId');
            $this->form_validation->set_rules('feeModeName', 'Fee Mode Name', 'trim|required', array('required' => 'Fee Mode Name Can Not Be Blank.'));
            $this->form_validation->set_rules('feeModeShortName', 'Fee Mode Short Name', 'trim|required', array('required' => 'Fee Mode Short Name Can Not Be Blank.'));
            if ($this->form_validation->run() == FALSE) {
                $this->editFeeMode($fmm_id);
            } else if (!$this->FeeManagement->isFeeModeNameSafeUpdate($fmm_id, trim($this->input->post('feeModeName')))) {
                $this->session->set_flashdata('errorMessage', "A Fee Mode With This Name (" . addslashes(trim($this->input->post('feeModeName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->editFeeMode($fmm_id);
            } else if (!$this->FeeManagement->isFeeModeShortNameSafeUpdate($fmm_id, trim($this->input->post('feeModeShortName')))) {
                $this->session->set_flashdata('errorMessage', "A Fee Mode With This Short Name (" . addslashes(trim($this->input->post('feeModeShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->editFeeMode($fmm_id);
            } else {
                $feeModeUpdateData = array(
                    'fmm_id' => $fmm_id,
                    'fmm_name' => addslashes(trim($this->input->post('feeModeName'))),
                    'fmm_short_name' => addslashes(trim($this->input->post('feeModeShortName'))),
                    'fmm_description' => addslashes(trim($this->input->post('feeModeDesc'))),
                    'fmm_updated_on' => date("Y-m-d H:i:s"),
                    'fmm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->updateFeeModeInfo($feeModeUpdateData)) {
                    $this->session->set_flashdata('successMessage', 'Fee Mode Updated Successfully.');
                    redirect("admin/FeeConfig/modes");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Fee Mode. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleFeeModeStatus($fmm_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeModeUpdateData = array(
                'fmm_id' => $fmm_id,
                'fmm_updated_on' => date("Y-m-d H:i:s"),
                'fmm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fmm_active_status' => $toUpdateStatus
            );
            if ($this->FeeManagement->updateFeeModeInfo($feeModeUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Mode Status Updated Successfully.');
                redirect("admin/FeeConfig/modes");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Fee Mode Status. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteFeeMode($fmm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeModeUpdateData = array(
                'fmm_id' => $fmm_id,
                'fmm_updated_on' => date("Y-m-d H:i:s"),
                'fmm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fmm_delete_status' => 'T'
            );
            if ($this->FeeManagement->updateFeeModeInfo($feeModeUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Mode Deleted Successfully.');
                redirect("admin/FeeConfig/modes");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Fee Mode. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteFeeMode($fmm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeModeUpdateData = array(
                'fmm_id' => $fmm_id,
                'fmm_updated_on' => date("Y-m-d H:i:s"),
                'fmm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fmm_delete_status' => 'F'
            );
            if ($this->FeeManagement->updateFeeModeInfo($feeModeUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Fee Mode Recovered Successfully.');
                redirect("admin/FeeConfig/modes");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Fee Mode. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    /* Request Handlers For Fee Head (Optional) */

    public function optionalHeads() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allOptionalFeeHeads'] = $this->FeeManagement->getAllOptionalFeeHeads()->result();
            $this->load->view('admin/fee/optionalFeeHeads', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createOptionalFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->load->view('admin/fee/createOptionalFeeHead');
        } else {
            redirect("admin/");
        }
    }

    public function saveNewOptionalFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('optFeeHeadName', 'Optional Fee Head Name', 'trim|required', array('required' => 'Optional Fee Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('optFeeHeadShortName', 'Optional Fee Head Short Name', 'trim|required', array('required' => 'Optional Fee Head Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('optFeeHeadType', 'Optional Fee Head Type', 'trim|required', array('required' => 'Please Select Any Type.'));
            if ($this->input->post('optFeeHeadType') == 'H') {
                $this->form_validation->set_rules('optFeeHeadCap', 'Capacity', 'trim|required', array('required' => 'Capacity Can Not Be Blank.'));
            }
            $queryByOptFeeHeadName = $this->FeeManagement->getFeeHeadInfoByName(addslashes(trim($this->input->post('optFeeHeadName'))));
            $optFeeHeadInfoByName = $queryByOptFeeHeadName->result();
            $queryByOptFeeHeadShortName = $this->FeeManagement->getOptionalFeeHeadInfoByShortName(addslashes(trim($this->input->post('optFeeHeadShortName'))));
            $optFeeHeadInfoByShortName = $queryByOptFeeHeadShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createOptionalFeeHead();
            } else if (sizeof($optFeeHeadInfoByName)) {
                $this->session->set_flashdata('errorMessage', "An Optional Fee Head With This Name (" . addslashes(trim($this->input->post('optFeeHeadName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->createOptionalFeeHead();
            } else if (sizeof($optFeeHeadInfoByShortName)) {
                $this->session->set_flashdata('errorMessage', "An Optional Fee Head With This Short Name (" . addslashes(trim($this->input->post('optFeeHeadShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->createOptionalFeeHead();
            } else {
                $newOptionalFeeHeadInfo = array(
                    'fhom_name' => addslashes(trim($this->input->post('optFeeHeadName'))),
                    'fhom_short_name' => addslashes(trim($this->input->post('optFeeHeadShortName'))),
                    'fhom_type' => trim($this->input->post('optFeeHeadType')),
                    'fhom_description' => addslashes(trim($this->input->post('optFeeHeadDesc'))),
                    'fhom_capacity' => trim($this->input->post('optFeeHeadCap')),
                    'fhom_added_on' => date("Y-m-d H:i:s"),
                    'fhom_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'fhom_updated_on' => date("Y-m-d H:i:s"),
                    'fhom_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->createNewOptionalFeeHead($newOptionalFeeHeadInfo)) {
                    $this->session->set_flashdata('successMessage', 'Optional Fee Head Created Successfully.');
                    redirect("admin/FeeConfig/optionalHeads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Optional Fee Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editOptionalFeeHead($fhom_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['optFeeHeadInfo'] = $this->FeeManagement->getOptionalFeeHeadInfoBy($fhom_id)->result()[0];
            $this->load->view('admin/fee/editOptionalFeeHead', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateOptionalFeeHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $fhom_id = $this->input->post('optFeeHeadId');
            $this->form_validation->set_rules('optFeeHeadName', 'Optional Fee Head Name', 'trim|required', array('required' => 'Optional Fee Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('optFeeHeadShortName', 'Optional Fee Head Short Name', 'trim|required', array('required' => 'Optional Fee Head Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('optFeeHeadType', 'Optional Fee Head Type', 'trim|required', array('required' => 'Please Select Any Type.'));
            if ($this->input->post('optFeeHeadType') == 'H') {
                $this->form_validation->set_rules('optFeeHeadCap', 'Capacity', 'trim|required', array('required' => 'Capacity Can Not Be Blank.'));
            }
            if ($this->form_validation->run() == FALSE) {
                $this->editOptionalFeeHead($fhom_id);
            } else if (!$this->FeeManagement->isOptionalFeeHeadNameSafeUpdate($fhom_id, trim($this->input->post('optFeeHeadName')))) {
                $this->session->set_flashdata('errorMessage', "An Optional Fee Head With This Name (" . addslashes(trim($this->input->post('optFeeHeadName'))) . ") Already Exits. Please Choose A Different Name.");
                $this->editOptionalFeeHead($fhom_id);
            } else if (!$this->FeeManagement->isOptionalFeeHeadShortNameSafeUpdate($fhom_id, trim($this->input->post('optFeeHeadShortName')))) {
                $this->session->set_flashdata('errorMessage', "An Optional Fee Head With This Short Name (" . addslashes(trim($this->input->post('optFeeHeadShortName'))) . ") Already Exits. Please Choose A Different Short Name.");
                $this->editOptionalFeeHead($fhom_id);
            } else {
                $optFeeHeadUpdateData = array(
                    'fhom_id' => $fhom_id,
                    'fhom_name' => addslashes(trim($this->input->post('optFeeHeadName'))),
                    'fhom_short_name' => addslashes(trim($this->input->post('optFeeHeadShortName'))),
                    'fhom_type' => trim($this->input->post('optFeeHeadType')),
                    'fhom_description' => addslashes(trim($this->input->post('optFeeHeadDesc'))),
                    'fhom_capacity' => trim($this->input->post('optFeeHeadCap')),
                    'fhom_updated_on' => date("Y-m-d H:i:s"),
                    'fhom_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->FeeManagement->updateOptionalFeeHeadInfo($optFeeHeadUpdateData)) {
                    $this->session->set_flashdata('successMessage', 'Optional Fee Head Updated Successfully.');
                    redirect("admin/FeeConfig/optionalHeads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Optional Fee Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleOptionalFeeHeadStatus($fhom_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $optFeeHeadUpdateData = array(
                'fhom_id' => $fhom_id,
                'fhom_updated_on' => date("Y-m-d H:i:s"),
                'fhom_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhom_active_status' => $toUpdateStatus
            );
            if ($this->FeeManagement->updateOptionalFeeHeadInfo($optFeeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Optional Fee Head Status Updated Successfully.');
                redirect("admin/FeeConfig/optionalHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Optional Fee Head Status. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteOptionalFeeHead($fhom_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $feeHeadUpdateData = array(
                'fhom_id' => $fhom_id,
                'fhom_updated_on' => date("Y-m-d H:i:s"),
                'fhom_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhom_delete_status' => 'T'
            );
            if ($this->FeeManagement->updateOptionalFeeHeadInfo($feeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Optional Fee Head Deleted Successfully.');
                redirect("admin/FeeConfig/optionalHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Optional Fee Head. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteOptionalFeeHead($fhom_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $optFeeHeadUpdateData = array(
                'fhom_id' => $fhom_id,
                'fhom_updated_on' => date("Y-m-d H:i:s"),
                'fhom_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'fhom_delete_status' => 'F'
            );
            if ($this->FeeManagement->updateOptionalFeeHeadInfo($optFeeHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Optional Fee Head Recovered Successfully.');
                redirect("admin/FeeConfig/optionalHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Optional Fee Head. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    /* Request Handlers For Fee Structure */

    public function optionalStructure() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allOptionalFeeStructures'] = $this->FeeManagement->getAllOptionalFeeStructure()->result();
            $this->load->view('admin/fee/optionalFeeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createEditFeeOptionalStructure() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $selectedSession = "";
            $feeHeadsAndStructure = array();
            $viewData['sessions'] = $this->SessionManagement->getNonDeletedActiveSessions(array('C', 'O'))->result();
            if (isset($_POST['getFeeHeads'])) {
                $selectedSession = trim($this->input->post('session'));
                if ($selectedSession == "") {
                    $this->session->set_flashdata('errorMessage', "Please Select Any Session.");
                } else {
                    $this->session->set_flashdata('errorMessage', NULL);
                    $allOptionalFeeHeads = $this->FeeManagement->getNonDeletedOptionalFeeHeads()->result();
                    for ($i = 0; $i < sizeof($allOptionalFeeHeads); $i++) {
                        $optFeeStructureInfo = $this->FeeManagement->getOptionalFeeStructureInfoBy($allOptionalFeeHeads[$i]->fhom_id, $selectedSession)->result();
                        if (sizeof($optFeeStructureInfo)) {
                            array_push($feeHeadsAndStructure, array(
                                'fhom_id' => $allOptionalFeeHeads[$i]->fhom_id,
                                'fhom_name' => stripslashes($allOptionalFeeHeads[$i]->fhom_name),
                                'fhom_type' => ($allOptionalFeeHeads[$i]->fhom_type == "T") ? "Transport" : "Hostel",
                                'fsom_id' => $optFeeStructureInfo[0]->fsom_id,
                                'fsom_amount' => ($optFeeStructureInfo[0]->fsom_amount == "" || $optFeeStructureInfo[0]->fsom_amount == NULL) ? 0.00 : $optFeeStructureInfo[0]->fsom_amount,
                                'if_checked' => TRUE
                            ));
                        } else {
                            array_push($feeHeadsAndStructure, array(
                                'fhom_id' => $allOptionalFeeHeads[$i]->fhom_id,
                                'fhom_name' => stripslashes($allOptionalFeeHeads[$i]->fhom_name),
                                'fhom_type' => ($allOptionalFeeHeads[$i]->fhom_type == "T") ? "Transport" : "Hostel",
                                'fsom_id' => NULL,
                                'fsom_amount' => 0,
                                'if_checked' => FALSE
                            ));
                        }
                    }
                }
            } else if (isset($_POST['saveBtn'])) {
                $selectedSession = trim($this->input->post('session'));
                $selectedHeads = $this->input->post('selectedFeeHeads');
                $selectedHeadsFee = $this->input->post('amount');
                $selectedFeeStructureIds = $this->input->post('fsomIds');
                $toUpdateFeeStructureInfo = array();
                $toCreateFeeStructureInfo = array();
                for ($i = 0; $i < sizeof($selectedHeads); $i++) {
                    if (trim($selectedFeeStructureIds[$selectedHeads[$i]]) == "" || trim($selectedFeeStructureIds[$selectedHeads[$i]]) == NULL) {
                        array_push($toCreateFeeStructureInfo, array(
                            'session_id' => $selectedSession,
                            'fhom_id' => $selectedHeads[$i],
                            'fsom_amount' => trim($selectedHeadsFee[$selectedHeads[$i]]),
                            'fsom_added_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsom_added_on' => date("Y-m-d H:i:s"),
                            'fsom_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsom_updated_on' => date("Y-m-d H:i:s")
                        ));
                    } else {
                        array_push($toUpdateFeeStructureInfo, array(
                            'fsom_id' => trim($selectedFeeStructureIds[$selectedHeads[$i]]),
                            'fsom_amount' => trim($selectedHeadsFee[$selectedHeads[$i]]),
                            'fsom_delete_status' => 'F',
                            'fsom_updated_by' => $this->session->userdata("adminData")["smember_id"],
                            'fsom_updated_on' => date("Y-m-d H:i:s")
                        ));
                    }
                }
                $this->db->trans_start();
                if ($this->FeeManagement->setOptionalFeeStructureToDeletedInfoBy($selectedSession)) {
                    if (sizeof($toCreateFeeStructureInfo)) {
                        $this->FeeManagement->createNewOptionalFeeStructureInfoMulti($toCreateFeeStructureInfo);
                    }
                    if (sizeof($toUpdateFeeStructureInfo)) {
                        $this->FeeManagement->updateOptionalFeeStructureInfoMulti($toUpdateFeeStructureInfo);
                    }
                    $this->db->trans_complete();
                    $this->session->set_flashdata('successMessage', 'Optional Fee Structure Updated Successfully.');
                    redirect("admin/FeeConfig/optionalStructure");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Optional Fee Structure. Try Later.');
                    redirect(current_url());
                }
            }
            $viewData['selectedSession'] = $selectedSession;
            $viewData['feeHeadAndStructure'] = $feeHeadsAndStructure;
            $this->load->view('admin/fee/createEditOptionalFeeStructure', $viewData);
        } else {
            redirect("admin/");
        }
    }

    /* Request Handler For Hostel Fee Module */

    public function getFeeAgainst() {
        $head = $_POST['head'];
        $session = $_POST['session'];
        $query = $this->FeeManagement->getOptionalFeeStructureInfoBy($head, $session);
        $optHeadFee = $query->result();
        $value = 0;
        if (sizeof($optHeadFee)) {
            $value = $optHeadFee[0]->fsom_amount;
            $responseData = array(
                'csrfName' => $this->security->get_csrf_token_name(),
                'csrfHash' => $this->security->get_csrf_hash(),
                'fee' => $value,
                'dueAmount' => 0.00,
                'fsom_id' => $optHeadFee[0]->fsom_id,
                'structureFound' => true
            );
        } else {
            $responseData = array(
                'csrfName' => $this->security->get_csrf_token_name(),
                'csrfHash' => $this->security->get_csrf_hash(),
                'fee' => 0,
                'dueAmount' => 0.00,
                'fsom_id' => 0,
                'structureFound' => false);
        }
        echo json_encode($responseData);
    }

}

KBHT - 2023