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

class Customer_Model extends CI_Model {
  
  private $tableName = 'customer';
  
  function __construct() 
  {
    /* Call the Model constructor */
    parent::__construct();
  }
  
  
   public function get_search_customer_by_phone($str) {
        $this->db->select('c.*');
        $this->db->from('customer c');
       
        $this->db->order_by("c.cus_name", "asc");
       // $this->db->or_like('c.cus_name',$str);
		$this->db->where('c.cus_phone',$str);
       // $this->db->or_like('user_last_name',$str);
       // $this->db->or_like('user_group_name',$str);
	    $this->db->LIMIT(1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
 
 
    public function get_search_customer($str) {
        
        //get first caractor
        $first_char=substr($str, 0, 1);
        if($first_char==0){
        $str=substr($str, 1); 
        }
        $this->db->select('c.*');
        $this->db->from('customer c');
       
        $this->db->order_by("c.cus_name", "asc");
        $this->db->or_like('c.cus_name',$str);
		$this->db->or_like('c.cus_phone',$str);
	//	$this->db->or_like('c.cus_phone',"%$str%");
		$this->db->or_like('c.cus_id',$str);
       // $this->db->or_like('user_last_name',$str);
       // $this->db->or_like('user_group_name',$str);
	    $this->db->LIMIT(100);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
 
 	function save_customer(&$customer_data,$cus_id=false)
	{
		if (!$cus_id)
		{
			$this->db->insert($this->tableName,$customer_data);
		}else {
			$this->db->where('cus_id', $cus_id);
			return $this->db->update($this->tableName,$customer_data);
		}
	}
	
	function get_all_customers() {
		$this->db->select($this->tableName.'.*');
		$this->db->where("cus_status",1);//("id !=",$id);
		//$this->db->where("cus_id", $id);
		$this->db->order_by("cus_id", "desc");
		$query = $this->db->get($this->tableName);
		
		//echo $this->db->last_query();
		return $query->result_array();
	}
	
	function get_all_customers_serverside($start='',$length='',$search_key_val='') {
		$this->db->select('c.*');
		$this->db->from('customer c');
		$this->db->where("c.cus_status",1);
			if($search_key_val){
			$this->db->where("c.cus_code LIKE '%$search_key_val%' OR c.cus_name LIKE '%$search_key_val%'");
		}
		if($start!='' && $length!=''){
			if($length>0) $this->db->limit($length,$start);
				$this->db->order_by("c.cus_name", "asc");
		        $query = $this->db->get();
		        return $query->result_array();
		}
		else{
		    	$query = $this->db->get();
		        return $query->num_rows();
		    
		}
	}
	
	
	
	
	
	
	

	function get_all_customer_print($cus_id=false) {
		$this->db->select('customer.*, mstr_country.country_short_name');
		$this->db->from('mstr_country');
		$this->db->join('customer', 'customer.country_id = mstr_country.country_id', 'left');
		$this->db->order_by("customer.cus_name", "asc");
		$this->db->where("customer.cus_status IS NOT NULL");//("id !=",$id);
		//$this->db->where("customer.cus_type","normal");
		if($cus_id){
		$this->db->where("customer.cus_id",$cus_id);
		}
		$query = $this->db->get();
		return $query->result();
	}	
	
	function get_all_customer() {
		$this->db->select('customer.*, mstr_country.country_short_name');
		$this->db->from('mstr_country');
		$this->db->join('customer', 'customer.country_id = mstr_country.country_id', 'left');
		$this->db->order_by("customer.cus_id", "desc");
		$this->db->where("customer.cus_status IS NOT NULL");//("id !=",$id);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_customer_info($id)
	 {
		$this->db->select('*');
		$this->db->from('customer');
		$this->db->where("cus_id", $id);
		$this->db->order_by("cus_id", "desc");
		$query = $this->db->get();
		
		return $query->row_array(); 
	 }
	
	public function delete_customer($cus_id)
	{
		$this->db->where('cus_id', $cus_id);
		$this->db->delete('customer');
	
	}

	public function disable_customer($cus_id)
	{
		$data = array(
			'cus_status' => 0
		);	
		$this->db->where('cus_id', $cus_id);
		$this->db->update('customer', $data);
	}
	
	public function enable_customer($cus_id)
	{
		$data = array(
			'cus_status' => 1
		);	
		$this->db->where('cus_id', $cus_id);
		$this->db->update('customer', $data);
	}	

function get_customers($id='') {
		$this->db->select($this->tableName.'.*');
		$this->db->order_by("cus_name", "asc");
		$this->db->where("cus_status",1);//("id !=",$id);
		if($id)$this->db->where("cus_id",$id);
		$this->db->where("cus_id !=",1);
		$this->db->where("cus_type","normal");
		$query = $this->db->get($this->tableName);
		return $query->result_array();
	}
	
}