<?php
class Customer_model extends CI_Model {
	
	public $db_tabel    = 'customers';
	 
	public function getAllCustomer()
	{

		$query = $this->db->query("SELECT * FROM customers");
		$result = $query->result();
		return $result;
	}

    function GetCustomerById($id)
    {
       
        $this->db->select('*');
        $this->db->from('customers');
        $this->db->where('id',$id); 
        $query = $this->db->get();
        return $query->result();
    } 

    function allcustomer_count()
    {   
        $query = $this->db->get('customers');
        return $query->num_rows();  
    }

    function allcustomer($limit,$start,$col,$dir)
    {   
        
        $query = $this->db->limit($limit,$start)->order_by($col,$dir)->get('customers');
        
        if($query->num_rows()>0)
        {
            return $query->result(); 
        }
        else
        {
            return null;
        }
        
    }

    function customer_search($limit,$start,$search,$col,$dir)
    {
       
        $query = $this->db->like('id',$search)->or_like('customer_code',$search)->or_like('customer_name',$search)->or_like('email',$search)->or_like('phone',$search)
                ->limit($limit,$start)
                ->order_by($col,$dir)
                ->get('customers');
        
       
        if($query->num_rows()>0)
        {
            return $query->result();  
        }
        else
        {
            return null;
        }
    }

    function customer_search_count($search)
    {
     
        $query =  $this->db->like('id',$search)
                ->or_like('customer_ncode',$search)
                ->or_like('customer_name',$search)
                ->get('customers');

        if(!empty($query))
        {
            return $query->num_rows();
        }

        return null;

    } 

    public function InsertCustomer($data)
    {
        $result = $this->db->insert('customers', $data);
        return $result;
    }
  
    public function UpdateCustomer($id, $data)
    {
        $this->db->where('id',$id);
        $result = $this->db->update('customers', $data);
        return $result;
    }


    public function checkAvailability($code)
    {
        $result = 0;
      try 
      {
        $query = $this->db->query("SELECT * FROM customers WHERE customer_code='$code'");
        if ($query->num_rows() > 0)
            $result =1;
      }
      catch(Exception $e) 
      {
        echo 'Message: ' .$e->getMessage();
      }
      return $result;
    }
	


}