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 @@ -