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

class Demand extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        error_reporting(0);
        $this->load->library('javascript');
        $this->load->library('form_validation');
        $this->load->library('email');
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('file');
        $this->load->helper('download');
        $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();
        }

        // if ($_SESSION['name'] == "") {
        //     redirect('index.php/login/index', 'refresh');
        // }
        //error_reporting(E_ALL);
    }
    public function demand_generate()
    {
        $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['division']) && !isset($inps['postvalue'])) {
            if ($inps['month'] == "" || $inps['year'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select Month and Year</div>';
                redirect('Demand/demand_generate', 'refresh');
            }
            // if ($inps['month_days'] == '') {
            //     $_SESSION['mem_add'] = '<div class="alert alert-danger">Select Number of days</div>';
            //     redirect('demand/demand_generate', 'refresh');
            // }

            $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');
            }
            $members = $this->demand_model->get_member_loan_thrift_insurance_dues($inps);

            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 * 30) / $ynodays);
                    $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt) / 1200);
                    // $member->loan_int_amt = round(($member->loan_int_rate * $member->loan_os_amt * $inps['month_days']) / 36500);
                }
                if ($member->f_fst_int == 1) {
                    $member->f_int_amt = round(($member->f_int_rate * $member->f_os * $member->f_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->f_int_amt = round(($member->f_int_rate * $member->f_os * $inps['month_days']) / 36500);
                }
                $member->old_pri_int = round(($member->old_due_pri * 3) / 1200);
                $member->f_pri_int = round(($member->f_old_pri * 3) / 1200);
                // $member->old_pri_int = round(($member->old_due_pri * 3 * $inps['month_days']) / 36500);
                // $member->f_pri_int = round(($member->f_old_pri * 3 * $inps['month_days']) / 36500);
                $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['address'] = "";
            if ($inps['division'] != "") {
                $data['address'] = $this->member_model->get_division_address($inps['division']);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['month_days'] = $inps['month_days'];
            $data['members'] = $members_data;
        }

        if (isset($inps['postvalue'])) {
            $this->load->model('demand_model');
            $postvalue = $inps['postvalue'];
            foreach ($postvalue as $key => $value) {
                // print_r($value);die;
                $this->demand_model->insert_demand($value);
                // $up_thrift = array("me_no" => $value['me_no'], "thrift_amt" => $value['d_thrift'], "tot_thrift" => $value['tot_thrift']);
                // print_r($up_thrift); die;
                // $this->member_model->update_thrift($up_thrift);
                // $ins_others = array("me_no" => $value['me_no'], "others_amt" => $value['d_others']);
                // $this->member_model->insert_others($ins_others);
                $up_loan = array("me_no" => $value['me_no'], "fst_int" => "0");
                $this->member_model->update_loan($up_loan);
                // $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $value['me_no'])->get("thrift")->result();
                // $thrift = $thrift[0];
                // $next_thrift = date("Y-m-d",strtotime($thrift['thrift_generated_for']." + 1 month"));
                // // $data['me']
                // $payment = $this->demand_model->get_last_thrift_generate(array("me_no"=>$value['me_no'],"month"=>date("F",strtotime($next_thrift)),"year"=>date("Y",strtotime($next_thrift))));
                // $total_thrift_gen=$payment['thrift_amt']+$thrift['total_thrift_generated'];
                // $thrift_int = round(($total_thrift_gen * 11 * date("t",strtotime($next_thrift." + 1 month")) / 36500));
                // $total_thrift_int=$thrift_int+$thrift['total_thrift_int'];
                // $this->member_model->update_payment(array("payment_id"=>$payment['payment_id'],"thrift_int_amount"=>$thrift_int,"thrift_int_on"=>date("Y-m-d")));
                // $update = array("total_thrift_generated"=>$total_thrift_gen,"thrift_generated_for" => $next_thrift,"total_thrift_int"=>$total_thrift_int);
                // $res = $this->db->where("me_no", $value['me_no'])->update("thrift", $update);
            }
            $_SESSION['mem_add'] = "Demand Generated Successfully";
            redirect('Demand/demand_generate', 'refresh');
        }

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

    public function demand_old()
    {
        $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();
        if (isset($inps['division'])) {
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_demand($inps);
            $data['address'] = "";
            if ($inps['division'] != "") {
                $data['address'] = $this->member_model->get_division_address($inps['division']);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            // $data['display_demand_type'] = "";
            // if ($data['section'] != "" && $data['section'] != "MANAGER") {
            //     if ($inps['display_demand_type'] != "all") {
            //         $data['display_demand_type'] = "thrift";
            //     } else {
            //         $data['display_demand_type'] = "wothrift";
            //     }
            // } else {
            //     $data['display_demand_type'] = "all";
            // }
        }
        // echo "<pre>";print_r($data);die;
        $data['content'] = 'demand/old';
        $this->load->view('layout/main', $data);
    }
    public function sub_day_book()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $inps = $this->input->post();

        if (isset($inps['division']) || isset($inps['added_on'])) {
            if (($inps['month'] == "" || $inps['year'] == "") && $inps['added_on'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select month and year Or Date</div>';
                redirect('Demand/sub_day_book', 'refresh');
            }
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_payment($inps);
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['added_on'] = $inps['added_on'];
        }
        $data['content'] = 'demand/reciept';
        $this->load->view('layout/main', $data);
    }

    public function day_book()
    {
        $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['banks'] = $this->member_model->get_bank();
        $inps = $this->input->post();
        if ($inps['added_on'] != "") {
            $data['added_on'] = $inps['added_on'];
            $data['payment_rec'] = $this->db->select('payment.*,member.division,member.section,member.mem_name')->from('payment')
                ->join('member', 'payment.me_no = member.me_no')->where('paid_on', $inps['added_on'])->where('payment.is_deleted', 0)->get()->result_array();
            // echo $this->db->last_query();die();
            $data['cash_book'] = $this->db->select("*")->where('paid_date', $inps['added_on'])->where('is_deleted', 0)->order_by('category', 'ASC')->get("cash_book")->result_array();
        }
        // print_r($data['cash_book']);die();
        $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'];
        }
        $cheque_array = array_column($data['cash_book'], "cheque_no");
        if (count($cheque_array) > 0) {
            $cheque_details = $this->member_model->get_cheque_by_chequeno($cheque_array);
        } else {
            $cheque_details = array();
        }
        $cheque_tot_amount = array();
        foreach ($cheque_details as $cd) {
            $cheque_tot_amount[$cd['cheque_no']] = $cd['cheque_amount'];
        }
        $income_cashbook = array();
        $expense_cashbook = array();
        $expense_cheque_cashbook = array();
        $cancelled_cashbook = array();
        foreach ($data['cash_book'] as $cb) {
            $index = "";
            if (isset($category_temp[$cb['category']])) {
                $index .= $category_temp[$cb['category']];
            }
            if (isset($sub_category_temp[$cb['sub_category']])) {
                $index .= " - " . $sub_category_temp[$cb['sub_category']];
            }
            if (isset($cb['explanation']) && $cb['explanation'] != "") {
                $index .= " <br> <span style='color:black;'>" . $cb['explanation'] . "</span>";
            }
            $index_cheque = "Cheque: " . $cb['cheque_no'];
            if ($cb['is_cancelled'] == "0") {
                if ($cb['type'] == "0") {
                    if (!isset($income_cashbook[$index][$cb['bank_name']])) {
                        $income_cashbook[$index][$cb['bank_name']] = $cb['amount'];
                    } else {
                        $income_cashbook[$index][$cb['bank_name']] += $cb['amount'];
                    }
                } else {
                    if (!isset($expense_cashbook[$index][$cb['bank_name']])) {
                        $expense_cashbook[$index][$cb['bank_name']] = $cb['amount'];
                    } else {
                        $expense_cashbook[$index][$cb['bank_name']] += $cb['amount'];
                    }
                    if (!isset($expense_cheque_cashbook[$index_cheque][$cb['bank_name']])) {
                        $expense_cheque_cashbook[$index_cheque][$cb['bank_name']] = $cheque_tot_amount[$cb['cheque_no']];
                    }
                    // else {
                    //     $expense_cheque_cashbook[$index_cheque][$cb['bank_name']] += $cb['amount'];
                    // }
                }
            } else {
                if (!isset($cancelled_cashbook[$index][$cb['bank_name']])) {
                    $cancelled_cashbook[$index][$cb['bank_name']] = $cb['amount'];
                } else {
                    $cancelled_cashbook[$index][$cb['bank_name']] += $cb['amount'];
                }
            }
        }
        // echo "<pre>";
        // print_r($data['cash_book']);
        // print_r($income_cashbook);
        // print_r($expense_cashbook);
        // print_r($cancelled_cashbook);die();
        $total_amount = array();
        $total_amount_member = array();
        $total = 0;
        foreach ($data['payment_rec'] as $pay) {
            if ($pay['tot_pay'] > 0) {
                if ($pay['ent_fees'] > 0) {
                    if (!isset($total_amount_member[$pay['me_no'] . " - " . $pay['mem_name']][$pay['cash_type']])) {
                        $total_amount_member[$pay['me_no'] . " - " . $pay['mem_name']][$pay['cash_type']] = $pay['tot_pay'];
                    } else {
                        $total_amount_member[$pay['me_no'] . " - " . $pay['mem_name']][$pay['cash_type']] += $pay['tot_pay'];
                    }
                } else {
                    if (!isset($total_amount_member[$pay['division']][$pay['cash_type']])) {
                        $total_amount_member[$pay['division']][$pay['cash_type']] = $pay['tot_pay'];
                    } else {
                        $total_amount_member[$pay['division']][$pay['cash_type']] += $pay['tot_pay'];
                    }
                }
                $total += $pay['tot_pay'];
                if ($pay['thrift_amt'] ||  $pay['old_thrift'] > 0) {
                    if (!isset($total_amount['Thrift'][$pay['cash_type']])) {
                        $total_amount['Thrift'][$pay['cash_type']] = $pay['thrift_amt'] +  $pay['old_thrift'];
                    } else {
                        $total_amount['Thrift'][$pay['cash_type']] += $pay['thrift_amt'] +  $pay['old_thrift'];
                    }
                }

                if ($pay['others'] ||  $pay['old_others'] > 0) {
                    if (!isset($total_amount['Others'][$pay['cash_type']])) {
                        $total_amount['Others'][$pay['cash_type']] = $pay['others'] +  $pay['old_others'];
                    } else {
                        $total_amount['Others'][$pay['cash_type']] += $pay['others'] +  $pay['old_others'];
                    }
                }

                if ($pay['share_amt'] > 0) {
                    if (!isset($total_amount['Share'][$pay['cash_type']])) {
                        $total_amount['Share'][$pay['cash_type']] = $pay['share_amt'];
                    } else {
                        $total_amount['Share'][$pay['cash_type']] += $pay['share_amt'];
                    }
                }
                if ($pay['loan_int_amt'] || $pay['last_int'] > 0) {
                    if (!isset($total_amount['Interest'][$pay['cash_type']])) {
                        $total_amount['Interest'][$pay['cash_type']] = $pay['loan_int_amt'] + $pay['last_int'];
                    } else {
                        $total_amount['Interest'][$pay['cash_type']] += $pay['loan_int_amt'] + $pay['last_int'];
                    }
                }
                if ($pay['loan_pri_amt'] || $pay['last_principal']  || $pay['ex_os'] || $pay['os_pay'] > 0) {
                    if (!isset($total_amount['Principal'][$pay['cash_type']])) {
                        $total_amount['Principal'][$pay['cash_type']] = $pay['loan_pri_amt']  + $pay['last_principal']  + $pay['ex_os'] + $pay['os_pay'];
                    } else {
                        $total_amount['Principal'][$pay['cash_type']] += $pay['loan_pri_amt']  + $pay['last_principal']  + $pay['ex_os'] + $pay['os_pay'];
                    }
                }
                if ($pay['f_int'] || $pay['f_old_int']) {
                    if (!isset($total_amount['FL. Interest'][$pay['cash_type']])) {
                        $total_amount['FL. Interest'][$pay['cash_type']] = $pay['f_int'] + $pay['f_old_int'];
                    } else {
                        $total_amount['FL. Interest'][$pay['cash_type']] += $pay['f_int'] + $pay['f_old_int'];
                    }
                }
                if ($pay['f_pri'] || $pay['f_old_pri']  || $pay['f_ex_os'] || $pay['f_os_pay']) {
                    if (!isset($total_amount['FL. Principal'][$pay['cash_type']])) {
                        $total_amount['FL. Principal'][$pay['cash_type']] = $pay['f_pri']  + $pay['f_old_pri']  + $pay['f_ex_os'] + $pay['f_os_pay'];
                    } else {
                        $total_amount['FL. Principal'][$pay['cash_type']] += $pay['f_pri']  + $pay['f_old_pri']  + $pay['f_ex_os'] + $pay['f_os_pay'];
                    }
                }
                if ($pay['insurance']) {
                    if (!isset($total_amount['Insurance'][$pay['cash_type']])) {
                        $total_amount['Insurance'][$pay['cash_type']] = $pay['insurance'];
                    } else {
                        $total_amount['Insurance'][$pay['cash_type']] += $pay['insurance'];
                    }
                }
                if ($pay['ent_fees']) {
                    if (!isset($total_amount['Entrance Fees'][$pay['cash_type']])) {
                        $total_amount['Entrance Fees'][$pay['cash_type']] = $pay['ent_fees'];
                    } else {
                        $total_amount['Entrance Fees'][$pay['cash_type']] += $pay['ent_fees'];
                    }
                }
                if ($pay['books']) {
                    if (!isset($total_amount['Books & Forms'][$pay['cash_type']])) {
                        $total_amount['Books & Forms'][$pay['cash_type']] = $pay['books'];
                    } else {
                        $total_amount['Books & Forms'][$pay['cash_type']] += $pay['books'];
                    }
                }
                if ($pay['p_int'] ||  $pay['old_p_int'] || $pay['f_p_int'] || $pay['f_old_p_int']) {
                    if (!isset($total_amount['Peneal Int'][$pay['cash_type']])) {
                        $total_amount['Peneal Int'][$pay['cash_type']] = $pay['p_int'] +  $pay['old_p_int'] + $pay['f_p_int'] + $pay['f_old_p_int'];
                    } else {
                        $total_amount['Peneal Int'][$pay['cash_type']] += $pay['p_int'] +  $pay['old_p_int'] + $pay['f_p_int'] + $pay['f_old_p_int'];
                    }
                }
                if ($pay['due_by'] > 0) {
                    if (!isset($total_amount['Due by'][$pay['cash_type']])) {
                        $total_amount['Due by'][$pay['cash_type']] = $pay['due_by'];
                    } else {
                        $total_amount['Due by'][$pay['cash_type']] += $pay['due_by'];
                    }
                }
            }
        }
        // echo $total;die();
        $data['balance_member_count'] = 0;
        $data['balance_count'] = 0;
        $data['expense_cashbook'] = $expense_cashbook;
        $data['expense_cheque_cashbook'] = $expense_cheque_cashbook;
        if ((count($total_amount) + count($data['expense_cheque_cashbook'])) > (count($total_amount_member) + count($data['expense_cashbook']))) {
            $data['balance_member_count'] = (count($total_amount) + count($data['expense_cheque_cashbook'])) - (count($total_amount_member) + count($data['expense_cashbook']));
        } else if ((count($total_amount) + count($data['expense_cheque_cashbook'])) < (count($total_amount_member) + count($data['expense_cashbook']))) {
            $data['balance_count'] = (count($total_amount_member) + count($data['expense_cashbook'])) - (count($total_amount) + count($data['expense_cheque_cashbook']));
        }
        $data['total_amount'] = $total_amount;
        $data['total_amount_member'] = $total_amount_member;

        $data['income_cashbook'] = $income_cashbook;
        $data['cancelled_cashbook'] = $cancelled_cashbook;
        // echo "<pre>";
        // print_r($data);
        // die;
        $data['year'] = $inps['year'];
        $data['month'] = $inps['month'];
        $data['added_on'] = $inps['added_on'];
        $data['content'] = 'demand/day_book';
        $this->load->view('layout/main', $data);
    }
    public function general_ledger()
    {
        $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['banks'] = $this->member_model->get_bank();
        $inps = $this->input->post();
        if ($inps['month'] != "" && $inps['year'] != "" && ($inps['category'] != "" || $inps['bank'] != "")) {
            $data['month'] = $inps['month'];
            $data['year'] = $inps['year'];
            $data['category_val'] = $inps['category'];
            $data['bank_val'] = $inps['bank'];
            $data['payment_rec'] = $this->db->select('payment.*,member.division,member.section,member.mem_name')->from('payment')
                ->join('member', 'payment.me_no = member.me_no')->where("DATE_FORMAT(paid_on, '%M') = ", $inps['month'])->where("year(paid_on)", $inps['year'])->where('payment.is_deleted', 0)->get()->result_array();
            // echo $this->db->last_query();die();
            if ($inps['bank'] != "") {
                $where = "bank_name='" . $inps['bank'] . "'";
            } else {
                $where = "category='" . $inps['category'] . "'";
            }
            $data['cash_book'] = $this->db->select("*")->where($where)->where('paid_month', $inps['month'])->where('paid_year', $inps['year'])->where('is_deleted', 0)->order_by('category', 'ASC')->get("cash_book")->result_array();
        }
        // print_r($data['cash_book']);die();
        $data['category_array'] = $this->member_model->get_category();
        $category_temp = array();
        $category_columns = array();
        foreach ($data['category_array'] as $cat) {
            $category_temp[$cat['id']] = $cat['category'];
            if ($cat['id'] == $inps['category']) {
                $category_columns[$inps['category']] = $cat['gl'];
            }
        }
        $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'];
        }
        $cashbook = array();
        $expense_cashbook = array();
        $cancelled_cashbook = array();
        foreach ($data['cash_book'] as $cb) {
            $index = "";
            if (isset($category_temp[$cb['category']])) {
                $index .= $category_temp[$cb['category']];
            }
            if (isset($sub_category_temp[$cb['sub_category']])) {
                $index .= " - " . $sub_category_temp[$cb['sub_category']];
            }
            if (isset($cb['cash_type'])) {
                $index .= " - " . $cb['bank_name'];
            }
            if ($cb['is_cancelled'] == "0") {
                if ($cb['type'] == "0") {
                    if (!isset($cashbook[$cb['paid_date']][$index]['income'])) {
                        $cashbook[$cb['paid_date']][$index]['income'] = $cb['amount'];
                    } else {
                        $cashbook[$cb['paid_date']][$index]['income'] += $cb['amount'];
                    }
                } else {
                    if (!isset($cashbook[$cb['paid_date']][$index]['expense'])) {
                        $cashbook[$cb['paid_date']][$index]['expense'] = $cb['amount'];
                    } else {
                        $cashbook[$cb['paid_date']][$index]['expense'] += $cb['amount'];
                    }
                }
            }
        }

        $total_amount = array();
        $total_amount_member = array();
        $total = 0;
        foreach ($data['payment_rec'] as $pay) {
            if ($pay['tot_pay'] > 0) {
                // $index = $pay['me_no'] . " - " . $pay['mem_name'];
                $index = "";
                if (isset($category_temp[$inps['category']])) {
                    $index .= $category_temp[$inps['category']];
                }
                if (isset($pay['cash_type'])) {
                    $index .= " - " . $pay['cash_type'];
                }
                if (isset($category_columns[$inps['category']]) && $category_columns[$inps['category']] != "") {
                    $cat_col = explode(",", $category_columns[$inps['category']]);
                    foreach ($cat_col as $col) {
                        if ($pay[$col] > 0) {
                            // echo $col . " - " . $pay[$col];
                            if (!isset($cashbook[$pay['paid_on']][$index]['income'])) {
                                $cashbook[$pay['paid_on']][$index]['income'] = $pay[$col];
                            } else {
                                $cashbook[$pay['paid_on']][$index]['income'] += $pay[$col];
                            }
                        }
                    }
                }
            }
        }
        // echo "<pre>";
        // print_r($cashbook);
        // die();

        $data['cashbook'] = $cashbook;
        $data['year'] = $inps['year'];
        $data['month'] = $inps['month'];
        $data['added_on'] = $inps['added_on'];
        $data['content'] = 'demand/general_ledger';
        $this->load->view('layout/main', $data);
    }
    public function cash_book()
    {
        $this->load->model('member_model');
        $data['common'] = $this->member_model->get_common();
        $inps = $this->input->post();
        if ($inps['added_on'] != "" || $inps['month'] != "" || $inps['year'] != "") {
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_payment($inps);
            // print_r( $data['members']);die;

            $sno = 0;
            $data['tot_td'] = 0;
            $data['tot_loan_pri'] = 0;
            $data['tot_loan_int'] = 0;
            $data['tot_last_pri'] = 0;
            $data['tot_last_int'] = 0;
            $data['tot_f_pri'] = 0;
            $data['tot_f_int'] = 0;
            $data['tot_f_old_pri'] = 0;
            $data['tot_f_old_int'] = 0;
            $data['tot_others'] = 0;
            $data['tot_insurance'] = 0;
            $data['tot_tot'] = 0;
            $data['share_amt_tot'] = 0;
            $data['ent_fees_tot'] = 0;

            $data['tot_td_'] = 0;
            $data['tot_loan_pri_'] = 0;
            $data['tot_loan_int_'] = 0;
            $data['tot_last_pri_'] = 0;
            $data['tot_last_int_'] = 0;
            $data['tot_f_pri_'] = 0;
            $data['tot_f_int_'] = 0;
            $data['tot_f_old_pri_'] = 0;
            $data['tot_f_old_int_'] = 0;
            $data['tot_others_'] = 0;
            $data['tot_insurance_'] = 0;
            $data['tot_tot_'] = 0;
            $data['share_amt_tot_'] = 0;
            $data['ent_fees_tot_'] = 0;
            $data['tansaction_id'] = array();
            foreach ($data['members'] as $member) {
                if ($member->cash_type == "Cash") {
                    $data['share_amt_tot'] += $member->share_amt;
                    $data['ent_fees_tot'] += $member->ent_fees;
                    $data['tot_td'] += $member->thrift_amt;
                    $data['tot_loan_pri'] += $member->loan_pri_amt + $member->ex_os;
                    $data['tot_loan_int'] += $member->loan_int_amt + $member->p_int;
                    $data['tot_last_pri'] += $member->last_principal;
                    $data['tot_last_int'] += $member->last_int;
                    $data['tot_f_pri'] += $member->f_pri + $member->f_ex_os;
                    $data['tot_f_int'] += $member->f_int + $member->f_p_int;
                    $data['tot_f_old_pri'] += $member->f_old_pri;
                    $data['tot_f_old_int'] += $member->f_old_int;
                    $data['tot_others'] += $member->others;
                    $data['tot_insurance'] += $member->insurance;
                    $data['tot_tot'] += $member->tot_pay;
                } else {
                    $data['share_amt_tot_'] += $member->share_amt;
                    $data['ent_fees_tot_'] += $member->ent_fees;
                    $data['tot_td_'] += $member->thrift_amt;
                    $data['tot_loan_pri_'] += $member->loan_pri_amt + $member->ex_os;
                    $data['tot_loan_int_'] += $member->loan_int_amt + $member->p_int;
                    $data['tot_last_pri_'] += $member->last_principal;
                    $data['tot_last_int_'] += $member->last_int;
                    $data['tot_f_pri_'] += $member->f_pri + $member->f_ex_os;
                    $data['tot_f_int_'] += $member->f_int + $member->f_p_int;
                    $data['tot_f_old_pri_'] += $member->f_old_pri;
                    $data['tot_f_old_int_'] += $member->f_old_int;
                    $data['tot_others_'] += $member->others;
                    $data['tot_insurance_'] += $member->insurance;
                    $data['tot_tot_'] += $member->tot_pay;
                    if ($member->cheque_trans_id != "") {
                        if (!in_array($member->cheque_trans_id, $data['tansaction_id'])) {
                            $data['tansaction_id'][] = $member->cheque_trans_id;
                        }
                    }
                }
            }
            $data['tot_loan_int_'] = $data['tot_loan_int_'] + $data['tot_last_int_'];
            $data['tot_loan_int'] = $data['tot_loan_int'] + $data['tot_last_int'];
            $data['tot_loan_pri_'] = $data['tot_loan_pri_'] + $data['tot_last_pri_'];
            $data['tot_loan_pri'] = $data['tot_loan_pri'] + $data['tot_last_pri'];
            $data['tot_f_pri_'] = $data['tot_f_pri_'] + $data['tot_f_old_pri_'];
            $data['tot_f_pri'] = $data['tot_f_pri'] + $data['tot_f_old_pri'];
            $data['tot_f_int_'] = $data['tot_f_int_'] + $data['tot_f_old_int_'];
            $data['tot_f_int'] = $data['tot_f_int'] + $data['tot_f_old_int'];
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['added_on'] = $inps['added_on'];
            // echo "<pre>";  print_r($data);die();
        }
        $data['content'] = 'demand/cash_book';
        $this->load->view('layout/main', $data);
    }

    public function reciept()
    {
        $inps = $this->input->get();
        if ($inps['id'] != "") {
            $this->load->model('demand_model');
            $data['member'] = $this->demand_model->get_member_payment_id($inps);
            // print_r( $data['member']);die;
            $data['member'] = $data['member'][0];
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['mem_no'] = $inps['mem_no'];
            $data['added_on'] = $inps['added_on'];
        } else {
            $_SESSION['mem_add'] = "Reciept missing";
            redirect('Demand/sub_day_book', 'refresh');
        }
        $this->load->view('demand/mem_receipt', $data);
    }
    public function savepodate()
    {
        $inps = $this->input->post();
        if ($inps['id'] != "") {

            $this->load->model('demand_model');
            $data['member'] = $this->demand_model->get_member_savepayment_id($inps);
        }
        echo 1;
    }
    public function demand_recover()
    {
        $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['banks'] = $this->member_model->get_bank();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();
        // print_r($inps);die;
        if (isset($inps['division'])) {
            $recover_res = $this->demand_model->get_recover_check($inps);
            if ($recover_res > 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Already Recovered</div>';
                redirect('Demand/demand_recover', 'refresh');
            }
            $data['members'] = $this->demand_model->get_member_demand($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Not Generated</div>';
                redirect('Demand/demand_recover', 'refresh');
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        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');
            // echo "<pre>";
            // print_r($postvalue);
            // die();
            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['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();

                // if (($loano['loan_os_amt'] != $value['d_loan_os'] || $loano['f_os'] != $value['f_os']) && $value['p_pay'] != "1") {
                //     $year = $value['year'];
                //     $isLeapYear = (bool) date('L', strtotime("$year-01-01"));
                //     $ynodays = $isLeapYear ? 36600 : 36500;
                //     if ($loano['fst_int'] == 1) {
                //         $value['d_int'] = round(($loano['loan_int_rate'] * $loano['loan_os_amt'] * $loano['no_of_days']) / $ynodays);
                //     } else {
                //         $value['d_int'] = round(($loano['loan_int_rate'] * $loano['loan_os_amt']) / 1200);
                //     }
                //     if ($loano['f_fst_int'] == 1) {
                //         $value['f_int'] = round(($loano['f_int_rate'] * $loano['f_os'] * $loano['f_no_of_days']) / $ynodays);
                //     } else {
                //         $value['f_int'] = round(($loano['f_int_rate'] * $loano['f_os']) / 1200);
                //     }
                //     $old_dues = $this->db->select("*")->where("me_no", $value['me_no'])->order_by("id desc")->limit("1")->get("old_dues")->result_array();
                //     $old_dues = $old_dues[0];
                //     $value['p_int'] = round(($old_dues['old_due_pri'] * 3) / 1200);
                //     $value['f_p_int'] = round(($old_dues['f_old_pri'] * 3) / 1200);
                //     $value['d_last_principal'] = $old_dues['old_due_pri'];
                //     $value['d_last_int'] = $old_dues['old_due_int'];
                //     $value['f_old_pri'] = $old_dues['f_old_pri'];
                //     $value['f_old_int'] = $old_dues['f_old_int'];
                //     $value['old_p_int'] = $old_dues['old_pri_int'];
                //     $value['f_old_p_int'] = $old_dues['f_old_p_int'];
                //     if ($loano['loan_os_amt'] <= 0) {
                //         $value['d_principal'] = 0;
                //     } elseif ($loano['loan_os_amt'] < $value['d_principal']) {
                //         $value['d_principal'] = $loano['loan_os_amt'];
                //     }
                //     if ($loano['f_os'] <= 0) {
                //         $value['f_pri'] = 0;
                //     } elseif ($loano['f_os'] < $value['f_pri']) {
                //         $value['f_pri'] = $loano['f_os'];
                //     }
                //     $value['old_ins'] = $old_dues['old_ins'];
                //     $value['d_last_thrift'] = $old_dues['old_thrift'];
                // }

                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 ($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['d_insurance'];
                    if ($temp >= 0) {
                        $payment['insurance'] = $value['d_insurance'];
                        $calc['_insurance'] = 0;
                    } else {
                        $payment['insurance'] = $value['d_insurance'] + $temp;
                        $calc['_insurance'] = $value['d_insurance'] - $payment['insurance'];
                    }
                } else {
                    $payment['insurance'] = 0;
                    $calc['_insurance'] = $value['d_insurance'];
                }


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

                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['f_pri'];
                    if ($temp >= 0) {
                        $payment['f_pri'] = $value['f_pri'];
                        $calc['_f_pri'] = 0;
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                    } else {
                        $payment['f_pri'] = $value['f_pri'] + $temp;
                        $calc['_f_pri'] = $value['f_pri'] - $payment['f_pri'];
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                    }
                } else {
                    $payment['f_pri'] = 0;
                    $calc['_f_pri'] = $value['f_pri'];
                    $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                }

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


                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_principal'];
                    if ($temp >= 0) {
                        $payment['loan_pri_amt'] = $value['d_principal'];
                        $calc['_loan_pri_amt'] = 0;
                        $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                    } else {
                        $payment['loan_pri_amt'] = $value['d_principal'] + $temp;
                        $calc['_loan_pri_amt'] = $value['d_principal'] - $payment['loan_pri_amt'];
                        $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                    }
                } else {
                    $payment['loan_pri_amt'] = 0;
                    $calc['_loan_pri_amt'] = $value['d_principal'];
                    $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                }

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

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

                $calc['ex'] = $calc['loan_os'];
                if ($value['due_by'] != "1") {

                    if ($temp > 0) {
                        $temp = $temp - $calc['loan_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'];
                    }

                    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'];
                    }
                } else {
                    $payment['os_pay'] = 0;
                    $calc['loan_os'] = $calc['ex'];
                    $payment['f_os_pay'] = 0;
                    $calc['cur_f_os'] = $calc['f_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'] = $due['due_by'] + $temp;
                    $payment['due_by'] = $temp;
                    // $data = array('due_by' => $calc['due_by']);
                    // }
                } else {
                    $payment['due_by'] = 0;
                }

                $due['old_due_int'] = $calc['_loan_int_amt'] + $calc['_last_int'];
                $due['old_p_int'] = $calc['_p_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['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['payment_month'] = $value['month'];
                $payment['payment_year'] = $value['year'];
                $payment['paid_on'] = $added_on;
                $payment['cash_type'] = $cash_type;
                $payment['cheque_trans_id'] = $cheque_trans_id;

                $payment['paid_by'] = $_SESSION['name'];
                // echo "<pre>";
                // print_r($payment);
                // print_r($due);
                // print_r($value);
                // die();
                $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $value['me_no'])->get("thrift")->result();
                $thrift = $thrift[0];
                // $old_payment = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $value['me_no'])->order_by("id desc")->limit("1")->get("payment")->result_array();
                // if (count($old_payment) > 0 && $old_payment[0]['thrift_amt'] > 0) {
                //       $thrift_int = $thrift->total_thrift_generated * 
                // }
                $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_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'],   'old_thrift' => $due['old_thrift'],

                $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'] < $value['d_principal']) {
                    $calc['loan_principle_amt'] = $calc['loan_os'];
                } else {
                    $calc['loan_principle_amt'] = $value['d_principal'];
                }
                if ($calc['cur_f_os'] <= 0) {
                    $calc['f_pri'] = 0;
                    $calc['cur_f_os'] = 0;
                } else {
                    $calc['f_pri'] = $value['f_pri'];
                }

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

                $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);
            }
            $_SESSION['mem_add'] = "Demand Recovered Successfully";
            redirect('Demand/demand_recover', 'refresh');
        }
        // print_r($data);die;
        $data['content'] = 'demand/recover';
        $this->load->view('layout/main', $data);
    }
    public function recover_reverse()
    {
        $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();
        $members_data = array();
        $inps = $this->input->post();
        if (isset($inps['division'])) {
            $this->load->model('demand_model');
            $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) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Next demand generated, cannot reverse</div>';
                redirect('Demand/recover_reverse', 'refresh');
            }

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

            foreach ($members as $member) {
                $member_no = $member->me_no;
                $payment = $this->db->select("*")->where("payment_month", $lastr['payment_month'])->where("payment_year", $lastr['payment_year'])->where("me_no", $member_no)->where("is_deleted", 0)->get("payment")->result();

                if (count($payment) > 0) {
                    $payment = $payment[0];
                    $calc['thrift_amt'] = $payment ? $payment->thrift_amt : 0;
                    $calc['mem_no'] = $payment ? $payment->me_no : 0;
                    $calc['last_pri'] = $payment ? $payment->last_principal : 0;
                    $calc['pri_amt'] = $payment ? $payment->loan_pri_amt : 0;
                    $calc['os_pay'] = $payment ? $payment->os_pay : 0;
                    $calc['tot_pri'] = $calc['pri_amt'] + $calc['last_pri'];
                    $calc['last_int'] = $payment ? $payment->last_int : 0;
                    $calc['tot_pay'] = $payment ? $payment->tot_pay : 0;
                    $loano = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("loan")->result();
                    // print_r($loano);die;
                    $loano = $loano[0];
                    $calc['cur_os'] = $loano->loan_os_amt + $calc['tot_pri'] + $calc['pri_amt'] + $calc['os_pay'];
                    // $calc['loan_os'] = $calc['cur_os'] + $calc['tot_pri'];
                    $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("thrift")->result();
                    $thrift = $thrift[0];
                    $calc['total_t'] =  $thrift->tot_thrift;
                    $calc['tot_thrift'] = $calc['total_t'] - $calc['thrift_amt'];
                    $update = array("tot_thrift" => $calc['tot_thrift']);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("thrift", $update);
                    $update = array("is_deleted" => 1);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("payment", $update);
                    $demand = $this->db->select("*")->where("d_month", $lastr['payment_month'])->where("d_year", $lastr['payment_year'])->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("demand")->result();
                    $demand = $demand[0];
                    $calc['old_pri'] = $demand ? $demand->d_last_principal : 0;
                    $calc['old_int'] = $demand ? $demand->d_last_int : 0;
                    $calc['tot_amt'] = $demand ? $demand->d_total : 0;
                    $update = array('old_due_int' => $calc['old_int'], 'old_due_pri' => $calc['old_pri']);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("old_dues", $update);
                    $update = array('loan_os_amt' => $calc['cur_os']);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("loan", $update);
                }
            }

            $_SESSION['mem_add'] = "Recover reversed successfully";
            redirect('Demand/recover_reverse', 'refresh');
        }
        $data['content'] = 'demand/rreverse';
        $this->load->view('layout/main', $data);
    }
    //Single Recover
    public function specific_reverse()
    {
        $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();
        $data['banks'] = $this->member_model->get_bank();
        $members_data = array();
        $inps = $this->input->post();

        if (isset($inps['mem_no'])) {
            $this->load->model('demand_model');
            $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";

            $ddate = $lastd['added_on'];
            $rdate = $lastr['paid_on'];

            if ($rdate < $ddate) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Next demand generated, cannot reverse</div>';
                redirect('Demand/specific_reverse', 'refresh');
            }
            if (isset($inps['mem_no'])) {
                $count = $this->db->select("r_count")->where("payment_month", $lastr['payment_month'])->where("payment_year", $lastr['payment_year'])->where("me_no", $inps['mem_no'])->where("ent_fees", 0)->where("is_deleted", 0)->get("payment")->result();
                if ($count[0]->r_count >= 1) {
                    $_SESSION['mem_add'] = '<div class="alert alert-danger">This Member Number Number Alredy reversed</div>';
                    redirect('Demand/specific_reverse', 'refresh');
                }
            }
            $inps['payment_month'] = $lastr['payment_month'];
            $inps['payment_year'] = $lastr['payment_year'];
            $members = $this->demand_model->get_members_byf($inps);
            foreach ($members as $member) {
                $member_no = $member->me_no;
                $payment = $this->db->select("*")->where("payment_month", $lastr['payment_month'])->where("payment_year", $lastr['payment_year'])->where("me_no", $member_no)->where("ent_fees", 0)->where("is_deleted", 0)->where("r_count <", 1)->get("payment")->result();
                // $payment_id = $this->db->select("payment_id")->where("payment_month", $lastr['payment_month'])->where("payment_year", $lastr['payment_year'])->where("me_no", $member_no)->where("ent_fees", 0)->where("is_deleted", 0)->get("payment")->result();
                // $rec_no = $this->db->select("rec_no")->where("payment_month", $lastr['payment_month'])->where("payment_year", $lastr['payment_year'])->where("me_no", $member_no)->where("ent_fees", 0)->where("is_deleted", 0)->get("payment")->result();
                // $check = $payment[0]->tot_pay;
                // print_r($payment[0]->tot_pay);die;
                if (count($payment) > 0) {
                    $payment = $payment[0];
                    $calc['thrift_amt'] = $payment ? $payment->thrift_amt : 0;
                    $calc['old_thrift'] = $payment ? $payment->old_thrift : 0;
                    $calc['mem_no'] = $payment ? $payment->me_no : 0;
                    $calc['last_pri'] = $payment ? $payment->last_principal : 0;
                    $calc['pri_amt'] = $payment ? $payment->loan_pri_amt : 0;
                    $calc['tot_pri'] = $calc['pri_amt'] + $calc['last_pri'];
                    $calc['last_int'] = $payment ? $payment->last_int : 0;
                    $calc['tot_pay'] = $payment ? $payment->tot_pay : 0;
                    $calc['f_pri'] = $payment ? $payment->f_pri : 0;
                    $calc['f_os_pay'] = $payment ? $payment->f_os_pay : 0;
                    $calc['f_old_pri'] = $payment ? $payment->f_old_pri : 0;
                    $calc['due_by'] = $payment ? $payment->due_by : 0;
                    $loano = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("loan")->result();
                    $loano = $loano[0];
                    $calc['cur_os'] = $loano->loan_os_amt;
                    $calc['cur_f_os'] = $loano->f_os;
                    $calc['cur_f_os'] = $calc['cur_f_os'] + $calc['f_pri'] + $calc['f_os_pay'] + $calc['f_old_pri'];
                    $calc['loan_os'] = $calc['cur_os'] + $calc['tot_pri'];
                    $thrift = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("thrift")->result();
                    $thrift = $thrift[0];
                    $calc['total_t'] =  $thrift->tot_thrift;
                    $calc['tot_thrift'] = $calc['total_t'] - $calc['thrift_amt'] - $calc['old_thrift'];
                    $update = array("tot_thrift" => $calc['tot_thrift']);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("thrift", $update);
                    $update = array("is_deleted" => 1);
                    $res = $this->db->where("payment_id", $payment->payment_id)->update("payment", $update);
                    $demand = $this->db->select("*")->where("d_month", $lastr['payment_month'])->where("d_year", $lastr['payment_year'])->where("is_deleted", 0)->where("me_no", $calc['mem_no'])->get("demand")->result();
                    $demand = $demand[0];
                    $calc['old_pri'] = $demand ? $demand->d_last_principal : 0;
                    $calc['old_int'] = $demand ? $demand->d_last_int : 0;
                    $calc['old_ins'] = $demand ? $demand->old_ins : 0;
                    $calc['old_others'] = $demand ? $demand->old_others : 0;
                    $calc['old_thrift'] = $demand ? $demand->old_thrift : 0;
                    $calc['old_p_int'] = $demand ? $demand->old_p_int : 0;
                    $calc['tot_amt'] = $demand ? $demand->d_total : 0;
                    $calc['f_old_pri'] = $demand ? $demand->f_old_pri : 0;
                    $calc['f_old_int'] = $demand ? $demand->f_old_int : 0;
                    $calc['f_old_p_int'] = $demand ? $demand->f_old_p_int : 0;
                    $update = array('old_due_int' => $calc['old_int'], 'old_due_pri' => $calc['old_pri'], 'f_old_pri' => $calc['f_old_pri'], 'f_old_int' => $calc['f_old_int'], 'f_old_p_int' => $calc['f_old_p_int']);
                    $res = $this->db->where_in("me_no", $calc['mem_no'])->update("old_dues", $update);
                    $calc['tot_amt'] = $calc['tot_amt'] - $calc['tot_pay'];
                    if ($calc['tot_amt'] <= 0) {
                        $calc['loan_os'] = $calc['loan_os'] - $calc['tot_amt'];
                        $calc['loan_os'] = $calc['loan_os'] - $calc['due_by'];
                    }
                    $update = array('loan_os_amt' => $calc['loan_os'], 'f_os' => $calc['cur_f_os']);
                    $res = $this->db->where("me_no", $calc['mem_no'])->update("loan", $update);
                    // $delete = $this->db->where('payment_id', $payment_id[0]->payment_id)->delete('payment');
                }
            }
            $inps['d_month'] = $lastr['payment_month'];
            $inps['d_year'] = $lastr['payment_year'];
            // print_r($inps);
            $data['members_val'] = $this->demand_model->particular_member_demand($inps);
            // print_r($data['members_val']);die;
            $data['division'] =  $inps['division'];
            $data['section'] =  $inps['section'];
            $data['month'] =  $inps['d_month'];
            $data['year'] =  $inps['d_year'];
            $data['rec_no'] = $payment->rec_no;
        }
        // print_r($data['members_val']);die;
        $data['content'] = 'demand/preverse';
        $this->load->view('layout/main', $data);
    }

    public function specific_recover()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['banks'] = $this->member_model->get_bank();
        $members_data = array();
        $this->load->model('demand_model');
        $inps = $this->input->post();
        // print_r($inps);die;
        if (isset($inps['division'])) {
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        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']);
                // $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);

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

                $calc['cur_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();

                if ($reverse_rec[0]['r_count'] == 0) {
                    $payment['r_count'] = 1;
                }
                $re = $re + 1;

                if ($payment['tot_pay'] == "" || $payment['tot_pay'] == 0) {
                    $payment['rec_no'] = $reverse_rec[0]['rec_no'];
                    $payment['status'] = "Cancelled";
                } else if ($payment['tot_pay'] != "" && $reverse_rec[0]['rec_no'] == 0) {
                    $payment['rec_no'] = $re;
                } else if ($payment['tot_pay'] != "" && $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 ($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['d_insurance'];
                    if ($temp >= 0) {
                        $payment['insurance'] = $value['d_insurance'];
                        $calc['_insurance'] = 0;
                    } else {
                        $payment['insurance'] = $value['d_insurance'] + $temp;
                        $calc['_insurance'] = $value['d_insurance'] - $payment['insurance'];
                    }
                } else {
                    $payment['insurance'] = 0;
                    $calc['_insurance'] = $value['d_insurance'];
                }


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

                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['f_pri'];
                    if ($temp >= 0) {
                        $payment['f_pri'] = $value['f_pri'];
                        $calc['_f_pri'] = 0;
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                    } else {
                        $payment['f_pri'] = $value['f_pri'] + $temp;
                        $calc['_f_pri'] = $value['f_pri'] - $payment['f_pri'];
                        $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                    }
                } else {
                    $payment['f_pri'] = 0;
                    $calc['_f_pri'] = $value['f_pri'];
                    $calc['cur_f_os'] = $calc['cur_f_os'] - $payment['f_pri'];
                }

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


                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_principal'];
                    if ($temp >= 0) {
                        $payment['loan_pri_amt'] = $value['d_principal'];
                        $calc['_loan_pri_amt'] = 0;
                        $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                    } else {
                        $payment['loan_pri_amt'] = $value['d_principal'] + $temp;
                        $calc['_loan_pri_amt'] = $value['d_principal'] - $payment['loan_pri_amt'];
                        $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                    }
                } else {
                    $payment['loan_pri_amt'] = 0;
                    $calc['_loan_pri_amt'] = $value['d_principal'];
                    $calc['loan_os'] = $calc['cur_os'] - $payment['loan_pri_amt'];
                }

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

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

                $calc['ex'] = $calc['loan_os'];

                if ($value['due_by'] != "1") {

                    if ($temp > 0) {
                        $temp = $temp - $calc['loan_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'];
                    }

                    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'];
                    }
                } else {
                    $payment['os_pay'] = 0;
                    $calc['loan_os'] = $calc['ex'];
                    $payment['f_os_pay'] = 0;
                    $calc['cur_f_os'] = $calc['f_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'])->where('d_month', $value['month'])->where('d_year', $value['year'])->update('due_by', $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'] = $due['due_by'] + $temp;
                    $payment['due_by'] = $temp;
                    // $data = array('due_by' => $calc['due_by']);
                    // }
                } else {
                    $payment['due_by'] = 0;
                }

                $due['old_due_int'] = $calc['_loan_int_amt'] + $calc['_last_int'];
                $due['old_p_int'] = $calc['_p_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['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['payment_month'] = $value['month'];
                $payment['payment_year'] = $value['year'];
                $payment['paid_on'] = $added_on;
                $payment['cash_type'] = $cash_type;
                $payment['cheque_trans_id'] = $cheque_trans_id;

                $payment['paid_by'] = $_SESSION['name'];
                // echo "<pre>";
                // print_r($payment);
                // print_r($due);
                // print_r($value);
                // 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_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'],   'old_thrift' => $due['old_thrift'],

                $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'] < $value['d_principal']) {
                    $calc['loan_principle_amt'] = $calc['loan_os'];
                } else {
                    $calc['loan_principle_amt'] = $value['d_principal'];
                }
                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);

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

            $_SESSION['mem_add'] = "Demand Recovered Successfully";
            redirect('Demand/specific_reverse', 'refresh');
        }
        $data['content'] = 'demand/preverse';
        $this->load->view('layout/main', $data);
    }


    //Due Member
    public function overdue()
    {
        $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();
        if (isset($inps['division'])) {
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_demand($inps);

            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        // print_r($data);die();
        $data['content'] = 'demand/overdue';
        $this->load->view('layout/main', $data);
    }
    public function payment_diff()
    {
        $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();
        if (isset($inps['division'])) {
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_demand_pay($inps);

            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
        }
        // print_r($data);die();
        $data['content'] = 'demand/payment_diff';
        $this->load->view('layout/main', $data);
    }
    public function transfer_list()
    {
        $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();
        $this->load->model('demand_model');
        $data['members'] = $this->demand_model->get_member_transfer($inps);
        $data['division'] = $inps['division'];
        $data['section'] = $inps['section'];
        $data['year'] = $inps['year'];
        $data['month'] = $inps['month'];

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

    public function recovery_amount()
    {
        $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->demand_model->get_member_recovery($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Not Recoverd</div>';
                redirect('Demand/recovery_amount', 'refresh');
            } else {
                $data['members'] = $this->demand_model->get_member_recovery_demand($inps);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            // print_r($data['members']);die;
        }
        $data['content'] = 'demand/recover_amount';
        $this->load->view('layout/main', $data);
    }
    public function p_pay_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();
        $inps = $this->input->post();

        if (isset($inps['division'])) {
            if (($inps['month'] == "" || $inps['year'] == "") && $inps['added_on'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select month and year Or Date</div>';
                redirect('Demand/p_pay_details', 'refresh');
            }
            $data['members'] = $this->demand_model->get_partial_payment($inps);
            if (count($data['members']) <= 0) {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Record Found</div>';
                redirect('Demand/p_pay_details', 'refresh');
            }
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_partial_payment($inps);
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['added_on'] = $inps['added_on'];
            //   print_r($data);die();
        }
        $data['content'] = 'demand/p_pay_details';
        $this->load->view('layout/main', $data);
    }

    public function od_member()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $inps = $this->input->post();

        if (isset($inps['division']) && !isset($inps['postvalue'])) {
            if ($inps['month'] == "" || $inps['year'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select Month and Year</div>';
                redirect('Demand/od_member', 'refresh');
            }
            $demand_res = $this->demand_model->get_od_member_demand_dues($inps);
            if ($demand_res == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Demand Not Generated</div>';
                redirect('Demand/od_member', 'refresh');
            }
            // print_r($inps);die;

            $data['members'] = $this->demand_model->get_member_demand($inps);
            // print_r( $data['members']);die;


            $data['address'] = "";
            if ($inps['division'] != "") {
                $data['address'] = $this->member_model->get_division_address($inps['division']);
            }
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['month_days'] = $inps['month_days'];
        }
        $data['content'] = 'demand/od_member';
        $this->load->view('layout/main', $data);
    }

    public function dcp()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $inps = $this->input->post();

        if (isset($inps['division']) && !isset($inps['postvalue'])) {
            $data['demand'] = $this->demand_model->get_ledger_demand($inps);
            $data['recover'] = $this->demand_model->get_ledger_recovery($inps);
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['month_days'] = $inps['month_days'];
        }
        $data['content'] = 'demand/ledger_balance';
        $this->load->view('layout/main', $data);
    }

    public function circular_sms()
    {
        $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();
        $members_data = array();
        $inps = $this->input->post();
        if (isset($inps['division'])) {
            if (trim($inps['message']) == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Message cannot be empty</div>';
                redirect('Demand/circular_sms', 'refresh');
            }
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_members_byf($inps);
            // print_r($data['members']);die();
            foreach ($data['members'] as $mem) {
                if ($mem->mem_mob != "" && $mem->mem_mob != "0") {
                    $curl = curl_init();

                    curl_setopt_array($curl, array(
                        CURLOPT_URL => "http://site.ping4sms.com/api/smsapi?key=4fa16c52ad6cd8eb256405997cd086ca&route=2&sender=SOCITY&number=" . $mem->mem_mob . "&sms=" . urlencode($inps['message']),
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_ENCODING => "",
                        CURLOPT_MAXREDIRS => 10,
                        CURLOPT_TIMEOUT => 30,
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                        CURLOPT_CUSTOMREQUEST => "GET",
                        CURLOPT_HTTPHEADER => array(
                            "cache-control: no-cache",
                            "postman-token: a340e1a5-09ae-89bc-c39f-fd7f50f28fb7"
                        ),
                    ));

                    $response = curl_exec($curl);
                    $err = curl_error($curl);

                    curl_close($curl);
                }
            }
            $_SESSION['mem_add'] = "Circular send successfully";
            redirect('Demand/circular_sms', 'refresh');
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['mem_no'] = $inps['mem_no'];
        }
        $data['content'] = 'demand/circular_sms';
        $this->load->view('layout/main', $data);
    }
    public function receipt_sms()
    {
        $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($inps['division'])) {
            if ($inps['month'] == "" || $inps['year'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select month and year</div>';
                redirect('Demand/sub_day_book', 'refresh');
            }
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_payment($inps);
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['added_on'] = $inps['added_on'];
            // print_r($data['members']);die();
            foreach ($data['members'] as $mem) {
                if ($mem->phone != "" && $mem->phone != "0") {
                    $message = $mem->mem_name . " (" . $mem->me_no . ") " . $mem->payment_month . "'" . $mem->payment_year . " Ã Â®Å¡Ã Â®Â®Ã Â¯ÂÃ Â®ÂªÃ Â®Â³Ã Â®Â¤Ã Â¯ÂÃ Â®Â¤Ã Â®Â¿Ã Â®Â²Ã Â¯Â Ã Â®Â°Ã Â¯â€š." . $mem->tot_pay . " Ã Â®Å¡Ã Â¯â€ Ã Â®Â²Ã Â¯ÂÃ Â®Â¤Ã Â¯ÂÃ Â®Â¤Ã Â®Â¿Ã Â®Â¯Ã Â¯ÂÃ Â®Â³Ã Â¯ÂÃ Â®Â³Ã Â¯â‚¬Ã Â®Â°Ã Â¯ÂÃ Â®â€¢Ã Â®Â³Ã Â¯Â. Ã Â®Å¡Ã Â®Â¿.Ã Â®Å¡Ã Â¯â€¡:" . $mem->thrift_amt . " Ã Â®â€¦Ã Â®Å¡Ã Â®Â²Ã Â¯Â:" . $mem->loan_pri_amt . " Ã Â®Â¤Ã Â®ÂµÃ Â®Â£Ã Â¯Ë†:0/0 Ã Â®ÂµÃ Â®Å¸Ã Â¯ÂÃ Â®Å¸Ã Â®Â¿:" . $mem->loan_int_amt .  " Ã Â®Â°Ã Â®Å¡Ã Â¯â‚¬Ã Â®Â¤Ã Â¯ÂÃ Â®Å½Ã Â®Â£Ã Â¯Â:" . $mem->rec_no . " Ã Â®Â¤Ã Â¯â€ Ã Â®Â©Ã Â¯ÂÃ Â®Â©Ã Â¯â€šÃ Â®Â°Ã Â¯Â Ã Â®â€¡Ã Â®ÂªÃ Â®Â¿ Ã Â®Å¡Ã Â¯Å Ã Â®Å¡Ã Â¯Ë†Ã Â®Å¸Ã Â¯ÂÃ Â®Å¸Ã Â®Â¿. Ã Â®â€¡Ã Â®â„¢Ã Â¯ÂÃ Â®â€¢Ã Â¯â€¡ Ã Â®â€¢Ã Â®Â¿Ã Â®Â³Ã Â®Â¿Ã Â®â€¢Ã Â¯Â Ã Â®Å¡Ã Â¯â€ Ã Â®Â¯Ã Â¯ÂÃ Â®Â¯Ã Â®ÂµÃ Â¯ÂÃ Â®Â®Ã Â¯Â " . base_url() . "index.php/Demand/reciept?id=" . $mem->payment_id; //" Ã Â®Âª.Ã Â®â€¢Ã Â®Å¸Ã Â®Â©Ã Â¯Â:" . $mem->f_pri . " Ã Â®ÂµÃ Â®Å¸Ã Â¯ÂÃ Â®Å¸Ã Â®Â¿:" . $mem->f_int .
                    // print_r($message);die();
                    $curl = curl_init();

                    curl_setopt_array($curl, array(
                        CURLOPT_URL => "http://site.ping4sms.com/api/smsapi?key=4fa16c52ad6cd8eb256405997cd086ca&route=2&sender=SOCITY&number=" . $mem->phone . "&sms=" . urlencode($message),
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_ENCODING => "",
                        CURLOPT_MAXREDIRS => 10,
                        CURLOPT_TIMEOUT => 30,
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                        CURLOPT_CUSTOMREQUEST => "GET",
                        CURLOPT_HTTPHEADER => array(
                            "cache-control: no-cache",
                            "postman-token: a340e1a5-09ae-89bc-c39f-fd7f50f28fb7"
                        ),
                    ));

                    $response = curl_exec($curl);
                    $err = curl_error($curl);

                    curl_close($curl);
                }
            }
            $_SESSION['mem_add'] = "SMS send successfully";
            redirect('Demand/receipt_sms', 'refresh');
            //   print_r($data);die();
        }
        $data['content'] = 'demand/receipt_sms';
        $this->load->view('layout/main', $data);
    }

    // public function send_mail()
    // {

    //     $data['divisions'] = $this->member_model->get_division_mail_id();

    //     foreach ($data['divisions'] as $div) {
    //         // print_r($div['division_mail']);die;

    //         $from_email = "vigneshaliesvicky@gmail.com";
    //         $to_email = $div['division_mail'];

    //         //Load email library
    //         $this->load->library('email');
    //         $this->email->from($from_email, 'Identification');
    //         $this->email->to($to_email);
    //         $this->email->subject('This Month Demad Details');
    //         $this->email->message('https://brightecs.com/ym76/index.php/Demand/demand_generate');
    //         //Send mail
    //         if ($this->email->send()) {
    //             $_SESSION['mem_add'] = '<div class="alert alert-success">Mail send successfully..!</div>';
    //             redirect('Demand/demand_generate', 'refresh');
    //         } else {
    //             $_SESSION['mem_add'] = '<div class="alert alert-danger">Cannot send the mail ...!</div>';
    //             redirect('Demand/demand_generate', 'refresh');
    //         }
    //     }
    //     $data['content'] = 'demand/generate';
    //     $this->load->view('layout/main', $data);
    // }

    function send_mail()
    {
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'vigneshaliesvicky@gmail.com', // change it to yours
            'smtp_pass' => 'VigneshMarch@05', // change it to yours
            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );

        $data['divisions'] = $this->member_model->get_division_mail_id();

        $this->load->config('email');
        $this->load->library('email');

        foreach ($data['divisions'] as $div) {
            $from_email = "vigneshaliesvicky@gmail.com";
            $to_email = $div['division_mail'];

            $from = $this->config->item('smtp_user');
            $to =  $to_email;
            $subject = 'This Month Demad Details';
            $message = 'https://brightecs.com/ym76/index.php/Demand/demand_generate';

            $this->email->set_newline("\r\n");
            $this->email->from($from);
            $this->email->to($to);
            $this->email->subject($subject);
            $this->email->message($message);

            if ($this->email->send()) {
                echo 'Email has been sent successfully';
            } else {
                show_error($this->email->print_debugger());
            }
        }
        $data['content'] = 'demand/generate';
        $this->load->view('layout/main', $data);
    }

    public function receipt_mail()
    {
        $this->load->library('M_pdf');
        $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();
        $members_data = array();
        $inps = $this->input->post();
        if (isset($inps['division'])) {
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['mem_no'] = $inps['mem_no'];
            $data['month'] = $inps['month'];
            $data['year'] = $inps['year'];
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_demand($inps);
            print_r($data['members']);
            die;
            $data['address'] = "";
            if ($inps['division'] != "") {
                $data['address'] = $this->member_model->get_division_address($inps['division']);
            }
            // 		print_r($data);die();
            $html = $this->load->view('demand/pdf', $data, true); //load the pdf.php by passing our data and get all data in $html varriable.

            $pdfFilePath = getcwd() . "/assets/dpdf/webpreparations-" . time() . ".pdf";

            //actually, you can pass mPDF parameter on this load() function
            $pdf = $this->m_pdf->load();
            //generate the PDF!
            $stylesheet = '<style>' . file_get_contents('https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css') . '</style>';
            // apply external css
            $pdf->WriteHTML($stylesheet, 1);
            $pdf->WriteHTML($html, 2);
            //offer it to user via browser download! (The PDF won't be saved on your server HDD)
            //$pdfFilePath
            $pdf->Output();
            die();
            $_SESSION['mem_add'] = "Receipt send successfully";
            redirect('Demand/receipt_mail', 'refresh');
        }
        $data['content'] = 'demand/remail';
        $this->load->view('layout/main', $data);
    }
    public function fl_list()
    {
        $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();

        $this->load->model('demand_model');
        $data['members'] = $this->demand_model->get_member_fl_list($inps);
        $data['division'] = $inps['division'];
        $data['section'] = $inps['section'];
        $data['year'] = $inps['year'];
        $data['month'] = $inps['month'];

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

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

        $this->load->model('demand_model');
        $data['members'] = $this->demand_model->get_member_ml_list($inps);
        $data['division'] = $inps['division'];
        $data['section'] = $inps['section'];
        $data['year'] = $inps['year'];
        $data['month'] = $inps['month'];

        $data['content'] = 'demand/ml_list';
        $this->load->view('layout/main', $data);
    }
    public function not_recovered()
    {
        $data['content'] = 'demand/not_recovered';
        $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();
        $this->load->view('layout/main', $data);
    }


    public function annual()
    {
        $this->load->model('member_model');
        $data['member'] = $this->member_model->get_members();
        $data['content'] = 'ledger/annual';
        $this->load->view('layout/main', $data);
    }
    public function ledger()
    {
        $me_no = $this->input->post('mem_no');
        $years = $this->input->post('years');
        $years = explode('-', $years);
        $data['years'] = $years;
        $this->load->model('member_model');
        $data['member'] = $this->member_model->get_ledger_member($me_no);
        $data['loan'] = $this->member_model->get_loan($me_no);
        $data['smember'] = $this->member_model->get_loan1($data['loan']['surety_memno']);
        $data['demand'] = $this->member_model->get_demand($me_no);
        $data['thriftamt'] = $this->member_model->get_thriftamt($me_no);
        $data['shareamt'] = $this->member_model->get_shareamt($me_no);
        $data['payment'] = $this->member_model->get_payment($me_no);
        $data['content'] = 'ledger/ledger';
        $this->load->view('layout/main', $data);
    }
    public function dreceipt()
    {
        $this->load->model('member_model');
        $data['content'] = 'dreceipt/annual';
        $this->load->view('layout/main', $data);
    }
    public function dledger()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        // print_r($inps);die;

        $divsec = $this->db->select('member.division,member.section,sum(loan_int_amt) as loan_int_amt,sum(thrift_amt) as thrift_amt,sum(loan_pri_amt) as loan_pri_amt,sum(others) as others,paid_on')->from('payment')
            ->join('member', 'payment.me_no = member.me_no')->where('payment_month', $inps['month'])->where('payment_year', $inps['year'])->group_by('member.division')->group_by('member.section')->group_by('payment.payment_id')->get()->result_array();

        $data['divsec'] = $this->member_model->get_divisions($inps);


        // $data['divsec'] =  $divsec;
        //   print_r($data['divsec']);die;
        $data['content'] = 'dreceipt/annual';
        $this->load->view('layout/main', $data);
    }

    public function division_receipt()
    {
        $this->load->model('member_model');
        $data['content'] = 'division_receipt/annual';
        $this->load->view('layout/main', $data);
    }
    public function division_receipts()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        // print_r($inps);die;


        $data['divsec'] = $this->member_model->get_divisions_receipt($inps);


        // $data['divsec'] =  $divsec;
        //   print_r($data['divsec']);die;
        $data['content'] = 'division_receipt/annual';
        $this->load->view('layout/main', $data);
    }

    public function div_reciept()
    {
        $inps = $this->input->post();
        $data['division'] = $inps['division'];
        $data['section'] = $inps['section'];
        $data['total'] = $inps['total'];
        $data['paidon'] = $inps['paidon'];

        $data['thrift_amt'] = $inps['thrift_amt'];
        $data['loan_pri_amt'] = $inps['loan_pri_amt'];
        $data['loan_int_amt'] = $inps['loan_int_amt'];
        $data['others'] = $inps['others'];
        $data['share_amt'] = $inps['share_amt'];
        $data['books'] = $inps['books'];
        $data['ent_fees'] = $inps['ent_fees'];
        $data['due_by'] = $inps['due_by'];


        // print_r( $data);die;
        $this->load->view('dreceipt/view', $data);
    }
    public function bank_entry()
    {
        $data['content'] = 'bank_entry/index';
        $this->load->view('layout/main', $data);
    }
    public function get_members()
    {
        $data['d_month'] = $this->input->post('month');
        $data['d_year'] = $this->input->post('year');
        $data['demand'] = $this->db->select('*')->where('d_year', $data['d_year'])->where('d_month', $data['d_month'])->get("demand")->result_array();
        $demand = array_column($data['demand'], 'me_no');
        // print_r($demand);die;
        $data['name'] = $this->db->select('*')->where('me_no', $demand[1])->get('member')->result_array();
        // print_r($data['name']);die;
        $data['content'] = 'bank_entry/details';
        $this->load->view('layout/main', $data);
    }

    public function cash_group()
    {
        $this->load->model('member_model');
        $data['divisions'] = $this->member_model->division();
        $data['sections'] = $this->member_model->section();
        $data['common'] = $this->member_model->get_common();
        $inps = $this->input->post();
        //   print_r( $inps);die();

        if (isset($inps['division'])) {
            if (($inps['month'] == "" || $inps['year'] == "") && $inps['added_on'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">Select month and year Or Date</div>';
                redirect('Demand/cash_group', 'refresh');
            }
            $data['members'] = $this->demand_model->get_member_group($inps);
            if ($data['members'] == "") {
                $_SESSION['mem_add'] = '<div class="alert alert-danger">No Records Found</div>';
                redirect('Demand/cash_group', 'refresh');
            }
            $this->load->model('demand_model');
            $data['members'] = $this->demand_model->get_member_group($inps);
            $data['division'] = $inps['division'];
            $data['section'] = $inps['section'];
            $data['year'] = $inps['year'];
            $data['month'] = $inps['month'];
            $data['added_on'] = $inps['added_on'];
            //   print_r( $data['members']);die();
        }
        $data['content'] = 'cash_group/reciept';
        $this->load->view('layout/main', $data);
    }

    public function cash_grouping()
    {
        $inps = $this->input->post();
        $result = $this->db->query("select max(id) as id from cash_group");
        $member = $result->result();
        $data['id'] = $member[0]->id;
        $group_id = $data['id'] + 1;
        foreach ($inps['payment_id'] as $payid) {
            $update = array('group_id' => $group_id);
            $res = $this->db->where("payment_id", $payid)->update("payment", $update);
        }
        $insert = array('group_id' => $group_id, 'group_cash' => $inps['total_amount'], 'added_by' => $_SESSION["name"]);
        $grouping = $this->db->insert("cash_group", $insert);
        if ($inps != '') {
            echo "Grouping Successfully";
        } else {
            echo 1;
        }
    }
    public function print_group()
    {
        $inps = $this->input->post();

        if (isset($inps['added_on'])) {

            if ($inps != '') {
                $reverse_rec = $this->db->select('group_id,sum(loan_pri_amt) as loan_pri_amt,sum(share_amt) as share_amt,sum(thrift_amt) as thrift_amt,sum(insurance) as insurance,sum(loan_int_amt) as loan_int_amt')->where("paid_on", $inps['added_on'])->where("group_id !=", '0')->group_by('group_id')->get("payment")->result_array();
                // echo $this->db->last_query(); die;

            }
            $group_id = $reverse_rec['group_id'];
            $group = $this->db->select("*")->where_in("group_id", $group_id)->get("cash_group")->result_array();
            $data['group'] = $group;
            $data['payment'] = $reverse_rec;
        }
        //    print_r($reverse_rec);die();

        $data['content'] = 'cash_group/print';
        $this->load->view('layout/main', $data);
    }
    public function get_grouping_id()
    {
        $inps = $this->input->post();
        $this->load->model('member_model');
        $member = $this->demand_model->get_id($inps['id']);
        $html = '';
        // print_r($member); die;

        foreach ($member as $value) {
            // print_r($value); die;
            $html .= '<tr>
                                <td>' . $value->me_no . '</td>
                                <td>' . $value->mem_name . '</td>
                                <td>' . $total = $value->share_amt + $value->thrift_amt + $value->loan_pri_amt + $value->loan_int_amt + $value->f_pri + $value->f_int + $value->insurance . '</td>
                        </tr>';
            /* $total1 += $total; */
        }
        /* $html .= '<tr>
                                <td></td>
                                <td><b>Total</b></td>
                                <td>' . $total1 . '</td>
                               
                        </tr>'; */
        echo $html;
    }
    public function not_recovered_ims()
    {
        $inps = $this->input->post(); {
            // print_r($inps);die;
            $this->load->model('demand_model');
            $data['due_by'] = $this->demand_model->get_due_by($inps);
            foreach ($data['due_by'] as $value => $key) {
                $name = $this->db->select("*")->where("me_no", $key->me_no)->get("member")->result();
                foreach ($name as $value => $name1) {
                    $update = array("mem_name" => $name1->mem_name);
                    $this->db->where("me_no", $name1->me_no)->update("due_by", $update);
                }
                $value = $key->d_total - $key->tot_pay;
                $update = array("due_by" => $value);
                $data = $this->db->where("me_no", $key->me_no)->update("due_by", $update);
            }
            if ($inps['section'] == '') {
                $data = $this->db->select('*')->where('d_month', $inps['month'])->where('d_year', $inps['year'])->where('division', $inps['division'])->get('due_by')->result();
                // print_r($data['values']);die;
            } else  if ($inps['division'] != '' && $inps['section'] != '') {
                $data = $this->db->select('*')->where('d_month', $inps['month'])->where('d_year', $inps['year'])->where('division', $inps['division'])->where('section', $inps['section'])->get('due_by')->result();
            } else {
                $data = $this->db->select('*')->where('d_month', $inps['month'])->where('d_year', $inps['year'])->get('due_by')->result();
            }
            $data['value'] = $data;
        }
        $data['content'] = 'demand/not_recovered_display';
        $this->load->view('layout/main', $data);
    }

    public function download_database()
    {
        $this->load->dbutil();

        // Backup your entire database and assign it to a variable
        $backup = $this->dbutil->backup();

        // Load the file helper and write the file to your server
        write_file('/path/to/mybackup.gz', $backup);

        // Load the download helper and send the file to your desktop

        force_download('mybackup.gz', $backup);
    }
}
