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.

66 lines
2.8 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.bank_statement_parse_mt940.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{1,50})'
  30. )
  31. def __init__(self):
  32. """Initialize parser - override at least header_regex."""
  33. super(MT940Parser, self).__init__()
  34. self.mt940_type = 'ING'
  35. def handle_tag_61(self, data):
  36. """get transaction values"""
  37. super(MT940Parser, self).handle_tag_61(data)
  38. re_61 = self.tag_61_regex.match(data)
  39. if not re_61:
  40. raise ValueError("Cannot parse %s" % data)
  41. parsed_data = re_61.groupdict()
  42. self.current_transaction.transferred_amount = (
  43. str2amount(parsed_data['sign'], parsed_data['amount']))
  44. self.current_transaction.eref = parsed_data['reference']
  45. def handle_tag_86(self, data):
  46. """Parse 86 tag containing reference data."""
  47. if not self.current_transaction:
  48. return
  49. codewords = ['RTRN', 'BENM', 'ORDP', 'CSID', 'BUSP', 'MARF', 'EREF',
  50. 'PREF', 'REMI', 'ID', 'PURP', 'ULTB', 'ULTD',
  51. 'CREF', 'IREF', 'CNTP', 'ULTC', 'EXCH', 'CHGS']
  52. subfields = get_subfields(data, codewords)
  53. transaction = self.current_transaction
  54. # If we have no subfields, set message to whole of data passed:
  55. if not subfields:
  56. transaction.message = data
  57. else:
  58. handle_common_subfields(transaction, subfields)
  59. # Prevent handling tag 86 later for non transaction details:
  60. self.current_transaction = None