Browse Source

[FIX] Make sure old style parse results are handled properly.

pull/36/head
Ronald Portier (Therp BV) 9 years ago
parent
commit
f5f892ffa9
  1. 1
      account_bank_statement_import/__init__.py
  2. 3
      account_bank_statement_import/__openerp__.py
  3. 2
      account_bank_statement_import/models/__init__.py
  4. 55
      account_bank_statement_import/models/account_bank_statement_import.py
  5. 2
      account_bank_statement_import/parserlib.py
  6. 1
      account_bank_statement_import_mt940_base/__init__.py
  7. 2
      account_bank_statement_import_mt940_base/__openerp__.py
  8. 1
      account_bank_statement_import_mt940_nl_ing/__init__.py
  9. 2
      account_bank_statement_import_mt940_nl_ing/__openerp__.py
  10. 4
      account_bank_statement_import_mt940_nl_ing/tests/__init__.py
  11. 2
      account_bank_statement_import_mt940_nl_rabo/__init__.py

1
account_bank_statement_import/__init__.py

@ -1 +1,2 @@
# -*- encoding: utf-8 -*-
from . import models

3
account_bank_statement_import/__openerp__.py

@ -1,8 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
{
'name': 'Account Bank Statement Import',
'category': 'Banking addons',
'version': '8.0.1.0.1',
'license': 'AGPL-3',
'author': 'OpenERP SA,'
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/bank-statement-import',

2
account_bank_statement_import/models/__init__.py

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
from . import res_partner_bank
from . import account_bank_statement_import

55
account_bank_statement_import/models/account_bank_statement_import.py

@ -7,13 +7,14 @@ from zipfile import ZipFile, BadZipfile # BadZipFile in Python >= 3.2
from openerp import api, models, fields
from openerp.tools.translate import _
from openerp.exceptions import Warning
from openerp.exceptions import Warning as UserError
_logger = logging.getLogger(__name__)
_logger = logging.getLogger(__name__) # pylint: disable=invalid-name
class AccountBankStatementLine(models.Model):
"""Extend model account.bank.statement.line."""
# pylint: disable=too-many-public-methods
_inherit = "account.bank.statement.line"
# Ensure transactions can be imported only once (if the import format
@ -29,6 +30,7 @@ class AccountBankStatementLine(models.Model):
class AccountBankStatementImport(models.TransientModel):
"""Extend model account.bank.statement."""
# pylint: disable=too-many-public-methods
_name = 'account.bank.statement.import'
_description = 'Import Bank Statement'
@ -37,6 +39,7 @@ class AccountBankStatementImport(models.TransientModel):
""" Return False if the journal_id can't be provided by the parsed
file and must be provided by the wizard.
See account_bank_statement_import_qif """
# pylint: disable=no-self-use
return True
journal_id = fields.Many2one(
@ -58,8 +61,10 @@ class AccountBankStatementImport(models.TransientModel):
go to reconciliation."""
self.ensure_one()
data_file = base64.b64decode(self.data_file)
# pylint: disable=protected-access
statement_ids, notifications = self.with_context(
active_id=self.id)._import_file(data_file)
active_id=self.id # pylint: disable=no-member
)._import_file(data_file)
# dispatch to reconciliation interface
action = self.env.ref(
'account.action_bank_reconcile_bank_statements')
@ -95,7 +100,16 @@ class AccountBankStatementImport(models.TransientModel):
# Actually we don't care wether all the files have the same
# format. Although unlikely you might mix mt940 and camt files
# in one zipfile.
statements += self._parse_file(import_file)
parse_result = self._parse_file(import_file)
# Check for old version result, with separate currency and account
if isinstance(parse_result, tuple) and len(parse_result) == 3:
(currency_code, account_number, new_statements) = parse_result
for stmt_vals in new_statements:
stmt_vals['currency_code'] = currency_code
stmt_vals['account_number'] = account_number
else:
new_statements = parse_result
statements += new_statements
return statements
@api.model
@ -104,15 +118,7 @@ class AccountBankStatementImport(models.TransientModel):
# The appropriate implementation module returns the required data
statement_ids = []
notifications = []
parse_result = self._parse_all_files(data_file)
# Check for old version result, with separate currency and account
if isinstance(parse_result, tuple) and len(parse_result) == 3:
(currency_code, account_number, statements) = parse_result
for stmt_vals in statements:
stmt_vals['currency_code'] = currency_code
stmt_vals['account_number'] = account_number
else:
statements = parse_result
statements = self._parse_all_files(data_file)
# Check raw data:
self._check_parsed_data(statements)
# Import all statements:
@ -123,7 +129,7 @@ class AccountBankStatementImport(models.TransientModel):
statement_ids.append(statement_id)
notifications.append(new_notifications)
if len(statement_ids) == 0:
raise Warning(_('You have already imported that file.'))
raise UserError(_('You have already imported that file.'))
return statement_ids, notifications
@api.model
@ -144,7 +150,7 @@ class AccountBankStatementImport(models.TransientModel):
journal_id = self._get_journal(currency_id, bank_account_id)
# By now journal and account_number must be known
if not journal_id:
raise Warning(_('Can not determine journal for import.'))
raise UserError(_('Can not determine journal for import.'))
# Prepare statement data to be used for bank statements creation
stmt_vals = self._complete_statement(
stmt_vals, journal_id, account_number)
@ -152,7 +158,9 @@ class AccountBankStatementImport(models.TransientModel):
return self._create_bank_statement(stmt_vals)
@api.model
def _parse_file(self, dummy_data_file):
def _parse_file(self, data_file):
# pylint: disable=no-self-use
# pylint: disable=unused-argument
""" Each module adding a file support must extends this method. It
processes the file if it can, returns super otherwise, resulting in a
chain of responsability.
@ -182,21 +190,22 @@ class AccountBankStatementImport(models.TransientModel):
-o 'partner_name': string
-o 'ref': string
"""
raise Warning(_(
raise UserError(_(
'Could not make sense of the given file.\n'
'Did you install the module to support this type of file?'
))
@api.model
def _check_parsed_data(self, statements):
# pylint: disable=no-self-use
""" Basic and structural verifications """
if len(statements) == 0:
raise Warning(_('This file doesn\'t contain any statement.'))
raise UserError(_('This file doesn\'t contain any statement.'))
for stmt_vals in statements:
if 'transactions' in stmt_vals and stmt_vals['transactions']:
return
# If we get here, no transaction was found:
raise Warning(_('This file doesn\'t contain any transaction.'))
raise UserError(_('This file doesn\'t contain any transaction.'))
@api.model
def _find_currency_id(self, currency_code):
@ -207,7 +216,7 @@ class AccountBankStatementImport(models.TransientModel):
if currency_ids:
return currency_ids[0].id
else:
raise Warning(_(
raise UserError(_(
'Statement has invalid currency code %s') % currency_code)
# if no currency_code is provided, we'll use the company currency
return self.env.user.company_id.currency_id.id
@ -234,7 +243,7 @@ class AccountBankStatementImport(models.TransientModel):
if journal_id:
if (bank_account.journal_id.id and
bank_account.journal_id.id != journal_id):
raise Warning(
raise UserError(
_('The account of this statement is linked to '
'another journal.'))
if not bank_account.journal_id.id:
@ -257,7 +266,7 @@ class AccountBankStatementImport(models.TransientModel):
currency_id,
journal_currency_id
)
raise Warning(_(
raise UserError(_(
'The currency of the bank statement is not '
'the same as the currency of the journal !'
))
@ -271,7 +280,7 @@ class AccountBankStatementImport(models.TransientModel):
currency_id,
company_currency_id
)
raise Warning(_(
raise UserError(_(
'The currency of the bank statement is not '
'the same as the company currency !'
))

2
account_bank_statement_import/parserlib.py

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
"""Classes and definitions used in parsing bank statements."""
##############################################################################
#

1
account_bank_statement_import_mt940_base/__init__.py

@ -1 +1,2 @@
# -*- coding: utf-8 -*-
from . import mt940

2
account_bank_statement_import_mt940_base/__openerp__.py

@ -27,5 +27,5 @@
'depends': [
'account_bank_statement_import',
],
'installable': True
'installable': True,
}

1
account_bank_statement_import_mt940_nl_ing/__init__.py

@ -1 +1,2 @@
# -*- coding: utf-8 -*-
from . import account_bank_statement_import

2
account_bank_statement_import_mt940_nl_ing/__openerp__.py

@ -30,5 +30,5 @@
'demo': [
'demo/demo_data.xml',
],
'installable': True
'installable': True,
}

4
account_bank_statement_import_mt940_nl_ing/tests/__init__.py

@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
"""Test import of bank statement for MT940 ING."""
##############################################################################
#
@ -6,8 +6,6 @@
#
# All other contributions are (C) by their respective contributors
#
# All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the

2
account_bank_statement_import_mt940_nl_rabo/__init__.py

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2014-2015 Therp BV (<http://therp.nl>).
# Copyright (C) 2014-2015 Therp BV <http://therp.nl>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as

Loading…
Cancel
Save