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.

41 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. _logger = logging.getLogger(__name__)
  10. class AccountBankStatementImport(models.TransientModel):
  11. """Add process_camt method to account.bank.statement.import."""
  12. _inherit = 'account.bank.statement.import'
  13. @api.model
  14. def _parse_file(self, data_file):
  15. """Parse a CAMT053 XML file."""
  16. try:
  17. parser = self.env['account.bank.statement.import.camt.parser']
  18. _logger.debug("Try parsing with camt.")
  19. return parser.parse(data_file)
  20. except ValueError:
  21. try:
  22. with zipfile.ZipFile(StringIO.StringIO(data_file)) as data:
  23. currency = None
  24. account_number = None
  25. transactions = []
  26. for member in data.namelist():
  27. currency, account_number, new = self._parse_file(
  28. data.open(member).read()
  29. )
  30. transactions.extend(new)
  31. return currency, account_number, transactions
  32. except (zipfile.BadZipfile, ValueError):
  33. pass
  34. # Not a camt file, returning super will call next candidate:
  35. _logger.debug("Statement file was not a camt file.",
  36. exc_info=True)
  37. return super(AccountBankStatementImport, self)._parse_file(data_file)