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

class Demand_model extends CI_Model
{

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	//-----------------------------------------------------------------------------//
	//---------------------------------Demand Details------------------------------//
	//-----------------------------------------------------------------------------//
	public function get_demand($data)
	{
		$this->db->select("*");
		$this->db->join("member m", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where("d.is_deleted", 0);
		$this->db->where("m.is_deleted", 0);
		$this->db->where("d_month", $data['month']);
		$this->db->where("d_year", $data['year']);
		$res = $this->db->get("demand d")->result_array();
		return $res;
	}

	public function get_demand_pa($data)
	{
		$this->db->select("d.*");
		$this->db->join("member m", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where("d.is_deleted", 0);
		$this->db->where("m.is_deleted", 0);
		$this->db->where("d_month", $data['month']);
		$this->db->where("d_year", $data['year']);
		$res = $this->db->get("demand d")->result_array();
		return $res;
	}

	public function get_od_demand($data)
	{
		$this->db->select("*");
		$this->db->join("member m", "m.me_no=d.me_no");
		$this->db->join("old m", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where("d.is_deleted", 0);
		$this->db->where("m.is_deleted", 0);
		$this->db->where("d_month", $data['month']);
		$this->db->where("d_year", $data['year']);
		$res = $this->db->get("demand d")->result_array();
		return $res;
	}

	public function get_od_member_demand_dues($data)
	{
		$this->db->select('*');
		$this->db->join("demand d", "m.me_no=d.me_no");
		$this->db->join("old_dues o", "m.me_no=o.me_no");
		$this->db->join("thrift t", "m.me_no=t.me_no");
		$this->db->join("loan l", "m.me_no=l.me_no");

		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.d_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.d_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');

		// $this->db->order_by('m.division asc');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}

	public function get_member_loan_thrift_insurance_dues($data)
	{
		$this->db->select('*');
		$this->db->join("loan l", "m.me_no=l.me_no")->join("thrift t", "m.me_no=t.me_no")->join("share s", "m.me_no=s.me_no")->join("insurance i", "m.me_no=i.me_no")->join("others ot", "m.me_no=ot.me_no")->join("old_dues o", "m.me_no=o.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where('m.is_deleted', '0');
		if ($data['division'] != "" || $data['section'] != "") {
			$this->db->order_by('m.section asc');
		}
		$this->db->order_by('m.me_no asc');

		$result =   $this->db->get('member m')->result_array();
		//echo $this->db->last_query(); die();
		return $result;
	}

	public function get_olddues_details($me_no)
	{
		$res = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $me_no)->get("old_dues")->result_array();
		return $res[0];
	}
	public function get_thrift_details($me_no)
	{
		$res = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $me_no)->get("thrift")->result_array();
		return $res[0];
	}
	public function get_due_by_details($value)
	{
		$res = $this->db->select("due_by")->where("me_no", $value['me_no'])->get("old_dues")->result_array();
		return $res[0];
	}
	public function get_last_recover($data)
	{
		$this->db->select("paid_on,payment_month,payment_year");
		$this->db->join("member m", "m.me_no=p.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where('p.is_deleted', '0');
		$this->db->order_by("payment_id", "desc");
		$this->db->where('p.r_count <', 1);
		$this->db->limit(1);
		$result = $this->db->get("payment p")->result_array();
		return $result[0];
	}

	public function get_last_thrift_generate($data)
	{
		$this->db->select("p.*");
		$this->db->join("member m", "m.me_no=p.me_no");
		$this->db->where('m.me_no', $data['me_no']);
		$this->db->where('p.payment_year', $data['month']);
		$this->db->where('p.payment_month', $data['year']);
		$this->db->where('p.is_deleted', '0');
		$this->db->order_by("payment_id", "desc");
		$this->db->limit(1);
		$result = $this->db->get("payment p")->result_array();
		return $result[0];
	}

	public function update_payment($data)
	{
		$this->db->where("payment_id", $data['payment_id'])->update("payment", $data);
		return true;
	}

	public function update_member_demand_reverse($data)
	{
		$this->db->select("d_month,d_year");
		$this->db->join("member m", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		$this->db->where('d.is_deleted', '0');
		$this->db->order_by("d_id", "desc");
		$this->db->limit(1);
		$result = $this->db->get("demand d")->result_array();

		$this->db->select("me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		$this->db->where('m.is_deleted', '0');
		$mem_arr = $this->db->get("member m")->result_array();
		$mem_arr = array_column($mem_arr, "me_no");
		$update = array("is_deleted" => 1);

		if (count($result) > 0 && count($mem_arr) > 0) {

			$this->db->select("*");
			$this->db->join("member m", "m.me_no=p.me_no");
			$this->db->where_in('p.me_no', $mem_arr);
			$this->db->where('p.payment_month', $result[0]['d_month']);
			$this->db->where('p.payment_year', $result[0]['d_year']);
			$this->db->where('p.is_deleted', '0');
			$payment = $this->db->get("payment p")->result_array();

			if (count($payment) > 0) {
				return 1;
			}

			$this->db->where_in('d.me_no', $mem_arr);
			$this->db->where('d.d_month', $result[0]['d_month']);
			$this->db->where('d.d_year', $result[0]['d_year']);
			$this->db->where('d.is_deleted', '0');
			$this->db->update('demand d', $update);
			// $sql="update demand as d set is_deleted='1' join member as m on d.me_no=m.me_no where d_id!=''";
			// if($data['division']!="")
			// {
			// 	$sql.=" and m.division = '".$data['division']."'";
			// }
			// if($data['section']!="")
			// {
			// 	$sql.=" and m.section = '".$data['section']."'";
			// }
			// if($data['mem_no']!="")
			// {
			// 	$sql.=" and m.me_no = '".$data['mem_no']."'";
			// }
			// $sql.=" and d_month='".$result[0]['d_month']."' and d_year='".$result[0]['d_year']."'";
			// $this->db->query($sql);
		}
		return 0;
	}
	public function get_member_recovery($data)
	{
		$this->db->select('*');
		$this->db->join("payment d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.payment_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.payment_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.is_deleted', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	public function get_member_recovery_demand($data)
	{
		$this->db->select('*');
		$this->db->join("payment d", "m.me_no=d.me_no");
		$this->db->join("demand p", "m.me_no=p.me_no");

		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.payment_month', $data['month']);
			$this->db->where('p.d_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.payment_year', $data['year']);
			$this->db->where('p.d_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.is_deleted', '0');
		$this->db->order_by('m.me_no asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	public function get_p_payment($data)
	{
		$this->db->select('*');
		$this->db->join("payment d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.payment_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.payment_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.p_pay', '1');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	public function get_p_payment_details($data)
	{
		$this->db->select('*');
		$this->db->join("payment d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.payment_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.payment_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.p_pay', '1');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	public function get_last_demand($data)
	{
		$this->db->select("d.*");
		$this->db->join("member m", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where('d.is_deleted', '0');
		$this->db->order_by("d_id", "desc");
		$this->db->limit(1);
		$result = $this->db->get("demand d")->result_array();
		return $result[0];
	}
	public function insert_demand($data)
	{

		$this->db->insert("demand", $data);
		return true;
	}
	public function update_loan($data)
	{
		$this->db->where("me_no", $data['me_no'])->update("loan", $data);
		return true;
	}

	public function get_recover_check($data)
	{
		$this->db->select('*');
		$this->db->join("payment p", "m.me_no=p.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		$this->db->where('p.payment_month', $data['month']);
		$this->db->where('p.payment_year', $data['year']);
		$this->db->where('m.is_deleted', '0');
		$this->db->where('p.is_deleted', '0');
		$this->db->where('p.ent_fees', '0');
		$this->db->where('p.os_rno', '0');
		$this->db->where('p.p_pay', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return count($result);
	}
	public function get_loan_details($me_no)
	{
		$res = $this->db->select("*")->where("is_deleted", 0)->where("me_no", $me_no)->get("loan")->result_array();
		return $res[0];
	}
	public function get_maxrec_details()
	{
		$res = $this->db->query("select max(rec_no) as rec_no from payment")->result_array();
		return $res[0]['rec_no'];
	}

	public function get_member_payment($data)
	{
		$this->db->select("payment.*,member.mem_name as mem_name,member.mem_mob as phone");
		$this->db->join('payment', 'payment.me_no = member.me_no');
		if ($data['division'] != "") {
			$this->db->where('member.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('member.section', $data['section']);
		}
		if ($data['month'] != "") {
			$this->db->where("payment.payment_month='" . $data['month'] . "'");
		}
		if ($data['year'] != "") {
			$this->db->where("payment.payment_year='" . $data['year'] . "'");
		}
		if ($data['added_on'] != "") {
			$this->db->where('payment.paid_on', $data['added_on']);
		}
		$this->db->where('payment.is_deleted', '0');
		$this->db->order_by("member.me_no", "asc");
		$this->db->group_by('member.me_no');
		$this->db->group_by('member.mem_name');
		$this->db->group_by('member.mem_mob');
		$this->db->group_by('payment.payment_id');
		$result = $this->db->get('member')->result();
		// echo $this->db->last_query();die();
		return $result;
	}

	public function get_partial_payment($data)
	{
		$this->db->select('payment.*,member.mem_name as mem_name,member.mem_mob as phone');
		$this->db->from('payment');
		$this->db->join('member', 'payment.me_no = member.me_no');
		if ($data['section'] != "") {
			$this->db->where('member.section', $data['section']);
		}
		if ($data['division'] != "") {
			$this->db->where('member.me_no', $data['division']);
		}
		if ($data['month'] != "") {
			$this->db->where('payment.payment_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('payment.payment_year', $data['year']);
		}
		$this->db->where('payment.p_pay', '1');
		$this->db->where('payment.is_deleted', '0');
		$this->db->where('member.is_deleted', '0');
		$paymet =   $this->db->get()->result();
		return  $paymet;
	}
	public function get_od_member($data)
	{
		$this->db->select('*');
		$this->db->join("old_dues o", "m.me_no=o.me_no");
		$this->db->join("demand d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['month'] != "") {
			$this->db->where('d_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$result =   $this->db->get('member m')->result();
		return $result;
	}

	public function get_ledger_demand($inps)
	{
		$this->db->select('demand.d_month,demand.d_year,sum(d_principal) as demand_pri,sum(d_last_principal) as demand_last_principal,sum(d_loan_os) as demand_d_loan_os,sum(d_int) as demand_int,sum(d_last_int) as demand_last_int,sum(p_int) as demand_p_int,sum(demand.old_p_int) as demand_old_p_int,sum(d_thrift) as demand_thrift,sum(demand.old_thrift) as demand_old_thrift,sum(d_last_thrift) as old_thrift,sum(d_insurance) as demand_insurance,sum(demand.old_ins) as demand_old_ins,sum(d_others) as demand_others,sum(demand.old_others) as demand_old_others,sum(d_total) as demand_total');
		$this->db->from('demand');
		$this->db->join('member', 'demand.me_no = member.me_no');
		$this->db->join('old_dues', 'old_dues.me_no = member.me_no');
		$this->db->group_by('demand.d_month');
		$this->db->group_by('demand.d_year');
		if ($inps['division'] != "") {
			$this->db->where_in('member.division', $inps['division']);
		}
		if ($inps['section'] != "") {
			$this->db->where_in('member.section', $inps['section']);
		}
		$this->db->where_in('d_month', $inps['month']);
		$this->db->where_in('d_year', $inps['year']);
		// $this->db->where('member.is_deleted', '0');
		$demand =   $this->db->get()->result();
		return  $demand;
	}
	public function get_ledger_recovery($inps)
	{
		$this->db->select('payment.payment_month,payment.payment_year,sum(last_principal) as recover_last_principal,sum(ex_os) as recover_ex_os,sum(os_pay) as recover_os_pay,sum(last_int) as recover_last_int,sum(loan_pri_amt) as recover_pri,sum(loan_int_amt) as recover_int,sum(p_int) as recover_p_int,sum(thrift_amt) as recover_thrift,sum(old_thrift) as recover_old_thrift,sum(old_p_int) as recover_old_p_int,sum(old_others) as recover_old_others,sum(insurance) as recover_insurance,sum(others) as recover_others,sum(tot_pay) as recover_total');
		$this->db->from('payment');
		$this->db->join('member', 'payment.me_no = member.me_no');
		$this->db->group_by('payment.payment_month');
		$this->db->group_by('payment.payment_year');
		if ($inps['division'] != "") {
			$this->db->where_in('member.division', $inps['division']);
		}
		if ($inps['section'] != "") {
			$this->db->where_in('member.section', $inps['section']);
		}
		$this->db->where_in('payment_month', $inps['month']);
		$this->db->where_in('payment_year', $inps['year']);
		$this->db->where('p_pay', '0');
		// $this->db->where('member.is_deleted', '0');

		$payment =   $this->db->get()->result();
		return  $payment;
	}

	public function get_day_book_payment($data)
	{
		// print_r($data);die;
		$sql = 'select p.*,m.mem_name as mem_name,m.mem_mob as phone from payment p join member m on p.me_no=m.me_no where p.is_deleted="0"';
		if ($data['division'] != "") {
			$sql .= ' and m.division="' . $data['division'] . '"';
		}
		if ($data['section'] != "") {
			$sql .= ' and m.section="' . $data['section'] . '"';
		}
		if ($data['mem_no'] != "") {
			$sql .= ' and m.me_no="' . $data['mem_no'] . '"';
		}
		if ($data['year'] != "" && $data['month'] != "") {
			$sql .= ' and DATE_FORMAT(paid_on, "%M")="' . $data['month'] . '" and DATE_FORMAT(paid_on, "%Y")="' . $data['year'] . '"';
		} else {
			$sql .= ' and paid_on="' . $data['added_on'] . '"';
		}
		// 		print_r($sql);die();
		$result =   $this->db->query($sql)->result();
		return $result;
	}

	public function get_member_fl_list($data)
	{
		$this->db->select('*');
		$this->db->join("loan l", "m.me_no=l.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where('l.f_os >', 0);
		$this->db->where('m.is_deleted', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();

		// 		echo $this->db->last_query();die();
		return $result;
	}

	public function get_member_ml_list($data)
	{
		$this->db->select('*');
		$this->db->join("loan l", "m.me_no=l.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}
		$this->db->where('l.loan_os_amt >', 0);
		$this->db->where('m.is_deleted', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();

		// 		echo $this->db->last_query();die();
		return $result;
	}
	public function insert_due_by($up_due)
	{
		$this->db->insert("due_by", $up_due);
		return true;
	}
	public function get_due_by($inps)
	{
		$this->db->select('*');
		$this->db->where('d_month', $inps['month']);
		$this->db->where('d_year', $inps['year']);
		$query = $this->db->get('due_by')->result();
		return $query;
	}
	public function get_dues($inps)
	{
		$this->db->select('*');
		$this->db->where('d_month', $inps['month']);
		$this->db->where('d_year', $inps['year']);
		$this->db->where('division', $inps['division']);
		$this->db->where('section', $inps['section']);
		$query = $this->db->get('due_by')->result();
		return $query;
	}
	public function get_values($inps)
	{
		$this->db->select('me_no');
		$this->db->from('member');

		$this->db->where('member.division', $inps['division']);
		$this->db->where('member.section', $inps['section']);
		$query = $this->db->get()->result();
		return $query;
	}

	// public function get_member_not_recovered($ds){
	// 	$this->db->select('mem_name');
	// 	$this->db->select('me_no');
	//     $this->db->from('member');
	// 	$this->db->where('member.division',$ds['division']);
	// 	$this->db->where('member.section',$ds['section']); 
	// 	$result =   $this->db->get();
	// 	return $result->result();
	// }
	// public function get_member_not_recovered_demand($ym){
	// 	$this->db->select('d_total');
	//     $this->db->from('demand');
	// 	$this->db->where('demand.d_month',$ym['month']);
	// 	$this->db->where('demand.d_year',$ym['year']); 
	//     $query = $this->db->get();
	//     return $query->result();
	// }
	// public function get_member_not_recovered_payment($ym){
	// 	$this->db->select('tot_pay');
	//     $this->db->from('payment');
	// 	$this->db->where('payment.payment_month',$ym['month']);
	// 	$this->db->where('payment.payment_year',$ym['year']); 
	// 	$this->db->where('payment.paid_by','ym76'); 
	//     $query = $this->db->get();
	//     return $query->result();
	// }
	// public function get_dueyear($ym)
	// {
	// 	$this->db->select('*');
	//     $this->db->from('due_by');
	// 	$this->db->where('due_by.d_month',$ym['month']);
	// 	$this->db->where('due_by.d_year',$ym['year']); 
	// 	$result =   $this->db->get();
	// 	return $result->result();
	// }
	// public function get_dueby($ym)
	// {
	// 	$this->db->select('*');
	//     $this->db->from('due_by');
	// 	$this->db->where('due_by.d_month',$ym['month']);
	// 	$this->db->where('due_by.d_year',$ym['year']); 
	//     $query = $this->db->get();
	//     return $query->result_array();
	// }
	// public function get_member_not_recovered($data){
	// 	$this->db->select('*');
	// 	$this->db->join('demand',)
	// 	echo $this->db->last_query();die();
	// }
	// 					<!-- public function get_member_not_recovered($data)
	// 	{
	// 		$this->db->select('*');
	// 		$this->db->join("demand d", "m.me_no=d.me_no");
	// 		if ($data['division'] != "") {
	// 			$this->db->where('m.division', $data['division']);
	// 		}
	// 		if ($data['section'] != "") {
	// 			$this->db->where('m.section', $data['section']);
	// 		}
	// 		if ($data['me_no'] != "") {
	// 			$this->db->where('m.me_no', $data['me_no']);
	// 		}
	// 			if ($data['month'] != "") {
	// 			$this->db->where('d.d_month', $data['month']);
	// 		}
	// 		if ($data['year'] != "") {
	// 			$this->db->where('d.d_year', $data['year']);
	// 		}
	// 		$this->db->where('d.is_deleted', '0');
	// 		$this->db->where('m.is_deleted', '0');
	// 		$this->db->order_by('m.section asc');
	// 		$result =   $this->db->get('member m')->result_array();

	// 		$mnos=array_column($result,"me_no");

	// 			if(count($mnos)>0)
	// 			{
	// 		$this->db->select('*');
	// // 		$this->db->join("payment p", "m.me_no=p.me_no","left");

	// 			$this->db->where_in('p.me_no', $mnos);
	// 				// $this->db->where_in('m.me_no', $mnos);

	// 			if ($data['month'] != "") {
	// 			$this->db->where('p.payment_month', $data['month']);
	// 		}
	// 		if ($data['year'] != "") {
	// 			$this->db->where('p.payment_year', $data['year']);
	// 		}
	// 		$this->db->where('p.is_deleted', '0');
	// // 		$this->db->where('m.is_deleted', '0');
	// // 		$this->db->order_by('m.section asc');
	// 		$result =   $this->db->get('payment p')->result_array();
	// 			}else{
	// 			   	$result = array(); 
	// 			}

	// 				$mnos_p=array_column($result,"me_no");
	// 					// print_r($this->db->last_query());die();
	// 				if(count($mnos)>0)
	// 			{
	// 			    	$this->db->select('*');
	// 			    	$this->db->where_in('m.me_no', $mnos);
	// 			    	if(count($mnos_p)>0)
	// 			    	{
	// 			    	$this->db->where_not_in('m.me_no', $mnos_p);
	// 			    	}
	// 		// echo $this->db->last_query();die();

	// 				if ($data['division'] != "") {
	// 			$this->db->where('m.division', $data['division']);
	// 		}
	// 		if ($data['section'] != "") {
	// 			$this->db->where('m.section', $data['section']);
	// 		}
	// 		if ($data['me_no'] != "") {
	// 			$this->db->where('m.me_no', $data['me_no']);
	// 		}
	// 			$this->db->where('m.is_deleted', '0');
	// 		$this->db->order_by('m.section asc');
	// 		$result =   $this->db->get('member m')->result();
	// 			}else{
	// 			   	$result = array(); 
	// 			}
	// 		// echo $this->db->last_query();die();
	// 		return $result;
	// 	} -->
	public function get_member_savepayment_id($data)
	{
		$result = $this->db->where("payment_id", $data['id'])->update("payment", array("paid_on" => date("Y-m-d", strtotime($data['paid_on']))));
		return $result;
	}
	//overdue
	public function get_member_demand($data)
	{
		$this->db->select('*');
		$this->db->join("demand d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.d_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.d_year', $data['year']);
		}
		$this->db->where('d.is_deleted', '0');
		if ($data['division'] != "" || $data['section'] != "") {
			$this->db->order_by('m.section asc');
		}
		$this->db->order_by('m.me_no asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	public function particular_member_demand($data)
	{
		$this->db->select('*');
		$this->db->join("demand d", "m.me_no=d.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.d_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.d_year', $data['year']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		$this->db->where('d.d_month', $data['payment_month']);
		$this->db->where('d.d_year', $data['payment_year']);
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.is_deleted', '0');
		// $this->db->where('p.is_deleted', 1);
		$this->db->order_by('m.section asc');
		// echo $this->db->last_query();
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	//payment differ
	public function get_member_demand_pay($data)
	{
		$this->db->select('*');
		$this->db->join("demand d", "m.me_no=d.me_no");
		$this->db->join("payment p", "m.me_no=p.me_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		if ($data['month'] != "") {
			$this->db->where('d.d_month', $data['month']);
			$this->db->where('p.payment_month', $data['month']);
		}
		if ($data['year'] != "") {
			$this->db->where('d.d_year', $data['year']);
			$this->db->where('p.payment_year', $data['year']);
		}
		$this->db->where('m.is_deleted', '0');
		$this->db->where('d.is_deleted', '0');
		$this->db->where('p.is_deleted', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();
		return $result;
	}
	//transfer list
	public function get_member_transfer($data)
	{
		$this->db->select('m.*,t.division as division,t.section as section,t.date as added_date');
		$this->db->join("transfer t", "m.me_no=t.mem_no");
		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['me_no'] != "") {
			$this->db->where('m.me_no', $data['me_no']);
		}

		$this->db->where('m.is_deleted', '0');
		$this->db->order_by('m.section asc');
		$result =   $this->db->get('member m')->result();

		// 		echo $this->db->last_query();die();
		return $result;
	}
	public function get_members_byf($data)
	{
		$this->db->select("*");
		$this->db->join("payment p", "m.me_no=p.me_no");

		if ($data['division'] != "") {
			$this->db->where('m.division', $data['division']);
		}
		if ($data['section'] != "") {
			$this->db->where('m.section', $data['section']);
		}
		if ($data['mem_no'] != "") {
			$this->db->where('m.me_no', $data['mem_no']);
		}
		$this->db->where('m.is_deleted', 0);
		$this->db->where('p.is_deleted', 0);
		$this->db->where('p.payment_month', $data['payment_month']);
		$this->db->where('p.payment_year', $data['payment_year']);
		$this->db->where('p.r_count <', 1);
		$result = $this->db->get("member m")->result();
		// echo $this->db->last_query();die();
		return $result;
	}
	public function get_member_payment_id($data)
	{
		$sql = 'select p.*,l.*,m.*,t.*,s.*,m.division,m.section,m.mem_name as mem_name,m.mem_mob as phone,p.thrift_amt as thrift_amt from payment p join member m on p.me_no=m.me_no  join loan l on p.me_no=l.me_no join share s on p.me_no=s.me_no join thrift t on p.me_no= t.me_no where p.is_deleted="0"';
		$sql .= 'and p.payment_id="' . $data['id'] . '"';
		$result =   $this->db->query($sql)->result();
		return $result;
	}
	public function get_member_group($data)
	{
		$sql = 'select p.*,m.mem_name as mem_name,m.mem_mob as phone from payment p join member m on p.me_no=m.me_no where p.group_id="0"';
		if ($data['division'] != "") {
			$sql .= ' and m.division="' . $data['division'] . '"';
		}
		if ($data['section'] != "") {
			$sql .= ' and m.section="' . $data['section'] . '"';
		}
		if ($data['mem_no'] != "") {
			$sql .= ' and m.me_no="' . $data['mem_no'] . '"';
		}
		if ($data['year'] != "" && $data['month'] != "") {
			$sql .= ' and DATE_FORMAT(paid_on, "%M")="' . $data['month'] . '" and DATE_FORMAT(paid_on, "%Y")="' . $data['year'] . '"';
		} else {
			$sql .= ' and paid_on="' . $data['added_on'] . '"';
		}
		$result =   $this->db->query($sql)->result();
		// echo $this->db->last_query(); die;
		// print_r($result);die;

		return $result;
	}
	public function get_id($id)
	{
		$this->db->select('*');
		$this->db->where('group_id', $id);
		$result = $this->db->get('payment')->result_array();
		$mnos = array_column($result, "me_no");

		if (count($mnos) > 0) {
			$this->db->select('*');
			$this->db->from('payment');
			$this->db->join("member", "member.me_no=payment.me_no");
			$this->db->where_in('payment.me_no', $mnos);
			$this->db->where('group_id', $id);
			$query = $this->db->get()->result();
			return $query;
		}
	}
	function excel_insert($data)
	{
		$this->db->insert_batch('payment', $data);
	}

	public function get_dcp($inps)
	{
		$this->db->select('m.me_no,m.mem_name,p.payment_month,t.tot_thrift as cb,sum(p.thrift_amt) as amt');
		$this->db->join("loan l", "m.me_no=l.me_no")->join("thrift t", "m.me_no=t.me_no")->join("payment p", "m.me_no=p.me_no")->join("old_dues o", "m.me_no=o.me_no");
		$this->db->where('p.payment_year', $inps['year']);
		$this->db->group_by('m.me_no');
		$this->db->group_by('p.payment_month');

		// $this->db->order_by('m.section asc');

		$result =   $this->db->get('member m')->result_array();
		// 		echo $this->db->last_query();
		return $result;
	}
}
