Skip to content

Commit

Permalink
[ADD] field offset_footer in mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Amaral committed Jun 26, 2024
1 parent 11e18fa commit 0f2e817
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -157,7 +164,6 @@ class AccountStatementImportSheetMapping(models.Model):
string="Bank Account column",
help="Partner's bank account",
)

_sql_constraints = [
(
"check_amount_columns",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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

Check warning on line 365 in account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_txt_xlsx/models/account_statement_import_sheet_parser.py#L365

Added line #L365 was not covered by tests
values = list(row)
if mapping.skip_empty_lines and not any(values):
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
name="offset_row"
attrs="{'invisible': [('no_header', '=', True)]}"
/>
<field name="offset_footer" />
</group>
<group
attrs="{'invisible': [('debit_credit_column', '=', False)]}"
Expand Down

0 comments on commit 0f2e817

Please sign in to comment.