From d380524f9dd0cc19c86eba42346e9b96f92fd3d4 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Wed, 9 Nov 2016 10:06:09 +0000 Subject: [PATCH] Sales and Receivings code refactoring --- application/controllers/Receivings.php | 4 +-- application/controllers/Sales.php | 14 +++++----- application/libraries/Receiving_lib.php | 30 --------------------- application/libraries/Sale_lib.php | 36 ------------------------- application/models/Item_kit.php | 19 +++++++++++++ application/models/Receiving.php | 20 ++++++++++++++ application/models/Sale.php | 30 +++++++++++++++++++-- application/models/Sale_suspended.php | 12 --------- 8 files changed, 75 insertions(+), 90 deletions(-) diff --git a/application/controllers/Receivings.php b/application/controllers/Receivings.php index 102dda55f..2dda154f2 100644 --- a/application/controllers/Receivings.php +++ b/application/controllers/Receivings.php @@ -83,11 +83,11 @@ class Receivings extends Secure_Controller $quantity = ($mode == 'receive' || $mode == 'requisition') ? 1 : -1; $item_location = $this->receiving_lib->get_stock_source(); - if($mode == 'return' && $this->receiving_lib->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt)) + if($mode == 'return' && $this->Receiving->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt)) { $this->receiving_lib->return_entire_receiving($item_id_or_number_or_item_kit_or_receipt); } - elseif($this->receiving_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) + elseif($this->Item_kit->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) { $this->receiving_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $item_location); } diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 514ba99ce..667281e11 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -65,15 +65,13 @@ class Sales extends Secure_Controller $sort = $this->input->get('sort'); $order = $this->input->get('order'); - $is_valid_receipt = !empty($search) ? $this->sale_lib->is_valid_receipt($search) : FALSE; - $filters = array('sale_type' => 'all', 'location_id' => 'all', 'start_date' => $this->input->get('start_date'), 'end_date' => $this->input->get('end_date'), 'only_cash' => FALSE, 'only_invoices' => $this->config->item('invoice_enable') && $this->input->get('only_invoices'), - 'is_valid_receipt' => $is_valid_receipt); + 'is_valid_receipt' => $this->Sale->is_valid_receipt($search)); // check if any filter is set in the multiselect dropdown $filledup = array_fill_keys($this->input->get('filters'), TRUE); @@ -103,7 +101,7 @@ class Sales extends Secure_Controller $suggestions = array(); $receipt = $search = $this->input->get('term') != '' ? $this->input->get('term') : NULL; - if($this->sale_lib->get_mode() == 'return' && $this->sale_lib->is_valid_receipt($receipt)) + if($this->sale_lib->get_mode() == 'return' && $this->Sale->is_valid_receipt($receipt)) { // if a valid receipt or invoice was found the search term will be replaced with a receipt number (POS #) $suggestions[] = $receipt; @@ -280,11 +278,11 @@ class Sales extends Secure_Controller $item_location = $this->sale_lib->get_sale_location(); $item_id_or_number_or_item_kit_or_receipt = $this->input->post('item'); - if($mode == 'return' && $this->sale_lib->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt)) + if($mode == 'return' && $this->Sale->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt)) { $this->sale_lib->return_entire_sale($item_id_or_number_or_item_kit_or_receipt); } - elseif($this->sale_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) + elseif($this->Item_kit->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) { if(!$this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $item_location, $discount)) { @@ -382,7 +380,7 @@ class Sales extends Secure_Controller $customer_info = $this->_load_customer_data($customer_id, $data); $invoice_number = $this->_substitute_invoice_number($customer_info); - if($this->sale_lib->is_invoice_number_enabled() && $this->Sale->invoice_number_exists($invoice_number)) + if($this->sale_lib->is_invoice_number_enabled() && $this->Sale->check_invoice_number_exists($invoice_number)) { $data['error'] = $this->lang->line('sales_invoice_number_duplicate'); @@ -851,7 +849,7 @@ class Sales extends Secure_Controller { $sale_id = $this->input->post('sale_id'); $invoice_number = $this->input->post('invoice_number'); - $exists = !empty($invoice_number) && $this->Sale->invoice_number_exists($invoice_number, $sale_id); + $exists = !empty($invoice_number) && $this->Sale->check_invoice_number_exists($invoice_number, $sale_id); echo !$exists ? 'true' : 'false'; } diff --git a/application/libraries/Receiving_lib.php b/application/libraries/Receiving_lib.php index 25406a934..81e0e2585 100644 --- a/application/libraries/Receiving_lib.php +++ b/application/libraries/Receiving_lib.php @@ -263,36 +263,6 @@ class Receiving_lib $this->set_cart($items); } - public function is_valid_receipt($receipt_receiving_id) - { - //RECV # - $pieces = explode(' ', $receipt_receiving_id); - - if(count($pieces) == 2 && preg_match('/(RECV|KIT)/', $pieces[1])) - { - return $this->CI->Receiving->exists($pieces[1]); - } - else - { - return $this->CI->Receiving->get_receiving_by_reference($receipt_receiving_id)->num_rows() > 0; - } - - return FALSE; - } - - public function is_valid_item_kit($item_kit_id) - { - //KIT # - $pieces = explode(' ',$item_kit_id); - - if(count($pieces) == 2) - { - return $this->CI->Item_kit->exists($pieces[1]); - } - - return FALSE; - } - public function return_entire_receiving($receipt_receiving_id) { //RECV # diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index 670fb6bea..54c8224b1 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -439,42 +439,6 @@ class Sale_lib $this->set_cart($items); } - public function is_valid_receipt(&$receipt_sale_id) - { - //POS # - $pieces = explode(' ', $receipt_sale_id); - - if(count($pieces) == 2 && strtolower($pieces[0]) == 'pos') - { - return $this->CI->Sale->exists($pieces[1]); - } - elseif($this->CI->config->item('invoice_enable') == TRUE) - { - $sale_info = $this->CI->Sale->get_sale_by_invoice_number($receipt_sale_id); - if($sale_info->num_rows() > 0) - { - $receipt_sale_id = 'POS ' . $sale_info->row()->sale_id; - - return TRUE; - } - } - - return FALSE; - } - - public function is_valid_item_kit($item_kit_id) - { - //KIT # - $pieces = explode(' ', $item_kit_id); - - if(count($pieces) == 2) - { - return $this->CI->Item_kit->exists($pieces[1]); - } - - return FALSE; - } - public function return_entire_sale($receipt_sale_id) { //POS # diff --git a/application/models/Item_kit.php b/application/models/Item_kit.php index 8e4202565..91a673781 100644 --- a/application/models/Item_kit.php +++ b/application/models/Item_kit.php @@ -12,6 +12,25 @@ class Item_kit extends CI_Model return ($this->db->get()->num_rows() == 1); } + /* + Check if a given item_id is an item kit + */ + public function is_valid_item_kit($item_kit_id) + { + if(!empty($item_kit_id)) + { + //KIT # + $pieces = explode(' ', $item_kit_id); + + if(count($pieces) == 2 && preg_match('/(KIT)/', $pieces[0])) + { + return $this->exists($pieces[1]); + } + } + + return FALSE; + } + /* Gets total of rows */ diff --git a/application/models/Receiving.php b/application/models/Receiving.php index c12605efb..66b5182cb 100644 --- a/application/models/Receiving.php +++ b/application/models/Receiving.php @@ -19,6 +19,26 @@ class Receiving extends CI_Model return $this->db->get(); } + public function is_valid_receipt($receipt_receiving_id) + { + if(!empty($receipt_receiving_id)) + { + //RECV # + $pieces = explode(' ', $receipt_receiving_id); + + if(count($pieces) == 2 && preg_match('/(RECV|KIT)/', $pieces[0])) + { + return $this->exists($pieces[1]); + } + else + { + return $this->get_receiving_by_reference($receipt_receiving_id)->num_rows() > 0; + } + } + + return FALSE; + } + public function exists($receiving_id) { $this->db->from('receivings'); diff --git a/application/models/Sale.php b/application/models/Sale.php index 465440746..9ca10c593 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -183,7 +183,7 @@ class Sale extends CI_Model { $suggestions = array(); - if(!$this->sale_lib->is_valid_receipt($search)) + if(!$this->is_valid_receipt($search)) { $this->db->distinct(); $this->db->select('first_name, last_name'); @@ -238,6 +238,32 @@ class Sale extends CI_Model return ($start_from + $result['invoice_number_year']); } + + public function is_valid_receipt(&$receipt_sale_id) + { + if(!empty($receipt_sale_id)) + { + //POS # + $pieces = explode(' ', $receipt_sale_id); + + if(count($pieces) == 2 && preg_match('/(POS)/', $pieces[0])) + { + return $this->exists($pieces[1]); + } + elseif($this->config->item('invoice_enable') == TRUE) + { + $sale_info = $this->get_sale_by_invoice_number($receipt_sale_id); + if($sale_info->num_rows() > 0) + { + $receipt_sale_id = 'POS ' . $sale_info->row()->sale_id; + + return TRUE; + } + } + } + + return FALSE; + } public function exists($sale_id) { @@ -503,7 +529,7 @@ 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 check_invoice_number_exists($invoice_number, $sale_id = '') { $this->db->from('sales'); $this->db->where('invoice_number', $invoice_number); diff --git a/application/models/Sale_suspended.php b/application/models/Sale_suspended.php index c80609d3d..2f7d448e3 100644 --- a/application/models/Sale_suspended.php +++ b/application/models/Sale_suspended.php @@ -161,18 +161,6 @@ class Sale_suspended extends CI_Model return $this->db->get(); } - - 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)) - { - $this->db->where('sale_id !=', $sale_id); - } - - return ($this->db->get()->num_rows() == 1); - } public function get_comment($sale_id) {