Skip to content

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

remove currency separator duplicity

ulferts379f0ed72cfa
diff --git a/modules/costs/app/controllers/cost_objects_controller.rb b/modules/costs/app/controllers/cost_objects_controller.rb
index 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 < ApplicationController
cost_type = CostType.where(id: params[:cost_type_id]).first
if 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_plural
else
diff --git a/modules/costs/app/controllers/cost_types_controller.rb b/modules/costs/app/controllers/cost_types_controller.rb
index 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 < ApplicationController
cr.valid_from = today
end
- rate.rate = clean_currency(params[:rate])
+ rate.rate = parse_number_string_to_number(params[:rate])
if rate.save
flash[: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.rb
index 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 < ApplicationController
hr.project = @project
hr.user = @user
hr.valid_from = today
- hr.rate = clean_currency(params[:rate])
+ hr.rate = parse_number_string_to_number(params[:rate])
end
if rate.save
diff --git a/modules/costs/app/helpers/costlog_helper.rb b/modules/costs/app/helpers/costlog_helper.rb
index a386daed7ad..49f63f9f278 100644
--- a/modules/costs/app/helpers/costlog_helper.rb
+++ b/modules/costs/app/helpers/costlog_helper.rb
@@ -51,19 +51,4 @@ module CostlogHelper
done = 100.0 - ((100.0 / pcts) * 100).round
progress_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)
- end
end
diff --git a/modules/costs/app/helpers/costs/number_helper.rb b/modules/costs/app/helpers/costs/number_helper.rb
new file mode 100644
index 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
+end
diff --git a/modules/costs/app/models/cost_entry.rb b/modules/costs/app/models/cost_entry.rb
index 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::Base
end
def overwritten_costs=(costs)
- write_attribute(:overwritten_costs, CostRate.clean_currency(costs))
+ write_attribute(:overwritten_costs, CostRate.parse_number_string_to_number(costs))
end
def 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 attributes
diff --git a/modules/costs/app/models/cost_type.rb b/modules/costs/app/models/cost_type.rb
index 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::Base
def 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)
end
end
@@ -84,7 +84,7 @@ class CostType < ActiveRecord::Base
has_rate = false
if attributes && attributes[:rate].present?
- attributes[:rate] = Rate.clean_currency(attributes[:rate])
+ attributes[:rate] = Rate.parse_number_string(attributes[:rate])
has_rate = true
end
diff --git a/modules/costs/app/models/rate.rb b/modules/costs/app/models/rate.rb
index a46772b7287..6f618d17985 100644
--- a/modules/costs/app/models/rate.rb
+++ b/modules/costs/app/models/rate.rb
@@ -42,15 +42,7 @@ class Rate < ActiveRecord::Base
include 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::NumberHelper
private
@@ -65,7 +57,7 @@ class Rate < ActiveRecord::Base
end
def update_costs
- entry_class = self.is_a?(HourlyRate) ? TimeEntry : CostEntry
+ entry_class = is_a?(HourlyRate) ? TimeEntry : CostEntry
entry_class.where(rate_id: id).each(&:update_costs!)
end
@@ -74,10 +66,10 @@ class Rate < ActiveRecord::Base
next_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)
end
diff --git a/modules/costs/app/models/variable_cost_object.rb b/modules/costs/app/models/variable_cost_object.rb
index 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 < CostObject
if User.current.allowed_to? :edit_cost_objects, material_budget_item.cost_object.project
if 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 = attributes
else
material_budget_items.delete(material_budget_item)
@@ -144,7 +144,7 @@ class VariableCostObject < CostObject
attributes = labor_budget_item_attributes[labor_budget_item.id.to_s]
if User.current.allowed_to? :edit_cost_objects, labor_budget_item.cost_object.project
if 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 = attributes
else
labor_budget_items.delete(labor_budget_item)
diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.yml
index 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.rb
index 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::NumberToCurrencyConverterPatch
module InstanceMethods
def i18n_opts
super.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']))
end
end
end
\ No newline at end of file
diff --git a/modules/costs/lib/open_project/costs/patches/user_patch.rb b/modules/costs/lib/open_project/costs/patches/user_patch.rb
index 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::UserPatch
return unless rate_attributes
rate_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::UserPatch
def 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 = attributes
else
# TODO: this is surprising
diff --git a/modules/costs/spec/helpers/costs/number_helper_spec.rb b/modules/costs/spec/helpers/costs/number_helper_spec.rb
new file mode 100644
index 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
+end
diff --git a/modules/reporting_engine/lib/report/operator.rb b/modules/reporting_engine/lib/report/operator.rb
index 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::Operator
new '=n', label: :label_equals do
def modify(query, field, value)
- query.where "#{field} = #{clean_currency(value)}"
+ query.where "#{field} = #{parse_number_string(value)}"
query
end
end
diff --git a/modules/reporting_engine/lib/report/query_utils.rb b/modules/reporting_engine/lib/report/query_utils.rb
index 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::QueryUtils
delegate :quoted_false, :quoted_true, to: 'engine.reporting_connection'
attr_writer :engine
+ include Costs::NumberHelper
+
module PropagationHook
include Report::QueryUtils
@@ -168,17 +170,6 @@ module Report::QueryUtils
engine.send :sanitize_sql_for_conditions, statement
end
- ##
- # 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.
#