From 230d822fcef609eb991ac64bb871daf9af6e17b2 Mon Sep 17 00:00:00 2001 From: SteveIreland Date: Sat, 21 Dec 2019 23:20:51 -0500 Subject: [PATCH 1/2] Add support to parse_decimal function for tax_decimals and quantity_decimals parsing. --- application/controllers/Config.php | 12 +++++++----- application/controllers/Items.php | 12 ++++++------ application/controllers/Receivings.php | 4 ++-- application/controllers/Sales.php | 2 +- application/controllers/Taxes.php | 2 +- application/helpers/locale_helper.php | 11 +++++++++-- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/application/controllers/Config.php b/application/controllers/Config.php index 0ff258699..3707dbf31 100644 --- a/application/controllers/Config.php +++ b/application/controllers/Config.php @@ -635,11 +635,13 @@ class Config extends Secure_Controller public function save_tax() { $this->db->trans_start(); - + + $decimals = $this->config->item('tax_decimals'); + $batch_save_data = array( - 'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate')), + 'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate'), $decimals), '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_rate' => parse_decimals($this->input->post('default_tax_2_rate'), $decimals), 'default_tax_2_name' => $this->input->post('default_tax_2_name'), 'tax_included' => $this->input->post('tax_included') != NULL, 'use_destination_based_tax' => $this->input->post('use_destination_based_tax') != NULL, @@ -648,9 +650,9 @@ class Config extends Secure_Controller 'default_tax_jurisdiction' => $this->input->post('default_tax_jurisdiction'), 'tax_id' => $this->input->post('tax_id') ); - + $success = $this->Appconfig->batch_save($batch_save_data) ? TRUE : FALSE; - + $this->db->trans_complete(); $success &= $this->db->trans_status(); diff --git a/application/controllers/Items.php b/application/controllers/Items.php index 9025b8d75..b8d5df198 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -479,7 +479,7 @@ class Items extends Secure_Controller $upload_success = $this->_handle_image_upload(); $upload_data = $this->upload->data(); - $receiving_quantity = parse_decimals($this->input->post('receiving_quantity')); + $receiving_quantity = parse_decimals($this->input->post('receiving_quantity'), $this->config->item('quantity_decimals')); $item_type = $this->input->post('item_type') == NULL ? ITEM : $this->input->post('item_type'); if($receiving_quantity == '0' && $item_type!= ITEM_TEMP) @@ -499,7 +499,7 @@ class Items extends Secure_Controller 'item_number' => $this->input->post('item_number') == '' ? NULL : $this->input->post('item_number'), 'cost_price' => parse_decimals($this->input->post('cost_price')), 'unit_price' => parse_decimals($this->input->post('unit_price')), - 'reorder_level' => parse_decimals($this->input->post('reorder_level')), + 'reorder_level' => parse_decimals($this->input->post('reorder_level'), $this->config->item('quantity_decimals')), 'receiving_quantity' => $receiving_quantity, 'allow_alt_description' => $this->input->post('allow_alt_description') != NULL, 'is_serialized' => $this->input->post('is_serialized') != NULL, @@ -559,7 +559,7 @@ class Items extends Secure_Controller $count = count($tax_percents); for ($k = 0; $k < $count; ++$k) { - $tax_percentage = parse_decimals($tax_percents[$k]); + $tax_percentage = parse_decimals($tax_percents[$k], $this->config->item('tax_decimals')); if(is_numeric($tax_percentage)) { $items_taxes_data[] = array('name' => $tax_names[$k], 'percent' => $tax_percentage); @@ -572,7 +572,7 @@ class Items extends Secure_Controller $stock_locations = $this->Stock_location->get_undeleted_all()->result_array(); foreach($stock_locations as $location) { - $updated_quantity = parse_decimals($this->input->post('quantity_' . $location['location_id'])); + $updated_quantity = parse_decimals($this->input->post('quantity_' . $location['location_id']), $this->config->item('quantity_decimals')); if($item_data['item_type'] == ITEM_TEMP) { $updated_quantity = 0; @@ -693,7 +693,7 @@ class Items extends Secure_Controller 'trans_user' => $employee_id, 'trans_location' => $location_id, 'trans_comment' => $this->input->post('trans_comment'), - 'trans_inventory' => parse_decimals($this->input->post('newquantity')) + 'trans_inventory' => parse_decimals($this->input->post('newquantity'), $this->config->item('quantity_decimals')) ); $this->Inventory->insert($inv_data); @@ -703,7 +703,7 @@ class Items extends Secure_Controller $item_quantity_data = array( 'item_id' => $item_id, 'location_id' => $location_id, - 'quantity' => $item_quantity->quantity + parse_decimals($this->input->post('newquantity')) + 'quantity' => $item_quantity->quantity + parse_decimals($this->input->post('newquantity'), $this->config->item('quantity_decimals')) ); if($this->Item_quantity->save($item_quantity_data, $item_id, $location_id)) diff --git a/application/controllers/Receivings.php b/application/controllers/Receivings.php index 8ee3c3b44..ef2892be9 100644 --- a/application/controllers/Receivings.php +++ b/application/controllers/Receivings.php @@ -123,9 +123,9 @@ class Receivings extends Secure_Controller $description = $this->input->post('description'); $serialnumber = $this->input->post('serialnumber'); $price = parse_decimals($this->input->post('price')); - $quantity = parse_decimals($this->input->post('quantity')); + $quantity = parse_decimals($this->input->post('quantity'), $this->config->item('quantity_decimals')); $discount = parse_decimals($this->input->post('discount')); - $discount_type = parse_decimals($this->input->post('discount_type')); + $discount_type = $this->input->post('discount_type'); $item_location = $this->input->post('location'); $receiving_quantity = $this->input->post('receiving_quantity'); diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index d77221f23..5d30177e0 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -474,7 +474,7 @@ class Sales extends Secure_Controller $description = $this->input->post('description'); $serialnumber = $this->input->post('serialnumber'); $price = parse_decimals($this->input->post('price')); - $quantity = parse_decimals($this->input->post('quantity')); + $quantity = parse_decimals($this->input->post('quantity'), $this->config->item('quantity_decimals')); $discount = parse_decimals($this->input->post('discount')); $discount_type = $this->input->post('discount_type'); diff --git a/application/controllers/Taxes.php b/application/controllers/Taxes.php index 873bb7b95..a070476af 100644 --- a/application/controllers/Taxes.php +++ b/application/controllers/Taxes.php @@ -354,7 +354,7 @@ class Taxes extends Secure_Controller public function save($tax_rate_id = -1) { $tax_category_id = $this->input->post('rate_tax_category_id'); - $tax_rate = parse_decimals($this->input->post('tax_rate')); + $tax_rate = parse_decimals($this->input->post('tax_rate'), $this->config->item('tax_decimals')); if ($tax_rate == 0) { $tax_category_info = $this->Tax_category->get_info($tax_category_id); diff --git a/application/helpers/locale_helper.php b/application/helpers/locale_helper.php index 3fd7a3c95..1e302945e 100644 --- a/application/helpers/locale_helper.php +++ b/application/helpers/locale_helper.php @@ -400,18 +400,25 @@ function to_decimals($number, $decimals, $type=\NumberFormatter::DECIMAL) return $fmt->format($number); } -function parse_decimals($number) +function parse_decimals($number, $decimals = NULL) { // ignore empty strings and return + if(empty($number)) { return $number; } $config = get_instance()->config; + + if($decimals == NULL) + { + $decimals = $config->item('currency_decimals'); + } + $fmt = new \NumberFormatter($config->item('number_locale'), \NumberFormatter::DECIMAL); - $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $config->item('currency_decimals')); + $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimals); if(empty($config->item('thousands_separator'))) { From 43420f02f20ccc01c9d6606127ee28409c2d945b Mon Sep 17 00:00:00 2001 From: jekkos Date: Thu, 2 Jan 2020 12:40:31 +0100 Subject: [PATCH 2/2] Codestyle remarks --- application/controllers/Config.php | 6 ++---- application/controllers/Items.php | 12 ++++++------ application/controllers/Receivings.php | 2 +- application/controllers/Sales.php | 2 +- application/controllers/Taxes.php | 2 +- application/helpers/locale_helper.php | 10 ++++++++++ 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/application/controllers/Config.php b/application/controllers/Config.php index 3707dbf31..20f523384 100644 --- a/application/controllers/Config.php +++ b/application/controllers/Config.php @@ -636,12 +636,10 @@ class Config extends Secure_Controller { $this->db->trans_start(); - $decimals = $this->config->item('tax_decimals'); - $batch_save_data = array( - 'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate'), $decimals), + 'default_tax_1_rate' => parse_tax($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'), $decimals), + 'default_tax_2_rate' => parse_tax($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, 'use_destination_based_tax' => $this->input->post('use_destination_based_tax') != NULL, diff --git a/application/controllers/Items.php b/application/controllers/Items.php index b8d5df198..d5ca2d2cc 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -479,7 +479,7 @@ class Items extends Secure_Controller $upload_success = $this->_handle_image_upload(); $upload_data = $this->upload->data(); - $receiving_quantity = parse_decimals($this->input->post('receiving_quantity'), $this->config->item('quantity_decimals')); + $receiving_quantity = parse_quantity($this->input->post('receiving_quantity')); $item_type = $this->input->post('item_type') == NULL ? ITEM : $this->input->post('item_type'); if($receiving_quantity == '0' && $item_type!= ITEM_TEMP) @@ -499,7 +499,7 @@ class Items extends Secure_Controller 'item_number' => $this->input->post('item_number') == '' ? NULL : $this->input->post('item_number'), 'cost_price' => parse_decimals($this->input->post('cost_price')), 'unit_price' => parse_decimals($this->input->post('unit_price')), - 'reorder_level' => parse_decimals($this->input->post('reorder_level'), $this->config->item('quantity_decimals')), + 'reorder_level' => parse_quantity($this->input->post('reorder_level')), 'receiving_quantity' => $receiving_quantity, 'allow_alt_description' => $this->input->post('allow_alt_description') != NULL, 'is_serialized' => $this->input->post('is_serialized') != NULL, @@ -559,7 +559,7 @@ class Items extends Secure_Controller $count = count($tax_percents); for ($k = 0; $k < $count; ++$k) { - $tax_percentage = parse_decimals($tax_percents[$k], $this->config->item('tax_decimals')); + $tax_percentage = parse_tax($tax_percents[$k]); if(is_numeric($tax_percentage)) { $items_taxes_data[] = array('name' => $tax_names[$k], 'percent' => $tax_percentage); @@ -572,7 +572,7 @@ class Items extends Secure_Controller $stock_locations = $this->Stock_location->get_undeleted_all()->result_array(); foreach($stock_locations as $location) { - $updated_quantity = parse_decimals($this->input->post('quantity_' . $location['location_id']), $this->config->item('quantity_decimals')); + $updated_quantity = parse_quantity($this->input->post('quantity_' . $location['location_id'])); if($item_data['item_type'] == ITEM_TEMP) { $updated_quantity = 0; @@ -693,7 +693,7 @@ class Items extends Secure_Controller 'trans_user' => $employee_id, 'trans_location' => $location_id, 'trans_comment' => $this->input->post('trans_comment'), - 'trans_inventory' => parse_decimals($this->input->post('newquantity'), $this->config->item('quantity_decimals')) + 'trans_inventory' => parse_quantity($this->input->post('newquantity')) ); $this->Inventory->insert($inv_data); @@ -703,7 +703,7 @@ class Items extends Secure_Controller $item_quantity_data = array( 'item_id' => $item_id, 'location_id' => $location_id, - 'quantity' => $item_quantity->quantity + parse_decimals($this->input->post('newquantity'), $this->config->item('quantity_decimals')) + 'quantity' => $item_quantity->quantity + parse_quantity($this->input->post('newquantity')) ); if($this->Item_quantity->save($item_quantity_data, $item_id, $location_id)) diff --git a/application/controllers/Receivings.php b/application/controllers/Receivings.php index ef2892be9..faa90915a 100644 --- a/application/controllers/Receivings.php +++ b/application/controllers/Receivings.php @@ -123,7 +123,7 @@ class Receivings extends Secure_Controller $description = $this->input->post('description'); $serialnumber = $this->input->post('serialnumber'); $price = parse_decimals($this->input->post('price')); - $quantity = parse_decimals($this->input->post('quantity'), $this->config->item('quantity_decimals')); + $quantity = parse_quantity($this->input->post('quantity')); $discount = parse_decimals($this->input->post('discount')); $discount_type = $this->input->post('discount_type'); $item_location = $this->input->post('location'); diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 5d30177e0..ae8e37cd0 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -474,7 +474,7 @@ class Sales extends Secure_Controller $description = $this->input->post('description'); $serialnumber = $this->input->post('serialnumber'); $price = parse_decimals($this->input->post('price')); - $quantity = parse_decimals($this->input->post('quantity'), $this->config->item('quantity_decimals')); + $quantity = parse_quantity($this->input->post('quantity')); $discount = parse_decimals($this->input->post('discount')); $discount_type = $this->input->post('discount_type'); diff --git a/application/controllers/Taxes.php b/application/controllers/Taxes.php index a070476af..4c6a6004d 100644 --- a/application/controllers/Taxes.php +++ b/application/controllers/Taxes.php @@ -354,7 +354,7 @@ class Taxes extends Secure_Controller public function save($tax_rate_id = -1) { $tax_category_id = $this->input->post('rate_tax_category_id'); - $tax_rate = parse_decimals($this->input->post('tax_rate'), $this->config->item('tax_decimals')); + $tax_rate = parse_tax($this->input->post('tax_rate')); if ($tax_rate == 0) { $tax_category_info = $this->Tax_category->get_info($tax_category_id); diff --git a/application/helpers/locale_helper.php b/application/helpers/locale_helper.php index 1e302945e..641199474 100644 --- a/application/helpers/locale_helper.php +++ b/application/helpers/locale_helper.php @@ -400,6 +400,16 @@ function to_decimals($number, $decimals, $type=\NumberFormatter::DECIMAL) return $fmt->format($number); } +function parse_quantity($number) +{ + return parse_decimals($number, quantity_decimals()); +} + +function parse_tax($number) +{ + return parse_decimals($number, tax_decimals()); +} + function parse_decimals($number, $decimals = NULL) { // ignore empty strings and return