diff --git a/application/controllers/Customers.php b/application/controllers/Customers.php index c57c351ed..a3ff0fa07 100644 --- a/application/controllers/Customers.php +++ b/application/controllers/Customers.php @@ -17,6 +17,13 @@ class Customers extends Persons $this->_list_id = $CI->encryption->decrypt($CI->Appconfig->get('mailchimp_list_id')); } + public function index() + { + $data['table_headers'] = $this->xss_clean(get_customer_manage_table_headers()); + + $this->load->view('people/manage', $data); + } + /* Returns customer table data rows. This will be called with AJAX. */ @@ -34,7 +41,21 @@ class Customers extends Persons $data_rows = array(); foreach($customers->result() as $person) { - $data_rows[] = get_person_data_row($person, $this); + // retrieve the total amount the customer spent so far together with min, max and average values + $stats = $this->Customer->get_stats($person->person_id); + if(empty($stats)) + { + //create object with empty properties. + $stats = new stdClass; + $stats->total = 0; + $stats->min = 0; + $stats->max = 0; + $stats->average = 0; + $stats->avg_discount = 0; + $stats->quantity = 0; + } + + $data_rows[] = get_customer_data_row($person, $stats, $this); } $data_rows = $this->xss_clean($data_rows); @@ -90,7 +111,7 @@ class Customers extends Persons $data['customer_sales_tax_enabled'] = FALSE; } - // show the total amount the customer spent so far together with min, max and average values + // retrieve the total amount the customer spent so far together with min, max and average values $stats = $this->Customer->get_stats($customer_id); if(!empty($stats)) { diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index 9555ff162..e881a7c05 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -192,6 +192,46 @@ function get_person_data_row($person, $controller) )); } +function get_customer_manage_table_headers() +{ + $CI =& get_instance(); + + $headers = array( + array('people.person_id' => $CI->lang->line('common_id')), + array('last_name' => $CI->lang->line('common_last_name')), + array('first_name' => $CI->lang->line('common_first_name')), + array('email' => $CI->lang->line('common_email')), + array('phone_number' => $CI->lang->line('common_phone_number')), + array('total' => $CI->lang->line('common_total_spent'), 'sortable' => FALSE) + ); + + if($CI->Employee->has_grant('messages', $CI->session->userdata('person_id'))) + { + $headers[] = array('messages' => '', 'sortable' => FALSE); + } + + return transform_headers($headers); +} + +function get_customer_data_row($person, $stats, $controller) +{ + $CI =& get_instance(); + $controller_name = strtolower(get_class($CI)); + + return array ( + 'people.person_id' => $person->person_id, + 'last_name' => $person->last_name, + 'first_name' => $person->first_name, + 'email' => empty($person->email) ? '' : mailto($person->email, $person->email), + 'phone_number' => $person->phone_number, + 'total' => to_currency($stats->total), + 'messages' => empty($person->phone_number) ? '' : anchor("Messages/view/$person->person_id", '', + array('class'=>'modal-dlg', 'data-btn-submit' => $CI->lang->line('common_submit'), 'title'=>$CI->lang->line('messages_sms_send'))), + 'edit' => anchor($controller_name."/view/$person->person_id", '', + array('class'=>'modal-dlg', 'data-btn-submit' => $CI->lang->line('common_submit'), 'title'=>$CI->lang->line($controller_name.'_update')) + )); +} + function get_suppliers_manage_table_headers() { $CI =& get_instance(); diff --git a/application/language/en/common_lang.php b/application/language/en/common_lang.php index 4d9ac67af..17bd58e9a 100644 --- a/application/language/en/common_lang.php +++ b/application/language/en/common_lang.php @@ -57,6 +57,7 @@ $lang["common_search_options"] = "Search options"; $lang["common_searched_for"] = "Searched for"; $lang["common_state"] = "State"; $lang["common_submit"] = "Submit"; +$lang["common_total_spent"] = "Total Spent"; $lang["common_view_recent_sales"] = "View Recent Sales"; $lang["common_website"] = "website"; $lang["common_welcome"] = "Welcome"; diff --git a/application/models/Customer.php b/application/models/Customer.php index 9b5f472cb..ac75cf346 100644 --- a/application/models/Customer.php +++ b/application/models/Customer.php @@ -104,7 +104,7 @@ class Customer extends Person FROM ' . $this->db->dbprefix('sales') . ' AS sales INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items ON sales_items.sale_id = sales.sale_id - WHERE sales.customer_id=' . $this->db->escape($customer_id) . ' + WHERE sales.customer_id = ' . $this->db->escape($customer_id) . ' GROUP BY sale_id )' );