mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[REF][account_statement_import_sheet_file] Remove decimal
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
import itertools
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
from collections.abc import Iterable
|
||||
from datetime import datetime
|
||||
@@ -84,8 +85,8 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
||||
balance_end = last_line["balance"]
|
||||
data.update(
|
||||
{
|
||||
"balance_start": float(balance_start),
|
||||
"balance_end_real": float(balance_end),
|
||||
"balance_start": balance_start,
|
||||
"balance_end_real": balance_end,
|
||||
}
|
||||
)
|
||||
transactions = list(
|
||||
@@ -337,14 +338,14 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
||||
balance = None
|
||||
|
||||
if debit_credit is not None:
|
||||
amount = amount.copy_abs()
|
||||
amount = abs(amount)
|
||||
if debit_credit == mapping.debit_value:
|
||||
amount = -amount
|
||||
|
||||
if original_amount:
|
||||
original_amount = self._parse_decimal(
|
||||
original_amount, mapping
|
||||
).copy_sign(amount)
|
||||
original_amount = math.copysign(
|
||||
self._parse_decimal(original_amount, mapping), amount
|
||||
)
|
||||
else:
|
||||
original_amount = 0.0
|
||||
if mapping.amount_inverse_sign:
|
||||
@@ -458,9 +459,9 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
||||
@api.model
|
||||
def _parse_decimal(self, value, mapping):
|
||||
if isinstance(value, Decimal):
|
||||
return value
|
||||
return float(value)
|
||||
elif isinstance(value, float):
|
||||
return Decimal(str(value))
|
||||
return value
|
||||
thousands, decimal = mapping._get_float_separators()
|
||||
# Remove all characters except digits, thousands separator,
|
||||
# decimal separator, and signs
|
||||
@@ -472,4 +473,4 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
||||
)
|
||||
value = value.replace(thousands, "")
|
||||
value = value.replace(decimal, ".")
|
||||
return Decimal(value)
|
||||
return float(value)
|
||||
|
||||
@@ -600,55 +600,55 @@ class TestAccountStatementImportSheetFile(common.TransactionCase):
|
||||
test_cases = [
|
||||
(
|
||||
"1,234.56",
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # standard case with thousands separator
|
||||
(
|
||||
"1,234,567.89",
|
||||
Decimal("1234567.89"),
|
||||
1234567.89,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # multiple thousands separators
|
||||
(
|
||||
"-1,234.56",
|
||||
Decimal("-1234.56"),
|
||||
-1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # negative value
|
||||
(
|
||||
"$1,234.56",
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # prefixed with currency symbol
|
||||
(
|
||||
"1,234.56 USD",
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # suffixed with currency code
|
||||
(
|
||||
" 1,234.56 ",
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # leading and trailing spaces
|
||||
(
|
||||
"not a number",
|
||||
Decimal("0"),
|
||||
0,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # non-numeric input
|
||||
(" ", Decimal("0"), self.mock_mapping_comma_dot), # empty string
|
||||
("", Decimal("0"), self.mock_mapping_comma_dot), # empty space
|
||||
("USD", Decimal("0"), self.mock_mapping_comma_dot), # empty dolar
|
||||
(" ", 0, self.mock_mapping_comma_dot), # empty string
|
||||
("", 0, self.mock_mapping_comma_dot), # empty space
|
||||
("USD", 0, self.mock_mapping_comma_dot), # empty dolar
|
||||
(
|
||||
"12,34.56",
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # unusual thousand separator placement
|
||||
(
|
||||
"1234,567.89",
|
||||
Decimal("1234567.89"),
|
||||
1234567.89,
|
||||
self.mock_mapping_comma_dot,
|
||||
), # missing one separator
|
||||
(
|
||||
"1234.567,89",
|
||||
Decimal("1234567.89"),
|
||||
1234567.89,
|
||||
self.mock_mapping_dot_comma,
|
||||
), # inverted separators
|
||||
]
|
||||
@@ -660,21 +660,21 @@ class TestAccountStatementImportSheetFile(common.TransactionCase):
|
||||
|
||||
def test_decimal_and_float_inputs(self):
|
||||
# Test direct Decimal and float inputs
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(
|
||||
Decimal("-1234.56"), self.mock_mapping_comma_dot
|
||||
),
|
||||
Decimal("-1234.56"),
|
||||
)
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(Decimal("1234.56"), self.mock_mapping_comma_dot),
|
||||
Decimal("1234.56"),
|
||||
)
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(-1234.56, self.mock_mapping_comma_dot),
|
||||
Decimal("-1234.56"),
|
||||
-1234.56,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(1234.56, self.mock_mapping_comma_dot),
|
||||
Decimal("1234.56"),
|
||||
1234.56,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(
|
||||
Decimal("-1234.56"), self.mock_mapping_comma_dot
|
||||
),
|
||||
-1234.56,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.parser._parse_decimal(Decimal("1234.56"), self.mock_mapping_comma_dot),
|
||||
1234.56,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user