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.

68 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. """Implement BankStatementParser for MT940 IBAN ING files."""
  3. ##############################################################################
  4. #
  5. # Copyright (C) 2014-2015 Therp BV <http://therp.nl>.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import re
  22. from openerp.addons.account_bank_statement_import_mt940_base.mt940 import (
  23. MT940, str2amount, get_subfields, handle_common_subfields)
  24. class MT940Parser(MT940):
  25. """Parser for ing MT940 bank statement import files."""
  26. tag_61_regex = re.compile(
  27. r'^(?P<date>\d{6})(?P<line_date>\d{0,4})'
  28. r'(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})'
  29. r'(?P<reference>\w{0,16})'
  30. r'(//(?P<ingid>\w{14})/TRCD/(?P<ingtranscode>\w{0,34})){0,1}'
  31. )
  32. def __init__(self):
  33. """Initialize parser - override at least header_regex."""
  34. super(MT940Parser, self).__init__()
  35. self.mt940_type = 'ING'
  36. def handle_tag_61(self, data):
  37. """get transaction values"""
  38. super(MT940Parser, self).handle_tag_61(data)
  39. re_61 = self.tag_61_regex.match(data)
  40. if not re_61:
  41. raise ValueError("Cannot parse %s" % data)
  42. parsed_data = re_61.groupdict()
  43. self.current_transaction.transferred_amount = (
  44. str2amount(parsed_data['sign'], parsed_data['amount']))
  45. self.current_transaction.eref = parsed_data['reference']
  46. self.current_transaction.id = parsed_data['ingid']
  47. def handle_tag_86(self, data):
  48. """Parse 86 tag containing reference data."""
  49. if not self.current_transaction:
  50. return
  51. codewords = ['RTRN', 'BENM', 'ORDP', 'CSID', 'BUSP', 'MARF', 'EREF',
  52. 'PREF', 'REMI', 'ID', 'PURP', 'ULTB', 'ULTD',
  53. 'CREF', 'IREF', 'CNTP', 'ULTC', 'EXCH', 'CHGS']
  54. subfields = get_subfields(data, codewords)
  55. transaction = self.current_transaction
  56. # If we have no subfields, set message to whole of data passed:
  57. if not subfields:
  58. transaction.message = data
  59. else:
  60. handle_common_subfields(transaction, subfields)
  61. # Prevent handling tag 86 later for non transaction details:
  62. self.current_transaction = None