<?php 
class Customer_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}

 	function getCustomer($customer_data = array()){
 		$cond = (isset($customer_data['phone']) && !empty($customer_data['phone']))?
											" AND phone LIKE '%".trim($customer_data['phone'])."'":"";
 		$cond .= (!empty($customer_data['customer_id']))?
 											" AND customer_id = '".trim($customer_data['customer_id'])."'":"";

		$result = $this->db->query("SELECT * FROM customers WHERE status IN (0,1) $cond");
		if(empty($result)){
			return;
		}
		return (empty($customer_data))?$result->result():$result->row();
 	}

 	function createCustomer($customer_data = array(), $ret_customer_id = 0){
 		if(empty($customer_data))
 			return 0;

 		if(isset($customer_data['email']) && !empty($customer_data['email'])){
	 		$emailChk = $this->db->get_where('customers',array('email'=>$customer_data['email'],'status !='=>'2'));
	 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
	 			return 2;
	 		}
 		}
 		if(isset($customer_data['phone']) && !empty($customer_data['phone'])){
	 		$phoneChk = $this->db->get_where('customers',array('phone'=>$customer_data['phone'],'status !='=>'2'));
	 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
	 			return 3;
	 		}
	 	}
 		
		$status = $this->db->insert('customers',$customer_data);
		if($status && $ret_customer_id == 1){
			return $this->db->insert_id();
		}
		return ($status)?1:0;
 	}

 	function updateCustomer($customer_id = '', $customer_data = array()){
 		if(empty($customer_id) || empty($customer_data))
 			return 0;

 		$emailChk = $this->db->get_where('customers',array('email'=>$customer_data['email'],
 														   'customer_id !='=>$customer_id,
 														   'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('customers',array('phone'=>$customer_data['phone'],
 														   'customer_id !='=>$customer_id,
 														   'status !='=>'2'));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		
		$status = $this->db->update('customers',$customer_data,array('customer_id'=>$customer_id));
		return ($status)?1:0;
 	}

 	function changeStatus($customer_id = '', $status = '0'){
 		if(empty($customer_id)){
 			return 0;
 		}
 		$status = $this->db->update('customers',array('status'=>$status), array('customer_id'=>$customer_id));
 		return $status;
 	}
}
?>