diff --git a/app/services/report_parser.rb b/app/services/report_parser.rb index e035c3b..3cbed2b 100644 --- a/app/services/report_parser.rb +++ b/app/services/report_parser.rb @@ -98,7 +98,7 @@ class ReportParser { line_name: report_code.name, line_code: report_code.code, - value: source_value.presence || DEFAULT_MISSING_VALUE + value: normalized_statement_value(source_value) } end } @@ -149,6 +149,30 @@ class ReportParser value.to_s.scan(/\d+/).join.presence end + def normalized_statement_value(value) + return DEFAULT_MISSING_VALUE if value.nil? + return value if value.is_a?(Numeric) + + text = value.to_s.strip + return DEFAULT_MISSING_VALUE if text.blank? + + numeric_text?(text) ? text : DEFAULT_MISSING_VALUE + end + + def numeric_text?(text) + normalized = text.tr("\u00A0", ' ').squeeze(' ') + + integer_or_decimal = /\A[+-]?\d+(?:[.,]\d+)?\z/ + integer_or_decimal_grouped = /\A[+-]?\d{1,3}(?: \d{3})+(?:[.,]\d+)?\z/ + parenthesized_integer_or_decimal = /\A\(\d+(?:[.,]\d+)?\)\z/ + parenthesized_integer_or_decimal_grouped = /\A\(\d{1,3}(?: \d{3})+(?:[.,]\d+)?\)\z/ + + normalized.match?(integer_or_decimal) || + normalized.match?(integer_or_decimal_grouped) || + normalized.match?(parenthesized_integer_or_decimal) || + normalized.match?(parenthesized_integer_or_decimal_grouped) + end + def report_codes @report_codes ||= ReportCode.order(:code).select(:code, :name).to_a end