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 a0a9e26fb..eba554d65 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 @@ -77,6 +77,13 @@ class AccountStatementImportSheetMapping(models.Model): default=0, help="Vertical spaces to ignore before starting to parse", ) + offset_footer = fields.Integer( + string="Footer lines skip count", + help="Set the Footer lines number." + "Used in some csv/xlsx file that integrate meta data in" + "last lines.", + default="0", + ) timestamp_column = fields.Char(string="Timestamp column", required=True) currency_column = fields.Char( string="Currency column", @@ -157,7 +164,6 @@ class AccountStatementImportSheetMapping(models.Model): string="Bank Account column", help="Partner's bank account", ) - _sql_constraints = [ ( "check_amount_columns", 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 1d56b6ab8..d4928163e 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 @@ -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 @@ -50,7 +51,10 @@ def parse_header(self, data_file, encoding, csv_options): data = StringIO(data_file.decode(encoding or "utf-8")) csv_data = reader(data, **csv_options) - return list(next(csv_data)) + csv_data_lst = list(csv_data) + header = [value.strip() for value in csv_data_lst[0]] + return header + # return list(next(csv_data)) @api.model def parse(self, data_file, mapping, filename): @@ -95,7 +99,11 @@ def parse(self, data_file, mapping, filename): 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: @@ -182,7 +190,9 @@ def _parse_lines(self, mapping, data_file, currency_code): columns[column_name] = self._get_column_indexes( header, column_name, mapping ) - return self._parse_rows(mapping, currency_code, csv_or_xlsx, columns) + return self._parse_rows( + mapping, currency_code, csv_or_xlsx, columns, data_file=data_file + ) def _get_values_from_column(self, values, columns, column_name): indexes = columns[column_name] @@ -324,9 +334,16 @@ def _decimal(column_name): line["bank_account"] = bank_account return line - def _parse_rows(self, mapping, currency_code, csv_or_xlsx, columns): # noqa: C901 + def _parse_rows(self, mapping, currency_code, csv_or_xlsx, columns, data_file=None): + # Get the numbers of rows of the file + if isinstance(csv_or_xlsx, tuple): + numrows = csv_or_xlsx[1].nrows + else: + numrows = len(str(data_file.strip()).split("\\n")) + + footer_line = numrows - mapping.offset_footer if isinstance(csv_or_xlsx, tuple): - rows = range(mapping.offset_row + 1, csv_or_xlsx[1].nrows) + rows = range(mapping.offset_row + 1, footer_line) else: rows = csv_or_xlsx diff --git a/account_statement_import_txt_xlsx/tests/fixtures/sample_statement_offsets.xlsx b/account_statement_import_txt_xlsx/tests/fixtures/sample_statement_offsets.xlsx index 2cfc77a8a..2e804dd4b 100644 Binary files a/account_statement_import_txt_xlsx/tests/fixtures/sample_statement_offsets.xlsx and b/account_statement_import_txt_xlsx/tests/fixtures/sample_statement_offsets.xlsx differ 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 685807378..59e28fa40 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 @@ -476,6 +476,7 @@ def test_offsets(self): { "offset_column": 1, "offset_row": 2, + "offset_footer": 3, } ) wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create( 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 0ab58c800..4dd85f87e 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 @@ -59,6 +59,7 @@ name="offset_row" attrs="{'invisible': [('no_header', '=', True)]}" /> +