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

class Reports_Loan extends CI_Controller
{

	var $main_menu_name = "reports_loan";
	var $sub_menu_name = "";

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


		$this->load->model('Loan_Payment_Received_Model');
		$this->load->model('Loan_Model');
		$this->load->model('Reports_Loan_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Bank_Model');
		$this->load->model('Common_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Menu_Model');
		$this->load->model('User_Model');
		$this->load->model('System_Settings_Model');
		$this->load->model('Unit_Model');
		$this->load->model('Menu_Items_List_Model');
		$this->load->model('Route_Model');
		$this->load->model('Report_Loan_Model');

		date_default_timezone_set('asia/colombo');
	}

	//undifine index
	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$this->load->view('loan', $data);
	}

	// ------------------------------------------------------------------------------------- new

	public function rep_loan_daily()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_loan_instalment";
		$this->load->view('rep_loan_instalment', $data);
	}



	public function get_loan_installment_summary($value = '')
	{
		$wareh_id = '';

		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Reports_Loan_Model->get_loan_installment_summary_cash($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		echo json_encode($values);
	}



	public function get_loan_installment_list($value = '')
	{
		$wareh_id = '';

		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Reports_Loan_Model->get_loan_installment_list_cash($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);



		$columns = array();
		$data = array();
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();

				$row[] = date('Y-m-d', strtotime($users->pymnt_date_time));
				$row[] = $users->loan_ref;
				$row[] = $users->payment_term_name;
				$row[] = $users->cus_name;
				$row[] = $users->route_name;
				$row[] = $users->user_first_name;
				$row[] = $users->capital_received;
				//$row[] = $users->loan_interest_recived;
				$row[] = $users->panalty_received;
				$row[] = $users->due_received;
				$row[] = $users->pymnt_amount;



				$data[] = $row;
			}
			$output = array('data' => $data);
			echo json_encode($output);
		} else {
			$output = array('data' => '');
			echo json_encode($output);
		}
	}



	//-------------------------------------------------------------


	//-------------------------------------------------------------
	public function rep_close_loan()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_close_loan";
		$this->load->view('rep_to_be_close_loan', $data);
	}



	public function get_close_loan_summary($value = '')
	{
		$wareh_id = '';
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Reports_Loan_Model->get_close_loan_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$panalty = $this->Reports_Loan_Model->get_close_loan_summary_panalty($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$due = $this->Reports_Loan_Model->get_close_loan_summary_due($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$paid = $this->Reports_Loan_Model->get_close_loan_paid_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);


		$capital_balance = $values['loan_tot_amount'] - $paid['capital_received'];
		$income = $paid['loan_interest_recived'] + $paid['due_received'] + $paid['panalty_received'];



		$net_income = $income - $capital_balance;


		$total_payble = $values['loan_payable_total_amount'] + $panalty['panalty_amount'] + $due['due_amount'];

		$data = array(
			'srh_from_date' => $srh_from_date,
			'srh_to_date' => $srh_to_date,
			'total_capital' => $values['loan_tot_amount'],
			'total_interest' => $values['total_interest'],
			'total_loan' => $values['loan_payable_total_amount'],
			'total_panalty_amount' => $panalty['panalty_amount'],
			'total_due_amount' => $due['due_amount'],

			'capital_received' => $paid['capital_received'],
			'loan_interest_recived' => $paid['loan_interest_recived'],
			'loan_received' => $paid['loan_received'],
			'paid_amount' => $paid['pymnt_amount'],
			'panalty_received' => $paid['panalty_received'],
			'due_received' => $paid['due_received'],

			'capital_balance' => $values['loan_tot_amount'] - $paid['capital_received'],
			'interest_balance' => $values['total_interest'] - $paid['loan_interest_recived'],
			'panalty_balance' => $panalty['panalty_amount'] - $paid['panalty_received'],
			'due_balance' => $due['due_amount'] - $paid['due_received'],

			'total_balane' => $total_payble - $paid['pymnt_amount'],
			'total_payble' => $total_payble,
			'net_income' => $net_income,

		);


		//print_r($values2);

		echo json_encode($data);
	}



	public function get_close_loan_list($value = '')
	{
		$this->load->model('Loan_Panalty_Process_Model');
		$this->load->model('Loan_Due_Process_Model');


		$wareh_id = '';
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Reports_Loan_Model->get_close_loan_list($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);



		$columns = array();
		$data = array();
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();


				if ($users->loan_payment_term == 4) {
					$pTname = $users->payment_term_name . " " . $users->loan_payment_term_qty . " days";
					if ($users->reduce_fron_ceque == 1) {
						$pTname = $users->payment_term_name . " ( Custom duration ) ";
					}
				} else {
					$pTname = $users->payment_term_name;
				}

				$panalty = $this->Loan_Panalty_Process_Model->get_sum_panalty_by_loan_id($users->loan_id);
				$due = $this->Loan_Due_Process_Model->get_loan_due_sum_by_loan_id($users->loan_id);
				$payment = $this->Loan_Payment_Received_Model->get_loan_payment_total($users->loan_id);


				if ($panalty['panalty_amount']) {
					$panalty_amont = $panalty['panalty_amount'];
				} else {
					$panalty_amont = 0;
				}

				if ($due['due_amount']) {
					$due_amont = $due['due_amount'];
				} else {
					$due_amont = 0;
				};

				//$row[] = date('Y-m-d', strtotime($users->loan_close_date));
				$row[] = date('Y-m-d', strtotime($users->system_close_date));
				$row[] = ' <a target="_blank" href="' . base_url() . 'loan/details/' . $users->loan_id . '">' . $users->loan_ref . '</a>';
				$row[] = $pTname . " (" . $users->loan_no_of_terms . ")";
				$row[] = $users->cus_name;
				$row[] = $users->route_name;
				$row[] = $users->loan_tot_amount;
				//$row[] = $users->total_interest;
				$row[] = $panalty_amont;
				$row[] = $due_amont;
				$total_payble_amount_loan = $users->loan_payable_total_amount + $panalty['panalty_amount'] + $due['due_amount'];
				$balance_amount = $total_payble_amount_loan - $payment['pymnt_amount'];
				$row[] = number_format($balance_amount, 2, '.', ',');;



				$data[] = $row;
			}
			$output = array('data' => $data);
			echo json_encode($output);
		} else {
			$output = array('data' => '');
			echo json_encode($output);
		}
	}
	//-------------------------------------------------------------

	//-------------------------------------------------------------
	//Loan wise summary report with details report
	public function rep_loan_wise_summary()
	{
		$data['loan_details'] = $this->Report_Loan_Model->get_all_loans();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_loan_wise_summary";
		$this->load->view('rep_collection_loan_summary', $data);
	}

	public function get_loan_installment_summary_loan_wise($value = '')
	{
		$wareh_id = '';

		$loan_id = $this->input->post('loan_id');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Report_Loan_Model->get_loan_installment_summary_cash_loan_wise($wareh_id, $loan_id, $srh_from_date, $srh_to_date);
		echo json_encode($values);
	}

	public function get_loan_installment_list_loan_wise($value = '')
	{
		$wareh_id = '';
		$loan_id = $this->input->post('loan_id');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Report_Loan_Model->get_loan_installment_list_loan_wise($wareh_id, $loan_id, $srh_from_date, $srh_to_date);
		$columns = array();
		$data = array();
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();
				$status = "";
				if ($users->pymnt_returned == 0) {
					$status = '<span class="label label-success">ok</span>';
				} else {
					$status = '<span class="label label-danger">returned</span>';
				}
				$row[] = date('Y-m-d', strtotime($users->pymnt_date_time));
				$row[] = $users->user_first_name;
				$row[] = $users->pymnt_amount;
				$row[] = $users->pymnt_amount;
				$row[] = $status;
				$data[] = $row;
			}
			$output = array('data' => $data);
			echo json_encode($output);
		} else {
			$output = array('data' => '');
			echo json_encode($output);
		}
	}
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	//after close date still not finish loan report
	public function rep_not_close_loan()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_not_close_loan";
		$this->load->view('rep_not_close_loan', $data);
	}

	public function get_not_close_loan_summary($value = '')
	{
		$wareh_id = '';
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Report_Loan_Model->get_not_close_loan_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$panalty = $this->Report_Loan_Model->get_not_close_loan_summary_panalty($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$due = $this->Report_Loan_Model->get_not_close_loan_summary_due($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$paid = $this->Report_Loan_Model->get_not_close_loan_paid_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$capital_balance = $values['loan_tot_amount'] - $paid['capital_received'];
		$income = $paid['loan_interest_recived'] + $paid['due_received'] + $paid['panalty_received'];
		$net_income = $income;
		$total_payble = $values['loan_payable_total_amount'] + $panalty['panalty_amount'] + $due['due_amount'];

		$data = array(
			'loan_tot_amount' => $values['loan_tot_amount'],
			'total_interest' => $values['total_interest'],
			'loan_payable_total_amount' => $values['loan_payable_total_amount'],
			'panalty_amount' => $panalty['panalty_amount'],
			'due_amount' => $due['due_amount'],
			'capital_received' => $paid['capital_received'],
			'loan_interest_recived' => $paid['loan_interest_recived'],
			'loan_received' => $paid['loan_received'],
			'panalty_received' => $paid['panalty_received'],
			'due_received' => $paid['due_received'],
			'pymnt_amount' => $paid['pymnt_amount'],
			'capital_balance' => $values['loan_tot_amount'] - $paid['capital_received'],
			'interest_balance' => $values['total_interest'] - $paid['loan_interest_recived'],
			'panalty_balance' => $panalty['panalty_amount'] - $paid['panalty_received'],
			'due_balance' => $due['due_amount'] - $paid['due_received'],
			'total_balane' => $total_payble - $paid['pymnt_amount'],
			'total_payble' => $total_payble,
			'net_income' => $net_income
		);
		echo json_encode($data);
	}

	public function get_not_close_loan_list($value = '')
	{
		$this->load->model('Loan_Panalty_Process_Model');
		$this->load->model('Loan_Due_Process_Model');
		$this->load->model('Holiday_Model');

		$wareh_id = '';
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}
		$values = $this->Report_Loan_Model->get_not_close_loan_list($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$columns = array();
		$data = array();
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();

				if ($users->loan_payment_term == 4) {
					$pTname = $users->payment_term_name . " " . $users->loan_payment_term_qty . " days";
					if ($users->reduce_fron_ceque == 1) {
						$pTname = $users->payment_term_name . " ( Custom duration ) ";
					}
				} else {
					$pTname = $users->payment_term_name;
				}
				$panalty = $this->Loan_Panalty_Process_Model->get_sum_panalty_by_loan_id($users->loan_id);
				$due = $this->Loan_Due_Process_Model->get_loan_due_sum_by_loan_id($users->loan_id);
				$payment = $this->Loan_Payment_Received_Model->get_loan_payment_total($users->loan_id);

				if ($panalty['panalty_amount']) {
					$panalty_amont = $panalty['panalty_amount'];
				} else {
					$panalty_amont = 0;
				}

				if ($due['due_amount']) {
					$due_amont = $due['due_amount'];
				} else {
					$due_amont = 0;
				}
				//--------------------------------------

				$date_different = '';
				$loan_start_date = $users->loan_date;
				$loan_close_date = $users->loan_close_date;
				$holiday_count = $this->Holiday_Model->get_holiday_count_between_loan_start_date_and_end_date($loan_start_date, $loan_close_date);
				if ($holiday_count) {
					$total_holidays = $holiday_count;
				} else {
					$total_holidays = 0;
				}
				$total_no_of_term = $users->loan_no_of_terms;
				$loan_total_days = $total_no_of_term + $total_holidays - 1;
				$extented_closing_date = date('Y-m-d', strtotime($loan_start_date . ' + ' . $loan_total_days . ' days'));


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

				if ($today > $extented_closing_date) {
					$date1 = date_create($users->system_close_date);
					$date2 = date_create($extented_closing_date);
					$diff = date_diff($date1, $date2);
					$date_different = $diff->format("%a");
					$extented_closing_date = "<span class='label label-danger'>" . $extented_closing_date . "</span>";
				} else {
					$extented_closing_date = "<span class='label label-success'>" . $extented_closing_date . "</span>";
				}


				//--------------------------------------------				
				//$row[] = date('Y-m-d', strtotime($users->loan_close_date));
				$row[] = $users->cus_code;
				$row[] = $users->cus_name;
				$row[] = date('Y-m-d', strtotime($users->loan_close_date));
				$row[] = '<a target="_blank" href="' . base_url() . 'loan/details/' . $users->loan_id . '">' . $users->loan_ref . '</a>';
				$row[] = $pTname . " (" . $users->loan_no_of_terms . ")";

				$total_payble_amount_loan = $users->loan_payable_total_amount + $panalty['panalty_amount'] + $due['due_amount'];
				$balance_amount = $total_payble_amount_loan - $payment['pymnt_amount'];
				$row[] = number_format($balance_amount, 2, '.', ',');



				$data[] = $row;
			}
			$output = array('data' => $data);
			echo json_encode($output);
		} else {
			$output = array('data' => '');
			echo json_encode($output);
		}
	}
	//-------------------------------------------------------------	

	//-------------------------------------------------------------
	//loan report given time period
	public function loan_report()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "loan_report";
		$this->load->view('rep_loan_report', $data);
	}


	public function get_loan_summary($value = '')
	{
		$srh_to_date = '';
		$srh_from_date = '';

		if ($this->session->userdata('ss_group_id') == 1 || $this->session->userdata('ss_group_id') == 2) {
			$wareh_id = '';
		} else {
			$wareh_id = $this->session->userdata('ss_warehouse_id');
		}
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}

		$values = $this->Report_Loan_Model->get_loan_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$panalty = $this->Report_Loan_Model->get_loan_summary_panalty($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$due = $this->Report_Loan_Model->get_loan_summary_due($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$paid = $this->Report_Loan_Model->get_loan_paid_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$capital_balance = $values['loan_tot_amount'] - $paid['capital_received'];
		$income = $paid['loan_interest_recived'] + $paid['due_received'] + $paid['panalty_received'];
		$net_income = $income;
		$total_payble = $values['loan_payable_total_amount'] + $panalty['panalty_amount'] + $due['due_amount'];

		$data = array(
			'loan_tot_amount' => $values['loan_tot_amount'],
			'total_interest' => $values['total_interest'],
			'loan_payable_total_amount' => $values['loan_payable_total_amount'],
			'panalty_amount' => $panalty['panalty_amount'],
			'due_amount' => $due['due_amount'],
			'capital_received' => $paid['capital_received'],
			'loan_interest_recived' => $paid['loan_interest_recived'],
			'loan_received' => $paid['loan_received'],
			'panalty_received' => $paid['panalty_received'],
			'due_received' => $paid['due_received'],
			'pymnt_amount' => $paid['pymnt_amount'],
			'capital_balance' => $values['loan_tot_amount'] - $paid['capital_received'],
			'interest_balance' => $values['total_interest'] - $paid['loan_interest_recived'],
			'panalty_balance' => $panalty['panalty_amount'] - $paid['panalty_received'],
			'due_balance' => $due['due_amount'] - $paid['due_received'],
			'total_balane' => $total_payble - $paid['pymnt_amount'],
			'total_payble' => $total_payble,
			'net_income' => $net_income
		);
		echo json_encode($data);
	}

	public function get_loan_list($value = '')
	{
		$this->load->model('Loan_Panalty_Process_Model');
		$this->load->model('Loan_Due_Process_Model');
		$wareh_id = '';
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->post('srh_warehouse_id');
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');

		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}


		$start            = $this->input->post('start');
		$length           = $this->input->post('length');
		$search           = $this->input->post('search');



		$values = $this->Report_Loan_Model->currant_operation_loan_list($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date, $start, $length, $search['value']);


		$count = $this->Report_Loan_Model->currant_operation_loan_list($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date, "", "", $search['value']);
		$totalData = $count;
		$totalFiltered = $totalData;


		if ($values) {

			$columns = array();
			$data = array();
			//list panalty array start

			//-------------------------------------------------------------
			foreach ($values as $users) {
				$row = array();
				$loan_id = $users->loan_id;
				$payment_term = $users->loan_payment_term;
				$oustanding_value = 0;
				$should_be_received_value = 0;
				$total_paid_amount = 0;
				$a = 0;
				$total_paid_amount = $this->Loan_Model->get_total_paid_by_loan_id($loan_id);
				$total_panalty = $this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
				$total_due = $this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);
				$total_paid_panalty = $this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
				$total_paid_due = $this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
				$total_paid_loan = $this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);
				//$last_paid=$this->Loan_Payment_Received_Model->get_last_paid_loan_by_loan_id($loan_id);					
				$today = date('Y-m-d');
				$cosingl_date = date('Y-m-d', strtotime($users->loan_close_date));
				//calcilation start ------------------------------------------------------------------
				$data2 = array(
					'loan_id' => $loan_id,
					'payment_term' => $payment_term,
					'total_paid_amount' => $total_paid_amount,
					'total_panalty' => $total_panalty,
					'total_due' => $total_due,
					'total_paid_panalty' => $total_paid_panalty,
					'total_paid_due' => $total_paid_due,
					'total_paid_loan' => $total_paid_loan,
					'users' => $users
				);
				$oustanding_value = common_outstanding_calculation($data2);

				//print_r($oustanding_value);					
				// calcilation End ------------------------------------------------------------------
				if ($total_panalty['panalty_amount'] > 0) {
					$panalty_charge = $total_panalty['panalty_amount'];
				} else {
					$panalty_charge = 0;
				}
				if ($total_due['due_amount'] > 0) {
					$due_charge = $total_due['due_amount'];
				} else {
					$due_charge = 0;
				}
				if ($total_paid_panalty['panalty_received'] > 0) {
					$paid_panalty_charge = $total_paid_panalty['panalty_received'];
				} else {
					$paid_panalty_charge = 0;
				}
				if ($total_paid_due['due_received'] > 0) {
					$paid_due_charge = $total_paid_due['due_received'];
				} else {
					$paid_due_charge = 0;
				}
				if ($total_paid_loan['loan_received'] > 0) {
					$paid_loan_only = $total_paid_loan['loan_received'];
				} else {
					$paid_loan_only = 0;
				}
				if (isset($last_paid['pymnt_amount'])) {
					$last_paid = $last_paid['pymnt_amount'];
				} else {
					$last_paid = 0;
				}
				$panalty_charge_total = 0;
				//$last_panalty_details=$this->Loan_Payment_Received_Model->get_last_panalty_by_loan_id($loan_id);			
				//$panalty=$this->Panalty_Model->get_panalty_charge();

				$panalty_charge_total = $panalty_charge;

				if ($users->loan_payment_term == 4) {
					$pTname = $users->payment_term_name . " " . $users->loan_payment_term_qty . " days";
					if ($users->reduce_fron_ceque == 1) {
						$pTname = $users->payment_term_name . " ( Custom duration ) ";
					}
				} else {
					$pTname = $users->payment_term_name;
				}


				$due_balance = $due_charge - $paid_due_charge;
				$panlty_balance = $panalty_charge_total - $paid_panalty_charge;
				$total_need_to_pay = $users->loan_payable_total_amount + $panalty_charge_total + $due_charge;

				if ($cosingl_date < $today) {
					$close_status = '<span class="label label-yellow">' . date('Y-m-d', strtotime($users->loan_close_date)) . '</span>';
					// $last_outstanding=$oustanding_value+$due_balance+$panlty_balance;

				} else {

					$close_status = '<span class="label label-success">' . date('Y-m-d', strtotime($users->loan_close_date)) . '</span>';
					//$last_outstanding=$users->loan_payable_total_amount-$paid_loan_only;
				}


				$row_action = '<div class="btn-group text-left">                       
					 <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                       <ul role="menu" class="dropdown-menu pull-right">
                           <li><a href="' . base_url() . 'loan/details/' . $users->loan_id . '"><i class="fa fa-file-text"></i> Loan Details</a></li>
							<li><a href="#" onClick="reject(' . $users->loan_id . ')"><i class="fa fa-ban"></i> Reject</a></li>
							<li><a href="#" onClick="delete_loan(' . $users->loan_id . ')"><i class="fa fa-trash-o"></i> Delete</a></li>
							 <li><a href="' . base_url() . 'loan/loan_modify/' . $users->loan_id . '"><i class="fa fa-edit"></i> Update Loan Details</a></li>
							 
							  <li><a href="' . base_url() . 'management/loan_details/' . $users->loan_id . '"><i class="fa fa-edit"></i> Manage Loan payment & Panalty & Due</a></li>
									
                       </ul></div>';




				$row[] =  ' <a  id="tr_' . $users->loan_id . '" onclick="change_color(' . $users->loan_id . ')" target="_blank" href="' . base_url() . 'loan/details/' . $users->loan_id . '">' . $users->loan_ref . '</a>';
				$row[] = $users->cus_name;
				$row[] = $users->route_name;
				$row[] = date('Y-m-d', strtotime($users->loan_date));
				$row[] = $close_status;
				//$row[] = $pTname;
				$row[] = $users->loan_no_of_terms;
				$row[] = number_format($users->loan_tot_amount, 2, '.', ',');;


				$row[] = number_format($users->loan_term_amount, 2, '.', ',');;
				$row[] = number_format($oustanding_value, 2, '.', ',');
				//$row[] = $users->loan_tot_amount;	
				$row[] = number_format($paid_loan_only, 2, '.', ',');
				$row[] = number_format($users->loan_payable_total_amount - $paid_loan_only, 2, '.', ',');
				//$row[] = number_format($users->loan_payable_total_amount, 2, '.', ',');
				//$row[]=$row_action;	
				$row[] = $users->cus_address;
				$row[] = $users->cus_mobile;
				$row[] = $row_action;
				$data[] = $row;
			}

			//-------------------------------------------------------------
			$output = array(
				//"draw"            => intval( $requestData['draw'] ),  
				"recordsTotal"    => intval($totalData),
				"recordsFiltered" => intval($totalFiltered),
				"data"            => $data
			);
			echo json_encode($output);
		} else {
			$output = array(
				//"draw"            => intval( $requestData['draw'] ),  
				"recordsTotal"    => intval(0),
				"recordsFiltered" => intval(0),
				"data"            => ""
			);
			echo json_encode($output);
		}
	}

	//-----------------------------------------------------------
	public function loan_report_print()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "loan_report";

		//print data start

		$this->load->model('Loan_Panalty_Process_Model');
		$this->load->model('Loan_Due_Process_Model');
		$wareh_id = '';
		$srh_to_date = '';
		$srh_from_date = '';
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		$route_id = $this->input->get('route_id');
		$loan_payment_term = $this->input->get('loan_payment_term');


		if ($this->input->get('route_id')) {
		} else {
			$route_id = -1;
		}


		if ($this->input->get('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->get('srh_to_date')));
		}
		if ($this->input->get('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->get('srh_from_date')));
		}
		$start            = 1;
		$length           = 1000;
		$search           = "";


		$values = $this->Report_Loan_Model->currant_operation_loan_list($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date, $start, $length, $search);



		if ($values) {
			$columns = array();
			$data = array();
			//list panalty array start	     
			//-------------------------------------------------------------
			foreach ($values as $users) {
				$row = array();
				$loan_id = $users->loan_id;
				$payment_term = $users->loan_payment_term;
				$oustanding_value = 0;
				$should_be_received_value = 0;
				$total_paid_amount = 0;
				$a = 0;
				$total_paid_amount = $this->Loan_Model->get_total_paid_by_loan_id($loan_id);
				$total_panalty = $this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
				$total_due = $this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);
				$total_paid_panalty = $this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
				$total_paid_due = $this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
				$total_paid_loan = $this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);
				//$last_paid=$this->Loan_Payment_Received_Model->get_last_paid_loan_by_loan_id($loan_id);					
				$today = date('Y-m-d');
				$cosingl_date = date('Y-m-d', strtotime($users->loan_close_date));
				//calcilation start ------------------------------------------------------------------
				$data2 = array(
					'loan_id' => $loan_id,
					'payment_term' => $payment_term,
					'total_paid_amount' => $total_paid_amount,
					'total_panalty' => $total_panalty,
					'total_due' => $total_due,
					'total_paid_panalty' => $total_paid_panalty,
					'total_paid_due' => $total_paid_due,
					'total_paid_loan' => $total_paid_loan,
					'users' => $users
				);
				$oustanding_value = common_outstanding_calculation($data2);
				// calcilation End ------------------------------------------------------------------
				if ($total_panalty['panalty_amount'] > 0) {
					$panalty_charge = $total_panalty['panalty_amount'];
				} else {
					$panalty_charge = 0;
				}
				if ($total_due['due_amount'] > 0) {
					$due_charge = $total_due['due_amount'];
				} else {
					$due_charge = 0;
				}
				if ($total_paid_panalty['panalty_received'] > 0) {
					$paid_panalty_charge = $total_paid_panalty['panalty_received'];
				} else {
					$paid_panalty_charge = 0;
				}
				if ($total_paid_due['due_received'] > 0) {
					$paid_due_charge = $total_paid_due['due_received'];
				} else {
					$paid_due_charge = 0;
				}
				if ($total_paid_loan['loan_received'] > 0) {
					$paid_loan_only = $total_paid_loan['loan_received'];
				} else {
					$paid_loan_only = 0;
				}
				if (isset($last_paid['pymnt_amount'])) {
					$last_paid = $last_paid['pymnt_amount'];
				} else {
					$last_paid = 0;
				}
				$panalty_charge_total = 0;
				//$last_panalty_details=$this->Loan_Payment_Received_Model->get_last_panalty_by_loan_id($loan_id);			
				//$panalty=$this->Panalty_Model->get_panalty_charge();

				$panalty_charge_total = $panalty_charge;

				if ($users->loan_payment_term == 4) {
					$pTname = $users->payment_term_name . " " . $users->loan_payment_term_qty . " days";
					if ($users->reduce_fron_ceque == 1) {
						$pTname = $users->payment_term_name . " ( Custom duration ) ";
					}
				} else {
					$pTname = $users->payment_term_name;
				}
				$due_balance = $due_charge - $paid_due_charge;
				$panlty_balance = $panalty_charge_total - $paid_panalty_charge;
				$total_need_to_pay = $users->loan_payable_total_amount + $panalty_charge_total + $due_charge;
				if ($cosingl_date < $today) {
					$close_status = '<span class="label label-yellow">' . date('Y-m-d', strtotime($users->loan_close_date)) . '</span>';
					// $last_outstanding=$oustanding_value+$due_balance+$panlty_balance;

				} else {
					$close_status = '<span class="label label-success">' . date('Y-m-d', strtotime($users->loan_close_date)) . '</span>';
					//$last_outstanding=$users->loan_payable_total_amount-$paid_loan_only;
				}

				$row['loan_ref'] =  $users->loan_ref;
				$row['cus_name'] = $users->cus_name;
				$route_name = $users->route_name;
				$row['loan_date'] = date('Y-m-d', strtotime($users->loan_date));
				$row['close_status'] = $close_status;
				$row['pTname'] = $pTname;
				$row['loan_no_of_terms'] = $users->loan_no_of_terms;
				$row['loan_tot_amount'] = $users->loan_tot_amount;
				$row['loan_term_amount'] = $users->loan_term_amount;
				$row['oustanding_value'] = number_format($oustanding_value, 2, '.', ',');
				//$row[] = $users->loan_tot_amount;	
				$row['paid_loan_only'] = number_format($paid_loan_only, 2, '.', ',');
				$row['loan_balance'] = number_format($users->loan_payable_total_amount - $paid_loan_only, 2, '.', ',');
				$row['t_loan_amount'] = number_format($users->loan_payable_total_amount, 2, '.', ',');
				//$row[]=$row_action;	
				$row['cus_address'] = $users->cus_address;
				$row['cus_mobile'] = $users->cus_mobile;

				$data[] = $row;
			}

			//-------------------------------------------------------------
			$data['data_array'] = $data;
			$data['route_name'] = $route_name;
		} else {
			$data['data_array'] = "";
			$data['route_name'] = "";
		}


		$this->load->view('reports/loan_report_print', $data);
	}



	public function collection_report_old()
	{
		$data['user_list'] = $this->User_Model->getUsers();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_collection";
		$this->load->view('reports/collection_report', $data);
	}

	public function collection_report()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_collection";
		$this->load->model('Report_Loan_Model');
		$data['user_list'] = $this->Report_Loan_Model->get_collectors();
		$data['selected_date'] = $this->input->post('srh_from_date') ?: date('Y-m-d');
		$data['selected_collector'] = $this->input->post('user_id') ?: '';

		if ($this->input->post('add_category') == 'Search') {
			$data['report'] = $this->Report_Loan_Model->get_collection_report(
				$data['selected_collector'],
				$data['selected_date']
			);
			$data['summary'] = $this->Report_Loan_Model->get_collection_summary(
				$data['selected_collector'],
				$data['selected_date']
			);
		} else {
			$data['report'] = [];
			$data['summary'] = ['total_paid' => 0, 'panalty_received' => 0, 'due_received' => 0];
		}

		$this->load->view('reports/collection_report', $data);
	}





	//-------------------------------------------------------------
	//loan report given time period
	public function arrears_report()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "rep_loan_arrears";
		$this->load->view('rep_loan_arrears', $data);
	}

	public function get_loan_arrears_summary($value = '')
	{
		$this->load->model('Panalty_Model');

		if ($this->session->userdata('ss_group_id') == 1 || $this->session->userdata('ss_group_id') == 2) {
			$wareh_id = '';
		} else {
			$wareh_id = $this->session->userdata('ss_warehouse_id');
		}
		$route = $this->input->post('route_id');
		$payment_t = $this->input->post('loan_payment_term');
		$arr_terms = $this->input->post('arr_terms');
		$values = $this->Report_Loan_Model->getLoan_approved_issued($wareh_id, $route, $payment_t);
		$data = array();
		$total_oustanging_with_panalty_due = 0;
		$total_arrears_installment_count = 0;
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();
				$loan_id = $users->loan_id;
				$payment_term = $users->loan_payment_term;
				$oustanding_value = 0;
				$total_paid_amount = $this->Loan_Model->get_total_paid_by_loan_id($loan_id);
				$total_panalty = $this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
				$total_due = $this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);
				$total_paid_panalty = $this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
				$total_paid_due = $this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
				$total_paid_loan = $this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);
				$last_paid = $this->Loan_Payment_Received_Model->get_last_paid_loan_by_loan_id($loan_id);
				//calcilation start ------------------------------------------------------------------
				$data2 = array(
					'loan_id' => $loan_id,
					'payment_term' => $payment_term,
					'total_paid_amount' => $total_paid_amount,
					'total_panalty' => $total_panalty,
					'total_due' => $total_due,
					'total_paid_panalty' => $total_paid_panalty,
					'total_paid_due' => $total_paid_due,
					'total_paid_loan' => $total_paid_loan,
					'users' => $users
				);
				//is_avalable_link(1, 1);
				$oustanding_value = common_outstanding_calculation($data2);
				// calcilation End ------------------------------------------------------------------					
				if ($total_panalty['panalty_amount'] > 0) {
					$panalty_charge = $total_panalty['panalty_amount'];
				} else {
					$panalty_charge = 0;
				}
				if ($total_due['due_amount'] > 0) {
					$due_charge = $total_due['due_amount'];
				} else {
					$due_charge = 0;
				}
				if ($total_paid_panalty['panalty_received'] > 0) {
					$paid_panalty_charge = $total_paid_panalty['panalty_received'];
				} else {
					$paid_panalty_charge = 0;
				}
				if ($total_paid_due['due_received'] > 0) {
					$paid_due_charge = $total_paid_due['due_received'];
				} else {
					$paid_due_charge = 0;
				}
				if ($total_paid_loan['loan_received'] > 0) {
					$paid_loan_only = $total_paid_loan['loan_received'];
				} else {
					$paid_loan_only = 0;
				}
				if (isset($last_paid['pymnt_amount'])) {
					$last_paid = $last_paid['pymnt_amount'];
				} else {
					$last_paid = 0;
				}
				$panalty_charge_total = 0;
				//$last_panalty_details=$this->Loan_Payment_Received_Model->get_last_panalty_by_loan_id($loan_id);			
				//$panalty=$this->Panalty_Model->get_panalty_charge();					
				$panalty_charge_total = $panalty_charge;
				$due_balance = $due_charge - $paid_due_charge;
				$panlty_balance = $panalty_charge_total - $paid_panalty_charge;
				$oustanging_with_panalty_due = $oustanding_value + $due_balance + $panlty_balance;
				if ($oustanding_value == 0) {
					$arrears_installment_count = 0;
				} else {
					$arrears_installment_count = round($oustanding_value / $users->loan_term_amount);
				}


				if ($arr_terms) {
					if ($oustanging_with_panalty_due > 0 && $arr_terms == $arrears_installment_count) {
						$total_oustanging_with_panalty_due += $oustanging_with_panalty_due;
						$total_arrears_installment_count += $arrears_installment_count;
					}
				} else {
					if ($oustanging_with_panalty_due > 0) {
						$total_oustanging_with_panalty_due += $oustanging_with_panalty_due;
						$total_arrears_installment_count += $arrears_installment_count;
					}
				}
			}
			$data = array(
				'arrears_instalment' => $total_arrears_installment_count,
				'arrears_amount' => $total_oustanging_with_panalty_due,
			);
			echo json_encode($data);
		} else {
			return NULL;
		}
	}

	public function get_loan_arrears_list($value = '')
	{
		$this->load->model('Panalty_Model');

		if ($this->session->userdata('ss_group_id') == 1 || $this->session->userdata('ss_group_id') == 2) {
			$wareh_id = '';
		} else {
			$wareh_id = $this->session->userdata('ss_warehouse_id');
		}
		$route = $this->input->post('route_id');
		$payment_t = $this->input->post('loan_payment_term');
		$arr_terms = $this->input->post('arr_terms');

		$values = $this->Report_Loan_Model->getLoan_approved_issued($wareh_id, $route, $payment_t);
		$count = count($values);
		$totalData = $count;
		$totalFiltered = $totalData;
		$columns = array();
		$data = array();
		//list panalty array start
		if (!empty($values)) {
			foreach ($values as $users) {
				$row = array();
				$loan_id = $users->loan_id;
				$payment_term = $users->loan_payment_term;
				$oustanding_value = 0;
				$total_paid_amount = $this->Loan_Model->get_total_paid_by_loan_id($loan_id);
				$total_panalty = $this->Loan_Payment_Received_Model->get_total_panalty_by_loan_id($loan_id);
				$total_due = $this->Loan_Payment_Received_Model->get_total_due_by_loan_id($loan_id);
				$total_paid_panalty = $this->Loan_Payment_Received_Model->get_total_paid_panalty_by_loan_id($loan_id);
				$total_paid_due = $this->Loan_Payment_Received_Model->get_total_paid_due_by_loan_id($loan_id);
				$total_paid_loan = $this->Loan_Payment_Received_Model->get_total_paid_loan_by_loan_id($loan_id);
				$last_paid = $this->Loan_Payment_Received_Model->get_last_paid_loan_by_loan_id($loan_id);
				//calcilation start ------------------------------------------------------------------
				$data2 = array(
					'loan_id' => $loan_id,
					'payment_term' => $payment_term,
					'total_paid_amount' => $total_paid_amount,
					'total_panalty' => $total_panalty,
					'total_due' => $total_due,
					'total_paid_panalty' => $total_paid_panalty,
					'total_paid_due' => $total_paid_due,
					'total_paid_loan' => $total_paid_loan,
					'users' => $users
				);
				//is_avalable_link(1, 1);
				$oustanding_value = common_outstanding_calculation($data2);
				// calcilation End ------------------------------------------------------------------					
				if ($total_panalty['panalty_amount'] > 0) {
					$panalty_charge = $total_panalty['panalty_amount'];
				} else {
					$panalty_charge = 0;
				}
				if ($total_due['due_amount'] > 0) {
					$due_charge = $total_due['due_amount'];
				} else {
					$due_charge = 0;
				}
				if ($total_paid_panalty['panalty_received'] > 0) {
					$paid_panalty_charge = $total_paid_panalty['panalty_received'];
				} else {
					$paid_panalty_charge = 0;
				}
				if ($total_paid_due['due_received'] > 0) {
					$paid_due_charge = $total_paid_due['due_received'];
				} else {
					$paid_due_charge = 0;
				}
				if ($total_paid_loan['loan_received'] > 0) {
					$paid_loan_only = $total_paid_loan['loan_received'];
				} else {
					$paid_loan_only = 0;
				}
				if (isset($last_paid['pymnt_amount'])) {
					$last_paid = $last_paid['pymnt_amount'];
				} else {
					$last_paid = 0;
				}
				$panalty_charge_total = 0;
				//$last_panalty_details=$this->Loan_Payment_Received_Model->get_last_panalty_by_loan_id($loan_id);			
				//$panalty=$this->Panalty_Model->get_panalty_charge();					
				$panalty_charge_total = $panalty_charge;
				if ($users->loan_payment_term == 4) {
					$pTname = $users->payment_term_name . " " . $users->loan_payment_term_qty . " days";
					if ($users->reduce_fron_ceque == 1) {
						$pTname = $users->payment_term_name . " ( Custom duration ) ";
					}
				} else {
					$pTname = $users->payment_term_name;
				}
				$due_balance = $due_charge - $paid_due_charge;
				$panlty_balance = $panalty_charge_total - $paid_panalty_charge;
				$oustanging_with_panalty_due = $oustanding_value + $due_balance + $panlty_balance;

				if ($oustanding_value == 0) {
					$arrears_installment_count = 0;
				} else {
					$arrears_installment_count = round($oustanding_value / $users->loan_term_amount);
				}
				$total_oustanging_with_panalty_due = 0;



				if ($arr_terms) {
					if ($oustanging_with_panalty_due > 0 && $arr_terms == $arrears_installment_count) {
						$total_oustanging_with_panalty_due += $oustanging_with_panalty_due;
						//$total_need_to_pay=$users->loan_payable_total_amount+$panalty_charge_total+$due_charge;	
						$row[] = $users->loan_date;
						$row[] = $users->loan_close_date;
						$row[] = ' <a href="' . base_url() . 'loan/details/' . $users->loan_id . '">' . $users->loan_ref . '</a>';
						$row[] = $users->cus_name . " (" . $users->cus_phone . ")";
						$row[] = $users->loan_tot_amount;
						$row[] = $users->loan_no_of_terms;

						$row[] = number_format($users->loan_payable_total_amount - $paid_loan_only, 2, '.', ',');
						$row[] = number_format($oustanging_with_panalty_due, 2, '.', ',');
						$row[] = $arrears_installment_count;
						$clas = '';
						$loan_editable = false;
						$loan_editable = false;
						$data[] = $row;
					}
				} else {
					if ($oustanging_with_panalty_due > 0) {
						$total_oustanging_with_panalty_due += $oustanging_with_panalty_due;
						//$total_need_to_pay=$users->loan_payable_total_amount+$panalty_charge_total+$due_charge;	
						$row[] = $users->loan_date;
						$row[] = $users->loan_close_date;
						$row[] = ' <a target="_blank" href="' . base_url() . 'loan/details/' . $users->loan_id . '">' . $users->loan_ref . '</a>';
						$row[] = $users->cus_name . " (" . $users->cus_phone . ")";
						$row[] = $users->loan_tot_amount;
						$row[] = $users->loan_no_of_terms;
						$row[] = number_format($users->loan_payable_total_amount - $paid_loan_only, 2, '.', ',');;
						$row[] = number_format($oustanging_with_panalty_due, 2, '.', ',');
						$row[] = $arrears_installment_count;
						$clas = '';
						$loan_editable = false;
						$loan_editable = false;
						$data[] = $row;
					}
				}
			}
			$json_data = array(
				//"draw"            => intval( $requestData['draw'] ),  
				"recordsTotal"    => intval($totalData),
				"recordsFiltered" => intval($totalFiltered),

				"data"            => $data
			);

			echo json_encode($json_data);
		} else {
			$json_data = array(
				//"draw"            => intval( $requestData['draw'] ),  
				"recordsTotal"    => intval(0),
				"recordsFiltered" => intval(0),
				"data"            => ""
			);
			echo json_encode($json_data);
		}
	}



	//-----------------------------------------------------------

	//-------------------------------------------------------------
	//capital report given time period
	public function customer_analysis()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "customer_analysis";
		$cus_id = $this->input->get('cus_id'); // if customer filter is applied
		//if($cus_id){
		$data['customers'] = $this->Report_Loan_Model->get_customer_analysis($cus_id);
		//}
		// get all or filtered customers

		$this->load->view('reports/customer_analysis', $data);
	}




	//capital report given time period
	public function capital_report()
	{
		$data['routes_list'] = $this->Route_Model->get_all_active_routes();
		$data['payment_term_list'] = $this->Loan_Model->get_loan_payments_term();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "capital_report";
		$this->load->view('rep_capital_report', $data);
	}

	public function get_loan_capital_summary($value = '')
	{
		$srh_to_date = '';
		$srh_from_date = '';

		if ($this->session->userdata('ss_group_id') == 1 || $this->session->userdata('ss_group_id') == 2) {
			$wareh_id = '';
		} else {
			$wareh_id = $this->session->userdata('ss_warehouse_id');
		}
		$route_id = $this->input->post('route_id');
		$loan_payment_term = $this->input->post('loan_payment_term');
		if ($this->input->post('srh_to_date')) {
			$srh_to_date = date('Y-m-d', strtotime($this->input->post('srh_to_date')));
		}
		if ($this->input->post('srh_from_date')) {
			$srh_from_date = date('Y-m-d', strtotime($this->input->post('srh_from_date')));
		}

		$values = $this->Report_Loan_Model->get_loan_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$panalty = $this->Report_Loan_Model->get_loan_summary_panalty($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$due = $this->Report_Loan_Model->get_loan_summary_due($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);
		$paid = $this->Report_Loan_Model->get_loan_paid_summary($wareh_id, $route_id, $loan_payment_term, $srh_from_date, $srh_to_date);

		$capital_balance = $values['loan_tot_amount'] - $paid['capital_received'];
		$income = $paid['loan_interest_recived'] + $paid['due_received'] + $paid['panalty_received'];
		$net_income = $income - $capital_balance;
		$total_payble = $values['loan_payable_total_amount'] + $panalty['panalty_amount'] + $due['due_amount'];

		$data = array(
			'loan_tot_amount' => $values['loan_tot_amount'],
			'total_interest' => $values['total_interest'],
			'loan_payable_total_amount' => $values['loan_payable_total_amount'],
			'panalty_amount' => $panalty['panalty_amount'],
			'due_amount' => $due['due_amount'],
			'capital_received' => $paid['capital_received'],
			'loan_interest_recived' => $paid['loan_interest_recived'],
			'loan_received' => $paid['loan_received'],
			'panalty_received' => $paid['panalty_received'],
			'due_received' => $paid['due_received'],
			'pymnt_amount' => $paid['pymnt_amount'],
			'capital_balance' => $values['loan_tot_amount'] - $paid['capital_received'],
			'interest_balance' => $values['total_interest'] - $paid['loan_interest_recived'],
			'panalty_balance' => $panalty['panalty_amount'] - $paid['panalty_received'],
			'due_balance' => $due['due_amount'] - $paid['due_received'],
			'total_balane' => $total_payble - $paid['pymnt_amount'],
			'total_payble' => $total_payble,
			'net_income' => $net_income
		);
		echo json_encode($data);
	}


	//-----------------------------------------------------------


	public function customer_outstanding_arrears_report()
	{
		$data['user_list'] = $this->Report_Loan_Model->get_collectors();
		$data['selected_date'] = $this->input->post('srh_from_date') ?: date('Y-m-d');
		$data['selected_collector'] = $this->input->post('user_id') ?: '';
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "customer_outstanding_arrears_report";

		if ($this->input->post('add_category') == 'Search') {
			$data['report'] = $this->Report_Loan_Model->get_customer_outstanding_arrears_report(
				$data['selected_collector'],
				$data['selected_date']
			);
			$data['summary'] = $this->Report_Loan_Model->get_customer_outstanding_arrears_summary(
				$data['selected_collector'],
				$data['selected_date']
			);
		} else {
			$data['report'] = [];
			$data['summary'] = ['total_outstanding' => 0, 'total_arrears' => 0];
		}

		$this->load->view('reports/customer_outstanding_arrears_view', $data);
	}

	public function customer_rating()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name']  = "customer_rating";

		$this->load->model('report_loan_model');
		$data['ratings'] = $this->report_loan_model->get_customer_ratings();

		$this->load->view('reports/customer_rating', $data);
	}

	public function marketing_report()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name']  = "marketing_report";

		$selected_month = $this->input->post('selected_month');
		if (empty($selected_month)) {
			$selected_month = date('Y-m');
		}

		$data['selected_month'] = $selected_month;
		$this->load->model('report_loan_model');
		$data['marketing_loans'] = $this->report_loan_model->get_marketing_report($selected_month);
        //echo '<pre>',print_r($data['marketing_loans']); die();
		$this->load->view('reports/marketing_report', $data);
	}
	
	public function income_report()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name']  = "marketing_report";

		$selected_month = $this->input->post('selected_month');
		if (empty($selected_month)) {
			$selected_month = date('Y-m');
		}

		$data['selected_month'] = $selected_month;
		$this->load->model('report_loan_model');
		$data['marketing_loans'] = $this->report_loan_model->get_marketing_report($selected_month);
        //echo '<pre>',print_r($data['marketing_loans']); die();
		$this->load->view('reports/income_report', $data);
	}


	public function recovery_report()
	{
		$data['user_list'] = $this->User_Model->getFilterUsers(5);
		$data['route_list'] = $this->Route_Model->get_all_active_routes(); // Add this line
		//echo '<pre>',print_r($data['route_list']); die();
		$data['selected_date'] = $this->input->post('srh_from_date') ?: date('Y-m-d');
		$data['selected_collector'] = $this->input->post('user_id') ?: '';
		$data['selected_route'] = $this->input->post('route_id') ?: ''; // Add this line
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "recovery_report";

		if ($this->input->post('add_category') == 'Search') {
			$data['report'] = $this->Report_Loan_Model->get_recovery_report(
				$data['selected_collector'],
				$data['selected_date'],
				$data['selected_route'] // Add route parameter
			);
			$data['summary'] = $this->Report_Loan_Model->get_recovery_summary(
				$data['selected_collector'],
				$data['selected_date'],
				$data['selected_route'] // Add route parameter
			);
		} else {
			$data['report'] = [];
			$data['summary'] = ['total_outstanding' => 0, 'total_arrears' => 0];
		}

		$this->load->view('reports/recovery_report_view', $data);
	}


	public function no_repeat_loan_report()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "no_repeat_loan_report";
		// ---- Data ----------------------------------------------------
		$data['report'] = $this->Report_Loan_Model->get_customers_no_repeat_loan();

		// ---- View ----------------------------------------------------
		$this->load->view('reports/no_repeat_loan_report', $data);
	}

	public function customer_compliments()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = "customer_compliments";

		// Get month from input or current
		$selected_month = $this->input->post('birthday_month');
		if (empty($selected_month)) {
			$selected_month = date('Y-m');
		}

		$data['selected_month'] = $selected_month;

		// Load model
		$this->load->model('report_loan_model');
		$data['customers'] = $this->report_loan_model->get_birthday_customers($selected_month);

		$this->load->view('reports/customer_compliments', $data);
	}

	public function get_loans_by_customer()
	{
		$cus_id = $this->input->post('cus_id');

		if (!$cus_id) {
			echo json_encode(['status' => false, 'message' => 'Customer ID missing']);
			return;
		}

		$this->db->select("l.*, c.cus_name, c.cus_nic");
		$this->db->from("loan l");
		$this->db->join("customer c", "c.cus_id = l.cus_id", "left");
		$this->db->where("l.cus_id", $cus_id);
		//$this->db->where("l.is_closed", 0); // optional: only ongoing loans

		$data = $this->db->get()->result_array();

		if (empty($data)) {
			echo json_encode(['status' => false]);
		} else {
			echo json_encode(['status' => true, 'data' => $data]);
		}
	}
}
