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

class Loans_model extends CI_Model {

	function __construct()
    {
     parent::__construct();		
	
    }
	
	function get_member_details($subject_id)
	{
		//$query = "SELECT * FROM user WHERE user_id = {$subject_id}";
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('user_id', $subject_id);
		
		$query = $this->db->get();
		if($query->num_rows()==1)
		{
			$row = $query->result_array();
			$member_no = $row[0]['member_no'];
			$member_name = $row[0]['name'];			
			$member_no_member_name = $member_no." - ".$member_name;
			
			return $member_no_member_name;	
		}
		else
			return NULL;
			
	}
	
	/*this function is called when a member is adding guarantors during loan application. 
	It authneticates is a guarantor exists based on the provided credentials. Returns guarantor's user_id or FALSE*/
	function confirm_guarantor($member_no, $national_id_no, $corporation_id)
	{
		//$query = "SELECT user_id FROM users WHERE member_no = $member_no AND  national_id_no = $national_id_no";
		$this->db->select('user_id');
		$this->db->from('users');
		$this->db->where('member_no', $member_no);
		$this->db->where('national_id_no', $national_id_no);
		$this->db->where('corporation_id', $corporation_id);
		
		$query = $this->db->get();		
		
		if($query->num_rows()==1)
		{
			$row = $query->result_array();
			$user_id = $row[0]['user_id'];
			
			return $user_id;	
		}
		else
			return "Not Found";
	}
	
	//this function gets the loan id of a user. The assumptions are a user can only have one loan which is not completely paid
	function get_loan_id($user_id)
	{
		$query = "SELECT loan_id FROM loan_application WHERE user_id=$user_id AND status !=5";
		$this->db->select('loan_id');
		$this->db->from('loan_application');
		$this->db->where('user_id', $user_id);
		$this->db->where('user_id !=', 5);
		
		$query = $this->db->get();		
		
		if($query->num_rows()==1)
		{
			$row = $query->result_array();
			$loan_id = $row[0]['loan_id'];
			
			return $loan_id;	
		}
		else
			return NULL;
	}
}
?>