'.$this->lang->line('upload_no_file_selected').'
'); - } + } + + private function _check_encryption() + { + $encryption_key = $this->config->item('encryption_key'); + + // check if the encryption_key config item is the default one + if($encryption_key == '' || $encryption_key == 'YOUR KEY') + { + // Config path + $config_path = APPPATH . 'config/config.php'; + + // Open the file + $config = file_get_contents($config_path); + + // $key will be assigned a 32-byte (256-bit) hex-encoded random key + $key = bin2hex($this->encryption->create_key(32)); + + // replace the empty placeholder with a real randomly generated encryption key + if($encryption_key == '') + { + $config = str_replace("['encryption_key'] = '';", "['encryption_key'] = '" . $key . "';", $config); + } + else + { + $config = str_replace("['encryption_key'] = 'YOUR KEY';", "['encryption_key'] = '" . $key . "';", $config); + } + + // set the encryption key in the config item + $this->config->set_item('encryption_key', $key); + + // Write the new config.php file + $handle = fopen($config_path, 'w+'); + + // Chmod the file + @chmod($config_path, 0777); + + $result = FALSE; + + // Verify file permissions + if(is_writable($config_path)) + { + // Write the file + $result = (fwrite($handle, $config) === FALSE) ? FALSE : TRUE; + } + + // Chmod the file + @chmod($config_path, 0444); + + fclose($handle); + + return $result; + } + + return TRUE; + } public function backup_db() { diff --git a/application/controllers/Home.php b/application/controllers/Home.php index 59ca39547..a2b812dfa 100644 --- a/application/controllers/Home.php +++ b/application/controllers/Home.php @@ -16,9 +16,9 @@ class Home extends Secure_Controller public function logout() { - $this->Employee->logout(); - $this->track_page('logout', 'logout'); + + $this->Employee->logout(); } } ?> \ No newline at end of file diff --git a/application/controllers/Items.php b/application/controllers/Items.php index e31954b32..96cf176ac 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -172,8 +172,8 @@ class Items extends Secure_Controller if($item_id == -1) { - $data['default_tax_1_rate'] = $this->Appconfig->get('default_tax_1_rate'); - $data['default_tax_2_rate'] = $this->Appconfig->get('default_tax_2_rate'); + $data['default_tax_1_rate'] = $this->config->item('default_tax_1_rate'); + $data['default_tax_2_rate'] = $this->config->item('default_tax_2_rate'); $item_info->receiving_quantity = 0; $item_info->reorder_level = 0; @@ -269,7 +269,7 @@ class Items extends Secure_Controller $item = $this->xss_clean($item); // update the UPC/EAN/ISBN field if empty / NULL with the newly generated barcode - if(empty($item['item_number']) && $this->Appconfig->get('barcode_generate_if_empty')) + if(empty($item['item_number']) && $this->config->item('barcode_generate_if_empty')) { // get the newly generated barcode $barcode_instance = Barcode_lib::barcode_instance($item, $config); 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/Reports.php b/application/controllers/Reports.php index 7848280da..b89c3de0e 100644 --- a/application/controllers/Reports.php +++ b/application/controllers/Reports.php @@ -40,10 +40,13 @@ class Reports extends Secure_Controller //Summary sales report public function summary_sales($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_sales'); $model = $this->Summary_sales; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -63,7 +66,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -72,10 +75,13 @@ class Reports extends Secure_Controller //Summary categories report public function summary_categories($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_categories'); $model = $this->Summary_categories; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -95,7 +101,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -104,10 +110,13 @@ class Reports extends Secure_Controller //Summary customers report public function summary_customers($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_customers'); $model = $this->Summary_customers; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -127,7 +136,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -136,10 +145,13 @@ class Reports extends Secure_Controller //Summary suppliers report public function summary_suppliers($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_suppliers'); $model = $this->Summary_suppliers; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -159,7 +171,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -168,10 +180,13 @@ class Reports extends Secure_Controller //Summary items report public function summary_items($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_items'); $model = $this->Summary_items; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -191,7 +206,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -200,10 +215,13 @@ class Reports extends Secure_Controller //Summary employees report public function summary_employees($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_employees'); $model = $this->Summary_employees; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -223,7 +241,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -232,10 +250,13 @@ class Reports extends Secure_Controller //Summary taxes report public function summary_taxes($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_taxes'); $model = $this->Summary_taxes; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -253,7 +274,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -262,10 +283,13 @@ class Reports extends Secure_Controller //Summary discounts report public function summary_discounts($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_discounts'); $model = $this->Summary_discounts; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -280,7 +304,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -289,10 +313,13 @@ class Reports extends Secure_Controller //Summary payments report public function summary_payments($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_payments'); $model = $this->Summary_payments; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $tabular_data = array(); foreach($report_data as $row) @@ -308,7 +335,7 @@ class Reports extends Secure_Controller 'subtitle' => date($this->config->item('dateformat'), strtotime($start_date)) . '-' . date($this->config->item('dateformat'), strtotime($end_date)), 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'summary_data' => $summary ); $this->load->view('reports/tabular', $data); @@ -352,10 +379,13 @@ class Reports extends Secure_Controller //Graphical summary sales report public function graphical_summary_sales($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_sales'); $model = $this->Summary_sales; - - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -374,7 +404,7 @@ class Reports extends Secure_Controller 'chart_type' => 'reports/graphs/line', 'labels_1' => $labels, 'series_data_1' => $series, - 'summary_data_1' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))), + 'summary_data_1' => $summary, 'yaxis_title' => $this->lang->line('reports_revenue'), 'xaxis_title' => $this->lang->line('reports_date'), 'show_currency' => TRUE @@ -386,10 +416,13 @@ class Reports extends Secure_Controller //Graphical summary items report public function graphical_summary_items($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_items'); $model = $this->Summary_items; - - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -407,7 +440,7 @@ class Reports extends Secure_Controller 'chart_type' => 'reports/graphs/hbar', 'labels_1' => $labels, 'series_data_1' => $series, - 'summary_data_1' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))), + 'summary_data_1' => $summary, 'yaxis_title' => $this->lang->line('reports_items'), 'xaxis_title' => $this->lang->line('reports_revenue'), 'show_currency' => TRUE @@ -419,11 +452,13 @@ class Reports extends Secure_Controller //Graphical summary customers report public function graphical_summary_categories($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_categories'); $model = $this->Summary_categories; - - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); - $summary = $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))); + + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -451,11 +486,13 @@ class Reports extends Secure_Controller //Graphical summary suppliers report public function graphical_summary_suppliers($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_suppliers'); $model = $this->Summary_suppliers; - - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); - $summary = $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))); + + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -483,11 +520,13 @@ class Reports extends Secure_Controller //Graphical summary employees report public function graphical_summary_employees($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_employees'); $model = $this->Summary_employees; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); - $summary = $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -515,11 +554,13 @@ class Reports extends Secure_Controller //Graphical summary taxes report public function graphical_summary_taxes($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_taxes'); $model = $this->Summary_taxes; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); - $summary = $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -547,10 +588,13 @@ class Reports extends Secure_Controller //Graphical summary customers report public function graphical_summary_customers($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_customers'); $model = $this->Summary_customers; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -568,7 +612,7 @@ class Reports extends Secure_Controller 'chart_type' => 'reports/graphs/hbar', 'labels_1' => $labels, 'series_data_1' => $series, - 'summary_data_1' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))), + 'summary_data_1' => $summary, 'yaxis_title' => $this->lang->line('reports_customers'), 'xaxis_title' => $this->lang->line('reports_revenue'), 'show_currency' => TRUE @@ -580,10 +624,13 @@ class Reports extends Secure_Controller //Graphical summary discounts report public function graphical_summary_discounts($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_discounts'); $model = $this->Summary_discounts; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -601,7 +648,7 @@ class Reports extends Secure_Controller 'chart_type' => 'reports/graphs/bar', 'labels_1' => $labels, 'series_data_1' => $series, - 'summary_data_1' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))), + 'summary_data_1' => $summary, 'yaxis_title' => $this->lang->line('reports_count'), 'xaxis_title' => $this->lang->line('reports_discount_percent'), 'show_currency' => FALSE @@ -613,11 +660,13 @@ class Reports extends Secure_Controller //Graphical summary payments report public function graphical_summary_payments($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Summary_payments'); $model = $this->Summary_payments; - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); - $summary = $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))); + $report_data = $model->getData($inputs); + $summary = $this->xss_clean($model->getSummaryData($inputs)); $labels = array(); $series = array(); @@ -659,11 +708,15 @@ class Reports extends Secure_Controller public function specific_customer($start_date, $end_date, $customer_id, $sale_type) { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'customer_id' => $customer_id, 'sale_type' => $sale_type); + $this->load->model('reports/Specific_customer'); $model = $this->Specific_customer; + $model->create($inputs); + $headers = $this->xss_clean($model->getDataColumns()); - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'customer_id' => $customer_id, 'sale_type' => $sale_type)); + $report_data = $model->getData($inputs); $summary_data = array(); $details_data = array(); @@ -685,7 +738,7 @@ class Reports extends Secure_Controller 'headers' => $headers, 'summary_data' => $summary_data, 'details_data' => $details_data, - 'overall_summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'customer_id' => $customer_id, 'sale_type' => $sale_type))) + 'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular_details', $data); @@ -708,11 +761,15 @@ class Reports extends Secure_Controller public function specific_employee($start_date, $end_date, $employee_id, $sale_type) { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'employee_id' => $employee_id, 'sale_type' => $sale_type); + $this->load->model('reports/Specific_employee'); $model = $this->Specific_employee; + $model->create($inputs); + $headers = $this->xss_clean($model->getDataColumns()); - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'employee_id' => $employee_id, 'sale_type' => $sale_type)); + $report_data = $model->getData($inputs); $summary_data = array(); $details_data = array(); @@ -734,7 +791,7 @@ class Reports extends Secure_Controller 'headers' => $headers, 'summary_data' => $summary_data, 'details_data' => $details_data, - 'overall_summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date,'employee_id' => $employee_id, 'sale_type' => $sale_type))) + 'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular_details', $data); @@ -759,11 +816,15 @@ class Reports extends Secure_Controller public function specific_discount($start_date, $end_date, $discount, $sale_type) { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'discount' => $discount, 'sale_type' => $sale_type); + $this->load->model('reports/Specific_discount'); $model = $this->Specific_discount; + $model->create($inputs); + $headers = $this->xss_clean($model->getDataColumns()); - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'discount' => $discount, 'sale_type' => $sale_type)); + $report_data = $model->getData($inputs); $summary_data = array(); $details_data = array(); @@ -784,7 +845,7 @@ class Reports extends Secure_Controller 'headers' => $headers, 'summary_data' => $summary_data, 'details_data' => $details_data, - 'overall_summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date,'discount' => $discount, 'sale_type' => $sale_type))) + 'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular_details', $data); @@ -792,9 +853,13 @@ class Reports extends Secure_Controller public function get_detailed_sales_row($sale_id) { + $inputs = array('sale_id' => $sale_id); + $this->load->model('reports/Detailed_sales'); $model = $this->Detailed_sales; + $model->create($inputs); + $report_data = $model->getDataBySaleId($sale_id); $summary_data = $this->xss_clean(array( @@ -820,11 +885,15 @@ class Reports extends Secure_Controller public function detailed_sales($start_date, $end_date, $sale_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id); + $this->load->model('reports/Detailed_sales'); $model = $this->Detailed_sales; + + $model->create($inputs); $headers = $this->xss_clean($model->getDataColumns()); - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); $summary_data = array(); $details_data = array(); @@ -869,7 +938,7 @@ class Reports extends Secure_Controller 'editable' => 'sales', 'summary_data' => $summary_data, 'details_data' => $details_data, - 'overall_summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id))) + 'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular_details', $data); @@ -877,8 +946,12 @@ class Reports extends Secure_Controller public function get_detailed_receivings_row($receiving_id) { + $inputs = array('receiving_id' => $receiving_id); + $this->load->model('reports/Detailed_receivings'); $model = $this->Detailed_receivings; + + $model->create($inputs); $report_data = $model->getDataByReceivingId($receiving_id); @@ -902,11 +975,15 @@ class Reports extends Secure_Controller public function detailed_receivings($start_date, $end_date, $receiving_type, $location_id = 'all') { + $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'receiving_type' => $receiving_type, 'location_id' => $location_id); + $this->load->model('reports/Detailed_receivings'); $model = $this->Detailed_receivings; + + $model->create($inputs); $headers = $this->xss_clean($model->getDataColumns()); - $report_data = $model->getData(array('start_date' => $start_date, 'end_date' => $end_date, 'receiving_type' => $receiving_type, 'location_id' => $location_id)); + $report_data = $model->getData($inputs); $summary_data = array(); $details_data = array(); @@ -948,7 +1025,7 @@ class Reports extends Secure_Controller 'editable' => 'receivings', 'summary_data' => $summary_data, 'details_data' => $details_data, - 'overall_summary_data' => $this->xss_clean($model->getSummaryData(array('start_date' => $start_date, 'end_date' => $end_date, 'receiving_type' => $receiving_type, 'location_id' => $location_id))) + 'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular_details', $data); @@ -956,10 +1033,12 @@ class Reports extends Secure_Controller public function inventory_low() { + $inputs = array(); + $this->load->model('reports/Inventory_low'); $model = $this->Inventory_low; - $report_data = $model->getData(array()); + $report_data = $model->getData($inputs); $tabular_data = array(); foreach($report_data as $row) @@ -977,7 +1056,7 @@ class Reports extends Secure_Controller 'subtitle' => '', 'headers' => $this->xss_clean($model->getDataColumns()), 'data' => $tabular_data, - 'summary_data' => $this->xss_clean($model->getSummaryData(array())) + 'summary_data' => $this->xss_clean($model->getSummaryData($inputs)) ); $this->load->view('reports/tabular', $data); @@ -1000,10 +1079,12 @@ class Reports extends Secure_Controller public function inventory_summary($location_id = 'all', $item_count = 'all') { + $inputs = array('location_id' => $location_id, 'item_count' => $item_count); + $this->load->model('reports/Inventory_summary'); $model = $this->Inventory_summary; - $report_data = $model->getData(array('location_id' => $location_id, 'item_count' => $item_count)); + $report_data = $model->getData($inputs); $tabular_data = array(); foreach($report_data as $row) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 514ba99ce..d8c80b5b4 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -47,8 +47,6 @@ class Sales extends Secure_Controller public function get_row($row_id) { - $this->Sale->create_temp_table(); - $sale_info = $this->Sale->get_info($row_id)->row(); $data_row = $this->xss_clean(get_sale_data_row($sale_info, $this)); @@ -57,23 +55,19 @@ class Sales extends Secure_Controller public function search() { - $this->Sale->create_temp_table(); - $search = $this->input->get('search'); $limit = $this->input->get('limit'); $offset = $this->input->get('offset'); $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 +97,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 +274,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 +376,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'); @@ -595,8 +589,6 @@ class Sales extends Secure_Controller private function _load_sale_data($sale_id) { - $this->Sale->create_temp_table(); - $this->sale_lib->clear_all(); $sale_info = $this->Sale->get_info($sale_id)->row_array(); $this->sale_lib->copy_entire_sale($sale_id); @@ -615,7 +607,7 @@ class Sales extends Secure_Controller $data['show_stock_locations'] = $this->Stock_location->show_locations('sales'); $data['amount_change'] = $this->sale_lib->get_amount_due() * -1; $data['amount_due'] = $this->sale_lib->get_amount_due(); - $employee_info = $this->Employee->get_info($this->Employee->get_logged_in_employee_info()->person_id); + $employee_info = $this->Employee->get_info($this->sale_lib->get_employee()); $data['employee'] = $employee_info->first_name . ' ' . $employee_info->last_name; $this->_load_customer_data($this->sale_lib->get_customer(), $data); @@ -699,8 +691,6 @@ class Sales extends Secure_Controller $data['employees'][$employee->person_id] = $employee->first_name . ' ' . $employee->last_name; } - $this->Sale->create_temp_table(); - $sale_info = $this->xss_clean($this->Sale->get_info($sale_id)->row_array()); $data['selected_customer_name'] = $sale_info['customer_name']; $data['selected_customer_id'] = $sale_info['customer_id']; @@ -851,7 +841,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/helpers/table_helper.php b/application/helpers/table_helper.php index ce4b7ffac..7e02b565a 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -74,10 +74,10 @@ function get_sale_data_row($sale, $controller) } $row['receipt'] = anchor($controller_name."/receipt/$sale->sale_id", '', - array('title'=>$CI->lang->line('sales_show_receipt')) + array('title' => $CI->lang->line('sales_show_receipt')) ); $row['edit'] = anchor($controller_name."/edit/$sale->sale_id", '', - array('class'=>"modal-dlg print_hide", 'data-btn-delete' => $CI->lang->line('common_delete'), 'data-btn-submit' => $CI->lang->line('common_submit'), 'title' => $CI->lang->line($controller_name.'_update')) + array('class' => 'modal-dlg print_hide', 'data-btn-delete' => $CI->lang->line('common_delete'), 'data-btn-submit' => $CI->lang->line('common_submit'), 'title' => $CI->lang->line($controller_name.'_update')) ); return $row; diff --git a/application/hooks/db_log.php b/application/hooks/db_log.php index 81445547c..04459d5d7 100644 --- a/application/hooks/db_log.php +++ b/application/hooks/db_log.php @@ -8,7 +8,7 @@ function db_log_queries() if($CI->config->item('db_log_enabled')) { // Creating Query Log file with today's date in application/logs folder - $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.log'; + $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php'; // Opening file with pointer at the end of the file $handle = fopen($filepath, "a+"); diff --git a/application/language/id/common_lang.php b/application/language/id/common_lang.php index e35a7b8c8..46ff9fb05 100644 --- a/application/language/id/common_lang.php +++ b/application/language/id/common_lang.php @@ -19,9 +19,9 @@ $lang["common_fields_required_message"] = "Bagian yang ditandai warna merah waji $lang["common_first_name"] = "Nama Depan"; $lang["common_first_name_required"] = "Nama depan wajib diisi."; $lang["common_first_page"] = "First"; -$lang["common_gender"] = "Gender"; +$lang["common_gender"] = "Jenis Kelamin"; $lang["common_gender_female"] = "V"; -$lang["common_gender_male"] = "M"; +$lang["common_gender_male"] = "L"; $lang["common_inv"] = "Persediaan"; $lang["common_last_name"] = "Nama Belakang"; $lang["common_last_name_required"] = "Nama belakang wajib diisi."; diff --git a/application/language/th/common_lang.php b/application/language/th/common_lang.php index bde89eb76..337d796e4 100644 --- a/application/language/th/common_lang.php +++ b/application/language/th/common_lang.php @@ -13,7 +13,7 @@ $lang["common_delete"] = "ลบ"; $lang["common_print"] = "พิมพ์"; $lang["common_det"] = "สรุป"; $lang["common_edit"] = "แก้ไข"; -$lang["common_email"] = "Email"; +$lang["common_email"] = "อีเมล์"; $lang["common_email_invalid_format"] = "email address ไม่ถูกต้อง"; $lang["common_fields_required_message"] = "ช่องสีแดงต้องกรอก"; $lang["common_first_name"] = "ชื่อ"; diff --git a/application/language/th/config_lang.php b/application/language/th/config_lang.php index 90016deda..03bee54b7 100644 --- a/application/language/th/config_lang.php +++ b/application/language/th/config_lang.php @@ -2,7 +2,7 @@ $lang["config_address"] = "ที่อยู่"; $lang["config_address_required"] = "ที่อยู่ต้องกรอก"; -$lang["config_apostrophe"] = "apostrophe"; +$lang["config_apostrophe"] = "อัญประกาศเดี่ยว '"; $lang["config_backup_button"] = "สำรองข้อมูล"; $lang["config_backup_database"] = "สำรองฐานข้อมูล"; $lang["config_barcode"] = "ตั้งค่าระบบบาร์โค้ด"; @@ -77,7 +77,7 @@ $lang["config_default_tax_rate_number"] = "อัตราภาษีเริ $lang["config_default_tax_rate_required"] = "อัตราภาษีเริ่มต้นต้องกรอก"; $lang["config_default_tax_name_required"] = "The default tax name is a required field"; $lang["config_dot"] = "dot"; -$lang["config_email"] = "Email"; +$lang["config_email"] = "อีเมล์"; $lang["config_email_configuration"] = "Email Configuration"; $lang["config_email_protocol"] = "Protocol"; $lang["config_email_mailpath"] = "Path to Sendmail"; @@ -153,18 +153,18 @@ $lang["config_receipt_show_taxes"] = "Show Taxes"; $lang["config_receipt_show_total_discount"] = "Show Total Discount"; $lang["config_receipt_show_description"] = "Show Description"; $lang["config_receipt_show_serialnumber"] = "Show Serial Number"; -$lang["config_receipt_template"] = "Receipt Template"; -$lang["config_receipt_default"] = "Default"; -$lang["config_receipt_short"] = "Short"; -$lang["config_receiving_calculate_average_price"] = "Calc avg. Price (Receiving)"; +$lang["config_receipt_template"] = "ต้นแบบใบเสร็จ"; +$lang["config_receipt_default"] = "ค่าปริยาย"; +$lang["config_receipt_short"] = "สั้น"; +$lang["config_receiving_calculate_average_price"] = "คำนวณราคาเฉลี่ย (รับมา)"; $lang["config_recv_invoice_format"] = "ใบส่งของ"; $lang["config_return_policy_required"] = "ต้องกรอกเงื่อนไขการคืนสินค้า"; -$lang["config_right"] = "Right"; +$lang["config_right"] = "ถูกต้อง"; $lang["config_sales_invoice_format"] = "รหัสใบเสร็จ"; $lang["config_saved_successfully"] = "บันทึกข้อมูลร้านค้าเรียบร้อยแล้ว"; $lang["config_saved_unsuccessfully"] = "บันทึกข้อมูลร้านค้าไม่สำเร็จ"; -$lang["config_statistics"] = "Send statistics"; -$lang["config_statistics_tooltip"] = "Send statistics for development and feature improvement purposes"; +$lang["config_statistics"] = "ส่งยอดสถิติ"; +$lang["config_statistics_tooltip"] = "ส่งข้อมูลสถิติเพื่อการพัฒนาและปรับปรุงความสามารถ"; $lang["config_stock_location"] = "สถานที่เก็บ"; $lang["config_stock_location_duplicate"] = ""; $lang["config_stock_location_invalid_chars"] = "ชื่อสต๊อกสถานที่เก็บสินค้าต้องไม่มีอักษรพิเศษ '_'"; diff --git a/application/language/th/receivings_lang.php b/application/language/th/receivings_lang.php index ea22e733d..5b156866a 100644 --- a/application/language/th/receivings_lang.php +++ b/application/language/th/receivings_lang.php @@ -38,14 +38,14 @@ $lang["receivings_return"] = "คืนสินค้า"; $lang["receivings_select_supplier"] = "เลือกผู้ผลิต (ทางเลือก)"; $lang["receivings_start_typing_supplier_name"] = "เริ่มต้นพิมพ์ชื่อผู้ผลิต..."; $lang["receivings_stock_destination"] = "Stock destination"; -$lang["receivings_stock_locaiton"] = "Stock location"; +$lang["receivings_stock_locaiton"] = "สถานที่เก็บสินค้า"; $lang["receivings_stock_source"] = "Stock source"; $lang["receivings_successfully_deleted"] = "You have successfully deleted"; $lang["receivings_successfully_updated"] = "Receiving successfully updated"; $lang["receivings_supplier"] = "ผู้ผลิต"; -$lang["receivings_supplier_email"] = "Email"; -$lang["receivings_supplier_address"] = "Address"; -$lang["receivings_supplier_location"] = "Location"; +$lang["receivings_supplier_email"] = "อีเมล์"; +$lang["receivings_supplier_address"] = "ที่อยู่"; +$lang["receivings_supplier_location"] = "ที่อยู่"; $lang["receivings_total"] = "รวม"; $lang["receivings_unable_to_add_item"] = "ไม่สามารถเพิ่มสินค้าได้"; $lang["receivings_unsuccessfully_updated"] = "Receiving unsuccessfully updated"; diff --git a/application/language/th/sales_lang.php b/application/language/th/sales_lang.php index 808a66e53..12392f59a 100644 --- a/application/language/th/sales_lang.php +++ b/application/language/th/sales_lang.php @@ -7,12 +7,12 @@ $lang["sales_cancel_sale"] = "ยกเลิกการขาย"; $lang["sales_cash"] = "เงินสด"; $lang["sales_change_due"] = "เงินทอน"; $lang["sales_check"] = "เช็ค"; -$lang["sales_check_balance"] = "Check remainder"; +$lang["sales_check_balance"] = "เช็คยอดคงเหลือ"; $lang["sales_comment"] = "หมายเหตุ"; $lang["sales_comments"] = "หมายเหตุ"; $lang["sales_complete_sale"] = "จบการขาย"; $lang["sales_confirm_cancel_sale"] = "แน่ใจหรือไม่ที่จะล้างรายการขาย? สินค้าทุกอย่างจะถูกลบจากบอร์ด."; -$lang["sales_confirm_delete"] = "Are you sure you want to delete the selected sales?"; +$lang["sales_confirm_delete"] = "โปรดยืนยันการลบรายการขายที่เลือกไว้"; $lang["sales_credit"] = "เครดิตการ์ด"; $lang["sales_customer"] = "ลูกค้า"; $lang["sales_customer_email"] = "Customer Email"; diff --git a/application/libraries/Barcode_lib.php b/application/libraries/Barcode_lib.php index d41b5012a..14cec5473 100644 --- a/application/libraries/Barcode_lib.php +++ b/application/libraries/Barcode_lib.php @@ -24,28 +24,28 @@ class Barcode_lib public function get_barcode_config() { - $data['company'] = $this->CI->Appconfig->get('company'); - $data['barcode_content'] = $this->CI->Appconfig->get('barcode_content'); - $data['barcode_type'] = $this->CI->Appconfig->get('barcode_type'); - $data['barcode_font'] = $this->CI->Appconfig->get('barcode_font'); - $data['barcode_font_size'] = $this->CI->Appconfig->get('barcode_font_size'); - $data['barcode_height'] = $this->CI->Appconfig->get('barcode_height'); - $data['barcode_width'] = $this->CI->Appconfig->get('barcode_width'); - $data['barcode_quality'] = $this->CI->Appconfig->get('barcode_quality'); - $data['barcode_first_row'] = $this->CI->Appconfig->get('barcode_first_row'); - $data['barcode_second_row'] = $this->CI->Appconfig->get('barcode_second_row'); - $data['barcode_third_row'] = $this->CI->Appconfig->get('barcode_third_row'); - $data['barcode_num_in_row'] = $this->CI->Appconfig->get('barcode_num_in_row'); - $data['barcode_page_width'] = $this->CI->Appconfig->get('barcode_page_width'); - $data['barcode_page_cellspacing'] = $this->CI->Appconfig->get('barcode_page_cellspacing'); - $data['barcode_generate_if_empty'] = $this->CI->Appconfig->get('barcode_generate_if_empty'); + $data['company'] = $this->CI->config->item('company'); + $data['barcode_content'] = $this->CI->config->item('barcode_content'); + $data['barcode_type'] = $this->CI->config->item('barcode_type'); + $data['barcode_font'] = $this->CI->config->item('barcode_font'); + $data['barcode_font_size'] = $this->CI->config->item('barcode_font_size'); + $data['barcode_height'] = $this->CI->config->item('barcode_height'); + $data['barcode_width'] = $this->CI->config->item('barcode_width'); + $data['barcode_quality'] = $this->CI->config->item('barcode_quality'); + $data['barcode_first_row'] = $this->CI->config->item('barcode_first_row'); + $data['barcode_second_row'] = $this->CI->config->item('barcode_second_row'); + $data['barcode_third_row'] = $this->CI->config->item('barcode_third_row'); + $data['barcode_num_in_row'] = $this->CI->config->item('barcode_num_in_row'); + $data['barcode_page_width'] = $this->CI->config->item('barcode_page_width'); + $data['barcode_page_cellspacing'] = $this->CI->config->item('barcode_page_cellspacing'); + $data['barcode_generate_if_empty'] = $this->CI->config->item('barcode_generate_if_empty'); return $data; } public function validate_barcode($barcode) { - $barcode_type = $this->CI->Appconfig->get('barcode_type'); + $barcode_type = $this->CI->config->item('barcode_type'); $barcode_instance = $this->get_barcode_instance($barcode_type); return $barcode_instance->validate($barcode); } diff --git a/application/libraries/Email_lib.php b/application/libraries/Email_lib.php index cb155f0ca..2a8050391 100644 --- a/application/libraries/Email_lib.php +++ b/application/libraries/Email_lib.php @@ -18,7 +18,7 @@ class Email_lib 'mailpath' => $this->CI->config->item('mailpath'), 'smtp_host' => $this->CI->config->item('smtp_host'), 'smtp_user' => $this->CI->config->item('smtp_user'), - 'smtp_pass' => $this->CI->config->item('smtp_pass'), + 'smtp_pass' => $this->CI->encryption->decrypt($this->CI->config->item('smtp_pass')), 'smtp_port' => $this->CI->config->item('smtp_port'), 'smtp_timeout' => $this->CI->config->item('smtp_timeout'), 'smtp_crypto' => $this->CI->config->item('smtp_crypto') 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..8d212ba5d 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -214,6 +214,26 @@ class Sale_lib { $this->CI->session->unset_userdata('sales_customer'); } + + public function get_employee() + { + if(!$this->CI->session->userdata('sales_employee')) + { + $this->set_employee(-1); + } + + return $this->CI->session->userdata('sales_employee'); + } + + public function set_employee($employee_id) + { + $this->CI->session->set_userdata('sales_employee', $employee_id); + } + + public function remove_employee() + { + $this->CI->session->unset_userdata('sales_employee'); + } public function get_mode() { @@ -439,42 +459,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 # @@ -523,6 +507,7 @@ class Sale_lib } $this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id); + $this->set_employee($this->CI->Sale->get_employee($sale_id)->person_id); } public function copy_entire_suspended_sale($sale_id) diff --git a/application/libraries/Sms_lib.php b/application/libraries/Sms_lib.php index 9888e42e6..8f1bbbea0 100644 --- a/application/libraries/Sms_lib.php +++ b/application/libraries/Sms_lib.php @@ -16,7 +16,7 @@ class Sms_lib public function sendSMS($phone, $message) { $username = $this->CI->config->item('msg_uid'); - $password = $this->CI->config->item('msg_pwd'); + $password = $this->CI->encryption->decrypt($this->CI->config->item('msg_pwd')); $originator = $this->CI->config->item('msg_src'); $response = FALSE; @@ -27,7 +27,7 @@ class Sms_lib //echo $username . ' ' . $password . ' ' . $phone . ' ' . $message . ' ' . $originator; } else - { + { $response = TRUE; // make sure passed string is url encoded diff --git a/application/models/Employee.php b/application/models/Employee.php index 9b720cd80..dfeec9ff7 100644 --- a/application/models/Employee.php +++ b/application/models/Employee.php @@ -298,7 +298,6 @@ class Employee extends Person */ public function login($username, $password) { - $query = $this->db->get_where('employees', array('username' => $username, 'deleted' => 0), 1); if($query->num_rows() == 1) @@ -332,6 +331,7 @@ class Employee extends Person public function logout() { $this->session->sess_destroy(); + redirect('login'); } diff --git a/application/models/Item.php b/application/models/Item.php index 1cfc03a79..eb13a5842 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -514,7 +514,7 @@ class Item extends CI_Model * $old_price (optional) : the current-cost-price * * used in receiving-process to update cost-price if changed - * caution: must be used there before item_quantities gets updated, otherwise average price is wrong! + * caution: must be used before item_quantities gets updated, otherwise the average price is wrong! * */ public function change_cost_price($item_id, $items_received, $new_price, $old_price = null) @@ -539,32 +539,5 @@ class Item extends CI_Model return $this->save($data, $item_id); } - - //We create a temp table that allows us to do easy report queries - public function create_temp_table() - { - $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('items_temp') . - ' (INDEX(quantity), INDEX(location_id)) - ( - SELECT - items.name, - items.item_number, - items.description, - items.reorder_level, - item_quantities.quantity, - stock_locations.location_name, - stock_locations.location_id, - items.cost_price, - items.unit_price, - (items.cost_price * item_quantities.quantity) AS sub_total_value - FROM ' . $this->db->dbprefix('items') . ' AS items - INNER JOIN ' . $this->db->dbprefix('item_quantities') . ' AS item_quantities - ON items.item_id = item_quantities.item_id - INNER JOIN ' . $this->db->dbprefix('stock_locations') . ' AS stock_locations - ON item_quantities.location_id = stock_locations.location_id - WHERE items.deleted = 0 AND stock_locations.deleted = 0 - )' - ); - } } ?> \ No newline at end of file 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..01365ea95 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'); @@ -203,8 +223,17 @@ class Receiving extends CI_Model /* We create a temp table that allows us to do easy report/receiving queries */ - public function create_temp_table() + public function create_temp_table(array $inputs) { + if(empty($input['receiving_id'])) + { + $where = 'WHERE DATE(receiving_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']); + } + else + { + $where = 'WHERE receivings_items.receiving_id = ' . $this->db->escape($inputs['receiving_id']); + } + $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('receivings_items_temp') . ' (INDEX(receiving_date), INDEX(receiving_id)) ( @@ -236,6 +265,9 @@ class Receiving extends CI_Model ON receivings_items.receiving_id = receivings.receiving_id INNER JOIN ' . $this->db->dbprefix('items') . ' AS items ON receivings_items.item_id = items.item_id + ' . " + $where + " . ' GROUP BY receivings_items.receiving_id, items.item_id, receivings_items.line )' ); diff --git a/application/models/Sale.php b/application/models/Sale.php index 465440746..deaffc089 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -3,14 +3,69 @@ class Sale extends CI_Model { public function get_info($sale_id) { - $this->db->select('customer_id, customer_name, customer_first_name AS first_name, customer_last_name AS last_name, customer_email AS email, customer_comments AS comments, - sale_payment_amount AS amount_tendered, SUM(total) AS amount_due, (sale_payment_amount - SUM(total)) AS change_due, payment_type, - sale_id, sale_date, sale_time, comment, invoice_number, employee_id'); - $this->db->from('sales_items_temp'); + if($this->config->item('tax_included')) + { + $total = '1'; + $subtotal = '(1 - (SUM(1 - 100 / (100 + sales_items_taxes.percent))))'; + $tax = '(SUM(1 - 100 / (100 + sales_items_taxes.percent)))'; + } + else + { + $tax = '(SUM(sales_items_taxes.percent) / 100)'; + $total = '(1 + (SUM(sales_items_taxes.percent / 100)))'; + $subtotal = '1'; + } - $this->db->where('sale_id', $sale_id); - $this->db->group_by('sale_id'); - $this->db->order_by('sale_time', 'asc'); + $sale_total = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.item_unit_price * sales_items.quantity_purchased * sales_items.discount_percent / 100)'; + $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; + + $decimals = totals_decimals(); + + // create payments temp table to speed up the sales listing + $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') . + ' (PRIMARY KEY(sale_id), INDEX(sale_id)) + ( + SELECT payments.sale_id AS sale_id, + IFNULL(SUM(payments.payment_amount), 0) AS sale_payment_amount, + GROUP_CONCAT(CONCAT(payments.payment_type, " ", payments.payment_amount) SEPARATOR ", ") AS payment_type + FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments + INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales + ON sales.sale_id = payments.sale_id + WHERE sales.sale_id = ' . $this->db->escape($sale_id) . ' + GROUP BY sale_id + )' + ); + + $this->db->select(' + sales.sale_id AS sale_id, + DATE(sales.sale_time) AS sale_date, + sales.sale_time AS sale_time, + sales.comment AS comment, + sales.invoice_number AS invoice_number, + sales.employee_id AS employee_id, + sales.customer_id AS customer_id, + CONCAT(customer_p.first_name, " ", customer_p.last_name) AS customer_name, + customer_p.first_name AS first_name, + customer_p.last_name AS last_name, + customer_p.email AS email, + customer_p.comments AS comments, + payments.sale_payment_amount AS amount_tendered, + ' . " + IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals)) AS amount_due, + " . ' + payments.payment_type AS payment_type + '); + + $this->db->from('sales_items AS sales_items'); + $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); + $this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id', 'left'); + $this->db->join('customers AS customer', 'sales.customer_id = customer.person_id', 'left'); + $this->db->join('sales_payments_temp AS payments', 'sales.sale_id = payments.sale_id', 'left outer'); + $this->db->join('sales_items_taxes AS sales_items_taxes', 'sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id AND sales_items.line = sales_items_taxes.line', 'left outer'); + + $this->db->where('sales.sale_id', $sale_id); + $this->db->group_by('sales.sale_id'); + $this->db->order_by('sales.sale_time', 'asc'); return $this->db->get(); } @@ -28,61 +83,119 @@ class Sale extends CI_Model */ public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'sale_date', $order = 'desc') { - $this->db->select('sale_id, sale_date, sale_time, SUM(quantity_purchased) AS items_purchased, - customer_name, customer_company_name AS company_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'); - $this->db->from('sales_items_temp'); + if($this->config->item('tax_included')) + { + $total = '1'; + $subtotal = '(1 - (SUM(1 - 100 / (100 + sales_items_taxes.percent))))'; + $tax = '(SUM(1 - 100 / (100 + sales_items_taxes.percent)))'; + } + else + { + $tax = '(SUM(sales_items_taxes.percent) / 100)'; + $total = '(1 + (SUM(sales_items_taxes.percent / 100)))'; + $subtotal = '1'; + } - $this->db->where('sale_date BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); + $sale_total = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.item_unit_price * sales_items.quantity_purchased * sales_items.discount_percent / 100)'; + $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; + + $decimals = totals_decimals(); + + // create payments temp table to speed up the sales listing + $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') . + ' (PRIMARY KEY(sale_id), INDEX(sale_id)) + ( + SELECT payments.sale_id AS sale_id, + IFNULL(SUM(payments.payment_amount), 0) AS sale_payment_amount, + GROUP_CONCAT(CONCAT(payments.payment_type, " ", payments.payment_amount) SEPARATOR ", ") AS payment_type + FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments + INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales + ON sales.sale_id = payments.sale_id + WHERE DATE(sales.sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']) . ' + GROUP BY sale_id + )' + ); + + $this->db->select(' + sales.sale_id AS sale_id, + DATE(sales.sale_time) AS sale_date, + sales.sale_time AS sale_time, + SUM(sales_items.quantity_purchased) AS items_purchased, + CONCAT(customer_p.first_name, " ", customer_p.last_name) AS customer_name, + customer.company_name AS company_name, + ' . " + ROUND($sale_total * $subtotal, $decimals) AS subtotal, + IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals)) AS total, + IFNULL(ROUND($sale_total * $tax, $decimals), 0) AS tax, + ROUND($sale_total - $sale_cost, $decimals) AS profit, + ROUND($sale_cost, $decimals) AS cost, + IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals)) AS amount_due, + (payments.sale_payment_amount - IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals))) AS change_due, + " . ' + payments.sale_payment_amount AS amount_tendered, + payments.payment_type AS payment_type, + sales.invoice_number AS invoice_number + '); + + $this->db->from('sales_items AS sales_items'); + $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); + $this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id', 'left'); + $this->db->join('customers AS customer', 'sales.customer_id = customer.person_id', 'left'); + $this->db->join('sales_payments_temp AS payments', 'sales.sale_id = payments.sale_id', 'left outer'); + $this->db->join('sales_items_taxes AS sales_items_taxes', 'sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id AND sales_items.line = sales_items_taxes.line', 'left outer'); + + $this->db->where('DATE(sales.sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); if(!empty($search)) { if($filters['is_valid_receipt'] != FALSE) { $pieces = explode(' ', $search); - $this->db->where('sale_id', $pieces[1]); + $this->db->where('sales.sale_id', $pieces[1]); } else - { + { $this->db->group_start(); - $this->db->like('customer_last_name', $search); - $this->db->or_like('customer_first_name', $search); - $this->db->or_like('customer_name', $search); - $this->db->or_like('customer_company_name', $search); + // customer last name + $this->db->like('customer_p.last_name', $search); + // customer first name + $this->db->or_like('customer_p.first_name', $search); + // customer first and last name + $this->db->or_like('CONCAT(customer_p.first_name, " ", customer_p.last_name)', $search); + // customer company name + $this->db->or_like('customer.company_name', $search); $this->db->group_end(); } } if($filters['location_id'] != 'all') { - $this->db->where('item_location', $filters['location_id']); + $this->db->where('sales_items.item_location', $filters['location_id']); } if($filters['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sales_items.quantity_purchased > 0'); } elseif($filters['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sales_items.quantity_purchased < 0'); } if($filters['only_invoices'] != FALSE) { - $this->db->where('invoice_number IS NOT NULL'); + $this->db->where('sales.invoice_number IS NOT NULL'); } if($filters['only_cash'] != FALSE) { $this->db->group_start(); - $this->db->like('payment_type', $this->lang->line('sales_cash'), 'after'); - $this->db->or_where('payment_type IS NULL'); + $this->db->like('payments.payment_type', $this->lang->line('sales_cash'), 'after'); + $this->db->or_where('payments.payment_type IS NULL'); $this->db->group_end(); } - $this->db->group_by('sale_id'); + $this->db->group_by('sales.sale_id'); $this->db->order_by($sort, $order); if($rows > 0) @@ -102,7 +215,8 @@ class Sale extends CI_Model $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('people', 'people.person_id = sales.customer_id', 'left'); + $this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id', 'left'); + $this->db->join('customers AS customer', 'sales.customer_id = customer.person_id', 'left'); $this->db->where('DATE(sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date'])); @@ -116,9 +230,14 @@ class Sale extends CI_Model else { $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); + // customer last name + $this->db->like('customer_p.last_name', $search); + // customer first name + $this->db->or_like('customer_p.first_name', $search); + // customer first and last name + $this->db->or_like('CONCAT(customer_p.first_name, " ", customer_p.last_name)', $search); + // customer company name + $this->db->or_like('customer.company_name', $search); $this->db->group_end(); } } @@ -161,7 +280,7 @@ class Sale extends CI_Model } } - if( $gift_card_count > 0 ) + if($gift_card_count > 0) { $payments[] = array('payment_type' => $this->lang->line('sales_giftcard'), 'count' => $gift_card_count, 'payment_amount' => $gift_card_amount); } @@ -183,7 +302,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 +357,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 +648,15 @@ 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 get_employee($sale_id) + { + $this->db->from('sales'); + $this->db->where('sale_id', $sale_id); + + return $this->Employee->get_info($this->db->get()->row()->employee_id); + } + + public function check_invoice_number_exists($invoice_number, $sale_id = '') { $this->db->from('sales'); $this->db->where('invoice_number', $invoice_number); @@ -529,34 +682,49 @@ class Sale extends CI_Model } //We create a temp table that allows us to do easy report/sales queries - public function create_temp_table() + public function create_temp_table(array $inputs) { if($this->config->item('tax_included')) { - $total = '1'; - $subtotal = '(1 - (SUM(1 - 100 / (100 + sales_items_taxes.percent))))'; - $tax = '(SUM(1 - 100 / (100 + sales_items_taxes.percent)))'; + $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (100 / (100 + SUM(sales_items_taxes.percent))))'; + $sale_tax = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 - 100 / (100 + SUM(sales_items_taxes.percent))))'; } else { - $tax = '(SUM(sales_items_taxes.percent) / 100)'; - $total = '(1 + (SUM(sales_items_taxes.percent / 100)))'; - $subtotal = '1'; + $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 + (SUM(sales_items_taxes.percent) / 100)))'; + $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_tax = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (SUM(sales_items_taxes.percent) / 100))'; } - $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.item_unit_price * sales_items.quantity_purchased * sales_items.discount_percent / 100)'; $sale_cost = '(sales_items.item_cost_price * sales_items.quantity_purchased)'; - $decimals = totals_decimals(); + // increase the rounding of two decimals on top of the selected ones to avoid accumulative rounding errors in the totals + $decimals = totals_decimals() + 2; + if(empty($input['sale_id'])) + { + $where = 'WHERE DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']); + } + else + { + $where = 'WHERE sales.sale_id = ' . $this->db->escape($inputs['sale_id']); + } + + // create a temporary table to contain all the payment types and amount $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') . ' (PRIMARY KEY(sale_id), INDEX(sale_id)) ( - SELECT sale_id, - IFNULL(SUM(payment_amount), 0) 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 + SELECT payments.sale_id AS sale_id, + IFNULL(SUM(payments.payment_amount), 0) AS sale_payment_amount, + GROUP_CONCAT(CONCAT(payments.payment_type, " ", payments.payment_amount) SEPARATOR ", ") AS payment_type + FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments + INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales + ON sales.sale_id = payments.sale_id + ' . " + $where + " . ' + GROUP BY payments.sale_id )' ); @@ -594,11 +762,11 @@ class Sale extends CI_Model payments.sale_payment_amount, IFNULL(SUM(sales_items_taxes.percent), 0) AS item_tax_percent, ' . " - IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals)) AS total, - IFNULL(ROUND($sale_total * $tax, $decimals), 0) AS tax, - ROUND($sale_total * $subtotal, $decimals) AS subtotal, - ROUND($sale_total - $sale_cost, $decimals) AS profit, - ROUND($sale_cost, $decimals) AS cost + ROUND($sale_subtotal, $decimals) AS subtotal, + IFNULL(ROUND($sale_total, $decimals), ROUND($sale_subtotal, $decimals)) AS total, + IFNULL(ROUND($sale_tax, $decimals), 0) AS tax, + ROUND($sale_cost, $decimals) AS cost, + ROUND($sale_total - IFNULL($sale_tax, 0) - $sale_cost, $decimals) AS profit " . ' FROM ' . $this->db->dbprefix('sales_items') . ' AS sales_items INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales @@ -617,9 +785,15 @@ class Sale extends CI_Model ON sales.employee_id = employee.person_id LEFT OUTER JOIN ' . $this->db->dbprefix('sales_items_taxes') . ' AS sales_items_taxes ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id AND sales_items.line = sales_items_taxes.line + ' . " + $where + " . ' GROUP BY sales.sale_id, items.item_id, sales_items.line )' ); + + // drop the temporary table to contain memory consumption as it's no longer required + $this->db->query('DROP TEMPORARY TABLE IF EXISTS ' . $this->db->dbprefix('sales_payments_temp')); } } ?> 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) { diff --git a/application/models/reports/Detailed_receivings.php b/application/models/reports/Detailed_receivings.php index 7a970dc88..c02e85163 100644 --- a/application/models/reports/Detailed_receivings.php +++ b/application/models/reports/Detailed_receivings.php @@ -5,9 +5,12 @@ class Detailed_receivings extends Report function __construct() { parent::__construct(); + } + public function create(array $inputs) + { //Create our temp tables to work with the data in our report - $this->Receiving->create_temp_table(); + $this->Receiving->create_temp_table($inputs); } public function getDataColumns() @@ -53,21 +56,20 @@ class Detailed_receivings extends Report $this->db->from('receivings_items_temp'); $this->db->join('people AS employee', 'receivings_items_temp.employee_id = employee.person_id'); $this->db->join('suppliers AS supplier', 'receivings_items_temp.supplier_id = supplier.person_id', 'left'); - $this->db->where('receiving_date BETWEEN '. $this->db->escape($inputs['start_date']). ' AND '. $this->db->escape($inputs['end_date'])); - if ($inputs['location_id'] != 'all') + if($inputs['location_id'] != 'all') { $this->db->where('item_location', $inputs['location_id']); } - if ($inputs['receiving_type'] == 'receiving') + if($inputs['receiving_type'] == 'receiving') { $this->db->where('quantity_purchased > 0'); } - elseif ($inputs['receiving_type'] == 'returns') + elseif($inputs['receiving_type'] == 'returns') { $this->db->where('quantity_purchased < 0'); } - elseif ($inputs['receiving_type'] == 'requisitions') + elseif($inputs['receiving_type'] == 'requisitions') { $this->db->having('items_purchased = 0'); } @@ -80,8 +82,7 @@ class Detailed_receivings extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, item_number, category, quantity_purchased, serialnumber,total, discount_percent, item_location'); - $this->db->select($this->db->dbprefix('receivings_items_temp').".receiving_quantity"); + $this->db->select('name, item_number, category, quantity_purchased, serialnumber,total, discount_percent, item_location, receivings_items_temp.receiving_quantity'); $this->db->from('receivings_items_temp'); $this->db->join('items', 'receivings_items_temp.item_id = items.item_id'); $this->db->where('receiving_id = '.$value['receiving_id']); @@ -95,21 +96,20 @@ class Detailed_receivings extends Report { $this->db->select('SUM(total) AS total'); $this->db->from('receivings_items_temp'); - $this->db->where('receiving_date BETWEEN '. $this->db->escape($inputs['start_date']). ' AND '. $this->db->escape($inputs['end_date'])); - if ($inputs['location_id'] != 'all') + if($inputs['location_id'] != 'all') { $this->db->where('item_location', $inputs['location_id']); } - if ($inputs['receiving_type'] == 'receiving') + if($inputs['receiving_type'] == 'receiving') { $this->db->where('quantity_purchased > 0'); } - elseif ($inputs['receiving_type'] == 'returns') + elseif($inputs['receiving_type'] == 'returns') { $this->db->where('quantity_purchased < 0'); } - elseif ($inputs['receiving_type'] == 'requisitions') + elseif($inputs['receiving_type'] == 'requisitions') { $this->db->where('quantity_purchased = 0'); } diff --git a/application/models/reports/Detailed_sales.php b/application/models/reports/Detailed_sales.php index 6059412e4..ac2ac4fe3 100644 --- a/application/models/reports/Detailed_sales.php +++ b/application/models/reports/Detailed_sales.php @@ -5,11 +5,14 @@ class Detailed_sales extends Report function __construct() { parent::__construct(); - - //Create our temp tables to work with the data in our report - $this->Sale->create_temp_table(); } - + + public function create(array $inputs) + { + //Create our temp tables to work with the data in our report + $this->Sale->create_temp_table($inputs); + } + public function getDataColumns() { return array( @@ -27,21 +30,21 @@ class Detailed_sales extends Report 'payment_type' => $this->lang->line('sales_amount_tendered'), 'comment' => $this->lang->line('reports_comments'), 'edit' => ''), - 'details' => array( - $this->lang->line('reports_name'), - $this->lang->line('reports_category'), - $this->lang->line('reports_serial_number'), - $this->lang->line('reports_description'), - $this->lang->line('reports_quantity'), - $this->lang->line('reports_subtotal'), - $this->lang->line('reports_total'), - $this->lang->line('reports_tax'), - $this->lang->line('reports_cost'), - $this->lang->line('reports_profit'), - $this->lang->line('reports_discount')) + 'details' => array( + $this->lang->line('reports_name'), + $this->lang->line('reports_category'), + $this->lang->line('reports_serial_number'), + $this->lang->line('reports_description'), + $this->lang->line('reports_quantity'), + $this->lang->line('reports_subtotal'), + $this->lang->line('reports_total'), + $this->lang->line('reports_tax'), + $this->lang->line('reports_cost'), + $this->lang->line('reports_profit'), + $this->lang->line('reports_discount')) ); } - + public function getDataBySaleId($sale_id) { $this->db->select('sale_id, sale_date, SUM(quantity_purchased) AS items_purchased, employee_name, customer_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, payment_type, comment'); @@ -50,23 +53,22 @@ class Detailed_sales extends Report return $this->db->get()->row_array(); } - + public function getData(array $inputs) { $this->db->select('sale_id, sale_date, SUM(quantity_purchased) AS items_purchased, employee_name, customer_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, payment_type, comment'); $this->db->from('sales_items_temp'); - $this->db->where('sale_date BETWEEN '. $this->db->escape($inputs['start_date']). ' AND '. $this->db->escape($inputs['end_date'])); - if ($inputs['location_id'] != 'all') + if($inputs['location_id'] != 'all') { $this->db->where('item_location', $inputs['location_id']); } - if ($inputs['sale_type'] == 'sales') + if($inputs['sale_type'] == 'sales') { $this->db->where('quantity_purchased > 0'); } - elseif ($inputs['sale_type'] == 'returns') + elseif($inputs['sale_type'] == 'returns') { $this->db->where('quantity_purchased < 0'); } @@ -77,7 +79,7 @@ class Detailed_sales extends Report $data = array(); $data['summary'] = $this->db->get()->result_array(); $data['details'] = array(); - + foreach($data['summary'] as $key=>$value) { $this->db->select('name, category, quantity_purchased, item_location, serialnumber, description, subtotal, total, tax, cost, profit, discount_percent'); @@ -85,26 +87,25 @@ class Detailed_sales extends Report $this->db->where('sale_id', $value['sale_id']); $data['details'][$key] = $this->db->get()->result_array(); } - + return $data; } - + public function getSummaryData(array $inputs) { $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); $this->db->from('sales_items_temp'); - $this->db->where('sale_date BETWEEN '. $this->db->escape($inputs['start_date']). ' AND '. $this->db->escape($inputs['end_date'])); - if ($inputs['location_id'] != 'all') + if($inputs['location_id'] != 'all') { $this->db->where('item_location', $inputs['location_id']); } - if ($inputs['sale_type'] == 'sales') + if($inputs['sale_type'] == 'sales') { $this->db->where('quantity_purchased > 0'); } - elseif ($inputs['sale_type'] == 'returns') + elseif($inputs['sale_type'] == 'returns') { $this->db->where('quantity_purchased < 0'); } diff --git a/application/models/reports/Inventory_low.php b/application/models/reports/Inventory_low.php index 69e08d20f..3e5123fa0 100644 --- a/application/models/reports/Inventory_low.php +++ b/application/models/reports/Inventory_low.php @@ -5,11 +5,8 @@ class Inventory_low extends Report function __construct() { parent::__construct(); - - //Create our temp tables to work with the data in our report - $this->Item->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_item_name'), @@ -21,10 +18,14 @@ class Inventory_low extends Report public function getData(array $inputs) { - $this->db->select('name, item_number, quantity, reorder_level, location_name'); - $this->db->from('items_temp'); - $this->db->where('quantity <= reorder_level'); - $this->db->order_by('name'); + $this->db->select('items.name, items.item_number, item_quantities.quantity, items.reorder_level, stock_locations.location_name'); + $this->db->from('items'); + $this->db->join('item_quantities', 'items.item_id = item_quantities.item_id'); + $this->db->join('stock_locations', 'item_quantities.location_id = stock_locations.location_id'); + $this->db->where('items.deleted', 0); + $this->db->where('stock_locations.deleted', 0); + $this->db->where('item_quantities.quantity <= items.reorder_level'); + $this->db->order_by('items.name'); return $this->db->get()->result_array(); } diff --git a/application/models/reports/Inventory_summary.php b/application/models/reports/Inventory_summary.php index db5b092b4..27af10bcc 100644 --- a/application/models/reports/Inventory_summary.php +++ b/application/models/reports/Inventory_summary.php @@ -5,9 +5,6 @@ class Inventory_summary extends Report function __construct() { parent::__construct(); - - //Create our temp tables to work with the data in our report - $this->Item->create_temp_table(); } public function getDataColumns() @@ -23,26 +20,30 @@ class Inventory_summary extends Report } public function getData(array $inputs) - { - $this->db->select('name, item_number, quantity, reorder_level, location_name, cost_price, unit_price, sub_total_value'); - $this->db->from('items_temp'); + { + $this->db->select('items.name, items.item_number, item_quantities.quantity, items.reorder_level, stock_locations.location_name, items.cost_price, items.unit_price, (items.cost_price * item_quantities.quantity) AS sub_total_value'); + $this->db->from('items AS items'); + $this->db->join('item_quantities AS item_quantities', 'items.item_id = item_quantities.item_id'); + $this->db->join('stock_locations AS stock_locations', 'item_quantities.location_id = stock_locations.location_id'); + $this->db->where('items.deleted', 0); + $this->db->where('stock_locations.deleted', 0); // should be corresponding to values Inventory_summary::getItemCountDropdownArray() returns... if($inputs['item_count'] == 'zero_and_less') { - $this->db->where('quantity <= 0'); + $this->db->where('item_quantities.quantity <= 0'); } elseif($inputs['item_count'] == 'more_than_zero') { - $this->db->where('quantity > 0'); + $this->db->where('item_quantities.quantity > 0'); } if($inputs['location_id'] != 'all') { - $this->db->where('location_id', $inputs['location_id']); + $this->db->where('stock_locations.location_id', $inputs['location_id']); } - $this->db->order_by('name'); + $this->db->order_by('items.name'); return $this->db->get()->result_array(); } diff --git a/application/models/reports/Report.php b/application/models/reports/Report.php index 53d29f2e2..a9d872b10 100644 --- a/application/models/reports/Report.php +++ b/application/models/reports/Report.php @@ -11,14 +11,14 @@ abstract class Report extends CI_Model $this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE); $this->output->set_header('Pragma: no-cache'); } - - //Returns the column names used for the report + + // Returns the column names used for the report public abstract function getDataColumns(); - //Returns all the data to be populated into the report + // Returns all the data to be populated into the report public abstract function getData(array $inputs); - //Returns key=>value pairing of summary data for the report + // Returns key=>value pairing of summary data for the report public abstract function getSummaryData(array $inputs); } ?> \ No newline at end of file diff --git a/application/models/reports/Specific_customer.php b/application/models/reports/Specific_customer.php index fdb67677f..49a0c951b 100644 --- a/application/models/reports/Specific_customer.php +++ b/application/models/reports/Specific_customer.php @@ -5,9 +5,12 @@ class Specific_customer extends Report function __construct() { parent::__construct(); - + } + + public function create(array $inputs) + { //Create our temp tables to work with the data in our report - $this->Sale->create_temp_table(); + $this->Sale->create_temp_table($inputs); } public function getDataColumns() @@ -21,7 +24,7 @@ class Specific_customer extends Report { $this->db->select('sale_id, sale_date, SUM(quantity_purchased) AS items_purchased, employee_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, payment_type, comment'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND customer_id=" . $this->db->escape($inputs['customer_id'])); + $this->db->where('customer_id', $inputs['customer_id']); if ($inputs['sale_type'] == 'sales') { @@ -54,7 +57,7 @@ class Specific_customer extends Report { $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND customer_id=" . $this->db->escape($inputs['customer_id'])); + $this->db->where('customer_id', $inputs['customer_id']); if ($inputs['sale_type'] == 'sales') { diff --git a/application/models/reports/Specific_discount.php b/application/models/reports/Specific_discount.php index c7a926633..5a4f12af7 100755 --- a/application/models/reports/Specific_discount.php +++ b/application/models/reports/Specific_discount.php @@ -5,9 +5,12 @@ class Specific_discount extends Report function __construct() { parent::__construct(); - + } + + public function create(array $inputs) + { //Create our temp tables to work with the data in our report - $this->Sale->create_temp_table(); + $this->Sale->create_temp_table($inputs); } public function getDataColumns() @@ -21,8 +24,8 @@ class Specific_discount extends Report { $this->db->select('sale_id, sale_date, SUM(quantity_purchased) AS items_purchased, customer_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, payment_type, comment'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND discount_percent >=" . $this->db->escape($inputs['discount'])); - + $this->db->where('discount_percent >=', $inputs['discount']); + if ($inputs['sale_type'] == 'sales') { $this->db->where('quantity_purchased > 0'); @@ -44,7 +47,6 @@ class Specific_discount extends Report $this->db->select('name, serialnumber, category, description, quantity_purchased, subtotal, total, tax, cost, profit, discount_percent'); $this->db->from('sales_items_temp'); $this->db->where('sale_id', $value['sale_id']); - $this->db->where('discount_percent >= ', $inputs['discount']); $data['details'][$key] = $this->db->get()->result_array(); } @@ -55,8 +57,8 @@ class Specific_discount extends Report { $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND discount_percent >=" . $this->db->escape($inputs['discount'])); - + $this->db->where('discount_percent >=', $inputs['discount']); + if ($inputs['sale_type'] == 'sales') { $this->db->where('quantity_purchased > 0'); diff --git a/application/models/reports/Specific_employee.php b/application/models/reports/Specific_employee.php index 977de2103..369920530 100644 --- a/application/models/reports/Specific_employee.php +++ b/application/models/reports/Specific_employee.php @@ -5,9 +5,12 @@ class Specific_employee extends Report function __construct() { parent::__construct(); - + } + + public function create(array $inputs) + { //Create our temp tables to work with the data in our report - $this->Sale->create_temp_table(); + $this->Sale->create_temp_table($inputs); } public function getDataColumns() @@ -21,7 +24,7 @@ class Specific_employee extends Report { $this->db->select('sale_id, sale_date, SUM(quantity_purchased) AS items_purchased, customer_name, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit, payment_type, comment'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND employee_id=" . $this->db->escape($inputs['employee_id'])); + $this->db->where('employee_id', $inputs['employee_id']); if ($inputs['sale_type'] == 'sales') { @@ -54,7 +57,7 @@ class Specific_employee extends Report { $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " AND employee_id=" . $this->db->escape($inputs['employee_id'])); + $this->db->where('employee_id', $inputs['employee_id']); if ($inputs['sale_type'] == 'sales') { diff --git a/application/models/reports/Summary_categories.php b/application/models/reports/Summary_categories.php index b8fdf22a9..dbd34594f 100644 --- a/application/models/reports/Summary_categories.php +++ b/application/models/reports/Summary_categories.php @@ -1,67 +1,36 @@ Sale->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_category'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - + public function getData(array $inputs) { - $this->db->select('category, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + items.category AS category, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonFrom(); + + $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + + $this->commonWhere($inputs); $this->db->group_by('category'); $this->db->order_by('category'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_customers.php b/application/models/reports/Summary_customers.php index ebf863c71..a479e8f61 100644 --- a/application/models/reports/Summary_customers.php +++ b/application/models/reports/Summary_customers.php @@ -1,67 +1,36 @@ Sale->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_customer'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - + public function getData(array $inputs) { - $this->db->select('customer_name AS customer, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + CONCAT(customer_p.first_name, " ", customer_p.last_name) AS customer, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonFrom(); - $this->db->group_by('customer_id'); - $this->db->order_by('customer_last_name'); + $this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id'); + + $this->commonWhere($inputs); + + $this->db->group_by('sales.customer_id'); + $this->db->order_by('customer_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_discounts.php b/application/models/reports/Summary_discounts.php index 29d34a327..1ac12f688 100644 --- a/application/models/reports/Summary_discounts.php +++ b/application/models/reports/Summary_discounts.php @@ -1,13 +1,10 @@ Sale->create_temp_table(); } public function getDataColumns() @@ -17,52 +14,18 @@ class Summary_discounts extends Report public function getData(array $inputs) { - $this->db->select('CONCAT(discount_percent, "%") AS discount_percent, count(*) AS count'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->db->select('CONCAT(sales_items.discount_percent, "%") AS discount_percent, count(*) AS count'); + $this->db->from('sales_items AS sales_items'); + $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); + $this->db->where('discount_percent > 0'); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonWhere($inputs); - $this->db->group_by('sales_items_temp.discount_percent'); - $this->db->order_by('discount_percent'); + $this->db->group_by('sales_items.discount_percent'); + $this->db->order_by('sales_items.discount_percent'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_employees.php b/application/models/reports/Summary_employees.php index 52c7fc4d1..1cc275670 100644 --- a/application/models/reports/Summary_employees.php +++ b/application/models/reports/Summary_employees.php @@ -1,13 +1,10 @@ Sale->create_temp_table(); } public function getDataColumns() @@ -17,51 +14,23 @@ class Summary_employees extends Report public function getData(array $inputs) { - $this->db->select('employee_name AS employee, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + CONCAT(employee_p.first_name, " ", employee_p.last_name) AS employee, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonFrom(); - $this->db->group_by('employee_id'); - $this->db->order_by('employee_name'); + $this->db->join('people AS employee_p', 'sales.employee_id = employee_p.person_id'); + + $this->commonWhere($inputs); + + $this->db->group_by('sales.employee_id'); + $this->db->order_by('employee_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_items.php b/application/models/reports/Summary_items.php index 033a9b870..9da526a58 100644 --- a/application/models/reports/Summary_items.php +++ b/application/models/reports/Summary_items.php @@ -1,15 +1,12 @@ Sale->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_item'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); @@ -17,51 +14,23 @@ class Summary_items extends Report public function getData(array $inputs) { - $this->db->select('name, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + items.name AS name, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonFrom(); - $this->db->group_by('item_id'); - $this->db->order_by('name'); + $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + + $this->commonWhere($inputs); + + $this->db->group_by('items.item_id'); + $this->db->order_by('items.name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index b1f316bd8..f494f8e18 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -1,13 +1,10 @@ Sale->create_temp_table(); } public function getDataColumns() @@ -19,22 +16,9 @@ class Summary_payments extends Report { $this->db->select('sales_payments.payment_type, count(*) AS count, SUM(payment_amount) AS payment_amount'); $this->db->from('sales_payments'); - $this->db->join('sales', 'sales.sale_id=sales_payments.sale_id'); - $this->db->where("date(sale_time) BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->db->join('sales AS sales', 'sales.sale_id = sales_payments.sale_id'); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('payment_amount > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('payment_amount < 0'); - } + $this->commonWhere($inputs); $this->db->group_by("payment_type"); @@ -62,28 +46,5 @@ class Summary_payments extends Report return $payments; } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php new file mode 100644 index 000000000..2e4b9dd1e --- /dev/null +++ b/application/models/reports/Summary_report.php @@ -0,0 +1,92 @@ +db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . + ' (INDEX(sale_id), INDEX(item_id)) + ( + SELECT sales_items_taxes.sale_id AS sale_id, + sales_items_taxes.item_id AS item_id, + SUM(sales_items_taxes.percent) AS percent + FROM ' . $this->db->dbprefix('sales_items_taxes') . ' AS sales_items_taxes + INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales + ON sales.sale_id = sales_items_taxes.sale_id + INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items + ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.line = sales_items_taxes.line + WHERE DATE(sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']) . ' + GROUP BY sales_items_taxes.sale_id, sales_items_taxes.item_id + )' + ); + + if($this->config->item('tax_included')) + { + $sale_total = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_subtotal = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (100 / (100 + sales_items_taxes.percent)))'; + $sale_tax = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 - 100 / (100 + sales_items_taxes.percent)))'; + } + else + { + $sale_total = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 + (sales_items_taxes.percent / 100)))'; + $sale_subtotal = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_tax = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (sales_items_taxes.percent / 100))'; + } + + $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; + + $decimals = totals_decimals(); + + $this->db->select(" + ROUND($sale_subtotal, $decimals) AS subtotal, + IFNULL(ROUND($sale_total, $decimals), ROUND($sale_subtotal, $decimals)) AS total, + IFNULL(ROUND($sale_tax, $decimals), 0) AS tax, + ROUND($sale_cost, $decimals) AS cost, + ROUND($sale_total - IFNULL($sale_tax, 0) - $sale_cost, $decimals) AS profit + "); + } + + protected function commonFrom() + { + $this->db->from('sales_items AS sales_items'); + $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); + $this->db->join('sales_items_taxes_temp AS sales_items_taxes', 'sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id', 'left outer'); + } + + protected function commonWhere(array $inputs) + { + $this->db->where('DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); + + if($inputs['location_id'] != 'all') + { + $this->db->where('item_location', $inputs['location_id']); + } + + if($inputs['sale_type'] == 'sales') + { + $this->db->where('quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'returns') + { + $this->db->where('quantity_purchased < 0'); + } + } + + public function getSummaryData(array $inputs) + { + $this->commonSelect($inputs); + + $this->commonFrom(); + + $this->commonWhere($inputs); + + return $this->db->get()->row_array(); + } +} +?> \ No newline at end of file diff --git a/application/models/reports/Summary_sales.php b/application/models/reports/Summary_sales.php index 6d8b08dcf..fafb3da51 100644 --- a/application/models/reports/Summary_sales.php +++ b/application/models/reports/Summary_sales.php @@ -1,68 +1,34 @@ Sale->create_temp_table(); } public function getDataColumns() { return array($this->lang->line('reports_date'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - + public function getData(array $inputs) - { - $this->db->select('sale_date, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + { + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + DATE(sales.sale_time) AS sale_date, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); + + $this->commonFrom(); + + $this->commonWhere($inputs); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - $this->db->group_by('sale_date'); $this->db->order_by('sale_date'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } - } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_suppliers.php b/application/models/reports/Summary_suppliers.php index 07644a46f..b073bef4d 100644 --- a/application/models/reports/Summary_suppliers.php +++ b/application/models/reports/Summary_suppliers.php @@ -1,71 +1,38 @@ Sale->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_supplier'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - + public function getData(array $inputs) { - $this->db->select('CONCAT(company_name, " (", first_name, " ", last_name, ")") AS supplier, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->join('suppliers', 'suppliers.person_id = sales_items_temp.supplier_id'); - $this->db->join('people', 'suppliers.person_id = people.person_id'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + CONCAT(supplier_c.company_name, " (", supplier_p.first_name, " ", supplier_p.last_name, ")") AS supplier, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $this->commonFrom(); - $this->db->group_by('supplier_id'); - $this->db->order_by('last_name'); + $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + $this->db->join('suppliers AS supplier_c', 'supplier_c.person_id = items.supplier_id'); + $this->db->join('people AS supplier_p', 'supplier_c.person_id = supplier_p.person_id'); + + $this->commonWhere($inputs); + + $this->db->group_by('items.supplier_id'); + $this->db->order_by('supplier_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->join('suppliers', 'suppliers.person_id = sales_items_temp.supplier_id'); - $this->db->join('people', 'suppliers.person_id = people.person_id'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index c5f0a420f..f57fb696b 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -1,90 +1,67 @@ Sale->create_temp_table(); } - + public function getDataColumns() { return array($this->lang->line('reports_tax_percent'), $this->lang->line('reports_count'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax')); } - + public function getData(array $inputs) { $quantity_cond = ''; if ($inputs['sale_type'] == 'sales') { - $quantity_cond = 'and quantity_purchased > 0'; + $quantity_cond = 'AND quantity_purchased > 0'; } elseif ($inputs['sale_type'] == 'returns') { - $quantity_cond = 'and quantity_purchased < 0'; + $quantity_cond = 'AND quantity_purchased < 0'; } if ($inputs['location_id'] != 'all') { - $quantity_cond .= 'and item_location = '. $this->db->escape($inputs['location_id']); + $quantity_cond .= 'AND item_location = '. $this->db->escape($inputs['location_id']); } - if ($this->config->item('tax_included')) + if($this->config->item('tax_included')) { - $total = "1"; - $subtotal = "(100/(100+percent))"; - $tax = "(1 - (100/(100 +percent)))"; + $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (100 / (100 + sales_items_taxes.percent)))'; + $sale_tax = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 - 100 / (100 + sales_items_taxes.percent)))'; } else { - $tax = "(percent/100)"; - $total = "(1+(percent/100))"; - $subtotal = "1"; + $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 + (sales_items_taxes.percent / 100)))'; + $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_tax = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (sales_items_taxes.percent / 100))'; } - + $decimals = totals_decimals(); - $query = $this->db->query("SELECT percent, count(*) AS count, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax - FROM (SELECT name, CONCAT(ROUND(percent, $decimals), '%') AS percent, - ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $subtotal, $decimals) AS subtotal, - 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 - FROM ".$this->db->dbprefix('sales_items_taxes')." - JOIN ".$this->db->dbprefix('sales_items')." 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' - ." JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items_taxes').".sale_id=".$this->db->dbprefix('sales').".sale_id - WHERE date(sale_time) BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " $quantity_cond) AS temp_taxes - GROUP BY percent"); + $query = $this->db->query("SELECT percent, count(*) AS count, ROUND(SUM(subtotal), $decimals) AS subtotal, ROUND(SUM(total), $decimals) AS total, ROUND(SUM(tax), $decimals) AS tax + FROM ( + SELECT + CONCAT(IFNULL(ROUND(percent, $decimals), 0), '%') AS percent, + $sale_subtotal AS subtotal, + IFNULL($sale_total, $sale_subtotal) AS total, + IFNULL($sale_tax, 0) AS tax + FROM " . $this->db->dbprefix('sales_items') . ' AS sales_items + INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales + ON sales_items.sale_id = sales.sale_id + LEFT OUTER JOIN ' . $this->db->dbprefix('sales_items_taxes') . ' AS sales_items_taxes + ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id AND sales_items.line = sales_items_taxes.line + WHERE DATE(sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']) . " $quantity_cond + ) AS temp_taxes + GROUP BY percent" + ); return $query->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->where("sale_date BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date'])); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/views/configs/info_config.php b/application/views/configs/info_config.php index 663f6af7b..010f14781 100644 --- a/application/views/configs/info_config.php +++ b/application/views/configs/info_config.php @@ -25,7 +25,7 @@