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 27, 2024
1 parent 11e18fa commit 0668d35
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 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,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

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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 0668d35

Please sign in to comment.