<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Member extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        error_reporting(0);
        //error_reporting(E_ALL);
        $this->load->library('javascript');
        $this->load->library('form_validation');
        $this->load->library('email');
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->database();
        $this->load->library('session');
        $this->load->helper('url');
        session_status() === PHP_SESSION_ACTIVE ?: session_start();
        $method_name = $this->router->fetch_method();
        if (!isset($_SESSION['name']) && $method_name != "identify" && $method_name != "index") {
            redirect('Member/index', 'refresh');
        }
        $idletime = 600000;
        if (time() - $_SESSION['timestamp'] > $idletime) {
            session_unset();
        } else {
            $_SESSION['timestamp'] = time();
        }
        // error_reporting(E_ALL);
        //if ($_SESSION['name'] == "") {
        //    redirect('index.php/login/index', 'refresh');
        //}
        //error_reporting(E_ALL);
    }

    public function index()
    {
        $this->load->view('user/login');
    }
    public function identify()
    {
        $_SESSION["name"] = $this->input->post('name');
        $_SESSION["pass"] = $this->input->post('pass');
        $_SESSION['timestamp'] = time();
        redirect('Member/verify');
    }
    public function verify()
    {
        if ($this->common_model->check($_SESSION)) {
            $this->load->model('member_model');
            $data['member'] = $this->member_model->totol_member();
            $data['active'] = $this->member_model->totol_active_member();
            $data['inactive'] = $this->member_model->totol_inactive_member();

            $data['thrift'] = $this->member_model->totol_thrift_amount();
            $data['active_thrift'] = $this->member_model->totol_active_thrift();
            $data['inactive_thrift'] = $this->member_model->totol_inactive_thrift();

            $data['share'] = $this->member_model->totol_share_amount();
            $data['active_share'] = $this->member_model->totol_active_share();
            $data['inactive_share'] = $this->member_model->totol_inactive_share();

            $data['ins'] = $this->member_model->totol_ins_amount();
            $data['active_ins'] = $this->member_model->totol_active_ins();
            $data['inactive_ins'] = $this->member_model->totol_inactive_ins();

            $data['loan_amt'] = $this->member_model->totol_loan_amount();

            $lastMonth = Date("F", strtotime("first day of previous month"));
            $year = Date("Y");
            $inps['month'] =   $lastMonth;
            $inps['year'] = $year;
            $data['demand'] = $this->member_model->last_demand_amount($inps);
            $data['recovery'] = $this->member_model->last_recovery_amount($inps);
            $data['partial'] = $this->member_model->last_partial_amount($inps);


            $today = date('m-d');
            if ($today == '04-01') {
                $os_amount = $this->member_model->cb_loan_amount();
                $thrift_amount = $this->member_model->cb_thrift_amount();
                $share_amount = $this->member_model->cb_share_amount();
                foreach ($os_amount as $os) {
                    // print_r($os['loan_os_amt']);die;
                    $up_loan = array("me_no" => $os['me_no'], "cb_os" => $os['loan_os_amt']);
                    $this->member_model->update_loan($up_loan);
                }
                foreach ($thrift_amount as $thrift) {
                    // print_r($thrift['tot_thrift']);die;date('Y-m-d')
                    // print_r(date('Y-m-d'));die;

                    $up_thrift = array("me_no" => $thrift['me_no'], "cb_thrift" => $thrift['tot_thrift'], "cb_date" => date('Y-m-d'));
                    $this->member_model->update_thrift($up_thrift);
                }
                foreach ($share_amount as $share) {
                    // print_r($share['tot_share']);die;
                    $up_share = array("me_no" => $share['me_no'], "cb_share" => $share['tot_share']);
                    $this->member_model->update_share($up_share);
                }
            }

            $data['content'] = 'admin/dashboard';
            $this->load->view('layout/main', $data);
        } else {
            $data['error'] = 'Invalid Account';
            redirect('Member/index', $data);
        }
    }
    public function dashboard()
    {
        $this->load->model('member_model');
        $data['member'] = $this->member_model->totol_member();
        $data['active'] = $this->member_model->totol_active_member();
        $data['inactive'] = $this->member_model->totol_inactive_member();

        $data['thrift'] = $this->member_model->totol_thrift_amount();
        $data['active_thrift'] = $this->member_model->totol_active_thrift();
        $data['inactive_thrift'] = $this->member_model->totol_inactive_thrift();

        $data['share'] = $this->member_model->totol_share_amount();
        $data['active_share'] = $this->member_model->totol_active_share();
        $data['inactive_share'] = $this->member_model->totol_inactive_share();

        $data['ins'] = $this->member_model->totol_ins_amount();
        $data['active_ins'] = $this->member_model->totol_active_ins();
        $data['inactive_ins'] = $this->member_model->totol_inactive_ins();

        $data['loan_amt'] = $this->member_model->totol_loan_amount();

        $lastMonth = Date("F", strtotime("first day of previous month"));
        $year = Date("Y");
        $inps['month'] =   $lastMonth;
        $inps['year'] = $year;
        $data['demand'] = $this->member_model->last_demand_amount($inps);
        $data['recovery'] = $this->member_model->last_recovery_amount($inps);
        $data['partial'] = $this->member_model->last_partial_amount($inps);

        $data['content'] = 'admin/dashboard';
        $this->load->view('layout/main', $data);
    }
    public function add_member()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['members'] = $this->member_model->members();
        $data['default'] = $this->member_model->get_default();
        $data['banks'] = $this->member_model->get_bank();
        $result = $this->db->query("select max(me_no) as me_no from member");
        $member = $result->result();
        $data['me_no'] = $member[0]->me_no;
        // print_r($member);die;
        //print_r($result->result()); die();
        $data['value'] = $this->member_model->get_caste();
        $data['relationship'] = $this->member_model->get_relationship();
        $data['design'] = $this->member_model->get_design();

        $data['content'] = 'member/add';
        $this->load->view('layout/main', $data);
    }

    //checking member number
    public function check_memno_exists()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $member = $this->member_model->get_member($inps['me_no']);
        if (isset($member['me_no']) && $member['me_no'] != "") {
            echo "Member Number already exist";
        } else {
            echo 1;
        }
    }
    //checking mobile number
    public function check_mem_mob()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $member = $this->member_model->get_mobile($inps['mem_mob']);
        if (isset($member['mem_mob']) && $member['mem_mob'] != "") {
            echo "Mobile  Number already exist";
        } else {
            echo 1;
        }
    }
    //get section
    public function getsection()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $sections = $this->member_model->getsection($inps);
        $html = '<option value="">All</option>';
        foreach ($sections as $sec) {
            $html .= "<option value='" . $sec['section'] . "'>" . $sec['section'] . "</option>";
        }
        echo $html;
    }
    //insert Member
    public function insert_member()
    {
        $this->load->model('member_model');
        $_SESSION['name'] = $this->input->post('mem_name');
        $inps = $this->input->post();
        $common_table = $this->db->get("common")->result_array();
        $tot_pay = $inps['ent_fees'] + $inps['d_share'] + $inps['d_ins'] + $inps['d_others'] + $inps['d_books'];
        if ($tot_pay > 0) {
            $rec_res = $this->db->query("select max(rec_no) as rec_no from payment")->result_array();
            $rec_no = $rec_res[0]['rec_no'] + 1;
        } else {
            $rec_no = 0;
        }
        $ins_data_rec = array("me_no" => $inps['me_no'], "ent_fees" => $inps['ent_fees'], "thrift_amt" => '0', "share_amt" => $inps['d_share'], "insurance" => $inps['d_ins'], "others" => $inps['d_others'], "tot_pay" => $tot_pay, 'payment_month' => date('F', strtotime($inps['mem_doj'])), 'payment_year' => date('Y', strtotime($inps['mem_doj'])), "rec_no" => $rec_no, "cash_type" => $inps['cash_type'], "cheque_trans_id" => $inps['cheque_trans_id'], "paid_on" => date("Y-m-d", strtotime($inps['mem_doj'])), "paid_by" => $_SESSION['name'], "books" => $inps['d_books']);
        //    print_r($ins_data_rec);die;
        $this->db->insert("payment", $ins_data_rec);
        unset($inps['d_books']);
        unset($inps['ent_fees']);
        unset($inps['d_thrift']);
        unset($inps['d_share']);
        unset($inps['d_ins']);
        unset($inps['d_others']);
        unset($inps['cash_type']);
        unset($inps['cheque_trans_id']);

        if ($_FILES["mem_image"]["name"] != "") {
            $data['image'] = $target_file = "./assets/img/" . time() . basename($_FILES["mem_image"]["name"]);
            if (move_uploaded_file($_FILES["mem_image"]["tmp_name"], $target_file)) {
                $inps['mem_image'] =  time() . basename($_FILES["mem_image"]["name"]);
            } else {
                $inps['mem_image'] = "";
            }
        }
        // die();

        $member = $this->member_model->get_member($inps['me_no']);
        if (isset($member['me_no']) && $member['me_no'] != "") {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Member Number already exist</div>';
            redirect('Member/member_details', 'refresh');
        }
        if ($inps['mem_dob'] != "") {
            $inps['mem_dob'] = date("Y-m-d", strtotime($inps['mem_dob']));
        }
        if ($inps['mem_doj'] != "") {
            $inps['mem_doj'] = date("Y-m-d", strtotime($inps['mem_doj']));
        }
        if ($inps['scty_doj'] != "") {
            $inps['scty_doj'] = date("Y-m-d", strtotime($inps['scty_doj']));
        }
        if ($inps['mem_dor'] != "") {
            $inps['mem_dor'] = date("Y-m-d", strtotime($inps['mem_dor']));
        }
        $res = $this->member_model->add($inps);
        $res = $this->member_model->add_default_thrift($inps['me_no']);
        $res = $this->member_model->add_default_share($inps['me_no']);
        $res = $this->member_model->add_default_insurance($inps['me_no']);
        $res = $this->member_model->add_default_others($inps['me_no']);
        $res = $this->db->insert("loan", array("me_no" => $inps['me_no']));
        $res = $this->db->insert("old_dues", array("me_no" => $inps['me_no']));
        $_SESSION['mem_add'] = "Member Added Successfully";
        redirect('Member/member_details', 'refresh');
    }
    //view member
    public function member_details()
    {
        // print_r($_SESSION);
        // die();
        $this->load->model('member_model');
        $data['division'] = $this->member_model->division();
        $data['section'] = $this->member_model->section();
        $data['members'] = $this->member_model->members();
        //print "<pre>";
        // print_r($data);
        //  die();
        $data['content'] = 'member/index';
        $this->load->view('layout/main', $data);
    }
    //member transfer
    public function member_transfer()
    {

        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['members'] = $this->member_model->members();
        $data['content'] = 'member/transfer';
        $this->load->view('layout/main', $data);
    }
    public function transfer()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update($inps);
            $data = array("mem_no" => $inps['me_no'], "division" => $inps['division'], "section" => $inps['section']);
            $this->db->insert("transfer", $data);
        }
        $_SESSION['mem_add'] = "Member Transfered Successfully";
        redirect('Member/member_transfer', 'refresh');
    }
    function get_division_section()
    {
        $this->load->model('member_model');
        $inps = $this->input->post();
        $res = $this->db->select("*")->where("me_no", $inps['me_no'])->get("member")->result_array();
        echo json_encode($res[0]);
    }
    //member active
    public function member_active()
    {

        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['members'] = $this->member_model->members();
        $data['content'] = 'member/active';
        $this->load->view('layout/main', $data);
    }
    //member deactive
    public function member_deactive()
    {

        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['members'] = $this->member_model->deactive_members();
        $data['content'] = 'member/deactive';
        $this->load->view('layout/main', $data);
    }
    //member close
    public function member_close()
    {
        $this->load->model('member_model');
        $data['division'] = $this->member_model->division();
        $data['section'] = $this->member_model->section();
        $data['members'] = $this->member_model->closed_members();
        $data['content'] = 'member/close';
        $this->load->view('layout/main', $data);
    }
    public function member_closed()
    {
        $this->load->model('member_model');
        $inps['month'] = date('F');
        $inps['year'] = date("Y");
        $data['division'] = $this->member_model->division();
        $data['section'] = $this->member_model->section();
        $data['members'] = $this->member_model->close_members($inps);
        $data['content'] = 'member/close';
        $this->load->view('layout/main', $data);
    }
    //member search
    public function member_search()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $members = $this->member_model->member_search($inps);
        $html = '
       <table id="mem_detail" class="table table-striped table-bordered" style="width:100%">
       <thead>
           <tr>
           <th class="text-center">Member No.</th>
           <th class="text-center">Member Name</th>
           <th class="text-center">Division</th>
           <th class="text-center">Section</th>
           <th class="text-center">Phone No.</th>
           <th class="text-center">Action</th>
           </tr>
   </thead>
     
   <tbody>
       ';
        foreach ($members as $member) :
            if ($member['mem_mob'] > 0) {

                $mem_mob = $member['mem_mob'];
            } else {
                $mem_mob = "";
            }
            $html .= '  <tr>
           <td class="text-center">
               <a href="' . base_url() . 'index.php/Member/view/" >' . $member["me_no"] . '</a>
           </td>
           <td >
         
              ' . $member["mem_name"] . '
           </td>
           <td >
           
               ' . $member['division'] . '
           </td>
           <td >
        
               ' . $member['section'] . '
           </td>
           <td >
       ' . $mem_mob . '
           </td>
           <td class="text-center">
           <a href="' . base_url() . 'index.php/Member/edit_member/" class="btn btn-primary btn-xs" ><span class="fa fa-edit"></span><span class="sr-only"></span></a>
           <a href="' . base_url() . 'index.php/Member/view_member/" class="btn btn-primary btn-xs" ><span class="fa fa-search"></span><span class="sr-only"></span></a>
           <a href="' . base_url() . 'index.php/Member/close_member/" class="btn btn-danger btn-xs"> <span class="fa fa-times"></span><span class="sr-only"></span></a>
  
           </td>
       </tr>';
        endforeach;
        $html .= '
</tbody>
    </table>
';
        echo $html;
    }
    //search active member
    public function member_search_active()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $members = $this->member_model->member_search($inps);

        $html = '
       <table id="mem_detail" class="table table-striped table-bordered" style="width:100%">
       <thead>
           <tr>
           <th class="text-center">Member No.</th>
           <th class="text-center">Member Name</th>
           <th class="text-center">Division</th>
           <th class="text-center">Section</th>
           <th class="text-center">Phone No.</th>
           <th class="text-center">Action</th>
           </tr>
   </thead>
     
   <tbody>
       ';
        foreach ($members as $member) :
            if ($member['mem_mob'] > 0) {

                $mem_mob = $member['mem_mob'];
            } else {
                $mem_mob = "";
            }
            $html .= '  <tr>
           <td class="text-center">
               <a href="' . base_url() . 'index.php/Member/view/' . $member["me_no"] . '" >' . $member["me_no"] . '</a>
           </td>
           <td >
         
              ' . $member["mem_name"] . '
           </td>
           <td >
           
               ' . $member['division'] . '
           </td>
           <td >
        
               ' . $member['section'] . '
           </td>
           <td >
       ' . $mem_mob . '
           </td>
           <td class="text-center">
           <a href="' . base_url() . 'index.php/Member/deactive/' . $member['me_no'] . '" class="btn btn-primary btn-xs" ><span class="fa fa-ban"></span><span class="sr-only"></span></a>
           </td>
       </tr>';
        endforeach;
        $html .= '
</tbody>
    </table>
