From eccf08cf8f4a7242a3d1817d418a94a55bce5be2 Mon Sep 17 00:00:00 2001 From: Steve Ireland Date: Sat, 15 Jul 2017 16:42:27 -0400 Subject: [PATCH] Improve tax category maintenance. --- application/controllers/Config.php | 25 ++++++++++++++++++-- application/language/en/config_lang.php | 1 + application/models/Item.php | 8 +++++++ application/models/Tax.php | 8 +++++++ application/views/partial/tax_categories.php | 8 ++++++- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/application/controllers/Config.php b/application/controllers/Config.php index db2bc40e8..794a803e0 100644 --- a/application/controllers/Config.php +++ b/application/controllers/Config.php @@ -637,6 +637,7 @@ class Config extends Secure_Controller ); $success = $this->Appconfig->batch_save($batch_save_data) ? TRUE : FALSE; + $delete_rejected = FALSE; if($customer_sales_tax_support) { @@ -674,7 +675,16 @@ class Config extends Secure_Controller { if(!in_array($category['tax_category_id'], $not_to_delete)) { - $this->Tax->delete_tax_category($category['tax_category_id']); + $usg1 = $this->Tax->get_tax_category_usage($category['tax_category_id']); + $usg2 = $this->Item->get_tax_category_usage($category['tax_category_id']); + if(($usg1 + $usg2) == 0) + { + $this->Tax->delete_tax_category($category['tax_category_id']); + } + else + { + $delete_rejected = TRUE; + } } } } @@ -683,9 +693,20 @@ class Config extends Secure_Controller $success &= $this->db->trans_status(); + $message = ""; + if($success && $delete_rejected) + { + $message = $this->lang->line('config_tax_category_used'); + $success = false; + } + else + { + $message = $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully'); + } + echo json_encode(array( 'success' => $success, - 'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully') + 'message' => $message )); } diff --git a/application/language/en/config_lang.php b/application/language/en/config_lang.php index 2735698c6..528d704bf 100644 --- a/application/language/en/config_lang.php +++ b/application/language/en/config_lang.php @@ -225,6 +225,7 @@ $lang["config_table_configuration"] = "Table Configuration"; $lang["config_takings_printer"] = "Takings Printer"; $lang["config_tax"] = "Tax"; $lang["config_tax_category"] = "Tax Category"; +$lang["config_tax_category_used"] = "Tax category cannot be deleted because it is being used."; $lang["config_tax_configuration"] = "Tax Configuration"; $lang["config_tax_decimals"] = "Tax Decimals"; $lang["config_tax_included"] = "Tax Included"; diff --git a/application/models/Item.php b/application/models/Item.php index f3aedc25a..28bdb740d 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -52,6 +52,14 @@ class Item extends CI_Model return $this->db->count_all_results(); } + public function get_tax_category_usage($tax_category_id) + { + $this->db->from('items'); + $this->db->where('tax_category_id', $tax_category_id); + + return $this->db->count_all_results(); + } + /* Get number of rows */ diff --git a/application/models/Tax.php b/application/models/Tax.php index 9df461acd..0386d8f17 100644 --- a/application/models/Tax.php +++ b/application/models/Tax.php @@ -44,6 +44,14 @@ class Tax extends CI_Model return $this->db->count_all_results(); } + public function get_tax_category_usage($tax_category_id) + { + $this->db->from('tax_code_rates'); + $this->db->where('rate_tax_category_id', $tax_category_id); + + return $this->db->count_all_results(); + } + /** Gets information about a particular tax_code */ diff --git a/application/views/partial/tax_categories.php b/application/views/partial/tax_categories.php index 38b47c6ba..dbb3129df 100644 --- a/application/views/partial/tax_categories.php +++ b/application/views/partial/tax_categories.php @@ -32,7 +32,13 @@ foreach($tax_categories as $tax_category => $category)    - + 0) { + ?> + +