File: /home/cafsindia/crm_cafsindia_com/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('cust_mobile', $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);
}
/*
Performs a search on customers
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'cust_name', $order = 'asc'){
$this->db->from('customers');
$this->db->group_start();
$this->db->like('cust_name', $search);
$this->db->or_like('cust_mobile', $search);
$this->db->or_like('cust_email', $search);
$this->db->or_like('cust_address', $search);
// $this->db->or_like('CONCAT(first_name, " ", last_name)', $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->where('deleted', 0);
$this->db->like('cust_name', $search);
$this->db->or_like('cust_mobile', $search);
$this->db->or_like('cust_email', $search);
$this->db->or_like('cust_address', $search);
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();
// echo $this->db->last_query(); die;
if($query->num_rows() == 1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $customer_id is NOT a customer
$person_obj = parent::get_info(-1);
//Get all the fields from customer table
//append those fields to base parent object, we we have a complete empty object
foreach($this->db->list_fields('customers') as $field)
{
$person_obj->$field = '';
}
return $person_obj;
}
}
// /*
// Gets total about a particular customer
// */
// public function get_totals($customer_id){
// $this->db->select('SUM(payment_amount) AS total');
// $this->db->from('sales');
// $this->db->join('sales_payments', 'sales.sale_id = sales_payments.sale_id');
// $this->db->where('cust_id', $customer_id);
// $this->db->where('sales.shop_id',$this->session->userdata('shop_id'));
// return $this->db->get()->row();
// }
/*
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('cust_mobile', $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));
}
/*
Chech mobile number
*/
// public function is_Mob_exists($mob){
// $shop_id = $this->session->userdata('shop_id');
// $query = $this->db->query("SELECT count(*) as count FROM ospos_customers inner JOIN ospos_people on ospos_customers.person_id = ospos_people.person_id where ospos_people.phone_number = '$mob' and ospos_customers.shop_id = '$shop_id '");
// $row = $query->row_array();
// $Count = $row['count'];
// if((int)$Count !== 0){
// return false;
// }else{
// return true;
// }
// }
// /*
// Checks if account number exists
// */
// public function account_number_exists($account_number, $person_id = ''){
// $this->db->from('customers');
// $this->db->where('account_number', $account_number);
// $this->db->where('shop_id',$this->session->userdata('shop_id'));
// if(!empty($person_id))
// {
// $this->db->where('person_id !=', $person_id);
// }
//
// return ($this->db->get()->num_rows() == 1);
// }
//
// /*
// Gets total of rows
// */
// public function get_total_rows(){
// $this->db->from('customers');
// $this->db->where('deleted', 0);
// $this->db->where('shop_id',$this->session->userdata('shop_id'));
// return $this->db->count_all_results();
// }
//
/*
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('cust_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){
//echo "SAT :: $customer_ids"; die;
$this->db->from('customers');
// $this->db->join('people', 'people.person_id = customers.person_id');
$this->db->where_in('customers.cust_id', $customer_ids);
return $this->db->get()->result_array();
//echo $this->db->last_query(); die;
}
// /*
// Deletes one customer
// */
// public function delete($customer_id){
// $this->db->where('person_id', $customer_id);
//
// return $this->db->update('customers', array('deleted' => 1));
// }
//
//
// /*
// Get search suggestions to find customers
// */
// public function get_search_suggestions($search, $unique = TRUE, $limit = 25){
// $suggestions = array();
//
// $this->db->from('customers');
// $this->db->join('people', 'customers.person_id = people.person_id');
// $this->db->group_start();
// $this->db->like('first_name', $search);
// $this->db->or_like('last_name', $search);
// $this->db->or_like('phone_number',$search);
// $this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
// $this->db->group_end();
// $this->db->where('deleted', 0);
// $this->db->where('customers.shop_id',$this->session->userdata('shop_id'));
// $this->db->order_by('last_name', 'asc');
// foreach($this->db->get()->result() as $row)
// {
// $suggestions[] = array('value' => $row->person_id, 'label' => $row->first_name.' '.$row->last_name);
// }
//
// if(!$unique)
// {
// $this->db->from('customers');
// $this->db->join('people', 'customers.person_id = people.person_id');
// $this->db->where('deleted', 0);
// $this->db->where('customers.shop_id',$this->session->userdata('shop_id'));
// $this->db->like('email', $search);
// $this->db->order_by('email', 'asc');
// foreach($this->db->get()->result() as $row)
// {
// $suggestions[] = array('value' => $row->person_id, 'label' => $row->email);
// }
//
// $this->db->from('customers');
// $this->db->join('people', 'customers.person_id = people.person_id');
// $this->db->where('deleted', 0);
// $this->db->where('customers.shop_id',$this->session->userdata('shop_id'));
// $this->db->like('phone_number', $search);
// $this->db->order_by('phone_number', 'asc');
// foreach($this->db->get()->result() as $row)
// {
// $suggestions[] = array('value' => $row->person_id, 'label' => $row->phone_number);
// }
//
// $this->db->from('customers');
// $this->db->join('people', 'customers.person_id = people.person_id');
// $this->db->where('deleted', 0);
// $this->db->where('customers.shop_id',$this->session->userdata('shop_id'));
// $this->db->like('account_number', $search);
// $this->db->order_by('account_number', 'asc');
// foreach($this->db->get()->result() as $row)
// {
// $suggestions[] = array('value' => $row->person_id, 'label' => $row->account_number);
// }
// }
//
// //only return $limit suggestions
// if(count($suggestions > $limit))
// {
// $suggestions = array_slice($suggestions, 0, $limit);
// }
//
// return $suggestions;
// }
//
}
?>