';
        echo $html;
    }
    //search deactive member
    public function member_search_deactive()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $members = $this->member_model->member_search_deactive($inps);

        $html = '
       <table id="mem_detail" class="table table-striped table-bordered" style="width:100%">
       <thead>
           <tr>
           <th class="text-center">Member No.</th>
           <th class="text-center">Member Name</th>
           <th class="text-center">Division</th>
           <th class="text-center">Section</th>
           <th class="text-center">Phone No.</th>
           <th class="text-center">Action</th>
           </tr>
   </thead>
     
   <tbody>
       ';
        foreach ($members as $member) :
            if ($member['mem_mob'] > 0) {

                $mem_mob = $member['mem_mob'];
            } else {
                $mem_mob = "";
            }
            $html .= '  <tr>
           <td class="text-center">
               <a href="' . base_url() . 'index.php/Member/view/' . $member["me_no"] . '" >' . $member["me_no"] . '</a>
           </td>
           <td >
         
              ' . $member["mem_name"] . '
           </td>
           <td >
           
               ' . $member['division'] . '
           </td>
           <td >
        
               ' . $member['section'] . '
           </td>
           <td >
       ' . $mem_mob . '
           </td>
           <td class="text-center">
           <a href="' . base_url() . 'index.php/Member/active/' . $member['me_no'] . '" class="btn btn-primary btn-xs" ><span class="fa fa-check"></span><span class="sr-only"></span></a>
           </td>
       </tr>';
        endforeach;
        $html .= '
</tbody>
    </table>
';
        echo $html;
    }
    //search deactive member
    public function member_search_close()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $members = $this->member_model->member_search_close($inps);

        $html = '
       <table id="table_id1" class="table table-striped table-bordered" style="width:100%">
       <thead>
           <tr>
           <th class="text-center">Member No.</th>
           <th class="text-center">Member Name</th>
           <th class="text-center">Division</th>
           <th class="text-center">Section</th>
           <th class="text-center">Phone No.</th>
           </tr>
   </thead>
     
   <tbody>
       ';
        foreach ($members as $member) :
            if ($member['mem_mob'] > 0) {

                $mem_mob = $member['mem_mob'];
            } else {
                $mem_mob = "";
            }
            $html .= '  <tr>
           <td class="text-center">
               <a href="' . base_url() . 'index.php/Member/view/' . $member["me_no"] . '" >' . $member["me_no"] . '</a>
           </td>
           <td >
         
              ' . $member["mem_name"] . '
           </td>
           <td >
           
               ' . $member['division'] . '
           </td>
           <td >
        
               ' . $member['section'] . '
           </td>
           <td >
       ' . $mem_mob . '
           </td>
       </tr>';
        endforeach;
        $html .= '
</tbody>
    </table>
