<?php

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

    /**
    Auther Reshma Rajan
     */
    class Userdata__model extends CI_Model 
    {

        function __construct() {
            parent::__construct();
            $this->load->database();
			
		          }
		
		/*
		Description :  To get data in pipeline
		Use			: 	To Listing
		Date		:	@
		Author		:	OI	
		*/
		public function common_get_pipeline($table, $columns,$condtion = NULL,$search_value = NULL,$search_like = NULL,$joins,$limit,$order_by = NULL,$where_in = NULL,$where_in_data = NULL)
			{
				$this->db->initialize();
				$this->db->select($columns,false)->from($table);
				if (is_array($joins) && count($joins) > 0)
				{
					foreach($joins as $k => $v)
					{
						$this->db->join($v['table'], $v['condition'], $v['jointype']);
					}
				}
				if($condtion != NULL){
					if (is_array($condtion) && count($condtion) > 0)
					{
						foreach($condtion as $k => $v)
						{  
							 if($v['value'] != '' && $v['value'] != NULL){
								$this->db->where($v['key'], $v['value']);
							 }
						}
					}
				}
				
				$total_count = COUNT($this->db->get()->result_array());
				
				//Getting values for DataTale

				$this->db->select($columns,false)->from($table);
				if (is_array($joins) && count($joins) > 0)
				{
					foreach($joins as $k => $v)
					{
						$this->db->join($v['table'], $v['condition'], $v['jointype']);
					}
				}
				if($condtion != NULL){
					if (is_array($condtion) && count($condtion) > 0)
					{
						foreach($condtion as $k => $v)
						{  
							 if($v['value'] != '' && $v['value'] != NULL){
								$this->db->where($v['key'], $v['value']);
							 }
						}
					}
				}
				if($search_value != NULL && $search_value != ''){
					if (is_array($search_like) && count($search_like) > 0)
					{
					 $this->db->group_start();
						foreach($search_like as $k )
						{  
							$this->db->or_like($k,$search_value);                   
						}
					  $this->db->group_end(); 
					}
				} 
				
				

				
				
				//Where in clause
				if($where_in != NULL && (!empty($where_in_data))){
				$this->db->where_in('agent_id',$where_in_data);
				}
				if(is_array($limit) && count($limit) > 0){
					$this->db->limit($limit['length'],$limit['start']);
				}
				if($order_by != NULL){
				  if(is_array($order_by) && count($order_by) > 0){
					$this->db->order_by($order_by['key'],$order_by['type']);
				  }
				}
				$query = $this->db->get();
				
				#echo $this->db->last_query();die;
				//$filter_count = $this->db->query('SELECT FOUND_ROWS() count;')->row()->count;
				$filter_count = $this->db->query('SELECT count(*) AS count;');
				$filter_count =$query->num_rows();
				
				$content = array();
				$count = $limit['start']+1;
				$data['draw'] ='test';
				if(count($query->result_array()) > 0){
					foreach ($query->result_array() as $row) {
						
						$row[] = $count;
						$content[] = array_values($row);
						$count ++;
						
						//array_push($content[], $count);
					}
				$this->db->close();
					 return json_encode(array("draw"=> $data['draw'],"recordsTotal"=> $total_count,"recordsFiltered"=> $filter_count,"data" => $content));
				}else{
					$this->db->close();
						return json_encode(array("draw"=> $data['draw'],"recordsTotal"=> $total_count,"recordsFiltered"=> $filter_count,"data" => $content));
				}
			}
		/*
		Description :  To get all agents
		Date		:	10/10/2017
		Author		:	Arun	
		*/
	function get_all_agent($dis_id)
		{
			$this->db->initialize();
			$this->db->select('id');
			$this->db->from('login');
			$this->db->where('login.district_id',$dis_id);			
			$result = $this->db->get();
			$ret_val = $result->result_array();
			$this->db->close();
			return $ret_val;
		}
	/*
		Description :  To get relation from master tbl
		Date		:	10/10/2017
		Author		:	Arun	
		*/	
	function get_relation($rid)
		{
			$this->db->initialize();
			$this->db->select('relation_master.title');
			$this->db->from('relation_master');
			$this->db->where('relation_master.id',$rid);			
			$query = $this->db->get();
			$result=$query->first_row();
			$this->db->close();
			return $result;
		}
	/*
		Description :  To get all Job from master tbl
		Date		:	10/10/2017
		Author		:	Arun	
		*/		
	function get_job($jid)
		{
			$this->db->initialize();
			$this->db->select('job_options_master.title');
			$this->db->from('job_options_master');
			$this->db->where('job_options_master.id',$jid);			
			$query = $this->db->get();
			$result=$query->first_row();
			$this->db->close();
			return $result;
		}
/*
		Description :  To get all diseases
		Date		:	10/10/2017
		Author		:	Arun	
		*/		
	function get_diseases($id)
		{
			$this->db->initialize();
			$this->db->select('diseases_issues.*');
			$this->db->from('diseases_issues');
			$this->db->select('question_master.questions ');
			$this->db->join('question_master','question_master.id=diseases_issues.question_id','left');
			$this->db->where('diseases_issues.family_member_id',$id);			
			$result = $this->db->get();
			$ret_val=$result->result_array();
			$this->db->close();
			return $ret_val;
		}
		/*
		Description :  To get Family details based on id
		Date		:	10/10/2017
		Author		:	Arun	
		*/
	function get_family($id)
		{
			$this->db->initialize();
			$this->db->select('survey_family.*');
			$this->db->from('survey_family');
			$this->db->where('survey_family.family_id',$id);			
			$query = $this->db->get();
			$result=$query->first_row();
			$this->db->close();
			return $result;
		}	
	/*
		Description :  To get all family members based on family_id
		Date		:	10/10/2017
		Author		:	Arun	
		*/		
	function get_family_members($id)
    {
		$this->db->initialize();
    	$this->db->select('family_members.*');
		$this->db->from('family_members');
		//$this->db->join('diseases_issues','diseases_issues.family_member_id=family_members.member_id','left');	
		/*$this->db->join('panchayath','panchayath.id=user_data.grama_panchayath','left');
		$this->db->join('ward','ward.id=user_data.ward','left');*/
		$this->db->where('family_members.family_id',$id);
		$result = $this->db->get();
		$this->db->close();
		return $result->result_array();		
    }
	/*
		Description :  To get all services
		Date		:	11/10/2017
		Author		:	Arun	
		*/
    function get_services($id)
    	{
			$this->db->initialize();
    		$this->db->select('service.*');
			$this->db->from('service');
			$this->db->where('service.family_id',$id);
			$query = $this->db->get();
			$this->db->close();
			return $result=$query->result_array();
    	}
    /*
		Description :  To get all Issues
		Date		:	11/10/2017
		Author		:	Arun	
		*/
    function get_issues($id)
		{
			$this->db->initialize();
			$this->db->select('family_issues.*');
			$this->db->from('family_issues');
			$this->db->select('question_master.questions ');
			$this->db->join('question_master','question_master.id=family_issues.question_id','left');
			$this->db->where('family_issues.family_id',$id);			
			$result = $this->db->get();
			$this->db->close();
			return $result->result_array();
		}
	/*
		Description :  To export agent
		Date		:	11/10/2017
		Author		:	Arun	
		*/				
    public function get_agent_export($data)
		{
			$this->db->initialize();
			$this->db->select('user_data.mobile,user_data.created_at,user_data.id,login.name as agent_name,user_data.name,user_data.guardian_name,user_data.dob,user_data.sex,user_data.id,taluk_all.taluk_name,panchayath.panchayath_name as grama_panchayath,ward.ward_name as ward,cast_certificate,toilet_avail,aadhar,aadhar_avail,ration_card,rationcard_avail,jobcard,jobcard_avail,name_base_line_survey,electric_bill,electic_bill_avail,back_acc,bank_name,branch_name,bank_account_avail,ifc_code,cat_bpl_apl,cat_sc_st,cat_farmer,handicapped,landless_labour,women_centric_family,open_place_avil_or_not,toilet_construction,bank_loan,material_for_construction,readymade_toilet,caste,cast_other,profile_pic,doc_pic');
			$this->db->from('user_data');
			$this->db->join('login','user_data.userId=login.id','left');				
			$this->db->join('taluk_all','taluk_all.id=user_data.taluk_name','left');	
			$this->db->join('panchayath','panchayath.id=user_data.grama_panchayath','left');
			$this->db->join('ward','ward.id=user_data.ward','left');
			$this->db->where('login.status',ACTIVE);
			$this->db->where('user_data.status',ACTIVE);
			/*if(isset($data['seach_val']) && $data['seach_val'] !='0' && $data['seach_val'] !=''){

            		$this->db->like('user_data.name',$data['seach_val']);                   
       			} */
       			if(isset($data['seach_val']) && $data['seach_val'] !='0' && $data['seach_val'] !=''){
		            if (is_array($data['search_like']) && count($data['search_like']) > 0)
		            {
		             $this->db->group_start();
		                foreach($data['search_like'] as $k )
		                {  
		                    $this->db->or_like($k,$data['seach_val']);                   
		                }
		              $this->db->group_end(); 
		            }
		        }
   			if(isset($data['id']) && $data['id'] !='' && $data['id'] !='0'){

        		$this->db->where('user_data.id',$data['id']);                   
   			}
   			if(isset($data['agent_id']) && $data['agent_id'] !='' && $data['agent_id'] !='0'){

        		$this->db->where('user_data.userId',$data['agent_id']);                   
   			}  
			
				//$this->db->limit(12);
			$this->db->order_by('user_data.id','desc');    
			$query = $this->db->get();
			$this->db->close();
			return $query->result_array();
		}
		/*
		Description :  To get agent report
		Date		:	11/10/2017
		Author		:	Arun	
		*/
	public function get_report_export($data)
		{
			$this->db->initialize();
			$this->db->select('login.id,login.name as agent_name,survey_family.family_id as familyId,survey_family.name,survey_family.address,survey_family.village,survey_family.ward_no,survey_family.ration_card_no,survey_family.suvey_date,survey_family.house_no,survey_family.ration_shop_no,survey_family.ration_card_color,survey_family.land,survey_family.land_cent,survey_family.ayalkootam_name,survey_family.ayalkootam_ward_no');				

			$this->db->from('survey_family');
			$this->db->join('login','login.id=survey_family.agent_id','left');				
			/*$this->db->join('taluk_all','taluk_all.id=user_data.taluk_name','left');	
			$this->db->join('panchayath','panchayath.id=user_data.grama_panchayath','left');
			$this->db->join('ward','ward.id=user_data.ward','left');*/
			$this->db->where('login.status',ACTIVE);
			$this->db->where('survey_family.status',ACTIVE);
			/*if(isset($data['seach_val']) && $data['seach_val'] !='0' && $data['seach_val'] !=''){

            		$this->db->like('user_data.name',$data['seach_val']);                   
       			} */
       			if(isset($data['seach_val']) && $data['seach_val'] !='0' && $data['seach_val'] !=''){
		            if (is_array($data['search_like']) && count($data['search_like']) > 0)
		            {
		             $this->db->group_start();
		                foreach($data['search_like'] as $k )
		                {  
		                    $this->db->or_like($k,$data['seach_val']);                   
		                }
		              $this->db->group_end(); 
		            }
		        }
   			if(isset($data['id']) && $data['id'] !='' && $data['id'] !='0'){

        		$this->db->where('survey_family.family_id',$data['id']);                   
   			}
   			if(isset($data['agent_id']) && $data['agent_id'] !='' && $data['agent_id'] !='0'){

        		$this->db->where('survey_family.agent_id',$data['agent_id']);                   
   			} 
   			if(isset($data['survey_status']) && $data['survey_status'] !='' && $data['survey_status'] !='0'){

        		$this->db->where('survey_family.survey_status',$data['survey_status']);                   
   			} 
			
				//$this->db->limit(12);
			$this->db->order_by('survey_family.family_id','desc');    
			$query = $this->db->get();
			$this->db->close();
			return $query->result_array();
		}
		/*
		Description :  To get agent
		Date		:	11/10/2017
		Author		:	Arun	
		*/
	function get_agent_list()
		{
			$this->db->initialize();
			$this->db->select('id,name,status');
			$this->db->from('login');
			$this->db->where('role',AGENT);
			$this->db->order_by("name","asc");
			$result = $this->db->get();
			$this->db->close();
			return $result->result_array();
		
		}
	/*
	Description :  To get agent details
		Date		:	27/10/2017
		Author		:	Arun	
	*/	
	function get_agent_details($agt_id)
		{
			$this->db->initialize();
			$this->db->select('id,name,district_id,taluk_id,block_id,corporation,muncipality,panchayath_id,cds,ads,constitution');
			$this->db->from('login');
			$this->db->where('id',$agt_id);
			$this->db->where('role',AGENT);
			$result = $this->db->get();
			$this->db->close();
			return $result->result_array();
		}

	
}