From bab4791b5ead323a2b273635ab37b0f27ae78c44 Mon Sep 17 00:00:00 2001 From: Steve Ireland Date: Sun, 9 Jul 2017 11:30:02 -0400 Subject: [PATCH] Consolidate tax configuration. --- application/controllers/Config.php | 114 ++++++++- application/language/en/config_lang.php | 3 + application/models/Tax.php | 52 ++++ application/views/configs/general_config.php | 95 +------- application/views/configs/manage.php | 116 ++++----- application/views/configs/tax_config.php | 238 +++++++++++++++++++ application/views/partial/tax_categories.php | 39 +++ database/3.0.2_to_3.1.0.sql | 5 + database/database.sql | 2 +- database/tables.sql | 2 +- 10 files changed, 513 insertions(+), 153 deletions(-) create mode 100644 application/views/configs/tax_config.php create mode 100644 application/views/partial/tax_categories.php diff --git a/application/controllers/Config.php b/application/controllers/Config.php index df299b551..5e6bfd126 100644 --- a/application/controllers/Config.php +++ b/application/controllers/Config.php @@ -198,12 +198,14 @@ class Config extends Secure_Controller { $data['stock_locations'] = $this->Stock_location->get_all()->result_array(); $data['dinner_tables'] = $this->Dinner_table->get_all()->result_array(); + $data['tax_categories'] = $this->Tax->get_all_tax_categories()->result_array(); $data['customer_rewards'] = $this->Customer_rewards->get_all()->result_array(); $data['support_barcode'] = $this->barcode_lib->get_list_barcodes(); $data['logo_exists'] = $this->config->item('company_logo') != ''; $data['line_sequence_options'] = $this->sale_lib->get_line_sequence_options(); $data['register_mode_options'] = $this->sale_lib->get_register_mode_options(); $data['rounding_options'] = Rounding_code::get_rounding_options(); + $data['tax_codes'] = $this->get_tax_code_options(); $data = $this->xss_clean($data); @@ -230,6 +232,20 @@ class Config extends Secure_Controller $this->load->view("configs/manage", $data); } + + public function get_tax_code_options() + { + $tax_codes = $this->Tax->get_all_tax_codes()->result_array(); + $tax_code_options = array(); + foreach($tax_codes as $tax_code) + { + $a = $tax_code['tax_code']; + $b = $tax_code['tax_code_name']; + $tax_code_options[$a] = $b; + } + return $tax_code_options; + } + public function save_info() { $upload_success = $this->_handle_logo_upload(); @@ -269,13 +285,6 @@ class Config extends Secure_Controller { $batch_save_data = array( 'theme' => $this->input->post('theme'), - 'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate')), - 'default_tax_1_name' => $this->input->post('default_tax_1_name'), - 'default_tax_2_rate' => parse_decimals($this->input->post('default_tax_2_rate')), - 'default_tax_2_name' => $this->input->post('default_tax_2_name'), - 'tax_included' => $this->input->post('tax_included') != NULL, - 'customer_sales_tax_support' => $this->input->post('customer_sales_tax_support') != NULL, - 'default_origin_tax_code' => $this->input->post('default_origin_tax_code'), 'receiving_calculate_average_price' => $this->input->post('receiving_calculate_average_price') != NULL, 'lines_per_page' => $this->input->post('lines_per_page'), 'default_sales_discount' => $this->input->post('default_sales_discount'), @@ -492,6 +501,15 @@ class Config extends Secure_Controller $this->load->view('partial/dinner_tables', array('dinner_tables' => $dinner_tables)); } + public function tax_categories() + { + $tax_categories = $this->Tax->get_all_tax_categories()->result_array(); + + $tax_categories = $this->xss_clean($tax_categories); + + $this->load->view('partial/tax_categories', array('tax_categories' => $tax_categories)); + } + public function customer_rewards() { $customer_rewards = $this->Customer_rewards->get_all()->result_array(); @@ -597,6 +615,88 @@ class Config extends Secure_Controller )); } + public function save_tax() + { + $this->db->trans_start(); + + $customer_sales_tax_support = $this->input->post('customer_sales_tax_support') != NULL; + + $batch_save_data = array( + 'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate')), + 'default_tax_1_name' => $this->input->post('default_tax_1_name'), + 'default_tax_2_rate' => parse_decimals($this->input->post('default_tax_2_rate')), + 'default_tax_2_name' => $this->input->post('default_tax_2_name'), + 'tax_included' => $this->input->post('tax_included') != NULL, + 'customer_sales_tax_support' => $customer_sales_tax_support, + 'default_origin_tax_code' => $this->input->post('default_origin_tax_code') + ); + + $result = $this->Appconfig->batch_save($batch_save_data); + $success = $result ? TRUE : FALSE; + + if($customer_sales_tax_support) + { + $not_to_delete = array(); + foreach($this->input->post() as $key => $value) + { + if(strstr($key, 'tax_category')) + { + $tax_category_id = preg_replace("/.*?_(\d+)$/", "$1", $key); + $not_to_delete[] = $tax_category_id; + $array_save[$tax_category_id]['tax_category'] = $value; + } + elseif(strstr($key, 'tax_group_sequence')) + { + $tax_category_id = preg_replace("/.*?_(\d+)$/", "$1", $key); + $not_to_delete[] = $tax_category_id; + $array_save[$tax_category_id]['tax_group_sequence'] = $value; + } + } + + if(!empty($array_save)) + { + foreach($array_save as $key => $value) + { + // save or update + $prev_id = $key; + $category_data = array('tax_category' => $value['tax_category'], 'tax_group_sequence' => $value['tax_group_sequence']); + if($this->Tax->save_tax_category($category_data, $key)) + { + if ($prev_id != $category_data['tax_category_id']) + { + unset($not_to_delete[$prev_id]); + $not_to_delete[] = $category_data['tax_category_id']; + } + } + } + } + + // all locations not available in post will be deleted now + $tax_categories = $this->Tax->get_all_tax_categories()->result_array(); + + foreach($tax_categories as $tax_category) + { + if(!empty($tax_category['tax_category_id']) && !in_array($tax_category['tax_category_id'], $not_to_delete)) + { + $this->Tax->delete_tax_category($tax_category['tax_category_id']); + } + } + } + + $this->db->trans_complete(); + $success2 = $this->db->trans_status(); + + $success3 = $success && $success2; + + $this->_clear_session_state(); + + echo json_encode(array( + 'success' => $success3, + 'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully') + )); + + } + public function save_rewards() { $this->db->trans_start(); diff --git a/application/language/en/config_lang.php b/application/language/en/config_lang.php index 8f41040cb..1df762070 100644 --- a/application/language/en/config_lang.php +++ b/application/language/en/config_lang.php @@ -223,6 +223,9 @@ $lang["config_stock_location_required"] = "Stock location is a required field"; $lang["config_table"] = "Table"; $lang["config_table_configuration"] = "Table Configuration"; $lang["config_takings_printer"] = "Takings Printer"; +$lang["config_tax"] = "Tax"; +$lang["config_tax_configuration"] = "Tax Configuration"; +$lang["config_tax_category"] = "Tax Category"; $lang["config_tax_decimals"] = "Tax Decimals"; $lang["config_tax_included"] = "Tax Included"; $lang["config_theme"] = "Theme"; diff --git a/application/models/Tax.php b/application/models/Tax.php index 7f1ec3466..3bc747df5 100644 --- a/application/models/Tax.php +++ b/application/models/Tax.php @@ -26,6 +26,14 @@ class Tax extends CI_Model return ($this->db->get()->num_rows() == 1); } + public function tax_category_exists($tax_category_id) + { + $this->db->from('tax_categories'); + $this->db->where('tax_category_id', $tax_category_id); + + return ($this->db->get()->num_rows() == 1); + } + /* Gets total of rows */ @@ -178,6 +186,33 @@ class Tax extends CI_Model } } + /** + * Inserts or updates a tax_category + */ + public function save_tax_category(&$tax_category_data, $tax_category_id = -1) + { + if(!$this->tax_category_exists($tax_category_id)) + { + $this->db->trans_start(); + + $this->db->insert('tax_categories', $tax_category_data); + $tax_category_id = $this->db->insert_id(); + $tax_category_data['tax_category_id'] = $tax_category_id; + + $this->db->trans_complete(); + + return $this->db->trans_status(); + } + else + { + $this->db->where('tax_category_id', $tax_category_id); + + return $this->db->update('tax_categories', $tax_category_data); + } + + } + + public function save_tax_rates(&$tax_rate_data, $tax_code) { if(!$this->tax_rate_exists($tax_code, $tax_rate_data['rate_tax_category_id'])) @@ -234,6 +269,14 @@ class Tax extends CI_Model return $this->db->delete('tax_codes', array('tax_code' => $tax_code)); } + /* + Deletes one tax_codes entry + */ + public function delete_tax_category($tax_category_id) + { + return $this->db->delete('tax_categories', array('tax_category_id' => $tax_category_id)); + } + /* Deletes a list of tax codes */ @@ -369,6 +412,15 @@ class Tax extends CI_Model return $this->db->get(); } + public function get_all_tax_codes() + { + $this->db->select('tax_code, tax_code_name'); + $this->db->from('tax_codes'); + $this->db->order_by('tax_code_name'); + + return $this->db->get(); + } + public function get_tax_category_id($tax_category) { $this->db->select('tax_category_id'); diff --git a/application/views/configs/general_config.php b/application/views/configs/general_config.php index 3f33e7850..e0ce713bb 100644 --- a/application/views/configs/general_config.php +++ b/application/views/configs/general_config.php @@ -11,77 +11,6 @@ -
- lang->line('config_default_tax_rate_1'), 'default_tax_1_rate', array('class' => 'control-label col-xs-2')); ?> -
- 'default_tax_1_name', - 'id' => 'default_tax_1_name', - 'class' => 'form-control input-sm', - 'value'=>$this->config->item('default_tax_1_name')!==FALSE ? $this->config->item('default_tax_1_name') : $this->lang->line('items_sales_tax_1'))); ?> -
-
- 'default_tax_1_rate', - 'id' => 'default_tax_1_rate', - 'class' => 'form-control input-sm', - 'value'=> to_tax_decimals($this->config->item('default_tax_1_rate')))); ?> - % -
-
- -
- lang->line('config_default_tax_rate_2'), 'default_tax_2_rate', array('class' => 'control-label col-xs-2')); ?> -
- 'default_tax_2_name', - 'id' => 'default_tax_2_name', - 'class' => 'form-control input-sm', - 'value'=>$this->config->item('default_tax_2_name')!==FALSE ? $this->config->item('default_tax_2_name') : $this->lang->line('items_sales_tax_2'))); ?> -
-
- 'default_tax_2_rate', - 'id' => 'default_tax_2_rate', - 'class' => 'form-control input-sm', - 'value'=> to_tax_decimals($this->config->item('default_tax_2_rate')))); ?> - % -
-
- -
- lang->line('config_tax_included'), 'tax_included', array('class' => 'control-label col-xs-2')); ?> -
- 'tax_included', - 'id' => 'tax_included', - 'value' => 'tax_included', - 'checked'=>$this->config->item('tax_included'))); ?> -
-
- -
- lang->line('config_customer_sales_tax_support'), 'customer_sales_tax_support', array('class' => 'control-label col-xs-2')); ?> -
- 'customer_sales_tax_support', - 'id' => 'customer_sales_tax_support', - 'value' => 'customer_sales_tax_support', - 'checked'=>$this->config->item('customer_sales_tax_support'))); ?> -
-
- -
- lang->line('config_default_origin_tax_code'), 'default_origin_tax_code', array('class' => 'control-label col-xs-2')); ?> -
- 'default_origin_tax_code', - 'id' => 'default_origin_tax_code', - 'class' => 'form-control input-sm text-uppercase', - 'value'=>$this->config->item('default_origin_tax_code'))); ?> -
-
-
lang->line('config_default_sales_discount'), 'default_sales_discount', array('class' => 'control-label col-xs-2 required')); ?>
@@ -323,14 +252,6 @@ $(document).ready(function() rules: { - default_tax_1_rate: - { - remote: "" - }, - default_tax2_rate: - { - remote: "" - }, lines_per_page: { required: true, @@ -341,20 +262,16 @@ $(document).ready(function() required: true, remote: "" } - }, + }, messages: { - default_tax_1_rate: - { - number: "lang->line('config_default_tax_rate_number'); ?>" - }, default_sales_discount: - { - required: "lang->line('config_default_sales_discount_required'); ?>", - number: "lang->line('config_default_sales_discount_number'); ?>" - }, - lines_per_page: + { + required: "lang->line('config_default_sales_discount_required'); ?>", + number: "lang->line('config_default_sales_discount_number'); ?>" + }, + lines_per_page: { required: "lang->line('config_lines_per_page_required'); ?>", number: "lang->line('config_lines_per_page_number'); ?>" diff --git a/application/views/configs/manage.php b/application/views/configs/manage.php index 821b1ae54..386049bc3 100644 --- a/application/views/configs/manage.php +++ b/application/views/configs/manage.php @@ -1,37 +1,40 @@ load->view("partial/header"); ?>
-
- load->view("configs/info_config"); ?> -
-
- load->view("configs/general_config"); ?> -
-
- load->view("configs/locale_config"); ?> -
-
- load->view("configs/barcode_config"); ?> -
-
- load->view("configs/stock_config"); ?> -
-
- load->view("configs/table_config"); ?> -
-
- load->view("configs/reward_config"); ?> -
-
- load->view("configs/receipt_config"); ?> -
-
- load->view("configs/invoice_config"); ?> -
+
+ load->view("configs/info_config"); ?> +
+
+ load->view("configs/general_config"); ?> +
+
+ load->view("configs/tax_config"); ?> +
+
+ load->view("configs/locale_config"); ?> +
+
+ load->view("configs/barcode_config"); ?> +
+
+ load->view("configs/stock_config"); ?> +
+
+ load->view("configs/table_config"); ?> +
+
+ load->view("configs/reward_config"); ?> +
+
+ load->view("configs/receipt_config"); ?> +
+
+ load->view("configs/invoice_config"); ?> +
load->view("configs/email_config"); ?>
diff --git a/application/views/configs/tax_config.php b/application/views/configs/tax_config.php new file mode 100644 index 000000000..1e4ac074a --- /dev/null +++ b/application/views/configs/tax_config.php @@ -0,0 +1,238 @@ + 'tax_config_form', 'class' => 'form-horizontal')); ?> +
+
+
lang->line('common_fields_required_message'); ?>
+
    + +
    + lang->line('config_tax_included'), 'tax_included', array('class' => 'control-label col-xs-2')); ?> +
    + 'tax_included', + 'id' => 'tax_included', + 'value' => 'tax_included', + 'checked'=>$this->config->item('tax_included'))); ?> +
    +
    + +
    + lang->line('config_default_tax_rate_1'), 'default_tax_1_rate', array('class' => 'control-label col-xs-2')); ?> +
    + 'default_tax_1_name', + 'id' => 'default_tax_1_name', + 'class' => 'form-control input-sm', + 'value'=>$this->config->item('default_tax_1_name')!==FALSE ? $this->config->item('default_tax_1_name') : $this->lang->line('items_sales_tax_1'))); ?> +
    +
    + 'default_tax_1_rate', + 'id' => 'default_tax_1_rate', + 'class' => 'form-control input-sm', + 'value'=> to_tax_decimals($this->config->item('default_tax_1_rate')))); ?> + % +
    +
    + +
    + lang->line('config_default_tax_rate_2'), 'default_tax_2_rate', array('class' => 'control-label col-xs-2')); ?> +
    + 'default_tax_2_name', + 'id' => 'default_tax_2_name', + 'class' => 'form-control input-sm', + 'value'=>$this->config->item('default_tax_2_name')!==FALSE ? $this->config->item('default_tax_2_name') : $this->lang->line('items_sales_tax_2'))); ?> +
    +
    + 'default_tax_2_rate', + 'id' => 'default_tax_2_rate', + 'class' => 'form-control input-sm', + 'value'=> to_tax_decimals($this->config->item('default_tax_2_rate')))); ?> + % +
    +
    + +
    + lang->line('config_customer_sales_tax_support'), 'customer_sales_tax_support', array('class' => 'control-label col-xs-2')); ?> +
    + 'customer_sales_tax_support', + 'id' => 'customer_sales_tax_support', + 'value' => 'customer_sales_tax_support', + 'checked'=>$this->config->item('customer_sales_tax_support'))); ?> +
    +
    + +
    + lang->line('config_default_origin_tax_code'), 'default_origin_tax_code', array('class' => 'control-label col-xs-2')); ?> +
    + config->item('default_origin_tax_code'), array('class' => 'form-control input-sm')); ?> +
    +
    + + +
    + load->view('partial/tax_categories', array('tax_categories' => $tax_categories)); ?> +
    + + 'submit', + 'id' => 'submit', + 'value'=>$this->lang->line('common_submit'), + 'class' => 'btn btn-primary btn-sm pull-right')); ?> +
    +
    + + + diff --git a/application/views/partial/tax_categories.php b/application/views/partial/tax_categories.php new file mode 100644 index 000000000..2f819044d --- /dev/null +++ b/application/views/partial/tax_categories.php @@ -0,0 +1,39 @@ +$category) +{ + $tax_category_id = $category['tax_category_id']; + $tax_category = $category['tax_category']; + $tax_group_sequence = $category['tax_group_sequence']; + ++$i; +?> +
    + lang->line('config_tax_category') . ' ' . $i, 'tax_category_' . $i, array('class'=>'control-label col-xs-2')); ?> +
    + 'tax_category_' . $tax_category_id, + 'id'=>'tax_category_' . $tax_category_id, + 'class'=>'valid_chars form-control input-sm', + 'value'=>$tax_category + ); + echo form_input($form_data); + ?> +
    +
    + 'tax_group_sequence_' . $tax_category_id, + 'id'=>'tax_group_sequence_' . $tax_category_id, + 'class'=>'valid_chars form-control input-sm', + 'value'=>$tax_group_sequence + ); + echo form_input($form_data); + ?> +
    + +    + +
    + diff --git a/database/3.0.2_to_3.1.0.sql b/database/3.0.2_to_3.1.0.sql index feab9fe79..a41b0738e 100644 --- a/database/3.0.2_to_3.1.0.sql +++ b/database/3.0.2_to_3.1.0.sql @@ -243,3 +243,8 @@ INSERT INTO `ospos_grants` (`permission_id`, `person_id`) VALUES -- update to receivings UPDATE ospos_items SET receiving_quantity = 1 WHERE receiving_quantity = 0; + +-- fix tax category maintenance + +ALTER TABLE `ospos_tax_categories` + MODIFY COLUMN `tax_category_id` int(10) NOT NULL AUTO_INCREMENT; diff --git a/database/database.sql b/database/database.sql index c16d0aef2..9184cfc20 100644 --- a/database/database.sql +++ b/database/database.sql @@ -810,7 +810,7 @@ CREATE TABLE `ospos_suppliers` ( -- CREATE TABLE IF NOT EXISTS `ospos_tax_categories` ( - `tax_category_id` int(10) NOT NULL, + `tax_category_id` int(10) NOT NULL AUTO_INCREMENT, `tax_category` varchar(32) NOT NULL, `tax_group_sequence` tinyint(2) NOT NULL, PRIMARY KEY (`tax_category_id`) diff --git a/database/tables.sql b/database/tables.sql index b36194a2d..15f52e73a 100644 --- a/database/tables.sql +++ b/database/tables.sql @@ -810,7 +810,7 @@ CREATE TABLE `ospos_suppliers` ( -- CREATE TABLE IF NOT EXISTS `ospos_tax_categories` ( - `tax_category_id` int(10) NOT NULL, + `tax_category_id` int(10) NOT NULL AUTO_INCREMENT, `tax_category` varchar(32) NOT NULL, `tax_group_sequence` tinyint(2) NOT NULL, PRIMARY KEY (`tax_category_id`)