File: /home/cafsindia/allyindian_com/sbltt/application/models/Customer.php
<?php
class Customer extends Person
{
//Mobile no if exists
public function mob_exists($mobile){
$this->db->from('customers');
$this->db->where('phone_number', $mobile);
$query = $this->db->get();
if($query->num_rows() == 1){
$info = $query->row();
$cust_id = $info->cust_id;
return $cust_id;
}else{
return false;
}
}
/*Determines if a given person_id is a customer*/
public function exists($customer_id){
$this->db->from('customers');
$this->db->where('cust_id', $customer_id);
return ($this->db->get()->num_rows() == 1);
}
public function get_tariff_type() {
$this->db->from('tariff_type');
$this->db->order_by('tariff_type_name', 'asc');
return $this->db->get();
}
public function get_booking_type() {
$this->db->from('booking_type');
$this->db->order_by('booking_type_name', 'asc');
return $this->db->get();
}
/*Performs a search on customers*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'customer_name', $order = 'asc'){
$this->db->from('customers');
// $this->db->join('booking_type', 'booking_type.booking_type_id = customers.cust_type','left');
$this->db->group_start();
$this->db->like('customer_name', $search);
$this->db->or_like('cust_address', $search);
$this->db->or_like('cust_email', $search);
$this->db->or_like('phone_number', $search);
//$this->db->or_like('booking_type.booking_type_name', $search);
$this->db->or_like('dob', $search);
$this->db->or_like('allow_contract', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by('cust_id','desc');
if($rows > 0){
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
/*Gets rows*/
public function get_found_rows($search){
$this->db->from('customers');
$this->db->group_start();
$this->db->like('customer_name', $search);
$this->db->or_like('cust_address', $search);
$this->db->or_like('cust_email', $search);
$this->db->or_like('phone_number', $search);
//$this->db->or_like('cust_type', $search);
$this->db->or_like('dob', $search);
$this->db->or_like('allow_contract', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
}
/*Gets all of state*/
public function get_state() {
$this->db->from('state');
$this->db->order_by('state_name', 'asc');
return $this->db->get();
}
/*Gets information about a particular customer*/
public function get_info($customer_id){
$this->db->from('customers');
$this->db->where('customers.cust_id', $customer_id);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row();
}else{
$person_obj = parent::get_info(-1);
foreach($this->db->list_fields('customers') as $field)
{
$person_obj->$field = '';
}
return $person_obj;
}
}
/*Inserts or updates a customer*/
public function save_customer(&$customer_data, $customer_id = FALSE){
if(!$customer_id || !$this->exists($customer_id, TRUE)){
if($this->db->insert('customers', $customer_data)){
$customer_data['cust_id'] = $this->db->insert_id();
return TRUE;
}
return FALSE;
}
$this->db->where('cust_id', $customer_id);
return $this->db->update('customers', $customer_data);
}
/*Inserts or updates a customer from leads*/
public function lead_save_customer($customer_data, $mobile){
if(!$this->mob_exists($mobile)){
$this->db->insert('customers', $customer_data);
return $this->db->insert_id();
}else{
$this->db->where('phone_number', $mobile);
$this->db->update('customers', $customer_data);
return $this->mob_exists($mobile);
}
}
/*Deletes a list of customers*/
public function delete_list($customer_ids){
$this->db->where_in('cust_id', $customer_ids);
return $this->db->update('customers', array('deleted' => 1));
}
/*Returns all the customers*/
public function get_all($rows = 0, $limit_from = 0){
$this->db->from('customers');
$this->db->join('people', 'customers.person_id = people.person_id');
$this->db->where('deleted', 0);
$this->db->order_by('customer_name', 'asc');
if($rows > 0){
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
/*Gets information about multiple customers*/
public function get_multiple_info($customer_ids){
$this->db->from('customers');
$this->db->where_in('customers.cust_id', $customer_ids);
return $this->db->get()->result_array();
}
}
?>