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.

40 lines
1.6 KiB

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