';
        echo $html;
    }
    //edit member
    public function edit_member()
    {
        $me_no = $this->uri->segment(3);
        //  echo $me_no; die();
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['divisions'] = $this->member_model->division();
            $data['sections'] = $this->member_model->section();
            $data['member'] = $this->member_model->get_member($me_no);
            $data['value'] = $this->member_model->get_caste();
            $data['relationship'] = $this->member_model->get_relationship();
            $data['design'] = $this->member_model->get_design();
            // print_r($data['member']); die();
            $data['content'] = 'member/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/member_details', 'refresh');
        }
    }
    //update member
    public function update_member()
    {
        $inps = $this->input->post();

        $data['image'] = $target_file = "./assets/img/" . time() . basename($_FILES["mem_image"]["name"]);
        if (move_uploaded_file($_FILES["mem_image"]["tmp_name"], $target_file)) {
            $inps['mem_image'] =  time() . basename($_FILES["mem_image"]["name"]);
        } else {
            $inps['mem_image'] = "";
        }

        $this->load->model('member_model');
        if ($inps['mem_dob'] != "") {
            $inps['mem_dob'] = date("Y-m-d", strtotime($inps['mem_dob']));
        }
        if ($inps['mem_doj'] != "") {
            $inps['mem_doj'] = date("Y-m-d", strtotime($inps['mem_doj']));
        }
        if ($inps['scty_doj'] != "") {
            $inps['scty_doj'] = date("Y-m-d", strtotime($inps['scty_doj']));
        }
        if ($inps['mem_dor'] != "") {
            $inps['mem_dor'] = date("Y-m-d", strtotime($inps['mem_dor']));
        }
        $res = $this->member_model->update($inps);

        $_SESSION['mem_add'] = "Member Updated Successfully";
        redirect('Member/member_details', 'refresh');
    }
    //view member
    public function view_member()
    {
        $me_no = $this->uri->segment(3);
        
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['divisions'] = $this->member_model->division();
            $data['sections'] = $this->member_model->section();
            $data['member'] = $this->member_model->get_member($me_no);
            // print_r($data);die();
            $data['member'] = (object) $data['member'];
            $data['content'] = 'member/view';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/member_details', 'refresh');
        }
    }
    //delete member
    public function close_member()
    {
        $me_no = $this->uri->segment(3);
        if ($me_no != "") {
            $cmonth = date('F');
            $cyear = date("Y");
            $cdate = date("Y-m-d");
            // print_r($cdate); die;
            $inps = array("me_no" => $me_no, "is_deleted" => "2",  "close_date" =>  $cdate, "close_month" =>  $cmonth, "close_year" => $cyear);
            $this->load->model('member_model');
            $res = $this->member_model->update($inps);
        }
        $_SESSION['mem_add'] = "Member Closed Successfully";
        redirect('Member/member_details', 'refresh');
    }
    //deactive
    public function deactive()
    {
        $me_no = $this->uri->segment(3);
        if ($me_no != "") {
            $inps = array("me_no" => $me_no, "is_deleted" => "1");
            $this->load->model('member_model');
            $res = $this->member_model->update($inps);
        }
        $_SESSION['mem_add'] = "Member Closed Successfully";
        redirect('Member/member_active', 'refresh');
    }
    //active
    public function active()
    {
        $me_no = $this->uri->segment(3);
        if ($me_no != "") {
            $inps = array("me_no" => $me_no, "is_deleted" => "0");
            $this->load->model('member_model');
            $res = $this->member_model->update($inps);
        }
        $_SESSION['mem_add'] = "Member Closed Successfully";
        redirect('Member/member_deactive', 'refresh');
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------Thrift Details------------------------------//
    //-----------------------------------------------------------------------------//
    public function thrift_details()
    {

        $this->load->model('member_model');
        $data['thrifts'] = $this->member_model->get_thrift_all();
        // print_r($data['thrifts']);die;
        $data['content'] = 'thrift/index';
        $this->load->view('layout/main', $data);
    }
    public function dueby_details()
    {
        $this->load->model('member_model');
        $data['members'] = $this->member_model->members();
        $data['banks'] = $this->member_model->get_bank();
        $members_temp = array();
        foreach ($data['members'] as $mem) {
            $members_temp[$mem['me_no']] = $mem;
        }
        $data['members_temp'] = $members_temp;
        $data['category_array'] = $this->member_model->get_category();
        $category_temp = array();
        foreach ($data['category_array'] as $cat) {
            $category_temp[$cat['id']] = $cat['category'];
        }
        $data['category'] = $category_temp;
        $data['sub_category_array'] = $this->member_model->get_sub_category();
        $sub_category_temp = array();
        foreach ($data['sub_category_array'] as $cat) {
            $sub_category_temp[$cat['id']] = $cat['sub_category'];
        }
        $data['sub_category'] = $sub_category_temp;
        $inps = $this->input->post();
        if (isset($inps['year']) || isset($inps['added_on'])) {
            $data['cash_book'] = $this->member_model->get_cash_book($inps);
        }
        $data['cheque_details'] = $this->member_model->get_cheque(date('Y-m-d'));
        $data['duebys'] = $this->member_model->get_due_all();
        // print_r($data['thrifts']);die;
        $data['content'] = 'dueby/index';
        $this->load->view('layout/main', $data);
    }
    public function dueby_particular($me_no)
    {
        $data['content'] = 'dueby/particular';
        $data['dueby'] = $this->member_model->get_particular_dueby($me_no);
        $data['demand'] = $this->member_model->get_pa_demand($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function particular($me_no)
    {
        $data['content'] = 'thrift/particular';
        $data['thrift'] = $this->member_model->get_particular($me_no);
        $data['demand'] = $this->member_model->get_pa_demand($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function edit_thrift()
    {
        $me_no = $this->uri->segment(3);
        //  echo $me_no; die();
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['thrift'] = $this->member_model->get_thrift($me_no);
            //print_r($data);die();
            $data['content'] = 'thrift/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/thrift_details', 'refresh');
        }
    }

    public function update_thrift()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update_thrift($inps);
        }
        $_SESSION['mem_add'] = "Thrift Updated Successfully";
        redirect('Member/thrift_details', 'refresh');
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------Share Details------------------------------//
    //-----------------------------------------------------------------------------//
    public function share_details()
    {

        $this->load->model('member_model');
        $data['shares'] = $this->member_model->get_share_all();
        $data['content'] = 'share/index';
        $this->load->view('layout/main', $data);
    }
    public function particular_share($me_no)
    {
        $data['content'] = 'share/particular';
        $data['share'] = $this->member_model->get_particular_share($me_no);
        $data['value'] = $this->member_model->get_pa_demand_share($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function edit_share()
    {
        $me_no = $this->uri->segment(3);
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['share'] = $this->member_model->get_share($me_no);
            $data['content'] = 'share/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/share_details', 'refresh');
        }
    }
    public function update_share()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update_share($inps);
        }
        $_SESSION['mem_add'] = "Share Updated Successfully";
        redirect('Member/share_details', 'refresh');
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------Others Details------------------------------//
    //-----------------------------------------------------------------------------//
    public function others_details()
    {
        $data['content'] = 'others/index';
        $this->load->model('member_model');
        $data['others'] = $this->member_model->get_others_all();
        // print_r($data['others']);die;
        $this->load->view('layout/main', $data);
    }
    public function particular_others($me_no)
    {
        $data['content'] = 'others/particular';
        $data['others'] = $this->member_model->get_particular_others($me_no);
        $data['demand'] = $this->member_model->get_pa_demand_others($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function edit_others()
    {
        $me_no = $this->uri->segment(3);
        //  echo $me_no; die();
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['others'] = $this->member_model->get_others($me_no);
            //print_r($data);die();
            $data['content'] = 'others/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/others_details', 'refresh');
        }
    }

    public function update_others()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update_others($inps);
        }
        $_SESSION['mem_add'] = "others Updated Successfully";
        redirect('Member/others_details', 'refresh');
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------DCP Details------------------------------//
    //-----------------------------------------------------------------------------//
    public function ledger_balance()
    {
        $data['content'] = 'dcp/index';
        $this->load->view('layout/main', $data);
    }
    public function dcp_search()
    {
        $inps = $this->input->post();
        if (isset($inps['type'])) {
            // print_r($inps);die;
            $value = $this->member_model->dcp_search($inps);
            $result = array();
            foreach ($value as $element) {
                $result[$element['me_no']][] = $element;
            }
            $data['value'] = $result;
            $data['type'] = $inps['type'];
            $data['year'] = $inps['year'];
            $data['next_year'] = $inps['year'] + 1;
        }

        $data['content'] = 'dcp/details';
        $this->load->view('layout/main', $data);
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------Loan Details------------------------------//
    //-----------------------------------------------------------------------------//
    public function loan_details()
    {

        $this->load->model('member_model');
        $data['loans'] = $this->member_model->get_loan_all();
        $data['content'] = 'loan/index';
        $this->load->view('layout/main', $data);
    }
    public function particular_loan($me_no)
    {
        $data['content'] = 'loan/particular';
        $data['member'] = $this->member_model->get_particular_loan($me_no);
        $data['loan'] = $this->member_model->get_pa_loan($me_no);
        $data['loans'] = $this->member_model->get_par_loan($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function loan_search()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $loan_amt = $this->member_model->loan_search($inps);
        if ($loan_amt->loan_os_amt > 0) {
            echo 1;
        } else {
            echo 0;
        }
    }
    public function edit_loan()
    {
        $me_no = $this->uri->segment(3);
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['loan'] = $this->member_model->get_loan($me_no);
            $data['members'] = $this->member_model->members();
            // print_r($data['loan']);die;
            $data['content'] = 'loan/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/loan_details', 'refresh');
        }
    }

    public function add_loan()
    {
        $data = array();
        $this->load->model('member_model');
        $data['loan'] = $this->member_model->total_loan_amount();
        $data['members'] = $this->member_model->members();
        $data['content'] = 'loan/add';
        $this->load->view('layout/main', $data);
    }

    public function close_loan()
    {
        $data = array();
        $this->load->model('member_model');
        $data['members'] = $this->member_model->members();
        $data['content'] = 'loan/close';
        $this->load->view('layout/main', $data);
    }
    public function update_loan()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update_loan($inps);
        }
        $_SESSION['mem_add'] = "Loan Updated Successfully";
        redirect('Member/loan_details', 'refresh');
    }

    public function get_loan_close_details()
    {
        $this->load->model('member_model');
        $members_data = array();
        $inps = $this->input->post();
        $this->load->model('demand_model');
        $demand_res = $this->demand_model->get_demand($inps);
        if (count($demand_res) > 0) {
            echo "Demand Already Generated";
            die();
        }
        $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);

        $member = (object) $members[0];
        if ($member->fst_int == 1) {
            $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $member->no_of_days) / 36500);
        } else {
            $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
        }
        $member->old_pri_int = round(($member->old_due_pri * 3) / 1200);
        // $member->f_pri_int = round(($member->f_old_pri * 3) / 1200); + $member->f_int_amt + $member->f_pri+ $member->f_old_pri+ $member->f_old_int+ $member->f_pri_int
        $member->total = $member->loan_int_amt + $member->loan_principle_amt  + $member->old_due_pri  + $member->old_due_int  + $member->insurance + $member->old_pri_int  +  $member->old_ins +  $member->old_others + $member->old_p_int;
        $member->total = round($member->total);
        // $members_data[$key] = (array) $member;
        // print_r($key);die;
        $ins_t = $member->insurance + $member->old_ins;
        $mt_int = $member->loan_int_amt + $member->old_due_int;
        // $f_int = $member->f_int_amt + $member->f_old_int;
        $mt_pri = $member->loan_os_amt + $member->old_due_pri;
        // $f_pri = $member->f_os + $member->f_old_pri;+ $f_int + $f_pri + $member->f_pri_int
        // <input type="hidden" id="f_int_amt" name="f_int_amt" value="' . $member->f_int_amt . '" >
        //   <input type="hidden" id="f_old_int" name="f_old_int" value="' . $member->f_old_int . '" >
        //    <input type="hidden" id="of_os" name="of_os" value="' . $member->f_os . '" >
        //   <input type="hidden" id="f_old_pri" name="f_old_pri" value="' . $member->f_old_pri . '" >
        //   <input type="hidden" id="f_pri_int" name="f_pri_int" value="' . $member->f_pri_int . '" >
        //      <div class="row mt-3">
        //      <div class="col-sm-6"><b>F Int.</b></div>
        //      <div class="col-sm-6">
        //          <input type="number" onchange="get_total();" class="form-control" name="f_int" id="f_int" value="' . $f_int . '">
        //      </div>
        //  </div>
        //     <div class="row mt-3">
        //     <div class="col-sm-6"><b>F Pri.</b></div>
        //     <div class="col-sm-6">
        //         <input type="number" onchange="get_total();" class="form-control" name="f_pri" id="f_pri" value="' . $f_pri . '">
        //     </div>
        // </div>
        //     <div class="row mt-3">
        //     <div class="col-sm-6"><b>FP Int.</b></div>
        //     <div class="col-sm-6">
        //         <input type="number" onchange="get_total();" class="form-control" name="fp_int" id="fp_int" value="' . $member->f_pri_int . '">
        //     </div>
        //    </div>
        $total = $ins_t + $mt_int + $mt_pri  + $member->old_others + $member->old_pri_int;
        $html = '
              <input type="hidden" id="old_ins" name="old_ins" value="' . $member->old_ins . '" >
              <input type="hidden" id="insurance" name="insurance" value="' . $member->insurance . '" >
              <input type="hidden" id="loan_int_amt" name="loan_int_amt" value="' . $member->loan_int_amt . '" >
              <input type="hidden" id="old_due_int" name="old_due_int" value="' . $member->old_due_int . '" >
              <input type="hidden" id="loan_os_amt" name="loan_os_amt" value="' . $member->loan_os_amt . '" >
              <input type="hidden" id="old_due_pri" name="old_due_pri" value="' . $member->old_due_pri . '" >
              <input type="hidden" id="old_pri_int" name="old_pri_int" value="' . $member->old_pri_int . '" >
           
            <div class="row mt-3">
                <div class="col-sm-6"><b>Others</b></div>
                <div class="col-sm-6">
                    <input type="number" onchange="get_total();" class="form-control" name="d_others" id="d_others" value="' . $member->old_others . '">
                </div>
            </div>
           <div class="row mt-3">
                <div class="col-sm-6"><b>Insurance</b></div>
                <div class="col-sm-6">
                    <input type="number" onchange="get_total();" class="form-control" name="d_ins" id="d_ins" value="' . $ins_t . '">
                </div>
            </div>
            <div class="row mt-3">
                <div class="col-sm-6"><b>P Int.</b></div>
                <div class="col-sm-6">
                    <input type="number" onchange="get_total();" class="form-control" name="p_int" id="p_int" value="' . $member->old_pri_int . '">
                </div>
            </div>
            <div class="row mt-3">
                <div class="col-sm-6"><b>Int.</b></div>
                <div class="col-sm-6">
                    <input type="number" onchange="get_total();" class="form-control" name="mt_int" id="mt_int" value="' . $mt_int . '">
                </div>
            </div>
             <div class="row mt-3">
                <div class="col-sm-6"><b>MT Pri.</b></div>
                <div class="col-sm-6">
                    <input type="number" onchange="get_total();" class="form-control" name="mt_pri" id="mt_pri" value="' . $mt_pri . '">
                </div>
            </div>
             <div class="row mt-3">
                <div class="col-sm-6"><b>Total</b></div>
                <div class="col-sm-6">
                    <input type="number" class="form-control" id="total_amt" readonly value="' . $total . '">
                </div>
            </div>
            ';

        echo $html;
    }
    public function loan_close()
    {
        $inps = $this->input->post();

        $this->load->model('member_model');
        $payment = array();
        $calc = array();
        $ins_loan = array();
        if ($inps['me_no'] != "") {
            $data['loan'] = $this->member_model->get_loan($inps['me_no']);
            //   if($inps['mt_pri']>0)
            //   {
            //       $ins_loan['loan_os_amt']=$data['loan']['loan_os_amt']-$inps['mt_pri'];
            //       if
            //   }

            $payment['others'] = $inps['d_others'];

            if ($inps['d_ins'] > 0) {
                $inps['d_ins'] = $inps['d_ins'] - $inps['old_ins'];
                if ($inps['d_ins'] >= 0) {
                    $payment['old_ins'] = $inps['old_ins'];
                    $calc['_old_ins'] = 0;
                } else {

                    $payment['old_ins'] = $inps['old_ins'] + $inps['d_ins'];
                    $calc['_old_ins'] = $inps['old_ins'] - $payment['old_ins'];
                }
            } else {
                $payment['old_ins'] = 0;
                $calc['_old_ins'] = $inps['old_ins'];
            }


            if ($inps['d_ins'] > 0) {
                $inps['d_ins'] = $inps['d_ins'] - $inps['insurance'];
                if ($inps['d_ins'] >= 0) {
                    $payment['insurance'] = $inps['insurance'];
                    $calc['_insurance'] = 0;
                } else {

                    $payment['insurance'] = $inps['insurance'] + $inps['d_ins'];
                    $calc['_insurance'] = $inps['insurance'] - $payment['insurance'];
                }
            } else {
                $payment['insurance'] = 0;
                $calc['_insurance'] = $inps['insurance'];
            }

            // if ($inps['fp_int'] > 0) {
            //     $inps['fp_int'] = $inps['fp_int'] - $inps['f_pri_int'];
            //     if ($inps['fp_int'] >= 0) {
            //         $payment['f_p_int'] = $inps['f_pri_int'];
            //         $calc['_f_p_int'] = 0;
            //     } else {
            //         $payment['f_p_int'] = $inps['f_pri_int'] + $inps['fp_int'];
            //         $calc['_f_p_int'] = $inps['f_pri_int'] - $payment['f_p_int'];
            //     }
            // } else {
            //     $payment['f_p_int'] = 0;
            //     $calc['_f_p_int'] = $inps['f_pri_int'];
            // }

            if ($inps['p_int'] > 0) {
                $inps['p_int'] = $inps['p_int'] - $inps['old_pri_int'];
                if ($inps['p_int'] >= 0) {
                    $payment['p_int'] = $inps['old_pri_int'];
                    $calc['_p_int'] = 0;
                } else {
                    $payment['p_int'] = $inps['old_pri_int'] + $inps['p_int'];
                    $calc['_p_int'] = $inps['old_pri_int'] - $payment['p_int'];
                }
            } else {
                $payment['p_int'] = 0;
                $calc['_p_int'] = $inps['old_pri_int'];
            }

            // if ($inps['f_int'] > 0) {
            //     $inps['f_int'] = $inps['f_int'] - $inps['f_old_int'];
            //     if ($inps['f_int'] >= 0) {
            //         $payment['f_old_int'] = $inps['f_old_int'];
            //         $calc['_f_old_int'] = 0;
            //     } else {
            //         $payment['f_old_int'] = $inps['f_old_int'] + $inps['f_int'];
            //         $calc['_f_old_int'] = $inps['f_old_int'] - $payment['f_old_int'];
            //     }
            // } else {
            //     $payment['f_old_int'] = 0;
            //     $calc['_f_old_int'] = $inps['f_old_int'];
            // }


            // if ($inps['f_int'] > 0) {
            //     $inps['f_int'] = $inps['f_int'] - $inps['f_int_amt'];
            //     if ($inps['f_int'] >= 0) {
            //         $payment['f_int'] = $inps['f_int_amt'];
            //         $calc['_f_int'] = 0;
            //     } else {
            //         $payment['f_int'] = $inps['f_int_amt'] + $inps['f_int'];
            //         $calc['_f_int'] = $inps['f_int_amt'] - $payment['f_int'];
            //     }
            // } else {
            //     $payment['f_int'] = 0;
            //     $calc['_f_int'] = $inps['f_int_amt'];
            // }

            if ($inps['mt_int'] > 0) {
                $inps['mt_int'] = $inps['mt_int'] - $inps['old_due_int'];
                if ($inps['mt_int'] >= 0) {
                    $payment['last_int'] = $inps['old_due_int'];
                    $calc['_last_int'] = 0;
                } else {
                    $payment['last_int'] = $inps['old_due_int'] + $inps['mt_int'];
                    $calc['_last_int'] = $inps['old_due_int'] - $payment['last_int'];
                }
            } else {
                $payment['last_int'] = 0;
                $calc['_last_int'] = $inps['old_due_int'];
            }


            if ($inps['mt_int'] > 0) {
                $inps['mt_int'] = $inps['mt_int'] - $inps['loan_int_amt'];
                if ($inps['mt_int'] >= 0) {
                    $payment['loan_int_amt'] = $inps['loan_int_amt'];
                    $calc['_loan_int_amt'] = 0;
                } else {
                    $payment['loan_int_amt'] = $inps['loan_int_amt'] + $inps['mt_int'];
                    $calc['_loan_int_amt'] = $inps['loan_int_amt'] - $payment['loan_int_amt'];
                }
            } else {
                $payment['loan_int_amt'] = 0;
                $calc['_loan_int_amt'] = $inps['loan_int_amt'];
            }


            // if ($inps['f_pri'] > 0) {
            //     $inps['f_pri'] = $inps['mt_pri'] - $inps['f_old_pri'];
            //     if ($inps['f_pri'] >= 0) {
            //         $payment['f_old_pri'] = $inps['f_old_pri'];
            //         $calc['_f_old_pri'] = 0;
            //     } else {
            //         $payment['f_old_pri'] = $inps['f_old_pri'] + $inps['f_pri'];
            //         $calc['_f_old_pri'] = $inps['f_old_pri'] - $payment['f_old_pri'];
            //     }
            // } else {
            //     $payment['f_old_pri'] = 0;
            //     $calc['_f_old_pri'] = $inps['f_old_pri'];
            // }

            // if ($inps['f_pri'] > 0) {
            //     $inps['f_pri'] = $inps['f_pri'] - $inps['of_os'];
            //     if ($inps['f_pri'] >= 0) {
            //         $payment['f_pri'] = $inps['of_os'];
            //         $calc['_f_pri'] = 0;
            //     } else {
            //         $payment['f_pri'] = $inps['of_os'] + $inps['f_pri'];
            //         $calc['_f_pri'] = $inps['of_os'] - $payment['f_pri'];
            //     }
            // } else {
            //     $payment['f_pri'] = 0;
            //     $calc['_f_pri'] = $inps['of_os'];
            // }



            if ($inps['mt_pri'] > 0) {
                $inps['mt_pri'] = $inps['mt_pri'] - $inps['old_due_pri'];
                if ($inps['mt_pri'] >= 0) {
                    $payment['last_principal'] = $inps['old_due_pri'];
                    $calc['_last_principal'] = 0;
                } else {
                    $payment['last_principal'] = $inps['old_due_pri'] + $inps['mt_pri'];
                    $calc['_last_principal'] = $inps['old_due_pri'] - $payment['last_principal'];
                }
            } else {
                $payment['last_principal'] = 0;
                $calc['_last_principal'] = $inps['old_due_pri'];
            }

            if ($inps['mt_pri'] > 0) {
                $inps['mt_pri'] = $inps['mt_pri'] - $inps['loan_os_amt'];
                if ($inps['mt_pri'] >= 0) {
                    $payment['loan_pri_amt'] = $inps['loan_os_amt'];
                    $calc['_loan_pri_amt'] = 0;
                } else {
                    $payment['loan_pri_amt'] = $inps['loan_os_amt'] + $inps['mt_pri'];
                    $calc['_loan_pri_amt'] = $inps['loan_os_amt'] - $payment['loan_pri_amt'];
                }
            } else {
                $payment['loan_pri_amt'] = 0;
                $calc['_loan_pri_amt'] = $inps['loan_os_amt'];
            }
            $payment['tot_pay'] = array_sum($payment);
            $due['old_due_int'] = $calc['_loan_int_amt'] + $calc['_last_int'];
            $due['old_p_int'] = $calc['_p_int'];
            $due['old_due_pri'] = $calc['_last_principal'];
            $due['loan_os_amt'] = $calc['_loan_pri_amt'];
            // $due['f_old_pri'] = $calc['_f_pri'];
            // $due['f_os'] = $calc['_f_old_pri'];
            // $due['f_old_int'] = $calc['_f_old_int'] + $calc['_f_int'] + $calc['_f_p_int'];

            $due['old_ins'] = $calc['_old_ins'] + $calc['_insurance'];

            // $payment['insurance'] = $payment['insurance'] + $payment['old_ins'];
            $payment['me_no'] = $inps['me_no'];
            $payment['share_amt'] = 0;
            $payment['payment_month'] = date("F");
            $payment['payment_year'] = date("Y");
            $payment['paid_on'] = $inps['updated_on'];
            $payment['paid_by'] = $_SESSION['name'];


            if ($payment['tot_pay'] > 0) {
                $rec_res = $this->db->query("select max(rec_no) as rec_no from payment")->result_array();
                $rec_no = $rec_res[0]['rec_no'] + 1;
            } else {
                $rec_no = 0;
            }
            $payment['rec_no'] = $rec_no;
            //   print "<pre>";print_r($payment);die();, "f_os" => $due['f_os']'f_old_int' => $due['f_old_int'], 'f_old_pri' => $due['f_old_pri'],
            $this->db->insert("payment", $payment);

            $update_dues = array('old_due_int' => $due['old_due_int'], 'old_due_pri' => $due['old_due_pri'],  'old_ins' => $due['old_ins'], 'old_p_int' => $due['old_p_int']);

            $this->db->where("me_no", $inps['me_no'])->update("old_dues", $update_dues);
            $ins_loan = array("loan_os_amt" => $due['loan_os_amt']);
            if ($due['loan_os_amt'] <= 0) {
                $ins_loan['loan_amount'] = 0;
                $ins_loan['loan_amount'] = 0;
                $ins_loan['loan_no'] = "";
                $ins_loan['loan_principle_amt'] = 0;
                $ins_loan['loan_os_amt'] = 0;
            }
            // if ($due['f_os'] <= 0) {
            //     $ins_loan['f_pri'] = 0;
            //     $ins_loan['f_os'] = 0;
            // }

            $this->db->where("me_no", $inps['me_no']);
            $this->db->update("loan", $ins_loan);
        }
        //  print "<pre>";print_r($inps);die();
        $_SESSION['mem_add'] = "Loan Paid Successfully";
        redirect('Member/loan_details', 'refresh');
    }
    public function insert_loan()
    {
        $inps = $this->input->post();
        // print_r($inps);die;
        $this->load->model('member_model');
        if ($inps['me_no'] != "") {
            $data['loan'] = $this->member_model->get_loan($inps['me_no']);
            $this->load->model('demand_model');

            $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);

            $member = (object) $members[0];
            if ($member->fst_int == 1) {
                $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $member->no_of_days) / 36500);
            } else {
                $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
            }
            if ($member->f_fst_int == 1) {
                $member->f_int_amt = round(($member->f_int_rate * $member->f_os * $member->f_no_of_days) / 36500);
            } else {
                $member->f_int_amt = round(($member->f_int_rate * $member->f_os) / 1200);
            }
            $member->old_pri_int = round(($member->old_due_pri * 3) / 1200);
            $member->f_pri_int = round(($member->f_old_pri * 3) / 1200);
            $member->total = $member->loan_int_amt + $member->f_int_amt + $member->loan_principle_amt + $member->f_pri + $member->old_due_pri + $member->f_old_pri + $member->old_due_int + $member->f_old_int + $member->insurance + $member->old_pri_int + $member->f_pri_int +  $member->old_ins +  $member->old_others + $member->old_p_int;
            $member->total = round($member->total);
            if ($member->total > 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Please close old loan!<div>';
                redirect('index.php/member/loan_details', 'refresh');
                // echo '<script>alert("Please close old loan!");window.location.href = "'.base_url().'index.php/member/loan_details";</script>';
                //die();
            }

            $tot_pay = $inps['ent_fees'] + $inps['d_share'] + $inps['d_ins'] + $inps['d_others'];
            if ($tot_pay > 0) {
                $rec_res = $this->db->query("select max(rec_no) as rec_no from payment")->result_array();
                $rec_no = $rec_res[0]['rec_no'] + 1;
            } else {
                $rec_no = 0;
            }
            $ins_data_rec = array("me_no" => $inps['me_no'], "ent_fees" => $inps['ent_fees'], "share_amt" => $inps['d_share'], "insurance" => $inps['d_ins'], "others" => $inps['d_others'], "tot_pay" => $tot_pay, "rec_no" => $rec_no, "cash_type" => $inps['cash_type'], "paid_on" => date("Y-m-d"));
            $this->db->insert("payment", $ins_data_rec);
            $data = array(
                "mem_no" => $inps['me_no'],
                "amount" => $inps['loan_os_amt'],
                "category" => 16,
                "payment_mode" => "Cheque",
                "cash_type" => $inps['cash_type'],
                "paid_month" => date("F", strtotime($inps['loan_date'])),
                "paid_year" => date("Y", strtotime($inps['loan_date'])),
                "paid_date" => date("Y-m-d", strtotime($inps['loan_date'])),
                "added_datetime" => date("Y-m-d H:i:s"),
                "status" => 0,
                "type" => 1
            );
            unset($inps['ent_fees']);
            unset($inps['d_share']);
            unset($inps['d_ins']);
            unset($inps['d_others']);
            unset($inps['cash_type']);
            $inps['loan_date'] = date("Y-m-d", strtotime($inps['loan_date']));
            $inps['f_loan_date'] = date("Y-m-d", strtotime($inps['f_loan_date']));
            $res = $this->member_model->update_loan($inps);
            $this->member_model->insert_cash_book($data);
        }
        $_SESSION['mem_add'] = "Loan Added Successfully";
        redirect('member/loan_details', 'refresh');
    }
    //-----------------------------------------------------------------------------//
    //---------------------------------Insurence Details------------------------------//
    //-----------------------------------------------------------------------------//


    //insurance details

    public function insurance_details()
    {
        $this->load->model('member_model');
        $data['value'] = $this->member_model->get_insurance_all();
        $data['content'] = 'insurence/index';
        $this->load->view('layout/main', $data);
    }
    public function particular_insurence($me_no)
    {
        $data['content'] = 'insurence/particular';
        $data['insu'] = $this->member_model->get_particular_insu($me_no);
        $data['value'] = $this->member_model->get_pa_insu($me_no);
        // print_r($data['thrift']);die;
        $this->load->view('layout/main', $data);
    }
    public function edit_insurance()
    {
        $me_no = $this->uri->segment(3);
        //  echo $me_no; die();
        if ($me_no != "") {
            $this->load->model('member_model');
            $data['insurance'] = $this->member_model->get_insurance($me_no);
            //print_r($data);die();
            $data['content'] = 'insurence/edit';
            $this->load->view('layout/main', $data);
        } else {
            redirect('Member/insurance_details', 'refresh');
        }
    }

    public function update_insurance()
    {
        $inps = $this->input->post();
        if ($inps['me_no'] != "") {
            $this->load->model('member_model');
            $res = $this->member_model->update_insurance($inps);
        }
        $_SESSION['mem_add'] = "insurance Updated Successfully";
        redirect('Member/insurance_details', 'refresh');
    }


    //---------------------------------------------------//
    //-------------------Setting-------------------------//
    //---------------------------------------------------//
    //caste
    function new_caste()
    {
        $data['content'] = 'caste/caste_add';
        $this->load->view('layout/main', $data);
    }
    function caste_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $caste_id = $this->member_model->add_caste($requestdata);
        if ($caste_id == "ex") {

            $_SESSION['mem_add'] = '<div class="alert alert-danger">Caste already exists</div>';
            redirect("Member/caste_details");
        }
        redirect('Member/caste_details');
    }
    function bank_details()
    {
        $this->load->model('member_model');
        $data['bank'] = $this->member_model->get_bank();
        $data['content'] = 'bank/bank_details';
        $this->load->view('layout/main', $data);
    }
    function delete_bank()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_bank($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/bank_details");
    }
    function edit_bank()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_bank($id);
        // print_r($request);die();
        $data['content'] = 'bank/edit_bank';
        $this->load->view('layout/main', $data);
    }

    function update_bank()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_bank($requestData);
        redirect("Member/bank_details");
    }

    function new_bank()
    {
        $data['content'] = 'bank/bank_add';
        $this->load->view('layout/main', $data);
    }

    function bank_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $bank_id = $this->member_model->add_bank($requestdata);
        if ($bank_id == "ex") {

            $_SESSION['mem_add'] = '<div class="alert alert-danger">Bank already exists</div>';
            redirect("Member/bank_details");
        }
        redirect('Member/bank_details');
    }

    function caste_details()
    {
        $this->load->model('member_model');
        $data['caste'] = $this->member_model->get_caste();
        $data['content'] = 'caste/caste_details';
        $this->load->view('layout/main', $data);
    }
    function delete_caste()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_caste($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/caste_details");
    }
    function edit_caste()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_caste($id);
        // print_r($request);die();
        $data['content'] = 'caste/edit_caste';
        $this->load->view('layout/main', $data);
    }

    function update_caste()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_caste($requestData);
        redirect("Member/caste_details");
    }

    // division 
    function new_division()
    {

        $data['content'] = 'division/division_add';
        $this->load->view('layout/main', $data);
    }
    public function division_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $res = $this->member_model->add_division($requestdata);
        if ($res == 1) {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Division already exists</div>';
        } else {
            $_SESSION['mem_add'] = "Division added Successfully";
        }
        redirect('Member/division_details', 'refresh');
    }

    public function division_details()
    {
        $this->load->model('member_model');
        $data['division'] = $this->member_model->get_division();
        $data['content'] = 'division/division_details';
        $this->load->view('layout/main', $data);
    }

    public function delete_division()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $res = $this->db->select("*")->where("id", $id)->get("division")->result_array();
        $members = $this->db->select("*")->where("division", $res[0]['division'])->where("is_deleted", 0)->get("member")->result_array();
        if (count($members) > 0) {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Division exists in member details</div>';
            redirect('Member/division_details', 'refresh');
        }
        $response = $this->member_model->delete_division($id);

        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/division_details");
    }

    public function edit_division()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_division($id);
        $data['content'] = 'division/edit_division';
        $this->load->view('layout/main', $data);
    }

    public function update_division()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_division($requestData);
        redirect("Member/division_details");
    }
    // section 
    function new_section()
    {
        $this->load->model('member_model');
        $data['division'] = $this->member_model->get_division();
        $data['content'] = 'section/section_add';
        $this->load->view('layout/main', $data);
    }
    function section_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $section_id = $this->member_model->add_section($requestdata);
        if ($section_id == "ex") {

            $_SESSION['mem_add'] = '<div class="alert alert-danger">Section already exists</div>';
            redirect("Member/section_details");
        }
        $this->db->insert("mapdivisionsection", array("division_id" => $requestdata['division'], "section_id" => $section_id));
        $_SESSION['mem_add'] = "Section added successfully";
        redirect('Member/section_details');
    }
    function section_details()
    {
        $this->load->model('member_model');
        $data['data'] = $this->member_model->get_section();
        $data['division'] = $this->member_model->get_division();
        $data['content'] = 'section/section_details';
        $this->load->view('layout/main', $data);
    }

    function sub_category_details()
    {
        $this->load->model('member_model');
        $data['data'] = $this->member_model->get_sub_category();
        $data['category'] = $this->member_model->get_category();
        $category_temp = array();
        foreach ($data['category'] as $cat) {
            $category_temp[$cat['id']] = $cat['category'];
        }
        $data['category'] = $category_temp;
        $data['content'] = 'sub_category/sub_category_details';
        $this->load->view('layout/main', $data);
    }
    function new_sub_category()
    {
        $this->load->model('member_model');
        $data['category'] = $this->member_model->get_category();
        $data['content'] = 'sub_category/sub_category_add';
        $this->load->view('layout/main', $data);
    }
    function sub_category_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $res = $this->member_model->add_sub_category($requestdata);
        if ($res == "1") {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Sub Category already exists</div>';
            redirect("Member/sub_category_details");
        }
        $_SESSION['mem_add'] = "Sub Category added successfully";
        redirect('Member/sub_category_details');
    }
    public function remove_sub_category()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->update_sub_category(array("id" => $id, "status" => 1));
        if ($response) {
            $this->session->set_flashdata('msg', 'Sub Category Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/sub_category_details");
    }

    public function edit_sub_category()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_sub_category($id);
        $data['content'] = 'sub_category/sub_category_update';
        $this->load->view('layout/main', $data);
    }

    public function update_sub_category()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_sub_category($requestData);
        redirect("Member/sub_category_details");
    }
    public function section_search()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $sections = $this->member_model->getsection($requestdata);
        $html = '';
        $i = 1;
        foreach ($sections as $value) {
            $html .= '<tr><td>' . $i . '</td>
                                    <td>' . $value['section'] . '</td>
                                    <td>' . $value['section_email'] . '</td>
                                    <td>
                                        <a href="delete_section?id=' . $value['id'] . '" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span><span class="sr-only"></span></a>
                                   
                                        <a href="edit_section?id=' . $value['id'] . '" class="btn btn-primary btn-xs"><span class="fa fa-edit"></span><span class="sr-only"></span></a>
                                    </td>
                                </tr>';
            $i++;
        }
        echo $html;
    }

    public function check_section_exit()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $member = $this->member_model->get_exist_section($inps['section']);
        if (isset($member['section']) && $member['section'] != "") {
            echo "Section already exist";
        } else {
            echo 1;
        }
    }
    function delete_section()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_section($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/section_details");
    }
    function edit_section()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_section($id);
        $data['content'] = 'section/section_update';
        $this->load->view('layout/main', $data);
    }
    function update_section()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_section($requestData);
        redirect("Member/section_details");
    }
    // designation 
    function new_designation()
    {
        $data['content'] = 'designation/designation_add';
        $this->load->view('layout/main', $data);
    }

    function designation_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $desig_id = $this->member_model->add_designation($requestdata);
        if ($desig_id == "ex") {

            $_SESSION['mem_add'] = '<div class="alert alert-danger">Designation already exists</div>';
            redirect("Member/designation_details");
        }
        redirect('Member/designation_details');
    }

    function designation_details()
    {
        $this->load->model('member_model');
        $data['designation'] = $this->member_model->get_design();
        $data['content'] = 'designation/designation_details';
        $this->load->view('layout/main', $data);
    }
    function delete_designation()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_designation($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/designation_details");
    }
    function edit_designation()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_designation($id);
        $data['content'] = 'designation/edit_designation';
        $this->load->view('layout/main', $data);
    }
    function update_designation()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_designation($requestData);
        redirect("Member/designation_details");
    }
    function cheque_details()
    {
        $this->load->model('member_model');
        $data['cheque_details'] = $this->member_model->get_cheque_details();
        $data['content'] = 'cheque/cheque_details';
        $this->load->view('layout/main', $data);
    }
    function new_cheque()
    {
        $data['content'] = 'cheque/cheque_add';
        $this->load->view('layout/main', $data);
    }
    function cheque_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $insert_id = $this->member_model->add_cheque($requestdata);
        if ($insert_id == "ex") {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Cheque already exists</div>';
            redirect("Member/cheque_details");
        }
        redirect('Member/cheque_details');
    }
    function update_cheque()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_cheque($requestData);
        redirect('Member/cheque_details');
    }
    function delete_cheque()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_cheque($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Cheque Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect('Member/cheque_details');
    }
    function edit_cheque()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_cheque($id);
        $data['content'] = 'cheque/edit_cheque';
        $this->load->view('layout/main', $data);
    }
    // relationship 
    function new_relationship()
    {
        $data['content'] = 'relationship/relationship_add';
        $this->load->view('layout/main', $data);
    }
    function relationship_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $relationship_id = $this->member_model->add_relationship($requestdata);
        if ($relationship_id == "ex") {

            $_SESSION['mem_add'] = '<div class="alert alert-danger">Relationship already exists</div>';
            redirect("Member/relationship_details");
        }
        redirect('Member/relationship_details');
    }

    function relationship_details()
    {
        $this->load->model('member_model');
        $data['relationship'] = $this->member_model->get_relationship();
        $data['content'] = 'relationship/relationship_details';
        $this->load->view('layout/main', $data);
    }
    function delete_relationship()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $response = $this->member_model->delete_relationship($id);
        if ($response) {
            $this->session->set_flashdata('msg', 'Division Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/relationship_details");
    }
    function edit_relationship()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_relationship($id);
        $data['content'] = 'relationship/edit_relationship';
        $this->load->view('layout/main', $data);
    }

    function update_relationship()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_relationship($requestData);
        redirect("Member/relationship_details");
    }

    public function get_loan_os()
    {
        $inps = $this->input->post();
        $res = $this->db->select("loan_os_amt")->where("me_no", $inps['me_no'])->get("loan")->result();
        $this->load->model('demand_model');
        $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);
        $members_data = array();
        foreach ($members as $key => $member) {
            $member = (object) $member;
            $year = $inps['year'];
            $isLeapYear = (bool) date('L', strtotime("$year-01-01"));
           // $ynodays = $isLeapYear ? 36600 : 36500;
           $ynodays = 36500;
            if ($member->fst_int == 1) {
                $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $member->no_of_days) / $ynodays);
            } else {
                $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $inps['month_days']) / $ynodays);
                // $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
            }
            if ($member->f_fst_int == 1) {
                $member->f_int_amt = round(($member->f_int_rate * $member->f_os * $member->no_of_days) / $ynodays);
            } else {
                $member->f_int_amt = round(($member->f_int_rate * $member->f_os * 30) / $ynodays);
                // $member->f_int_amt = round(($member->f_int_rate * $member->f_os) / 1200);
            }
            $member->old_pri_int = round(($member->old_due_pri * 3 * $inps['month_days']) / $ynodays);
            $member->f_pri_int = round(($member->f_old_pri * 3) / $ynodays);
            $member->total = $member->loan_int_amt + $member->loan_principle_amt +  $member->old_due_pri +  $member->old_due_int + $member->insurance + $member->thrift_amt + $member->old_pri_int  +  $member->old_ins +  $member->old_others + $member->old_p_int + $member->f_pri + $member->f_int_amt + $member->f_old_pri + $member->f_old_int + $member->f_pri_int + $member->f_old_p_int;
            $member->total = round($member->total);
            $members_data[$key] = (array) $member;
        }
        $data['members'] = $members_data;
        echo json_encode($res[0]);
    }

    public function get_loan_percentage()
    {
        $inps = $this->input->post();
        $res = $this->db->select("loan_int_rate")->where_in("me_no", $inps['me_no'])->get("loan")->result();
        echo json_encode($res[0]);
    }


    public function get_partial_payment_details()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['membersf'] = $this->member_model->members();
        $data['banks'] = $this->member_model->get_bank();
        $members_data = array();
        $inps = $this->input->post();
        // print_r($inps);die;
        if (isset($inps['me_no']) && !isset($inps['postvalue'])) {
            $this->load->model('demand_model');
            $demand_res = $this->demand_model->get_demand($inps);
            if (count($demand_res) > 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Already Generated</div>';
                redirect('Demand/demand_generate', 'refresh');
            }
            $lastd = $this->demand_model->get_last_demand($inps);

            $lastr = $this->demand_model->get_last_recover($inps);
            $dm_num = date("m", strtotime($lastd['d_month']));
            $ddate = $lastd['d_year'] . "-" . $dm_num . "-01";
            $rm_num = date("m", strtotime($lastr['payment_month']));
            $rdate = $lastr['payment_year'] . "-" . $rm_num . "-01";
            if ($rdate < $ddate) {
                $last_demand = $lastd;
            } else {
                $last_demand = array();
            }
            //print_r($last_demand);die();
            $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);
            $sno = 1;
            foreach ($members as $key => $member) {
                $member = (object) $member;
                $year = $inps['year'];
                $isLeapYear = (bool) date('L', strtotime("$year-01-01"));
                // $ynodays = $isLeapYear ? 36600 : 36500;
                $ynodays = 36500;
                if ($member->fst_int == 1) {
                    $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $member->no_of_days) / $ynodays);
                } else {
                    $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $inps['month_days']) / $ynodays);
                    // $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
                }
                if ($member->f_fst_int == 1) {
                    $member->f_int_amt = round(($member->f_int_rate * $member->f_os * $member->no_of_days) / $ynodays);
                } else {
                    $member->f_int_amt = round(($member->f_int_rate * $member->f_os * 30) / $ynodays);
                    // $member->f_int_amt = round(($member->f_int_rate * $member->f_os) / 1200);
                }
                $member->old_pri_int = round(($member->old_due_pri * 3 * $inps['month_days']) / $ynodays);
                $member->f_pri_int = round(($member->f_old_pri * 3) / $ynodays);
                $member->total = $member->loan_int_amt + $member->loan_principle_amt +  $member->old_due_pri +  $member->old_due_int + $member->insurance + $member->thrift_amt + $member->old_pri_int  +  $member->old_ins +  $member->old_others + $member->old_p_int + $member->f_pri + $member->f_int_amt + $member->f_old_pri + $member->f_old_int + $member->f_pri_int + $member->f_old_p_int;
                $member->total = round($member->total);
                $members_data[$key] = (array) $member;
                $tot_p_int = $member->p_int + $member->old_p_int;
                $tot_f_p_int = $member->f_pri_int + $member->f_old_p_int;
                $loan_os_amt = $member->loan_os_amt - $member->old_due_pri;
                $f_loan_os_amt = $member->f_os - $member->f_old_pri;
                $othersamt = $last_demand['d_others'] + $member->old_others;
                $total_amount = $tot_p_int + $member->old_ins + $member->old_due_int + $member->old_thrift + $member->loan_os_amt + $member->f_os + $member->f_old_int + $member->f_pri_int + $member->f_old_p_int + $othersamt;
                $html = ' 
            <input type="hidden"  name="postvalue[' . $member->me_no . '][me_no]" value="' . $member->me_no . '" id="me_no' . $sno . '" data-id="' . $member->me_no . '" class="form-control">
            <div class="row mt-3">
                <div class="col-sm-4"><b>Paid Amount</b></div>
                <div class="col-sm-4">
                    <input type="number" value="0"  name="postvalue[' . $member->me_no . '][amt_recover]" id="recover_amt" data-id="' . $sno . '"  class="recover_amt form-control">
                </div>
                <div class="col-sm-4">

                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Total Amount</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="total_amount" id="total_amount"  value="' . $total_amount . '"  class=" form-control">
                <input type="hidden" name="dtotal_amount" id="dtotal_amount"  value="' . $total_amount . '"  class=" form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Others</b></div>
                <div class="col-sm-4">
                <input type="hidden" name="old_others_val" id="old_others_val" value="' . $othersamt . '">
                <input type="number" name="postvalue[' . $member->me_no . '][d_others]" id="others_amt" onchange=get_others_amt(); value="' . $othersamt . '" data-id="' . $sno . '" class="others_button form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>OD Insurance</b></div>
                <div class="col-sm-4">
                    <input type="text" readonly name="postvalue[' . $member->me_no . '][old_ins]" id="old_ins_' . $sno . '" value="' . $member->old_ins . '" data-id="' . $sno . '" class=" form-control">
                </div>
                <div class="col-sm-4">
    
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>OD Interest</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="postvalue[' . $member->me_no . '][d_last_int]" class="form-control" value="' . $member->old_due_int . '">
                </div>
                <div class="col-sm-4">
                  
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>OD Principal</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="postvalue[' . $member->me_no . '][d_last_principal]" value="' . $member->old_due_pri . '" id="d_last_principal' . $sno . '" data-id="' . $member->old_due_pri . '" class="form-control">
                </div>
                <div class="col-sm-4">
                   
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Peneal Interest</b></div>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="p_int" value="' . $tot_p_int . '" readonly>
                    <input type="hidden" name="postvalue[' . $member->me_no . '][p_int]" value="' . $member->p_int . '">
                    <input type="hidden" name="postvalue[' . $member->me_no . '][old_p_int]" value="' . $member->old_p_int . '">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>OD Thrift</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="postvalue[' . $member->me_no . '][d_last_thrift]" value="' . $member->old_thrift . '" id="d_last_thrift' . $sno . '" data-id="' . $member->old_thrift . '" class="form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Interest</b>&nbsp;&nbsp; (Number of days)</div>
                <div class="col-sm-2"><input type="number" class="form-control" onchange="get_interest();"  id="no_of_days" placeholder="Days " name="days"></div>
                <div class="col-sm-2">
                    <input type="number" class="form-control" id="interest" readonly name="postvalue[' . $member->me_no . '][d_int]" value="0">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Loan Os Amount</b></div>
                <div class="col-sm-4">
                <input type="text" readonly value="' . $loan_os_amt . '" id="loan_os_amt" class="form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>FL OD Interest</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="postvalue[' . $member->me_no . '][f_old_int]" class="form-control" value="' . $member->f_old_int . '">
                </div>
                <div class="col-sm-4">
                  
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>FL OD Principal</b></div>
                <div class="col-sm-4">
                <input type="text" readonly name="postvalue[' . $member->me_no . '][f_old_pri]" value="' . $member->f_old_pri . '" id="d_last_principal' . $sno . '" data-id="' . $member->f_old_pri . '" class="form-control">
                </div>
                <div class="col-sm-4">
                   
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>FL Peneal Interest</b></div>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="f_p_int" value="' . $tot_f_p_int . '" readonly>
                    <input type="hidden" name="postvalue[' . $member->me_no . '][f_p_int]" value="' . $member->f_p_int . '">
                    <input type="hidden" name="postvalue[' . $member->me_no . '][f_old_p_int]" value="' . $member->f_pri_int . '">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>FL Interest</b>&nbsp;&nbsp; (Number of days)</div>
                <div class="col-sm-2"><input type="number" class="form-control" onchange="get_fl_interest();"  id="fl_no_of_days" placeholder="Days " name="fl_no_of_days"></div>
                <div class="col-sm-2">
                <input type="hidden" id="fl_int_rate" name="fl_int_rate"  value="' . $member->f_int_rate . '">
                    <input type="number" class="form-control" id="fl_interest" readonly name="postvalue[' . $member->me_no . '][f_int]" value="0">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>FL Os Amount</b></div>
                <div class="col-sm-4">
                <input type="text" readonly value="' . $f_loan_os_amt . '" id="f_os" name="f_os" class="form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Cash Type - Bank</b></div>
                <div class="col-sm-4">
                <select class="form-control" name="cash_type" id="cash_type_id">
                <option value="">--Select--</option>';
                foreach ($data['banks'] as $bank) {
                    $html .= '<option value="' . $bank['bank'] . '">' . $bank['bank'] . '</option>';
                }
                $html .= '</select>
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Payment Date :</b></div>
                <div class="col-sm-4">
                    <input type="date" name="added_on" id="added_on_id" value="' . date("d-m-Y") . '" class="form-control">
                </div>
            </div><br><br>
            <div class="row mt-3" id="cheque_trans_id_div" style="display:none;">
                <div class="col-sm-6"><b>Cheque number or Transaction id</b></div>
                <div class="col-sm-6">
                    <input name="cheque_trans_id" id="cheque_trans_id" class="form-control">
                </div>
            </div>
            <br><br>
            <input type="hidden" name="postvalue[' . $member->me_no . '][month]" value="' . $last_demand['d_month'] . '">
            <input type="hidden" name="postvalue[' . $member->me_no . '][year]" value="' . $last_demand['d_year'] . '">
            <div class="row">
                <div class="col-sm-5"></div>
                <button type="button" onclick="check_validation();" class="btn btn-primary col-sm-2">Submit</button>
            </div>
            ';
                $sno++;
            }
            echo $html;
        }
    }


    public function get_close_payment_details()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['membersf'] = $this->member_model->members();
        $members_data = array();
        $inps = $this->input->post();
        // print_r($inps);die;
        if (isset($inps['me_no']) && !isset($inps['postvalue'])) {
            $this->load->model('demand_model');
            $demand_res = $this->demand_model->get_demand($inps);
            if (count($demand_res) > 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Already Generated</div>';
                redirect('Demand/demand_generate', 'refresh');
            }
            $lastd = $this->demand_model->get_last_demand($inps);

            $lastr = $this->demand_model->get_last_recover($inps);
            $dm_num = date("m", strtotime($lastd['d_month']));
            $ddate = $lastd['d_year'] . "-" . $dm_num . "-01";
            $rm_num = date("m", strtotime($lastr['payment_month']));
            $rdate = $lastr['payment_year'] . "-" . $rm_num . "-01";
            if ($rdate < $ddate) {
                $last_demand = $lastd;
            } else {
                $last_demand = array();
            }
            //print_r($last_demand);die();
            $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);
            $sno = 1;
            foreach ($members as $key => $member) {
                $member = (object) $member;
                $html = ' 
            <input type="hidden"  name="me_no" value="' . $member->me_no . '" id="me_no" data-id="' . $member->me_no . '" class="form-control">
            <div class="row mt-3">
                <div class="col-sm-4"><b>Share Amount</b></div>
                <div class="col-sm-4">
                <input type="text"  name="share_amount" id="share_amount"  value="' . $member->tot_share . '"  class=" form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Share Int. Amount</b></div>
                <div class="col-sm-4">
                    <input type="number" value="0"  name="share_int_amount" id="share_int_amount"   class="share_amount form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Thrift Amount</b></div>
                <div class="col-sm-4">
                <input type="text"  name="thrift_amount" id="thrift_amount"  value="' . $member->tot_thrift . '"  class=" form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Thrift Int. Amount</b></div>
                <div class="col-sm-4">
                    <input type="number" value="0"  name="thrift_int_amount" id="thrift_int_amount"  class="thrift_int_amount form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Cash Type - Bank</b></div>
                <div class="col-sm-4">
                    <input type="radio" id="tdccb" name="cash_type" value="tdccb">&nbsp; TDCCB&nbsp;&nbsp;
                    <input type="radio" id="sbi" name="cash_type" value="sbi">&nbsp; SBI&nbsp;&nbsp;
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Payment Date :</b></div>
                <div class="col-sm-4">
                    <input type="date" name="added_on" id="added_on_id" value="' . date("d-m-Y") . '" class="form-control">
                </div>
            </div><br><br>
            <div class="row mt-3" id="cheque_trans_id_div">
                <div class="col-sm-4"><b>Cheque number</b></div>
                <div class="col-sm-4">
                    <input name="cheque_trans_id" id="cheque_trans_id" class="form-control">
                </div>
            </div>
            <br><br>
            <div class="row">
                <div class="col-sm-5"></div>
                <button type="button" onclick="check_validation();" class="btn btn-primary col-sm-2">Submit</button>
            </div>
            ';
                $sno++;
            }
            echo $html;
        }
    }

    public function get_demand_values()
    {
        $inps = $this->input->post();

        $res = $this->db->select("d_others,d_insurance,p_int,d_thrift,d_int")->where_in("me_no", $inps['me_no'])->get("demand")->result();
        echo json_encode($res[0]);
    }
    public function get_thrift_values()
    {
        $inps = $this->input->post();
        $res = $this->db->select("old_thrift,old_p_int")->where_in("me_no", $inps['me_no'])->get("old_dues")->result();
        echo json_encode($res[0]);
    }

    public function get_interest_values()
    {
        $inps = $this->input->post();
        $res = $this->db->select("old_due_int")->where_in("me_no", $inps['me_no'])->get("old_dues")->result();
        echo json_encode($res[0]);
    }

    public function os()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['members'] = $this->member_model->members();
        $inps = $this->input->post();
        if (isset($_POST['postvalue'])) {
            $added_on = $this->input->post('added_on');
            $cash_type = $this->input->post('cash_type');
            $cheque_trans_id = $this->input->post('cheque_trans_id');
            $postvalue = $this->input->post('postvalue');
            foreach ($postvalue as $key => $value) {
                $payment['tot_pay'] = $value['amt_recover'];
                $calc['total_amt'] = $value['d_total'];

                $temp = $payment['tot_pay'];
                $loano = $this->demand_model->get_loan_details($value['me_no']);
                // print_r($value);die;
                // $up_due = array("me_no" => $value['me_no'], "d_total" => $value['d_total'], "tot_pay" => $value['amt_recover'], "division" => $value['division'], "section" => $value['section'], "d_month" => $value['month'], "d_year" => $value['year']);
                // $this->demand_model->insert_due_by($up_due);
                $calc['cur_os'] = $loano['loan_os_amt'];
                $calc['loan_principle_amt'] = $loano['loan_principle_amt'];
                $calc['loan_os'] = $loano['loan_os_amt'];
                $calc['cur_f_os'] = $loano['f_os'];
                $calc['curr_instalment'] = $loano['curr_instalment'];
                $re = $this->demand_model->get_maxrec_details();
                $reverse_rec = $this->db->select("*")->where("me_no", $value['me_no'])->where("payment_month", $value['month'])->where("payment_year", $value['year'])->order_by("payment_id desc")->limit("1")->get("payment")->result_array();
                $demand_rec = $this->db->select("*")->where("me_no", $value['me_no'])->where("d_month", $value['month'])->where("d_year", $value['year'])->order_by("d_id desc")->limit("1")->get("demand")->result_array();
                if ($payment['tot_pay'] == "" || $payment['tot_pay'] == 0) {
                    $payment['rec_no'] = 0;
                } else {
                    $re = $re + 1;
                    if ($reverse_rec[0]['rec_no'] != "") {
                        $payment['rec_no'] = $reverse_rec[0]['rec_no'];
                    } else {
                        $payment['rec_no'] = $re;
                    }
                }

                if ($temp > 0) {
                    $temp = $temp - $value['d_others'];
                    if ($temp >= 0) {
                        $payment['others'] = $value['d_others'];
                        $calc['_others'] = 0;
                    } else {
                        $payment['others'] = $value['d_others'] + $temp;
                        $calc['_others'] = $value['d_others'] - $payment['others'];
                    }
                } else {
                    $payment['others'] = 0;
                    $calc['_others'] = $value['d_others'];
                }

                if ($payment['others'] >= $demand_rec[0]['d_others']) {
                    $this->db->where("d_id", $demand_rec[0]['d_id'])->update("demand", array("d_others" => 0));
                }

                if ($temp > 0) {
                    $temp = $temp - $value['old_ins'];
                    if ($temp >= 0) {
                        $payment['old_ins'] = $value['old_ins'];
                        $calc['_old_ins'] = 0;
                    } else {
                        $payment['old_ins'] = $value['old_ins'] + $temp;
                        $calc['_old_ins'] = $value['old_ins'] - $payment['old_ins'];
                    }
                } else {
                    $payment['old_ins'] = 0;
                    $calc['_old_ins'] = $value['old_ins'];
                }


                if ($temp > 0) {
                    $temp = $temp - $value['f_old_p_int'];
                    if ($temp >= 0) {
                        $payment['f_old_p_int'] = $value['f_old_p_int'];
                        $calc['_f_old_p_int'] = 0;
                    } else {
                        $payment['f_old_p_int'] = $value['f_old_p_int'] + $temp;
                        $calc['_f_old_p_int'] = $value['f_old_p_int'] - $payment['f_old_p_int'];
                    }
                } else {
                    $payment['f_old_p_int'] = 0;
                    $calc['_f_old_p_int'] = $value['f_old_p_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['old_p_int'];
                    if ($temp >= 0) {
                        $payment['old_p_int'] = $value['old_p_int'];
                        $calc['_old_p_int'] = 0;
                    } else {
                        $payment['old_p_int'] = $value['old_p_int'] + $temp;
                        $calc['_old_p_int'] = $value['old_p_int'] - $payment['old_p_int'];
                    }
                } else {
                    $payment['old_p_int'] = 0;
                    $calc['_old_p_int'] = $value['old_p_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['f_old_int'];
                    if ($temp >= 0) {
                        $payment['f_old_int'] = $value['f_old_int'];
                        $calc['_f_old_int'] = 0;
                    } else {
                        $payment['f_old_int'] = $value['f_old_int'] + $temp;
                        $calc['_f_old_int'] = $value['f_old_int'] - $payment['f_old_int'];
                    }
                } else {
                    $payment['f_old_int'] = 0;
                    $calc['_f_old_int'] = $value['f_old_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['d_last_int'];
                    if ($temp >= 0) {
                        $payment['last_int'] = $value['d_last_int'];
                        $calc['_last_int'] = 0;
                    } else {
                        $payment['last_int'] = $value['d_last_int'] + $temp;
                        $calc['_last_int'] = $value['d_last_int'] - $payment['last_int'];
                    }
                } else {
                    $payment['last_int'] = 0;
                    $calc['_last_int'] = $value['d_last_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['f_old_pri'];
                    if ($temp >= 0) {
                        $payment['f_old_pri'] = $value['f_old_pri'];
                        $calc['_f_old_pri'] = 0;
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_old_pri'];
                    } else {
                        $payment['f_old_pri'] = $value['f_old_pri'] + $temp;
                        $calc['_f_old_pri'] = $value['f_old_pri'] - $payment['f_old_pri'];
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_old_pri'];
                    }
                } else {
                    $payment['f_old_pri'] = 0;
                    $calc['_f_old_pri'] = $value['f_old_pri'];
                    $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_old_pri'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['d_last_principal'];
                    if ($temp >= 0) {
                        $payment['last_principal'] = $value['d_last_principal'];
                        $calc['_last_principal'] = 0;
                        $calc['cur_os'] = $calc['cur_os'] - $payment['last_principal'];
                    } else {
                        $payment['last_principal'] = $value['d_last_principal'] + $temp;
                        $calc['_last_principal'] = $value['d_last_principal'] - $payment['last_principal'];
                        $calc['cur_os'] = $calc['cur_os'] - $payment['last_principal'];
                    }
                } else {
                    $payment['last_principal'] = 0;
                    $calc['_last_principal'] = $value['d_last_principal'];
                    $calc['cur_os'] = $calc['cur_os'] - $payment['last_principal'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['d_last_thrift'];
                    if ($temp >= 0) {
                        $payment['old_thrift'] = $value['d_last_thrift'];
                        $calc['_old_thrift'] = 0;
                    } else {
                        $payment['old_thrift'] = $value['d_last_thrift'] + $temp;
                        $calc['_old_thrift'] = $value['d_last_thrift'] - $payment['old_thrift'];
                    }
                } else {
                    $payment['old_thrift'] = 0;
                    $calc['_old_thrift'] = $value['d_last_thrift'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['f_p_int'];
                    if ($temp >= 0) {
                        $payment['f_p_int'] = $value['f_p_int'];
                        $calc['_f_p_int'] = 0;
                    } else {
                        $payment['f_p_int'] = $value['f_p_int'] + $temp;
                        $calc['_f_p_int'] = $value['f_p_int'] - $payment['f_p_int'];
                    }
                } else {
                    $payment['f_p_int'] = 0;
                    $calc['_f_p_int'] = $value['f_p_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - (int)$value['p_int'];
                    if ($temp >= 0) {
                        $payment['p_int'] = $value['p_int'];
                        $calc['_p_int'] = 0;
                    } else {
                        $payment['p_int'] = $value['p_int'] + $temp;
                        $calc['_p_int'] = $value['p_int'] - $payment['p_int'];
                    }
                } else {
                    $payment['p_int'] = 0;
                    $calc['_p_int'] = $value['p_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['f_int'];
                    if ($temp >= 0) {
                        $payment['f_int'] = $value['f_int'];
                        $calc['_f_int'] = 0;
                    } else {
                        $payment['f_int'] = $value['f_int'] + $temp;
                        $calc['_f_int'] = $value['f_int'] - $payment['f_int'];
                    }
                } else {
                    $payment['f_int'] = 0;
                    $calc['_f_int'] = $value['f_int'];
                }

                if ($temp > 0) {
                    $temp = $temp - $value['d_int'];
                    if ($temp >= 0) {
                        $payment['loan_int_amt'] = $value['d_int'];
                        $calc['_loan_int_amt'] = 0;
                    } else {
                        $payment['loan_int_amt'] = $value['d_int'] + $temp;
                        $calc['_loan_int_amt'] = $value['d_int'] - $payment['loan_int_amt'];
                    }
                } else {
                    $payment['loan_int_amt'] = 0;
                    $calc['_loan_int_amt'] = $value['d_int'];
                }

                $calc['ex'] = $calc['cur_os'];
                $calc['f_ex'] = $calc['cur_f_os'];

                if ($temp > 0) {
                    $temp = $temp - $calc['cur_f_os'];
                    if ($temp >= 0) {
                        $payment['f_os_pay'] = $calc['f_ex'];
                        $calc['cur_f_os'] = 0;
                    } else {
                        $payment['f_os_pay'] = $calc['f_ex'] + $temp;
                        $calc['cur_f_os'] = $calc['f_ex'] - $payment['f_os_pay'];
                    }
                } else {
                    $payment['f_os_pay'] = 0;
                    $calc['cur_f_os'] = $calc['f_ex'];
                }

                if ($temp > 0) {
                    $temp = $temp - $calc['cur_os'];
                    if ($temp >= 0) {
                        $payment['os_pay'] = $calc['ex'];
                        $calc['loan_os'] = 0;
                    } else {
                        $payment['os_pay'] = $calc['ex'] + $temp;
                        $calc['loan_os'] = $calc['ex'] - $payment['os_pay'];
                    }
                } else {
                    $payment['os_pay'] = 0;
                    $calc['loan_os'] = $calc['ex'];
                }

                $d = $this->demand_model->get_olddues_details($value['me_no']);
                $t = $this->demand_model->get_thrift_details($value['me_no']);
                $due = $this->demand_model->get_due_by_details($value);

                if ($temp > 0) {
                    // if ($value['thrift'] == "on") {
                    $temp1 = $temp;
                    // print_r($temp1);die; 

                    $temp = $temp1 - $t['tot_thrift'];
                    $uthrift = $temp1 - $temp;
                    $payment['thrift_amt'] = $uthrift;
                    $updating_thrift = $uthrift - $t['tot_thrift'];
                    $calc['update_thrift'] = $updating_thrift;
                    $data = array('tot_thrift' => $updating_thrift);
                    $this->db->where('me_no', $value['me_no'])->update('thrift', $data);
                    // }
                }
                if ($temp > 0) {
                    // if ($value['due_by'] == "on") {
                    $temp1 = $temp;
                    $temp = $temp1 - $due['due_by'];
                    $updue = $temp1 - $temp;
                    $updating_due = $updue - $due['due_by'];
                    $calc['due_by'] = $updating_due;
                    $payment['due_by'] = $updating_due;
                    $data = array('due_by' => $updating_due);
                    $this->db->where('me_no', $value['me_no'])->update('old_dues', $data);
                    // }
                }
                $due['old_due_int'] = $calc['_loan_int_amt'] + $calc['_last_int'];
                $due['old_p_int'] = (int)$calc['_p_int'] + (int)$calc['_old_p_int'];
                $due['old_due_pri'] = $calc['_loan_pri_amt'] + $calc['_last_principal'];
                $due['f_old_pri'] = $calc['_f_old_pri'] + $calc['_f_pri'];
                $due['f_old_int'] = $calc['_f_old_int'] + $calc['_f_int'];
                $due['f_old_p_int'] = $calc['_f_p_int'] + $calc['_f_old_p_int'];
                $due['old_thrift'] = $calc['_old_thrift'] + $calc['_thrift_amt'] + $calc['update_thrift'];
                $due['old_ins'] = $calc['_old_ins'] + $calc['_insurance'];
                $calc['due_by'] = $calc['due_by'];
                $payment['rec_no'] =  $payment['rec_no'];
                $payment['thrift_amt'] = $payment['thrift_amt'] + $calc['update_thrift'];
                $payment['insurance'] = $payment['insurance'] + $payment['old_ins'];
                $payment['me_no'] = $value['me_no'];
                $payment['share_amt'] = 0;
                $payment['due_by'] = $payment['due_by'];
                $month  = substr(trim($added_on), 5, 2);
                $dateObj   = DateTime::createFromFormat('!m', $month);
                $payment['payment_month'] = $dateObj->format('F');
                $payment['payment_year'] =  substr(trim($added_on), 0, 4);
                $payment['paid_on'] = $added_on;
                $payment['cash_type'] = $cash_type;
                $payment['cheque_trans_id'] = $cheque_trans_id;

                $payment['paid_by'] = $_SESSION['name'];
                $payment['p_pay'] = 1;

                // echo "<pre>";
                // print_r($payment);
                // print_r($due);
                // print_r($payment);
                // die();
                $this->db->insert("payment", $payment);
                //print_r($due);die;
                $update_dues = array('old_due_int' => $due['old_due_int'], 'old_due_pri' => $due['old_due_pri'], 'f_old_int' => $due['f_old_int'], 'f_old_pri' => $due['f_old_pri'], 'f_old_p_int' => $due['f_old_p_int'], 'old_thrift' => $due['old_thrift'], 'old_ins' => $due['old_ins'], 'due_os' => $calc['due_os'], 'old_others' => $calc['_others'], 'due_by' => $calc['due_by'], 'old_p_int' => $due['old_p_int']); //'f_old_int' => $due['f_old_int'],'f_old_pri' => $due['f_old_pri'], 'due_f_os' => $calc['due_f_os'],

                $this->db->where("me_no", $value['me_no'])->update("old_dues", $update_dues);
                if ($calc['loan_os'] <= 0) {
                    $calc['loan_principle_amt'] = 0;
                    $calc['loan_os'] = 0;
                } elseif ($calc['loan_os'] < $calc['loan_principle_amt']) {
                    $calc['loan_principle_amt'] = $calc['loan_os'];
                } else {
                    $calc['loan_principle_amt'] = $calc['loan_principle_amt'];
                }
                if ($calc['cur_f_os'] <= 0) {
                    $calc['f_pri'] = 0;
                    $calc['cur_f_os'] = 0;
                } else {
                    $calc['f_pri'] = $value['f_pri'];
                }
                $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $value['me_no'])->get("thrift")->result();
                $thrift = $thrift[0];
                $tot_thrift =  $thrift->tot_thrift + $payment['thrift_amt'] + $payment['old_thrift'];
                $update = array("tot_thrift" => $tot_thrift);
                // print_r($update);print_r($thrift);

                $res = $this->db->where("me_no", $value['me_no'])->update("thrift", $update);
                // if ($calc['cur_f_os'] <= 0) {
                //     $calc['f_pri'] = 0;
                //     $calc['cur_f_os'] = 0;
                // } else {
                //     $calc['f_pri'] = $value['f_pri'];
                // }

                $update_loan = array('loan_os_amt' => $calc['loan_os'], 'loan_principle_amt' => $calc['loan_principle_amt'], 'f_os' => $calc['cur_f_os'], 'f_pri' => $calc['f_pri'], 'curr_instalment' => $calc['curr_instalment'] + 1); //'f_os' => $calc['cur_f_os'],,  'f_pri' => $calc['f_pri']
                $this->db->where("me_no", $value['me_no']);
                $this->db->update("loan", $update_loan);

                $lastd = $this->demand_model->get_last_demand($inps);
                $lastr = $this->demand_model->get_last_recover($inps);

                $dm_num = date("m", strtotime($lastd['d_month']));
                $ddate = $lastd['d_year'] . "-" . $dm_num . "-01";
                $rm_num = date("m", strtotime($lastr['payment_month']));
                $rdate = $lastr['payment_year'] . "-" . $rm_num . "-01";
                if ($rdate < $ddate) {
                    $sdata = array("me_no" => $value['me_no'], "month" => $lastd['d_month'], "year" => $lastd['d_year']);
                    $demand_res = $this->demand_model->get_demand_pa($sdata);

                    if (count($demand_res) > 0) {
                        $d_total = $demand_res[0]['d_total'];
                        $update = array();
                        $update['d_loan_os'] = $calc['loan_os'];
                        $update['f_os'] = $calc['cur_f_os'];
                        if ($value['d_last_thrift'] >= $demand_res[0]['d_last_thrift']) {
                            $update['d_last_thrift'] = 0;
                            $d_total = $d_total - $demand_res[0]['d_last_thrift'];
                        }
                        if ($value['d_int'] >= $demand_res[0]['d_int']) {
                            $update['d_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['d_int'];
                        }
                        $update['d_principal'] = 0;
                        $d_total = $d_total - $demand_res[0]['d_principal'];
                        if ($value['d_last_principal'] >= $demand_res[0]['d_last_principal']) {
                            $update['d_last_principal'] = 0;
                            $d_total = $d_total - $demand_res[0]['d_last_principal'];
                        }
                        if ($value['d_last_int'] >= $demand_res[0]['d_last_int']) {
                            $update['d_last_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['d_last_int'];
                        }
                        $update['f_pri'] = 0;
                        if ($value['f_int'] >= $demand_res[0]['f_int']) {
                            $update['f_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['f_int'];
                        }
                        if ($value['f_p_int'] >= $demand_res[0]['f_p_int']) {
                            $update['f_p_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['f_p_int'];
                        }
                        if ($value['d_others'] >= $demand_res[0]['d_others']) {
                            $update['d_others'] = 0;
                            $d_total = $d_total - $demand_res[0]['d_others'];
                        }
                        if ($value['f_old_int'] >= $demand_res[0]['f_old_int']) {
                            $update['f_old_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['f_old_int'];
                        }
                        if ($value['old_thrift'] >= $demand_res[0]['old_thrift']) {
                            $update['old_thrift'] = 0;
                            $d_total = $d_total - $demand_res[0]['old_thrift'];
                        }
                        if ($value['old_ins'] >= $demand_res[0]['old_ins']) {
                            $update['old_ins'] = 0;
                            $d_total = $d_total - $demand_res[0]['old_ins'];
                        }
                        if ($value['old_others'] >= $demand_res[0]['old_others']) {
                            $update['old_others'] = 0;
                            $d_total = $d_total - $demand_res[0]['old_others'];
                        }
                        if ($value['old_p_int'] >= $demand_res[0]['old_p_int']) {
                            $update['old_p_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['old_p_int'];
                        }
                        if ($value['f_old_p_int'] >= $demand_res[0]['f_old_p_int']) {
                            $update['f_old_p_int'] = 0;
                            $d_total = $d_total - $demand_res[0]['f_old_p_int'];
                        }
                        $update['p_pay'] = "1";
                        $update['d_total'] = $d_total;
                        if (count($update) > 0) {
                            $res = $this->db->where("me_no", $value['me_no'])->update("demand", $update);
                        }
                    }
                }
                $_SESSION['mem_add'] = '<div class="alert alert-success">Payment completed successfully</div>';
                redirect('Member/os', 'refresh');
            }
        }
        // print_r($data['members']);die();
        $data['content'] = 'loan/os';
        $this->load->view('layout/main', $data);
    }
    public function close_member_pay()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['members'] = $this->member_model->members();
        $inps = $this->input->post();
        if (isset($_POST['cheque_trans_id'])) {
            // echo "<pre>";
            // print_r($inps);
            // die();
            if ($inps['share_amount'] > 0) {
                $data = array(
                    "mem_no" => $inps['me_no'],
                    "amount" => $inps['share_amount'],
                    "category" => 9,
                    "payment_mode" => "Cheque",
                    "cash_type" => $inps['cash_type'],
                    "cheque_no" => $inps['cheque_trans_id'],
                    "bank_name" => strtoupper($inps['cash_type']),
                    "paid_month" => date("F", strtotime($inps['added_on'])),
                    "paid_year" => date("Y", strtotime($inps['added_on'])),
                    "paid_date" => date("Y-m-d", strtotime($inps['added_on'])),
                    "added_datetime" => date("Y-m-d H:i:s"),
                    "status" => 0,
                    "type" => 1
                );
                $this->member_model->insert_cash_book($data);
                $share = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $inps['me_no'])->get("share")->result();
                $share = $share[0];
                $tot_share =  $share->tot_share - $inps['share_amount'];
                $update = array("tot_share" => $tot_share);
                $res = $this->db->where("me_no", $inps['me_no'])->update("share", $update);
            }
            if ($inps['share_int_amount'] > 0) {
                $data = array(
                    "mem_no" => $inps['me_no'],
                    "amount" => $inps['share_int_amount'],
                    "category" => 18,
                    "payment_mode" => "Cheque",
                    "cash_type" => $inps['cash_type'],
                    "cheque_no" => $inps['cheque_trans_id'],
                    "bank_name" => strtoupper($inps['cash_type']),
                    "paid_month" => date("F", strtotime($inps['added_on'])),
                    "paid_year" => date("Y", strtotime($inps['added_on'])),
                    "paid_date" => date("Y-m-d", strtotime($inps['added_on'])),
                    "added_datetime" => date("Y-m-d H:i:s"),
                    "status" => 0,
                    "type" => 1
                );
                $this->member_model->insert_cash_book($data);
            }
            if ($inps['thrift_amount'] > 0) {
                $data = array(
                    "mem_no" => $inps['me_no'],
                    "amount" => $inps['thrift_amount'],
                    "category" => 8,
                    "payment_mode" => "Cheque",
                    "cash_type" => $inps['cash_type'],
                    "cheque_no" => $inps['cheque_trans_id'],
                    "bank_name" => strtoupper($inps['cash_type']),
                    "paid_month" => date("F", strtotime($inps['added_on'])),
                    "paid_year" => date("Y", strtotime($inps['added_on'])),
                    "paid_date" => date("Y-m-d", strtotime($inps['added_on'])),
                    "added_datetime" => date("Y-m-d H:i:s"),
                    "status" => 0,
                    "type" => 1
                );
                $this->member_model->insert_cash_book($data);
                $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $inps['me_no'])->get("thrift")->result();
                $thrift = $thrift[0];
                $tot_thrift =  $thrift->tot_thrift - $inps['thrift_amount'];
                $update = array("tot_thrift" => $tot_thrift);
                $res = $this->db->where("me_no", $inps['me_no'])->update("thrift", $update);
            }
            if ($inps['thrift_int_amount'] > 0) {
                $data = array(
                    "mem_no" => $inps['me_no'],
                    "amount" => $inps['thrift_int_amount'],
                    "category" => 17,
                    "payment_mode" => "Cheque",
                    "cash_type" => $inps['cash_type'],
                    "cheque_no" => $inps['cheque_trans_id'],
                    "bank_name" => strtoupper($inps['cash_type']),
                    "paid_month" => date("F", strtotime($inps['added_on'])),
                    "paid_year" => date("Y", strtotime($inps['added_on'])),
                    "paid_date" => date("Y-m-d", strtotime($inps['added_on'])),
                    "added_datetime" => date("Y-m-d H:i:s"),
                    "status" => 0,
                    "type" => 1
                );
                $this->member_model->insert_cash_book($data);
            }
            $_SESSION['mem_add'] = '<div class="alert alert-success">Close payment completed successfully</div>';
            redirect('Member/close_member_pay', 'refresh');
        }
        // print_r($data['members']);die();
        $data['content'] = 'member/member_close';
        $this->load->view('layout/main', $data);
    }

    public function loan_close_amt()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['members'] = $this->member_model->members();
        $inps = $this->input->post();
        if (isset($_POST['postvalue'])) {
            $added_on = $this->input->post('added_on');
            $cash_type = $this->input->post('cash_type');
            $cheque_trans_id = $this->input->post('cheque_trans_id');
            $postvalue = $this->input->post('postvalue');
            foreach ($postvalue as $key => $value) {
                if ($value['thrift_amount'] > 0) {
                    $data = array(
                        "mem_no" => $value['me_no'],
                        "amount" => $value['thrift_amount'],
                        "category" => 17,
                        "payment_mode" => "Cheque",
                        "cash_type" => $cash_type,
                        "cheque_no" => $cheque_trans_id,
                        "paid_month" => date("F", strtotime($added_on)),
                        "paid_year" => date("Y", strtotime($added_on)),
                        "paid_date" => date("Y-m-d", strtotime($added_on)),
                        "added_datetime" => date("Y-m-d H:i:s"),
                        "status" => 0,
                        "type" => 0
                    );
                    $this->member_model->insert_cash_book($data);
                }
                if ($value['share_amount'] > 0) {
                    $data = array(
                        "mem_no" => $value['me_no'],
                        "amount" => $value['share_amount'],
                        "category" => 18,
                        "payment_mode" => "Cheque",
                        "cash_type" => $cash_type,
                        "cheque_no" => $cheque_trans_id,
                        "paid_month" => date("F", strtotime($added_on)),
                        "paid_year" => date("Y", strtotime($added_on)),
                        "paid_date" => date("Y-m-d", strtotime($added_on)),
                        "added_datetime" => date("Y-m-d H:i:s"),
                        "status" => 0,
                        "type" => 0
                    );
                    $this->member_model->insert_cash_book($data);
                }
            }
        }
        // print_r($data['members']);die();
        $data['content'] = 'loan/loan_close';
        $this->load->view('layout/main', $data);
    }

    public function get_loan_close_amt_details()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $data['membersf'] = $this->member_model->members();
        $members_data = array();
        $inps = $this->input->post();
        // print_r($inps);die;
        if (isset($inps['me_no']) && !isset($inps['postvalue'])) {
            $this->load->model('demand_model');
            $demand_res = $this->demand_model->get_demand($inps);
            if (count($demand_res) > 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Already Generated</div>';
                redirect('Demand/demand_generate', 'refresh');
            }
            $lastd = $this->demand_model->get_last_demand($inps);

            $lastr = $this->demand_model->get_last_recover($inps);
            $dm_num = date("m", strtotime($lastd['d_month']));
            $ddate = $lastd['d_year'] . "-" . $dm_num . "-01";
            $rm_num = date("m", strtotime($lastr['payment_month']));
            $rdate = $lastr['payment_year'] . "-" . $rm_num . "-01";
            if ($rdate < $ddate) {
                $last_demand = $lastd;
            } else {
                $last_demand = array();
            }
            //print_r($last_demand);die();
            $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);
            $sno = 1;
            foreach ($members as $key => $member) {
                $member = (object) $member;
                $year = $inps['year'];
                $isLeapYear = (bool) date('L', strtotime("$year-01-01"));
                // $ynodays = $isLeapYear ? 36600 : 36500;
                $ynodays = 36500;
                if ($member->fst_int == 1) {
                    $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $member->no_of_days) / $ynodays);
                } else {
                    $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $inps['month_days']) / $ynodays);
                    // $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
                }
                if ($member->f_fst_int == 1) {
                    $member->f_int_amt = round(($member->f_int_rate * $member->f_os * $member->no_of_days) / $ynodays);
                } else {
                    $member->f_int_amt = round(($member->f_int_rate * $member->f_os * 30) / $ynodays);
                    // $member->f_int_amt = round(($member->f_int_rate * $member->f_os) / 1200);
                }
                $member->old_pri_int = round(($member->old_due_pri * 3 * $inps['month_days']) / $ynodays);
                $member->f_pri_int = round(($member->f_old_pri * 3) / $ynodays);
                $member->total = $member->loan_int_amt + $member->loan_principle_amt +  $member->old_due_pri +  $member->old_due_int + $member->insurance + $member->thrift_amt + $member->old_pri_int  +  $member->old_ins +  $member->old_others + $member->old_p_int + $member->f_pri + $member->f_int_amt + $member->f_old_pri + $member->f_old_int + $member->f_pri_int + $member->f_old_p_int;
                $member->total = round($member->total);
                $members_data[$key] = (array) $member;
                $tot_p_int = $member->p_int + $member->old_p_int;
                $tot_f_p_int = $member->f_pri_int + $member->f_old_p_int;
                $loan_os_amt = $member->loan_os_amt - $member->old_due_pri;
                $f_loan_os_amt = $member->f_os - $member->f_old_pri;
                $othersamt = $last_demand['d_others'] + $member->old_others;
                $total_amount = $tot_p_int + $member->old_ins + $member->old_due_int + $member->old_thrift + $member->loan_os_amt + $member->f_os + $member->f_old_int + $member->f_pri_int + $member->f_old_p_int + $othersamt;
                $html = ' 
            <input type="hidden"  name="postvalue[' . $member->me_no . '][me_no]" value="' . $member->me_no . '" id="me_no' . $sno . '" data-id="' . $member->me_no . '" class="form-control">
            <div class="row mt-3">
                <div class="col-sm-4"><b>Thrift Amount</b></div>
                <div class="col-sm-4">
                    <input type="number" value="' . $member->tot_thrift . '"  name="postvalue[' . $member->me_no . '][thrift_amount]" id="tot_thrift_amount" data-id="' . $sno . '"  class="thrift_amount form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Share Amount</b></div>
                <div class="col-sm-4">
                    <input type="text" name="postvalue[' . $member->me_no . '][share_amount]" value="' . $member->tot_share . '" id="tot_share_amount' . $sno . '" data-id="' . $member->tot_share . '" class="form-control">
                </div>
                <div class="col-sm-4">
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Cash Type - Bank</b></div>
                <div class="col-sm-4">
                    <input type="radio" id="tdccb" name="cash_type" value="tdccb">&nbsp; TDCCB&nbsp;&nbsp;
                    <input type="radio" id="sbi" name="cash_type" value="sbi">&nbsp; SBI&nbsp;&nbsp;
                </div>
            </div><br><br>
            <div class="row mt-3">
                <div class="col-sm-4"><b>Payment Date :</b></div>
                <div class="col-sm-4">
                    <input type="date" name="added_on" id="added_on_id" value="' . date("d-m-Y") . '" class="form-control">
                </div>
            </div><br><br>
            <div class="row mt-3" id="cheque_trans_id_div" style="display:none;">
                <div class="col-sm-6"><b>Cheque number or Transaction id</b></div>
                <div class="col-sm-6">
                    <input name="cheque_trans_id" id="cheque_trans_id" class="form-control">
                </div>
            </div>
            <br><br>
            <div class="row">
                <div class="col-sm-5"></div>
                <button type="button" onclick="check_validation();" class="btn btn-primary col-sm-2">Submit</button>
            </div>
            ';
                $sno++;
            }
            echo $html;
        }
    }

    //-----------------------------------------------------------------------------//
    //---------------------------------General  Ledger------------------------------//
    //-----------------------------------------------------------------------------//
    public function gl()
    {
        $data['content'] = 'general_ledger/index';
        $data['category'] = $this->member_model->expense_category();
        $data['expense_others_category'] = $this->member_model->expense_others_category();
        $this->load->view('layout/main', $data);
    }
    public function ledger_search()
    {
        $data['category'] = $this->member_model->expense_category();
        $data['expense_others_category'] = $this->member_model->expense_others_category();
        $inps = $this->input->post(); {
            //  print_r($inps);
            // die;
            $data['values'] = $this->member_model->ledger_search($inps);
            $data['select'] = $inps['ledger'];
            // print_r($data['values']);
            // die;
        }
        // print_r($data);
        //     die;
        $data['content'] = 'general_ledger/index';
        $this->load->view('layout/main', $data);
    }
    public function transfer_details()
    {
        $this->load->model('member_model');
        $members_data = array();
        $inps = $this->input->post();
        if (isset($inps['year'])) {
            $month = $inps['month'];
            $nmonth = date('m', strtotime($month));
            $inps['month'] = $nmonth;
            $data['members'] = $this->member_model->get_member_transfer($inps);

            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Tranfer Found</div>';
                redirect('Member/transfer_details', 'refresh');
            } else {
                $data['members'] = $this->member_model->get_member_transfer_details($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        $data['content'] = 'demand/transfer_details';
        $this->load->view('layout/main', $data);
    }
    public function edited_loan_details()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();

        if (isset($inps['division'])) {
            $month = $inps['month'];
            $nmonth = date('m', strtotime($month));
            $inps['month'] = $nmonth;
            $data['members'] = $this->member_model->get_edited_loan($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Records Found</div>';
                redirect('Member/edited_loan_details', 'refresh');
            } else {
                $data['members'] = $this->member_model->get_edited_loan_details($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        $data['content'] = 'demand/edited_loan_details';
        $this->load->view('layout/main', $data);
    }

    public function due_by()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();

        if (isset($inps['division'])) {
            $data['members'] = $this->member_model->get_due_by($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Records Found</div>';
                redirect('Member/due_by', 'refresh');
            } else {
                $data['members'] = $this->member_model->get_due_by_details($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        $data['content'] = 'demand/due_by';
        $this->load->view('layout/main', $data);
    }

    public function edited_thrift_details()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();

        if (isset($inps['division'])) {
            $month = $inps['month'];
            $nmonth = date('m', strtotime($month));
            $inps['month'] = $nmonth;
            $data['members'] = $this->member_model->get_edited_thrift($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Records Found</div>';
                redirect('Member/edited_thrift_details', 'refresh');
            } else {
                $data['members'] = $this->member_model->get_edited_thrift_details($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        $data['content'] = 'demand/edited_thrift_details';
        $this->load->view('layout/main', $data);
    }


    public function new_member()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();

        if (isset($inps['division'])) {
            $month = $inps['month'];
            $nmonth = date('m', strtotime($month));
            $inps['month'] = $nmonth;
            // print_r($inps);
            // die;
            $data['members'] = $this->member_model->new_member($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Records Found</div>';
                redirect('Member/new_member', 'refresh');
            } else {
                $data['members'] = $this->member_model->new_member($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        $data['content'] = 'member/new_member_details';
        $this->load->view('layout/main', $data);
    }

    //----------------------------------------------------------------------------------//
    //------------------------------------Tally-----------------------------------------//
    //----------------------------------------------------------------------------------//  
    public function cash_book_details()
    {
        $data['content'] = 'tally/cash_book_details';
        $data['members'] = $this->member_model->members();
        $data['banks'] = $this->member_model->get_bank();
        $members_temp = array();
        foreach ($data['members'] as $mem) {
            $members_temp[$mem['me_no']] = $mem;
        }
        $data['members_temp'] = $members_temp;
        $data['category_array'] = $this->member_model->get_category();
        $category_temp = array();
        foreach ($data['category_array'] as $cat) {
            $category_temp[$cat['id']] = $cat['category'];
        }
        $data['category'] = $category_temp;
        $data['sub_category_array'] = $this->member_model->get_sub_category();
        $sub_category_temp = array();
        foreach ($data['sub_category_array'] as $cat) {
            $sub_category_temp[$cat['id']] = $cat['sub_category'];
        }
        $data['sub_category'] = $sub_category_temp;
        $inps = $this->input->post();
        if (isset($inps['year']) || isset($inps['added_on'])) {
            $data['cash_book'] = $this->member_model->get_cash_book($inps);
        }
        $data['cheque_details'] = $this->member_model->get_cheque(date('Y-m-d'));
        // echo "<pre>";
        // print_r($data['cheque_details']);
        // die();
        $this->load->view('layout/main', $data);
    }

    public function insert_cash_book()
    {
        $inps = $this->input->post();
        if ($inps['type'] == "Cash") {
            $cash_type = "Cash";
        } else {
            if ($inps['bank'] == "SBI") {
                $cash_type = "sbi";
            } else {
                $cash_type = "tdccb";
            }
        }
        $data = array(
            "mem_no" => $inps['member_number'],
            "amount" => $inps['amount'],
            "category" => $inps['statement'],
            "sub_category" => $inps['sub_category'],
            "explanation" => $inps['explanation'],
            "payment_mode" => $inps['type'],
            "bank_name" => $inps['bank'],
            "cash_type" => $cash_type,
            "cheque_type" => $inps['cheque_type'],
            "cheque_no" => $inps['cheque_no'],
            "voc_no" => $inps['voc_no'],
            "paid_month" => date("F", strtotime($inps['date'])),
            "paid_year" => date("Y", strtotime($inps['date'])),
            "paid_date" => date("Y-m-d", strtotime($inps['date'])),
            "added_datetime" => date("Y-m-d H:i:s"),
            "status" => 0,
            "type" => $inps['payment_type']
        );
        $this->member_model->insert_cash_book($data);
        if ($inps['statement'] == "21") {
            $due = $this->demand_model->get_due_by_details($value);
            $due_amount = $due['due_by'] - $data['amount'];
            if ($due_amount < 0) {
                $due_amount = 0;
            }
            $this->db->where("me_no", $inps['member_number'])->update("old_dues", array("due_by" => $due_amount));
            redirect('Member/dueby_details', 'refresh');
        }
        redirect('Member/cash_book_details', 'refresh');
    }
    public function update_cash_book()
    {
        $inps = $this->input->post();
        // echo "<pre>";print_r($inps);die();
        $data = array(
            "id" => $inps['id'],
            "amount" => $inps['amount'],
            "category" => $inps['category'],
            "sub_category" => $inps['sub_category'],
            "explanation" => $inps['explanation'],
            "paid_month" => date("F", strtotime($inps['paid_date'])),
            "paid_year" => date("Y", strtotime($inps['paid_date'])),
            "paid_date" => date("Y-m-d", strtotime($inps['paid_date'])),
            "updated_datetime" => date("Y-m-d H:i:s")
        );
        $this->member_model->update_cash_book($data);
        echo 1;
    }

    public function remove_cash_book()
    {
        $inps = $this->input->get();
        $data = array(
            "id" => $inps['id'],
            "is_deleted" => 1
        );
        $this->member_model->update_cash_book($data);
        redirect('Member/cash_book_details', 'refresh');
    }

    public function cancel_cash_book()
    {
        $inps = $this->input->get();
        $data = array(
            "id" => $inps['id'],
            "is_cancelled" => 1
        );
        $this->member_model->update_cash_book($data);
        redirect('Member/cash_book_details', 'refresh');
    }

    public function income_details()
    {
        $list = $this->input->post('list');
        $lists = explode("-", $list);
        $data['list'] = $lists[1];
        $stitle = $this->input->post('stitle');
        $stitles = explode("-", $stitle);
        $data['stitle'] = $stitles[1];
        $result = $this->db->query("select max(id) as id from income");
        $income = $result->result();
        $voc_no = $income[0]->id + 1;
        $data['voc_no'] = 'REC.NO.' . $voc_no;
        $data['value'] = $this->input->post('income_id');
        $data['mode'] = $this->input->post('mode');
        $data['adjustment'] = $this->input->post('adjustment');
        $data['camount'] = $this->input->post('amount');
        $data['tamil'] = $this->input->post('tamil');
        $data['sub_tamil'] = $this->input->post('sub_tamil');
        $data['paid_on'] = $this->input->post('date');
        $m = substr(trim($data['paid_on']), 5, 2);
        $data['income_year'] = substr(trim($data['paid_on']), 0, 4);
        // $data['income_month'] = date('F', strtotime($m));
        $dateObj   = DateTime::createFromFormat('!m', $m);
        $data['income_month'] = $dateObj->format('F');
        $data['paid_by'] =  $_SESSION["name"];
        $this->member_model->insert_income($data);
        redirect('Member/insert_tally', 'refresh');
    }

    public function income_others_details()
    {
        $list = $this->input->post('list');
        $lists = explode("-", $list);
        $data['list'] = $lists[1];
        $data['stitle'] = $this->input->post('stitle');
        $result = $this->db->query("select max(id) as id from income");
        $income = $result->result();
        $voc_no = $income[0]->id + 1;
        $data['voc_no'] = 'REC.NO.' . $voc_no;
        $data['value'] = $this->input->post('income_id');
        $data['camount'] = $this->input->post('camount');
        $data['tamil'] = $this->input->post('tamil');
        $data['sub_tamil'] = $this->input->post('sub_tamil');
        $data['paid_on'] = $this->input->post('date');
        $m = substr(trim($data['paid_on']), 5, 2);
        $data['income_year'] = substr(trim($data['paid_on']), 0, 4);
        // $data['income_month'] = date('F', strtotime($m));
        $dateObj   = DateTime::createFromFormat('!m', $m);
        $data['income_month'] = $dateObj->format('F');
        $data['paid_by'] =  $_SESSION["name"];
        $this->member_model->insert_income($data);
        redirect('Member/insert_tally', 'refresh');
    }

    public function expense_details()
    {
        $list = $this->input->post('list');
        $list = $this->input->post('list');
        $lists = explode("-", $list);
        $data['list'] = $lists[1];
        $data['stitle'] = $this->input->post('stitle');

        $result = $this->db->query("select max(rec_no) as id from expense");
        $income = $result->result();
        $expense = $income[0]->id;
        $m = substr(trim($expense), 7, 2);

        $result = $this->db->query("select max(rec_no) as id from total_expense");
        $income = $result->result();
        $total_expense = $income[0]->id;
        $n = substr(trim($total_expense), 7, 2);

        if ($m > $n) {
            $voc_no = $m;
        } else {
            $voc_no = $n;
        }
        $voc_no = $voc_no + 1;
        $data['rec_no'] = 'VOC.NO.' . $voc_no;
        $data['camount'] = $this->input->post('camount');
        $data['tamil'] = $this->input->post('sub_tamil');
        $data['sub_tamil'] = $this->input->post('sub_tamil1');
        $data['paid_on'] = $this->input->post('date');
        $m = substr(trim($data['paid_on']), 5, 2);
        $data['expense_year'] = substr(trim($data['paid_on']), 0, 4);
        $dateObj   = DateTime::createFromFormat('!m', $m);
        $data['expense_month'] = $dateObj->format('F');
        $data['paid_by'] =  $_SESSION["name"];
        $this->member_model->insert_expense1($data);
        redirect('Member/insert_tally', 'refresh');
    }
    public function insert_expenses()
    {
        $data['statement'] = $this->input->post('statement');
        $data['to'] = $this->input->post('to');
        $data['for'] = $this->input->post('for');
        $data['type'] = $this->input->post('type');
        $data['cheque_no'] = $this->input->post('cheque_no');

        $data['amount'] = $this->input->post('amount');


        $data['mode'] = $this->input->post('mode');
        $data['voc_no'] = $this->input->post('voc_no');
        $data['cheque_value'] = $this->input->post('cheque_amount');
        $data['paid_on'] = $this->input->post('date');
        $m = substr(trim($data['paid_on']), 5, 2);
        $data['expense_year'] = substr(trim($data['paid_on']), 0, 4);
        $dateObj   = DateTime::createFromFormat('!m', $m);
        $data['expense_month'] = $dateObj->format('F');
        $data['paid_by'] =  $_SESSION["name"];
        $status = $this->input->post('status');

        if ($data['type'] == "RECEIPT") {
            $dt['bank_balance'] = $this->member_model->get_bank_balance();
            $bank_balance =  $dt['bank_balance']->account_balance;
            $amount = $data['amount'];
            $balance = (int)$bank_balance - (int)$amount;
            $update_balance = array('account_balance' =>  $balance);
            $this->db->update("common", $update_balance);

            if ($status == "0") {
                $cheque_no =  $data['cheque_no'];
                $dt['cheque_amount'] = $this->member_model->get_cheque_amount($cheque_no);
                $cheque_amount = $dt['cheque_amount']->cheque_amount;
                $balance_cheque_amount = (int)$cheque_amount - (int)$amount;
                $taken_on = date('Y-m-d');
                $update_cheque = array('cheque_amount' =>  $balance_cheque_amount, 'taken_on' =>  $taken_on);
                $this->db->where("cheque_no", $cheque_no);
                $this->db->update("cheque", $update_cheque);
            }

            $inps['type'] = $data['type'];
            $inps['amount'] = $data['amount'];
            $inps['cheque_no'] =  $data['cheque_no'];
            $inps['bank_balance'] =   $balance;
            $inps['paid_on'] = $data['paid_on'];
            $inps['payment_month'] = $data['expense_month'];
            $inps['payment_year'] = $data['expense_year'];
            $this->member_model->insert_bank_details($inps);
        } else {
            $dt['bank_balance'] = $this->member_model->get_bank_balance();
            $bank_balance =  $dt['bank_balance']->account_balance;
            $amount = $data['amount'];
            $balance = (int)$bank_balance + (int)$amount;
            $update_balance = array('account_balance' =>  $balance);
            $this->db->update("common", $update_balance);

            $inps['type'] = $data['type'];
            $inps['amount'] = $data['amount'];
            $inps['cheque_no'] =  $data['cheque_no'];
            $inps['paid_on'] = $data['paid_on'];
            $inps['payment_month'] = $data['expense_month'];
            $inps['payment_year'] = $data['expense_year'];
            $this->member_model->insert_bank_details($inps);
        }
        // print_r($data);die;
        $this->member_model->insert_expense($data);
        redirect('Member/insert_tally');

        // $ex['for']= $data['for'];
        // $ex['expense_year']= $data['expense_year'];
        // $ex['expense_month']= $data['expense_month'];
        // $check = $this->member_model->check_expense($ex);
        // if ($check == '1') {
        //     $id = $this->member_model->check_expense_id($ex);
        // if($id->mode == 'Cash'){
        //     $update_dues = array('adjustment' => $data['amount'], 'adj_mode' => $data['mode']);
        //     $this->db->where("id", $id->id)->update("expense", $update_dues);
        // }else{
        //     $update_dues = array('amount' => $data['amount'], 'mode' => $data['mode']);
        //     $this->db->where("me_no", $inps['me_no'])->update("expense", $update_dues);
        // }

        // } else {
        //     $this->member_model->insert_expense($data);
        // }    
    }




    public function update_expense()
    {
        $data['statement'] = $this->input->post('statement');
        $data['amount'] = $this->input->post('amount');
        $data['to'] = $this->input->post('to');
        $data['paid_on'] = $this->input->post('paid_on');
        $data['id'] = $this->input->post('id');
        $m = substr(trim($data['paid_on']), 5, 2);
        $data['expense_year'] = substr(trim($data['paid_on']), 0, 4);
        $data['expense_month'] = date('F', strtotime($m));
        $data['paid_by'] =  $_SESSION["name"];
        $result = $this->member_model->update_expense($data);
        echo 1;
    }
    public function edit_income($id)
    {
        $data['content'] = 'tally/income_edit';
        $data['income'] = $this->db->where('id', $id)->get('income')->row();
        $this->load->view('layout/main', $data);
    }
    public function edit_income1($id)
    {
        $inps = $this->input->post();
        $this->db->where('id', $id)->update('income', $inps);
        redirect('Member/insert_tally', 'refresh');
    }
    public function edit_expense($id)
    {
        $data['content'] = 'tally/expense_edit';
        $data['expense'] = $this->db->where('id', $id)->get('total_expense')->row();
        $this->load->view('layout/main', $data);
    }
    public function edit_expense1($id)
    {
        $inps = $this->input->post();
        $this->db->where('id', $id)->update('total_expense', $inps);
        redirect('Member/insert_tally', 'refresh');
    }

    public function expenses()
    {
        $data['content'] = 'tally/expense';
        $data['expense'] = $this->member_model->get_expense1();
        $data['category'] =  $this->member_model->expense_category();
        $this->load->view('layout/main', $data);
    }
    public function delete_expence1($id)
    {
        $this->db->where('id', $id)->delete('expense');
        redirect('Member/insert_tally');
    }
    public function delete_income($id)
    {
        $this->db->where('id', $id)->delete('income');
        redirect('Member/insert_tally', 'refresh');
    }
    public function delete_expense($id)
    {
        $this->db->where('id', $id)->delete('total_expense');
        redirect('Member/insert_tally', 'refresh');
    }
    public function income_Category()
    {
        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();
        $category = array('find' => $data['id'] + 1, 'category' => $inps['category'], 'value' => $inps['value']);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value']);
        // print_r($t_category);die;
        $this->db->insert("category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }
    public function expense_Others_Category()
    {
        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();
        $category = array('find' => $data['id'] + 1, 'category' => $inps['category'], 'value' => $inps['value1']);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value2']);
        // print_r($t_category);die;
        $this->db->insert("expense_category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }
    public function expense_Category()
    {
        $expense = $this->db->query("select max(id) as id from expense_category");
        $expense_statement = $expense->result();
        $expense = $expense_statement[0]->id;
        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();
        $category = array('find' => $data['id'] + 1, 'category' => $inps['category'], 'value' => $inps['value1']);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value2']);
        // print_r($t_category);die;
        $this->db->insert("map_debit_category", array("debit_category" => $inps['debit_category'], "expense_category" => $expense + 1));
        $this->db->insert("expense_category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }
    public function expense_Statement_category()
    {
        $expense = $this->db->query("select max(id) as id from expense_sub_category");
        $expense_statement = $expense->result();
        $expense = $expense_statement[0]->id;

        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();
        $category = array('find' => $data['id'] + 1, 'category' => $inps['category'], 'id' => $expense + 1, 'value' => $inps['value1']);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value2']);
        $this->db->insert("map_category", array("debit_category" => $inps['expense_category'], "expense_statement_category" => $expense + 1));
        $this->db->insert("expense_sub_category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }
    public function debit_Category()
    {
        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();
        $category = array('find' => $data['id'] + 1, 'debit_category' => $inps['category']);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value2']);
        // print_r($t_category);die;
        $this->db->insert("debit_category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }
    public function income_sub_category()
    {
        $income = $this->db->query("select max(id) as id from sub_category");
        $income_id = $income->result();
        $incomes = $income_id[0]->id;

        $result = $this->db->query("select max(id) as id from tamil");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $inps = $this->input->post();

        $category = array('find' => $data['id'] + 1, 'category' => $inps['category'], 'value' => $inps['value'], 'id' => $incomes + 1);
        $t_category = array('find' => $data['id'] + 1, 'category' => $inps['t_category'], 'value' => $inps['value']);
        // print_r($t_category);die;

        $this->db->insert("map_income_category", array("income_category" => $inps['income_category'], "income_sub_category" => $incomes + 1));
        $this->db->insert("sub_category", $category);
        $this->db->insert("tamil", $t_category);
        redirect('Member/add_category', 'refresh');
    }

    public function add_category()
    {
        $data['content'] = 'tally/category';
        $data['debit_category'] = $this->member_model->debit_category();
        $data['income_category'] = $this->member_model->income_category();
        $data['income_others_category'] = $this->member_model->income_others_category();
        $data['income_sub_category'] = $this->member_model->income_sub_category();
        $data['expense_Statement_category'] = $this->member_model->expense_Statement_category();
        $data['expense_category'] = $this->member_model->expense_category();
        $data['expense_others_category'] = $this->member_model->expense_others_category();
        $data['expense_others_sub_category'] = $this->member_model->expense_others_sub_category();
        $this->load->view('layout/main', $data);
    }
    public function delete_category($id)
    {
        $this->db->where('id', $id)->delete('category');
        redirect('Member/add_category');
    }
    public function delete_debit_category($id)
    {
        $this->db->where('id', $id)->delete('debit_category');
        redirect('Member/add_category');
    }
    public function delete_sub_category($id)
    {
        $this->db->where('id', $id)->delete('sub_category');
        redirect('Member/add_category');
    }

    function expense_category_search()
    {
        if ($expense = $this->input->post('expense')) {
            echo $this->member_model->expense_category_search($expense);
        }
    }

    function get_sub_category()
    {
        if ($category_id = $this->input->post('category_id')) {
            echo $this->member_model->sub_category_search($category_id);
        }
    }

    function income_t_search()
    {
        if ($income = $this->input->post('income')) {
            echo $this->member_model->income_t_search($income);
        }
    }
    function income_t_others_search()
    {
        if ($income = $this->input->post('income')) {
            echo $this->member_model->income_t_search($income);
        }
    }

    function income_sub_t_others_search()
    {
        if ($income = $this->input->post('income')) {
            echo $this->member_model->income_sub_t_others_search($income);
        }
    }
    function expense_sub_t_others_search()
    {
        if ($income = $this->input->post('income')) {
            echo $this->member_model->income_sub_t_others_search($income);
        }
    }
    function expense_sub_t_others_search1()
    {
        if ($income = $this->input->post('income')) {
            echo $this->member_model->expense_sub_t_others_search1($income);
        }
    }
    public function get_expense_category()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $expense = $this->db->select("id")->where("category", $inps['expense'])->get("expense_category")->row();
        $expense_id = $expense->id;
        $sections = $this->member_model->get_expense_category($expense_id);
        $html = '<option value="">All</option>';
        foreach ($sections as $caste) {
            $html .= "<option value='" . $caste['find'] . '-' . $caste['category'] . "'>" . $caste['category'] . "</option>";
        }
        echo $html;
    }

    public function get_income_others()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $expense = $this->db->select("id")->where("category", $inps['expense'])->get("category")->row();
        $expense_id = $expense->id;
        $sections = $this->member_model->get_income_others($expense_id);
        $html = '<option value="">All</option>';
        foreach ($sections as $caste) {
            $html .= "<option value='" . $caste['find'] . '-' . $caste['category'] . "'>" . $caste['category'] . "</option>";
        }
        echo $html;
    }
    public function category_details()
    {
        $this->load->model('member_model');
        $data['category'] = $this->member_model->get_category();
        $data['content'] = 'category/category_details';
        $this->load->view('layout/main', $data);
    }
    function new_category()
    {
        $data['content'] = 'category/category_add';
        $this->load->view('layout/main', $data);
    }
    public function category_add()
    {
        $requestdata = $this->input->post(NULL, FALSE);
        $this->load->model('member_model');
        $res = $this->member_model->add_category($requestdata);
        if ($res == 1) {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Category already exists</div>';
        } else {
            $_SESSION['mem_add'] = "Category added Successfully";
        }
        redirect('Member/category_details', 'refresh');
    }
    public function remove_category()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $res = $this->db->select("*")->where("id", $id)->get("category")->result_array();
        $members = $this->db->select("*")->where("category", $res[0]['category'])->where("is_deleted", 0)->get("cash_book")->result_array();
        if (count($members) > 0) {
            $_SESSION['mem_add'] = '<div class="alert alert-danger">Category exists in Cashbook</div>';
            redirect('Member/category_details', 'refresh');
        }
        $response = $this->member_model->update_category(array("id" => $id, "status" => 1));
        if ($response) {
            $this->session->set_flashdata('msg', 'Category Deleted Successfully!');
        } else {
            $this->session->set_flashdata('msg', 'Unable to process your request.!');
        }
        redirect("Member/category_details");
    }

    public function edit_category()
    {
        $id = $this->input->get('id');
        $this->load->model('member_model');
        $data['data'] = $this->member_model->display_category($id);
        $data['content'] = 'category/edit_category';
        $this->load->view('layout/main', $data);
    }

    public function update_category()
    {
        $requestData = $this->input->post(NULL, TRUE);
        $this->load->model('member_model');
        $result['data'] = $this->member_model->update_category($requestData);
        redirect("Member/category_details");
    }
    public function logout()
    {
        session_destroy();
        redirect('/Member/index');
    }
}
