From 404fc5b5484db15b8b798a74dd9c63dfe50076d8 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sat, 28 May 2016 22:41:27 +0100 Subject: [PATCH] Sanity checked SQL escaping in all the models, improved code under different aspects (#598) --- application/models/Appconfig.php | 59 ++--- application/models/Customer.php | 160 ++++++------ application/models/Employee.php | 358 ++++++++++++++------------ application/models/Giftcard.php | 130 +++++----- application/models/Inventory.php | 9 +- application/models/Item.php | 180 +++++++------ application/models/Item_kit.php | 51 ++-- application/models/Item_kit_items.php | 8 +- application/models/Item_quantity.php | 30 +-- application/models/Item_taxes.php | 39 ++- application/models/Module.php | 38 +-- application/models/Person.php | 99 +++---- application/models/Receiving.php | 174 ++++++++----- application/models/Sale.php | 199 ++++++++------ application/models/Sale_suspended.php | 128 ++++----- application/models/Stock_location.php | 80 +++--- application/models/Supplier.php | 214 +++++++-------- 17 files changed, 1068 insertions(+), 888 deletions(-) diff --git a/application/models/Appconfig.php b/application/models/Appconfig.php index d9d3b2e59..d64bdaac2 100644 --- a/application/models/Appconfig.php +++ b/application/models/Appconfig.php @@ -1,83 +1,74 @@ db->from('app_config'); - $this->db->where('app_config.key',$key); - $query = $this->db->get(); - - return ($query->num_rows()==1); + $this->db->where('app_config.key', $key); + + return ($this->db->get()->num_rows() == 1); } - function get_all() + public function get_all() { $this->db->from('app_config'); - $this->db->order_by("key", "asc"); + $this->db->order_by('key', 'asc'); + return $this->db->get(); } - function get($key) + public function get($key) { $query = $this->db->get_where('app_config', array('key' => $key), 1); - - if($query->num_rows()==1) + + if($query->num_rows() == 1) { return $query->row()->value; } - - return ""; - + + return ''; } - function save($key, $value) + public function save($key, $value) { $config_data = array( - 'key'=>$key, - 'value'=>$value + 'key' => $key, + 'value' => $value ); - - if (!$this->exists($key)) + + if(!$this->exists($key)) { return $this->db->insert('app_config', $config_data); } - + $this->db->where('key', $key); return $this->db->update('app_config', $config_data); } - function batch_save($data) + public function batch_save($data) { - $success = true; - //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); foreach($data as $key=>$value) { - if(!$this->save($key, $value)) - { - $success = false; - break; - } + $this->save($key, $value); } - + $this->db->trans_complete(); - - return $success; + + return $this->db->trans_status(); } - function delete($key) + public function delete($key) { return $this->db->delete('app_config', array('key' => $key)); } - function delete_all() + public function delete_all() { return $this->db->empty_table('app_config'); } } - ?> \ No newline at end of file diff --git a/application/models/Customer.php b/application/models/Customer.php index e8b895e5f..b0b41dc22 100644 --- a/application/models/Customer.php +++ b/application/models/Customer.php @@ -4,30 +4,35 @@ class Customer extends Person /* Determines if a given person_id is a customer */ - function exists($person_id) + public function exists($person_id) { $this->db->from('customers'); $this->db->join('people', 'people.person_id = customers.person_id'); $this->db->where('customers.person_id', $person_id); - $query = $this->db->get(); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - - function account_number_exists($account_number,$person_id='') + + /* + 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); - if (!empty($person_id)) + + if(!empty($person_id)) { $this->db->where('person_id !=', $person_id); } - $query=$this->db->get(); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - - function get_total_rows() + + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('customers'); $this->db->where('deleted', 0); @@ -38,13 +43,14 @@ class Customer extends Person /* Returns all the customers */ - function get_all($rows = 0, $limit_from = 0) - { + 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("last_name", "asc"); - if ($rows > 0) + $this->db->order_by('last_name', 'asc'); + + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -55,7 +61,7 @@ class Customer extends Person /* Gets information about a particular customer */ - function get_info($customer_id) + public function get_info($customer_id) { $this->db->from('customers'); $this->db->join('people', 'people.person_id = customers.person_id'); @@ -69,15 +75,13 @@ class Customer extends Person else { //Get empty base parent object, as $customer_id is NOT a customer - $person_obj=parent::get_info(-1); + $person_obj = parent::get_info(-1); //Get all the fields from customer table - $fields = $this->db->list_fields('customers'); - //append those fields to base parent object, we we have a complete empty object - foreach ($fields as $field) + foreach($this->db->list_fields('customers') as $field) { - $person_obj->$field=''; + $person_obj->$field = ''; } return $person_obj; @@ -87,9 +91,9 @@ class Customer extends Person /* Gets total about a particular customer */ - function get_totals($customer_id) + public function get_totals($customer_id) { - $this->db->select('sum(payment_amount) as total', false); + $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('sales.customer_id', $customer_id); @@ -100,12 +104,12 @@ class Customer extends Person /* Gets information about multiple customers */ - function get_multiple_info($customer_ids) + public function get_multiple_info($customer_ids) { $this->db->from('customers'); $this->db->join('people', 'people.person_id = customers.person_id'); $this->db->where_in('customers.person_id', $customer_ids); - $this->db->order_by("last_name", "asc"); + $this->db->order_by('last_name', 'asc'); return $this->db->get(); } @@ -113,14 +117,14 @@ class Customer extends Person /* Inserts or updates a customer */ - function save_customer(&$person_data, &$customer_data, $customer_id=false) + public function save_customer(&$person_data, &$customer_data, $customer_id = FALSE) { //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); if(parent::save($person_data, $customer_id)) { - if (!$customer_id or !$this->exists($customer_id)) + if(!$customer_id or !$this->exists($customer_id)) { $customer_data['person_id'] = $person_data['person_id']; $this->db->insert('customers', $customer_data); @@ -140,7 +144,7 @@ class Customer extends Person /* Deletes one customer */ - function delete($customer_id) + public function delete($customer_id) { $this->db->where('person_id', $customer_id); @@ -150,7 +154,7 @@ class Customer extends Person /* Deletes a list of customers */ - function delete_list($customer_ids) + public function delete_list($customer_ids) { $this->db->where_in('person_id', $customer_ids); @@ -160,99 +164,105 @@ class Customer extends Person /* Get search suggestions to find customers */ - function get_search_suggestions($search, $unique=TRUE, $limit=25) + 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->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and - deleted = 0"); - $this->db->order_by("last_name", "asc"); - $by_name = $this->db->get(); - foreach($by_name->result() as $row) + $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('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); + $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); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->first_name.' '.$row->last_name); } - if (!$unique) + if(!$unique) { $this->db->from('customers'); - $this->db->join('people', 'customers.person_id=people.person_id'); + $this->db->join('people', 'customers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->like("email", $search); - $this->db->order_by("email", "asc"); - $by_email = $this->db->get(); - foreach($by_email->result() as $row) + $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); + $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->like("phone_number", $search); - $this->db->order_by("phone_number", "asc"); - $by_phone = $this->db->get(); - foreach($by_phone->result() as $row) + $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); + $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->like("account_number", $search); - $this->db->order_by("account_number", "asc"); - $by_account_number = $this->db->get(); - foreach($by_account_number->result() as $row) + $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); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->account_number); } } //only return $limit suggestions if(count($suggestions > $limit)) { - $suggestions = array_slice($suggestions, 0,$limit); + $suggestions = array_slice($suggestions, 0, $limit); } return $suggestions; } - function get_found_rows($search) + /* + Gets rows + */ + public function get_found_rows($search) { $this->db->from('customers'); $this->db->join('people', 'customers.person_id = people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - account_number LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and - deleted = 0"); + $this->db->group_start(); + $this->db->like('first_name', $search); + $this->db->or_like('last_name', $search); + $this->db->or_like('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('account_number', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); return $this->db->get()->num_rows(); } /* - Perform a search on customers + Performs a search on customers */ - function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc') + public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc') { $this->db->from('customers'); - $this->db->join('people', 'customers.person_id = people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - account_number LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and - deleted = 0"); + $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('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('account_number', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); $this->db->order_by($sort, $order); - if ($rows > 0) + + if($rows > 0) { $this->db->limit($rows, $limit_from); } diff --git a/application/models/Employee.php b/application/models/Employee.php index d577a02e5..9a39363a6 100644 --- a/application/models/Employee.php +++ b/application/models/Employee.php @@ -4,93 +4,97 @@ class Employee extends Person /* Determines if a given person_id is an employee */ - function exists($person_id) + public function exists($person_id) { $this->db->from('employees'); $this->db->join('people', 'people.person_id = employees.person_id'); - $this->db->where('employees.person_id',$person_id); - $query = $this->db->get(); - - return ($query->num_rows()==1); + $this->db->where('employees.person_id', $person_id); + + return ($this->db->get()->num_rows() == 1); } - - function get_total_rows() + + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('employees'); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); + return $this->db->count_all_results(); } + /* Returns all the employees */ - function get_all($limit=10000, $offset=0) + public function get_all($limit = 10000, $offset = 0) { $this->db->from('employees'); - $this->db->where('deleted',0); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->order_by("last_name", "asc"); + $this->db->where('deleted', 0); + $this->db->join('people', 'employees.person_id = people.person_id'); + $this->db->order_by('last_name', 'asc'); $this->db->limit($limit); $this->db->offset($offset); + return $this->db->get(); } /* Gets information about a particular employee */ - function get_info($employee_id) + public function get_info($employee_id) { $this->db->from('employees'); $this->db->join('people', 'people.person_id = employees.person_id'); - $this->db->where('employees.person_id',$employee_id); + $this->db->where('employees.person_id', $employee_id); $query = $this->db->get(); - - if($query->num_rows()==1) + + if($query->num_rows() == 1) { return $query->row(); } else { //Get empty base parent object, as $employee_id is NOT an employee - $person_obj=parent::get_info(-1); - + $person_obj = parent::get_info(-1); + //Get all the fields from employee table - $fields = $this->db->list_fields('employees'); - //append those fields to base parent object, we we have a complete empty object - foreach ($fields as $field) + foreach($this->db->list_fields('employees') as $field) { - $person_obj->$field=''; + $person_obj->$field = ''; } - + return $person_obj; } } - + /* Gets information about multiple employees */ - function get_multiple_info($employee_ids) + public function get_multiple_info($employee_ids) { $this->db->from('employees'); $this->db->join('people', 'people.person_id = employees.person_id'); - $this->db->where_in('employees.person_id',$employee_ids); - $this->db->order_by("last_name", "asc"); + $this->db->where_in('employees.person_id', $employee_ids); + $this->db->order_by('last_name', 'asc'); + return $this->db->get(); } - + /* Inserts or updates an employee */ - function save_employee(&$person_data, &$employee_data,&$grants_data,$employee_id=false) + public function save_employee(&$person_data, &$employee_data, &$grants_data, $employee_id = FALSE) { - $success=false; - + $success = FALSE; + //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); - - if(parent::save($person_data,$employee_id)) + + if(parent::save($person_data, $employee_id)) { - if (!$employee_id or !$this->exists($employee_id)) + if(!$employee_id or !$this->exists($employee_id)) { $employee_data['person_id'] = $employee_id = $person_data['person_id']; $success = $this->db->insert('employees', $employee_data); @@ -100,7 +104,7 @@ class Employee extends Person $this->db->where('person_id', $employee_id); $success = $this->db->update('employees', $employee_data); } - + //We have either inserted or updated a new employee, now lets set permissions. if($success) { @@ -116,179 +120,199 @@ class Employee extends Person } } } - } - + $this->db->trans_complete(); - + return $success; } - + /* Deletes one employee */ - function delete($employee_id) + public function delete($employee_id) { - $success=false; - - //Don't let employee delete their self - if($employee_id==$this->get_logged_in_employee_info()->person_id) - return false; - + $success = FALSE; + + //Don't let employees delete theirself + if($employee_id == $this->get_logged_in_employee_info()->person_id) + { + return FALSE; + } + //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); - + //Delete permissions if($this->db->delete('grants', array('person_id' => $employee_id))) { $this->db->where('person_id', $employee_id); $success = $this->db->update('employees', array('deleted' => 1)); } - $this->db->trans_complete(); + + $this->db->trans_complete(); + return $success; } - + /* Deletes a list of employees */ - function delete_list($employee_ids) + public function delete_list($employee_ids) { - $success=false; - - //Don't let employee delete their self - if(in_array($this->get_logged_in_employee_info()->person_id,$employee_ids)) - return false; + $success = FALSE; + + //Don't let employees delete theirself + if(in_array($this->get_logged_in_employee_info()->person_id, $employee_ids)) + { + return FALSE; + } //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); - $this->db->where_in('person_id',$employee_ids); + $this->db->where_in('person_id', $employee_ids); //Delete permissions - if ($this->db->delete('grants')) + if($this->db->delete('grants')) { //delete from employee table - $this->db->where_in('person_id',$employee_ids); + $this->db->where_in('person_id', $employee_ids); $success = $this->db->update('employees', array('deleted' => 1)); } - $this->db->trans_complete(); + + $this->db->trans_complete(); + return $success; } - + /* Get search suggestions to find employees */ - function get_search_suggestions($search,$limit=5) + public function get_search_suggestions($search, $limit = 5) { $suggestions = array(); - - $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0"); - $this->db->order_by("last_name", "asc"); - $by_name = $this->db->get(); - foreach($by_name->result() as $row) - { - $suggestions[]=array('value' => $row->person_id, 'label' => $row->first_name.' '.$row->last_name); - } - - $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->where('deleted', 0); - $this->db->like("email",$search); - $this->db->order_by("email", "asc"); - $by_email = $this->db->get(); - foreach($by_email->result() as $row) - { - $suggestions[]=array('value' => $row->person_id, 'label' => $row->email); - } - - $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->where('deleted', 0); - $this->db->like("username",$search); - $this->db->order_by("username", "asc"); - $by_username = $this->db->get(); - foreach($by_username->result() as $row) - { - $suggestions[]=array('value' => $row->person_id, 'label' => $row->username); - } - $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); + $this->db->join('people', 'employees.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('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); $this->db->where('deleted', 0); - $this->db->like("phone_number",$search); - $this->db->order_by("phone_number", "asc"); - $by_phone = $this->db->get(); - foreach($by_phone->result() as $row) + $this->db->order_by('last_name', 'asc'); + foreach($this->db->get()->result() as $row) { - $suggestions[]=array('value' => $row->person_id, 'label' => $row->phone_number); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->first_name.' '.$row->last_name); } - - + + $this->db->from('employees'); + $this->db->join('people', 'employees.person_id = people.person_id'); + $this->db->where('deleted', 0); + $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('employees'); + $this->db->join('people', 'employees.person_id = people.person_id'); + $this->db->where('deleted', 0); + $this->db->like('username', $search); + $this->db->order_by('username', 'asc'); + foreach($this->db->get()->result() as $row) + { + $suggestions[] = array('value' => $row->person_id, 'label' => $row->username); + } + + $this->db->from('employees'); + $this->db->join('people', 'employees.person_id = people.person_id'); + $this->db->where('deleted', 0); + $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); + } + //only return $limit suggestions if(count($suggestions > $limit)) { - $suggestions = array_slice($suggestions, 0,$limit); + $suggestions = array_slice($suggestions, 0, $limit); } + return $suggestions; - } - - function get_found_rows($search) + + /* + Gets rows + */ + public function get_found_rows($search) { $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - username LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0"); + $this->db->join('people', 'employees.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('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('username', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); + return $this->db->get()->num_rows(); } - + /* - Preform a search on employees + Performs a search on employees */ - function search($search, $rows = 0, $limit_from = 0, $sort = "last_name", $order = "asc") + public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc') { $this->db->from('employees'); - $this->db->join('people','employees.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - username LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0"); + $this->db->join('people', 'employees.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('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('username', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); $this->db->order_by($sort, $order); - if ($rows > 0) { + + if($rows > 0) + { $this->db->limit($rows, $limit_from); } + return $this->db->get(); } - + /* Attempts to login employee and set session. Returns boolean based on outcome. */ - function login($username, $password) + public function login($username, $password) { - $query = $this->db->get_where('employees', array('username' => $username,'password'=>md5($password), 'deleted'=>0), 1); - if ($query->num_rows() ==1) + $query = $this->db->get_where('employees', array('username' => $username, 'password' => md5($password), 'deleted' => 0), 1); + + if($query->num_rows() == 1) { - $row=$query->row(); + $row = $query->row(); $this->session->set_userdata('person_id', $row->person_id); - return true; + + return TRUE; } - return false; + + return FALSE; } - + /* Logs out a user by destorying all session data and redirect to login */ - function logout() + public function logout() { $this->session->sess_destroy(); redirect('login'); @@ -297,70 +321,78 @@ class Employee extends Person /* Determins if a employee is logged in */ - function is_logged_in() + public function is_logged_in() { - return $this->session->userdata('person_id')!=false; + return ($this->session->userdata('person_id') != FALSE); } - + /* Gets information about the currently logged in employee. */ - function get_logged_in_employee_info() + public function get_logged_in_employee_info() { if($this->is_logged_in()) { return $this->get_info($this->session->userdata('person_id')); } - - return false; + + return FALSE; } - + /* - * Determines whether the employee has access to at least one submodule + Determines whether the employee has access to at least one submodule */ - function has_module_grant($permission_id,$person_id) + public function has_module_grant($permission_id, $person_id) { $this->db->from('grants'); $this->db->like('permission_id', $permission_id, 'after'); - $this->db->where('person_id',$person_id); - $result = $this->db->get(); - $result_count = $result->num_rows(); - if ($result_count != 1) + $this->db->where('person_id', $person_id); + $result_count = $this->db->get()->num_rows(); + + if($result_count != 1) { - return $result_count != 0; + return ($result_count != 0); } + return $this->has_subpermissions($permission_id); } - - function has_subpermissions($permission_id) + + /* + Checks permissions + */ + public function has_subpermissions($permission_id) { $this->db->from('permissions'); $this->db->like('permission_id', $permission_id.'_', 'after'); - $result = $this->db->get(); - return $result->num_rows() == 0; + + return ($this->db->get()->num_rows() == 0); } - + /* Determines whether the employee specified employee has access the specific module. */ - function has_grant($permission_id,$person_id) + public function has_grant($permission_id, $person_id) { //if no module_id is null, allow access - if($permission_id==null) + if($permission_id == null) { - return true; + return TRUE; } - - $query = $this->db->get_where('grants', array('person_id'=>$person_id,'permission_id'=>$permission_id), 1); + + $query = $this->db->get_where('grants', array('person_id' => $person_id, 'permission_id' => $permission_id), 1); + return ($query->num_rows() == 1); } - - function get_employee_grants($person_id) + + /* + Gets employee permission grants + */ + public function get_employee_grants($person_id) { $this->db->from('grants'); - $this->db->where('person_id',$person_id); + $this->db->where('person_id', $person_id); + return $this->db->get()->result_array(); } - } ?> diff --git a/application/models/Giftcard.php b/application/models/Giftcard.php index 6dfdb2ef6..a3537d052 100644 --- a/application/models/Giftcard.php +++ b/application/models/Giftcard.php @@ -4,29 +4,29 @@ class Giftcard extends CI_Model /* Determines if a given giftcard_id is an giftcard */ - function exists($giftcard_id) + public function exists($giftcard_id) { $this->db->from('giftcards'); $this->db->where('giftcard_id', $giftcard_id); $this->db->where('deleted', 0); - return ($this->db->get()->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - function get_max_number() + /* + Gets max gift card number + */ + public function get_max_number() { $this->db->select_max('giftcard_number'); - $query = $this->db->get('giftcards'); - return $query->row(); + return $this->db->get('giftcards')->row(); } - function get_total_rows() - { - return $this->db->count_all('giftcards'); - } - - function count_all() + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('giftcards'); $this->db->where('deleted', 0); @@ -37,10 +37,10 @@ class Giftcard extends CI_Model /* Gets information about a particular giftcard */ - function get_info($giftcard_id) + public function get_info($giftcard_id) { $this->db->from('giftcards'); - $this->db->join('people', 'people.person_id=giftcards.person_id', 'left'); + $this->db->join('people', 'people.person_id = giftcards.person_id', 'left'); $this->db->where('giftcard_id', $giftcard_id); $this->db->where('deleted', 0); @@ -56,11 +56,9 @@ class Giftcard extends CI_Model $giftcard_obj = new stdClass(); //Get all the fields from giftcards table - $fields = $this->db->list_fields('giftcards'); - - foreach ($fields as $field) + foreach($this->db->list_fields('giftcards') as $field) { - $giftcard_obj->$field=''; + $giftcard_obj->$field = ''; } return $giftcard_obj; @@ -68,9 +66,9 @@ class Giftcard extends CI_Model } /* - Get an giftcard id given an giftcard number + Gets an giftcard id given an giftcard number */ - function get_giftcard_id($giftcard_number) + public function get_giftcard_id($giftcard_number) { $this->db->from('giftcards'); $this->db->where('giftcard_number', $giftcard_number); @@ -78,18 +76,18 @@ class Giftcard extends CI_Model $query = $this->db->get(); - if($query->num_rows()==1) + if($query->num_rows() == 1) { return $query->row()->giftcard_id; } - return false; + return FALSE; } /* Gets information about multiple giftcards */ - function get_multiple_info($giftcard_ids) + public function get_multiple_info($giftcard_ids) { $this->db->from('giftcards'); $this->db->where_in('giftcard_id', $giftcard_ids); @@ -102,18 +100,18 @@ class Giftcard extends CI_Model /* Inserts or updates a giftcard */ - function save(&$giftcard_data, $giftcard_id=false) + public function save(&$giftcard_data, $giftcard_id = FALSE) { - if (!$giftcard_id or !$this->exists($giftcard_id)) + if(!$giftcard_id or !$this->exists($giftcard_id)) { if($this->db->insert('giftcards', $giftcard_data)) { $giftcard_data['giftcard_id'] = $this->db->insert_id(); - return true; + return TRUE; } - return false; + return FALSE; } $this->db->where('giftcard_id', $giftcard_id); @@ -124,7 +122,7 @@ class Giftcard extends CI_Model /* Updates multiple giftcards at once */ - function update_multiple($giftcard_data, $giftcard_ids) + public function update_multiple($giftcard_data, $giftcard_ids) { $this->db->where_in('giftcard_id', $giftcard_ids); @@ -134,7 +132,7 @@ class Giftcard extends CI_Model /* Deletes one giftcard */ - function delete($giftcard_id) + public function delete($giftcard_id) { $this->db->where('giftcard_id', $giftcard_id); @@ -144,7 +142,7 @@ class Giftcard extends CI_Model /* Deletes a list of giftcards */ - function delete_list($giftcard_ids) + public function delete_list($giftcard_ids) { $this->db->where_in('giftcard_id', $giftcard_ids); @@ -154,7 +152,7 @@ class Giftcard extends CI_Model /* Get search suggestions to find giftcards */ - function get_search_suggestions($search, $limit=25) + public function get_search_suggestions($search, $limit = 25) { $suggestions = array(); @@ -162,25 +160,23 @@ class Giftcard extends CI_Model $this->db->like('giftcard_number', $search); $this->db->where('deleted', 0); $this->db->order_by('giftcard_number', 'asc'); - $by_number = $this->db->get(); - - foreach($by_number->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[]=array('label' => $row->giftcard_number); } $this->db->from('customers'); - $this->db->join('people', 'customers.person_id=people.person_id', 'left'); - $this->db->like('first_name', $this->db->escape_like_str($search)); - $this->db->or_like('last_name', $this->db->escape_like_str($search)); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); + $this->db->join('people', 'customers.person_id = people.person_id', 'left'); + $this->db->group_start(); + $this->db->like('first_name', $search); + $this->db->or_like('last_name', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); $this->db->where('deleted', 0); $this->db->order_by('last_name', 'asc'); - $by_name = $this->db->get(); - - foreach($by_name->result() as $row) + foreach($this->db->get()->result() as $row) { - $suggestions[]=array('label' => $row->first_name.' '.$row->last_name); + $suggestions[] = array('label' => $row->first_name.' '.$row->last_name); } //only return $limit suggestions @@ -193,23 +189,23 @@ class Giftcard extends CI_Model } /* - Preform a search on giftcards + Performs a search on giftcards */ - function search($search, $rows = 0, $limit_from = 0, $sort = 'giftcard_number', $order = 'asc') + public function search($search, $rows = 0, $limit_from = 0, $sort = 'giftcard_number', $order = 'asc') { $this->db->from('giftcards'); - $this->db->join('people', 'giftcards.person_id=people.person_id', 'left'); - $this->db->or_group_start(); - $this->db->like('first_name', $this->db->escape_like_str($search)); - $this->db->or_like('last_name', $this->db->escape_like_str($search)); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); - $this->db->or_like('giftcard_number', $this->db->escape_like_str($search)); - $this->db->or_like('giftcards.person_id', $this->db->escape_like_str($search)); + $this->db->join('people', 'giftcards.person_id = people.person_id', 'left'); + $this->db->group_start(); + $this->db->like('first_name', $search); + $this->db->or_like('last_name', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->or_like('giftcard_number', $search); + $this->db->or_like('giftcards.person_id', $search); $this->db->group_end(); $this->db->where('giftcards.deleted', 0); $this->db->order_by($sort, $order); - if ($rows > 0) + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -217,24 +213,31 @@ class Giftcard extends CI_Model return $this->db->get(); } - function get_found_rows($search) + /* + Gets gift cards + */ + public function get_found_rows($search) { $this->db->from('giftcards'); - $this->db->join('people', 'giftcards.person_id=people.person_id', 'left'); - $this->db->or_group_start(); - $this->db->like('first_name', $this->db->escape_like_str($search)); - $this->db->or_like('last_name', $this->db->escape_like_str($search)); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); - $this->db->or_like('giftcard_number', $this->db->escape_like_str($search)); - $this->db->or_like('giftcards.person_id', $this->db->escape_like_str($search)); + $this->db->join('people', 'giftcards.person_id = people.person_id', 'left'); + $this->db->group_start(); + $this->db->like('first_name', $search); + $this->db->or_like('last_name', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->or_like('giftcard_number', $search); + $this->db->or_like('giftcards.person_id', $search); $this->db->group_end(); $this->db->where('giftcards.deleted', 0); + return $this->db->get()->num_rows(); } - public function get_giftcard_value( $giftcard_number ) + /* + Gets gift card value + */ + public function get_giftcard_value($giftcard_number) { - if ( !$this->exists( $this->get_giftcard_id($giftcard_number) ) ) + if( !$this->exists($this->get_giftcard_id($giftcard_number)) ) { return 0; } @@ -245,7 +248,10 @@ class Giftcard extends CI_Model return $this->db->get()->row()->value; } - function update_giftcard_value( $giftcard_number, $value ) + /* + Updates gift card value + */ + public function update_giftcard_value($giftcard_number, $value) { $this->db->where('giftcard_number', $giftcard_number); $this->db->update('giftcards', array('value' => $value)); diff --git a/application/models/Inventory.php b/application/models/Inventory.php index 3017aceef..d1f18920c 100644 --- a/application/models/Inventory.php +++ b/application/models/Inventory.php @@ -1,23 +1,22 @@ db->insert('inventory', $inventory_data); } - function get_inventory_data_for_item($item_id, $location_id=false) + public function get_inventory_data_for_item($item_id, $location_id = FALSE) { $this->db->from('inventory'); $this->db->where('trans_items', $item_id); - if($location_id != false) + if($location_id != FALSE) { $this->db->where('trans_location', $location_id); } - $this->db->order_by("trans_date", "desc"); + $this->db->order_by('trans_date', 'desc'); return $this->db->get(); } } - ?> \ No newline at end of file diff --git a/application/models/Item.php b/application/models/Item.php index e7e4c967f..c85c2fb1f 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -8,24 +8,28 @@ class Item extends CI_Model { $this->db->from('items'); $this->db->where('item_id', $item_id); - $query = $this->db->get(); - return ($query->num_rows() == 1); + return ($this->db->get()->num_rows() == 1); } - - public function item_number_exists($item_number, $item_id='') + + /* + Determines if a given item_number exists + */ + public function item_number_exists($item_number, $item_id = '') { $this->db->from('items'); $this->db->where('item_number', $item_number); - if (!empty($item_id)) + if(!empty($item_id)) { $this->db->where('item_id !=', $item_id); } - $query=$this->db->get(); - return ($query->num_rows() == 1); + return ($this->db->get()->num_rows() == 1); } - + + /* + Gets total of rows + */ public function get_total_rows() { $this->db->from('items'); @@ -35,7 +39,7 @@ class Item extends CI_Model } /* - Get number of rows + Get number of rows */ public function get_found_rows($search, $filters) { @@ -43,74 +47,79 @@ class Item extends CI_Model } /* - Perform a search on items + Perform a search on items */ - public function search($search, $filters, $rows=0, $limit_from=0, $sort='items.name', $order='asc') + public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'items.name', $order = 'asc') { $this->db->from('items'); $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); $this->db->join('inventory', 'inventory.trans_items = items.item_id'); - if ($filters['stock_location_id'] > -1) + if($filters['stock_location_id'] > -1) { $this->db->join('item_quantities', 'item_quantities.item_id = items.item_id'); $this->db->where('location_id', $filters['stock_location_id']); } - if (empty($search)) + if(empty($search)) { $this->db->where('DATE_FORMAT(trans_date, "%Y-%m-%d") BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); } else { - if ($filters['search_custom'] == FALSE) + if($filters['search_custom'] == FALSE) { - $this->db->where("(name LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "item_number LIKE '" . $this->db->escape_like_str($search) . "%' OR " . - $this->db->dbprefix('items').".item_id LIKE '" . $this->db->escape_like_str($search) . "%' OR " . - "company_name LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "category LIKE '%" . $this->db->escape_like_str($search) . "%')"); + $this->db->group_start(); + $this->db->like('name', $search); + $this->db->or_like('item_number', $search); + $this->db->or_like('items.item_id', $search); + $this->db->or_like('company_name', $search); + $this->db->or_like('category', $search); + $this->db->group_end(); } else { - $this->db->where("(custom1 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom2 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom3 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom4 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom5 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom6 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom7 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom8 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom9 LIKE '%" . $this->db->escape_like_str($search) . "%' OR " . - "custom10 LIKE '%" . $this->db->escape_like_str($search) . "%')"); + $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('items.deleted', $filters['is_deleted']); - if ($filters['empty_upc'] != FALSE) + if($filters['empty_upc'] != FALSE) { - $this->db->where('item_number', null); + $this->db->where('item_number', NULL); } - if ($filters['low_inventory'] != FALSE) + if($filters['low_inventory'] != FALSE) { $this->db->where('quantity <=', 'reorder_level'); } - if ($filters['is_serialized'] != FALSE) + if($filters['is_serialized'] != FALSE) { $this->db->where('is_serialized', 1); } - if ($filters['no_description'] != FALSE) + if($filters['no_description'] != FALSE) { $this->db->where('items.description', ''); } - // avoid duplicate entry with same name because of inventory reporting multiple changes on the same item in the same date range + + // avoid duplicated entries with same name because of inventory reporting multiple changes on the same item in the same date range $this->db->group_by('items.item_id'); // order by name of item $this->db->order_by($sort, $order); - if ($rows > 0) + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -119,14 +128,14 @@ class Item extends CI_Model } /* - Returns all the items + Returns all the items */ - public function get_all($stock_location_id=-1, $rows=0, $limit_from=0) + public function get_all($stock_location_id = -1, $rows = 0, $limit_from = 0) { $this->db->from('items'); $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); - if ($stock_location_id > -1) + if($stock_location_id > -1) { $this->db->join('item_quantities', 'item_quantities.item_id=items.item_id'); $this->db->where('location_id', $stock_location_id); @@ -137,7 +146,7 @@ class Item extends CI_Model // order by name of item $this->db->order_by('items.name', 'asc'); - if ($rows > 0) + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -155,7 +164,7 @@ class Item extends CI_Model $this->db->from('items'); $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); $this->db->where('item_id', $item_id); - + $query = $this->db->get(); if($query->num_rows() == 1) @@ -172,7 +181,7 @@ class Item extends CI_Model foreach($fields as $field) { - $item_obj->$field=''; + $item_obj->$field = ''; } return $item_obj; @@ -191,7 +200,7 @@ class Item extends CI_Model $query = $this->db->get(); - if($query->num_rows()==1) + if($query->num_rows() == 1) { return $query->row()->item_id; } @@ -216,7 +225,7 @@ class Item extends CI_Model /* Inserts or updates a item */ - public function save(&$item_data, $item_id=FALSE) + public function save(&$item_data, $item_id = FALSE) { if(!$item_id or !$this->exists($item_id)) { @@ -250,10 +259,17 @@ class Item extends CI_Model */ public function delete($item_id) { + //Run these queries as a transaction, we want to make sure we do all or nothing + $this->db->trans_start(); + // set to 0 quantities $this->Item_quantity->reset_quantity($item_id); $this->db->where('item_id', $item_id); - return $this->db->update('items', array('deleted'=>1)); + $this->db->update('items', array('deleted'=>1)); + + $this->db->trans_complete(); + + return $this->db->trans_status(); } /* @@ -271,14 +287,20 @@ class Item extends CI_Model */ public function delete_list($item_ids) { + //Run these queries as a transaction, we want to make sure we do all or nothing + $this->db->trans_start(); + // set to 0 quantities $this->Item_quantity->reset_quantity_list($item_ids); - $this->db->where_in('item_id', $item_ids); - return $this->db->update('items', array('deleted'=>1)); + $this->db->update('items', array('deleted'=>1)); + + $this->db->trans_complete(); + + return $this->db->trans_status(); } - public function get_search_suggestions($search, $filters = array('is_deleted'=>FALSE, 'search_custom'=>FALSE), $unique = FALSE, $limit=25) + public function get_search_suggestions($search, $filters = array('is_deleted'=>FALSE, 'search_custom'=>FALSE), $unique = FALSE, $limit = 25) { $suggestions = array(); @@ -287,8 +309,7 @@ class Item extends CI_Model $this->db->where('deleted', $filters['is_deleted']); $this->db->like('name', $search); $this->db->order_by('name', 'asc'); - $by_name = $this->db->get(); - foreach($by_name->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('value' => $row->item_id, 'label' => $row->name); } @@ -298,35 +319,34 @@ class Item extends CI_Model $this->db->where('deleted', $filters['is_deleted']); $this->db->like('item_number', $search); $this->db->order_by('item_number', 'asc'); - $by_item_number = $this->db->get(); - foreach($by_item_number->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('value' => $row->item_id, 'label' => $row->item_number); } - if (!$unique) + 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'); - $by_category = $this->db->get(); - foreach($by_category->result() as $row) + 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 if false + // 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'); - $by_company_name = $this->db->get(); - foreach($by_company_name->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('label' => $row->company_name); } @@ -337,34 +357,33 @@ class Item extends CI_Model $this->db->where('deleted', $filters['is_deleted']); $this->db->like('description', $search); $this->db->order_by('description', 'asc'); - $by_description = $this->db->get(); - foreach($by_description->result() as $row) + 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; - })) { + if(!array_walk($suggestions, function($value, $label) use ($entry) { return $entry['label'] != $label; } )) + { $suggestions[] = $entry; } } //Search by custom fields - if ($filters['search_custom'] != FALSE) + 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']); - $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); - $by_description = $this->db->get(); - foreach($by_description->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('value' => $row->item_id, 'label' => $row->name); } @@ -389,8 +408,7 @@ class Item extends CI_Model $this->db->like('category', $search); $this->db->where('deleted', 0); $this->db->order_by('category', 'asc'); - $by_category = $this->db->get(); - foreach($by_category->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('label' => $row->category); } @@ -407,8 +425,7 @@ class Item extends CI_Model $this->db->like('location', $search); $this->db->where('deleted', 0); $this->db->order_by('location', 'asc'); - $by_category = $this->db->get(); - foreach($by_category->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('label' => $row->location); } @@ -425,8 +442,7 @@ class Item extends CI_Model $this->db->like('custom'.$field_no, $search); $this->db->where('deleted', 0); $this->db->order_by('custom'.$field_no, 'asc'); - $by_category = $this->db->get(); - foreach($by_category->result() as $row) + foreach($this->db->get()->result() as $row) { $row_array = (array) $row; $suggestions[] = array('label' => $row_array['custom'.$field_no]); diff --git a/application/models/Item_kit.php b/application/models/Item_kit.php index 91edef9b2..402f6cabf 100644 --- a/application/models/Item_kit.php +++ b/application/models/Item_kit.php @@ -4,15 +4,18 @@ class Item_kit extends CI_Model /* Determines if a given item_id is an item kit */ - function exists($item_kit_id) + public function exists($item_kit_id) { $this->db->from('item_kits'); $this->db->where('item_kit_id', $item_kit_id); - return ($this->db->get()->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - - function get_total_rows() + + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('item_kits'); @@ -22,7 +25,7 @@ class Item_kit extends CI_Model /* Gets information about a particular item kit */ - function get_info($item_kit_id) + public function get_info($item_kit_id) { $this->db->from('item_kits'); $this->db->where('item_kit_id', $item_kit_id); @@ -41,7 +44,7 @@ class Item_kit extends CI_Model //Get all the fields from items table $fields = $this->db->list_fields('item_kits'); - foreach ($fields as $field) + foreach($fields as $field) { $item_obj->$field = ''; } @@ -53,7 +56,7 @@ class Item_kit extends CI_Model /* Gets information about multiple item kits */ - function get_multiple_info($item_kit_ids) + public function get_multiple_info($item_kit_ids) { $this->db->from('item_kits'); $this->db->where_in('item_kit_id', $item_kit_ids); @@ -65,18 +68,18 @@ class Item_kit extends CI_Model /* Inserts or updates an item kit */ - function save(&$item_kit_data, $item_kit_id=false) + public function save(&$item_kit_data, $item_kit_id = FALSE) { - if (!$item_kit_id or !$this->exists($item_kit_id)) + if(!$item_kit_id or !$this->exists($item_kit_id)) { if($this->db->insert('item_kits', $item_kit_data)) { $item_kit_data['item_kit_id'] = $this->db->insert_id(); - return true; + return TRUE; } - return false; + return FALSE; } $this->db->where('item_kit_id', $item_kit_id); @@ -87,7 +90,7 @@ class Item_kit extends CI_Model /* Deletes one item kit */ - function delete($item_kit_id) + public function delete($item_kit_id) { return $this->db->delete('item_kits', array('item_kit_id' => $id)); } @@ -95,28 +98,26 @@ class Item_kit extends CI_Model /* Deletes a list of item kits */ - function delete_list($item_kit_ids) + public function delete_list($item_kit_ids) { $this->db->where_in('item_kit_id', $item_kit_ids); return $this->db->delete('item_kits'); } - function get_search_suggestions($search, $limit=25) + public function get_search_suggestions($search, $limit = 25) { $suggestions = array(); $this->db->from('item_kits'); //KIT # - if (stripos($search, 'KIT ') !== false) + if(stripos($search, 'KIT ') !== FALSE) { $this->db->like('item_kit_id', str_ireplace('KIT ', '', $search)); - $this->db->order_by('item_kit_id', 'asc'); - $by_name = $this->db->get(); - foreach($by_name->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('value' => 'KIT '. $row->item_kit_id, 'label' => 'KIT ' . $row->item_kit_id); } @@ -124,11 +125,9 @@ class Item_kit extends CI_Model else { $this->db->like('name', $search); - $this->db->order_by('name', 'asc'); - $by_name = $this->db->get(); - foreach($by_name->result() as $row) + foreach($this->db->get()->result() as $row) { $suggestions[] = array('value' => 'KIT ' . $row->item_kit_id, 'label' => $row->name); } @@ -146,21 +145,21 @@ class Item_kit extends CI_Model /* Perform a search on items */ - function search($search, $rows=0, $limit_from=0, $sort='name', $order='asc') + public function search($search, $rows=0, $limit_from=0, $sort='name', $order='asc') { $this->db->from('item_kits'); $this->db->like('name', $search); $this->db->or_like('description', $search); //KIT # - if (stripos($search, 'KIT ') !== false) + if(stripos($search, 'KIT ') !== FALSE) { $this->db->or_like('item_kit_id', str_ireplace('KIT ', '', $search)); } $this->db->order_by($sort, $order); - if ($rows > 0) + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -168,14 +167,14 @@ class Item_kit extends CI_Model return $this->db->get(); } - function get_found_rows($search) + public function get_found_rows($search) { $this->db->from('item_kits'); $this->db->like('name', $search); $this->db->or_like('description', $search); //KIT # - if (stripos($search, 'KIT ') !== false) + if(stripos($search, 'KIT ') !== FALSE) { $this->db->or_like('item_kit_id', str_ireplace('KIT ', '', $search)); } diff --git a/application/models/Item_kit_items.php b/application/models/Item_kit_items.php index 34013be23..3c02454a7 100644 --- a/application/models/Item_kit_items.php +++ b/application/models/Item_kit_items.php @@ -4,7 +4,7 @@ class Item_kit_items extends CI_Model /* Gets item kit items for a particular item kit */ - function get_info($item_kit_id) + public function get_info($item_kit_id) { $this->db->from('item_kit_items'); $this->db->where('item_kit_id', $item_kit_id); @@ -16,14 +16,14 @@ class Item_kit_items extends CI_Model /* Inserts or updates an item kit's items */ - function save(&$item_kit_items_data, $item_kit_id) + public function save(&$item_kit_items_data, $item_kit_id) { //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); $this->delete($item_kit_id); - foreach ($item_kit_items_data as $row) + foreach($item_kit_items_data as $row) { $row['item_kit_id'] = $item_kit_id; $this->db->insert('item_kit_items', $row); @@ -37,7 +37,7 @@ class Item_kit_items extends CI_Model /* Deletes item kit items given an item kit */ - function delete($item_kit_id) + public function delete($item_kit_id) { return $this->db->delete('item_kit_items', array('item_kit_id' => $item_kit_id)); } diff --git a/application/models/Item_quantity.php b/application/models/Item_quantity.php index 24d9b733d..ac9ef37f0 100644 --- a/application/models/Item_quantity.php +++ b/application/models/Item_quantity.php @@ -1,19 +1,18 @@ db->from('item_quantities'); $this->db->where('item_id', $item_id); $this->db->where('location_id', $location_id); - $query = $this->db->get(); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - function save($location_detail, $item_id, $location_id) + public function save($location_detail, $item_id, $location_id) { - if (!$this->exists($item_id, $location_id)) + if(!$this->exists($item_id, $location_id)) { return $this->db->insert('item_quantities', $location_detail); } @@ -24,22 +23,23 @@ class Item_quantity extends CI_Model return $this->db->update('item_quantities', $location_detail); } - function get_item_quantity($item_id, $location_id) + public function get_item_quantity($item_id, $location_id) { $this->db->from('item_quantities'); $this->db->where('item_id', $item_id); $this->db->where('location_id', $location_id); $result = $this->db->get()->row(); - if(empty($result) == true) + if(empty($result) == TRUE) { //Get empty base parent object, as $item_id is NOT an item $result = new stdClass(); + //Get all the fields from items table (TODO to be reviewed) - $fields = $this->db->list_fields('item_quantities'); - foreach($fields as $field) + foreach($this->db->list_fields('item_quantities') as $field) { $result->$field = ''; } + $result->quantity = 0; } @@ -51,11 +51,11 @@ class Item_quantity extends CI_Model * if $quantity_change is negative, it will be subtracted, * if it is positive, it will be added to the current quantity */ - function change_quantity($item_id, $location_id, $quantity_change) + public function change_quantity($item_id, $location_id, $quantity_change) { $quantity_old = $this->get_item_quantity($item_id, $location_id); $quantity_new = $quantity_old->quantity + intval($quantity_change); - $location_detail = array('item_id'=>$item_id, 'location_id'=>$location_id, 'quantity'=>$quantity_new); + $location_detail = array('item_id' => $item_id, 'location_id' => $location_id, 'quantity' => $quantity_new); return $this->save($location_detail, $item_id, $location_id); } @@ -63,21 +63,21 @@ class Item_quantity extends CI_Model /* * Set to 0 all quantity in the given item */ - function reset_quantity($item_id) + public function reset_quantity($item_id) { $this->db->where('item_id', $item_id); - return $this->db->update('item_quantities', array('quantity'=>0)); + return $this->db->update('item_quantities', array('quantity' => 0)); } /* * Set to 0 all quantity in the given list of items */ - function reset_quantity_list($item_ids) + public function reset_quantity_list($item_ids) { $this->db->where_in('item_id', $item_ids); - return $this->db->update('item_quantities', array('quantity'=>0)); + return $this->db->update('item_quantities', array('quantity' => 0)); } } ?> \ No newline at end of file diff --git a/application/models/Item_taxes.php b/application/models/Item_taxes.php index e7f52ac14..562ef88ca 100644 --- a/application/models/Item_taxes.php +++ b/application/models/Item_taxes.php @@ -4,10 +4,11 @@ class Item_taxes extends CI_Model /* Gets tax info for a particular item */ - function get_info($item_id) + public function get_info($item_id) { $this->db->from('items_taxes'); $this->db->where('item_id',$item_id); + //return an array of taxes for an item return $this->db->get()->result_array(); } @@ -15,36 +16,52 @@ class Item_taxes extends CI_Model /* Inserts or updates an item's taxes */ - function save(&$items_taxes_data, $item_id) + public function save(&$items_taxes_data, $item_id) { //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); $this->delete($item_id); - $result = TRUE; - foreach ($items_taxes_data as $row) + + foreach($items_taxes_data as $row) { $row['item_id'] = $item_id; - $result &= $this->db->insert('items_taxes', $row); + $this->db->insert('items_taxes', $row); } $this->db->trans_complete(); - return $result; + return $this->db->trans_status(); } - - function save_multiple(&$items_taxes_data, $item_ids) + + /* + Saves taxes for multiple items + */ + public function save_multiple(&$items_taxes_data, $item_ids) { - foreach(explode(":", $item_ids) as $item_id) + //Run these queries as a transaction, we want to make sure we do all or nothing + $this->db->trans_start(); + + foreach(explode(':', $item_ids) as $item_id) { - $this->save($items_taxes_data, $item_id); + $this->delete($item_id); + + foreach($items_taxes_data as $row) + { + $row['item_id'] = $item_id; + $this->db->insert('items_taxes', $row); + } } + + $this->db->trans_complete(); + + return $this->db->trans_status(); } /* Deletes taxes given an item */ - function delete($item_id) + public function delete($item_id) { return $this->db->delete('items_taxes', array('item_id' => $item_id)); } diff --git a/application/models/Module.php b/application/models/Module.php index 32d5365d1..e89a9bc57 100644 --- a/application/models/Module.php +++ b/application/models/Module.php @@ -6,62 +6,68 @@ class Module extends CI_Model parent::__construct(); } - function get_module_name($module_id) + public function get_module_name($module_id) { $query = $this->db->get_where('modules', array('module_id' => $module_id), 1); - if ($query->num_rows() ==1) + if($query->num_rows() == 1) { $row = $query->row(); + return $this->lang->line($row->name_lang_key); } return $this->lang->line('error_unknown'); } - function get_module_desc($module_id) + public function get_module_desc($module_id) { $query = $this->db->get_where('modules', array('module_id' => $module_id), 1); - if ($query->num_rows() ==1) + + if($query->num_rows() == 1) { $row = $query->row(); + return $this->lang->line($row->desc_lang_key); } return $this->lang->line('error_unknown'); } - function get_all_permissions() + public function get_all_permissions() { $this->db->from('permissions'); + return $this->db->get(); } - function get_all_subpermissions() + public function get_all_subpermissions() { $this->db->from('permissions'); - $this->db->join('modules', 'modules.module_id=permissions.module_id'); + $this->db->join('modules', 'modules.module_id = permissions.module_id'); // can't quote the parameters correctly when using different operators.. - $this->db->where($this->db->dbprefix('modules').'.module_id!=', 'permission_id', FALSE); + $this->db->where($this->db->dbprefix('modules') . '.module_id!=', 'permission_id', FALSE); + return $this->db->get(); } - function get_all_modules() + public function get_all_modules() { $this->db->from('modules'); - $this->db->order_by("sort", "asc"); + $this->db->order_by('sort', 'asc'); + return $this->db->get(); } - function get_allowed_modules($person_id) + public function get_allowed_modules($person_id) { $this->db->from('modules'); - $this->db->join('permissions','permissions.permission_id=modules.module_id'); - $this->db->join('grants','permissions.permission_id=grants.permission_id'); - $this->db->where("person_id",$person_id); - $this->db->order_by("sort", "asc"); + $this->db->join('permissions', 'permissions.permission_id = modules.module_id'); + $this->db->join('grants', 'permissions.permission_id = grants.permission_id'); + $this->db->where('person_id', $person_id); + $this->db->order_by('sort', 'asc'); + return $this->db->get(); } - } ?> diff --git a/application/models/Person.php b/application/models/Person.php index 2198e7504..24aae3dc2 100644 --- a/application/models/Person.php +++ b/application/models/Person.php @@ -1,53 +1,60 @@ db->from('people'); - $this->db->where('people.person_id',$person_id); - $query = $this->db->get(); + $this->db->where('people.person_id', $person_id); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - /*Gets all people*/ - function get_all($limit=10000, $offset=0) + /* + Gets all people + */ + public function get_all($limit = 10000, $offset = 0) { $this->db->from('people'); - $this->db->order_by("last_name", "asc"); + $this->db->order_by('last_name', 'asc'); $this->db->limit($limit); $this->db->offset($offset); + return $this->db->get(); } - - function count_all() + + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('people'); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); + return $this->db->count_all_results(); } /* Gets information about a person as an array. */ - function get_info($person_id) + public function get_info($person_id) { $query = $this->db->get_where('people', array('person_id' => $person_id), 1); - if($query->num_rows()==1) + if($query->num_rows() == 1) { return $query->row(); } else { //create object with empty properties. - $fields = $this->db->list_fields('people'); $person_obj = new stdClass; - foreach ($fields as $field) + foreach($this->db->list_fields('people') as $field) { - $person_obj->$field=''; + $person_obj->$field = ''; } return $person_obj; @@ -57,62 +64,67 @@ class Person extends CI_Model /* Get people with specific ids */ - function get_multiple_info($person_ids) + public function get_multiple_info($person_ids) { $this->db->from('people'); - $this->db->where_in('person_id',$person_ids); - $this->db->order_by("last_name", "asc"); + $this->db->where_in('person_id', $person_ids); + $this->db->order_by('last_name', 'asc'); + return $this->db->get(); } /* Inserts or updates a person */ - function save(&$person_data,$person_id=false) + public function save(&$person_data, $person_id = FALSE) { - if (!$person_id or !$this->exists($person_id)) + if(!$person_id or !$this->exists($person_id)) { - if ($this->db->insert('people', $person_data)) + if($this->db->insert('people', $person_data)) { $person_data['person_id'] = $this->db->insert_id(); - return true; + + return TRUE; } - - return false; + + return FALSE; } $this->db->where('person_id', $person_id); - return $this->db->update('people',$person_data); + + return $this->db->update('people', $person_data); } /* - Get search suggestions to find customers + Get search suggestions to find person */ - function get_search_suggestions($search,$limit=25) + public function get_search_suggestions($search, $limit = 25) { $suggestions = array(); -// $this->db->select("person_id"); +// $this->db->select('person_id'); // $this->db->from('people'); -// $this->db->where('deleted',0); -// $this->db->where('person_id',$this->db->escape($search)); -// $this->db->like('first_name',$this->db->escape_like_str($search)); -// $this->db->or_like('last_name',$this->db->escape_like_str($search)); -// $this->db->or_like("CONCAT(`first_name`,' ',`last_name`)",$this->db->escape_like_str($search)); -// $this->db->or_like('email',$search); -// $this->db->or_like('phone_number',$search); -// $this->db->order_by('last_name', "asc"); +// $this->db->where('deleted', 0); +// $this->db->where('person_id', $search); +// $this->db->group_start(); +// $this->db->like('first_name', $search); +// $this->db->or_like('last_name', $search); +// $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); +// $this->db->or_like('email', $search); +// $this->db->or_like('phone_number', $search); +// $this->db->group_end(); +// $this->db->order_by('last_name', 'asc'); $by_person_id = $this->db->get(); foreach($by_person_id->result() as $row) { - $suggestions[]=array('label' => $row->person_id); + $suggestions[] = array('label' => $row->person_id); } //only return $limit suggestions if(count($suggestions > $limit)) { - $suggestions = array_slice($suggestions, 0,$limit); + $suggestions = array_slice($suggestions, 0, $limit); } return $suggestions; @@ -121,18 +133,17 @@ class Person extends CI_Model /* Deletes one Person (doesn't actually do anything) */ - function delete($person_id) + public function delete($person_id) { - return true; + return TRUE; } /* Deletes a list of people (doesn't actually do anything) */ - function delete_list($person_ids) + public function delete_list($person_ids) { - return true; + return TRUE; } - } ?> diff --git a/application/models/Receiving.php b/application/models/Receiving.php index c70750348..1c09186ac 100644 --- a/application/models/Receiving.php +++ b/application/models/Receiving.php @@ -1,67 +1,74 @@ db->from('receivings'); $this->db->join('people', 'people.person_id = receivings.supplier_id', 'LEFT'); - $this->db->where('receiving_id',$receiving_id); + $this->db->where('receiving_id', $receiving_id); + return $this->db->get(); } - - function get_invoice_count() + + /* + Gets total of invoice rows + */ + public function get_invoice_count() { $this->db->from('receivings'); - $this->db->where('invoice_number is not null'); + $this->db->where('invoice_number IS NOT NULL'); + return $this->db->count_all_results(); } - function get_receiving_by_invoice_number($invoice_number) + public function get_receiving_by_invoice_number($invoice_number) { $this->db->from('receivings'); $this->db->where('invoice_number', $invoice_number); + return $this->db->get(); } - function get_invoice_number_for_year($year='', $start_from = 0) + public function get_invoice_number_for_year($year = '', $start_from = 0) { $year = $year == '' ? date('Y') : $year; - $this->db->select("COUNT( 1 ) AS invoice_number_year", FALSE); + $this->db->select('COUNT( 1 ) AS invoice_number_year'); $this->db->from('receivings'); - $this->db->where("DATE_FORMAT(receiving_time, '%Y' ) = ", $year, FALSE); - $this->db->where("invoice_number IS NOT ", "NULL", FALSE); + $this->db->where('DATE_FORMAT(receiving_time, "%Y" ) = ', $year); + $this->db->where('invoice_number IS NOT NULL'); $result = $this->db->get()->row_array(); - return ($start_from + $result[ 'invoice_number_year' ] + 1); + + return ($start_from + $result['invoice_number_year'] + 1); } - function exists($receiving_id) + public function exists($receiving_id) { $this->db->from('receivings'); $this->db->where('receiving_id',$receiving_id); - $query = $this->db->get(); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - function update($receiving_data, $receiving_id) + public function update($receiving_data, $receiving_id) { $this->db->where('receiving_id', $receiving_id); - $success = $this->db->update('receivings',$receiving_data); - - return $success; + + return $this->db->update('receivings', $receiving_data); } - function save($items, $supplier_id, $employee_id, $comment, $invoice_number, $payment_type, $receiving_id=false) + public function save($items, $supplier_id, $employee_id, $comment, $invoice_number, $payment_type, $receiving_id = FALSE) { if(count($items)==0) + { return -1; + } $receivings_data = array( - 'supplier_id'=>$this->Supplier->exists($supplier_id) ? $supplier_id : null, - 'employee_id'=>$employee_id, - 'payment_type'=>$payment_type, - 'comment'=>$comment, - 'invoice_number'=>$invoice_number + 'supplier_id' => $this->Supplier->exists($supplier_id) ? $supplier_id : null, + 'employee_id' => $employee_id, + 'payment_type' => $payment_type, + 'comment' => $comment, + 'invoice_number' => $invoice_number ); //Run these queries as a transaction, we want to make sure we do all or nothing @@ -93,8 +100,7 @@ class Receiving extends CI_Model $items_received = $item['receiving_quantity'] != 0 ? $item['quantity'] * $item['receiving_quantity'] : $item['quantity']; // update cost price, if changed AND is set in config as wanted - if($cur_item_info->cost_price != $item['price'] - AND $this->config->item('receiving_calculate_average_price') != FALSE) + if($cur_item_info->cost_price != $item['price'] AND $this->config->item('receiving_calculate_average_price') != FALSE) { $this->Item->change_cost_price($item['item_id'], $items_received, @@ -107,11 +113,9 @@ class Receiving extends CI_Model $this->Item_quantity->save(array('quantity'=>$item_quantity->quantity + $items_received, 'item_id'=>$item['item_id'], 'location_id'=>$item['item_location']), $item['item_id'], $item['item_location']); - - + $recv_remarks ='RECV '.$receiving_id; - $inv_data = array - ( + $inv_data = array( 'trans_date'=>date('Y-m-d H:i:s'), 'trans_items'=>$item['item_id'], 'trans_user'=>$employee_id, @@ -119,13 +123,15 @@ class Receiving extends CI_Model 'trans_comment'=>$recv_remarks, 'trans_inventory'=>$items_received ); + $this->Inventory->insert($inv_data); $supplier = $this->Supplier->get_info($supplier_id); } + $this->db->trans_complete(); - if ($this->db->trans_status() === FALSE) + if($this->db->trans_status() === FALSE) { return -1; } @@ -133,34 +139,42 @@ class Receiving extends CI_Model return $receiving_id; } - function delete_list($receiving_ids,$employee_id,$update_inventory=TRUE) - { - $result = TRUE; - foreach($receiving_ids as $receiving_id) { - $result &= $this->delete($receiving_id,$employee_id,$update_inventory); - } - return $result; - } - - function delete($receiving_id,$employee_id,$update_inventory=TRUE) + public function delete_list($receiving_ids, $employee_id, $update_inventory = TRUE) { // start a transaction to assure data integrity $this->db->trans_start(); - if ($update_inventory) { + + foreach($receiving_ids as $receiving_id) + { + $this->delete($receiving_id, $employee_id, $update_inventory); + } + + // execute transaction + $this->db->trans_complete(); + + return $this->db->trans_status(); + } + + public function delete($receiving_id, $employee_id, $update_inventory = TRUE) + { + // start a transaction to assure data integrity + $this->db->trans_start(); + + if($update_inventory) + { // defect, not all item deletions will be undone?? // get array with all the items involved in the sale to update the inventory tracking $items = $this->get_receiving_items($receiving_id)->result_array(); - foreach($items as $item) { + foreach($items as $item) + { // create query to update inventory tracking - $inv_data = array - ( + $inv_data = array( 'trans_date'=>date('Y-m-d H:i:s'), 'trans_items'=>$item['item_id'], 'trans_user'=>$employee_id, 'trans_comment'=>'Deleting receiving ' . $receiving_id, 'trans_location'=>$item['item_location'], 'trans_inventory'=>$item['quantity_purchased']*-1 - ); // update inventory $this->Inventory->insert($inv_data); @@ -171,58 +185,80 @@ class Receiving extends CI_Model $item['quantity_purchased']*-1); } } + // delete all items $this->db->delete('receivings_items', array('receiving_id' => $receiving_id)); // delete sale itself $this->db->delete('receivings', array('receiving_id' => $receiving_id)); + // execute transaction $this->db->trans_complete(); return $this->db->trans_status(); } - function get_receiving_items($receiving_id) + public function get_receiving_items($receiving_id) { $this->db->from('receivings_items'); - $this->db->where('receiving_id',$receiving_id); + $this->db->where('receiving_id', $receiving_id); + return $this->db->get(); } - function get_supplier($receiving_id) + public function get_supplier($receiving_id) { $this->db->from('receivings'); - $this->db->where('receiving_id',$receiving_id); + $this->db->where('receiving_id', $receiving_id); + return $this->Supplier->get_info($this->db->get()->row()->supplier_id); } - function invoice_number_exists($invoice_number,$receiving_id='') + public function invoice_number_exists($invoice_number, $receiving_id = '') { $this->db->from('receivings'); $this->db->where('invoice_number', $invoice_number); - if (!empty($receiving_id)) + if(!empty($receiving_id)) { $this->db->where('receiving_id !=', $receiving_id); } - $query=$this->db->get(); - return ($query->num_rows()==1); + $query = $this->db->get(); + + return ($query->num_rows() == 1); } - //We create a temp table that allows us to do easy report/receiving queries - function create_receivings_items_temp_table() + /* + We create a temp table that allows us to do easy report/receiving queries + */ + public function create_receivings_items_temp_table() { - $this->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS ".$this->db->dbprefix('receivings_items_temp')." - (SELECT date(receiving_time) as receiving_date, ".$this->db->dbprefix('receivings_items').".receiving_id, comment, item_location, invoice_number, payment_type, employee_id, - ".$this->db->dbprefix('items').".item_id, ".$this->db->dbprefix('receivings').".supplier_id, quantity_purchased, ".$this->db->dbprefix('receivings_items').".receiving_quantity, - item_cost_price, item_unit_price, discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, - ".$this->db->dbprefix('receivings_items').".line as line, serialnumber, ".$this->db->dbprefix('receivings_items').".description as description, - (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as total, - (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit, - (item_cost_price*quantity_purchased) as cost - FROM ".$this->db->dbprefix('receivings_items')." - INNER JOIN ".$this->db->dbprefix('receivings')." ON ".$this->db->dbprefix('receivings_items').'.receiving_id='.$this->db->dbprefix('receivings').'.receiving_id'." - INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('receivings_items').'.item_id='.$this->db->dbprefix('items').'.item_id'." - GROUP BY receiving_id, item_id, line)"); + $this->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS " . $this->db->dbprefix('receivings_items_temp') . " + (SELECT + date(receiving_time) AS receiving_date, + " . $this->db->dbprefix('receivings_items') . " . receiving_id, + comment, + item_location, + invoice_number, + payment_type, + employee_id, + " . $this->db->dbprefix('items') . " . item_id, + " . $this->db->dbprefix('receivings') . " . supplier_id, + quantity_purchased, + " . $this->db->dbprefix('receivings_items') . " . receiving_quantity, + item_cost_price, + item_unit_price, + discount_percent, + (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) AS subtotal, + " . $this->db->dbprefix('receivings_items') . " . line AS line, + serialnumber, + " . $this->db->dbprefix('receivings_items') . " . description AS description, + (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) AS total, + (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) - (item_cost_price * quantity_purchased) AS profit, + (item_cost_price * quantity_purchased) AS cost + FROM " . $this->db->dbprefix('receivings_items') . " + INNER JOIN " . $this->db->dbprefix('receivings') . " ON " . $this->db->dbprefix('receivings_items') . '.receiving_id=' . $this->db->dbprefix('receivings') . '.receiving_id' . " + INNER JOIN " . $this->db->dbprefix('items') . " ON " . $this->db->dbprefix('receivings_items') . '.item_id=' . $this->db->dbprefix('items') . '.item_id' . " + GROUP BY receiving_id, item_id, line)" + ); } - } ?> diff --git a/application/models/Sale.php b/application/models/Sale.php index 01b8e06ad..35c57ae73 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -4,9 +4,9 @@ class Sale extends CI_Model public function get_info($sale_id) { $this->db->select('first_name, last_name, email, comment, sale_payment_amount AS amount_tendered, payment_type, - invoice_number, sale_time, employee_id, customer_id, comments, sale_id, (sale_payment_amount - total) AS change_due', FALSE); - $this->db->select('DATE_FORMAT(sale_time, "%d-%m-%Y") AS sale_date', FALSE); - $this->db->select('CONCAT(first_name, " ", last_name) AS customer_name', FALSE); + invoice_number, sale_time, employee_id, customer_id, comments, sale_id, (sale_payment_amount - total) AS change_due'); + $this->db->select('DATE_FORMAT(sale_time, "%d-%m-%Y") AS sale_date'); + $this->db->select('CONCAT(first_name, " ", last_name) AS customer_name'); $this->db->select('SUM(item_unit_price * quantity_purchased * (1 - discount_percent / 100)) AS amount_due'); $this->db->from('sales_items_temp'); $this->db->join('people', 'people.person_id = sales_items_temp.customer_id', 'left'); @@ -34,17 +34,17 @@ class Sale extends CI_Model CONCAT(customer.first_name, " ", customer.last_name) AS customer_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, sale_payment_amount AS amount_tendered, SUM(total) AS amount_due, (sale_payment_amount - SUM(total)) AS change_due, - payment_type, invoice_number', FALSE); + payment_type, invoice_number'); $this->db->from('sales_items_temp'); $this->db->join('people AS customer', 'sales_items_temp.customer_id = customer.person_id', 'left'); - if (empty($search)) + if(empty($search)) { $this->db->where('DATE(sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); } else { - if ($filters['is_valid_receipt'] != FALSE) + if($filters['is_valid_receipt'] != FALSE) { $pieces = explode(' ', $search); $this->db->where('sales_items_temp.sale_id', $pieces[1]); @@ -52,32 +52,34 @@ class Sale extends CI_Model else { - $this->db->like('last_name', $search); - $this->db->or_like('first_name', $search); - $this->db->or_like('CONCAT(customer.first_name, " ", last_name)', $search); + $this->db->group_start(); + $this->db->like('last_name', $search); + $this->db->or_like('first_name', $search); + $this->db->or_like('CONCAT(customer.first_name, " ", last_name)', $search); + $this->db->group_end(); } } - if ($filters['location_id'] != 'all') + if($filters['location_id'] != 'all') { $this->db->where('item_location', $filters['location_id']); } - if ($filters['sale_type'] == 'sales') + if($filters['sale_type'] == 'sales') { $this->db->where('quantity_purchased > 0'); } - elseif ($filters['sale_type'] == 'returns') + elseif($filters['sale_type'] == 'returns') { $this->db->where('quantity_purchased < 0'); } - if ($filters['only_invoices'] != FALSE) + if($filters['only_invoices'] != FALSE) { - $this->db->where('invoice_number <> ', 'NULL'); + $this->db->where('invoice_number IS NOT NULL'); } - if ($filters['only_cash'] != FALSE) + if($filters['only_cash'] != FALSE) { $this->db->like('payment_type ', $this->lang->line('sales_cash'), 'after'); } @@ -85,7 +87,7 @@ class Sale extends CI_Model $this->db->group_by('sale_id'); $this->db->order_by($sort, $order); - if ($rows > 0) + if($rows > 0) { $this->db->limit($rows, $limit_from); } @@ -99,45 +101,47 @@ class Sale extends CI_Model public function get_payments_summary($search, $filters) { // get payment summary - $this->db->select('payment_type, count(*) AS count, SUM(payment_amount) AS payment_amount', FALSE); + $this->db->select('payment_type, count(*) AS count, SUM(payment_amount) AS payment_amount'); $this->db->from('sales'); - $this->db->join('sales_payments', 'sales_payments.sale_id=sales.sale_id'); + $this->db->join('sales_payments', 'sales_payments.sale_id = sales.sale_id'); $this->db->join('people', 'people.person_id = sales.customer_id', 'left'); - if (empty($search)) + if(empty($search)) { - $this->db->where('DATE(sale_time) BETWEEN '. $this->db->escape($filters['start_date']). ' AND '. $this->db->escape($filters['end_date'])); + $this->db->where('DATE(sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); } else { - if ($filters['is_valid_receipt'] != FALSE) + if($filters['is_valid_receipt'] != FALSE) { $pieces = explode(' ',$search); $this->db->where('sales.sale_id', $pieces[1]); } else { - $this->db->like('last_name', $search); - $this->db->or_like('first_name', $search); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_start(); + $this->db->like('last_name', $search); + $this->db->or_like('first_name', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); } } - if ($filters['sale_type'] == 'sales') + if($filters['sale_type'] == 'sales') { $this->db->where('payment_amount > 0'); } - elseif ($filters['sale_type'] == 'returns') + elseif($filters['sale_type'] == 'returns') { $this->db->where('payment_amount < 0'); } - if ($filters['only_invoices'] != FALSE) + if($filters['only_invoices'] != FALSE) { - $this->db->where('invoice_number <> ', 'NULL'); + $this->db->where('invoice_number IS NOT NULL'); } - if ($filters['only_cash'] != FALSE) + if($filters['only_cash'] != FALSE) { $this->db->like('payment_type ', $this->lang->line('sales_cash'), 'after'); } @@ -168,7 +172,10 @@ class Sale extends CI_Model return $payments; } - + + /* + Gets total of rows + */ public function get_total_rows() { $this->db->from('sales'); @@ -176,11 +183,11 @@ class Sale extends CI_Model return $this->db->count_all_results(); } - public function get_search_suggestions($search, $limit=25) + public function get_search_suggestions($search, $limit = 25) { $suggestions = array(); - if (!$this->sale_lib->is_valid_receipt($search)) + if(!$this->sale_lib->is_valid_receipt($search)) { $this->db->distinct(); $this->db->select('first_name, last_name'); @@ -204,10 +211,13 @@ class Sale extends CI_Model return $suggestions; } + /* + Gets total of invoice rows + */ public function get_invoice_count() { $this->db->from('sales'); - $this->db->where('invoice_number is not null'); + $this->db->where('invoice_number IS NOT NULL'); return $this->db->count_all_results(); } @@ -223,10 +233,10 @@ class Sale extends CI_Model public function get_invoice_number_for_year($year = '', $start_from = 0) { $year = $year == '' ? date('Y') : $year; - $this->db->select("COUNT( 1 ) AS invoice_number_year", FALSE); + $this->db->select('COUNT( 1 ) AS invoice_number_year'); $this->db->from('sales'); - $this->db->where("DATE_FORMAT(sale_time, '%Y' ) = ", $year, FALSE); - $this->db->where("invoice_number IS NOT ", "NULL", FALSE); + $this->db->where('DATE_FORMAT(sale_time, "%Y" ) = ', $year); + $this->db->where('invoice_number IS NOT NULL'); $result = $this->db->get()->row_array(); return ($start_from + $result['invoice_number_year']); @@ -252,7 +262,7 @@ class Sale extends CI_Model $this->db->trans_start(); // first delete all payments - $this->db->delete('sales_payments', array('sale_id'=>$sale_id)); + $this->db->delete('sales_payments', array('sale_id' => $sale_id)); // add new payments foreach($payments as $payment) @@ -261,7 +271,7 @@ class Sale extends CI_Model 'sale_id'=>$sale_id, 'payment_type'=>$payment['payment_type'], 'payment_amount'=>$payment['payment_amount'] - ); + ); $success = $this->db->insert('sales_payments', $sales_payments_data); } @@ -274,9 +284,9 @@ class Sale extends CI_Model return $success; } - public function save($items, $customer_id, $employee_id, $comment, $invoice_number, $payments, $sale_id=false) + public function save($items, $customer_id, $employee_id, $comment, $invoice_number, $payments, $sale_id = FALSE) { - if(count($items)==0) + if(count($items) == 0) { return -1; } @@ -297,7 +307,7 @@ class Sale extends CI_Model foreach($payments as $payment_id=>$payment) { - if ( substr( $payment['payment_type'], 0, strlen( $this->lang->line('sales_giftcard') ) ) == $this->lang->line('sales_giftcard') ) + if( substr( $payment['payment_type'], 0, strlen( $this->lang->line('sales_giftcard') ) ) == $this->lang->line('sales_giftcard') ) { // We have a gift card and we have to deduct the used value from the total value of the card. $splitpayment = explode( ':', $payment['payment_type'] ); @@ -357,7 +367,7 @@ class Sale extends CI_Model $this->Inventory->insert($inv_data); $customer = $this->Customer->get_info($customer_id); - if ($customer_id == -1 or $customer->taxable) + if($customer_id == -1 or $customer->taxable) { foreach($this->Item_taxes->get_info($item['item_id']) as $row) { @@ -373,7 +383,7 @@ class Sale extends CI_Model } $this->db->trans_complete(); - if ($this->db->trans_status() === FALSE) + if($this->db->trans_status() === FALSE) { return -1; } @@ -381,7 +391,7 @@ class Sale extends CI_Model return $sale_id; } - public function delete_list($sale_ids, $employee_id, $update_inventory=TRUE) + public function delete_list($sale_ids, $employee_id, $update_inventory = TRUE) { $result = TRUE; @@ -393,16 +403,17 @@ class Sale extends CI_Model return $result; } - public function delete($sale_id, $employee_id, $update_inventory=TRUE) + public function delete($sale_id, $employee_id, $update_inventory = TRUE) { // start a transaction to assure data integrity $this->db->trans_start(); + // first delete all payments $this->db->delete('sales_payments', array('sale_id' => $sale_id)); // then delete all taxes on items $this->db->delete('sales_items_taxes', array('sale_id' => $sale_id)); - if ($update_inventory) + if($update_inventory) { // defect, not all item deletions will be undone?? // get array with all the items involved in the sale to update the inventory tracking @@ -453,14 +464,14 @@ class Sale extends CI_Model return $this->db->get(); } - public function get_payment_options($giftcard=true) + public function get_payment_options($giftcard = TRUE) { $payments = array( - $this->lang->line('sales_debit') => $this->lang->line('sales_debit'), - $this->lang->line('sales_credit') => $this->lang->line('sales_credit'), - $this->lang->line('sales_cash') => $this->lang->line('sales_cash'), - $this->lang->line('sales_check') => $this->lang->line('sales_check') - ); + $this->lang->line('sales_debit') => $this->lang->line('sales_debit'), + $this->lang->line('sales_credit') => $this->lang->line('sales_credit'), + $this->lang->line('sales_cash') => $this->lang->line('sales_cash'), + $this->lang->line('sales_check') => $this->lang->line('sales_check') + ); if($giftcard) { @@ -478,11 +489,11 @@ class Sale extends CI_Model return $this->Customer->get_info($this->db->get()->row()->customer_id); } - public function invoice_number_exists($invoice_number, $sale_id='') + public function invoice_number_exists($invoice_number, $sale_id = '') { $this->db->from('sales'); $this->db->where('invoice_number', $invoice_number); - if (!empty($sale_id)) + if(!empty($sale_id)) { $this->db->where('sale_id !=', $sale_id); } @@ -490,9 +501,9 @@ class Sale extends CI_Model return ($this->db->get()->num_rows()==1); } - public function get_giftcard_value( $giftcardNumber ) + public function get_giftcard_value($giftcardNumber) { - if ( !$this->Giftcard->exists($this->Giftcard->get_giftcard_id($giftcardNumber)) ) + if(!$this->Giftcard->exists($this->Giftcard->get_giftcard_id($giftcardNumber))) { return 0; } @@ -506,43 +517,61 @@ class Sale extends CI_Model //We create a temp table that allows us to do easy report/sales queries public function create_sales_items_temp_table() { - if ($this->config->item('tax_included')) + if($this->config->item('tax_included')) { - $total = "1"; - $subtotal = "(1 - (SUM(1 - 100/(100+percent))))"; - $tax="(SUM(1 - 100/(100+percent)))"; + $total = '1'; + $subtotal = '(1 - (SUM(1 - 100 / (100 + percent))))'; + $tax = '(SUM(1 - 100 / (100 + percent)))'; } else { - $tax = "(SUM(percent)/100)"; - $total = "(1+(SUM(percent/100)))"; - $subtotal = "1"; + $tax = '(SUM(percent) / 100)'; + $total = '(1 + (SUM(percent / 100)))'; + $subtotal = '1'; } $decimals = totals_decimals(); - $this->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS ".$this->db->dbprefix('sales_items_temp')." - (SELECT date(sale_time) as sale_date, sale_time, ".$this->db->dbprefix('sales_items').".sale_id, comment, payments.payment_type, payments.sale_payment_amount, item_location, customer_id, employee_id, - ".$this->db->dbprefix('items').".item_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, SUM(percent) as item_tax_percent, - discount_percent, ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $subtotal, $decimals) as subtotal, - ".$this->db->dbprefix('sales_items').".line as line, serialnumber, ".$this->db->dbprefix('sales_items').".description as description, - ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $total, $decimals) as total, - ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $tax, $decimals) as tax, - ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100)- (item_cost_price*quantity_purchased), $decimals) as profit, - (item_cost_price * quantity_purchased) as cost, - invoice_number - FROM ".$this->db->dbprefix('sales_items')." - INNER JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'." - INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'." - INNER JOIN (SELECT sale_id, SUM(payment_amount) AS sale_payment_amount, - GROUP_CONCAT(CONCAT(payment_type,' ',payment_amount) SEPARATOR ', ') AS payment_type FROM " . $this->db->dbprefix('sales_payments') . " GROUP BY sale_id) AS payments - ON " . $this->db->dbprefix('sales_items') . '.sale_id'. "=" . "payments.sale_id - LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'." - LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON " - .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." and " - .$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." and " - .$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line'." - GROUP BY sale_id, item_id, line)"); + $this->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS " . $this->db->dbprefix('sales_items_temp') . " + (SELECT + date(sale_time) AS sale_date, + sale_time, + ".$this->db->dbprefix('sales_items').".sale_id, + comment, + payments.payment_type, + payments.sale_payment_amount, + item_location, + customer_id, + employee_id, + ".$this->db->dbprefix('items').".item_id, + supplier_id, + quantity_purchased, + item_cost_price, + item_unit_price, + SUM(percent) AS item_tax_percent, + discount_percent, + ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $subtotal, $decimals) AS subtotal, + ".$this->db->dbprefix('sales_items').".line AS line, + serialnumber, + ".$this->db->dbprefix('sales_items').".description AS description, + ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $total, $decimals) AS total, + ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100) * $tax, $decimals) AS tax, + ROUND((item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100)- (item_cost_price * quantity_purchased), $decimals) AS profit, + (item_cost_price * quantity_purchased) AS cost, + invoice_number + FROM ".$this->db->dbprefix('sales_items')." + INNER JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'." + INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'." + INNER JOIN (SELECT sale_id, SUM(payment_amount) AS sale_payment_amount, + GROUP_CONCAT(CONCAT(payment_type,' ',payment_amount) SEPARATOR ', ') AS payment_type + FROM " . $this->db->dbprefix('sales_payments') . " GROUP BY sale_id) AS payments ON " . $this->db->dbprefix('sales_items').'.sale_id'."=payments.sale_id + LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'." + LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON " + .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." AND " + .$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." AND " + .$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line'." + GROUP BY sale_id, item_id, line)" + ); //Update null item_tax_percents to be 0 instead of null $this->db->where('item_tax_percent IS NULL'); diff --git a/application/models/Sale_suspended.php b/application/models/Sale_suspended.php index 7c21c3ac9..715ad05ec 100644 --- a/application/models/Sale_suspended.php +++ b/application/models/Sale_suspended.php @@ -1,63 +1,70 @@ db->from('sales_suspended'); $this->db->order_by('sale_id'); + return $this->db->get(); } public function get_info($sale_id) { $this->db->from('sales_suspended'); - $this->db->where('sale_id',$sale_id); + $this->db->where('sale_id', $sale_id); $this->db->join('people', 'people.person_id = sales_suspended.customer_id', 'LEFT'); + return $this->db->get(); } - - function get_invoice_count() + + /* + Gets total of invocie rows + */ + public function get_invoice_count() { $this->db->from('sales_suspended'); - $this->db->where('invoice_number is not null'); + $this->db->where('invoice_number IS NOT NULL'); + return $this->db->count_all_results(); } - function get_sale_by_invoice_number($invoice_number) + public function get_sale_by_invoice_number($invoice_number) { $this->db->from('sales_suspended'); $this->db->where('invoice_number', $invoice_number); + return $this->db->get(); } - function exists($sale_id) + public function exists($sale_id) { $this->db->from('sales_suspended'); - $this->db->where('sale_id',$sale_id); - $query = $this->db->get(); + $this->db->where('sale_id', $sale_id); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - function update($sale_data, $sale_id) + public function update($sale_data, $sale_id) { $this->db->where('sale_id', $sale_id); - $success = $this->db->update('sales_suspended',$sale_data); - - return $success; + + return $this->db->update('sales_suspended', $sale_data); } - function save($items,$customer_id,$employee_id,$comment,$invoice_number,$payments,$sale_id=false) + public function save($items, $customer_id, $employee_id, $comment, $invoice_number, $payments, $sale_id = FALSE) { - if(count($items)==0) + if(count($items) == 0) + { return -1; + } $sales_data = array( - 'sale_time' => date('Y-m-d H:i:s'), - 'customer_id'=> $this->Customer->exists($customer_id) ? $customer_id : null, - 'employee_id'=>$employee_id, - 'comment'=>$comment, - 'invoice_number'=>$invoice_number + 'sale_time' => date('Y-m-d H:i:s'), + 'customer_id' => $this->Customer->exists($customer_id) ? $customer_id : null, + 'employee_id' => $employee_id, + 'comment' => $comment, + 'invoice_number' => $invoice_number ); //Run these queries as a transaction, we want to make sure we do all or nothing @@ -68,12 +75,12 @@ class Sale_suspended extends CI_Model foreach($payments as $payment_id=>$payment) { - $sales_payments_data = array - ( - 'sale_id'=>$sale_id, - 'payment_type'=>$payment['payment_type'], - 'payment_amount'=>$payment['payment_amount'] + $sales_payments_data = array( + 'sale_id' => $sale_id, + 'payment_type' => $payment['payment_type'], + 'payment_amount' => $payment['payment_amount'] ); + $this->db->insert('sales_suspended_payments', $sales_payments_data); } @@ -81,40 +88,42 @@ class Sale_suspended extends CI_Model { $cur_item_info = $this->Item->get_info($item['item_id']); - $sales_items_data = array - ( - 'sale_id'=>$sale_id, - 'item_id'=>$item['item_id'], - 'line'=>$item['line'], - 'description'=>character_limiter($item['description'], 30), - 'serialnumber'=>character_limiter($item['serialnumber'], 30), - 'quantity_purchased'=>$item['quantity'], - 'discount_percent'=>$item['discount'], - 'item_cost_price' => $cur_item_info->cost_price, - 'item_unit_price'=>$item['price'], - 'item_location'=>$item['item_location'] + $sales_items_data = array( + 'sale_id' => $sale_id, + 'item_id' => $item['item_id'], + 'line' => $item['line'], + 'description' => character_limiter($item['description'], 30), + 'serialnumber' => character_limiter($item['serialnumber'], 30), + 'quantity_purchased' => $item['quantity'], + 'discount_percent' => $item['discount'], + 'item_cost_price' => $cur_item_info->cost_price, + 'item_unit_price' => $item['price'], + 'item_location' => $item['item_location'] ); $this->db->insert('sales_suspended_items', $sales_items_data); $customer = $this->Customer->get_info($customer_id); - if ($customer_id == -1 or $customer->taxable) + if($customer_id == -1 or $customer->taxable) { foreach($this->Item_taxes->get_info($item['item_id']) as $row) { - $this->db->insert('sales_suspended_items_taxes', array( - 'sale_id' =>$sale_id, - 'item_id' =>$item['item_id'], - 'line' =>$item['line'], - 'name' =>$row['name'], - 'percent' =>$row['percent'] - )); + $sales_items_taxes = array( + 'sale_id' => $sale_id, + 'item_id' => $item['item_id'], + 'line' => $item['line'], + 'name' => $row['name'], + 'percent' => $row['percent'] + ); + + $this->db->insert('sales_suspended_items_taxes', $sales_items_taxes); } } } + $this->db->trans_complete(); - if ($this->db->trans_status() === FALSE) + if($this->db->trans_status() === FALSE) { return -1; } @@ -122,7 +131,7 @@ class Sale_suspended extends CI_Model return $sale_id; } - function delete($sale_id) + public function delete($sale_id) { //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); @@ -137,36 +146,39 @@ class Sale_suspended extends CI_Model return $this->db->trans_status(); } - function get_sale_items($sale_id) + public function get_sale_items($sale_id) { $this->db->from('sales_suspended_items'); - $this->db->where('sale_id',$sale_id); + $this->db->where('sale_id', $sale_id); + return $this->db->get(); } - function get_sale_payments($sale_id) + public function get_sale_payments($sale_id) { $this->db->from('sales_suspended_payments'); - $this->db->where('sale_id',$sale_id); + $this->db->where('sale_id', $sale_id); + return $this->db->get(); } - function invoice_number_exists($invoice_number,$sale_id='') + public function invoice_number_exists($invoice_number, $sale_id = '') { $this->db->from('sales_suspended'); $this->db->where('invoice_number', $invoice_number); - if (!empty($sale_id)) + if(!empty($sale_id)) { $this->db->where('sale_id !=', $sale_id); } - $query=$this->db->get(); - return ($query->num_rows()==1); + + return ($this->db->get()->num_rows() == 1); } - function get_comment($sale_id) + public function get_comment($sale_id) { $this->db->from('sales_suspended'); - $this->db->where('sale_id',$sale_id); + $this->db->where('sale_id', $sale_id); + return $this->db->get()->row()->comment; } } diff --git a/application/models/Stock_location.php b/application/models/Stock_location.php index f024e723b..9c533147a 100644 --- a/application/models/Stock_location.php +++ b/application/models/Stock_location.php @@ -1,46 +1,48 @@ db->from('stock_locations'); - $this->db->where('location_name',$location_name); - $query = $this->db->get(); + $this->db->where('location_name', $location_name); - return ($query->num_rows()>=1); + return ($this->db->get()->num_rows() >= 1); } - function get_all($limit=10000, $offset=0) + public function get_all($limit = 10000, $offset = 0) { $this->db->from('stock_locations'); $this->db->limit($limit); $this->db->offset($offset); + return $this->db->get(); } - function get_undeleted_all($module_id='items') + public function get_undeleted_all($module_id = 'items') { $this->db->from('stock_locations'); - $this->db->join('permissions','permissions.location_id=stock_locations.location_id'); - $this->db->join('grants','grants.permission_id=permissions.permission_id'); + $this->db->join('permissions', 'permissions.location_id = stock_locations.location_id'); + $this->db->join('grants', 'grants.permission_id = permissions.permission_id'); $this->db->where('person_id', $this->session->userdata('person_id')); $this->db->like('permissions.permission_id', $module_id, 'after'); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); + return $this->db->get(); } - function show_locations($module_id='items') + public function show_locations($module_id = 'items') { $stock_locations = $this->get_allowed_locations($module_id); + return count($stock_locations) > 1; } - function multiple_locations() + public function multiple_locations() { return $this->get_all()->num_rows() > 1; } - function get_allowed_locations($module_id='items') + public function get_allowed_locations($module_id = 'items') { $stock = $this->get_undeleted_all($module_id)->result_array(); $stock_locations = array(); @@ -48,47 +50,51 @@ class Stock_location extends CI_Model { $stock_locations[$location_data['location_id']] = $location_data['location_name']; } + return $stock_locations; } - function is_allowed_location($location_id, $module_id='items') + public function is_allowed_location($location_id, $module_id = 'items') { $this->db->from('stock_locations'); - $this->db->join('permissions','permissions.location_id=stock_locations.location_id'); - $this->db->join('grants','grants.permission_id=permissions.permission_id'); + $this->db->join('permissions', 'permissions.location_id = stock_locations.location_id'); + $this->db->join('grants', 'grants.permission_id = permissions.permission_id'); $this->db->where('person_id', $this->session->userdata('person_id')); $this->db->like('permissions.permission_id', $module_id, 'after'); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); $this->db->where('stock_locations.location_id', $location_id); - $query = $this->db->get(); - return ($query->num_rows()==1); + + return ($this->db->get()->num_rows() == 1); } - function get_default_location_id() + public function get_default_location_id() { $this->db->from('stock_locations'); - $this->db->join('permissions','permissions.location_id=stock_locations.location_id'); - $this->db->join('grants','grants.permission_id=permissions.permission_id'); + $this->db->join('permissions', 'permissions.location_id = stock_locations.location_id'); + $this->db->join('grants', 'grants.permission_id = permissions.permission_id'); $this->db->where('person_id', $this->session->userdata('person_id')); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); $this->db->limit(1); + return $this->db->get()->row()->location_id; } - function get_location_name($location_id) + public function get_location_name($location_id) { $this->db->from('stock_locations'); - $this->db->where('location_id',$location_id); + $this->db->where('location_id', $location_id); + return $this->db->get()->row()->location_name; } - function save(&$location_data,$location_id) + public function save(&$location_data, $location_id) { $location_name = $location_data['location_name']; - if (!$this->exists($location_name)) + if(!$this->exists($location_name)) { $this->db->trans_start(); + $location_data = array('location_name'=>$location_name, 'deleted'=>0); $this->db->insert('stock_locations', $location_data); $location_id = $this->db->insert_id(); @@ -96,15 +102,15 @@ class Stock_location extends CI_Model $this->_insert_new_permission('items', $location_id, $location_name); $this->_insert_new_permission('sales', $location_id, $location_name); $this->_insert_new_permission('receivings', $location_id, $location_name); - // insert quantities for existing items $items = $this->Item->get_all(); - foreach ($items->result_array() as $item) + foreach($items->result_array() as $item) { $quantity_data = array('item_id' => $item['item_id'], 'location_id' => $location_id, 'quantity' => 0); $this->db->insert('item_quantities', $quantity_data); } + $this->db->trans_complete(); return $this->db->trans_status(); @@ -113,20 +119,20 @@ class Stock_location extends CI_Model { $this->db->where('location_id', $location_id); - return $this->db->update('stock_locations',$location_data); + return $this->db->update('stock_locations', $location_data); } } - function _insert_new_permission($module, $location_id, $location_name) + private function _insert_new_permission($module, $location_id, $location_name) { // insert new permission for stock location - $permission_id = $module."_".$location_name; - $permission_data = array('permission_id'=>$permission_id, 'module_id'=>$module, 'location_id' => $location_id); + $permission_id = $module . '_' . $location_name; + $permission_data = array('permission_id' => $permission_id, 'module_id' => $module, 'location_id' => $location_id); $this->db->insert('permissions', $permission_data); // insert grants for new permission $employees = $this->Employee->get_all(); - foreach ($employees->result_array() as $employee) + foreach($employees->result_array() as $employee) { $grants_data = array('permission_id' => $permission_id, 'person_id' => $employee['person_id']); $this->db->insert('grants', $grants_data); @@ -136,17 +142,19 @@ class Stock_location extends CI_Model /* Deletes one item */ - function delete($location_id) + public function delete($location_id) { $this->db->trans_start(); + $this->db->where('location_id', $location_id); $this->db->update('stock_locations', array('deleted' => 1)); $this->db->where('location_id', $location_id); $this->db->delete('permissions'); + $this->db->trans_complete(); + + return $this->db->trans_status(); } - - } ?> \ No newline at end of file diff --git a/application/models/Supplier.php b/application/models/Supplier.php index 7ba625bf0..3934e2326 100644 --- a/application/models/Supplier.php +++ b/application/models/Supplier.php @@ -4,71 +4,67 @@ class Supplier extends Person /* Determines if a given person_id is a customer */ - function exists($person_id) + public function exists($person_id) { $this->db->from('suppliers'); $this->db->join('people', 'people.person_id = suppliers.person_id'); - $this->db->where('suppliers.person_id',$person_id); - $query = $this->db->get(); + $this->db->where('suppliers.person_id', $person_id); - return ($query->num_rows()==1); + return ($this->db->get()->num_rows() == 1); } - - function get_total_rows() + + /* + Gets total of rows + */ + public function get_total_rows() { $this->db->from('suppliers'); - $this->db->where('deleted',0); + $this->db->where('deleted', 0); + return $this->db->count_all_results(); } /* Returns all the suppliers */ - function get_all($limit_from = 0, $rows = 0) + public function get_all($limit_from = 0, $rows = 0) { $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->order_by("company_name", "asc"); - if ($rows > 0) { + $this->db->order_by('company_name', 'asc'); + if($rows > 0) + { $this->db->limit($rows, $limit_from); } + return $this->db->get(); } - function count_all() - { - $this->db->from('suppliers'); - $this->db->where('deleted',0); - return $this->db->count_all_results(); - } - /* Gets information about a particular supplier */ - function get_info($supplier_id) + public function get_info($supplier_id) { $this->db->from('suppliers'); $this->db->join('people', 'people.person_id = suppliers.person_id'); - $this->db->where('suppliers.person_id',$supplier_id); + $this->db->where('suppliers.person_id', $supplier_id); $query = $this->db->get(); - if($query->num_rows()==1) + if($query->num_rows() == 1) { return $query->row(); } else { //Get empty base parent object, as $supplier_id is NOT an supplier - $person_obj=parent::get_info(-1); - - //Get all the fields from supplier table - $fields = $this->db->list_fields('suppliers'); + $person_obj = parent::get_info(-1); + //Get all the fields from supplier table //append those fields to base parent object, we we have a complete empty object - foreach ($fields as $field) + foreach($this->db->list_fields('suppliers') as $field) { - $person_obj->$field=''; + $person_obj->$field = ''; } return $person_obj; @@ -78,12 +74,12 @@ class Supplier extends Person /* Gets information about multiple suppliers */ - function get_multiple_info($suppliers_ids) + public function get_multiple_info($suppliers_ids) { $this->db->from('suppliers'); $this->db->join('people', 'people.person_id = suppliers.person_id'); - $this->db->where_in('suppliers.person_id',$suppliers_ids); - $this->db->order_by("last_name", "asc"); + $this->db->where_in('suppliers.person_id', $suppliers_ids); + $this->db->order_by('last_name', 'asc'); return $this->db->get(); } @@ -91,172 +87,184 @@ class Supplier extends Person /* Inserts or updates a suppliers */ - function save_supplier(&$person_data, &$supplier_data,$supplier_id=false) + public function save_supplier(&$person_data, &$supplier_data, $supplier_id = FALSE) { - $success=false; - //Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); if(parent::save($person_data,$supplier_id)) { - if (!$supplier_id or !$this->exists($supplier_id)) + if(!$supplier_id or !$this->exists($supplier_id)) { $supplier_data['person_id'] = $person_data['person_id']; - $success = $this->db->insert('suppliers', $supplier_data); + $this->db->insert('suppliers', $supplier_data); } else { $this->db->where('person_id', $supplier_id); - $success = $this->db->update('suppliers', $supplier_data); + $this->db->update('suppliers', $supplier_data); } - } $this->db->trans_complete(); - return $success; + return $this->db->trans_status(); } /* Deletes one supplier */ - function delete($supplier_id) + public function delete($supplier_id) { $this->db->where('person_id', $supplier_id); + return $this->db->update('suppliers', array('deleted' => 1)); } /* Deletes a list of suppliers */ - function delete_list($supplier_ids) + public function delete_list($supplier_ids) { - $this->db->where_in('person_id',$supplier_ids); + $this->db->where_in('person_id', $supplier_ids); + return $this->db->update('suppliers', array('deleted' => 1)); } /* Get search suggestions to find suppliers */ - function get_search_suggestions($search, $unique = FALSE, $limit = 25) + public function get_search_suggestions($search, $unique = FALSE, $limit = 25) { $suggestions = array(); $this->db->from('suppliers'); - $this->db->join('people', 'suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->like("company_name", $search); - $this->db->order_by("company_name", "asc"); - $by_company_name = $this->db->get(); - foreach ($by_company_name->result() as $row) { + $this->db->like('company_name', $search); + $this->db->order_by('company_name', 'asc'); + foreach($this->db->get()->result() as $row) + { $suggestions[] = array('value' => $row->person_id, 'label' => $row->company_name); } $this->db->from('suppliers'); - $this->db->join('people', 'suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); $this->db->distinct(); - $this->db->like("agency_name", $search); - $this->db->where("agency_name", "<> null"); - $this->db->order_by("agency_name", "asc"); - $by_agency_name = $this->db->get(); - foreach ($by_agency_name->result() as $row) { + $this->db->like('agency_name', $search); + $this->db->where('agency_name IS NOT NULL'); + $this->db->order_by('agency_name', 'asc'); + foreach($this->db->get()->result() as $row) + { $suggestions[] = array('value' => $row->person_id, 'label' => $row->agency_name); } $this->db->from('suppliers'); - $this->db->join('people', 'suppliers.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%" . $this->db->escape_like_str($search) . "%' or - last_name LIKE '%" . $this->db->escape_like_str($search) . "%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%" . $this->db->escape_like_str($search) . "%') and deleted=0"); - $this->db->order_by("last_name", "asc"); - $by_name = $this->db->get(); - foreach ($by_name->result() as $row) { + $this->db->join('people', 'suppliers.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('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); + $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) + if(!$unique) { $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->like("email",$search); - $this->db->order_by("email", "asc"); - $by_email = $this->db->get(); - foreach($by_email->result() as $row) + $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); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->email); } $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->like("phone_number",$search); - $this->db->order_by("phone_number", "asc"); - $by_phone = $this->db->get(); - foreach($by_phone->result() as $row) + $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); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->phone_number); } $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); + $this->db->join('people', 'suppliers.person_id = people.person_id'); $this->db->where('deleted', 0); - $this->db->like("account_number",$search); - $this->db->order_by("account_number", "asc"); - $by_account_number = $this->db->get(); - foreach($by_account_number->result() as $row) + $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); + $suggestions[] = array('value' => $row->person_id, 'label' => $row->account_number); } } //only return $limit suggestions if(count($suggestions > $limit)) { - $suggestions = array_slice($suggestions, 0,$limit); + $suggestions = array_slice($suggestions, 0, $limit); } + return $suggestions; - } - function get_found_rows($search) + /* + Gets rows + */ + public function get_found_rows($search) { $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - company_name LIKE '%".$this->db->escape_like_str($search)."%' or - agency_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - account_number LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0"); + $this->db->join('people', 'suppliers.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('company_name', $search); + $this->db->or_like('agency_name', $search); + $this->db->or_like('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('account_number', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); + return $this->db->get()->num_rows(); } /* Perform a search on suppliers */ - function search($search, $rows = 0, $limit_from = 0, $sort = "last_name", $order = "asc") + public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc') { $this->db->from('suppliers'); - $this->db->join('people','suppliers.person_id=people.person_id'); - $this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or - last_name LIKE '%".$this->db->escape_like_str($search)."%' or - company_name LIKE '%".$this->db->escape_like_str($search)."%' or - agency_name LIKE '%".$this->db->escape_like_str($search)."%' or - email LIKE '%".$this->db->escape_like_str($search)."%' or - phone_number LIKE '%".$this->db->escape_like_str($search)."%' or - account_number LIKE '%".$this->db->escape_like_str($search)."%' or - CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0"); + $this->db->join('people', 'suppliers.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('company_name', $search); + $this->db->or_like('agency_name', $search); + $this->db->or_like('email', $search); + $this->db->or_like('phone_number', $search); + $this->db->or_like('account_number', $search); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $search); + $this->db->group_end(); + $this->db->where('deleted', 0); + $this->db->order_by($sort, $order); - if ($rows > 0) { + + if($rows > 0) + { $this->db->limit($rows, $limit_from); } + return $this->db->get(); } - } ?>