Browse Source

Merge pull request #132 from akretion/10.0-qif-encoding-autodetec

[10.0] [FIX] Solve encoding issue and add AMEX support
pull/151/head
beau sebastien 7 years ago
committed by GitHub
parent
commit
426b3a439d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      account_bank_statement_import_qif/__manifest__.py
  2. 25
      account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py
  3. 1
      requirements.txt

2
account_bank_statement_import_qif/__manifest__.py

@ -8,7 +8,7 @@
{ {
'name': 'Import QIF Bank Statements', 'name': 'Import QIF Bank Statements',
'category': 'Accounting', 'category': 'Accounting',
'version': '10.0.1.0.0',
'version': '10.0.1.0.1',
'author': 'OpenERP SA,' 'author': 'OpenERP SA,'
'Tecnativa,' 'Tecnativa,'
'Odoo Community Association (OCA)', 'Odoo Community Association (OCA)',

25
account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py

@ -12,6 +12,15 @@ from odoo.tools.translate import _
from odoo import api, models from odoo import api, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
try:
import chardet
except (ImportError, IOError) as err:
chardet = False
_logger.debug(err)
class AccountBankStatementImport(models.TransientModel): class AccountBankStatementImport(models.TransientModel):
_inherit = "account.bank.statement.import" _inherit = "account.bank.statement.import"
@ -20,10 +29,21 @@ class AccountBankStatementImport(models.TransientModel):
def _check_qif(self, data_file): def _check_qif(self, data_file):
return data_file.strip().startswith('!Type:') return data_file.strip().startswith('!Type:')
def _get_qif_encoding(self, data_file):
if chardet:
return chardet.detect(data_file)['encoding']
else:
return u'utf-8'
def _parse_qif_date(self, date_str):
return dateutil.parser.parse(date_str, fuzzy=True).date()
def _parse_file(self, data_file): def _parse_file(self, data_file):
if not self._check_qif(data_file): if not self._check_qif(data_file):
return super(AccountBankStatementImport, self)._parse_file( return super(AccountBankStatementImport, self)._parse_file(
data_file) data_file)
encoding = self._get_qif_encoding(data_file)
data_file = data_file.decode(encoding)
try: try:
file_data = "" file_data = ""
for line in StringIO.StringIO(data_file).readlines(): for line in StringIO.StringIO(data_file).readlines():
@ -39,15 +59,14 @@ class AccountBankStatementImport(models.TransientModel):
transactions = [] transactions = []
vals_line = {} vals_line = {}
total = 0 total = 0
if header == "Bank":
if header in ("Bank", "CCard"):
vals_bank_statement = {} vals_bank_statement = {}
for line in data_list: for line in data_list:
line = line.strip() line = line.strip()
if not line: if not line:
continue continue
if line[0] == 'D': # date of transaction if line[0] == 'D': # date of transaction
vals_line['date'] = dateutil.parser.parse(
line[1:], fuzzy=True).date()
vals_line['date'] = self._parse_qif_date(line[1:])
elif line[0] == 'T': # Total amount elif line[0] == 'T': # Total amount
total += float(line[1:].replace(',', '')) total += float(line[1:].replace(',', ''))
vals_line['amount'] = float(line[1:].replace(',', '')) vals_line['amount'] = float(line[1:].replace(',', ''))

1
requirements.txt

@ -0,0 +1 @@
chardet
Loading…
Cancel
Save