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..93cced737 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,14 +334,22 @@ 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")) + + label_line = mapping.offset_row + 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 lines = [] - for row in rows: + for index, row in enumerate(rows, label_line): if isinstance(csv_or_xlsx, tuple): book = csv_or_xlsx[0] sheet = csv_or_xlsx[1] @@ -343,6 +361,8 @@ def _parse_rows(self, mapping, currency_code, csv_or_xlsx, columns): # noqa: C9 cell_value = xldate_as_datetime(cell_value, book.datemode) values.append(cell_value) else: + if index >= footer_line: + continue values = list(row) if mapping.skip_empty_lines and not any(values): continue 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)]}" /> +