You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """Add process_camt method to account.bank.statement.import."""
  3. # © 2013-2016 Therp BV <http://therp.nl>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import logging
  6. import StringIO
  7. import zipfile
  8. from odoo import api, models
  9. from ..camt import CamtParser as Parser
  10. _logger = logging.getLogger(__name__)
  11. class AccountBankStatementImport(models.TransientModel):
  12. """Add process_camt method to account.bank.statement.import."""
  13. _inherit = 'account.bank.statement.import'
  14. @api.model
  15. def _parse_file(self, data_file):
  16. """Parse a CAMT053 XML file."""
  17. try:
  18. parser = Parser()
  19. _logger.debug("Try parsing with camt.")
  20. return parser.parse(data_file)
  21. except ValueError:
  22. try:
  23. with zipfile.ZipFile(StringIO.StringIO(data_file)) as data:
  24. currency = None
  25. account_number = None
  26. transactions = []
  27. for member in data.namelist():
  28. currency, account_number, new = self._parse_file(
  29. data.open(member).read()
  30. )
  31. transactions.extend(new)
  32. return currency, account_number, transactions
  33. except (zipfile.BadZipfile, ValueError):
  34. pass
  35. # Not a camt file, returning super will call next candidate:
  36. _logger.debug("Statement file was not a camt file.",
  37. exc_info=True)
  38. return super(AccountBankStatementImport, self)._parse_file(data_file)