OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
remove currency separator duplicity
ulferts379f0ed72cfa
diff --git a/modules/costs/app/controllers/cost_objects_controller.rb b/modules/costs/app/controllers/cost_objects_controller.rbindex d0333faf670..153c44ef3b1 100644--- a/modules/costs/app/controllers/cost_objects_controller.rb+++ b/modules/costs/app/controllers/cost_objects_controller.rb@@ -196,7 +196,7 @@ class CostObjectsController < ApplicationControllercost_type = CostType.where(id: params[:cost_type_id]).firstif cost_type && params[:units].present?- volume = BigDecimal(Rate.clean_currency(params[:units])) rescue 0.0+ volume = Rate.parse_number_string_to_number(params[:units])@costs = (volume * cost_type.rate_at(params[:fixed_date]).rate rescue 0.0)@unit = volume == 1.0 ? cost_type.unit : cost_type.unit_pluralelsediff --git a/modules/costs/app/controllers/cost_types_controller.rb b/modules/costs/app/controllers/cost_types_controller.rbindex f36fc08020a..bacf91306b3 100644--- a/modules/costs/app/controllers/cost_types_controller.rb+++ b/modules/costs/app/controllers/cost_types_controller.rb@@ -129,7 +129,7 @@ class CostTypesController < ApplicationControllercr.valid_from = todayend- rate.rate = clean_currency(params[:rate])+ rate.rate = parse_number_string_to_number(params[:rate])if rate.saveflash[:notice] = t(:notice_successful_update)redirect_to action: 'index'diff --git a/modules/costs/app/controllers/hourly_rates_controller.rb b/modules/costs/app/controllers/hourly_rates_controller.rbindex 411d5be9ca9..a5e9f2db0c6 100644--- a/modules/costs/app/controllers/hourly_rates_controller.rb+++ b/modules/costs/app/controllers/hourly_rates_controller.rb@@ -134,7 +134,7 @@ class HourlyRatesController < ApplicationControllerhr.project = @projecthr.user = @userhr.valid_from = today- hr.rate = clean_currency(params[:rate])+ hr.rate = parse_number_string_to_number(params[:rate])endif rate.savediff --git a/modules/costs/app/helpers/costlog_helper.rb b/modules/costs/app/helpers/costlog_helper.rbindex a386daed7ad..49f63f9f278 100644--- a/modules/costs/app/helpers/costlog_helper.rb+++ b/modules/costs/app/helpers/costlog_helper.rb@@ -51,19 +51,4 @@ module CostlogHelperdone = 100.0 - ((100.0 / pcts) * 100).roundprogress_bar([closed, done], options)end-- def clean_currency(value)- return nil if value.nil? || value == ''-- value = value.strip- value.gsub!(t(:currency_delimiter), '') if value.include?(t(:currency_delimiter)) && value.include?(t(:currency_separator))- value.gsub(',', '.')- BigDecimal(value)- end-- def to_currency_with_empty(rate)- rate.nil? ?- '0.0' :- number_to_currency(rate.rate)- endenddiff --git a/modules/costs/app/helpers/costs/number_helper.rb b/modules/costs/app/helpers/costs/number_helper.rbnew file mode 100644index 00000000000..55391a63ad7--- /dev/null+++ b/modules/costs/app/helpers/costs/number_helper.rb@@ -0,0 +1,65 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Costs::NumberHelper+ # Turns a string representing a number in the current locale+ # to a string representing a number in en (without delimiters).+ def parse_number_string(value)+ return value unless value&.is_a?(String) && value.present?++ value = value.strip++ # All locales seem to have their delimiters set to "".+ # We thus remove all typical delimiters that are not the separator.+ separator = I18n.t(:'number.format.separator')++ if separator+ delimiters = Regexp.new('[ .,’˙]'.gsub(separator, ''))++ value.gsub!(delimiters, '')++ value.gsub!(separator, '.')+ end++ value+ end++ # Turns a string representing a number in the current locale+ # to a BigDecimal number.+ #+ # In case the string cannot be parsed, 0.0 is returned.+ def parse_number_string_to_number(value)+ BigDecimal(parse_number_string(value))+ rescue TypeError, ArgumentError+ 0.0+ end++ def to_currency_with_empty(rate)+ rate.nil? ? '0.0' : number_to_currency(rate.rate)+ end+enddiff --git a/modules/costs/app/models/cost_entry.rb b/modules/costs/app/models/cost_entry.rbindex 5dc64fd011d..3819ac1359e 100644--- a/modules/costs/app/models/cost_entry.rb+++ b/modules/costs/app/models/cost_entry.rb@@ -82,11 +82,11 @@ class CostEntry < ActiveRecord::Baseenddef overwritten_costs=(costs)- write_attribute(:overwritten_costs, CostRate.clean_currency(costs))+ write_attribute(:overwritten_costs, CostRate.parse_number_string_to_number(costs))enddef units=(units)- write_attribute(:units, CostRate.clean_currency(units))+ write_attribute(:units, CostRate.parse_number_string(units))end# tyear, tmonth, tweek assigned where setting spent_on attributesdiff --git a/modules/costs/app/models/cost_type.rb b/modules/costs/app/models/cost_type.rbindex 6cdcba84e71..f4074ea1c56 100644--- a/modules/costs/app/models/cost_type.rb+++ b/modules/costs/app/models/cost_type.rb@@ -73,7 +73,7 @@ class CostType < ActiveRecord::Basedef new_rate_attributes=(rate_attributes)rate_attributes.each do |_index, attributes|- attributes[:rate] = Rate.clean_currency(attributes[:rate])+ attributes[:rate] = Rate.parse_number_string(attributes[:rate])rates.build(attributes)endend@@ -84,7 +84,7 @@ class CostType < ActiveRecord::Basehas_rate = falseif attributes && attributes[:rate].present?- attributes[:rate] = Rate.clean_currency(attributes[:rate])+ attributes[:rate] = Rate.parse_number_string(attributes[:rate])has_rate = trueenddiff --git a/modules/costs/app/models/rate.rb b/modules/costs/app/models/rate.rbindex a46772b7287..6f618d17985 100644--- a/modules/costs/app/models/rate.rb+++ b/modules/costs/app/models/rate.rb@@ -42,15 +42,7 @@ class Rate < ActiveRecord::Baseinclude ActiveModel::ForbiddenAttributesProtection- def self.clean_currency(value)- if value && value.is_a?(String)- value = value.strip- value.gsub!(I18n.t(:currency_delimiter), '') if value.include?(I18n.t(:currency_delimiter)) && value.include?(I18n.t(:currency_separator))- value.gsub(',', '.')- else- value- end- end+ extend Costs::NumberHelperprivate@@ -65,7 +57,7 @@ class Rate < ActiveRecord::Baseenddef update_costs- entry_class = self.is_a?(HourlyRate) ? TimeEntry : CostEntry+ entry_class = is_a?(HourlyRate) ? TimeEntry : CostEntryentry_class.where(rate_id: id).each(&:update_costs!)end@@ -74,10 +66,10 @@ class Rate < ActiveRecord::Basenext_rate = self.next# get entries from the current project- entries = o.find_entries(valid_from, (next_rate.valid_from if next_rate))+ entries = o.find_entries(valid_from, (next_rate&.valid_from))# and entries from subprojects that need updating (only applies to hourly_rates)- entries += o.orphaned_child_entries(valid_from, (next_rate.valid_from if next_rate))+ entries += o.orphaned_child_entries(valid_from, (next_rate&.valid_from))o.update_entries(entries)enddiff --git a/modules/costs/app/models/variable_cost_object.rb b/modules/costs/app/models/variable_cost_object.rbindex df69f318ec8..a0f216ad9d1 100644--- a/modules/costs/app/models/variable_cost_object.rb+++ b/modules/costs/app/models/variable_cost_object.rb@@ -112,7 +112,7 @@ class VariableCostObject < CostObjectif User.current.allowed_to? :edit_cost_objects, material_budget_item.cost_object.projectif attributes && attributes[:units].to_i > 0- attributes[:budget] = Rate.clean_currency(attributes[:budget])+ attributes[:budget] = Rate.parse_number_string(attributes[:budget])material_budget_item.attributes = attributeselsematerial_budget_items.delete(material_budget_item)@@ -144,7 +144,7 @@ class VariableCostObject < CostObjectattributes = labor_budget_item_attributes[labor_budget_item.id.to_s]if User.current.allowed_to? :edit_cost_objects, labor_budget_item.cost_object.projectif attributes && attributes[:hours].to_i > 0 && attributes[:user_id].to_i > 0 && project.possible_assignees.map(&:id).include?(attributes[:user_id].to_i)- attributes[:budget] = Rate.clean_currency(attributes[:budget])+ attributes[:budget] = Rate.parse_number_string(attributes[:budget])labor_budget_item.attributes = attributeselselabor_budget_items.delete(labor_budget_item)diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.ymlindex 329f174a063..0f4edf04dae 100644--- a/modules/costs/config/locales/en.yml+++ b/modules/costs/config/locales/en.yml@@ -113,9 +113,6 @@ en:caption_set_rate: "Set current rate"caption_show_locked: "Show locked types"cost_objects_title: "Budgets"- label_cost_type_plural: "Cost types"- currency_delimiter: ","- currency_separator: "."description_date_for_new_rate: "Date for new rate"diff --git a/modules/costs/lib/open_project/costs/patches/number_to_currency_converter_patch.rb b/modules/costs/lib/open_project/costs/patches/number_to_currency_converter_patch.rbindex 117f01da73b..0a38c204922 100644--- a/modules/costs/lib/open_project/costs/patches/number_to_currency_converter_patch.rb+++ b/modules/costs/lib/open_project/costs/patches/number_to_currency_converter_patch.rb@@ -34,9 +34,7 @@ module OpenProject::Costs::Patches::NumberToCurrencyConverterPatchmodule InstanceMethodsdef i18n_optssuper.merge(unit: ERB::Util.h(Setting.plugin_openproject_costs['costs_currency']),- format: ERB::Util.h(Setting.plugin_openproject_costs['costs_currency_format']),- delimiter: I18n.t(:currency_delimiter),- separator: I18n.t(:currency_separator))+ format: ERB::Util.h(Setting.plugin_openproject_costs['costs_currency_format']))endendend\ No newline at end of filediff --git a/modules/costs/lib/open_project/costs/patches/user_patch.rb b/modules/costs/lib/open_project/costs/patches/user_patch.rbindex ee826991770..ba6e6252771 100644--- a/modules/costs/lib/open_project/costs/patches/user_patch.rb+++ b/modules/costs/lib/open_project/costs/patches/user_patch.rb@@ -74,7 +74,7 @@ module OpenProject::Costs::Patches::UserPatchreturn unless rate_attributesrate_attributes.each do |_index, attributes|- attributes[:rate] = Rate.clean_currency(attributes[:rate])+ attributes[:rate] = Rate.parse_number_string(attributes[:rate])if project.nil?default_rates.build(attributes)@@ -107,7 +107,7 @@ module OpenProject::Costs::Patches::UserPatchdef update_rate(rate, attributes, project_rate = true)if attributes && attributes[:rate].present?- attributes[:rate] = Rate.clean_currency(attributes[:rate])+ attributes[:rate] = Rate.parse_number_string(attributes[:rate])rate.attributes = attributeselse# TODO: this is surprisingdiff --git a/modules/costs/spec/helpers/costs/number_helper_spec.rb b/modules/costs/spec/helpers/costs/number_helper_spec.rbnew file mode 100644index 00000000000..01a3e048f40--- /dev/null+++ b/modules/costs/spec/helpers/costs/number_helper_spec.rb@@ -0,0 +1,142 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require File.dirname(__FILE__) + '/../../spec_helper'++describe Costs::NumberHelper, type: :helper do+ describe '#parse_number_string' do+ context 'for a german local' do+ it 'parses a string with delimiter and separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("123.456,78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string with space delimiter and separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("123 456,78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string without delimiter and separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("12345678"))+ .to eql "12345678"+ end+ end++ it 'parses a string without delimiter and with separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("123456,78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string with delimiter and without separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("12.345.678"))+ .to eql "12345678"+ end+ end++ it 'parses a string with space delimiter and without separator correctly' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("12 345 678"))+ .to eql "12345678"+ end+ end++ it 'returns alphabetical values instead of a delimiter unchanged' do+ I18n.with_locale(:de) do+ expect(helper.parse_number_string("123456a78"))+ .to eql "123456a78"+ end+ end+ end++ context 'for an english local' do+ it 'parses a string with delimiter and separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("123,456.78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string with space delimiter and separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("123 456.78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string without delimiter and separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("12345678"))+ .to eql "12345678"+ end+ end++ it 'parses a string without delimiter and with separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("123456.78"))+ .to eql "123456.78"+ end+ end++ it 'parses a string with delimiter and without separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("12,345,678"))+ .to eql "12345678"+ end+ end++ it 'parses a string with space delimiter and without separator correctly' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("12 345 678"))+ .to eql "12345678"+ end+ end++ it 'returns alphabetical values instead of a delimiter unchanged' do+ I18n.with_locale(:en) do+ expect(helper.parse_number_string("123456a78"))+ .to eql "123456a78"+ end+ end+ end++ context 'for nil' do+ it 'is nil' do+ expect(helper.parse_number_string(nil))+ .to be_nil+ end+ end+ end+enddiff --git a/modules/reporting_engine/lib/report/operator.rb b/modules/reporting_engine/lib/report/operator.rbindex c60d9f0c255..93b65db9fd2 100644--- a/modules/reporting_engine/lib/report/operator.rb+++ b/modules/reporting_engine/lib/report/operator.rb@@ -175,7 +175,7 @@ class Report::Operatornew '=n', label: :label_equals dodef modify(query, field, value)- query.where "#{field} = #{clean_currency(value)}"+ query.where "#{field} = #{parse_number_string(value)}"queryendenddiff --git a/modules/reporting_engine/lib/report/query_utils.rb b/modules/reporting_engine/lib/report/query_utils.rbindex 0df36853f00..85f45ca490a 100644--- a/modules/reporting_engine/lib/report/query_utils.rb+++ b/modules/reporting_engine/lib/report/query_utils.rb@@ -35,6 +35,8 @@ module Report::QueryUtilsdelegate :quoted_false, :quoted_true, to: 'engine.reporting_connection'attr_writer :engine+ include Costs::NumberHelper+module PropagationHookinclude Report::QueryUtils@@ -168,17 +170,6 @@ module Report::QueryUtilsengine.send :sanitize_sql_for_conditions, statementend- ##- # FIXME: This is redmine- # Generates string representation for a currency.- #- # @see CostRate.clean_currency- # @param [BigDecimal] value- # @return [String]- def clean_currency(value)- CostRate.clean_currency(value).to_f.to_s- end-### Generates a SQL case statement.#