diff --git a/application/controllers/Items.php b/application/controllers/Items.php index 34c2aafe6..574011c0e 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -57,6 +57,7 @@ class Items extends Secure_Controller $filters = array_merge($filters, $filledup); $items = $this->Item->search($search, $filters, $limit, $offset, $sort, $order); + $total_rows = $this->Item->get_found_rows($search, $filters); $data_rows = array(); @@ -120,6 +121,14 @@ class Items extends Secure_Controller echo json_encode($suggestions); } + public function suggest_kits() + { + $suggestions = $this->xss_clean($this->Item->get_kit_search_suggestions($this->input->post_get('term'), + array('search_custom' => FALSE, 'is_deleted' => FALSE), TRUE)); + + echo json_encode($suggestions); + } + /* Gives search suggestions based on what is being searched for */ diff --git a/application/models/Item.php b/application/models/Item.php index c81a64c5b..daf5666c4 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -496,6 +496,106 @@ class Item extends CI_Model return $suggestions; } + public function get_kit_search_suggestions($search, $filters = array('is_deleted' => FALSE, 'search_custom' => FALSE), $unique = FALSE, $limit = 25) + { + $suggestions = array(); + + $this->db->select('item_id, name'); + $this->db->from('items'); + $this->db->where('deleted', $filters['is_deleted']); + $this->db->where("item_type = '1'"); // standard, exclude kit items since kits will be picked up later + $this->db->like('name', $search); + $this->db->order_by('name', 'asc'); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('value' => $row->item_id, 'label' => $row->name); + } + + $this->db->select('item_id, item_number'); + $this->db->from('items'); + $this->db->where('deleted', $filters['is_deleted']); + $this->db->like('item_number', $search); + $this->db->order_by('item_number', 'asc'); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('value' => $row->item_id, 'label' => $row->item_number); + } + + if(!$unique) + { + //Search by category + $this->db->select('category'); + $this->db->from('items'); + $this->db->where('deleted', $filters['is_deleted']); + $this->db->distinct(); + $this->db->like('category', $search); + $this->db->order_by('category', 'asc'); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('label' => $row->category); + } + + //Search by supplier + $this->db->select('company_name'); + $this->db->from('suppliers'); + $this->db->like('company_name', $search); + // restrict to non deleted companies only if is_deleted is FALSE + $this->db->where('deleted', $filters['is_deleted']); + $this->db->distinct(); + $this->db->order_by('company_name', 'asc'); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('label' => $row->company_name); + } + + //Search by description + $this->db->select('item_id, name, description'); + $this->db->from('items'); + $this->db->where('deleted', $filters['is_deleted']); + $this->db->like('description', $search); + $this->db->order_by('description', 'asc'); + foreach($this->db->get()->result() as $row) + { + $entry = array('value' => $row->item_id, 'label' => $row->name); + if(!array_walk($suggestions, function($value, $label) use ($entry) { return $entry['label'] != $label; } )) + { + $suggestions[] = $entry; + } + } + + //Search by custom fields + if($filters['search_custom'] != FALSE) + { + $this->db->from('items'); + $this->db->group_start(); + $this->db->like('custom1', $search); + $this->db->or_like('custom2', $search); + $this->db->or_like('custom3', $search); + $this->db->or_like('custom4', $search); + $this->db->or_like('custom5', $search); + $this->db->or_like('custom6', $search); + $this->db->or_like('custom7', $search); + $this->db->or_like('custom8', $search); + $this->db->or_like('custom9', $search); + $this->db->or_like('custom10', $search); + $this->db->group_end(); + $this->db->where('deleted', $filters['is_deleted']); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('value' => $row->item_id, 'label' => $row->name); + } + } + } + + //only return $limit suggestions + if(count($suggestions > $limit)) + { + $suggestions = array_slice($suggestions, 0,$limit); + } + + return $suggestions; + } + public function get_category_suggestions($search) { $suggestions = array(); diff --git a/application/views/item_kits/form.php b/application/views/item_kits/form.php index dba610952..7559e7388 100644 --- a/application/views/item_kits/form.php +++ b/application/views/item_kits/form.php @@ -216,7 +216,7 @@ $(document).ready(function() $("#item_name").autocomplete({ - source: '', + source: '', minChars: 0, delay: 15, cacheLength: 1,