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.

69 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. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as published
  10. # by the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import re
  23. from openerp.addons.bank_statement_parse_mt940.mt940 import (
  24. MT940, str2amount, get_subfields, handle_common_subfields)
  25. class MT940Parser(MT940):
  26. """Parser for ing MT940 bank statement import files."""
  27. tag_61_regex = re.compile(
  28. r'^(?P<date>\d{6})(?P<line_date>\d{0,4})'
  29. r'(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})'
  30. r'(?P<reference>\w{1,50})'
  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. def handle_tag_86(self, data):
  47. """Parse 86 tag containing reference data."""
  48. if not self.current_transaction:
  49. return
  50. codewords = ['RTRN', 'BENM', 'ORDP', 'CSID', 'BUSP', 'MARF', 'EREF',
  51. 'PREF', 'REMI', 'ID', 'PURP', 'ULTB', 'ULTD',
  52. 'CREF', 'IREF', 'CNTP', 'ULTC', 'EXCH', 'CHGS']
  53. subfields = get_subfields(data, codewords)
  54. transaction = self.current_transaction
  55. # If we have no subfields, set message to whole of data passed:
  56. if not subfields:
  57. transaction.message = data
  58. else:
  59. handle_common_subfields(transaction, subfields)
  60. # Prevent handling tag 86 later for non transaction details:
  61. self.current_transaction = None
  62. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: