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 70d11b5d..5a14e293 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 @@ -66,6 +66,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( 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 31233a09..df2bbffc 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 0ffecd93..0a28dad9 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 @@ -31,6 +31,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" @@ -47,6 +56,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") @@ -71,6 +81,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") @@ -95,6 +106,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") @@ -119,6 +131,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") @@ -189,6 +202,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") @@ -219,6 +233,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") @@ -247,6 +262,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( @@ -282,6 +298,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( @@ -316,6 +333,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( @@ -345,3 +363,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 47f4eeab..3b84ba94 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 @@ -42,6 +42,14 @@ class AccountStatementImportSheetMappingWizard(models.TransientModel): amount_column = fields.Char( 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( 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')]}" /> +