Skip to content

Commit

Permalink
TA#66666 [IMP] bank_statement_import_csv : handling comma decimal (#179)
Browse files Browse the repository at this point in the history
* TA#66666 [IMP] bank_statement_import_csv : parse decimal

---------

Co-authored-by: majouda <[email protected]>
  • Loading branch information
lanto-razafindrabe and majouda authored Jun 19, 2024
1 parent a011700 commit cae54e7
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bank_statement_import_csv/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Bank Statement Import CSV",
"version": "1.2.1",
"version": "1.2.2",
"author": "Numigi",
"maintainer": "Numigi",
"website": "https://numigi.com",
Expand Down
90 changes: 63 additions & 27 deletions bank_statement_import_csv/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0+e\n"
"Project-Id-Version: Odoo Server 14.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-12-09 19:41+0000\n"
"PO-Revision-Date: 2021-12-09 14:43-0500\n"
"Last-Translator: <>\n"
"POT-Creation-Date: 2024-06-19 16:30+0000\n"
"PO-Revision-Date: 2024-06-19 16:30+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
"X-Generator: Poedit 2.3\n"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__active
Expand Down Expand Up @@ -124,7 +122,7 @@ msgstr "Configuration"
#. module: bank_statement_import_csv
#: model_terms:ir.ui.view,arch_db:bank_statement_import_csv.bank_statement_import_wizard_form
msgid "Configuration"
msgstr "Configuration"
msgstr ""

#. module: bank_statement_import_csv
#: model_terms:ir.ui.view,arch_db:bank_statement_import_csv.bank_statement_import_wizard_form
Expand Down Expand Up @@ -173,7 +171,7 @@ msgstr "Colonne devise"
#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard_line__date
msgid "Date"
msgstr "Date"
msgstr ""

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__date_column
Expand All @@ -185,6 +183,26 @@ msgstr "Colonne date"
msgid "Date Format"
msgstr "Format de la date"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__decimal_separator
msgid "Decimal Separator"
msgstr "Séparateur de décimales"

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard.py:0
#, python-format
msgid ""
"Decimal separator and thousands separator must not be together as comma."
msgstr ""
"Le séparateur décimal et le séparateur de milliers ne doivent pas être "
"ensemble sous forme de virgule."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard.py:0
#, python-format
msgid "Decimal separator must not be empty or filled by spaces."
msgstr "Le séparateur décimal ne doit pas être vide ni rempli d'espaces."

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__delimiter
msgid "Delimiter"
Expand All @@ -201,6 +219,7 @@ msgid "Description"
msgstr "Libellé"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_account_journal__display_name
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__display_name
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard__display_name
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard_line__display_name
Expand Down Expand Up @@ -240,11 +259,12 @@ msgid "Has Error"
msgstr "As des erreurs"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_account_journal__id
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__id
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard__id
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard_line__id
msgid "ID"
msgstr "ID"
msgstr ""

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard__is_ready
Expand All @@ -255,14 +275,15 @@ msgstr "Est prêt"
#: model:ir.model,name:bank_statement_import_csv.model_account_journal
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard__journal_id
msgid "Journal"
msgstr "Journal"
msgstr ""

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__description_column
msgid "Label Column"
msgstr "Colonne libellé"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_account_journal____last_update
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config____last_update
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard____last_update
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard_line____last_update
Expand Down Expand Up @@ -299,7 +320,7 @@ msgid "Name"
msgstr "Nom"

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard_line.py:90
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard_line.py:0
#, python-format
msgid "No active currency found with the code {}."
msgstr "Aucune devise active trouvée pour le code {}."
Expand Down Expand Up @@ -387,7 +408,6 @@ msgid "Statement"
msgstr "Relevé"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_account_bank_statement_import_journal_creation__statement_import_config_id
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_account_journal__statement_import_config_id
msgid "Statement Import Config"
msgstr "Configuration d'import de relevé"
Expand All @@ -403,13 +423,27 @@ msgid "The bank statement is ready to be created."
msgstr "Le relevé bancaire est prêt à être créé."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/loader.py:103
#: model:ir.model.fields,help:bank_statement_import_csv.field_bank_statement_import_config__decimal_separator
msgid ""
"The character used to separate the integer part from the decimal part of the"
" decimal value. By default is comma."
msgstr "Le caractère utilisé pour séparer la partie entière de la partie décimale du valeur décimale. Par défaut, c'est une virgule."

#. module: bank_statement_import_csv
#: model:ir.model.fields,help:bank_statement_import_csv.field_bank_statement_import_config__thousands_separator
msgid ""
"The character used to separate the thousands in decimal value.By default is "
"space."
msgstr "Le caractère utilisé pour séparer les milliers dans la valeur décimale. Par défaut, c'est l'espace."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/loader.py:0
#, python-format
msgid "The currrency is required when an amount in foreign currency is given."
msgstr ""
msgstr "L'unité de devise est requise lorsque le montant d'une devise étrangère est fourni."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard.py:101
#: code:addons/bank_statement_import_csv/wizard/bank_statement_import_wizard.py:0
#, python-format
msgid ""
"The encoding defined in the configuration ({encoding}) does not match the "
Expand All @@ -419,23 +453,23 @@ msgstr ""
"fichier sélectionné ({filename})."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/loader.py:81
#: code:addons/bank_statement_import_csv/loader.py:206
#: code:addons/bank_statement_import_csv/loader.py:0
#: code:addons/bank_statement_import_csv/loader.py:0
#, python-format
msgid "The given date ({date}) does not match the format {format}."
msgstr ""
msgstr "La date donnée ({date}) ne correspond pas au format {format}."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/loader.py:240
#: code:addons/bank_statement_import_csv/loader.py:0
#, python-format
msgid "The given value ({}) does not seem to be a valid curency code."
msgstr ""
msgstr "La valeur donnée ({}) ne semble pas être un code de devise valide."

