From 894c42c0cbdf2e2cf23a154319fb4a90f838eb64 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Wed, 31 Jan 2024 09:55:38 +0100 Subject: [PATCH] [IMP] account_statement_import_sheet_file: add field amount_inverse_sign_column. In some cases such as in credit card statements the amounts are expressed in the inverse sign. By setting this flag during the upload the amounts will be inverted in sign. --- .../account_statement_import_sheet_mapping.py | 7 ++++ .../account_statement_import_sheet_parser.py | 24 ++++++++++++-- ..._statement_credit_card_inverse_sign_en.csv | 3 ++ ...est_account_statement_import_sheet_file.py | 32 +++++++++++++++++++ ...account_statement_import_sheet_mapping.xml | 2 ++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 account_statement_import_sheet_file/tests/fixtures/sample_statement_credit_card_inverse_sign_en.csv diff --git a/account_statement_import_sheet_file/models/account_statement_import_sheet_mapping.py b/account_statement_import_sheet_file/models/account_statement_import_sheet_mapping.py index fd43a9ed..5782f784 100644 --- a/account_statement_import_sheet_file/models/account_statement_import_sheet_mapping.py +++ b/account_statement_import_sheet_file/models/account_statement_import_sheet_mapping.py @@ -79,6 +79,13 @@ class AccountStatementImportSheetMapping(models.Model): string="Credit amount column", help="Credit amount of transaction in journal's currency", ) + amount_inverse_sign = fields.Boolean( + string="Inverse sign of amount", + help="In some cases such as in credit card statements the " + "amounts are expressed in the inverse sign. " + "By setting this flag during the upload the amounts " + "will be inverted in sign.", + ) balance_column = fields.Char( help="Balance after transaction in journal's currency", ) diff --git a/account_statement_import_sheet_file/models/account_statement_import_sheet_parser.py b/account_statement_import_sheet_file/models/account_statement_import_sheet_parser.py index c652e010..2be2f260 100644 --- a/account_statement_import_sheet_file/models/account_statement_import_sheet_parser.py +++ b/account_statement_import_sheet_file/models/account_statement_import_sheet_parser.py @@ -4,6 +4,7 @@ import itertools import logging +from collections.abc import Iterable from datetime import datetime from decimal import Decimal from io import StringIO @@ -97,7 +98,11 @@ class AccountStatementImportSheetParser(models.TransientModel): def _get_column_indexes(self, header, column_name, mapping): column_indexes = [] - if mapping[column_name] and "," in mapping[column_name]: + if ( + mapping[column_name] + and isinstance(mapping[column_name], Iterable) + and "," in mapping[column_name] + ): # We have to concatenate the values column_names_or_indexes = mapping[column_name].split(",") else: @@ -320,6 +325,18 @@ class AccountStatementImportSheetParser(models.TransientModel): else None ) + debit_column = ( + self._get_values_from_column(values, columns, "amount_debit_column") + if columns["amount_debit_column"] + else None + ) + + credit_column = ( + self._get_values_from_column(values, columns, "amount_credit_column") + if columns["amount_credit_column"] + else None + ) + if currency != currency_code: continue @@ -342,7 +359,10 @@ class AccountStatementImportSheetParser(models.TransientModel): ).copy_sign(amount) else: original_amount = 0.0 - + if mapping.amount_inverse_sign: + amount = -amount + original_amount = -original_amount + balance = -balance if balance is not None else balance line = { "timestamp": timestamp, "amount": amount, diff --git a/account_statement_import_sheet_file/tests/fixtures/sample_statement_credit_card_inverse_sign_en.csv b/account_statement_import_sheet_file/tests/fixtures/sample_statement_credit_card_inverse_sign_en.csv new file mode 100644 index 00000000..282e5f9a --- /dev/null +++ b/account_statement_import_sheet_file/tests/fixtures/sample_statement_credit_card_inverse_sign_en.csv @@ -0,0 +1,3 @@ +"Date","Label","Currency","Amount","Amount Currency","Partner Name","Bank Account" +"12/15/2018","LABEL 1","USD","33.50","0.0","John Doe","123456789" +"12/15/2018","LABEL 2","EUR","-1,525.00","-1,000.00","Azure Interior","" diff --git a/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py b/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py index 13b1fb82..08caf065 100644 --- a/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py +++ b/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py @@ -506,3 +506,35 @@ class TestAccountStatementImportSheetFile(common.TransactionCase): line4 = statement.line_ids.filtered(lambda x: x.payment_ref == "LABEL 4") self.assertEqual(line1.amount, 50) self.assertEqual(line4.amount, -1300) + + def test_amount_inverse_sign(self): + self.sample_statement_map.amount_inverse_sign = True + journal = self.AccountJournal.create( + { + "name": "Bank", + "type": "bank", + "code": "BANK", + "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, + } + ) + filename = "fixtures/sample_statement_credit_card_inverse_sign_en.csv" + data = self._data_file(filename, "utf-8") + wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create( + { + "statement_filename": filename, + "statement_file": data, + "sheet_mapping_id": self.sample_statement_map.id, + } + ) + wizard.with_context( + account_statement_import_sheet_file_test=True + ).import_file_button() + statement = self.AccountBankStatement.search([("journal_id", "=", journal.id)]) + self.assertEqual(len(statement), 1) + self.assertEqual(len(statement.line_ids), 2) + line1 = statement.line_ids.filtered(lambda x: x.payment_ref == "LABEL 1") + self.assertEqual(line1.amount, -33.50) + line2 = statement.line_ids.filtered(lambda x: x.payment_ref == "LABEL 2") + self.assertEqual(line2.amount, 1525.00) + self.assertEqual(line2.amount_currency, 1000.00) diff --git a/account_statement_import_sheet_file/views/account_statement_import_sheet_mapping.xml b/account_statement_import_sheet_file/views/account_statement_import_sheet_mapping.xml index 3165de06..49f34bc9 100644 --- a/account_statement_import_sheet_file/views/account_statement_import_sheet_mapping.xml +++ b/account_statement_import_sheet_file/views/account_statement_import_sheet_mapping.xml @@ -89,6 +89,7 @@ 'invisible': [('amount_type', '=', 'distinct_credit_debit')], }" /> + +