From eecfd33eadcb91bc05f09e0093c57b9a07b7f4a9 Mon Sep 17 00:00:00 2001 From: Henrik Norlin Date: Sun, 3 Apr 2022 15:06:07 +0200 Subject: [PATCH] [IMP] account_statement_import_txt_xlsx: wizard: 2 amount columns, for IN and OUT [IMP] account_statement_import_txt_xlsx: wizard: amount2_reverse boolean field, with tests --- .../account_statement_import_sheet_mapping.py | 8 +++ .../account_statement_import_sheet_parser.py | 12 +++- .../tests/fixtures/amount2.csv | 3 + .../test_account_statement_import_txt_xlsx.py | 55 +++++++++++++++++++ ...account_statement_import_sheet_mapping.xml | 5 ++ ...t_statement_import_sheet_mapping_wizard.py | 8 +++ ..._statement_import_sheet_mapping_wizard.xml | 6 ++ 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 account_statement_import_txt_xlsx/tests/fixtures/amount2.csv diff --git a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_mapping.py b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_mapping.py index 4d7e7b57..f8bba48e 100644 --- a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_mapping.py +++ b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_mapping.py @@ -69,6 +69,14 @@ class AccountStatementImportSheetMapping(models.Model): required=True, help="Amount of transaction in journal's currency", ) + amount2_column = fields.Char( + string="Amount2 column", + help="Some statements have two amount columns, for IN and OUT", + ) + amount2_reverse = fields.Boolean( + string="Amount2 reverse +/-", + help="If there are positive numbers for money going OUT, reverse +/-", + ) balance_column = fields.Char( string="Balance column", help="Balance after transaction in journal's currency", diff --git a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py index 4255e000..b8a37536 100644 --- a/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py +++ b/account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py @@ -113,6 +113,9 @@ class AccountStatementImportSheetParser(models.TransientModel): header.index(mapping.currency_column) if mapping.currency_column else None ) columns["amount_column"] = header.index(mapping.amount_column) + columns["amount2_column"] = ( + header.index(mapping.amount2_column) if mapping.amount2_column else None + ) columns["balance_column"] = ( header.index(mapping.balance_column) if mapping.balance_column else None ) @@ -189,7 +192,13 @@ class AccountStatementImportSheetParser(models.TransientModel): if columns["currency_column"] is not None else currency_code ) - amount = values[columns["amount_column"]] + factor = -1 if mapping["amount2_reverse"] else 1 + if values[columns["amount_column"]] in (False, "", "0.00"): + amount = values[columns["amount2_column"]] + amount = factor * self._parse_decimal(amount, mapping) + else: + amount = values[columns.get("amount_column")] + amount = self._parse_decimal(amount, mapping) balance = ( values[columns["balance_column"]] if columns["balance_column"] is not None @@ -252,7 +261,6 @@ class AccountStatementImportSheetParser(models.TransientModel): if isinstance(timestamp, str): timestamp = datetime.strptime(timestamp, mapping.timestamp_format) - amount = self._parse_decimal(amount, mapping) if balance: balance = self._parse_decimal(balance, mapping) else: diff --git a/account_statement_import_txt_xlsx/tests/fixtures/amount2.csv b/account_statement_import_txt_xlsx/tests/fixtures/amount2.csv new file mode 100644 index 00000000..9bff8eec --- /dev/null +++ b/account_statement_import_txt_xlsx/tests/fixtures/amount2.csv @@ -0,0 +1,3 @@ +"Date","Label","Amount","Amount2","Balance","Partner Name","Bank Account" +"12/15/2018","Your best supplier","0.00","33.50","-23.50","John Doe","123456789" +"12/15/2018","Your payment","1,533.50","0.00","1,510.00","Azure Interior","" diff --git a/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py b/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py index 9236b16f..9bab4854 100644 --- a/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py +++ b/account_statement_import_txt_xlsx/tests/test_account_statement_import_txt_xlsx.py @@ -29,6 +29,15 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): self.AccountStatementImportSheetMappingWizard = self.env[ "account.statement.import.sheet.mapping.wizard" ] + self.suspense_account = self.env["account.account"].create( + { + "code": "987654", + "name": "Suspense Account", + "user_type_id": self.env.ref( + "account.data_account_type_current_assets" + ).id, + } + ) def _data_file(self, filename, encoding=None): mode = "rt" if encoding else "rb" @@ -45,6 +54,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/sample_statement_en.csv", "utf-8") @@ -69,6 +79,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/empty_statement_en.csv", "utf-8") @@ -93,6 +104,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/sample_statement_en.xlsx") @@ -117,6 +129,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/empty_statement_en.xlsx") @@ -187,6 +200,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/original_currency.csv", "utf-8") @@ -217,6 +231,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) data = self._data_file("fixtures/original_currency_empty.csv", "utf-8") @@ -245,6 +260,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) statement_map = self.sample_statement_map.copy( @@ -280,6 +296,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) statement_map = self.sample_statement_map.copy( @@ -314,6 +331,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): "type": "bank", "code": "BANK", "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, } ) statement_map = self.sample_statement_map.copy( @@ -343,3 +361,40 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase): self.assertEqual(statement.balance_start, 10.0) self.assertEqual(statement.balance_end_real, 1510.0) self.assertEqual(statement.balance_end, 1510.0) + + def test_amount2(self): + journal = self.AccountJournal.create( + { + "name": "Bank", + "type": "bank", + "code": "BANK", + "currency_id": self.currency_usd.id, + "suspense_account_id": self.suspense_account.id, + } + ) + statement_map = self.sample_statement_map.copy( + { + "amount2_column": "Amount2", + "amount2_reverse": True, + "balance_column": "Balance", + "original_currency_column": None, + "original_amount_column": None, + } + ) + data = self._data_file("fixtures/amount2.csv", "utf-8") + wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create( + { + "statement_filename": "fixtures/amount2.csv", + "statement_file": data, + "sheet_mapping_id": statement_map.id, + } + ) + wizard.with_context( + account_statement_import_txt_xlsx_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) + self.assertEqual(statement.balance_start, 10.0) + self.assertEqual(statement.balance_end_real, 1510.0) + self.assertEqual(statement.balance_end, 1510.0) diff --git a/account_statement_import_txt_xlsx/views/account_statement_import_sheet_mapping.xml b/account_statement_import_txt_xlsx/views/account_statement_import_sheet_mapping.xml index 8f9718b5..9981063b 100644 --- a/account_statement_import_txt_xlsx/views/account_statement_import_sheet_mapping.xml +++ b/account_statement_import_txt_xlsx/views/account_statement_import_sheet_mapping.xml @@ -38,6 +38,10 @@ + + diff --git a/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.py b/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.py index 2e38de4b..d1b86a3f 100644 --- a/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.py +++ b/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.py @@ -47,6 +47,14 @@ class AccountStatementImportSheetMappingWizard(models.TransientModel): string="Amount column", help="Amount of transaction in journal's currency", ) + amount2_column = fields.Char( + string="Amount2 column", + help="Some statements have two amount columns, for IN and OUT", + ) + amount2_reverse = fields.Boolean( + string="Amount2 reverse +/-", + help="If there are positive numbers for money going OUT, reverse +/-", + ) balance_column = fields.Char( string="Balance column", help="Balance after transaction in journal's currency", diff --git a/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.xml b/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.xml index 51097816..9ebadc38 100644 --- a/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.xml +++ b/account_statement_import_txt_xlsx/wizards/account_statement_import_sheet_mapping_wizard.xml @@ -52,6 +52,12 @@ context="{'header': header}" attrs="{'required': [('state', '=', 'final')]}" /> +