#. module: bank_statement_import_csv
#: code:addons/bank_statement_import_csv/loader.py:225
#: code:addons/bank_statement_import_csv/loader.py:0
#, python-format
msgid "The given value ({}) does not seem to be a valid number."
msgstr ""
msgstr "La valeur donnée ({}) ne semble pas être un nombre valide."

#. module: bank_statement_import_csv
#: model:ir.model.fields,help:bank_statement_import_csv.field_bank_statement_import_config__first_row
Expand All @@ -459,6 +493,11 @@ msgstr ""
" - cp1252\n"
" - latin-1"

#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_config__thousands_separator
msgid "Thousands Separator"
msgstr "Séparateur des milliers"

#. module: bank_statement_import_csv
#: model_terms:ir.ui.view,arch_db:bank_statement_import_csv.bank_statement_import_wizard_form
msgid "Validate"
Expand All @@ -482,7 +521,4 @@ msgstr "Retrait / dépôt activé"
#. module: bank_statement_import_csv
#: model:ir.model.fields,field_description:bank_statement_import_csv.field_bank_statement_import_wizard_line__wizard_id
msgid "Wizard"
msgstr "Wizard"

#~ msgid "Bank Statement Line"
#~ msgstr "Ligne de relevé bancaire"
msgstr ""
24 changes: 18 additions & 6 deletions bank_statement_import_csv/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
from .error import BankStatementError

ZERO = Decimal("0")
LOCALE_MAP = {
(".", ""): "en_US",
(".", " "): "en_US",
(".", ","): "en_US",
(",", "."): "de",
(",", ""): "ru",
(",", " "): "ru",
}


class BankStatementLoader:
Expand All @@ -24,6 +32,9 @@ def __init__(self, config):
self._delimiter = config.get("delimiter", ",")
self._quotechar = config.get("quotechar")

self._decimal_separator = config.get("decimal_separator")
self._thousands_separator = config.get("thousands_separator")

self._date_index = self._get_index_of("date")
self._date_format = self._get_format_of("date")

Expand Down Expand Up @@ -174,7 +185,9 @@ def _get_balance(self, row):

def _get_cell_decimal(self, row, index):
amount_str = self._get_cell(row, index)
return parse_decimal_or_error(amount_str)
thousands_separator = self._thousands_separator
locale = LOCALE_MAP.get((self._decimal_separator, thousands_separator), "en_US")
return parse_decimal_or_error(amount_str, locale)

def _get_cell(self, row, index):
if index < len(row):
Expand Down Expand Up @@ -215,16 +228,15 @@ def _get_parse_date_error(str_date, format_):
)


def parse_decimal_or_error(value):
def parse_decimal_or_error(value, locale):
try:
return _parse_decimal(value)
return _parse_decimal(value, locale)
except NumberFormatError:
return _get_decimal_error(value)


def _parse_decimal(value):
value = value.replace(" ", "")
return parse_decimal(value) if value else None
def _parse_decimal(value, locale="en_US"):
return parse_decimal(value, locale) if value else None


def _get_decimal_error(value):
Expand Down
16 changes: 16 additions & 0 deletions bank_statement_import_csv/models/bank_statement_import_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,27 @@ class BankStatementImportConfig(models.Model):
currency_amount_column = fields.Integer()
currency_amount_enabled = fields.Boolean()

decimal_separator = fields.Char(
required=True,
default=",",
help=(
"The character used to separate the integer part "
"from the decimal part of the decimal value. By default is comma."
),
)
thousands_separator = fields.Char(
default=" ",
help=("The character used to separate the thousands in decimal value."
"By default is space."),
)

def get_csv_loader_config(self):
return {
"first_row_index": self.first_row - 1,
"delimiter": self.delimiter or None,
"quotechar": self.quotechar or None,
"decimal_separator": self.decimal_separator,
"thousands_separator": self.thousands_separator,
"date": {
"index": self.date_column - 1,
"format": self.date_format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<group>
<field name="first_row"/>
<field name="reversed_order"/>
<field name="decimal_separator"/>
<field name="thousands_separator"/>
</group>
<group>
<field name="encoding"/>
Expand Down Expand Up @@ -97,7 +99,7 @@
<field name="name"/>
<separator/>
<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
</search>
</search>
</field>
</record>

Expand Down
15 changes: 15 additions & 0 deletions bank_statement_import_csv/wizard/bank_statement_import_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ def _try_get_file_content(self):

def _get_loader(self):
config = self._get_csv_loader_config()
if not self.config_id.decimal_separator.strip():
# Field is already required but this avoid when calling function separately
raise ValidationError(
_("Decimal separator must not be empty or filled by spaces.")
)
if (
self.config_id.decimal_separator == ","
and self.config_id.thousands_separator == ","
):
raise ValidationError(
_(
"Decimal separator and thousands separator "
"must not be together as comma."
)
)
return BankStatementLoader(config)

def _get_csv_loader_config(self):
Expand Down

0 comments on commit cae54e7

Please sign in to comment.