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.

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