<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of customerRegistrationmodel
 *
 * @author BusyMan
 */
class LandCustomerRegistrationModel extends CI_Model {

    //put your code here
    private $_tbl_CREG = 'tbl_customer_reg';
    private $_tbl_CA = 'tbl_calls';

// save customer info
    public function insert_customer_info($data) {
        $this->db->insert($this->_tbl_CREG, $data);
        return $this->db->insert_id();
    }

// get all customer Info 
    public function getlandCustomer(){
        $query=$this->db->get($this->_tbl_CREG);
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function getAllReferenceInfo($table){
        if($table == 1){
            $this->db->select('id, customerId, applicant_name');
            $query  = $this->db->get($this->_tbl_CREG);
            $result = $query->result_array();
            if(!empty($result)){
                return $result;
            }
        }else if($table == 2){
            $this->db->select('id, userId, userName');
            $query  = $this->db->get('tbl_user');
            $result = $query->result_array();
            if(!empty($result)){
                return $result;
            }
        }
    }

    public function getRef($user_id){
        $query = $this->db->select('*')->get_where($this->_tbl_CREG, array('ref_id'=>$user_id));
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        }
    }

// get all land customerId
    public function getallcustomers(){
    	$query=$this->db->select('*')->get($this->_tbl_CREG);
    	$result=$query->result_array();
    	if(!empty($result)){
    		return $result;
    	}
    }    
    
    public function getallcustomerId(){
    	$query=$this->db->select('customerId')
    			->get($this->_tbl_CREG);
    	$result=$query->result_array();
    	if(!empty($result)){
    		return $result;
    	}
    }
    public function getallcustomerNames(){
    	$query=$this->db->select('applicant_name')
    			->get($this->_tbl_CREG);
    	$result=$query->result_array();
    	if(!empty($result)){
    		return $result;
    	}
    }
    public function findcomamount($customerId){
    	$query=$this->db->select('com_emp')
    			->get_where($this->_tbl_CREG,array('customerId'=>$customerId));
    	$result=$query->result_array();
    	if(!empty($result)){
    		return $result;
    	}
    }

    public function updatecomamount($datacomup)
    {
        $datax['com_emp']=$datacomup['commission'];
        $this->db->update($this->_tbl_CREG, $datax, array('customerId' => $datacomup['customerId']));
        return $this->db->affected_rows();
    }
    public function updateincamount($datacomup)
    {
        $datax['incentive']=$datacomup['incentive'];
        $this->db->update($this->_tbl_CREG, $datax, array('customerId' => $datacomup['customerId']));
        return $this->db->affected_rows();
    }
    
    

//get krm_no
    public function getkro_num(){
        $query=$this->db->select('id')->order_by('id','desc')->limit(1)->get($this->_tbl_CREG);
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }


    public function getlandCustomerByRef($refid){
        $this->db->select('customerId');
        $this->db->select('applicant_name');
        $this->db->select('mobile');
        $this->db->select('nominee_mobile');
        $this->db->select('ref_id');
        $query=$this->db->get_where($this->_tbl_CREG,array('ref_id'=>$refid));
        //$query=$this->db->get($this->_tbl_CREG);
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }
    public function getcustomerlastcall($customerId){
        //$query=$this->db->select('*')->get_where($this->_tbl_CA,array('customerId'=>$customerId))->order_by('c_id','desc')->limit(1);
        $query=$this->db->select('*')->order_by('c_id','desc')->limit(1)->get_where($this->_tbl_CA,array('customerId'=>$customerId));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }
    
    //get customerName By id
    public function getlandCustomerNameById($customerId){
        $this->db->select('applicant_name');
        $this->db->select('ref_id');
        $query=$this->db->get_where($this->_tbl_CREG,array('customerId'=>$customerId));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }


    //get customerInfo By Id

    public function getcustomerById($id){
        $this->db->select('tbl_customer_reg.*');
        $this->db->select('tbl_project.project_name');
        // $this->db->select('tbl_user.username');
        $this->db->join('tbl_project','tbl_project.project_id=tbl_customer_reg.projectcategory');
        // $this->db->join('tbl_user','tbl_user.userid=tbl_customer_reg.ref_id');
        $query=$this->db->get_where($this->_tbl_CREG, array('id' =>$id));        
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }


    public function deletecustomerById($id)
    {
        $query=$this->db->delete($this->_tbl_CREG,array('id' =>$id));
        return $this->db->affected_rows();
    }

    public function updateCustomerById($data)
    {
        $datax['id']=$data['id'];
        $query=$this->db->update($this->_tbl_CREG,$data,array('id'=>$datax['id']));
        return $this->db->affected_rows(); 
    }

    public function checkpassword($data){
        $query=$this->db->get_where($this->_tbl_CREG,array('user_password'=>$data['courrentPassword'],
                                                        'id' => $data['id'],
                                                        'applicant_name' => $data['applicant_name'],
                                                        'customerId' => $data['customerId'],
                                                        'customer_access' => $data['customer_access']
                                                        // 'admin_access' => $data['admin_access'],
                                                        // 'employeeId' => $data['employeeId'],
                                                        // 'admin_level' => $data['admin_level']
                                                        )
                                                        );
        $result=$query->row_array();
        if(!empty($result)){return $result;} 

    }

    public function updatePassword($data)
    {
        $datax['user_password']=$data['password'];
        $this->db->update($this->_tbl_CREG, $datax, array('id' => $data['id'],'customerId' => $data['customerId']));
        return $this->db->affected_rows();


        /*---------amother process query ----------------------
          $this->db->set('password',sha1(md5($data['password'])))
                    ->where('admin_id',$data['admin_id'])
                    ->where('employeeId',$data['employeeId'])
                    ->update($this->_table);
        --------------------------------------------------------------------*/
    }



    public function getcustomerByIdsinview($id){
        $query=$this->db->get_where('tbl_customer_reg', array('id' =>$id));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }
    
    public function getcustomerByIds($customerId){
        $query=$this->db->get_where('tbl_customer_reg', array('customerId' =>$customerId));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function getincomeByCustomerIds($id){
        $query=$this->db->get_where('tbl_land_account', array('customerId'=>$id));
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }
    public function getPercentsById($customer_id){
        $query=$this->db->get_where('tbl_salary_expense', array('salary_type'=>5, 'customerId'=>$customer_id));
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }
    public function getIncentiveById($id){
        $query=$this->db->get_where('tbl_salary_expense', array('salary_type'=>4, 'customerId'=>$id));
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }
    public function getCommissionById($id){
        $query=$this->db->get_where('tbl_salary_expense', array('salary_type'=>3, 'customerId'=>$id));
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

    public function getCustomerIdByName($c_name){
        $this->db->select('id, customerId');
        $query = $this->db->get_where('tbl_customer_reg', array('applicant_name'=>$c_name));
        $result = $query->row_array();
        if(!empty($result)){
            return $result;
        }
    }

}
